#101·HRM

Replace bare except with explicit exception in model loading (evaluate.py)

Author: somdiptoCreated Dec 23, 2025Updated Dec 23, 2025

Problem

In evaluate.py, the model loading section uses a bare except: clause:

python
try:
    train_state.model.load_state_dict(torch.load(eval_cfg.checkpoint, map_location="cuda"), assign=True)
except:
    train_state.model.load_state_dict({k.removeprefix("_orig_mod."): v for k, v in torch.load(eval_cfg.checkpoint, map_location="cuda").items()}, assign=True)

Using a bare except: is dangerous because it catches all exceptions, including KeyboardInterrupt and SystemExit, which can hide bugs, interrupt signals, or errors that should be handled differently.

Recommended Fix

Replace the bare except: with explicit exception(s) such as except RuntimeError as e: or another relevant exception. Also, consider printing/logging the exception for easier debugging.

Proposed Solution

Example update:

python
try:
    train_state.model.load_state_dict(torch.load(eval_cfg.checkpoint, map_location="cuda"), assign=True)
except RuntimeError as e:
    print(f"Model load error: {e}")
    train_state.model. load_state_dict({k. removeprefix("_orig_mod."): v for k, v in torch.load(eval_cfg. checkpoint, map_location="cuda").items()}, assign=True)

Benefits:

  • Conforms to Python best practices
  • Prevents masking other errors
  • Improves maintainability and debugging

References