Skip to content

Safetensors checkpoint loading fails with a torch.device map_location #15459

Backlog

Description

@mertkayacs

Describe the bug

I get an OSError when I pass torch.device("cpu") to load_state_dict for a valid safetensors checkpoint. The helper accepts torch.device in its type annotation, and the same argument works for .bin checkpoints. I have a small fix for the safetensors call and would like to submit it.

Reproduction

from pathlib import Path
from tempfile import TemporaryDirectory

import torch
from safetensors.torch import save_file

from diffusers.models.model_loading_utils import load_state_dict

with TemporaryDirectory() as directory:
    path = Path(directory) / "model.safetensors"
    save_file({"weight": torch.ones(1)}, path)
    load_state_dict(path, map_location=torch.device("cpu"))

Context

I found this while reading checkpoint loading in model_loading_utils.py, then reproduced it with the one-tensor checkpoint above.

Logs

SafetensorError: device device(type='cpu') is invalid
OSError: Unable to load weights from checkpoint file

System Info

Diffusers 0.44.0.dev0 at 08d5de3, Linux x86_64, Python 3.14.5, PyTorch 2.15.0+cpu, safetensors 0.8.0. CPU only, single process.

Activity

  1. LEKKALAGANESH commented on Dec 4, 2026

    @LEKKALAGANESH

    Hi @mertkayacs, are you still planning to open a PR for this? The torch.device → safe_open(device=...) mismatch in load_state_dict is easy to reproduce with your snippet. If you'd rather not, I'm happy to pick it up with a regression test for the safetensors path. No rush either way.

  2. mertkayacs commented on Dec 4, 2026

    @mertkayacs
    ContributorAuthor

    Thanks for checking, @LEKKALAGANESH. I've opened #15489 with a regression test for the safetensors path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions