-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[None][fix] Use fp32 for indexer weight_proj GEMM #9243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
eeadd4b to
e6693d5
Compare
Signed-off-by: Chang Liu (Enterprise Products) <[email protected]>
Signed-off-by: Chang Liu (Enterprise Products) <[email protected]>
Signed-off-by: Chang Liu (Enterprise Products) <[email protected]>
118120e to
e9489cf
Compare
Signed-off-by: Chang Liu (Enterprise Products) <[email protected]>
Signed-off-by: Chang Liu (Enterprise Products) <[email protected]>
📝 WalkthroughWalkthroughThe changes refactor the DSA indexer implementation to consolidate Q/K projection and preparation logic into internal helper methods, introduce FP32 float conversion, modify weight projection parameters, and remove indexer_weights from forward signatures. Related changes rename a fuse flag and remove indexer_weights outputs across dependent attention modules. Changes
Sequence DiagramsequenceDiagram
participant forward as forward()
participant float_conv as _to_float()
participant qk_rope as _qk_projection_and_rope()
participant prep_q as _prep_q_or_k()
participant prep_k as _prep_q_or_k()
participant weight_scale as _weight_scale()
participant cache as _update_k_cache()
forward->>float_conv: convert inputs to FP32
float_conv-->>forward: hidden_states (FP32)
forward->>qk_rope: qr, hidden_states, indexer_k, position_ids
Note over qk_rope: Q/K projection<br/>K selection<br/>RoPE split & rotation
qk_rope-->>forward: q_pe, q_nope, k_pe, k_nope
par Prepare Q and K in parallel
forward->>prep_q: q_pe, q_nope
Note over prep_q: Concat, activate<br/>reshape, FP8 quantize
prep_q-->>forward: q_prepared
forward->>prep_k: k_pe, k_nope
Note over prep_k: Concat, activate<br/>reshape, FP8 quantize
prep_k-->>forward: k_prepared
end
forward->>weight_scale: weights, q_scale
Note over weight_scale: Centralized weight<br/>scaling logic
weight_scale-->>forward: scaled_weights
forward->>cache: update K cache
cache-->>forward: ✓ Cache updated
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Tip 📝 Customizable high-level summaries are now available in beta!You can now customize how CodeRabbit generates the high-level summary in your pull requests — including its content, structure, tone, and formatting.
Example instruction:
Note: This feature is currently in beta for Pro-tier users, and pricing will be announced later. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (2)
tensorrt_llm/_torch/models/modeling_deepseekv3.py (2)
356-369: Clarify kv_a_proj_with_mqa comment now that only indexer.wk is fusedThe comment still says
kv_a_proj_with_mqais oversized “to include indexer weights”, but after this PR onlyindexer.wkis fused;indexer.weights_projstays separate. Consider rewording to avoid implying the weight_proj GEMM is also fused into this module.
559-561: Fused indexer K path looks consistent; consider guarding fuse flag usage
- The new
kv_a_proj_with_mqaout_features (kv_lora_rank + qk_rope_head_dim + q_lora_rank + indexer.head_dim) matches the split inforward_impl_with_dsaand the offset used inpost_load_weights, so theindexer_kslice and theindexer.wkcopy line up correctly.- The dtype assertion before copying
indexer.wkinto the fused module is a good safety check and aligns with the new FP32-onlyweights_projpath.One small robustness tweak:
forward_impl_with_dsainMLAassumesself.fuse_a_indexer_kexists. If any non‑DeepseekV32 MLA instance ever runs with DSA enabled, usinggetattr(self, "fuse_a_indexer_k", False)(or defaulting the flag inMLA.__init__) would avoid a potentialAttributeError.Also applies to: 584-591, 598-607
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
tensorrt_llm/_torch/attention_backend/sparse/dsa.py(3 hunks)tensorrt_llm/_torch/models/modeling_deepseekv3.py(4 hunks)tensorrt_llm/_torch/modules/attention.py(1 hunks)
🧰 Additional context used
🧠 Learnings (8)
📓 Common learnings
Learnt from: thorjohnsen
Repo: NVIDIA/TensorRT-LLM PR: 6910
File: cpp/tensorrt_llm/batch_manager/kvCacheManager.cpp:0-0
Timestamp: 2025-08-14T21:04:50.248Z
Learning: In KV cache onboarding logic during prefill in cpp/tensorrt_llm/batch_manager/kvCacheManager.cpp, when calculating which blocks fall within the attention window, use getTokensPerBlock() to advance token indices rather than block->getUniqueTokens().size(), because the calculation needs to consider the post-prefill state where blocks will be filled to capacity, not their current token count.
📚 Learning: 2025-08-14T06:36:40.701Z
Learnt from: timlee0212
Repo: NVIDIA/TensorRT-LLM PR: 6886
File: tensorrt_llm/_torch/models/modeling_deepseekv3.py:0-0
Timestamp: 2025-08-14T06:36:40.701Z
Learning: In DeepSeek V3 model (tensorrt_llm/_torch/models/modeling_deepseekv3.py), the disagreement between AllReduce.__init__ guard and _compute_mlp_tp_size logic for MNNVL usage is expected by design. The AllReduce component and MLP TP-size computation intentionally use different criteria for MNNVL availability decisions.
Applied to files:
tensorrt_llm/_torch/models/modeling_deepseekv3.py
📚 Learning: 2025-08-19T12:45:11.997Z
Learnt from: amitz-nv
Repo: NVIDIA/TensorRT-LLM PR: 7033
File: tensorrt_llm/_torch/pyexecutor/model_engine.py:0-0
Timestamp: 2025-08-19T12:45:11.997Z
Learning: In tensorrt_llm/_torch/pyexecutor/model_engine.py, DoRA (Delta Orthogonal Rank Adaptation) functionality was removed from the PyTorch flow to eliminate issues with inverted DoRA detection logic. The original is_dora condition was checking if scaling_vec_pointer == 0, which was potentially incorrect.
Applied to files:
tensorrt_llm/_torch/models/modeling_deepseekv3.pytensorrt_llm/_torch/attention_backend/sparse/dsa.py
📚 Learning: 2025-08-15T06:46:53.813Z
Learnt from: eopXD
Repo: NVIDIA/TensorRT-LLM PR: 6767
File: cpp/tensorrt_llm/batch_manager/kvCacheManager.cpp:0-0
Timestamp: 2025-08-15T06:46:53.813Z
Learning: In the TensorRT-LLM KV cache manager, SWA (Sliding Window Attention) combined with beam search is currently in a broken/non-functional state and is planned for future rework. During preparatory refactoring phases, code related to SWA+beam search may intentionally remain in a non-working state until the broader rework is completed.
Applied to files:
tensorrt_llm/_torch/models/modeling_deepseekv3.pytensorrt_llm/_torch/modules/attention.pytensorrt_llm/_torch/attention_backend/sparse/dsa.py
📚 Learning: 2025-08-14T21:04:50.248Z
Learnt from: thorjohnsen
Repo: NVIDIA/TensorRT-LLM PR: 6910
File: cpp/tensorrt_llm/batch_manager/kvCacheManager.cpp:0-0
Timestamp: 2025-08-14T21:04:50.248Z
Learning: In KV cache onboarding logic during prefill in cpp/tensorrt_llm/batch_manager/kvCacheManager.cpp, when calculating which blocks fall within the attention window, use getTokensPerBlock() to advance token indices rather than block->getUniqueTokens().size(), because the calculation needs to consider the post-prefill state where blocks will be filled to capacity, not their current token count.
Applied to files:
tensorrt_llm/_torch/modules/attention.py
📚 Learning: 2025-09-29T15:14:28.503Z
Learnt from: amitz-nv
Repo: NVIDIA/TensorRT-LLM PR: 8063
File: tensorrt_llm/lora_manager.py:1080-1112
Timestamp: 2025-09-29T15:14:28.503Z
Learning: In tensorrt_llm/lora_manager.py, when calculating part_sizes for attn_qkv fused LoRA modules, the sizes are correctly multiplied by tp_size because model_config.num_heads and model_config.num_kv_heads are already divided by tp_size (per-TP-rank values), so multiplication is needed to get the original full concatenated dimension size. The interleave_fused_lora_weights_for_tp function provides proper validation.
Applied to files:
tensorrt_llm/_torch/modules/attention.py
📚 Learning: 2025-08-19T12:45:35.429Z
Learnt from: amitz-nv
Repo: NVIDIA/TensorRT-LLM PR: 7033
File: tensorrt_llm/_torch/pyexecutor/model_engine.py:2086-2092
Timestamp: 2025-08-19T12:45:35.429Z
Learning: DoRA (Delta Orthogonal Rank Adaptation) functionality has been removed from the PyTorch flow in tensorrt_llm/_torch/pyexecutor/model_engine.py. The is_dora field is computed but not used downstream in the PyTorch flow, so converting it to a tensor would be wasteful overhead.
Applied to files:
tensorrt_llm/_torch/attention_backend/sparse/dsa.py
📚 Learning: 2025-08-14T15:43:23.107Z
Learnt from: MatthiasKohl
Repo: NVIDIA/TensorRT-LLM PR: 6904
File: tensorrt_llm/_torch/attention_backend/trtllm.py:259-262
Timestamp: 2025-08-14T15:43:23.107Z
Learning: In TensorRT-LLM's attention backend, tensor parameters in the plan() method are assigned directly without validation (dtype, device, contiguity checks). This maintains consistency across all tensor inputs and follows the pattern of trusting callers to provide correctly formatted tensors.
Applied to files:
tensorrt_llm/_torch/attention_backend/sparse/dsa.py
🧬 Code graph analysis (1)
tensorrt_llm/_torch/attention_backend/sparse/dsa.py (3)
tensorrt_llm/_torch/utils.py (2)
maybe_compile(344-365)_(217-223)tensorrt_llm/quantization/utils/fp8_utils.py (1)
fp8_quantize_1x128_sf_transpose(523-533)tensorrt_llm/_torch/modules/multi_stream_utils.py (1)
maybe_execute_in_parallel(35-74)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Pre-commit Check
🔇 Additional comments (2)
tensorrt_llm/_torch/modules/attention.py (1)
1220-1236: DSA fused indexer_k split matches fused projection sizingThe new
split([self.q_lora_rank, self.kv_lora_rank, self.qk_rope_head_dim, self.indexer.head_dim], -1)for the fused path aligns with the updatedkv_a_proj_with_mqaout_features and with the offset used when copyingindexer.wkinto the fused weights. The fallback path (no fusion) remains unchanged aside from passingindexer_k=None, so behavior for non‑fused configurations is preserved.tensorrt_llm/_torch/attention_backend/sparse/dsa.py (1)
677-680: FP32weights_projpath and Q/K helper refactor look consistentThis refactor cleanly separates concerns:
weights_projis now explicitly FP32, unquantized, and uses standard GEMM (use_custom_cublas_mm=False), with_to_floatensuring FP32 inputs. That matches the goal of promoting the indexer’s weight projection to FP32._qk_projection_and_ropeand_prep_q_or_kcentralize Q/K projection, normalization, RoPE, rotation, and FP8 quantization, avoiding duplicate logic and reusing fusedindexer_kwhen provided.- The new
forwardflow keeps shapes coherent:
- Q path:
[T, n_heads, head_dim]→ FP8 + per‑token scales → reshape to[T, n_heads, head_dim]and[T, n_heads, 1].- K path:
[T, head_dim]→ FP8 + scales as[T, head_dim]and[T, 1], matching_update_k_cache’s expectations._weight_scaleapplies the same scaling factor as before, now directly to the FP32weights_projoutput using the q-scale tensor.Functionally this aligns with the existing sparse indexer pipeline while swapping the weight GEMM to FP32. Please double‑check that:
fp8_mqa_logits/fp8_paged_mqa_logitsare happy withweightsbeingfloat32(not bf16), and- tests cover both fused (
indexer_kprovided) and unfused (indexer_k=None) Indexer paths after this change.Also applies to: 720-727, 1242-1272, 1273-1312
Signed-off-by: Chang Liu (Enterprise Products) <[email protected]>
|
/bot run |
|
PR_Github #24951 [ run ] triggered by Bot. Commit: |
|
PR_Github #24951 [ run ] completed with state |
Signed-off-by: Chang Liu (Enterprise Products) <[email protected]>
|
/bot run |
|
PR_Github #25084 [ run ] triggered by Bot. Commit: |
Signed-off-by: Chang Liu (Enterprise Products) <[email protected]>
Signed-off-by: Chang Liu (Enterprise Products) <[email protected]>
|
PR_Github #25084 [ run ] completed with state |
|
/bot reuse-pipeline |
|
PR_Github #25147 [ reuse-pipeline ] triggered by Bot. Commit: |
|
PR_Github #25147 [ reuse-pipeline ] completed with state |
This PR should depend on #9232 and updates the
indexer.weight_projto use FP32 GEMM, matching the reference implementation. Our initial perf/accuracy study shows that promotingweight_projto FP32 can improve accuracy, but also introduces expected performance regression.Accuracy experiments indicate that FP32 weight_proj may stabilize topk and improves model accuracy. However, enabling this means:
weight_projcan no longer be fused for nvfp4 path.weight_projcould decompose to three kernels (copy_to_fp32 → fp32_gemm → splitKreduce), which adds overhead.Accuracy Impact (based on #9232)
Performance Impact(config: DSV3.2-NVFP4; ISL/OSL=8k/1k; DEP=8; MTP=1; concurrency=64)
Summary by CodeRabbit
Description
Test Coverage
PR Checklist
Please review the following before submitting your PR:
PR description clearly explains what and why. If using CodeRabbit's summary, please make sure it makes sense.
PR Follows TRT-LLM CODING GUIDELINES to the best of your knowledge.
Test cases are provided for new code paths (see test instructions)
Any new dependencies have been scanned for license and vulnerabilities
CODEOWNERS updated if ownership changes
Documentation updated as needed
Update tava architecture diagram if there is a significant design change in PR.
The reviewers assigned automatically/manually are appropriate for the PR.
Please check this after reviewing the above items as appropriate for this PR.
GitHub Bot Help
/bot [-h] ['run', 'kill', 'skip', 'reuse-pipeline'] ...Provide a user friendly way for developers to interact with a Jenkins server.
Run
/bot [-h|--help]to print this help message.See details below for each supported subcommand.
run [--reuse-test (optional)pipeline-id --disable-fail-fast --skip-test --stage-list "A10-PyTorch-1, xxx" --gpu-type "A30, H100_PCIe" --test-backend "pytorch, cpp" --add-multi-gpu-test --only-multi-gpu-test --disable-multi-gpu-test --post-merge --extra-stage "H100_PCIe-TensorRT-Post-Merge-1, xxx" --detailed-log --debug(experimental)]Launch build/test pipelines. All previously running jobs will be killed.
--reuse-test (optional)pipeline-id(OPTIONAL) : Allow the new pipeline to reuse build artifacts and skip successful test stages from a specified pipeline or the last pipeline if no pipeline-id is indicated. If the Git commit ID has changed, this option will be always ignored. The DEFAULT behavior of the bot is to reuse build artifacts and successful test results from the last pipeline.--disable-reuse-test(OPTIONAL) : Explicitly prevent the pipeline from reusing build artifacts and skipping successful test stages from a previous pipeline. Ensure that all builds and tests are run regardless of previous successes.--disable-fail-fast(OPTIONAL) : Disable fail fast on build/tests/infra failures.--skip-test(OPTIONAL) : Skip all test stages, but still run build stages, package stages and sanity check stages. Note: Does NOT update GitHub check status.--stage-list "A10-PyTorch-1, xxx"(OPTIONAL) : Only run the specified test stages. Examples: "A10-PyTorch-1, xxx". Note: Does NOT update GitHub check status.--gpu-type "A30, H100_PCIe"(OPTIONAL) : Only run the test stages on the specified GPU types. Examples: "A30, H100_PCIe". Note: Does NOT update GitHub check status.--test-backend "pytorch, cpp"(OPTIONAL) : Skip test stages which don't match the specified backends. Only support [pytorch, cpp, tensorrt, triton]. Examples: "pytorch, cpp" (does not run test stages with tensorrt or triton backend). Note: Does NOT update GitHub pipeline status.--only-multi-gpu-test(OPTIONAL) : Only run the multi-GPU tests. Note: Does NOT update GitHub check status.--disable-multi-gpu-test(OPTIONAL) : Disable the multi-GPU tests. Note: Does NOT update GitHub check status.--add-multi-gpu-test(OPTIONAL) : Force run the multi-GPU tests in addition to running L0 pre-merge pipeline.--post-merge(OPTIONAL) : Run the L0 post-merge pipeline instead of the ordinary L0 pre-merge pipeline.--extra-stage "H100_PCIe-TensorRT-Post-Merge-1, xxx"(OPTIONAL) : Run the ordinary L0 pre-merge pipeline and specified test stages. Examples: --extra-stage "H100_PCIe-TensorRT-Post-Merge-1, xxx".--detailed-log(OPTIONAL) : Enable flushing out all logs to the Jenkins console. This will significantly increase the log volume and may slow down the job.--debug(OPTIONAL) : Experimental feature. Enable access to the CI container for debugging purpose. Note: Specify exactly one stage in thestage-listparameter to access the appropriate container environment. Note: Does NOT update GitHub check status.For guidance on mapping tests to stage names, see
docs/source/reference/ci-overview.mdand the
scripts/test_to_stage_mapping.pyhelper.kill
killKill all running builds associated with pull request.
skip
skip --comment COMMENTSkip testing for latest commit on pull request.
--comment "Reason for skipping build/test"is required. IMPORTANT NOTE: This is dangerous since lack of user care and validation can cause top of tree to break.reuse-pipeline
reuse-pipelineReuse a previous pipeline to validate current commit. This action will also kill all currently running builds associated with the pull request. IMPORTANT NOTE: This is dangerous since lack of user care and validation can cause top of tree to break.