Safetensors checkpoint loading fails with a torch.device map_location

オープン 初心者向け
#15,459 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
78/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python, pytorch

調査の方向性

src/diffusers/models/model_loading_utils.pyから始めます。ここではload_state_dictがsafetensorsチェックポイントを処理しています。map_locationの扱いを.binの処理と比較してください。issueにある1テンソルの再現例を使って失敗を確認し、関連するモデル読み込みテストを確認してください。map_location=torch.device("cpu")を渡したときに有効なチェックポイントが読み込まれれば完了です。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Python
スター
35k
フォーク
7.5k
平均マージ
3日 2時間
マージ済み PR(30日)
58

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

huggingface/diffusers のほかの issue

huggingface/diffusers の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。