safetensors 项目常见问题解决方案

safetensors 项目常见问题解决方案

safetensors Simple, safe way to store and distribute tensors safetensors 项目地址: https://gitcode.com/gh_mirrors/sa/safetensors

项目基础介绍

safetensors 是一个用于安全存储和分发张量的开源项目。与传统的 pickle 格式不同,safetensors 提供了一种更安全的方式来存储和加载张量数据,同时保持高效的性能(零拷贝)。该项目主要使用 Python 和 Rust 语言进行开发,适合需要处理大规模张量数据的开发者使用。

主要编程语言

  • Python: 用于实现项目的核心功能和提供用户友好的接口。
  • Rust: 用于实现高性能的底层操作,确保数据的安全性和高效性。

新手使用注意事项及解决方案

1. 安装 Rust 环境

问题描述: 新手在尝试从源码安装 safetensors 时,可能会遇到 Rust 环境未安装或版本不匹配的问题。

解决步骤:

  1. 安装 Rust: 使用以下命令安装 Rust 环境。
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
    
  2. 更新 Rust: 确保 Rust 环境是最新的稳定版本。
    rustup update
    
  3. 克隆项目并安装: 克隆 safetensors 项目并安装依赖。
    git clone https://github.com/huggingface/safetensors
    cd safetensors/bindings/python
    pip install setuptools_rust
    pip install -e .
    

2. 安装 Python 依赖

问题描述: 新手在安装 safetensors 时,可能会遇到 Python 依赖库未安装或版本不匹配的问题。

解决步骤:

  1. 安装 pip: 确保系统中已安装 pip。
    sudo apt-get install python3-pip
    
  2. 安装 safetensors: 使用 pip 安装 safetensors。
    pip install safetensors
    
  3. 检查依赖: 如果安装过程中出现依赖问题,可以使用以下命令安装缺失的依赖。
    pip install -r requirements.txt
    

3. 使用 safe_open 时设备错误

问题描述: 新手在使用 safe_open 函数时,可能会遇到设备错误,例如指定设备为 cpu 但实际运行在 gpu 上。

解决步骤:

  1. 检查设备: 在使用 safe_open 函数时,确保指定的设备与实际运行环境一致。
    with safe_open("model.safetensors", framework="pt", device="cpu") as f:
        for key in f.keys():
            tensors[key] = f.get_tensor(key)
    
  2. 调试信息: 如果出现设备错误,可以在代码中添加调试信息,检查设备是否正确。
    import torch
    print(torch.cuda.is_available())  # 检查 GPU 是否可用
    
  3. 切换设备: 如果需要在 GPU 上运行,确保代码中指定 device="cuda"
    with safe_open("model.safetensors", framework="pt", device="cuda") as f:
        for key in f.keys():
            tensors[key] = f.get_tensor(key)
    

通过以上步骤,新手可以顺利解决在使用 safetensors 项目时遇到的常见问题。

safetensors Simple, safe way to store and distribute tensors safetensors 项目地址: https://gitcode.com/gh_mirrors/sa/safetensors

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

root@autodl-container-8bf9409db0-3243e00c:~/autodl-tmp# python utils_one.py 2025-06-04 22:18:14,601 - __main__ - INFO - 初始化对话评估系统... 2025-06-04 22:18:14,602 - __main__ - INFO - 加载嵌入模型: /root/autodl-tmp/paraphrase-multilingual-MiniLM-L12-v2 2025-06-04 22:18:16,425 - __main__ - INFO - 嵌入模型加载成功,设备: cuda 2025-06-04 22:18:16,426 - __main__ - INFO - 加载生成模型: /root/autodl-tmp/Qwen3-14b 2025-06-04 22:18:16,651 - accelerate.utils.modeling - INFO - We will use 90% of the memory on device 0 for storing the model, and 10% for the buffer to avoid OOM. You can set `max_memory` in to a higher value to use more memory (at your own risk). Loading checkpoint shards: 67%|██████████████████████████████████████████████████████████████████████████████████████████████████████▋ | 4/6 [00:03<00:01, 1.27it/s] 2025-06-04 22:18:19,830 - __main__ - ERROR - 生成模型加载失败: Error while deserializing header: MetadataIncompleteBuffer 2025-06-04 22:18:19,830 - __main__ - ERROR - 生成模型加载失败: Error while deserializing header: MetadataIncompleteBuffer 2025-06-04 22:18:19,830 - __main__ - ERROR - 评估系统初始化失败: Error while deserializing header: MetadataIncompleteBuffer Traceback (most recent call last): File "/root/autodl-tmp/utils_one.py", line 482, in <module> evaluator = DialogueEvaluator( ^^^^^^^^^^^^^^^^^^ File "/root/autodl-tmp/utils_one.py", line 231, in __init__ self.dialogue_generator = DialogueGenerator(gen_model_path, device) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/root/autodl-tmp/utils_one.py", line 88, in __init__ self.model = AutoModelForCausalLM.from_pretrained( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/root/miniconda3/lib/python3.12/site-packages/transformers/models/auto/auto_factory.py", line 571, in from_pretrained return model_class.from_pretrained( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/root/miniconda3/lib/python3.12/site-packages/transformers/modeling_utils.py", line 279, in _wrapper return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/root/miniconda3/lib/python3.12/site-packages/transformers/modeling_utils.py", line 4400, in from_pretrained ) = cls._load_pretrained_model( ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/root/miniconda3/lib/python3.12/site-packages/transformers/modeling_utils.py", line 4818, in _load_pretrained_model state_dict = load_state_dict( ^^^^^^^^^^^^^^^^ File "/root/miniconda3/lib/python3.12/site-packages/transformers/modeling_utils.py", line 513, in load_state_dict with safe_open(checkpoint_file, framework="pt") as f: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ safetensors_rust.SafetensorError: Error while deserializing header: MetadataIncompleteBuffer
最新发布
06-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐举跃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值