TensorRT-LLM icon indicating copy to clipboard operation
TensorRT-LLM copied to clipboard

[None][feat] Integrate helix parallelism

Open brb-nv opened this issue 1 month ago • 1 comments

Description

This MR integrates helix parallelism, an experimental feature, in TRTLLM.

Background:

  • Helix parallelism is a decode-only context parallelism method. Hence, it's used in disaggregated setting where only gen servers would have helix.
  • This involves sharding the request's seqlen across multiple CP (context parallel) ranks.
  • For a given query token in decode phase, “local attention” is computed w.r.t previous tokens on each CP rank.
  • Ensuing communication among CP ranks enables “correction” of local attention such that attention computation is exact.
  • Given KV parallelism is applicable only to attn layer, CP GPUs are "repurposed" to TP GPUs for FFN layer.

Changes in this MR:

  • At a broader level, we enable helix parallelism with DeepseekV3 and add a disagg integration test (a smoke test for now).
  • Example to explain the core changes:
    • Suppose we are dealing with the first decode step for a request with ISL 7 and gen server has two-way context parallelism i.e. cpSize=2.
    • Let's say first 4 tokens reside on cpRank0 and next 3 tokens reside on cpRank1.
    • We have an incoming query token, q7 (corresponding to first generated token). While we perform local attn computation wrt to q7 on both cpRanks, its KV cache is written only to one cpRank (rank1 in the example) and the kv7 is also considered in local attn only on that rank. We call this rank "active helix rank".
  • Known limitation: Currently only the last CP rank is considered active rank. This shall be lifted in a follow-up MR. image

Most changes in this MR enforce this:

  • KV cache is added for query token only on active rank in resource_manager.py.
  • Actual KV cache write happens in mla rope kernels and changes to rope kernels skip writing KV cache on inactive ranks.
  • The number of tokens considered in local attn computation is determined by seq_len_kv in trtllm.py which is also adjusted accordingly.

"Repurposing" attn CP ranks to FFN TP ranks can make things quite messy. To keep this readable,

  • We pass mapping with CP only to the attention layers in modeling_deepseekv3.py and pass mapping without cp to the rest.
  • We use a similar trick in communicator.py to obtain the right TP groups.

Test Coverage

$ pytest tests/unittest/_torch/modules/test_mla_helix.py -s -v
$ TRTLLM_USE_UCX_KVCACHE=1 TLLM_LOG_LEVEL=INFO pytest tests/integration/defs/disaggregated/test_disaggregated.py::test_disaggregated_deepseek_v3_lite_bf16_tllm_gen_helix -s -v

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.

  • [x] 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 the stage-list parameter 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.md and the scripts/test_to_stage_mapping.py helper.

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.

Summary by CodeRabbit

Release Notes

  • New Features

    • Added context parallelism support with Helix-based distributed inference capabilities
    • DeepSeekV3 model now supports context parallelism for enhanced performance on multi-GPU setups
    • New --cp_size command-line argument for configuring context parallel size (default: 1)
    • Enhanced disaggregated serving configuration for context-tensor parallel distribution
  • Tests

    • Added new test configuration for disaggregated DeepSeekV3 inference with context parallelism

✏️ Tip: You can customize this high-level summary in your review settings.

brb-nv avatar Nov 20 '25 20:11 brb-nv

📝 Walkthrough

Walkthrough

This pull request implements context parallelism support with Helix configuration across the TensorRT-LLM inference stack. It adds per-rank inactivity tracking (helix_is_inactive_rank) to CUDA kernels and Python layers, introduces CP size configuration parameters, implements mapping repurposing logic for CP/TP distribution, and extends model initialization and executor logic to handle inactive Helix ranks during generation.

Changes

