transformers - ✅(Solved) Fix [v5] Inconsistent serialization of `lm_head.weight` (tied weights?) depending on model device in v5/`main`, while v4.57 behaves correctly [1 pull requests, 3 comments, 3 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#44466Fetched 2026-04-08 00:28:16
View on GitHub
Comments
3
Participants
3
Timeline
18
Reactions
0
Timeline (top)
subscribed ×6mentioned ×5commented ×3cross-referenced ×2

Fix Action

Fixed

PR fix notes

PR #44497: [tie weights] 🚨 If both weights are present with same weights, still tie them

Description (problem / solution / changelog)

What does this PR do?

As per the title. Fixes https://github.com/huggingface/transformers/issues/44466 and avoid issues with torch .bin checkpoints which always contain both keys!

Changed files

  • src/transformers/modeling_utils.py (modified, +13/-10)
  • tests/utils/test_modeling_utils.py (modified, +30/-4)

Code Example

- `transformers` version: 5.3.0.dev0
- Platform: Linux-6.8.0-100-generic-x86_64-with-glibc2.39
- Python version: 3.12.12
- Huggingface_hub version: 1.5.0
- Safetensors version: 0.7.0
- Accelerate version: 1.12.0
- Accelerate config:    not found
- DeepSpeed version: not installed
- PyTorch version (accelerator?): 2.9.1+rocm6.4 (CUDA)
- Using distributed or parallel set-up in script?: <fill in>
- Using GPU in script?: <fill in>
- GPU type: AMD Instinct MI300X

---

from transformers import AutoModelForCausalLM
from safetensors import safe_open
from pathlib import Path
from packaging import version
import transformers

model = AutoModelForCausalLM.from_pretrained("facebook/opt-125m")

move_to_device = False  # toggle this flag.
if move_to_device:
    print("model to cuda")
    model.eval()
    model = model.to("cuda")

model.save_pretrained("opt-125m-saved")

with safe_open(Path("opt-125m-saved", "model.safetensors"), framework="pt") as f:
    checkpoint_keys = f.keys()

    for name in checkpoint_keys:
        print("checkpoint key", name)

    if move_to_device:
        if version.parse(transformers.__version__) >= version.parse("5.0"):
            # NOTE: this looks wrong / to be a regression to me.
            assert "lm_head.weight" in checkpoint_keys
        else:
            # NOTE: transformers<5 behavior looks to be the correct one to me.
            assert "lm_head.weight" not in checkpoint_keys
    else:
        assert "lm_head.weight" not in checkpoint_keys
RAW_BUFFERClick to expand / collapse

System Info

- `transformers` version: 5.3.0.dev0
- Platform: Linux-6.8.0-100-generic-x86_64-with-glibc2.39
- Python version: 3.12.12
- Huggingface_hub version: 1.5.0
- Safetensors version: 0.7.0
- Accelerate version: 1.12.0
- Accelerate config:    not found
- DeepSpeed version: not installed
- PyTorch version (accelerator?): 2.9.1+rocm6.4 (CUDA)
- Using distributed or parallel set-up in script?: <fill in>
- Using GPU in script?: <fill in>
- GPU type: AMD Instinct MI300X

and 4.57.6 for testing.

Who can help?

@ArthurZucker @Cyrilvallez

Information

  • The official example scripts
  • My own modified scripts

Tasks

  • An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
  • My own task or dataset (give details below)

Reproduction

Hi,

As mentioned in the title, it looks to me something is broken in the serialization of tied weights in Transformers v5 (tried both v5.2 and current main @ 81b53436bf97b8cb02040839a902e0b799be3159).

When moving a model to cuda device, lm_head.weight gets serialized while it does not when staying on CPU. This is inconsistent with v4.57 behavior (consistent serialization on all devices) and looks strange to me.

What do you think?

from transformers import AutoModelForCausalLM
from safetensors import safe_open
from pathlib import Path
from packaging import version
import transformers

model = AutoModelForCausalLM.from_pretrained("facebook/opt-125m")

move_to_device = False  # toggle this flag.
if move_to_device:
    print("model to cuda")
    model.eval()
    model = model.to("cuda")

model.save_pretrained("opt-125m-saved")

with safe_open(Path("opt-125m-saved", "model.safetensors"), framework="pt") as f:
    checkpoint_keys = f.keys()

    for name in checkpoint_keys:
        print("checkpoint key", name)

    if move_to_device:
        if version.parse(transformers.__version__) >= version.parse("5.0"):
            # NOTE: this looks wrong / to be a regression to me.
            assert "lm_head.weight" in checkpoint_keys
        else:
            # NOTE: transformers<5 behavior looks to be the correct one to me.
            assert "lm_head.weight" not in checkpoint_keys
    else:
        assert "lm_head.weight" not in checkpoint_keys

Expected behavior

model.safetensors after save_pretrained should contain the same tensor keys independently of the device the PretrainedModel is placed on.

It used to be the case with v4.57.

It is not anymore the case with v5.

extent analysis

Fix Plan

1. Update transformers version to a stable release

Update the transformers version to a stable release (e.g., 5.3.0 or 5.2.1) to avoid potential regressions.

2. Use save_pretrained with use_cache=False

Use save_pretrained with use_cache=False to ensure that tied weights are not cached and serialized correctly.

model.save_pretrained("opt-125m-saved", use_cache=False)

3. Update safetensors version to a compatible version

Update the safetensors version to a compatible version (e.g., 0.7.1 or 0.8.0) to ensure compatibility with the updated transformers version.

4. Verify the fix

Run the reproduction script with the updated versions and verify that the lm_head.weight tensor is serialized correctly on both CPU and CUDA devices.

Verification

  1. Run the reproduction script with the updated versions.
  2. Check the output of the script to ensure that the lm_head.weight tensor is serialized correctly on both CPU and CUDA devices.
  3. Verify that the expected behavior is achieved, i.e., the model.safetensors file contains the same tensor keys independently of the device the PretrainedModel is placed on.

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…

FAQ

Expected behavior

model.safetensors after save_pretrained should contain the same tensor keys independently of the device the PretrainedModel is placed on.

It used to be the case with v4.57.

It is not anymore the case with v5.

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 [v5] Inconsistent serialization of `lm_head.weight` (tied weights?) depending on model device in v5/`main`, while v4.57 behaves correctly [1 pull requests, 3 comments, 3 participants]