Skip to content

feat(server meta): expose SWA geometry (n_swa + SWA-layer count) for exact gemma-4 KV prediction #123

Description

@marksverdhei

Extends #119 (which added n_layer/n_head_kv/n_embd_k_gqa/n_embd_v_gqa/cache_type_{k,v} to /v1/models meta). Those let a client compute KV/token assuming all layers scale with context — correct for dense models, but a 2–8× over-estimate for SWA-hybrid models (gemma-4 12B/31B, gemma-4-12b-256k@gem), where most layers cap KV at a small sliding window and only a few full-attention layers scale with ctx.

heierchat’s VRAM panel currently uses the all-layers value as a safe upper bound (errs toward over-VRAM, never under-predicts an OOM) labeled . To make gemma-4 exact the FE needs the SWA split, which llama.cpp already knows.

Proposed additive meta fields (per loaded model, same path as #119)

field source example (gemma-4-12b)
n_swa llama_model_n_swa(model) (already public) 1024
n_swa_layers new accessor llama_model_n_swa_layers(model) = count of il where hparams.is_swa(il) e.g. 40
swa_type hparams.swa_type as string (none/standard/chunked) standard

n_full_layers = n_layer − n_swa_layers is derived on the FE.

Exact KV/token the FE will then compute

kv_per_tok(ctx) = (k_gqa + v_gqa) * kv_type_bytes * (
                    n_full_layers
                  + n_swa_layers * min(ctx, n_swa) / ctx )

i.e. full layers scale with ctx; SWA layers saturate at min(ctx, n_swa).

Implementation sketch (our fork — clean, mirrors #119)

  1. src/llama-model.cpp + include/llama.h: add LLAMA_API int32_t llama_model_n_swa_layers(const llama_model * model) next to the existing llama_model_n_swa — loop il in [0, n_layer) counting hparams.is_swa(il). (n_swa, is_swa, is_swa_any already exist in src/llama-hparams.h.)
  2. tools/server/server-context.h: add int32_t model_n_swa; int32_t model_n_swa_layers; std::string swa_type; to server_context_meta.
  3. tools/server/server-context.cpp: populate them where feat(server): expose KV-cache geometry + quant in /v1/models meta #119 populated n_layer etc.; emit n_swa, n_swa_layers, swa_type in the /v1/models meta JSON.

Downstream (tracked separately, snoop-kube’s lane)

Impact

None until rolled; purely additive meta. Makes the gemma-4 VRAM verdict exact instead of a safe upper bound. Related: #119, #122. FE side queued by heierchat ("Exact KV for SWA-hybrid models in the VRAM panel").

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions