Skip to content

fix(awq): pack token-aligned replay kwargs with the captured rows (#4880) - #4996

Merged
Qubitium merged 1 commit into
mainfrom
root/awq-dsv4-hashmoe-4880
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
root/awq-dsv4-hashmoe-4880

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Fixes #4880. AWQ could not complete on any DeepSeek-V4 checkpoint: the first
decoder layer is a hash_moe block whose router selects experts with
gate.tid2eid[input_ids.reshape(-1)], and the routed-expert gate/up scale
group replays that block on a packed sample of the captured token rows.
_sanitize_kwargs kept input_ids (it is in the block's signature) but its
value was the current calibration batch's ids, so the router produced a
different number of index rows than the replayed hidden states:

IndexError: The shape of the mask [16] at index 0 does not match the shape of the indexed tensor [40, 64] at index 0
  defuser/modeling/moe_experts_interface.py:175 in linear_loop_experts_forward

The IndexError is not an _AWQScaleGroupError, so the fallback never applied
and quantize() aborted. GPTQ completes on the same model; the failure was
hidden behind #4695's rotary crash before #4722.

What Changed

  • gptqmodel/looper/awq_processor.py
    • _AWQ_TOKEN_ALIGNED_KWARGS = ("input_ids",): kwargs whose entries line up
      one-for-one with replayed token rows.
    • AWQProcessor._pack_token_aligned_kwargs(provenance, batch_indices) selects
      them with the same (source batch, row) provenance that
      _pack_token_rows reports, returning [1, retained]. Values the capture
      cannot satisfy are skipped rather than guessed.
    • _token_row_replay_kwargs merges the packed ids into the replay kwargs and
      now returns them even when the model captured no attention mask
      (hash_moe routing needs them regardless of the mask).
    • _prepare_module_forward validates the row count of every token-aligned
      kwarg and raises _AWQScaleGroupError on a mismatch, so the configured
      plain-quantization fallback applies instead of an unhandled IndexError.
  • docs/awq-calibration-aggregation.md: new "token-aligned kwargs" bullet and
    the replay sequence diagram.
  • docs/layer-type-replay-masks.md + tests/test_docs_contract_refs.py: the
    _rotary_layer_type_kwargs citation moves to the symbol form
    (awq_processor.py::_rotary_layer_type_kwargs) because this file grew ~70
    lines above it. No behaviour change.
  • tests/test_awq_deepseek_v4_hash_moe_4880.py (new, 3 nodes) and the AWQ CPU
    unit CI suite now runs it.

Tests

tests/test_awq_deepseek_v4_hash_moe_4880.py on unmodified origin/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 a
    block replaying 20 token rows.
  • test_awq_quantizes_a_hash_moe_layer_with_aligned_token_rows - the real
    quantize() raises the IndexError above in layer 0.

On this branch: 3 passed. The end-to-end node builds a 2-layer
DeepseekV4Config with mlp_layer_types=["hash_moe", "hash_moe"] (8 routed
experts, hidden 64), calibrates with rows of 12/20/8 tokens so the packed
replay spans several batches, records every _search_best_scale replay, and
asserts each DeepseekV4SparseMoeBlock replay received exactly one
input_ids entry 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):

tree result
origin/main 6 failed, 165 passed, 2 skipped
this branch 6 failed, 168 passed, 2 skipped (+3 = the new tests)

The 6 failures are the pre-existing afmoe/flint/jamba nodes, identical in
both trees. tests/test_docs_contract_refs.py + the new file: 23 passed.
ruff check clean.

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

Logic Flow Documentation

docs/awq-calibration-aggregation.md carries the capture -> pack -> replay
diagram and the new bullet; the fallback contract table there is unchanged
(a token-aligned mismatch now takes the same _AWQScaleGroupError path as any
other unsafe group).

Notes

  • The guard is deliberately narrow: only input_ids is treated as
    token-aligned, and only modules whose forward signature accepts it receive
    it (_sanitize_kwargs). DeepSeek-V4's DeepseekV4SparseMoeBlock is the only
    shipped block in this transformers version that takes it that way.
  • If a future model exposes another per-token kwarg, add it to
    _AWQ_TOKEN_ALIGNED_KWARGS; the packing and the guard both follow the tuple.

)

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.
@Qubitium
Qubitium merged commit a62bed0 into main Nov 17, 2026
9 checks passed
@Qubitium
Qubitium deleted the root/awq-dsv4-hashmoe-4880 branch November 17, 2026 21:36
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
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