Safetensors checkpoint loading fails with a torch.device map_location

Đang mở Phù hợp với người mới
#15,459 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
78/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python, pytorch
Lĩnh vực
backend, machine-learning

Hướng nghiên cứu

Bắt đầu tại src/diffusers/models/model_loading_utils.py, nơi load_state_dict xử lý các checkpoint safetensors, rồi so sánh cách xử lý map_location với đường dẫn .bin. Dùng ví dụ tái hiện chỉ có một tensor trong issue để xác nhận lỗi và kiểm tra các bài kiểm thử tải mô hình liên quan. Hoàn tất khi một checkpoint hợp lệ được tải với map_location=torch.device("cpu") được truyền vào.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Python
Star
35k
Fork
7.5k
Merge trung bình
3 ngày 2 giờ
Pull request đã merge (30 ngày)
58

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của huggingface/diffusers

Tất cả issue của huggingface/diffusers

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.