Repository navigation
fix(awq): resolve scale-search rotary from the attention rope label - #4722
Merged
Merged
Conversation
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
This was referenced Nov 17, 2026
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.
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-searchKeyError: 'sliding_attention'on DeepSeek V4. Four files, +488/-26, with thedecision confined to
AWQProcessor.Fixes #4695.
1. The problem
AWQProcessor._prepare_module_forwardrebuiltposition_embeddingswith_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 asrope_layer_type. The rotarytherefore 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
TypeErrorinstead.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:#cc0000Removed mechanisms and their
mainlocations at19c40b201:gptqmodel/looper/awq_processor.py:4167-4181sliding_attentionpassed to amain/compressrotarygptqmodel/looper/awq_processor.py:4173-4181KeyErrorinstead of the caller falling backgptqmodel/looper/awq_processor.py:4087-4092position_embeddingsbecame a tuplegptqmodel/looper/awq_processor.py:4122-4140gptqmodel/looper/awq_processor.py:4255-4264xwas split2. 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:#b386003. Failure and ambiguity contract
rope_layer_typeKeyErrorKeyErrorposition_embeddingsis a mappingTypeErrorat the attentionThe fallback is deliberately conservative: when the caller cannot prove a valid
label, it does not invent one.
4. Change inventory
gptqmodel/looper/awq_processor.pytests/test_awq_deepseek_v4_rotary_4695.pydocs/awq-rotary-replay.mdREADME.md5. Tests / guarantees
test_selector_prefers_decoupled_rope_layer_typerope_layer_typewins over the architecture label for both rope labelstest_architecture_layer_type_is_not_passed_when_rotary_cannot_resolve_itNone, not a rotary calltest_mapping_position_embeddings_are_rebuilt_per_rope_typetest_unresolvable_selector_keeps_matching_captured_embeddingstest_chunked_replay_slices_mapping_position_embeddingsxtest_awq_quantizes_tiny_deepseek_v4_with_decoupled_rope_types6. Validation
Red evidence against unmodified
main(19c40b201): all seven tests fail. Sixfail 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
rope_layer_type" is necessary but notsufficient: it would turn the
KeyErrorinto aTypeErrorbecause V4'sattention indexes a mapping. This PR also preserves the mapping container.
position_ids" doesnot match the shipped Transformers model:
DeepseekV4Attention.forwardrequires
position_embeddingsand indexes it directly.packing, or inference-kernel behavior changes.