security(opt): enable weights_only=True by default#1056
security(opt): enable weights_only=True by default#1056RinZ27 wants to merge 1 commit intoNVIDIA:mainfrom
Conversation
Signed-off-by: RinZ27 <222222878+RinZ27@users.noreply.github.com>
📝 WalkthroughWalkthroughThe Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
modelopt/torch/opt/conversion.py (2)
630-632: Consider adding a security comment for consistency.The
weights_only=Truedefault is correct and aligns with the security guidelines. For consistency withload_modelopt_state(line 526), consider adding a brief inline comment explaining this is a security measure for ModelOpt-generated checkpoints.📝 Optional: Add security comment for consistency
# load checkpoint kwargs.setdefault("map_location", "cpu") + # Security NOTE: weights_only=True is used here on ModelOpt-generated checkpoints kwargs.setdefault("weights_only", True) objs = torch.load(f, **kwargs)🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@modelopt/torch/opt/conversion.py` around lines 630 - 632, Add a brief inline security comment next to the kwargs.setdefault("weights_only", True) in conversion.py (near the torch.load call) explaining that setting weights_only=True is a security measure for loading ModelOpt-generated checkpoints, mirroring the comment style used in load_modelopt_state to make intent consistent and clear.
553-553: Documentation example could reinforce secure loading pattern.The example shows
torch.load("model_weights.pt")withoutweights_only=True. While PyTorch >= 2.6 defaults toTrue, explicitly showing the secure pattern in documentation helps reinforce best practices for users on older PyTorch versions.📝 Optional: Update docstring example
- model.load_state_dict(torch.load("model_weights.pt"), ...) # Load the model weights + model.load_state_dict(torch.load("model_weights.pt", weights_only=True), ...) # Load the model weights🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@modelopt/torch/opt/conversion.py` at line 553, Update the docstring/example to explicitly use the secure loading pattern by passing weights_only=True to torch.load in the example call (i.e., change the call shown next to model.load_state_dict to use torch.load("model_weights.pt", weights_only=True)); reference the model.load_state_dict and torch.load usage in conversion.py so readers see the explicit secure flag.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@modelopt/torch/opt/conversion.py`:
- Around line 630-632: Add a brief inline security comment next to the
kwargs.setdefault("weights_only", True) in conversion.py (near the torch.load
call) explaining that setting weights_only=True is a security measure for
loading ModelOpt-generated checkpoints, mirroring the comment style used in
load_modelopt_state to make intent consistent and clear.
- Line 553: Update the docstring/example to explicitly use the secure loading
pattern by passing weights_only=True to torch.load in the example call (i.e.,
change the call shown next to model.load_state_dict to use
torch.load("model_weights.pt", weights_only=True)); reference the
model.load_state_dict and torch.load usage in conversion.py so readers see the
explicit secure flag.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: a5f245a3-81e0-4c09-8930-881be6f870b2
📒 Files selected for processing (1)
modelopt/torch/opt/conversion.py
What does this PR do?
Type of change: Bug fix (Security)
This PR enables
weights_only=Trueby default inload_modelopt_stateandrestorefunctions.The
modelopt_stateis designed to store metadata using Pydantic'smodel_dump(), which results in plain Python dictionaries and basic types (lists, strings, numbers). These types are natively supported by PyTorch'sweights_only=Truemode.Enabling this by default provides a critical security hardening against potential RCE from malicious state files without breaking existing workflows.
Before your PR is "Ready for review"
CONTRIBUTING.md: N/AAdditional Information
Related to issue #1055.
Summary by CodeRabbit