Skip to content

models(transformer): wire Gemma 3 per-layer sliding window + attn/final softcaps + QPAS #274

Description

@jamesburton

Summary

Wires four Gemma-family ModelConfig fields through the dense CPU forward path in TransformerModel (both RunLayersAndFinalNormCore and the batched RunLayersAndFinalNormBatched), completing the CPU-side Gemma 3 forward implementation on top of the architecture detection / config plumbing landed in #208 and the CPU Attention.Execute softCap parameter landed in #255.

What gets wired

  1. PerLayerSlidingWindow[layer] — per-layer override for the sliding-window mask. Gemma 3's sliding_window_pattern produces an interleaved local/global pattern (Gemma 3 ships a 5:1 local:global ratio; e.g. with pattern=2, layers 0/2 are sliding, 1/3 are full). A new GetLayerSlidingWindow(int) helper resolves per-layer-override (with null for full attention) and falls back to the model-wide SlidingWindowSize. No-op on every architecture without the per-layer field set.

  2. AttnLogitSoftcap — passed as the new softCap argument to Attention.Execute so the post-scale soft-cap fires in the CPU kernel exactly as in the Vulkan FA shader (attention_flash_f32.comp). Gemma 2 ships 50.0; Gemma 3 leaves the field null but the plumbing is wired regardless.

  3. FinalLogitSoftcap — applied in-place via a new ApplyFinalLogitSoftcap helper after RunLmHead's GEMM and before the result-tensor copy; mirrored in the batched lm_head path inside ForwardBatch. Uses TensorPrimitives.Multiply + TensorPrimitives.Tanh for the SIMD-accelerated kernel.

  4. QueryPreAttnScalar (QPAS) — overrides the default 1/sqrt(headDim) attention scale with 1/sqrt(QueryPreAttnScalar) when non-null. Routed via the existing scale-providing Attention.Execute overloads. Gemma 3 ships 256.

All four mechanisms are no-ops on every existing architecture (default field values are null), so this is purely additive plumbing.

Dependencies

Test plan

  • dotnet build src/DotLLM.Models/DotLLM.Models.csproj is clean.
  • dotnet build tests/DotLLM.Tests.Unit/DotLLM.Tests.Unit.csproj is clean.
  • Existing Gemma / Transformer tests still pass.
  • End-to-end synthetic Gemma 3 forward-pass coverage follows in a separate PR stacked on this one.

Files touched

  • src/DotLLM.Models/Architectures/TransformerModel.cs (+74/-8)

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions