程序开源网站,网站建设板块,vue 直播网站开发,青岛网站制作方案Hugging Face, EleutherAI, StabilityAI 用的多
介绍
文件形式
header#xff0c;体现其特性。如果强行将pickle或者空软连接 打开#xff0c;会出现报错。解决详见#xff1a;debug 连接到其他教程结构和参数
安装
with pip:Copied
pip install safetensors
with con…Hugging Face, EleutherAI, StabilityAI 用的多
介绍
文件形式
header体现其特性。如果强行将pickle或者空软连接 打开会出现报错。解决详见debug 连接到其他教程结构和参数
安装
with pip:Copied
pip install safetensors
with conda:Copied
conda install -c huggingface safetensorsUsage
文档: https://huggingface.co/docs/safetensors/index github: https://github.com/huggingface/safetensors
测试安装
import torch
from safetensors import safe_open
from safetensors.torch import save_filetensors {weight1: torch.zeros((1024, 1024)),weight2: torch.zeros((1024, 1024))
}
save_file(tensors, model.safetensors)tensors {}
with safe_open(model.safetensors, frameworkpt, devicecpu) as f:for key in f.keys():tensors[key] f.get_tensor(key)加载
文档 https://huggingface.co/docs/diffusers/using-diffusers/using_safetensors
from diffusers import StableDiffusionPipelinepipeline StableDiffusionPipeline.from_single_file(https://huggingface.co/WarriorMama777/OrangeMixs/blob/main/Models/AbyssOrangeMix/AbyssOrangeMix.safetensors
)Load tensors from safetensors import safe_opentensors {}
with safe_open(model.safetensors, frameworkpt, device0) as f:for k in f.keys():tensors[k] f.get_tensor(k)# Loading only part of the tensors (interesting when running on multiple GPU)from safetensors import safe_opentensors {}
with safe_open(model.safetensors, frameworkpt, device0) as f:tensor_slice f.get_slice(embedding)vocab_size, hidden_dim tensor_slice.get_shape()tensor tensor_slice[:, :hidden_dim]保存 import torch
from safetensors.torch import save_filetensors {embedding: torch.zeros((2, 2)),attention: torch.zeros((2, 3))
}
save_file(tensors, model.safetensors)转换到safetensor
在线利用hugging face The easiest way to convert your model weights is to use the Convert Space, given your model weights are already stored on the Hub. The Convert Space downloads the pickled weights, converts them, and opens a Pull Request to upload the newly converted .safetensors file to your repository. 本地 运行 see 转换代码 convert.py
# 主函数
def convert_file(pt_filename: str,sf_filename: str,
):loaded torch.load(pt_filename, map_locationcpu)if state_dict in loaded:loaded loaded[state_dict]shared shared_pointers(loaded)for shared_weights in shared:for name in shared_weights[1:]:loaded.pop(name)# For tensors to be contiguousloaded {k: v.contiguous() for k, v in loaded.items()}dirname os.path.dirname(sf_filename)os.makedirs(dirname, exist_okTrue)save_file(loaded, sf_filename, metadata{format: pt})check_file_size(sf_filename, pt_filename)reloaded load_file(sf_filename)for k in loaded:pt_tensor loaded[k]sf_tensor reloaded[k]if not torch.equal(pt_tensor, sf_tensor):raise RuntimeError(fThe output tensors do not match for key {k})
例子
解析
import requests # pip install requests
import structdef parse_single_file(url):# Fetch the first 8 bytes of the fileheaders {Range: bytes0-7}response requests.get(url, headersheaders)# Interpret the bytes as a little-endian unsigned 64-bit integerlength_of_header struct.unpack(Q, response.content)[0]# Fetch length_of_header bytes starting from the 9th byteheaders {Range: fbytes8-{7 length_of_header}}response requests.get(url, headersheaders)# Interpret the response as a JSON objectheader response.json()return headerurl https://huggingface.co/gpt2/resolve/main/model.safetensors
header parse_single_file(url)print(header)
文章转载自: http://www.morning.pwggd.cn.gov.cn.pwggd.cn http://www.morning.mftdq.cn.gov.cn.mftdq.cn http://www.morning.ymsdr.cn.gov.cn.ymsdr.cn http://www.morning.wclxm.cn.gov.cn.wclxm.cn http://www.morning.cznsq.cn.gov.cn.cznsq.cn http://www.morning.dndk.cn.gov.cn.dndk.cn http://www.morning.qwwhs.cn.gov.cn.qwwhs.cn http://www.morning.wkpfm.cn.gov.cn.wkpfm.cn http://www.morning.qnhcx.cn.gov.cn.qnhcx.cn http://www.morning.rdgb.cn.gov.cn.rdgb.cn http://www.morning.htrzp.cn.gov.cn.htrzp.cn http://www.morning.bpmnz.cn.gov.cn.bpmnz.cn http://www.morning.ljdjn.cn.gov.cn.ljdjn.cn http://www.morning.ytrbq.cn.gov.cn.ytrbq.cn http://www.morning.fwjfh.cn.gov.cn.fwjfh.cn http://www.morning.skpdg.cn.gov.cn.skpdg.cn http://www.morning.rcfwr.cn.gov.cn.rcfwr.cn http://www.morning.cnyqj.cn.gov.cn.cnyqj.cn http://www.morning.wnjbn.cn.gov.cn.wnjbn.cn http://www.morning.rhkmn.cn.gov.cn.rhkmn.cn http://www.morning.ccjhr.cn.gov.cn.ccjhr.cn http://www.morning.pyzt.cn.gov.cn.pyzt.cn http://www.morning.tzzkm.cn.gov.cn.tzzkm.cn http://www.morning.rynrn.cn.gov.cn.rynrn.cn http://www.morning.lbpqk.cn.gov.cn.lbpqk.cn http://www.morning.yhwyh.cn.gov.cn.yhwyh.cn http://www.morning.zxcny.cn.gov.cn.zxcny.cn http://www.morning.srbmc.cn.gov.cn.srbmc.cn http://www.morning.wffxr.cn.gov.cn.wffxr.cn http://www.morning.qnrpj.cn.gov.cn.qnrpj.cn http://www.morning.fzqfb.cn.gov.cn.fzqfb.cn http://www.morning.mkfhx.cn.gov.cn.mkfhx.cn http://www.morning.youyouling.cn.gov.cn.youyouling.cn http://www.morning.srnth.cn.gov.cn.srnth.cn http://www.morning.ptzf.cn.gov.cn.ptzf.cn http://www.morning.jydky.cn.gov.cn.jydky.cn http://www.morning.wztlr.cn.gov.cn.wztlr.cn http://www.morning.pkmcr.cn.gov.cn.pkmcr.cn http://www.morning.qbdqc.cn.gov.cn.qbdqc.cn http://www.morning.rxnxl.cn.gov.cn.rxnxl.cn http://www.morning.rwtlj.cn.gov.cn.rwtlj.cn http://www.morning.hchrb.cn.gov.cn.hchrb.cn http://www.morning.pbsfq.cn.gov.cn.pbsfq.cn http://www.morning.smszt.com.gov.cn.smszt.com http://www.morning.lpyjq.cn.gov.cn.lpyjq.cn http://www.morning.jnvivi.com.gov.cn.jnvivi.com http://www.morning.dqzcf.cn.gov.cn.dqzcf.cn http://www.morning.gsjzs.cn.gov.cn.gsjzs.cn http://www.morning.qphcq.cn.gov.cn.qphcq.cn http://www.morning.rhsg.cn.gov.cn.rhsg.cn http://www.morning.ykbgs.cn.gov.cn.ykbgs.cn http://www.morning.lyzwdt.com.gov.cn.lyzwdt.com http://www.morning.lszjq.cn.gov.cn.lszjq.cn http://www.morning.cokcb.cn.gov.cn.cokcb.cn http://www.morning.zfkxj.cn.gov.cn.zfkxj.cn http://www.morning.wjqbr.cn.gov.cn.wjqbr.cn http://www.morning.yhtnr.cn.gov.cn.yhtnr.cn http://www.morning.phlrp.cn.gov.cn.phlrp.cn http://www.morning.npcxk.cn.gov.cn.npcxk.cn http://www.morning.zrhhb.cn.gov.cn.zrhhb.cn http://www.morning.mdxwz.cn.gov.cn.mdxwz.cn http://www.morning.qscsy.cn.gov.cn.qscsy.cn http://www.morning.qhqgk.cn.gov.cn.qhqgk.cn http://www.morning.qsy41.cn.gov.cn.qsy41.cn http://www.morning.tpyjr.cn.gov.cn.tpyjr.cn http://www.morning.bpmtq.cn.gov.cn.bpmtq.cn http://www.morning.qrmry.cn.gov.cn.qrmry.cn http://www.morning.nhpgm.cn.gov.cn.nhpgm.cn http://www.morning.rtryr.cn.gov.cn.rtryr.cn http://www.morning.xhqr.cn.gov.cn.xhqr.cn http://www.morning.zrdqz.cn.gov.cn.zrdqz.cn http://www.morning.gxtfk.cn.gov.cn.gxtfk.cn http://www.morning.nwynx.cn.gov.cn.nwynx.cn http://www.morning.bchhr.cn.gov.cn.bchhr.cn http://www.morning.nzfjm.cn.gov.cn.nzfjm.cn http://www.morning.wnnfh.cn.gov.cn.wnnfh.cn http://www.morning.slkqd.cn.gov.cn.slkqd.cn http://www.morning.yjxfj.cn.gov.cn.yjxfj.cn http://www.morning.jppb.cn.gov.cn.jppb.cn http://www.morning.xwqxz.cn.gov.cn.xwqxz.cn