Skip to content

[TRTLLM-13685][chore] Move MLA module to separate file - #15648

Merged
yuxianq merged 8 commits into
NVIDIA:mainfrom
yuxianq:move-mla
Jul 2, 2026
Merged

[TRTLLM-13685][chore] Move MLA module to separate file#15648
yuxianq merged 8 commits into
NVIDIA:mainfrom
yuxianq:move-mla

Conversation

@yuxianq

@yuxianq yuxianq commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • New Features

    • Added dedicated support for MLA attention, including dense and sparse execution paths.
    • Improved handling for multiple context-attention strategies and FP8-aware execution.
  • Bug Fixes

    • Updated internal wiring so MLA is loaded from the correct implementation path.
    • Reduced the risk of circular import issues in attention-related components.
  • Documentation

    • Updated developer guidance to point to the correct MLA source and usage references.
  • Tests

    • Adjusted test coverage to use the new MLA import location.

Description

Move the MLA module and MLA-specific custom ops/helpers out of tensorrt_llm/_torch/modules/attention.py into tensorrt_llm/_torch/modules/mla.py.

Shared attention utilities, including the standard Attention module and Helix CP helpers, remain in attention.py and are imported by mla.py where needed. Model, test, benchmark, and documentation imports are updated to use the new MLA module path.

Test Coverage

  • pre-commit run --files AGENTS.md tensorrt_llm/_torch/modules/ATTENTION_DEVELOPER_GUIDE.md tensorrt_llm/_torch/custom_ops/__init__.py tensorrt_llm/_torch/models/modeling_deepseekv3.py tensorrt_llm/_torch/models/modeling_speculative.py tensorrt_llm/_torch/modules/attention.py tensorrt_llm/_torch/modules/mla.py tensorrt_llm/tools/layer_wise_benchmarks/mark_utils.py tests/unittest/_torch/attention/test_attention_mla.py tests/unittest/_torch/attention/sparse/test_sparse_mla_forward.py tests/unittest/_torch/attention/sparse/test_short_seq_mha.py tests/unittest/_torch/modules/test_mla_helix.py
  • python3 -m compileall -q tensorrt_llm/_torch/modules/attention.py tensorrt_llm/_torch/modules/mla.py tensorrt_llm/_torch/models/modeling_deepseekv3.py tensorrt_llm/_torch/models/modeling_speculative.py tensorrt_llm/tools/layer_wise_benchmarks/mark_utils.py tests/unittest/_torch/attention/test_attention_mla.py tests/unittest/_torch/attention/sparse/test_sparse_mla_forward.py tests/unittest/_torch/attention/sparse/test_short_seq_mha.py tests/unittest/_torch/modules/test_mla_helix.py

Targeted pytest could not run locally because this environment is missing TRT-LLM native extensions:

  • without PYTHONPATH=$PWD: operator trtllm::deepseek_v4_q_norm does not exist
  • with PYTHONPATH=$PWD TRT_LLM_NO_LIB_INIT=1: ModuleNotFoundError: No module named 'kv_cache_manager_v2.rawref._rawref'

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)

  • If PR introduces API changes, an appropriate PR label is added - either api-compatible or api-breaking. For api-breaking, include BREAKING in the PR title.

  • 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

To see a list of available CI bot commands, please comment /bot help.

@yuxianq

yuxianq commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run

@yuxianq
yuxianq marked this pull request as ready for review June 26, 2026 03:54
@yuxianq
yuxianq requested review from a team as code owners June 26, 2026 03:54
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #55972 [ run ] triggered by Bot. Commit: f34317b Link to invocation

@coderabbitai

coderabbitai Bot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The PR moves MLA implementation into tensorrt_llm/_torch/modules/mla.py, trims attention.py to generic attention code, and updates imports, docs, benchmarks, and tests to reference the new module.

Changes

MLA module split and import rewiring

Layer / File(s) Summary
Import and reference rewiring
AGENTS.md, tensorrt_llm/_torch/custom_ops/__init__.py, tensorrt_llm/_torch/models/modeling_deepseekv3.py, tensorrt_llm/_torch/models/modeling_speculative.py, tensorrt_llm/_torch/modules/ATTENTION_DEVELOPER_GUIDE.md, tensorrt_llm/tools/layer_wise_benchmarks/mark_utils.py, tests/unittest/_torch/attention/..., tests/unittest/_torch/modules/test_mla_helix.py
MLA references in docs, comments, model imports, benchmark utilities, and tests now point to tensorrt_llm._torch.modules.mla.
Attention module cleanup
tensorrt_llm/_torch/modules/attention.py
attention.py keeps generic attention helpers, adds explicit Attention checks in custom-op entry points, and removes the MLA/custom-op tail.
MLA setup and custom ops
tensorrt_llm/_torch/modules/mla.py
The new module defines MLA custom ops, the FP8/BF16 BMM helper, and the MLA constructor and weight-setup logic.
Forward routing
tensorrt_llm/_torch/modules/mla.py
The module adds rotary/scaling helpers, non-DSA forward execution, DSA projection/dispatch, and context/generation routing.
Absorption and public forward
tensorrt_llm/_torch/modules/mla.py
The module adds absorption and sparse KV-cache paths plus the public MLA.forward entry point and weight-transformation hooks.