Cohort / File(s) Summary
CUDA Kernel Signatures
cpp/tensorrt_llm/kernels/mlaKernels.cu, cpp/tensorrt_llm/kernels/mlaKernels.h
Added helix_is_inactive_rank boolean pointer parameter to MLA rope generation kernel signatures; threaded through kernel invocations to gate token processing and K/V updates based on rank inactivity status.
Tensor Operations & Rope Generation
cpp/tensorrt_llm/thop/attentionOp.cpp, cpp/tensorrt_llm/thop/dsv3RopeOp.cpp
Extended MLA tensor parameter handling to expect and forward two tensors (helix_position_offsets, helix_is_inactive_rank); added new field to MlaRopeGenArgs struct and propagated inactive rank mask through rope generation pipelines.
Torch Attention Backend
tensorrt_llm/_torch/attention_backend/trtllm.py
Added helix_position_offsets and helix_is_inactive_rank to plan/forward/mla_rope_generation APIs; extended TrtllmAttentionMetadata with inactive rank tracking; adjusted KV length planning to exclude inactive rank contributions.
Distributed Communication
tensorrt_llm/_torch/distributed/communicator.py
Implemented early CP communicator creation and mapping repurposing logic: when cp_size > 1, creates a copy with Helix mapping, scales TP by CP size, and restores original mapping after TP/PP communicator initialization.
Model Architecture
tensorrt_llm/_torch/models/modeling_deepseekv3.py
Extended DeepseekV3 layer constructors with optional mapping_with_cp parameter; added CP rank/size extraction and weight-split logic for KV projection; implemented mapping repurposing during model initialization for cp_size > 1.
Attention Modules
tensorrt_llm/_torch/modules/attention.py
Added mapping_with_cp parameter to MLA and Attention constructors; enforced num_heads equality and Helix CP type validation; updated forward paths to propagate helix parameters and support position_ids threading.
Executor & Resource Management
tensorrt_llm/_torch/pyexecutor/executor_request_queue.py, tensorrt_llm/_torch/pyexecutor/llm_request.py, tensorrt_llm/_torch/pyexecutor/model_engine.py, tensorrt_llm/_torch/pyexecutor/resource_manager.py
Added py_helix_is_inactive_rank flag to LlmRequest; implemented helix inactive rank tracking in model engine with conditional position/token calculations; gated KV cache allocation for inactive ranks in resource manager; extended AttentionMetadata with inactive rank exposure.
CLI & Configuration
examples/llm-api/quickstart_advanced.py, tensorrt_llm/commands/serve.py
Added --cp_size and cp_config command-line arguments; propagated context_parallel_size through LLM initialization; implemented cp_type string-to-enum conversion with validation.
Infrastructure & Mapping
tensorrt_llm/llmapi/disagg_utils.py, tensorrt_llm/mapping.py
Updated instance rank calculation to include context_parallel_size; added hardcoded Helix CP type fallback when cp_size > 1 to override externally provided cp_config.
Test Infrastructure
tests/integration/defs/disaggregated/test_configs/disagg_config_ctxtp2_gentp1cp2_deepseek_v3_lite_bf16_tllm_gen.yaml, tests/integration/defs/disaggregated/test_disaggregated.py
Added new disaggregated test configuration file for context TP and generation Helix setup; introduced test_disaggregated_deepseek_v3_lite_bf16_tllm_gen_helix test case with model symlink setup.

Sequence Diagram(s)

