Repository navigation
fix(awq): keep deepseek_v4 o_a_proj out of the input_layernorm fold - #6454
Merged
Merged
Conversation
The generic AWQ planner treats every width-matched `:!` leaf declared in a quantized block as a reader of that block's normed input and multiplies its input columns by the fold scale. DeepSeek V4 declares `self_attn.o_a_proj:!` without a stage, so it lands in the stage-0 block next to q_a_proj and kv_proj. o_a_proj is the grouped output projection: it reads the attention output, not the input_layernorm output. It still qualified because its per-group input width num_heads * head_dim / o_groups equals hidden_size (64 * 512 / 8 == 4096 on V4-Flash, and on the tiny parity fixture), so every V4 AWQ run scaled it and changed the model output. On the tiny fixture the fold alone (identity quantization) changed logits by 1.5e-1 / 1.1e-1 per layer. Declare o_a_proj `:noconsumer`, the existing opt-out for a non-quantized module in the run that reads a different tensor. Do the same for compressor.indexer.q_b_proj, which reads the q latent and would be selected the same way by any config with q_lora_rank == hidden_size. The regression test builds tiny native V4 layers of all three attention types with these widths matching, hooks the native forward to find the linears that really read the input_layernorm output, and requires the planner's group to equal that set and to fold neutrally. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
Summary
The DeepSeek V4 AWQ
input_layernormfold also scaledself_attn.o_a_proj, which does not read the normed input.The generic planner (
BaseQModel.awq_get_modules_for_scaling,_norm_scale_consumers,gptqmodel/models/base.py) treats a:!leaf declared in a quantized block as a reader of that block's input when its input width equals the norm width. It then multiplies the leaf's input columns by the fold scale. V4 declaresself_attn.o_a_proj:!without a stage, so it lands in the stage-0 block withq_a_projandkv_proj.In the native forward (
DeepseekV4Attention.forward,gptqmodel/models/ports/native/deepseek_v4/modeling_deepseek_v4.py),o_a_projis the grouped output projection:self.o_a_proj(attn_output.reshape(..., o_groups, -1)). It reads the attention output. It still qualified becauseDeepseekV4GroupedLinear.in_featuresis the per-group widthnum_heads * head_dim / o_groups. That equalshidden_sizeon the released V4-Flash config (64 * 512 / 8 = 4096) and on the tiny parity fixture (4 * 32 / 2 = 64). So every V4-Flash AWQ run scaledo_a_projand changed the model output.On a tiny random model built from
tests/fixtures/ports/native_parity/deepseek_v4.json(HCA layer, CSA layer), fold only (no quantization), relative logits change:linspace(0.55, 1.45), layer 0 / layer 1AWQProcessor._quantize_layer, searched scales, identity quantization, attention groups, cumulative after layer 0 / 1What Changed
gptqmodel/models/definitions/deepseek_v4.py:o_a_proj:!becomeso_a_proj:!:noconsumer.noconsumeris the existing opt-out for a non-quantized module that runs in the fold's run but reads a different tensor (Gemma 4'srouter.projuses it). Both_norm_scale_consumersandcanonical_awq_foldshonour it.compressor.indexer.q_b_proj:!becomescompressor.indexer.q_b_proj:!:noconsumer. It reads the q latent (q_residual), and the same rule would pick it up on any config withq_lora_rank == hidden_size. No released config has that (Flash 1024 vs 4096), so today's plans are unchanged by this line. The new test covers it.o_a_projand the compressor projections are still not quantized.tests/module_tree/test_deepseek_v4_local_port.py: newtest_awq_input_layernorm_fold_skips_width_matched_non_readers. It uses the fix(awq): fold dots3_note input_layernorm into every projection that reads it #6453 pattern on all three attention types (sliding, CSA, HCA), withnum_attention_heads=4, q_lora_rank=16so that both widths equalhidden_size=16. The test:input_layernormoutput;q_a_proj,kv_projand the compressor/indexerkv_proj/gate_proj/weights_projthat exist on the layer type;input_layernormgroup (targets + consumers) equals that set and passesassert_awq_folds_neutral.tests/module_tree/fixtures/awq_fold_plans.json: only thedeepseek_v4entry was regenerated, with--write --types deepseek_v4. The change:self_attn.o_a_projis removed from theinput_layernormconsumers in both layer classes (sliding/HCA layers and CSA layers). Before regenerating,--checkover every type reportedchanged: ['deepseek_v4']only.o_a_projand a real block-input reader such as Laguna'sg_proj:!look the same to it: an unstaged:!nn.Linearwith in_features == hidden_size. Telling them apart in shared code would need runtime capture of non-quantized module inputs, which is a processor change and would touch every model. A declared opt-out keeps all other plans unchanged.Tests
Environment: CPU only,
/root/venv/bin/python(torch 2.15.0, transformers 5.23.0),python -P,PYTHONPATH=<tree>,HF_HUB_OFFLINE=1. Base: origin/main 452550d.Measured Impact
End-to-end A/B on CPU:
GPTQModel.load(...).quantize(...)thensave. The model is a tiny random V4 (parity-fixture config, 2 layers HCA + CSA, routed MoE, non-unitinput_layernormgains). Calibration is 8 x 24 random tokens, 4-bit, group_size 16. Every saved tensor was compared.fallback=None)AWQ check:
self_attn.o_a_proj.weightin the saved checkpoint equals the sourceattn.wo_a.weighton this PR and differs from it on main, in both layers. The other differences follow from that: the attention outputs differ, so downstream calibration differs.No VRAM/performance impact: only module-tree flags changed.
Model Support / Module Plan
MODEL_MAPentry has an explicit registry plan case. (Not applicable: no new entry.)test_exact_native_and_defused_checkpoint_owners.)fusable_models.json. (Not applicable: no change.)model.module_plan(). (Not applicable.)Model test node IDs and results:
tests/module_tree/test_deepseek_v4_local_port.py(28 passed, including the 3 newtest_awq_input_layernorm_fold_skips_width_matched_non_readers[...])tests/test_deepseek_v4_local_port.py,tests/module_tree/test_flash_model_tree_consumption.py,tests/module_tree/test_registered_module_plans.py: passed (the registered plan fixture does not recordnoconsumer, so it is unchanged)AWQ onboarding safety
AWQ evidence and per-variant policy: the
input_layernormoutput is read byq_a_proj,kv_projand, when the layer has them,compressor.kv_proj/gate_proj(HCA, CSA) andcompressor.indexer.kv_proj/gate_proj/scorer.weights_proj(CSA). All of these readhidden_statesinDeepseekV4Attention.forwardand the compressor/indexer forwards. The new test checks this set against the hooked native forward for sliding, CSA and HCA layers and requires the fold to be neutral.o_a_projreads the de-rotated attention output, andcompressor.indexer.q_b_projreadsq_residual. Neither is a consumer. The post-attention MoE group and the per-expert up -> down groups are unchanged and still covered bytest_awq_scale_folds_preserve_real_hyper_connected_layer_output.Logic Flow Documentation
Diagram summary: not applicable. The change only adds an existing flag to two module-tree leaves; no control flow changes.
Review Requirements
I or an AI agent (such as Codex or Claude) have:
Kernel Accuracy Requirements
Notes
deepseek_v4, the generic AWQ planner addedself_attn.o_a_projas a scale consumer of theinput_layernormgroup.o_a_projreads the attention output; it was selected only because its per-group input widthnum_heads*head_dim/o_groupsequalshidden_size(64*512/8 = 4096 on V4-Flash). Fold-only logits change on the tiny fixture: 1.5e-1 / 1.1e-1 -> 2.8e-7 / 2.0e-7._norm_scale_consumers(andcanonical_awq_folds) admit any:!2-D-weight leaf in the block whose input width equals the norm width. I checked every non-MoE consumer in the current fold inventory against its port's forward:in_proj_a/in_proj_b/in_proj_ba(agnes, qwen3_5*, qwen3_next, intern_s2_preview, minicpmv4_6, zdtaichu5_0, ovis2_6_next),b_proj/f_a_proj/g_a_proj(glm5_next, solar_open2),g_proj(laguna, brumby, lfm3_vl),indexer.weights_proj(deepseek_v32) and phi4mm'sqkv_proj.lora_A.*.native_parityfixtures that build and run founddeepseek_v4'so_a_projas the only width-coincidence consumer.🤖 Generated with Claude Code