Skip to content

fix(awq): derive the decoder seq_len for replay rotaries - #6641

Merged
Qubitium merged 1 commit into
mainfrom
fix/deepseek-vl2-awq-rotary-seq-len
Dec 1, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/deepseek-vl2-awq-rotary-seq-len

Conversation

@Qubitium

@Qubitium Qubitium commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes #6631. DeepSeek-VL2 AWQ scale-search replay crashed on every device with
TypeError: DeepseekV2RotaryEmbedding.forward() missing 1 required positional argument: 'seq_len'.

Root cause: AWQProcessor._build_position_embeddings called the decoder's root rotary as
rotary(x, position_ids). The vendored DeepSeek-VL2 rotary is stateless and its forward
requires seq_len; the native decoder computes
seq_len = max(past + length, int(position_ids.max()) + 1) (gptqmodel/models/ports/deepseek_vl_v2/modeling_deepseek.py:502-503)
and every decoder layer consumes the resulting position_embeddings.

Fix at the shared replay owner:

  • Resolve each rotary forward implementation's replay plan once (parameter names plus the
    required arguments beyond (x, position_ids)), not once per replay batch.
  • Derive seq_len from the replayed positions for any rotary that requires it.
  • Raise _AWQScaleGroupError for any other required argument the replay cannot derive,
    so the configured plain-quantization fallback applies instead of a bare TypeError.
  • _refresh_forward_kwargs_from_cache now uses the same builder instead of calling the
    rotary directly.
  • The end-to-end regression asserts that every layer's q/k/v attention group produced a
    real AWQ scale-search loss (not the plain fallback) and that the rotary received the
    native max(activation_length, max_position + 1) value.

The MODEL_MAP scan found only DeepSeek-VL2 with a required third rotary argument consumed
by a position_embeddings attention. Baichuan, InternLM and Xverse also take seq_len, but
compute it inside their attention forwards, which do not accept position_embeddings, so
the replay never rebuilds them. Transformers typed rotaries (layer_type/attention_type)
are unchanged.

What Changed

  • gptqmodel/looper/awq_processor.py: cached rotary replay-plan resolution, required-argument
    derivation (seq_len) and the unsupported-replay error path; refresh uses the same helper.
  • tests/test_awq_rotary_device.py: focused regression for the native seq_len formula,
    the unsupported-replay error, and once-per-implementation replay-plan resolution.
  • tests/test_deepseek_vl_v2_local_port.py: real tiny DeepSeek-VL2 AWQ quantization from the
    vendored port config on CPU, asserting the attention groups were scored and the rotary
    received the derived seq_len.

No API or behavior change for rotaries whose forward only takes (x, position_ids).

Tests

Environment: GB10 (sm_121), torch 2.15.0+cu132, transformers 5.23.0. CPU was used for the
replay regressions; the end-to-end DeepSeek-VL2 AWQ quantization was run on CPU and GB10.

Regression files, main (e91673fc) vs branch (54443687):

python -P -m pytest -q -rs \
  tests/test_awq_rotary_device.py \
  tests/test_awq_position_kwargs.py \
  tests/test_awq_vl_rotary_resolution.py \
  tests/test_awq_mimo_v2_rotary_selection.py \
  tests/test_awq_deepseek_v4_rotary_4695.py \
  tests/test_awq_laguna_rotary_label_4874.py \
  tests/test_deepseek_vl_v2_local_port.py \
  tests/module_tree/test_deepseek_vl_v2_local_port.py
  • main: 121 passed, 4 failed, 5 skipped. All four failures are the added tests: three
    assert the fixed replay behavior (TypeError: ... missing ... 'seq_len', the
    underivable-argument path, and the end-to-end quantize), one asserts the new replay-plan
    cache exists.
  • branch: 125 passed, 5 skipped.

Focused red/green:

python -P -m pytest -q tests/test_awq_rotary_device.py -k "seq_len or underivable"
python -P -m pytest -q tests/test_deepseek_vl_v2_local_port.py -k awq_quantize_tiny
  • main: 2 failed with TypeError: _SeqLenTrackingRotary.forward() missing 1 required positional argument: 'seq_len' / _UnderivableRotary.forward() ... 'cache_position';
    the end-to-end DeepSeek-VL2 test fails with
    TypeError: DeepseekV2RotaryEmbedding.forward() missing 1 required positional argument: 'seq_len'.
  • branch: 13 passed for test_awq_rotary_device.py; the end-to-end test passes.

Fallback-only mutation check: temporarily making a required seq_len raise
_AWQScaleGroupError lets quantize() finish but fails the end-to-end assertion with
layer 0 self_attn.q_proj used the plain quantization fallback instead of the AWQ scale search, so the test distinguishes a real fix from fallback masking.

AWQ CPU baseline from the onboarding skill:

python -P -m pytest -q -rs \
  tests/test_awq_scale_search_safety.py \
  tests/test_awq_moe_router_scaling.py \
  tests/test_awq_empty_groups.py \
  tests/test_awq_position_kwargs.py \
  tests/test_awq_scale_search_memory.py \
  tests/test_bloom_awq_replay.py \
  tests/module_tree/test_awq_relationships.py \
  tests/module_tree/test_bloom_awq_relationships.py \
  tests/module_tree/test_registered_module_plans.py \
  tests/module_tree/test_definition_subset_ordering.py

