transformers - ✅(Solved) Fix _get_tied_weight_keys crashes with AttributeError when _tied_weights_keys is a list [1 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
huggingface/transformers#44861Fetched 2026-04-08 01:03:27
View on GitHub
Comments
0
Participants
1
Timeline
4
Reactions
0
Author
Participants
Timeline (top)
closed ×1cross-referenced ×1mentioned ×1subscribed ×1

Fix Action

Fixed

PR fix notes

PR #44862: fix: handle list-type _tied_weights_keys in _get_tied_weight_keys

Description (problem / solution / changelog)

Some models (e.g. Nemotron-H) define _tied_weights_keys as a list, which caused AttributeError: 'list' object has no attribute 'keys' when calling save_pretrained during full finetuning.

What does this PR do?

_get_tied_weight_keys in modeling_utils.py calls .keys() on the _tied_weights_keys attribute, assuming it's always a dict. However, some models (e.g. Nemotron-H) define _tied_weights_keys as a list, causing an AttributeError when saving checkpoints during full finetuning.

This fix handles both list/tuple and dict types for _tied_weights_keys.

Traceback: File "transformers/modeling_utils.py", line 341, in _get_tied_weight_keys tied_weight_keys.extend([f"{name}.{k}" if name else k for k in tied.keys()]) AttributeError: 'list' object has no attribute 'keys'

Reproduction: Full finetune of NVIDIA-Nemotron-3-Nano-4B via Unsloth Studio with Transformers 5.3.0 crashes at checkpoint save.

Fixes #44861

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline, Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the documentation guidelines, and here are tips on formatting docstrings.
  • Did you write any new necessary tests?

AI Disclosure: This PR was developed with assistance from Claude Code (Claude Opus 4.6). All changes were reviewed and validated by a human.

Who can review?

@Cyrilvallez — model loading / save_pretrained code path

Changed files

  • src/transformers/modeling_utils.py (modified, +5/-2)
  • tests/utils/test_modeling_utils.py (modified, +14/-0)
RAW_BUFFERClick to expand / collapse

System Info

  • transformers version: 5.3.0
  • Platform: Linux
  • Python version: 3.13

Who can help?

@Cyrilvallez

Information

  • The official example scripts
  • My own modified scripts

Reproduction

Full finetune of NVIDIA-Nemotron-3-Nano-4B crashes at checkpoint save with:

File "transformers/modeling_utils.py", line 341, in _get_tied_weight_keys tied_weight_keys.extend([f"{name}.{k}" if name else k for k in tied.keys()]) AttributeError: 'list' object has no attribute 'keys'

The Nemotron-H model defines _tied_weights_keys as a list, but _get_tied_weight_keys assumes it is always a dict and calls .keys() on it.

Expected behavior

save_pretrained should handle both list and dict types for _tied_weights_keys without crashing.

extent analysis

Fix Plan

To fix the issue, we need to modify the _get_tied_weight_keys function in transformers/modeling_utils.py to handle both list and dict types for _tied_weights_keys.

Step-by-Step Solution

  • Check the type of _tied_weights_keys and handle it accordingly:
def _get_tied_weight_keys(pretrained_model, model, is_bert):
    ...
    if isinstance(tied, dict):
        tied_weight_keys.extend([f"{name}.{k}" if name else k for k in tied.keys()])
    elif isinstance(tied, list):
        tied_weight_keys.extend([f"{name}.{k}" if name else k for k in tied])
    ...

However, since tied is a list of strings when _tied_weights_keys is a list, we can simply extend tied_weight_keys with tied:

def _get_tied_weight_keys(pretrained_model, model, is_bert):
    ...
    if isinstance(tied, dict):
        tied_weight_keys.extend([f"{name}.{k}" if name else k for k in tied.keys()])
    elif isinstance(tied, list):
        tied_weight_keys.extend(tied)
    ...

Verification

To verify the fix, run the full finetune of NVIDIA-Nemotron-3-Nano-4B and check if the checkpoint saves without crashing.

Extra Tips

  • Make sure to test the fix with different types of _tied_weights_keys to ensure it handles all cases correctly.
  • Consider submitting a pull request to the transformers library to include this fix in future versions.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

transformers - ✅(Solved) Fix _get_tied_weight_keys crashes with AttributeError when _tied_weights_keys is a list [1 pull requests, 1 participants]