Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

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

Aperta Adatta ai principianti
#19,564 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@David-Wu1119 ci sta già lavorando.

Dal 22/9/2026.

  • #19565 di @David-Wu1119 — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
75/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python, pytorch
Ambito
backend, tooling

Direzione di ricerca

Il bug si trova in tensorrt_llm/_torch/models/checkpoints/hf/weight_loader.py nel metodo _load_bin_or_path_file. Leggere la funzione per comprendere il flusso try/except/finally. La correzione consiste nel rimuovere il blocco finally e la parte di return, e restituire part_weights dopo il blocco except. Testare creando una directory di checkpoint contenente solo file .bin e assicurandosi che venga segnalato l'errore originale di torch.load, non un UnboundLocalError.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Customized kernels
System Info

Code inspection plus a reproduction of the control flow; observed on main (134fa245).

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.

Lingua principale
Python
Stelle
14.8k
Fork
2.8k
Merge medio
3g 17h
PR unite (30g)
471

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di NVIDIA/TensorRT-LLM

Tutte le issue di NVIDIA/TensorRT-LLM

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.