#4339·accelerate

FSDP2 get_state_dict() does not preserve tied-weight aliasing

Author: UnitedSnakesCreated Sep 26, 2026Updated Sep 26, 2026

System Info

bash
- `Accelerate` version: 1.16.0.dev0
- Platform: Linux-6.8.0-134-generic-x86_64-with-glibc2.39
- `accelerate` bash location: /workspace/transformer-tied-weights/.venv/bin/accelerate
- Python version: 3.12.3
- Numpy version: 2.5.2
- PyTorch version: 2.14.0+cu132
- PyTorch accelerator: CUDA
- System RAM: 503.51 GB
- GPU type: NVIDIA RTX A5000
- `Accelerate` default config:
        Not found

Information

  • The official example scripts
  • My own modified scripts

Tasks

  • One of the scripts in the examples/ folder of Accelerate or an officially supported no_trainer script in the examples folder of the transformers repo (such as run_no_trainer_glue.py)
  • My own task or dataset (give details below)

Reproduction

With FSDP2, tied parameters remain tied after accelerator.prepare(), but Accelerator.get_state_dict() materializes them as separate tensors.

import torch
from accelerate import Accelerator
from accelerate.utils import FullyShardedDataParallelPlugin
from transformers import Qwen2Config, Qwen2ForCausalLM

EMBED = "model.embed_tokens.weight"
HEAD = "lm_head.weight"

plugin = FullyShardedDataParallelPlugin(
    fsdp_version=2,
    auto_wrap_policy="transformer_based_wrap",
    transformer_cls_names_to_wrap=["Qwen2DecoderLayer"],
)
accelerator = Accelerator(fsdp_plugin=plugin)

model = Qwen2ForCausalLM(
    Qwen2Config(
        vocab_size=1024,
        hidden_size=128,
        intermediate_size=256,
        num_hidden_layers=1,
        num_attention_heads=4,
        num_key_value_heads=2,
        tie_word_embeddings=True,
    )
)

optimizer = torch.optim.AdamW(model.parameters())
model, optimizer = accelerator.prepare(model, optimizer)

params = dict(model.named_parameters(remove_duplicate=False))
prepared_tied = params[EMBED] is params[HEAD]

state_dict = accelerator.get_state_dict(model)

if accelerator.is_main_process:
    ptr = lambda x: x.untyped_storage().data_ptr()
    print("after prepare:", prepared_tied)
    print("values equal:", torch.equal(state_dict[EMBED], state_dict[HEAD]))
    print("after state:", ptr(state_dict[EMBED]) == ptr(state_dict[HEAD]))

Run with:

python -m torch.distributed.run --standalone --nproc-per-node=2 repro.py

Output:

after prepare: True
values equal: True
after state: False

On Qwen/Qwen2.5-0.5B in bf16, this causes the tied embedding/head tensor to be serialized twice:

normal checkpoint:   988,097,824 bytes
FSDP2 checkpoint:  1,260,367,448 bytes
increase:             259.66 MiB (+27.55%)

This appears related to #2958. The unwrap-based fix in #2959 was later reverted in #3096.

A small fix is to record shared Parameter identities before materialization and restore only those aliases afterwards:

shared_parameters = {}
for name, parameter in model.named_parameters(remove_duplicate=False):
    shared_parameters.setdefault(id(parameter), []).append(name)

state_dict = get_model_state_dict(model, options=options)

for names in shared_parameters.values():
    present = [name for name in names if name in state_dict]
    if len(present) > 1:
        for alias in present[1:]:
            state_dict[alias] = state_dict[present[0]]

This does not infer ties from tensor equality; it only restores aliases present in the live model.

I have tested this fix with a regression test and a 2-GPU Qwen2.5-0.5B run. With the fix, the checkpoint size returns to 988,097,824 bytes.

I may be misunderstanding the intended semantics here, so please let me know if this behavior is expected or if there is a better place to address it.

Expected behavior

Accelerator.get_state_dict() should preserve alias relationships for parameters that are tied in the live model, so tied weights are not materialized and serialized as independent tensors.