Skip to content

[Bug] _load_bin_or_path_file hides the real torch.load error behind UnboundLocalError #19564

Description

@David-Wu1119

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

  • The official example scripts
  • My own modified scripts

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Customized kernels<NV>Specialized/modified CUDA kernels in TRTLLM for LLM ops, beyond standard TRT. Dev & perf.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions