Skip to content

Fix unreachable code and resource leak in hf.py#899

Open
Chessing234 wants to merge 1 commit intostate-spaces:mainfrom
Chessing234:fix/hf-utils-unreachable-code-and-resource-leak
Open

Fix unreachable code and resource leak in hf.py#899
Chessing234 wants to merge 1 commit intostate-spaces:mainfrom
Chessing234:fix/hf-utils-unreachable-code-and-resource-leak

Conversation

@Chessing234
Copy link
Copy Markdown

Summary

  • Fix unreachable code in load_state_dict_hf: The function had a premature return on the torch.load(...) line (line 18), which made the subsequent dtype conversion and device mapping code (lines 20-23) completely unreachable. This means when loading a model with a non-fp32 dtype, the state dict was never converted to the requested dtype or moved to the target device -- it silently returned fp32 weights on CPU instead. Fixed by assigning the torch.load result to state_dict and returning after conversion.
  • Fix resource leak in load_config_hf: The file handle from open() was passed directly to json.load() without being closed. Changed to use a with statement to ensure proper cleanup.

Test plan

  • Verify load_state_dict_hf correctly converts dtype when dtype is not torch.float32
  • Verify load_state_dict_hf correctly maps state dict to the specified device
  • Verify load_config_hf still correctly loads config files

In load_state_dict_hf, a premature `return` on the torch.load line made
the subsequent dtype conversion and device mapping code unreachable.
This meant that when loading a model with a non-fp32 dtype, the state
dict was never converted to the requested dtype or moved to the target
device, silently returning fp32 weights on CPU instead.

Fix by assigning the torch.load result to `state_dict` and returning
after the dtype/device conversion.

Also fix load_config_hf to use a `with` statement for the file handle
instead of passing an unclosed open() directly to json.load.

Signed-off-by: Taksh <taxkothari@gmail.com>
Signed-off-by: Taksh <takshkothari09@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant