Skip to content

Conversation

@longlee0622
Copy link
Collaborator

@longlee0622 longlee0622 commented Nov 18, 2025

Make is_torch_compiling flag thread-safe.

Summary by CodeRabbit

  • Refactor
    • Enhanced thread-safety for internal state management by migrating core flags to thread-local storage, ensuring reliable behavior in multi-threaded environments.

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

@longlee0622 longlee0622 requested a review from a team as a code owner November 18, 2025 06:55
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 18, 2025

📝 Walkthrough

Walkthrough

The change replaces global boolean flags with thread-local storage in tensorrt_llm/_torch/utils.py. Two flags—torch compiling state and piecewise running state—are converted from global variables to threading.local() objects, with their accessor functions updated to read from and write to thread-local attributes.

Changes

Cohort / File(s) Summary
Thread-local state refactoring
tensorrt_llm/_torch/utils.py
Replaced global boolean flags with thread-local storage objects (_torch_compiling and _piecewise_running). Updated set_torch_compiling(), is_torch_compiling(), set_piecewise_running(), and is_piecewise_running() to use thread-local accessors with default False values.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Single file with consistent, repetitive refactoring pattern
  • Verify thread-local initialization and default value handling
  • Confirm accessor functions correctly use getattr() with proper defaults
  • Check that no edge cases exist for concurrent access patterns

Pre-merge checks and finishing touches

❌ Failed checks (3 warnings)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
Title check ⚠️ Warning The title mentions 'Direct control of skip_maybe_compile' which doesn't align with the actual changes involving thread-safety of torch compiling and piecewise running flags. Update title to reflect the main change: make torch compiling and piecewise running flags thread-safe, e.g., '[None][chore] Make torch compiling and piecewise running flags thread-safe'.
Description check ⚠️ Warning PR description is incomplete. The author provided only a title and left required sections (Description, Test Coverage) unfilled with template placeholders. Complete the Description section explaining the issue and solution. Document relevant test coverage that validates the thread-safety changes made to the flags.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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 (1)
tensorrt_llm/_torch/utils.py (1)

280-290: Consider making _enable_piecewise_cuda_graph thread-safe for consistency.

While this is outside the scope of the current changes, note that _enable_piecewise_cuda_graph remains a global variable (lines 280-290), which is inconsistent with the thread-safety improvements made to the torch compiling and piecewise running flags. If thread isolation is needed for this flag as well, consider applying the same thread-local pattern.

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 96cfdd8 and 521744b.

📒 Files selected for processing (1)
  • tensorrt_llm/_torch/utils.py (2 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: nv-lschneider
Repo: NVIDIA/TensorRT-LLM PR: 7910
File: cpp/tensorrt_llm/kernels/nccl_device/config.cu:15-17
Timestamp: 2025-09-23T15:01:00.070Z
Learning: In TensorRT-LLM NCCL device kernels, the <sstream> header is not needed as an explicit include in config.cu because it's provided transitively through other headers. Local compilation testing confirms this works without the explicit include.
Learnt from: nv-lschneider
Repo: NVIDIA/TensorRT-LLM PR: 7910
File: cpp/tensorrt_llm/thop/allreduceOp.cpp:352-446
Timestamp: 2025-09-23T15:12:38.312Z
Learning: In TensorRT-LLM NCCL device implementation, NCCL version 2.28+ requirements are handled at runtime in the nccl_device/config layer rather than with compile-time guards. This allows the allreduceOp to remain version-agnostic and delegates version compatibility validation to the appropriate lower-level components that can gracefully handle unsupported configurations.
📚 Learning: 2025-07-28T17:06:08.621Z
Learnt from: moraxu
Repo: NVIDIA/TensorRT-LLM PR: 6303
File: tests/integration/test_lists/qa/examples_test_list.txt:494-494
Timestamp: 2025-07-28T17:06:08.621Z
Learning: In TensorRT-LLM testing, it's common to have both CLI flow tests (test_cli_flow.py) and PyTorch API tests (test_llm_api_pytorch.py) for the same model. These serve different purposes: CLI flow tests validate the traditional command-line workflow, while PyTorch API tests validate the newer LLM API backend. Both are legitimate and should coexist.

Applied to files:

  • tensorrt_llm/_torch/utils.py
⏰ 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/utils.py (2)

14-15: LGTM! Thread-local storage correctly initialized.

The thread-local storage objects are properly created and follow the existing pattern used elsewhere in this file (lines 64, 71).


48-61: Implementation verified as correct — all usages follow the getter/setter pattern.

Verification confirms no direct global variable access exists in the codebase. All callers properly use the getter/setter functions (set_torch_compiling, is_torch_compiling, set_piecewise_running, is_piecewise_running) at the expected locations:

  • model_engine.py: sets flags during initialization
  • piecewise_optimizer.py: manages piecewise running state
  • fused_moe/interface.py and attention.py: check torch compiling state
  • utils.py: checks piecewise running state in decorator

The thread-local storage pattern with getattr defaults is consistent and thread-safe.

@longlee0622 longlee0622 requested a review from a team as a code owner November 18, 2025 07:24
@longlee0622 longlee0622 requested a review from hyukn November 18, 2025 07:24
@longlee0622 longlee0622 changed the title [None][chore] Make torch compiling and piecewise running flags thread-safe [None][chore] Direct control of skip_maybe_compile Nov 18, 2025
@longlee0622 longlee0622 force-pushed the fix-thread-safety branch 2 times, most recently from 8ef54bd to 7885595 Compare November 18, 2025 08:03
…-safe

    Control skip_maybe_compile behavior directly

Signed-off-by: Jonas Li <[email protected]>
@longlee0622
Copy link
Collaborator Author

/bot run

@tensorrt-cicd
Copy link
Collaborator

PR_Github #24879 [ run ] triggered by Bot. Commit: 3af01fa

Copy link
Collaborator

@hyukn hyukn left a comment

Choose a reason for hiding this comment

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

Overall LGTM.

@tensorrt-cicd
Copy link
Collaborator

PR_Github #24879 [ run ] completed with state SUCCESS. Commit: 3af01fa
/LLM/main/L0_MergeRequest_PR pipeline #18782 completed with status: 'FAILURE'

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.

3 participants