Fix TypeError in save_pretrained error handling (fixes #38422) #38449
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a TypeError in the
save_pretrained
error handling routine that occurs whenshared_names
contains shared tensors.Problem
In
src/transformers/modeling_utils.py
line 3747, the code attempts to callset(shared_names)
whereshared_names
isList[Set[str]]
. This raises:TypeError: unhashable type: 'set'
The bug occurs because you can't create a set from a list of sets (sets are unhashable).
Solution
Replace
error_names.append(set(shared_names))
witherror_names.extend(shared_names)
to properly handle theList[Set[str]]
structure.This preserves the intended behavior where
error_names
is a list of sets, with each set representing a group of tensors that share memory/storage.Testing
Related Issue
Fixes #38422
Changes
src/transformers/modeling_utils.py
: Replaceappend(set(shared_names))
withextend(shared_names)