Skip to content

fix(awq): resolve scale-search rotary from the attention rope label - #4722

Merged
Qubitium merged 1 commit into
mainfrom
fix/4695-awq-rope-layer-type
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/4695-awq-rope-layer-type

Conversation

@ZX-ModelCloud

Copy link
Copy Markdown
Collaborator

TL;DR

AWQ scale search now resolves the rotary label from the attention instead of the
architecture layer type, validates it against the rotary's declared labels, and
preserves a mapping-shaped position_embeddings. This fixes the first-scale-search
KeyError: 'sliding_attention' on DeepSeek V4. Four files, +488/-26, with the
decision confined to AWQProcessor.

Fixes #4695.

1. The problem

AWQProcessor._prepare_module_forward rebuilt position_embeddings with
_rotary_layer_type_kwargs, which only read the architecture attribute
(layer_type / attention_type). DeepSeek V4 keeps architecture types
(sliding_attention, compressed_sparse_attention,
heavily_compressed_attention) separate from the labels its rotary indexes
(main, compress) and exposes the latter as rope_layer_type. The rotary
therefore raised KeyError: 'sliding_attention' before any scale was scored.

A second defect was hidden behind the first: the rebuild always returned a
tuple, but V4's attention indexes position_embeddings[self.rope_layer_type].
Fixing only the label would have produced a TypeError instead.

flowchart TD
    A["_prepare_module_forward<br/>rotary = _get_root_rotary()"] --> B["_rotary_layer_type_kwargs<br/>read architecture layer_type / attention_type"]
    B --> C["rotary(x, position_ids,<br/>layer_type=sliding_attention)"]
    C --> D{"rotary.rope_type has that key?"}
    D -- no --> E["KeyError: 'sliding_attention'<br/>scale search aborts"]
    D -- yes --> F["tuple of cos and sin"]
    F --> G{"attention indexes a mapping?"}
    G -- yes --> H["TypeError: tuple indices must be integers"]
    G -- no --> I["replay proceeds"]
    style E fill:#ffd6d6,stroke:#cc0000
    style H fill:#ffd6d6,stroke:#cc0000
Loading

Removed mechanisms and their main locations at 19c40b201:

Mechanism Location Effect
Architecture-only selector gptqmodel/looper/awq_processor.py:4167-4181 sliding_attention passed to a main/compress rotary
No label validation gptqmodel/looper/awq_processor.py:4173-4181 The rotary raised KeyError instead of the caller falling back
Tuple-only rebuild gptqmodel/looper/awq_processor.py:4087-4092 Mapping-shaped position_embeddings became a tuple
Tuple/list-only length check gptqmodel/looper/awq_processor.py:4122-4140 A mapping was treated as an unknown layout
Tensor/list/tuple-only chunk slicing gptqmodel/looper/awq_processor.py:4255-4264 A mapping kept its full batch while x was split

2. The new flow

flowchart TD
    A["_prepare_module_forward"] --> B["_rotary_layer_type_kwargs"]
    B --> C{"rotary.forward declares<br/>layer_type or attention_type?"}
    C -- no --> D["return empty selector<br/>call rotary(x, position_ids)"]
    C -- yes --> E["_rotary_layer_types<br/>rotary.layer_types or rope_type keys"]
    E --> F{"rope_layer_type, then layer_type,<br/>then attention_type is a declared label?"}
    F -- yes --> G["selector kwargs with that label"]
    F -- no --> H["return None"]
    G --> I["_build_position_embeddings"]
    H --> I
    I --> J{"captured position_embeddings<br/>is a Mapping?"}
    J -- yes --> K["rebuild one pair per captured key<br/>using that key as the label"]
    J -- no --> L{"selector is None?"}
    L -- yes --> M["return None"]
    L -- no --> N["rotary(x, position_ids,<br/>**selector)"]
    K --> O["module_kwargs['position_embeddings']"]
    N --> O
    M --> P{"_position_embeddings_match<br/>captured, seq_len?"}
    P -- yes --> Q["keep captured embeddings"]
    P -- no --> R["drop position_embeddings"]
    style H fill:#ffe9b3,stroke:#b38600
    style M fill:#ffe9b3,stroke:#b38600
    style R fill:#ffe9b3,stroke:#b38600
