System Info
Code inspection plus a reproduction of the control flow; observed on main (134fa24).
Who can help?
Code owners for tensorrt_llm/_torch/models (@NVIDIA/trt-llm-models-devs per CODEOWNERS).
Information
Reproduction
HfWeightLoader._load_bin_or_path_file (tensorrt_llm/_torch/models/checkpoints/hf/weight_loader.py) returns from a finally block:
@staticmethod
def _load_bin_or_path_file(file):
try:
part_weights = torch.load(file, weights_only=True, map_location='cpu', mmap=True)
except Exception:
logger.warning(f"Failed to load {file} with mmap=True, fallback to mmap=False")
part_weights = torch.load(file, weights_only=True, map_location='cpu', mmap=False)
finally:
return part_weights
When the mmap=False retry also raises, part_weights was never assigned, so the return in finally raises UnboundLocalError and replaces the real error. This path is taken whenever a checkpoint has no *.safetensors and the loader falls back to *.bin/*.pth.
For example, a .bin-only directory that contains HF Trainer's training_args.bin fails torch.load(..., weights_only=True) with UnpicklingError: Weights only load failed ... Unsupported global: ... TrainingArguments, but the user sees:
[ERROR] Error executing _load_bin_or_path_file with args ('.../training_args.bin',): cannot access local variable 'part_weights' where it is not associated with a value
The load fails either way; the problem is that the error reported points at the loader rather than at the file and the real cause. Python 3.14 also emits SyntaxWarning: 'return' in a 'finally' block for this function (PEP 765).
Expected behavior
The original torch.load exception propagates.
Actual behavior
UnboundLocalError: cannot access local variable 'part_weights' ...
Additional notes
The fix is to drop the finally: and return after the try/except, which keeps the successful paths unchanged. This was also pointed out by an automated review on #7171. I have a PR ready and will link it here.
Written with AI assistance and reviewed line by line.
System Info
Code inspection plus a reproduction of the control flow; observed on
main(134fa24).Who can help?
Code owners for
tensorrt_llm/_torch/models(@NVIDIA/trt-llm-models-devsper CODEOWNERS).Information
Reproduction
HfWeightLoader._load_bin_or_path_file(tensorrt_llm/_torch/models/checkpoints/hf/weight_loader.py) returns from afinallyblock:When the
mmap=Falseretry also raises,part_weightswas never assigned, so thereturninfinallyraisesUnboundLocalErrorand replaces the real error. This path is taken whenever a checkpoint has no*.safetensorsand the loader falls back to*.bin/*.pth.For example, a
.bin-only directory that contains HF Trainer'straining_args.binfailstorch.load(..., weights_only=True)withUnpicklingError: Weights only load failed ... Unsupported global: ... TrainingArguments, but the user sees:The load fails either way; the problem is that the error reported points at the loader rather than at the file and the real cause. Python 3.14 also emits
SyntaxWarning: 'return' in a 'finally' blockfor this function (PEP 765).Expected behavior
The original
torch.loadexception propagates.Actual behavior
UnboundLocalError: cannot access local variable 'part_weights' ...Additional notes
The fix is to drop the
finally:and return after thetry/except, which keeps the successful paths unchanged. This was also pointed out by an automated review on #7171. I have a PR ready and will link it here.Written with AI assistance and reviewed line by line.