Repository navigation
fix(awq): pack token-aligned replay kwargs with the captured rows (#4880) - #4996
Merged
Merged
Conversation
) DeepSeek-V4 begins with `hash_moe` layers whose router selects experts with `gate.tid2eid[input_ids.reshape(-1)]`. AWQ replays a packed sample of the captured token rows (`token_rows` aggregation) and forwarded the current calibration batch's `input_ids` unchanged, so the router produced 8 index rows for 20 replayed hidden rows and the expert loop raised `IndexError` in the first decoder layer. `_sanitize_kwargs` kept `input_ids` (it is in the block's signature) and nothing re-aligned it, so AWQ could not complete on any DeepSeek-V4 checkpoint. `AWQProcessor._pack_token_aligned_kwargs` now selects every `_AWQ_TOKEN_ALIGNED_KWARGS` entry (`input_ids`) with the same provenance as `_pack_token_rows`, and `_token_row_replay_kwargs` returns it next to the rebuilt mask and positions - also when the model captured no attention mask, because `hash_moe` routing needs it regardless. `_prepare_module_forward` validates the row count and raises `_AWQScaleGroupError` on a residual mismatch, so the configured plain-quantization fallback applies instead of an unhandled `IndexError`. Docs: `docs/awq-calibration-aggregation.md` documents the packing and the guard; the `_rotary_layer_type_kwargs` citation in `docs/layer-type-replay-masks.md` moves to the symbol form now that this file has grown above it.
This was referenced Nov 17, 2026
sLaffey
pushed a commit
to sLaffey/GPTQModel
that referenced
this pull request
Nov 18, 2026
…5015) (ModelCloud#5030) * fix(awq): map kept-row indices back onto the padded grid (ModelCloud#5015) ModelCloud#4996 packed `input_ids` with the replayed AWQ token rows so DeepSeek-V4 `hash_moe` routing (`tid2eid[input_ids.reshape(-1)]`) receives one id per hidden row, but it read `input_ids.reshape(-1)[row_index]`. The provenance `row_index` counts *kept* rows: the capture hook strips padding and excluded attention sinks before storing a feature, so every row after the first padded one was routed with another document's id. The row count still matched, so ModelCloud#4996's `numel != token_rows` guard never fired and the routed-expert scales were searched on activations the model never produces. `AWQProcessor._pack_token_aligned_kwargs` now maps each kept index back through the same `_token_row_source_structure` the mask and position replay uses (via a new `_kept_row_grid_positions` helper) and reads the id at `document * seq_len + position`. The identity is used only when the capture kept every grid row; anything unresolvable is skipped, so the `_AWQScaleGroupError` guard and the configured fallback still apply. Docs: `docs/awq-calibration-aggregation.md` records the kept-row mapping. * test(awq): run the ModelCloud#5015 kept-row regression in the AWQ CPU suite
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
Fixes #4880. AWQ could not complete on any DeepSeek-V4 checkpoint: the first
decoder layer is a
hash_moeblock whose router selects experts withgate.tid2eid[input_ids.reshape(-1)], and the routed-expert gate/up scalegroup replays that block on a packed sample of the captured token rows.
_sanitize_kwargskeptinput_ids(it is in the block's signature) but itsvalue was the current calibration batch's ids, so the router produced a
different number of index rows than the replayed hidden states:
The
IndexErroris not an_AWQScaleGroupError, so the fallback never appliedand
quantize()aborted. GPTQ completes on the same model; the failure washidden behind #4695's rotary crash before #4722.
What Changed
gptqmodel/looper/awq_processor.py_AWQ_TOKEN_ALIGNED_KWARGS = ("input_ids",): kwargs whose entries line upone-for-one with replayed token rows.
AWQProcessor._pack_token_aligned_kwargs(provenance, batch_indices)selectsthem with the same
(source batch, row)provenance that_pack_token_rowsreports, returning[1, retained]. Values the capturecannot satisfy are skipped rather than guessed.
_token_row_replay_kwargsmerges the packed ids into the replay kwargs andnow returns them even when the model captured no attention mask
(
hash_moerouting needs them regardless of the mask)._prepare_module_forwardvalidates the row count of every token-alignedkwarg and raises
_AWQScaleGroupErroron a mismatch, so the configuredplain-quantization fallback applies instead of an unhandled
IndexError.docs/awq-calibration-aggregation.md: new "token-aligned kwargs" bullet andthe replay sequence diagram.
docs/layer-type-replay-masks.md+tests/test_docs_contract_refs.py: the_rotary_layer_type_kwargscitation moves to the symbol form(
awq_processor.py::_rotary_layer_type_kwargs) because this file grew ~70lines above it. No behaviour change.
tests/test_awq_deepseek_v4_hash_moe_4880.py(new, 3 nodes) and the AWQ CPUunitCI suite now runs it.Tests
tests/test_awq_deepseek_v4_hash_moe_4880.pyon unmodifiedorigin/main(
7f0ca1f44, test copied into a separate worktree): 3 failed.test_packed_token_aligned_kwargs_follow_the_replayed_rows-AttributeError: 'AWQProcessor' object has no attribute '_pack_token_aligned_kwargs'.test_replay_rejects_a_token_aligned_kwarg_with_the_wrong_row_count-DID NOT RAISE _AWQScaleGroupError; the stale(1, 8)ids were forwarded to ablock replaying 20 token rows.
test_awq_quantizes_a_hash_moe_layer_with_aligned_token_rows- the realquantize()raises theIndexErrorabove in layer 0.On this branch: 3 passed. The end-to-end node builds a 2-layer
DeepseekV4Configwithmlp_layer_types=["hash_moe", "hash_moe"](8 routedexperts, hidden 64), calibrates with rows of 12/20/8 tokens so the packed
replay spans several batches, records every
_search_best_scalereplay, andasserts each
DeepseekV4SparseMoeBlockreplay received exactly oneinput_idsentry per replayed token row.Neighbouring AWQ suites (
test_awq_scaling_groups.py,test_awq_moe_root_capture_4697.py,test_awq_deepseek_v4_rotary_4695.py,test_awq_falcon_h1_parallel_branch_4741.py):origin/mainThe 6 failures are the pre-existing
afmoe/flint/jambanodes, identical inboth trees.
tests/test_docs_contract_refs.py+ the new file: 23 passed.ruff checkclean.Model Support / Module Plan
No model or module-plan change. DeepSeek-V4 was already onboarded; only the AWQ
replay plumbing for token-aligned kwargs changed, and it is model-agnostic
(any module whose signature takes
input_idsand indexes it per token row).Logic Flow Documentation
docs/awq-calibration-aggregation.mdcarries the capture -> pack -> replaydiagram and the new bullet; the fallback contract table there is unchanged
(a token-aligned mismatch now takes the same
_AWQScaleGroupErrorpath as anyother unsafe group).
Notes
input_idsis treated astoken-aligned, and only modules whose
forwardsignature accepts it receiveit (
_sanitize_kwargs). DeepSeek-V4'sDeepseekV4SparseMoeBlockis the onlyshipped block in this transformers version that takes it that way.
_AWQ_TOKEN_ALIGNED_KWARGS; the packing and the guard both follow the tuple.