Skip to content

fix(awq): map kept-row indices back onto the padded grid (#5015) - #5030

Merged
Qubitium merged 2 commits into
mainfrom
root/hashmoe-keptrow-5015
Nov 17, 2026
Merged

Qubitium merged 2 commits into
mainfrom
root/hashmoe-keptrow-5015

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Fixes #5015. #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], and the
provenance row_index counts kept rows, not grid positions. The capture
hook strips padding (and excluded attention sinks) before storing a feature, so
in a padded batch_size > 1 batch every row after the first padded document was
routed with another token's id. The row count still matched, so the #4996
numel != token_rows guard never fired: the routed-expert scales were searched
on activations the model never produces, silently.

What Changed

  • gptqmodel/looper/awq_processor.py
    • new AWQProcessor._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 position
      replay already uses.
    • _pack_token_aligned_kwargs(provenance, batch_indices, tensors=None, mask_overrides=None)
      now reads input_ids at document * seq_len + position for 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 _AWQScaleGroupError and the configured fallback
      remain the safety net.
    • _token_row_replay_kwargs passes the captured tensors and mask overrides
      through, 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 now
    documents 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 CPU
    unit CI 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 r must therefore
carry 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 on
    both 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, 2
skipped
— the 6 are the pre-existing afmoe/flint/jamba nodes, identical
on unmodified main (165 passed there for the same set without the new file).
ruff check clean.

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_ids and indexes it per
token row).

Logic Flow Documentation

docs/awq-calibration-aggregation.md carries the kept-row mapping rule next to
the existing token-aligned-kwargs bullet.

Notes

#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.
@Qubitium
Qubitium merged commit c060a9f into main Nov 17, 2026
9 checks passed
@Qubitium
Qubitium deleted the root/hashmoe-keptrow-5015 branch November 17, 2026 23:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant