Repository navigation
fix(awq): map kept-row indices back onto the padded grid (#5015) - #5030
Merged
Merged
Conversation
#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 #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.
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 #5015. #4996 packed
input_idswith the replayed AWQ token rows soDeepSeek-V4
hash_moerouting (tid2eid[input_ids.reshape(-1)]) receives oneid per hidden row — but it read
input_ids.reshape(-1)[row_index], and theprovenance
row_indexcounts kept rows, not grid positions. The capturehook strips padding (and excluded attention sinks) before storing a feature, so
in a padded
batch_size > 1batch every row after the first padded document wasrouted with another token's id. The row count still matched, so the #4996
numel != token_rowsguard never fired: the routed-expert scales were searchedon activations the model never produces, silently.
What Changed
gptqmodel/looper/awq_processor.pyAWQProcessor._kept_row_grid_positions(batch_index, row_count, mask_overrides):resolves
(documents, positions)for a capture batch's kept rows through_token_row_source_structure— the same structure the mask and positionreplay already uses.
_pack_token_aligned_kwargs(provenance, batch_indices, tensors=None, mask_overrides=None)now reads
input_idsatdocument * seq_len + positionfor each kept row.The identity mapping is used only when no structure can be resolved (an
unpadded capture, where kept index == grid index); an out-of-range index
still skips the kwarg, so
_AWQScaleGroupErrorand the configured fallbackremain the safety net.
_token_row_replay_kwargspasses the captured tensors and mask overridesthrough, so the sink-exclusion and layer-type mask overrides are honoured by
the id packer exactly as they are by the mask packer.
docs/awq-calibration-aggregation.md: the token-aligned-kwargs bullet nowdocuments the kept-row mapping and why the count guard cannot catch it.
tests/test_awq_hash_moe_kept_rows_5015.py(new, 3 nodes) and the AWQ CPUunitCI suite runs it.Tests
The oracle in the new test is independent of the code under test: the kept set
is the padding keep-mask from the cached attention mask, minus the processor's
own BOS sink positions when sink exclusion is on; kept row
rmust thereforecarry
input_ids[nonzero(keep)[r]].On unmodified
origin/main(e7cb6ad0a, test copied into a separate worktree):2 failed, 1 passed.
test_packed_input_ids_follow_the_padded_kept_grid(batch_size=4,variable-length rows):
320/944 replayed hash_moe tokens were routed with another token's id.test_packed_input_ids_follow_the_kept_grid_with_sinks_excluded(batch_size=4,
exclude_attention_sinks=True, 8 BOS sinks dropped):an excluded attention-sink token was replayed.test_unpadded_single_row_batches_are_unaffected(batch_size=1) passes onboth trees — that is the case fix(awq): pack token-aligned replay kwargs with the captured rows (#4880) #4996 already got right, and it guards that the
fix does not disturb it.
On this branch: 3 passed.
Neighbouring AWQ suites (
test_awq_deepseek_v4_hash_moe_4880.py,test_awq_deepseek_v4_rotary_4695.py,test_awq_moe_root_capture_4697.py,test_awq_falcon_h1_parallel_branch_4741.py,test_awq_scaling_groups.py,test_docs_contract_refs.py+ the new file): 194 passed, 6 failed, 2skipped — the 6 are the pre-existing
afmoe/flint/jambanodes, identicalon unmodified
main(165 passed there for the same set without the new file).ruff checkclean.Model Support / Module Plan
No model or module-plan change. DeepSeek-V4 was already onboarded; the fix is
model-agnostic (any block whose signature takes
input_idsand indexes it pertoken row).
Logic Flow Documentation
docs/awq-calibration-aggregation.mdcarries the kept-row mapping rule next tothe existing token-aligned-kwargs bullet.
Notes
_AWQ_TOKEN_ALIGNED_KWARGSstill contains onlyinput_ids; the mapping isapplied per entry, so adding a future per-token kwarg needs no other change.
wrong values.