Skip to content

Fix TypeError in save_pretrained error handling (fixes #38422) #38449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

rahulrshetty45
Copy link
Contributor

Summary

Fixes a TypeError in the save_pretrained error handling routine that occurs when shared_names contains shared tensors.

Problem

In src/transformers/modeling_utils.py line 3747, the code attempts to call set(shared_names) where shared_names is List[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)) with error_names.extend(shared_names) to properly handle the List[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

  • Verified syntax and imports work correctly
  • Tested the logic with sample data
  • Confirmed backward compatibility
  • Single line change with minimal impact

Related Issue

Fixes #38422

Changes

  • src/transformers/modeling_utils.py: Replace append(set(shared_names)) with extend(shared_names)

Copy link
Member

@Rocketknight1 Rocketknight1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this fix LGTM now! Thank you for the contribution and for iterating on the original

@Rocketknight1 Rocketknight1 enabled auto-merge (squash) May 29, 2025 13:26
@Rocketknight1 Rocketknight1 force-pushed the fix-save-pretrained-error-handling-38422 branch from 2f0e027 to e43a239 Compare May 29, 2025 13:32
@Rocketknight1 Rocketknight1 merged commit 8e5cefc into huggingface:main May 29, 2025
20 checks passed
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

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.

Bug in error handling routine in save_pretrained
3 participants