Skip to content

fix(turboquant): preserve generation and packed KV cache state - #6612

Merged
Qubitium merged 1 commit into
mainfrom
fix/turboquant-kv-cache-review-6603
Dec 1, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/turboquant-kv-cache-review-6603

Conversation

@ZX-ModelCloud

@ZX-ModelCloud ZX-ModelCloud commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Follow-up to #6603. Cache selection now honors the effective generation options. TurboQuant also preserves packed state across batch changes and readout failures, and reconstructs finite outputs from finite KV inputs across the supported dtypes.

What Changed

  • Consume an explicit dynamic selector after building or checking the packed cache. Validate the model's generation defaults and reject incompatible cache layouts and decoding modes before writing cache data.
  • Validate both operands, retain packed batch metadata, roll back failed layer readouts, and keep the cache free of autograd graphs. Store FP32 vector RMS and allocate QJL fields only when enabled.
  • Synchronize the decode timer at both boundaries and label the original perplexity measurements with their storage layout.

Tests

  • Ran the end-to-end TurboQuant and unit suites.
  • Added focused tests for the reported regressions.
bash format/check.sh
ruff check scripts/benchmark_turboquant_kv.py scripts/gil0_api_probe.py
python -m pytest -q -rs --tb=short tests/test_turboquant.py tests/test_turboquant_gpu.py tests/models/test_turboquant_kv_cache.py
python -m pytest -q -rs --tb=line tests/test_docs_contract_refs.py
python -m pytest -q -rs --tb=line tests/test_backend_config.py tests/test_lazy_kernel_registry_import.py tests/test_transformers_unpatched.py tests/test_model_port_policy.py tests/test_leapquant_state.py tests/test_config_unapplied_options_5583.py

Ruff passed. The TurboQuant suite passed 94 tests and skipped seven device cases. The documentation suite passed 264 tests. Adjacent suites passed 229 tests and skipped ten LeapQuant device cases. The revised device probe is unverified.

Regression Observed on #6603 Covered by
Effective generation settings Explicit dynamic conflicted with the injected cache. Inherited settings were skipped by the wrapper. test_generate_with_explicit_dynamic_cache_implementation, test_generate_honors_inherited_use_cache, test_generate_rejects_inherited_unsupported_options
Cache transitions Reorder left stale batch metadata. An injected readout failure left the new token in the cache. test_cache_can_append_after_reorder_changes_batch_size, test_cache_can_retry_after_materialization_fails
Finite KV range FP16 metadata overflowed and the FP32 sum of squares underflowed or overflowed. test_cache_preserves_scale_for_large_and_small_vectors, FP64 RMS and Hadamard oracles
Decode timing The timer sampled work submissions without synchronizing either boundary. test_benchmark_times_completed_decode

Measured Impact

The table gives packed bytes for a head dimension of 128 with QJL enabled.

Key/value bits Previous bytes per head/token Current bytes per head/token
4/4 134 138
4/3 118 122
3/2 86 90

This validation covers CPU correctness and packed byte counts. Revised throughput and perplexity are unverified.

Logic Flow Documentation

The generation and packed-cache diagrams are also in TurboQuant KV-cache quantization.

Boundary Previous behavior Current behavior
BaseQModel._prepare_kv_quant_cache Read keywords and a supplied generation config, then forwarded the cache selector with the cache. Check keywords, supplied config and model defaults. Consume the selector after fulfilling the request.
TurboQuantLayer.update Appended and advanced the length before readout. Restore the previous chunks and length when readout raises.
flowchart TD
    A["GPTQModel.generate"] --> B{"kv_quant set"}
    B -->|no| C["model.generate with caller kwargs"]
    B -->|yes| K{"effective use_cache"}
    K -->|false| C
    K -->|enabled| D{"supported generation options"}
    D -->|no| E["raise ValueError"]
    D -->|yes| F{"past_key_values supplied"}
    F -->|no| G["build TurboQuantDynamicCache"]
    F -->|yes| H{"TurboQuantDynamicCache with matching policy"}
    H -->|no| E
    H -->|yes| I["keep caller cache"]
    G --> L["consume dynamic selector if set"]
    I --> L
    L --> J["model.generate"]
Loading
flowchart LR
    A["key/value update"] --> B["normalize by vector norm"]
    B --> C["Hadamard rotation"]
    C --> D{"keys and use_qjl"}
    D -->|yes| E["b-bit MSE indices<br/>plus one-bit QJL sketch"]
    D -->|no| F["b-bit MSE indices"]
    E --> G["packed chunks<br/>indices, norms, sketch"]
    F --> G
    G --> H["materialize on attention call"]
    H --> I["inverse rotation and norm"]
Loading

Review Requirements

  • Checked the native generation entry points and packed-layer consumers.
  • Verified the regressions with independent numerical oracles and the tiny loaded model.

Resolve effective generation settings before injecting the cache. Roll back a failed layer readout and preserve batch metadata and vector scale across cache updates. Synchronize the decode benchmark at its timing boundaries.
@ZX-ModelCloud
ZX-ModelCloud marked this pull request as ready for review December 1, 2026 06:42
@Qubitium
Qubitium merged commit 9789329 into main Dec 1, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/turboquant-kv-cache-review-6603 branch December 1, 2026 08:09
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.

2 participants