Loading

3. Failure and ambiguity contract

Situation Before After
Rotary declares labels, attention exposes rope_layer_type Architecture label passed; KeyError The decoupled rope label is used
Rotary declares labels, attention has no valid selector, captured value is a tuple Architecture label passed; KeyError Captured pair kept when its length matches; otherwise dropped
Captured position_embeddings is a mapping Tuple returned; TypeError at the attention Mapping rebuilt per captured key
Captured mapping keys are not labels the rotary declares Not reachable; the rotary raised first Keep the captured mapping when its length matches; otherwise drop it
Rotary has no layer-type parameter Empty selector Empty selector (unchanged)

The fallback is deliberately conservative: when the caller cannot prove a valid
label, it does not invent one.

4. Change inventory

Area File +/- Purpose
AWQ replay gptqmodel/looper/awq_processor.py +126/-26 Label resolution, mapping rebuild, mapping length check and chunk slicing
Tests tests/test_awq_deepseek_v4_rotary_4695.py +232 Five selector/container unit cases, one chunk-slicing case, one public AWQ smoke test
Docs docs/awq-rotary-replay.md +126 Flow, before/after, failure contract, tests
Docs index README.md +4 Link the new reference page from the AWQ guidance

5. Tests / guarantees

Test node Invariant
test_selector_prefers_decoupled_rope_layer_type rope_layer_type wins over the architecture label for both rope labels
test_architecture_layer_type_is_not_passed_when_rotary_cannot_resolve_it An undeclared architecture label produces None, not a rotary call
test_mapping_position_embeddings_are_rebuilt_per_rope_type A stale mapping is rebuilt with one pair per captured key and the new sequence length
test_unresolvable_selector_keeps_matching_captured_embeddings A same-length captured pair survives when no selector can be resolved
test_chunked_replay_slices_mapping_position_embeddings A mapping is sliced with the same batch split as x
test_awq_quantizes_tiny_deepseek_v4_with_decoupled_rope_types Public AWQ quantization, packed modules, and a finite forward on a mixed-layer-type V4 model

6. Validation

python -m pytest tests/test_awq_deepseek_v4_rotary_4695.py -q
# 7 passed

python -m pytest tests/test_awq_position_kwargs.py \
  tests/test_awq_vl_rotary_resolution.py tests/test_awq_rotary_device.py \
  tests/test_awq_scaling_groups.py tests/test_deepseek_v4_support.py \
  tests/test_awq_scale_search_safety.py tests/test_awq_moe_router_scaling.py -q
# 273 passed, 2 skipped

Red evidence against unmodified main (19c40b201): all seven tests fail. Six
fail with KeyError: 'sliding_attention' (including the public AWQ smoke test);
the chunk-slicing case fails because the mapping keeps its full batch.

7. Reviewer notes

  • The issue's suggested "prefer rope_layer_type" is necessary but not
    sufficient: it would turn the KeyError into a TypeError because V4's
    attention indexes a mapping. This PR also preserves the mapping container.
  • The issue's note that V4 "computes its own rotary from position_ids" does
    not match the shipped Transformers model: DeepseekV4Attention.forward
    requires position_embeddings and indexes it directly.
  • Scope is limited to the AWQ replay scaffolding. No quantized storage,
    packing, or inference-kernel behavior changes.

DeepSeek V4 decouples architecture layer types from its main/compress rope
labels. AWQ passed the architecture label to the rotary and always rebuilt a
tuple, so the first scale search raised KeyError: 'sliding_attention'.

Prefer the attention's rope_layer_type, validate every label against the
rotary's declared layer types, and preserve a mapping-shaped
position_embeddings by rebuilding one pair per captured key. When no valid
label can be resolved, keep a same-length captured pair or drop a stale one
instead of calling the rotary with a label it cannot resolve.

Fixes #4695
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants