-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[https://nvbugs/5445466][fix] Eliminate race when loading HF dynamic modules #7268
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
Signed-off-by: Chang Liu (Enterprise Products) <[email protected]>
Signed-off-by: Chang Liu (Enterprise Products) <[email protected]>
📝 WalkthroughWalkthroughAdds a module-level context manager Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant Caller as Caller
participant MC as ModelConfig.from_pretrained
participant Lock as config_file_lock
participant Hub as transformers.utils.hub
participant HF as AutoConfig.from_pretrained
Caller->>MC: from_pretrained(checkpoint_dir)
MC->>Lock: enter(timeout=10)
alt lock acquired
MC->>Hub: cached_file(checkpoint_dir, "config.json")
Hub-->>MC: config.json path
MC->>MC: model_dir = parent(path)
MC->>HF: AutoConfig.from_pretrained(checkpoint_dir)
HF-->>MC: config
MC->>MC: load hf_quant_config.json, dtypes.json from model_dir
else timeout (no lock)
MC->>MC: log warning and proceed without lock
MC->>Hub: cached_file(...)
MC->>HF: AutoConfig.from_pretrained(...)
end
MC->>Lock: exit
MC-->>Caller: ModelConfig instance
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Tip 🔌 Remote MCP (Model Context Protocol) integration is now available!Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats. 📜 Recent review detailsConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro 💡 Knowledge Base configuration:
You can enable these sources in your CodeRabbit configuration. 📒 Files selected for processing (1)
💤 Files with no reviewable changes (1)
⏰ 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)
✨ Finishing Touches🧪 Generate unit tests
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
Status, Documentation and Community
|
/bot run --stage-list "GB200-8_GPUs-2_Nodes-PyTorch-Post-Merge-4" |
PR_Github #16592 [ run ] triggered by Bot |
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
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
tensorrt_llm/_torch/model_config.py (1)
1-19
: Missingfilelock
Dependency DeclarationThe verification shows that filelock is not listed in any of the project’s dependency manifests (
pyproject.toml
,setup.py
,requirements-dev.txt
,requirements.txt
). Without declaring it, installs will fail at runtime when importing the module.• Add
filelock
(with an appropriate version specifier) to your project’s dependencies.
– If you’re using Poetry, include it under[tool.poetry.dependencies]
inpyproject.toml
.
– If you’re usingsetup.py
, add it to theinstall_requires
list.
– If you manage requirements viarequirements.txt
, append a line such as:
text filelock>=3.12.0
Once
filelock
is declared, CI installs and runtime imports will resolve correctly.
🧹 Nitpick comments (2)
tensorrt_llm/_torch/model_config.py (2)
64-91
: Make the lock robust on first-run/NFS and reduce contention scope if desiredThe locking approach is sound and addresses the HF dynamic modules race. A few practical robustness tweaks will make it safer in shared caches and CI:
- Ensure the lock directory exists before creating the lock file. On a fresh environment, Path(HF_MODULES_CACHE) may not exist yet.
- Allow opting into SoftFileLock for NFS/shared filesystems (common in multi-node jobs), gated by an env var to avoid behavior changes by default.
- Improve the warning to include the lock path and PID for diagnosability.
Apply this diff within the context manager:
@contextlib.contextmanager def config_file_lock(timeout: int = 10): """ Context manager for file locking when loading pretrained configs. This prevents race conditions when multiple processes try to download/load the same model configuration simultaneously. Args: timeout: Maximum time to wait for lock acquisition in seconds """ - # Use a single global lock file in HF cache directory - # This serializes all model loading operations to prevent race conditions - lock_path = Path(HF_MODULES_CACHE) / "_remote_code.lock" + # Use a single global lock file in HF cache directory + # This serializes all model loading operations to prevent race conditions + lock_path = Path(transformers.utils.HF_MODULES_CACHE) / "_remote_code.lock" + # Ensure the directory exists (first-run safety) + lock_path.parent.mkdir(parents=True, exist_ok=True) - # Create and acquire the lock - lock = filelock.FileLock(str(lock_path), timeout=timeout) + # Create and acquire the lock. Allow opting into SoftFileLock for NFS via env var. + use_soft = os.environ.get("TRTLLM_CONFIG_LOCK_SOFT", "0") == "1" + lock_cls = filelock.SoftFileLock if use_soft else filelock.FileLock + lock = lock_cls(str(lock_path), timeout=timeout) try: with lock: yield except filelock.Timeout: - logger.warning( - f"Failed to acquire config lock within {timeout} seconds, proceeding without lock" - ) + logger.warning( + f"Config lock timeout after {timeout}s at path={lock_path} pid={os.getpid()}, proceeding without lock" + ) # Fallback: proceed without locking to avoid blocking indefinitely yield
393-406
: Consider per-checkpoint locking and forwarding all HF kwargsBoth refinements remain optional but will make this loader more flexible under concurrency and with advanced Hugging Face options:
- Per‐checkpoint lock: you’re currently using a global lock around
For high‐throughput scenarios loading different models in parallel, you could key the lock onwith config_file_lock(): transformers.AutoConfig.from_pretrained(…) transformers.utils.hub.cached_file(…)checkpoint_dir
(or a hash thereof) so that unrelated model loads don’t block each other.- Full HF kwargs support: in
tensorrt_llm/_torch/models/checkpoints/hf/config_loader.py:11
,
you forward**kwargs
intoModelConfig.from_pretrained(checkpoint_dir, **kwargs)
. However, the implementation intensorrt_llm/_torch/model_config.py
currently only accepts and forwardstrust_remote_code
toAutoConfig.from_pretrained
(and uses onlycheckpoint_dir
forcached_file
). If you ever need to load specific revisions, use a custom cache directory, pass auth tokens, or operate offline, you’ll want to extendModelConfig.from_pretrained
to accept the usual HF parameters—revision
,cache_dir
,use_auth_token
,local_files_only
, etc.—and pass them through to bothAutoConfig.from_pretrained
andhub.cached_file
.No existing call sites pass extra HF kwargs today, but updating the signature now avoids future breakage as you onboard more advanced loading scenarios.
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (2)
tensorrt_llm/_torch/model_config.py
(3 hunks)tests/integration/test_lists/waives.txt
(0 hunks)
💤 Files with no reviewable changes (1)
- tests/integration/test_lists/waives.txt
🧰 Additional context used
📓 Path-based instructions (2)
**/*.py
📄 CodeRabbit inference engine (CODING_GUIDELINES.md)
**/*.py
: Code must target Python 3.8+
Indent Python code with 4 spaces; do not use tabs
Preserve module namespaces when importing; import modules/packages and access members via the module (e.g., from package.subpackage import foo; foo.SomeClass())
Python file names should be snake_case
Python class names should be PascalCase
Python functions/methods and local variables should be snake_case; variables beginning with a number should be prefixed with k_ (e.g., k_99th_percentile)
Global variables should be UPPER_SNAKE_CASE prefixed with G_ (e.g., G_MY_GLOBAL); constants should be UPPER_SNAKE_CASE
Avoid shadowing variables from outer scopes; initialize all externally visible members in init
Prefer docstrings for interfaces used outside a file; comments should be reserved for in-function or file-local interfaces
Use Google-style docstrings for classes and functions; attributes and variables may be documented inline with trailing string literals
Avoid reflection when simpler, explicit code suffices (e.g., avoid dict(**locals()) patterns)
In try/except, catch the narrowest exceptions possible
For duck-typing patterns, keep the try body minimal and move logic to else to avoid masking unrelated failures
Files:
tensorrt_llm/_torch/model_config.py
**/*.{c,cc,cpp,cxx,h,hh,hpp,hxx,cu,cuh,py}
📄 CodeRabbit inference engine (CODING_GUIDELINES.md)
Prepend the NVIDIA copyright header (current year) to all source files (.cpp, .h, .cu, .py, etc.)
Files:
tensorrt_llm/_torch/model_config.py
🧬 Code graph analysis (1)
tensorrt_llm/_torch/model_config.py (2)
tensorrt_llm/logger.py (1)
warning
(131-132)tensorrt_llm/models/automodel.py (1)
AutoConfig
(10-49)
⏰ 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
PR_Github #16592 [ run ] completed with state |
Signed-off-by: Chang Liu (Enterprise Products) <[email protected]>
/bot run --stage-list "GB200-8_GPUs-2_Nodes-PyTorch-Post-Merge-4" |
PR_Github #16598 [ run ] triggered by Bot |
Signed-off-by: Chang Liu (Enterprise Products) <[email protected]>
PR_Github #16598 [ run ] completed with state |
/bot run --stage-list "GB200-8_GPUs-2_Nodes-PyTorch-Post-Merge-3" |
PR_Github #16632 [ run ] triggered by Bot |
PR_Github #16632 [ run ] completed with state |
/bot run --stage-list "GB200-8_GPUs-2_Nodes-PyTorch-Post-Merge-3" |
Signed-off-by: Chang Liu <[email protected]>
/bot run |
PR_Github #16789 [ run ] triggered by Bot |
PR_Github #16789 [ run ] completed with state |
/bot run |
PR_Github #16888 [ run ] triggered by Bot |
PR_Github #16888 [ run ] completed with state |
…modules (NVIDIA#7268) (NVIDIA#7379) Signed-off-by: Chang Liu (Enterprise Products) <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…modules (NVIDIA#7268) (NVIDIA#7379) Signed-off-by: Chang Liu (Enterprise Products) <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…modules (NVIDIA#7268) (NVIDIA#7379) Signed-off-by: Chang Liu (Enterprise Products) <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…modules (NVIDIA#7268) (NVIDIA#7379) Signed-off-by: Chang Liu (Enterprise Products) <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…modules (NVIDIA#7268) (NVIDIA#7379) Signed-off-by: Chang Liu (Enterprise Products) <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…modules (NVIDIA#7268) (NVIDIA#7379) Signed-off-by: Chang Liu (Enterprise Products) <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…modules (NVIDIA#7268) (NVIDIA#7379) Signed-off-by: Chang Liu (Enterprise Products) <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…modules (NVIDIA#7268) (NVIDIA#7379) Signed-off-by: Chang Liu (Enterprise Products) <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…modules (NVIDIA#7268) (NVIDIA#7379) Signed-off-by: Chang Liu (Enterprise Products) <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…modules (NVIDIA#7268) (NVIDIA#7379) Signed-off-by: Chang Liu (Enterprise Products) <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…modules (NVIDIA#7268) (NVIDIA#7379) Signed-off-by: Chang Liu (Enterprise Products) <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…modules (NVIDIA#7268) (NVIDIA#7379) Signed-off-by: Chang Liu (Enterprise Products) <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…modules (NVIDIA#7268) (NVIDIA#7379) Signed-off-by: Chang Liu (Enterprise Products) <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…modules (NVIDIA#7268) (NVIDIA#7379) Signed-off-by: Chang Liu (Enterprise Products) <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…modules (NVIDIA#7268) (NVIDIA#7379) Signed-off-by: Chang Liu (Enterprise Products) <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…modules (NVIDIA#7268) (NVIDIA#7379) Signed-off-by: Chang Liu (Enterprise Products) <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…modules (NVIDIA#7268) (NVIDIA#7379) Signed-off-by: Chang Liu (Enterprise Products) <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…modules (NVIDIA#7268) (NVIDIA#7379) Signed-off-by: Chang Liu (Enterprise Products) <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…modules (NVIDIA#7268) (NVIDIA#7379) Signed-off-by: Chang Liu (Enterprise Products) <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…modules (NVIDIA#7268) (NVIDIA#7379) Signed-off-by: Chang Liu (Enterprise Products) <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…modules (NVIDIA#7268) (NVIDIA#7379) Signed-off-by: Chang Liu (Enterprise Products) <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…modules (NVIDIA#7268) (NVIDIA#7379) Signed-off-by: Chang Liu (Enterprise Products) <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…modules (NVIDIA#7268) (NVIDIA#7379) Signed-off-by: Chang Liu (Enterprise Products) <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…modules (NVIDIA#7268) (NVIDIA#7379) Signed-off-by: Chang Liu (Enterprise Products) <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…modules (NVIDIA#7268) (NVIDIA#7379) Signed-off-by: Chang Liu (Enterprise Products) <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…modules (NVIDIA#7268) (NVIDIA#7379) Signed-off-by: Chang Liu (Enterprise Products) <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…modules (NVIDIA#7268) (NVIDIA#7379) Signed-off-by: Chang Liu (Enterprise Products) <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…modules (NVIDIA#7268) (NVIDIA#7379) Signed-off-by: Chang Liu (Enterprise Products) <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…modules (NVIDIA#7268) (NVIDIA#7379) Signed-off-by: Chang Liu (Enterprise Products) <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…modules (NVIDIA#7268) (NVIDIA#7379) Signed-off-by: Chang Liu (Enterprise Products) <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
Possibly related issue: huggingface/transformers#37492
Summary by CodeRabbit
New Features
Bug Fixes
Tests
Description
Test Coverage
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-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.