8000 Add Qwen3.5 hybrid architecture and Gated DeltaNet linear attention support by janpfeifer · Pull Request #502 · gomlx/gomlx · GitHub
[go: up one dir, main page]

Skip to content

Add Qwen3.5 hybrid architecture and Gated DeltaNet linear attention support - #502

Merged
janpfeifer merged 2 commits into
mainfrom
add-qwen3.5
Nov 25, 2026
Merged

janpfeifer merged 2 commits into
mainfrom
add-qwen3.5

Conversation

@janpfeifer
@janpfeifer janpfeifer commented Nov 25, 2026 •
Copy link
Copy Markdown
Contributor

Summary

Reusable Linear Attention / SSM / DeltaNet Package (ml/layers/attention/linear)

  • Added a new reusable ml/layers/attention/linear package with rich package documentation (doc.go, including unified recurrence equations, architecture matrix, usage examples, and literature links) and a two-tier API:
    • Stateless linear.Core(query, key, value, CoreOptions): unified chunkwise parallel scan and S == 1 single-step recurrence supporting all 6 combinations of decay (nil, scalar [B,S,H], channel-wise [B,S,H,Dk]) and update rule (associative write vs. surgical delta rule):
      • Simple Linear Attention
      • Mamba-2 / SSD (scalar decay)
      • GLA (channel-wise decay)
      • DeltaNet (un-gated delta rule)
      • Gated DeltaNet (scalar decay + delta rule, used by Qwen3.5)
      • Kimi Delta Attention / KDA (channel-wise decay + delta rule)
    • Fluent stateful linear.Builder: presets New / GatedDeltaNet, DeltaNet, GLA, and Simple, configurable projections (in_proj_qkv, in_proj_z, in_proj_a, in_proj_b, out_proj), Grouped Value Attention (WithNumKeyHeads), optional causal depthwise Conv1d (WithShortConv), QK L2 norm, gated/plain RMSNorm, padding masks (WithAttentionMask / WithSeqLen), and incremental prefill+decode state I/O (WithState, WithReturnState, DoneWithState).
    • Shared primitives (ops.go): CausalDepthwiseConv1D, RMSNormGated, L2NormLastAxis, and RepeatInterleaveHeads.
    • Algorithmic & training features:
      • Exact block-recursive unit lower-triangular inversion (invertUnitLowerTriangular) generalized for any power-of-2 chunk size >= 2.
      • Static and dynamic sequence-length support across all backends (While chunk scan).
      • Automatic static-chunk unrolling during training (scope.IsTraining(g) / WithUnrollChunks) so reverse-mode autodiff (core/autodiff.Gradient) works out of the box.

Qwen3.5 Hybrid Architecture & KVCache Support

  • Added attention.LinearLayer and MultiHeadAttentionBuilder.WithOutputGate (Sigmoid(gate) before o_proj) for Qwen3.5 hybrid models.
  • Extended kvcache.KVCache with LinearStateConfig, ConvStateSuffix, RecurrentStateSuffix, GetLinearState, and SetLinearState so LinearLayer scopes store fixed-size conv_state ([B, K-1, convDim]) and recurrent_state ([B, numVHeads, headKDim, headVDim]) tensors alongside standard KV-cache layers (untouched by PadTensors and skipped in generate's minCacheCap calculation).
  • Added ArchitectureQwen35 (ml/zoo/transformer/gated_deltanet.go):
    • Pre-norm residual block with zero-centered RMSNorm (1.0 + weight), per-head (query, gate) split, QK RMSNorm, partial RoPE, and SwiGLU MLP.
    • Linear attention layers delegate cleanly to linear.GatedDeltaNet(...).
  • Existing architectures (Standard/BERT, Gemma 8000 1/2/3/4) are untouched.

Verification

  • ml/layers/attention/linear tests (core_test.go, linear_test.go) pass on go, xla:cpu, and xla:cuda:
    • TestInvertUnitLowerTriangular across chunk sizes C = 2, 4, 8, 16, 32, 64
    • TestCoreAllModesMatchReference against a float64 token-by-token reference across all 6 modes (static While, static unrolled, and dynamic While)
    • TestBuilderPresetsPrefillAndDecodeParity (GatedDeltaNet, DeltaNet, GLA, Simple)
    • TestBuilderPaddingMaskEquivalence (WithAttentionMask vs. WithSeqLen)
    • TestBuilderAutodiffGradientsMatchFiniteDifferences (GatedDeltaNet and GLA multi-chunk unrolled backward verified against central finite differences on CPU, plus CUDA forward+backward smoke test)
  • ml/layers/attention/... and ml/zoo/transformer/... pass on go, xla:cpu, and xla:cuda, including TestQwen35HybridModelPrefillAndDecodeParity (full-sequence vs. incremental prefill + step-by-step decode parity + generate.Generator).
  • End-to-end verified on Qwen/Qwen3.5-4B in go-huggingface (PR Added inference example for CIFAR-10 demo. #87) for both sentence embedding (msmarco/benchmark_embed with -dynamic=true) and autoregressive generation (dolly/browse).

- Add reusable linear attention / SSM / DeltaNet package (ml/layers/attention/linear)
  with stateless Core and fluent stateful Builder (GatedDeltaNet, DeltaNet, GLA, Simple).
- Support all 6 combinations of decay (none, scalar, channel-wise) and update rule
  (associative write vs. surgical delta rule with generalized power-of-2 block-recursive
  WY/UT unit lower-triangular inversion), GVA, static/dynamic sequence lengths,
  incremental state caching, and unrolled chunk scanning for reverse-mode autodiff.
- Add comprehensive package documentation (doc.go) with equations, architecture table,
  usage examples, and literature links.
- Add unit, float64 token-by-token parity, prefill/decode state parity, padding mask,
  and finite-difference autodiff gradient tests across Go and XLA backends.
- Refactor ml/zoo/transformer/gated_deltanet.go to delegate to linear.GatedDeltaNet.
- Update docs/CHANGELOG.md, .agents/AGENTS.md, and skills reference.
@janpfeifer
janpfeifer merged commit cce1c95 into main Nov 25, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0