Skip to content

config: serialize num_cache_layers so consumers can read the switch layer budget #149

Open

Description

@planetf1

Title: config: serialize num_cache_layers so consumers can read the switch layer budget

The llama.cpp GGUF converter recently had to be updated for switch's layer accounting (by me in ggml-org/llama.cpp#30617, fixed in ggml-org/llama.cpp#30621) by deriving the decoder-layer count through parsing model.layers.N tensor names. That count was not available in config.json.

Suggestion

As @gabe-l-hart mentioned when closing the llama.cpp thread, serialize the existing SWITCH_CACHE_LAYERS constant as a config field:

# GraniteSwitchConfig.__init__
num_cache_layers: int = SWITCH_CACHE_LAYERS,   # new kwarg, defaulted to the existing constant
...
self.num_cache_layers = num_cache_layers

save_pretrained() picks up attributes set in __init__, so newly composed checkpoints would carry "num_cache_layers": 2 in config.json without any composer changes. The kwarg form (rather than hardcoding the constant in the body) lets a per-checkpoint value round-trip naturally through save_pretrained/from_dict if the engine's slot count ever varies.

Why this is safe

  • No behavioural change; the modeling can keep using switch.num_cache_layers as-is, and read the config field later if we want them in lockstep.
  • Old configs lack the field and fall back to the default of 2, which matches every loadable checkpoint: from_dict already rejects SingleSwitch-era configs, so anything that loads is MultiSwitch with exactly two slots.
  • New configs loaded by older package versions just carry an unknown extra key, which PretrainedConfig tolerates.

The llama.cpp converter can then prefer the field over tensor-name parsing (via a small additional llama.cpp PR which would keep the existing logic as fallback for models without it).

We could also use a different config name if it's better, such as a direct num_decoder_layers count instead of the slot count.

Happy to create a PR for switch and the follow-on in llama.cpp if that helps.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions