-
Notifications
You must be signed in to change notification settings - Fork 2k
[None][feat] Skip prefetching consolidated safetensors when appropriate #7225
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
* Why? Some models (e.g. anything produced by Mistral) can have both sharded safetensors and a consolidated safetensor in the same checkpoint directory. In such cases, prefetching both to memory is a waste of time, and memory. * What? This commit skips over consolidated safetensors when they are not the only safetensor file present in the checkpoint directory. Signed-off-by: William Zhang <[email protected]>
📝 WalkthroughWalkthroughConfiguration updates in pyproject.toml adjust tool include/exclude lists for a specific test file. The weight loader adds filtering to prefer sharded safetensors over consolidated ones. A new unit test validates this selection logic, and an integration test list entry is added to run the CPU-only test. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor Caller
participant Loader as HfWeightLoader
participant FS as Filesystem
participant Prefetch as prefetch_files
participant Parallel as _load_weights_in_parallel
Caller->>Loader: load_weights(model_dir)
Loader->>FS: list *.safetensors
FS-->>Loader: weight_files
alt Any non-"consolidated" shards exist
note right of Loader: Filter out files with "consolidated" in basename
Loader->>Loader: use shard_files
else No shards (only consolidated)
Loader->>Loader: use original weight_files
end
Loader->>Prefetch: prefetch(selected_files)
Prefetch-->>Loader: ok
Loader->>Parallel: load(selected_files)
Parallel-->>Loader: done
Loader-->>Caller: return
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested reviewers
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. ✨ 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 |
|
PR_Github #16467 [ 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: 2
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/models/checkpoints/hf/weight_loader.py (1)
1-1: Missing NVIDIA Apache-2.0 header and Python 3.8 typing compatibility.
- Per repo guidelines, prepend the NVIDIA Apache-2.0 header.
- The function annotation
-> dict[str, Any](and similar built-in generics elsewhere) requires Python 3.9+. To keep Python 3.8 compatibility, either addfrom __future__ import annotationsat the top of the file or switch totyping.Dict[str, Any].Example header and future import to add at the very top:
+# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +from __future__ import annotationsIf you prefer not to use postponed evaluation, change the annotations to
Dict[..., ...]and importDictfromtyping.
🧹 Nitpick comments (3)
tests/integration/test_lists/test-db/l0_a10.yml (1)
19-21: CPU-only unit test added to GPU A10 list: acceptable, but consider explicit CPU gating.Adding
unittest/_torch/models/checkpoints/hf/test_weight_loader.pyhere is fine and low-cost, but since it’s CPU-only, a marker (e.g.,@pytest.mark.cpu_only) plus job-side selection (or skip on GPU-required nodes) would make intent explicit and avoid accidental coupling to GPU infra later.If you want, I can add a
cpu_onlymarker and wire it in the test list selection.tests/unittest/_torch/models/checkpoints/hf/test_weight_loader.py (2)
12-49: Expand scenarios to cover common consolidated filename and case robustness.Current tests only gate on names containing “consolidated”. Add a case with
pytorch_model.safetensorsalongside shards to ensure we never co-load it when shards are present.Append to the parametrization:
@@ [ ( "foo", [ "model-00001-of-00002.safetensors", "model-000002-of-00002.safetensors", "consolidated.safetensors", ], ["model-00001-of-00002.safetensors", "model-000002-of-00002.safetensors"], ), + ( + "foo", + [ + "pytorch_model.safetensors", + "model-00001-of-00002.safetensors", + "model-00002-of-00002.safetensors", + ], + ["model-00001-of-00002.safetensors", "model-00002-of-00002.safetensors"], + ),If you want, I can also add a case-insensitivity check and adapt the loader accordingly.
65-71: Patch the local MPI barrier to avoid external environment coupling in unit tests.
load_weightscallslocal_mpi_barrier()after prefetch. In non-MPI CI, this is typically a no-op, but mocking it makes the test hermetic.@@ with ( mock.patch.object( loader, "_load_weights_in_parallel", side_effect=MyError ) as load_weights_in_parallel, mock.patch.object(loader, "prefetch_files") as prefetch_files, + mock.patch("tensorrt_llm._utils.local_mpi_barrier", return_value=None), pytest.raises(MyError), ):
📜 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 (4)
pyproject.toml(4 hunks)tensorrt_llm/_torch/models/checkpoints/hf/weight_loader.py(1 hunks)tests/integration/test_lists/test-db/l0_a10.yml(1 hunks)tests/unittest/_torch/models/checkpoints/hf/test_weight_loader.py(1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.py
📄 CodeRabbit inference engine (CODING_GUIDELINES.md)
**/*.py: Code must target Python 3.8+
Indent with 4 spaces; do not use tabs
Preserve module namespace when importing: from package.subpackage import foo; then use foo.SomeClass()
Python filenames use snake_case (e.g., some_file.py)
Class names use PascalCase
Function and method names use snake_case
Local variables use snake_case; prefix k for names starting with a number (e.g., k_99th_percentile)
Global variables are UPPER_SNAKE_CASE prefixed with G (e.g., G_MY_GLOBAL)
Constants are UPPER_SNAKE_CASE
Avoid shadowing variables from an outer scope
Initialize all externally visible members of a class in init
For interfaces used outside a file, prefer docstrings over comments; comments for internal code or local interfaces
Use Google-style docstrings for classes and functions (Sphinx-parsable)
Attributes and variables can be documented inline with trailing docstrings under the class or module
Avoid using reflection when easily avoidable; prefer explicit parameters/constructs over dict(**locals())
In try/except, catch the narrowest exception types possible
For duck-typing try/except, keep try body minimal and place logic in else after attribute existence checks
Files:
tensorrt_llm/_torch/models/checkpoints/hf/weight_loader.pytests/unittest/_torch/models/checkpoints/hf/test_weight_loader.py
**/*.{h,hpp,hxx,hh,c,cc,cpp,cxx,cu,cuh,py}
📄 CodeRabbit inference engine (CODING_GUIDELINES.md)
Prepend NVIDIA Apache-2.0 copyright header with current year to all source files
Files:
tensorrt_llm/_torch/models/checkpoints/hf/weight_loader.pytests/unittest/_torch/models/checkpoints/hf/test_weight_loader.py
🧠 Learnings (1)
📚 Learning: 2025-07-28T17:06:08.621Z
Learnt from: moraxu
PR: NVIDIA/TensorRT-LLM#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:
tests/integration/test_lists/test-db/l0_a10.yml
🧬 Code graph analysis (1)
tests/unittest/_torch/models/checkpoints/hf/test_weight_loader.py (1)
tensorrt_llm/_torch/models/checkpoints/hf/weight_loader.py (3)
HfWeightLoader(22-135)prefetch_files(121-135)load_weights(27-67)
⏰ 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)
pyproject.toml (1)
36-36: Tooling entries kept in sync across sections. LGTM.The new test file is consistently listed in isort’s skip, yapf ignore, autoflake exclude, and ruff include. This matches the comments about keeping lists aligned.
Also applies to: 67-67, 102-102, 146-146
tests/unittest/_torch/models/checkpoints/hf/test_weight_loader.py (1)
50-81: Test structure and assertions are solid.The use of a sentinel exception to capture the selected file list and the check that
prefetch_filesand_load_weights_in_parallelreceive identical sets is clean and effective.
|
PR_Github #16467 [ run ] completed with state |
…te (#7225) * Why? Some models (e.g. anything produced by Mistral) can have both sharded safetensors and a consolidated safetensor in the same checkpoint directory. In such cases, prefetching both to memory is a waste of time, and memory. * What? This commit skips over consolidated safetensors when they are not the only safetensor file present in the checkpoint directory. Signed-off-by: William Zhang <[email protected]>
…te (NVIDIA#7225) * Why? Some models (e.g. anything produced by Mistral) can have both sharded safetensors and a consolidated safetensor in the same checkpoint directory. In such cases, prefetching both to memory is a waste of time, and memory. * What? This commit skips over consolidated safetensors when they are not the only safetensor file present in the checkpoint directory. Signed-off-by: William Zhang <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…te (NVIDIA#7225) * Why? Some models (e.g. anything produced by Mistral) can have both sharded safetensors and a consolidated safetensor in the same checkpoint directory. In such cases, prefetching both to memory is a waste of time, and memory. * What? This commit skips over consolidated safetensors when they are not the only safetensor file present in the checkpoint directory. Signed-off-by: William Zhang <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…te (NVIDIA#7225) * Why? Some models (e.g. anything produced by Mistral) can have both sharded safetensors and a consolidated safetensor in the same checkpoint directory. In such cases, prefetching both to memory is a waste of time, and memory. * What? This commit skips over consolidated safetensors when they are not the only safetensor file present in the checkpoint directory. Signed-off-by: William Zhang <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…te (NVIDIA#7225) * Why? Some models (e.g. anything produced by Mistral) can have both sharded safetensors and a consolidated safetensor in the same checkpoint directory. In such cases, prefetching both to memory is a waste of time, and memory. * What? This commit skips over consolidated safetensors when they are not the only safetensor file present in the checkpoint directory. Signed-off-by: William Zhang <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…te (NVIDIA#7225) * Why? Some models (e.g. anything produced by Mistral) can have both sharded safetensors and a consolidated safetensor in the same checkpoint directory. In such cases, prefetching both to memory is a waste of time, and memory. * What? This commit skips over consolidated safetensors when they are not the only safetensor file present in the checkpoint directory. Signed-off-by: William Zhang <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…te (NVIDIA#7225) * Why? Some models (e.g. anything produced by Mistral) can have both sharded safetensors and a consolidated safetensor in the same checkpoint directory. In such cases, prefetching both to memory is a waste of time, and memory. * What? This commit skips over consolidated safetensors when they are not the only safetensor file present in the checkpoint directory. Signed-off-by: William Zhang <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
…te (NVIDIA#7225) * Why? Some models (e.g. anything produced by Mistral) can have both sharded safetensors and a consolidated safetensor in the same checkpoint directory. In such cases, prefetching both to memory is a waste of time, and memory. * What? This commit skips over consolidated safetensors when they are not the only safetensor file present in the checkpoint directory. Signed-off-by: William Zhang <[email protected]> Signed-off-by: Wangshanshan <[email protected]>
Description
Some models (e.g. anything produced by Mistral) can have both sharded safetensors and a consolidated safetensor in the same checkpoint directory. In such cases, prefetching both to memory is a waste of time, and memory.
This commit skips over consolidated safetensors when they are not the only safetensor file present in the checkpoint directory.
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.
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)]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.
Summary by CodeRabbit
Bug Fixes
Tests
Chores