Sequence Diagram(s)

sequenceDiagram
  participant F as MLA.forward
  participant P as trtllm.mla_dsa_proj
  participant A as trtllm.mla_dsa_attn_inplace
  participant I as forward_impl
  participant C as forward_context
  participant G as forward_absorption_generation
  alt DSA path
    F->>P: project hidden_states and position_ids
    F->>A: dispatch q, compressed_kv, k_pe, latent_cache, indexer_intermediates
  else Non-DSA path
    F->>I: run pure-PyTorch MLA execution
    I->>C: select a context strategy
    I->>G: run absorption generation
  end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • NVIDIA/TensorRT-LLM#15288: Also touches MLA lifecycle and transform_weights plumbing in the former modules/attention.py codepath.

Suggested reviewers

  • Funatiq
  • brb-nv
  • litaotju
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 29.55% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly states the main change and uses the repository's ticket/type format.
Description check ✅ Passed The description covers what changed, why, test coverage, and includes the PR checklist.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tensorrt_llm/_torch/models/modeling_speculative.py`:
- Around line 16-23: The remaining MLA import is still tied to the legacy
attention module path, so update the affected import usage to come from the
dedicated MLA module instead. In the speculative model code and any related test
setup such as test_model_loader_mx, reference MLA via the symbol in modules.mla
rather than modules.attention, unless you are intentionally keeping a legacy
compatibility coverage path. Keep the other module imports unchanged and make
sure all MLA references point to the new location.

In `@tensorrt_llm/_torch/modules/mla.py`:
- Around line 1914-1917: The FlashMLA padding path in mla.py creates extra heads
with uninitialized memory even though the comment says they should be zeros. In
the q_concat padding block, replace the q_padded allocation with a
zero-initialized tensor or explicitly zero the padded tail before copying
q_concat so the extra heads remain zeroed before dispatch to the sparse kernel.
Use the q_concat/q_padded logic in the padded tensor construction to locate the
fix.
- Around line 1291-1300: The chunked-prefill scratch tensors in mla.py are
hardcoded to allocate on the default CUDA device, which can mismatch the rank’s
actual tensor device in multi-GPU runs. Update the allocations in the relevant
MLA forward/chunked-prefill path to use q.device instead of a fixed cuda device,
and keep both softmax_stats_tensor and temp_softmax_stats_tensor on the same
device as q.
- Line 123: The unpacking in _extract_mla_extra_attrs within mla.py binds
metadata only to discard it, which triggers the Ruff RUF059 warning. Rename the
unused metadata binding to _metadata in the mla_layer extraction so the unused
variable is clearly marked and lint passes.
- Around line 1018-1041: The mixed context/generation path in the MLA forward
flow is using the full-batch position_ids and stale latent cache values after
external RoPE. Update the logic around apply_rope in the context and generation
slices so q_ctx/q_gen and k_pe_ctx/k_pe_gen receive matching sliced
position_ids, and refresh latent_cache_ctx/latent_cache_gen after RoPE the same
way the non-DSA path does. Use the existing forward_context_dsa, apply_rope,
latent_cache_ctx, and latent_cache_gen blocks as the place to keep cache writes
consistent when self.apply_rotary_emb is enabled.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 152ce3bf-9546-4f13-9ded-271ea999e266

📥 Commits

Reviewing files that changed from the base of the PR and between 9882d4f and f34317b.

📒 Files selected for processing (12)
  • AGENTS.md
  • tensorrt_llm/_torch/custom_ops/__init__.py
  • tensorrt_llm/_torch/models/modeling_deepseekv3.py
  • tensorrt_llm/_torch/models/modeling_speculative.py
  • tensorrt_llm/_torch/modules/ATTENTION_DEVELOPER_GUIDE.md
  • tensorrt_llm/_torch/modules/attention.py
  • tensorrt_llm/_torch/modules/mla.py
  • tensorrt_llm/tools/layer_wise_benchmarks/mark_utils.py
  • tests/unittest/_torch/attention/sparse/test_short_seq_mha.py
  • tests/unittest/_torch/attention/sparse/test_sparse_mla_forward.py
  • tests/unittest/_torch/attention/test_attention_mla.py
  • tests/unittest/_torch/modules/test_mla_helix.py

Comment thread tensorrt_llm/_torch/models/modeling_speculative.py
Comment thread tensorrt_llm/_torch/modules/mla.py
Comment thread tensorrt_llm/_torch/modules/mla.py
Comment thread tensorrt_llm/_torch/modules/mla.py
Comment thread tensorrt_llm/_torch/modules/mla.py
@yuxianq

yuxianq commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@yuxianq
yuxianq requested review from chang-l and liji-nv June 26, 2026 04:08
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #55974 [ run ] triggered by Bot. Commit: f34317b Link to invocation

@yuxianq
yuxianq removed request for 2ez4bz and hlu1 June 26, 2026 04:08
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #55972 [ run ] completed with state ABORTED. Commit: f34317b

Link to invocation

@yuxianq yuxianq changed the title [None][chore] Move MLA module to separate file [TRTLLM-13685][chore] Move MLA module to separate file Jun 26, 2026
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #55974 [ run ] completed with state SUCCESS. Commit: f34317b
/LLM/main/L0_MergeRequest_PR pipeline #44850 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@yuxianq

yuxianq commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #56080 [ run ] triggered by Bot. Commit: 3c154a8 Link to invocation

yuxianq added 2 commits July 1, 2026 10:42
Signed-off-by: Yuxian Qiu <142763828+yuxianq@users.noreply.github.com>
Signed-off-by: Yuxian Qiu <142763828+yuxianq@users.noreply.github.com>
@yuxianq

yuxianq commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #56914 [ run ] triggered by Bot. Commit: 737f056 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #56914 [ run ] completed with state SUCCESS. Commit: 737f056
/LLM/main/L0_MergeRequest_PR pipeline #45720 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@yuxianq

yuxianq commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@yuxianq
yuxianq enabled auto-merge (squash) July 1, 2026 16:53
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #56976 [ run ] triggered by Bot. Commit: 737f056 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #56976 [ run ] completed with state SUCCESS. Commit: 737f056
/LLM/main/L0_MergeRequest_PR pipeline #45779 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@yuxianq

yuxianq commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #57047 [ run ] triggered by Bot. Commit: 737f056 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #57047 [ run ] completed with state FAILURE. Commit: 737f056
/LLM/main/L0_MergeRequest_PR pipeline #45842 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@yuxianq

yuxianq commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #57101 [ run ] triggered by Bot. Commit: 737f056 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #57101 [ run ] completed with state SUCCESS. Commit: 737f056
/LLM/main/L0_MergeRequest_PR pipeline #45890 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@yuxianq

yuxianq commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator Author

/bot skip --comment "only DGX_H100-PyTorch-4/test_unittests.py::test_unittests_v2[unittest/disaggregated/test_cache_transceiver_harness.py] fails, this failed test also appear in other PR's CI and it is unrelated to this PR, skip CI"

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #57125 Bot args parsing error: Failed to parse bot args

Link to invocation

@yuxianq

yuxianq commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator Author

/bot help

@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown

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.

Details

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) --high-priority]

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. Supports wildcard * for pattern matching (e.g., "*PerfSanity*" matches all stages containing PerfSanity). Examples: "A10-PyTorch-1, xxx", "PerfSanity". 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. Supports wildcard * for pattern matching. Examples: --extra-stage "H100_PCIe-TensorRT-Post-Merge-1, xxx", --extra-stage "Post-Merge".

--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 the stage-list parameter to access the appropriate container environment. Note: Does NOT update GitHub check status.

--high-priority (OPTIONAL) : Run the pipeline with high priority. This option is restricted to authorized users only and will route the job to a high-priority queue.

kill

kill

Kill all running builds associated with pull request.

skip

skip --comment COMMENT

Skip 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-pipeline

Reuse 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.

@yuxianq

yuxianq commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator Author

/bot skip --comment "only DGX_H100-PyTorch-4/test_unittests.py::test_unittests_v2[unittest/disaggregated/test_cache_transceiver_harness.py] fails, this failed test also appear in other PR's CI and it is unrelated to this PR, skip CI"

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #57138 Bot args parsing error: Failed to parse bot args

Link to invocation

@yuxianq

yuxianq commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator Author

/bot skip --comment "this single failed test also appears in other PR's CI and it is unrelated to this PR, skip CI"

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #57143 Bot args parsing error: Failed to parse bot args

Link to invocation

@yuxianq

yuxianq commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator Author

/bot skip --comment "only DGX_H100-PyTorch-4/test_unittests.py::test_unittests_v2[unittest/disaggregated/test_cache_transceiver_harness.py] fails, this failed test also appear in CI of other PR and it is unrelated to this PR, skip CI"

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #57144 [ skip ] triggered by Bot. Commit: 737f056 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #57144 [ skip ] completed with state SUCCESS. Commit: 737f056
Skipping testing for commit 737f056

Link to invocation

@yuxianq
yuxianq merged commit 93ad566 into NVIDIA:main Jul 2, 2026
8 checks passed
evezhier pushed a commit to evezhier/TensorRT-LLM that referenced this pull request Jul 2, 2026
Signed-off-by: Yuxian Qiu <142763828+yuxianq@users.noreply.github.com>
BrianLi23 pushed a commit to BrianLi23/TensorRT-LLM that referenced this pull request Jul 9, 2026
Signed-off-by: Yuxian Qiu <142763828+yuxianq@users.noreply.github.com>
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.

7 participants