sequenceDiagram
    participant Request
    participant ResourceMgr as Resource<br/>Manager
    participant ModelEngine
    participant AttentionBE as Attention<br/>Backend
    participant MLAKernel as MLA<br/>Kernel

    Request->>ResourceMgr: prepare_resources()
    activate ResourceMgr
    alt cp_size > 1 and not last rank
        ResourceMgr->>ResourceMgr: mark py_helix_is_inactive_rank=true
        ResourceMgr->>ResourceMgr: skip KV cache allocation
    else active rank
        ResourceMgr->>ResourceMgr: allocate KV cache normally
    end
    deactivate ResourceMgr

    Request->>ModelEngine: forward pass (generation)
    activate ModelEngine
    alt helix_is_inactive_rank[batch]==true
        ModelEngine->>ModelEngine: fix past_seen_token_num<br/>(no increment)
        ModelEngine->>ModelEngine: skip token processing
    else active
        ModelEngine->>ModelEngine: increment past_seen_token_num
        ModelEngine->>AttentionBE: plan() with helix params
    end
    deactivate ModelEngine

    AttentionBE->>AttentionBE: adjust kv_lens planning<br/>(exclude inactive ranks)
    AttentionBE->>MLAKernel: mla_rope_generation<br/>(helix_is_inactive_rank)
    activate MLAKernel
    alt helix_is_inactive_rank[batch]==true
        MLAKernel->>MLAKernel: skip token processing
        MLAKernel->>MLAKernel: skip K/V updates
    else active
        MLAKernel->>MLAKernel: apply rope & assign QKV
        MLAKernel->>MLAKernel: update K/V cache
    end
    deactivate MLAKernel

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Areas requiring extra attention:

  • Mapping repurposing logic (communicator.py, modeling_deepseekv3.py, mapping.py): Core logic for switching between CP and TP distributions; mutations and restorations must be correctly sequenced and scoped to avoid state leaks.
  • KV length planning adjustments (trtllm.py, model_engine.py): Changes to how KV cache lengths are calculated when inactive ranks are present; verify accounting is correct for all rank states.
  • Warmup control flow (model_engine.py): Conditional position_id and past_seen_token_num calculations based on warmup state and inactivity; ensure all branches are consistent.
  • Cross-layer parameter threading (executor_request_queue.py, model_engine.py, resource_manager.py): helix_is_inactive_rank flows through multiple abstraction layers; verify end-to-end propagation and type conversions (bool → tensor → pointer).
  • Model initialization side effects (modeling_deepseekv3.py): Temporary mapping mutations during model construction; verify original mapping is reliably restored even on error paths.

Suggested reviewers

  • schetlur-nv
  • nvchenghaoz
  • Shixiaowei02
  • Superjomn
  • Tabrizian
  • Funatiq
  • QiJune

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 34.04% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description check ✅ Passed The PR description provides a comprehensive explanation of helix parallelism, background context, specific implementation details with an example, test coverage commands, and confirmation of the PR checklist.
Title check ✅ Passed The PR title '[TRTLLM-5971][feat] Integrate helix parallelism' clearly and specifically describes the main change: integration of helix parallelism into TensorRT-LLM.
✨ Finishing touches
  • [ ] 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • [ ] Create PR with unit tests
  • [ ] Post copyable unit tests in a comment