Result: 591 passed, 3 skipped (the skips are unavailable local DeciLM/EXAONE/Nanbeige
fixtures).

Tiny DeepSeek-VL2 AWQ (the #6631 reproduction, widened only so the AWQ CPU kernel can pack
the tiny projections; group_size=16, fp16, CPU):

  • main: TypeError: DeepseekV2RotaryEmbedding.forward() missing 1 required positional argument: 'seq_len'.
  • branch: quantize completes; the same script on GB10 CUDA completes.

Seeded Qwen2.5-0.5B-Instruct AWQ A/B (CPU, 8 fixed calibration rows, bits=4, group_size=128):

  • 626/626 checkpoint tensors bit-identical; config.json and quantize_config.json identical.
  • quant_log.csv differs only in the wall-clock time column; loss/sample columns match.

bash format/check.sh: All checks passed! (ruff 0.14.2).

Skipped and unverified:

  • test_variable_length_awq_runs_on_remote_family[decilm|exaone|nanbeige]: local fixtures
    unavailable on this host; their variable-length replay paths were not rerun.
  • test_tiny_qwen2_vl_quantizes_with_awq_and_gptq[awq|gptq]: local Qwen2-VL fixture
    unavailable.
  • No other skip was introduced by this change.

Measured Impact

Not a performance/accuracy change. The only measured effect is that the previously failing
DeepSeek-VL2 AWQ quantize now completes on CPU and GB10, and the Qwen2.5 A/B checkpoint is
bit-identical.

Model Support / Module Plan

Not applicable: no new model, alias or definition. The existing DeepSeek-VL2 module-plan
tests are included in the run above.

AWQ onboarding safety

  • Native forward evidence: DeepseekV2Model.forward computes
    seq_len = max(past + length, int(position_ids.max()) + 1) and passes
    position_embeddings to every decoder layer; DeepseekV2Attention.forward consumes them.
  • This change does not alter AWQ grouping, search, or fold declarations. Existing
    neutral-fold/candidate-search/rollback tests are included in the baseline run above.
  • DeepSeek-VL2 is now AWQ-scaling qualified on the tested tiny config (CPU and GB10
    quantize completes); the other layouts are unchanged.
  • No-group/fallback behavior is unchanged except that an underivable required rotary
    argument now reaches the existing _AWQScaleGroupError fallback contract.

Logic Flow Documentation

Not applicable: this is a local call-convention fix inside the existing replay helper. It
does not change stages, source-of-decision, selection order, or tensor lifecycle. The new
branch only routes an underivable required argument through the existing fail-closed
_AWQScaleGroupError path.

Review Requirements

  • Reviewed this PR.
  • Checked project structure, APIs and conventions.
  • Avoided monkeypatching; used the existing replay helper.
  • Minimized the changed surface.
  • Considered regressions; ran the AWQ baseline and a seeded Qwen2.5 A/B.

Kernel Accuracy Requirements

Not applicable: no kernel, fusion or packing change.

Notes

The signature cache is keyed by the underlying forward function, so the per-device rotary
copies and every replay chunk reuse it. The seq_len derivation uses a host read of the
replayed positions only when the rotary actually requires seq_len, matching the native
decoder's formula.

DeepSeek-VL2's root rotary is stateless and its forward requires the
decoder's `seq_len`. AWQ's scale-search replay rebuilt position_embeddings
with `rotary(x, position_ids)`, so every replayed attention raised
`TypeError: ... missing 1 required positional argument: 'seq_len'` on CPU
and CUDA. The native decoder computes
`max(past + length, int(position_ids.max()) + 1)`.

Resolve each rotary forward implementation's replay plan once (the
parameter names plus the required arguments beyond `(x, position_ids)`),
derive `seq_len` from the replayed positions for any rotary that requires
it, and route other underivable required arguments through
`_AWQScaleGroupError` so the configured plain-quantization fallback applies.
`_refresh_forward_kwargs_from_cache` uses the same builder instead of
calling the rotary directly.

The end-to-end DeepSeek-VL2 regression asserts that every layer's q/k/v
attention group produced a real AWQ scale-search loss (not the plain
fallback) and that the rotary received the native
`max(activation_length, max_position + 1)` value. A fallback-only variant
fails that assertion.

The MODEL_MAP scan found only DeepSeek-VL2 with a required third argument
consumed by a `position_embeddings` attention. Baichuan, InternLM and Xverse
also take `seq_len`, but compute it inside their attention forwards, which
do not accept `position_embeddings`, so the replay never rebuilds them.

Fixes #6631
@Qubitium
Qubitium force-pushed the fix/deepseek-vl2-awq-rotary-seq-len branch from 330b3bc to 5444368 Compare December 1, 2026 11:08
@Qubitium
Qubitium marked this pull request as ready for review December 1, 2026 11:24
@Qubitium
Qubitium merged commit 0d26155 into main Dec 1, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/deepseek-vl2-awq-rotary-seq-len branch December 1, 2026 11:25
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.

AWQ: DeepSeek-VL2 scale-search replay crashes (root rotary called without required seq_len)

1 participant