Repository navigation
feat(leapquant): add recurrent-state quantization - #6044
Draft
ZX-ModelCloud wants to merge 2 commits into
Draft
ZX-ModelCloud wants to merge 2 commits into
ZX-ModelCloud wants to merge 2 commits into
Conversation
ZX-ModelCloud
force-pushed
the
feat/leapquant-support
branch
2 times, most recently
from
November 24, 2026 08:29
d8a5474 to
658b675
Compare
Add an opt-in runtime quantizer for the recurrent state of delta-rule linear attention. It is not a weight format: weights, checkpoints, and quant_method are unchanged. - Add LeapQuantConfig/LeapQuantState with per-window packed state, Compensator Tokens, residual smoothing, and a portable Torch path. - Integrate Kimi K3 KDA, Qwen3.5 GDN, and GLM-5.3 KDA through package-owned cache layers; no Transformers patching. - Add GPTQModel.load(state_quant=...) and enable_state_quant. - Add FP32 oracle tests and model parity tests. - Document the flow, storage, constraints, and the portable-path performance caveat. - Expand grouped GDN key heads to their value-head groups and cover the path with a regression.
ZX-ModelCloud
force-pushed
the
feat/leapquant-support
branch
from
November 24, 2026 08:30
658b675 to
c29f6e3
Compare
ZX-ModelCloud
commented
Nov 24, 2026
ZX-ModelCloud
left a comment
Collaborator
Author
There was a problem hiding this comment.
Fixed in db2b809ab. Qwen3.5 and GLM-5.3 now use the packed cache in generate(), and Kimi K3 decodes with the checkpoint's gate layout. Regression tests cover padding, offloaded reorder, and recovery from invalid cache updates. All 80 LeapQuant tests and format/check.sh passed.
| if self._leapquant is not None: | ||
| self._leapquant.reset() | ||
|
|
||
| def reorder_cache(self, beam_idx: torch.Tensor) -> None: |
Comment on lines
+846
to
+848
| def _prepare_cache_for_generation( | ||
| self, generation_config, model_kwargs, generation_mode, batch_size, max_cache_length, | ||
| ): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add opt-in LeapQuant quantization for the fixed-size recurrent state of delta-rule linear attention in Kimi K3 KDA, dense Qwen3.5 GDN, and GLM-5.3 KDA.
LeapQuant is not a weight format: model weights, checkpoints, and
quant_methodare unchanged, and it composes with weight quantization in the supported model ports. The feature is off by default and is selected throughLeapQuantConfigonGPTQModel.load(..., state_quant=...)ormodel.enable_state_quant(...).What Changed
gptqmodel/utils/leapquant.py:LeapQuantConfig,LeapQuantState, per-window quantization, Compensator Tokens, residual smoothing, packed-state save/restore, storage accounting, atomic reorder, and a portable Torch path.gptqmodel/models/ports/kimi_k3/modeling_kimi_linear.py: KDA cache ownership, prefill/decode integration, reset, reorder, offload, and fail-closed guards.gptqmodel/models/ports/native/qwen3_5/modeling_qwen3_5.py: GDN cache layer and quantized decode, including grouped key/value heads.gptqmodel/models/ports/native/glm5_next/modeling_glm5_next.py: KDA cache layer and quantized decode.gptqmodel/models/auto.pyandgptqmodel/models/base.py:state_quant=load argument andenable_state_quant.gptqmodel/__init__.py: exportLeapQuantConfig.docs/leapquant.md,docs/README.md,docs/support-matrix.md: flow, before/after table, storage, constraints, and the portable-path caveat.tests/test_leapquant_state.py,tests/test_leapquant_models.py: FP32 oracle and model integration coverage.Out of scope in this PR:
Review fixes
Tests
The first suite passed all 155 cases, including 80 LeapQuant cases. The Kimi port suite passed 133 cases and skipped 13 cases that require pinned tokenizer fixtures. The format and diff checks passed.
test_generate_uses_packed_recurrent_statetest_qwen_grouped_key_heads_quantized_decodetest_padded_decode_fails_before_cache_updatestest_unsupported_cache_operation_preserves_attention_prefixandtest_offloaded_cache_reorder_preserves_next_tokentest_failed_step_preserves_live_window_for_retry,test_compensator_stays_fp32_under_autocast, andtest_packed_restore_validates_before_replacing_live_stateMeasured Impact
No speedup is claimed. On a synthetic decode fixture, the portable Torch path is slower than the dense FP32 recurrence; the paper's speedups depend on specialized kernels, which are a separate follow-up.
Accuracy coverage:
1e-31e-2storage_bytes()Model Support / Module Plan
Not applicable: this PR adds no
MODEL_MAPentry, model definition, alias, or module-plan change. It adds an opt-in runtime cache policy to existing ports.AWQ onboarding safety
Not applicable: no model onboarding or AWQ grouping/search/replay/fold change.
Logic Flow Documentation
Documentation page:
docs/leapquant.md, linked fromdocs/README.md. It contains the flow, the before/after table, storage accounting, constraints, and validation details.flowchart TD A[Generate with caching] --> B{Caller supplies cache} B -->|Yes| C[Keep the caller cache] B -->|No| D{Decoder enables LeapQuant} D -->|No| E[Build the port's dense cache] D -->|Yes| F[Build the port's LeapQuant cache] C --> G[Model forward] E --> G F --> G G --> H{Packed recurrent state} H -->|No| I[Run the dense recurrence] H -->|Yes| J{Unpadded and unpacked input} J -->|No| K[Raise before updating cache] J -->|Yes| L[Run packed prefill or decode]flowchart TD A[Decode token] --> B{State policy} B -->|off| C[Existing delta-rule kernel] C --> D[Dense recurrent state] B -->|LeapQuant| E[Packed checkpoint + window buffer] E --> F[Replay buffered records] F --> G[Compute output and new update] G --> H{Window boundary} H -->|no| I[Buffer record] H -->|yes| J[Fit Compensator and quantize residual] J --> EKimiDeltaAttention,Qwen3_5GatedDeltaNet, andGlm5NextTextLinearAttentionread and rewrite a dense recurrent tensor through the cache layer.LeapQuantState; decode replays the packed checkpoint and buffered records, then re-quantizes at the window boundary.LinearAttentionLayerorKimiDeltaCacheLayerstores the dense recurrent tensor.LeapQuantLinearAttentionLayerorKimiDeltaCacheLayer(state_quant=...)stores int8 codes, scales, Compensator factors, and the window buffer; the dense slot stays empty.LeapQuantState.quantize_checkpoints the final state and clears the window.state_quant=Nonereturns the original cache and kernel path.Review Requirements
Kernel Accuracy Requirements
Notes