[!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.

  • Provide your own instructions using the high_level_summary_instructions setting.
  • Format the summary however you like (bullet lists, tables, multi-section layouts, contributor stats, etc.).
  • Use high_level_summary_in_walkthrough to move the summary from the description to the walkthrough section.

Example instruction:

"Divide the high-level summary into five sections:

  1. 📝 Description — Summarize the main change in 50–60 words, explaining what was done.
  2. 📓 References — List relevant issues, discussions, documentation, or related PRs.
  3. 📦 Dependencies & Requirements — Mention any new/updated dependencies, environment variable changes, or configuration updates.
  4. 📊 Contributor Summary — Include a Markdown table showing contributions: | Contributor | Lines Added | Lines Removed | Files Changed |
  5. ✔️ Additional Notes — Add any extra reviewer context. Keep each section concise (under 200 words) and use bullet or numbered lists for clarity."

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.

❤️ Share

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

coderabbitai[bot] avatar Nov 21 '25 18:11 coderabbitai[bot]

Where is usage documented? I don't see any docs in the changed files list

laikhtewari avatar Nov 25 '25 20:11 laikhtewari

/bot run --disable-fail-fast

brb-nv avatar Nov 26 '25 19:11 brb-nv

PR_Github #25881 [ run ] triggered by Bot. Commit: 28e50c0

tensorrt-cicd avatar Nov 26 '25 19:11 tensorrt-cicd

/bot run --disable-fail-fast

brb-nv avatar Nov 26 '25 21:11 brb-nv

PR_Github #25890 [ run ] triggered by Bot. Commit: 26f4d07

tensorrt-cicd avatar Nov 26 '25 21:11 tensorrt-cicd

PR_Github #25881 [ run ] completed with state ABORTED. Commit: 28e50c0 LLM/main/L0_MergeRequest_PR #19626 (Blue Ocean) completed with status: ABORTED

tensorrt-cicd avatar Nov 26 '25 21:11 tensorrt-cicd

/bot run --disable-fail-fast

brb-nv avatar Nov 27 '25 02:11 brb-nv

PR_Github #25890 [ run ] completed with state SUCCESS. Commit: 26f4d07 /LLM/main/L0_MergeRequest_PR pipeline #19634 completed with status: 'FAILURE'

tensorrt-cicd avatar Nov 27 '25 02:11 tensorrt-cicd

PR_Github #25932 [ run ] triggered by Bot. Commit: 3642531

tensorrt-cicd avatar Nov 27 '25 02:11 tensorrt-cicd

PR_Github #25932 [ run ] completed with state SUCCESS. Commit: 3642531 /LLM/main/L0_MergeRequest_PR pipeline #19663 completed with status: 'FAILURE'

tensorrt-cicd avatar Nov 27 '25 15:11 tensorrt-cicd

/bot run --disable-fail-fast --only-multi-gpu-test

brb-nv avatar Nov 27 '25 15:11 brb-nv

/bot run --disable-fail-fast --only-multi-gpu-test

brb-nv avatar Nov 27 '25 15:11 brb-nv

PR_Github #26043 [ run ] triggered by Bot. Commit: b89b5c5

tensorrt-cicd avatar Nov 27 '25 15:11 tensorrt-cicd

PR_Github #26044 [ run ] triggered by Bot. Commit: b89b5c5

tensorrt-cicd avatar Nov 27 '25 15:11 tensorrt-cicd

PR_Github #26043 [ run ] completed with state ABORTED. Commit: b89b5c5

tensorrt-cicd avatar Nov 27 '25 15:11 tensorrt-cicd

/bot run --disable-fail-fast --only-multi-gpu-test

brb-nv avatar Nov 27 '25 16:11 brb-nv

PR_Github #26051 [ run ] triggered by Bot. Commit: 83d6416

tensorrt-cicd avatar Nov 27 '25 16:11 tensorrt-cicd

PR_Github #26044 [ run ] completed with state ABORTED. Commit: b89b5c5 LLM/main/L0_MergeRequest_PR #19768 (Blue Ocean) completed with status: ABORTED

tensorrt-cicd avatar Nov 27 '25 16:11 tensorrt-cicd

PR_Github #26051 [ run ] completed with state SUCCESS. Commit: 83d6416 /LLM/main/L0_MergeRequest_PR pipeline #19775 (Partly Tested) completed with status: 'FAILURE'

tensorrt-cicd avatar Nov 27 '25 23:11 tensorrt-cicd

/bot run --disable-fail-fast

brb-nv avatar Nov 27 '25 23:11 brb-nv

PR_Github #26073 [ run ] triggered by Bot. Commit: 83d6416

tensorrt-cicd avatar Nov 27 '25 23:11 tensorrt-cicd

PR_Github #26073 [ run ] completed with state SUCCESS. Commit: 83d6416 /LLM/main/L0_MergeRequest_PR pipeline #19797 completed with status: 'FAILURE'

tensorrt-cicd avatar Nov 28 '25 07:11 tensorrt-cicd

/bot run --disable-fail-fast

brb-nv avatar Nov 28 '25 15:11 brb-nv

PR_Github #26214 [ run ] triggered by Bot. Commit: 83d6416

tensorrt-cicd avatar Nov 28 '25 15:11 tensorrt-cicd

/bot run --disable-fail-fast

brb-nv avatar Nov 28 '25 20:11 brb-nv

PR_Github #26228 [ run ] triggered by Bot. Commit: 98051c3

tensorrt-cicd avatar Nov 28 '25 20:11 tensorrt-cicd

PR_Github #26214 [ run ] completed with state ABORTED. Commit: 83d6416 LLM/main/L0_MergeRequest_PR #19915 (Blue Ocean) completed with status: ABORTED

tensorrt-cicd avatar Nov 28 '25 20:11 tensorrt-cicd

/bot run --disable-fail-fast

brb-nv avatar Nov 29 '25 00:11 brb-nv

PR_Github #26230 [ run ] triggered by Bot. Commit: 98051c3

tensorrt-cicd avatar Nov 29 '25 00:11 tensorrt-cicd