Skip to content

perf(engine): TierPlanner prices Gemma 4 KV at uniform full-context fp32 — runtime uses SWA rings, KV-share aliasing and per-layer head_dim, so auto -g/ctx underestimates capacity ~4-6× #220

Description

@pekkah

Problem

TierPlanner.Plan prices GPU KV uniformly: 2 × gpuLayers × NumKvHeads × HeadDim × sizeof(float) × ctx for every layer (TierPlanner.cs:97-126). The Gemma 4 runtime allocates nothing like that:

Net effect: for Gemma 4 the planner overestimates per-token KV cost by roughly 4–6× (more with narrowed KV). On a 12 GB card with -g -1, that means fewer GPU layers and/or a much smaller RecommendedCtxSize than what actually fits — the loader's ClampGemma4KvShareBoundary (InferenceEngineLoader.cs:216) already patches one Gemma-4 blind spot in the planner, and this is the rest of the family.

This matters most for the hybrid configs (Gemma 4 12B-class on 12 GB, where every reclaimed GB is another GPU layer) and for auto-context on E4B long-context runs.

Proposed work

  1. Teach TierPlanner the per-layer KV shape: when hp.IsSwaLayer is populated, price SWA layers at SwaRingSize(ctx, hp.SlidingWindowSize) positions; skip KV-share layers (hp.KvSourceLayer[L] >= 0); use per-layer head_dim where the hyperparams carry it. Reuse the same helpers CudaForwardPass uses so the two can't drift (single source of truth for ring sizing).
  2. Accept the resolved KV dtype (already proposed in perf(cuda,engine): CPU-MoE mode + expert-aware TierPlanner for CudaHybridForwardPass — auto placement is structurally wrong for pure-attention MoE on 12 GB #215 item 3) so bf16/q8_0 runs budget correctly.
  3. Re-derive RecommendedCtxSize from the corrected per-token cost.

Acceptance

  • A planner unit test (GGUF-free, cf. Test coverage: GGUF-free regression tests for k_eq_v detection + TierPlanner packed sizing (PR #174 follow-up) #175's pattern) with a synthetic Gemma-4-shaped ModelHyperparams (SWA pattern + KV-share + per-layer head_dim) asserting planned GpuKvBytes matches the runtime allocator's arithmetic.
  • On a 12 GB profile, Gemma 4 12B -g -1 gains GPU layers and/or context vs today's plan; no change for uniform-attention models.
  • No OOM regression: validate the plan's headroom on an actual long-context run (the planner must not under-reserve once SWA-aware).

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions