Skip to content

[loading] tie_weights is finally symmetric - #42507

Merged
Cyrilvallez merged 11 commits into
mainfrom
symetric-tie-weights
Dec 1, 2025
Merged

Cyrilvallez merged 11 commits into
mainfrom
symetric-tie-weights

Conversation

@Cyrilvallez

@Cyrilvallez Cyrilvallez commented Dec 1, 2025 •

Copy link
Copy Markdown
Member

What does this PR do?

As per the title. This makes tie_weights work fully symmetrically, even in cases where one weight is tied between more than 2 keys.
This will hopefully end the pain of all models checkpoints having the saved weight in the "wrong" key.

This fixes several opened issues: #42313 (though we still have an issue with the tokenizer here apparently), #42460 and I think others that I lost track of.

Note: for facebook/bart-large bart checkpoint, all 3 tied weight keys are explicitly present in the safetensors checkpoint. So according to this PR, they are not tied anymore (with a warning), whereas they would be anyway before. IMO this is fine as if they are in the checkpoints, they were not tied when saved. We could introduce a value check if both source and targets are present and still tie if everything is equal, but I think this is too expensive and too niche to add for now. We may consider doing it if we notice this is more frequent so those bart checkpoints

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

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.

@molbap molbap left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Read through the new logic, thanks! I suggested some changes in the comments for clarity mostly, logic seems sound except the ordering in case of missing both keys but that's it!

Comment thread src/transformers/modeling_utils.py Resolved
Comment thread src/transformers/modeling_utils.py Resolved
Comment thread src/transformers/modeling_utils.py Resolved
Comment thread src/transformers/modeling_utils.py Resolved
target_param_names = [target_param_name]
# Both are missing -> check other keys in case more than 2 keys are tied to the same weight
elif not source_is_there and not target_is_there:
for target_backup, source_backup in tied_keys[i + 1 :]:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

maybe dumb question, why do we only search forward here? is there some ordering implied?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Since they all have the same source, and we tie both source and target if we find it somewhere at the beginning when both are missinf, all later iterations will not enter this branch anymore as the source is now present 👌 But you're definitely right to ask, not necessarily trivial!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

aah got it. Thanks! Yeah all live modifications of iterables are hard to track haha

@ArthurZucker ArthurZucker left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, let's make sure the marian and etc models are fixed !

# checkpoint -> let's raise a warning and do nothing
if source_is_there and target_is_there:
logger.warning(
f"The tied weights mapping and config for this model specifies to tie {source_param_name} to "

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

very nice and actionnable

Comment thread src/transformers/modeling_utils.py Resolved
Comment thread tests/utils/test_modeling_utils.py Resolved
@github-actions

github-actions Bot commented Dec 1, 2025

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: bart, openai

@Cyrilvallez
Cyrilvallez merged commit 9f58710 into main Dec 1, 2025
@Cyrilvallez
Cyrilvallez deleted the symetric-tie-weights branch December 1, 2025 14:39
sarathc-cerebras pushed a commit to sarathc-cerebras/transformers that referenced this pull request Dec 7, 2025
* symetric tie weights

* remove bart ones i added the other day

* remove open ai skip i added the other day

* style

* clarify comments

* unfortunately cannot raise now

* typo

* add flag

* typo

* add tests

* more tests
SangbumChoi pushed a commit to SangbumChoi/transformers that referenced this pull request Jan 23, 2026
* symetric tie weights

* remove bart ones i added the other day

* remove open ai skip i added the other day

* style

* clarify comments

* unfortunately cannot raise now

* typo

* add flag

* typo

* add tests

* more tests
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.

4 participants