快速入门Safetensors

news/2024/2/21 4:15:32

快速入门Safetensors

    • 什么是Safetensors
    • 架构
    • 常用操作
    • 速度对比
    • 彩蛋

Safetensors官方网址

什么是Safetensors

Safetensors是一种新的简单格式,用于安全存储张量(与pickle相反),而且速度仍然很快(零拷贝)。

架构

在这里插入图片描述

常用操作

# pip install safetensors# Load
from safetensors import safe_opentensors = {}
with safe_open("model.safetensors", framework="pt", device=0) 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", framework="pt", device=0) as f:tensor_slice = f.get_slice("embedding")vocab_size, hidden_dim = tensor_slice.get_shape()tensor = tensor_slice[:, :hidden_dim]# save
import torch
from safetensors.torch import save_filetensors = {"embedding": torch.zeros((2, 2)),"attention": torch.zeros((2, 3))
}
save_file(tensors, "model.safetensors")

速度对比

  • cpu

Loaded safetensors 0:00:00.004015
Loaded pytorch 0:00:00.307460
on CPU, safetensors is faster than pytorch by: 76.6 X

  • gpu

Loaded safetensors 0:00:00.165206
Loaded pytorch 0:00:00.353889
on GPU, safetensors is faster than pytorch by: 2.1 X

彩蛋

Lora的Safetensors格式存储有训练时的元数据信息

代码来自sd-web-ui项目源码

def read_metadata_from_safetensors(filename):import jsonwith open(filename, mode="rb") as file:metadata_len = file.read(8)metadata_len = int.from_bytes(metadata_len, "little")json_start = file.read(2)assert metadata_len > 2 and json_start in (b'{"', b"{'"), f"{filename} is not a safetensors file"json_data = json_start + file.read(metadata_len-2)json_obj = json.loads(json_data)res = {}for k, v in json_obj.get("__metadata__", {}).items():res[k] = vif isinstance(v, str) and v[0:1] == '{':try:res[k] = json.loads(v)except Exception:passreturn res
import sys
print(read_metadata_from_safetensors(sys.argv[1]))
{"ss_sd_model_name": "dream-shaper-5.ckpt","ss_resolution": "(512, 512)","ss_clip_skip": "2","ss_num_train_images": "5000","ss_tag_frequency": {"100_coloredic0n": {"coloredic0n icon a white ambulance with red stripe on it": 1,"coloredic0n icon a brown backpack": 1,"coloredic0n icon a banana peel": 1,"coloredic0n icon a battery with a lightning on it": 1,"coloredic0n icon a bed with a pillow on it": 1,"coloredic0n icon a red book with the words on it": 1,"coloredic0n icon a cabbage with green leaves": 1,"coloredic0n icon a cactus plant in a pot": 1,"coloredic0n icon a cassette tape": 1,"coloredic0n icon a  champagne bottle": 1,"coloredic0n icon a piece of cheese": 1,"coloredic0n icon a squirrel sitting with a nut in its hands": 1,"coloredic0n icon a cookie with a chocolate chip on top": 1,"coloredic0n icon a red price percent symbol on it": 1,"coloredic0n icon a fence": 1,"coloredic0n icon a red fire hydrant": 1,"coloredic0n icon a woman with long hair smiling and wearing a pink shirt": 1,"coloredic0n icon a pair of glasses": 1,"coloredic0n icon a pink jellyfish floating in the air": 1,"coloredic0n icon a ladybug with a black and red pattern on it": 1,"coloredic0n icon a green lawnmower": 1,"coloredic0n icon a carton of milk with a cap on top": 1,"coloredic0n icon a bowl of noodles with peas and tomatoes": 1,"coloredic0n icon a notebook with a pen on top of it": 1,"coloredic0n icon a onion": 1,"coloredic0n icon a padlock": 1,"coloredic0n icon a panda face": 1,"coloredic0n icon a blue pen": 1,"coloredic0n icon a pencil": 1,"coloredic0n icon a black power plug": 1,"coloredic0n icon a rat with a long tail": 1,"coloredic0n icon a raven": 1,"coloredic0n icon a rifle with a bullets": 1,"coloredic0n icon a red and white rocket ship": 1,"coloredic0n icon a red rose with green leaves": 1,"coloredic0n icon a yellow rubber duck": 1,"coloredic0n icon a sneaker with a red and white sole": 1,"coloredic0n icon a green snail with a yellow shell": 1,"coloredic0n icon a pair of socks with a red and white design": 1,"coloredic0n icon a brown couch with pillows and a pillow on it": 1,"coloredic0n icon a grey sofa with pillows and a pillow on it": 1,"coloredic0n icon a sunflower": 1,"coloredic0n icon a yellow ticket with a star on it": 1,"coloredic0n icon a toilet seat with a lid up": 1,"coloredic0n icon a wood log": 1,"coloredic0n icon a red and white target with an arrow in the center": 1,"coloredic0n icon a bucket filled with water": 1,"coloredic0n icon a calendar": 1,"coloredic0n icon a red pencil sharper with a metal blade": 1,"coloredic0n icon a red and white and red megaphone": 1}},"ss_batch_size_per_device": "2","ss_bucket_info": "null","ss_bucket_no_upscale": "False","ss_cache_latents": "True","ss_caption_dropout_every_n_epochs": "0","ss_caption_dropout_rate": "0.0","ss_caption_tag_dropout_rate": "0.0","ss_color_aug": "False","ss_dataset_dirs": {"100_coloredic0n": {"n_repeats": 100,"img_count": 50}},"ss_enable_bucket": "False","ss_epoch": "1","ss_face_crop_aug_range": "None","ss_flip_aug": "False","ss_full_fp16": "False","ss_gradient_accumulation_steps": "1","ss_gradient_checkpointing": "False","ss_keep_tokens": "0","ss_learning_rate": "0.0001","ss_lowram": "False","ss_lr_scheduler": "constant","ss_lr_warmup_steps": "0","ss_max_bucket_reso": "None","ss_max_grad_norm": "1.0","ss_max_token_length": "None","ss_max_train_steps": "2500","ss_min_bucket_reso": "None","ss_min_snr_gamma": "None","ss_mixed_precision": "bf16","ss_network_alpha": "Dynamic","ss_network_dim": "Dynamic","ss_network_module": "networks.lora","ss_new_sd_model_hash": "ffad8f3da766de2a56343e388df43941ee27143a03c596e0b8f06976d4fa52e9","ss_noise_offset": "None","ss_num_batches_per_epoch": "2500","ss_num_epochs": "1","ss_num_reg_images": "0","ss_optimizer": "bitsandbytes.optim.adamw.AdamW8bit","ss_output_name": "Colored_Icons","ss_prior_loss_weight": "1.0","ss_random_crop": "False","ss_reg_dataset_dirs": {},"ss_sd_model_hash": "02a6900f","ss_sd_scripts_commit_hash": "9533285e998c74b0e18d114aea71f7143c8f3fa9","ss_seed": "1234","ss_session_id": "2548637574","ss_shuffle_caption": "False","ss_text_encoder_lr": "5e-05","ss_total_batch_size": "2","ss_training_comment": "Dynamic resize with sv_fro: 0.9 from 128; None","ss_training_finished_at": "1683314416.566944","ss_training_started_at": "1683312438.3090656","ss_unet_lr": "0.0001","ss_v2": "False","sshs_legacy_hash": "a45a7daa","sshs_model_hash": "6a455e4a89e4c0cbb5985bf8ebb96397e61bb4b8431b121281d6ec796daecae1"}

https://www.xjx100.cn/news/3270988.html

相关文章

宿舍报修|宿舍报修小程序|基于微信小程序的宿舍报修系统的设计与实现(源码+数据库+文档)

宿舍报修小程序目录 目录 基于微信小程序的宿舍报修系统的设计与实现 一、前言 二、系统功能设计 三、系统实现 1、用户小程序功能模块 2、学生信息管理 3、维修人员管理 4、故障上报管理 5、论坛信息管理 四、数据库设计 1、实体ER图 2、具体的表设计如下所示&…

LeetCode79. Word Search——回溯

文章目录 一、题目二、题解 一、题目 Given an m x n grid of characters board and a string word, return true if word exists in the grid. The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertic…

「计算机网络」物理层

物理层的基本概念 物理层的作用:尽可能屏蔽掉不同传输媒体和通信手段的差异物理层规程:用于物理层的协议主要任务:确定与传输媒体的接口有关的一些特性 机械特性电器特性功能特性过程特性 数据通信的基础知识 数据通信系统的模型 划分为…

IM聊天系统为什么需要做消息幂等?如何使用Redis以及Lua脚本做消息幂等【第12期】

0前言 消息收发模型 第一张图是一个时序图,第二张图是一个标清楚步骤的流程图,更加清晰。消息的插入环节主要在2步。save部分。主要也是对这个部分就行消息幂等的操作。 前情提要:使用Redis发布 token 以及lua脚本来共同完成消息的幂等 目…

使用深度学习进行“序列到序列”回归

目录 下载数据 准备训练数据 定义网络架构 训练网络 测试网络 此示例说明如何使用深度学习预测发动机的剩余使用寿命 (RUL)。 要训练深度神经网络以根据时间序列数据或序列数据预测数值,可以使用长短期记忆 (LSTM) 网络。 此示例使用 [1] 中所述的涡轮风扇发动机…

23种设计模式之抽象工厂模式

目录 什么是抽象工厂模式 基本结构 基本实现步骤 实现代码(有注释) 应用场景 简单工厂、工厂方法、抽象工厂的区别 什么是抽象工厂模式 抽象工厂模式也是一种创建型设计模式,提供了一系列相关或相互依赖对象的接口,而无需…

GO 的 Web 开发系列(五)—— 使用 Swagger 生成一份好看的接口文档

经过前面的文章,已经完成了 Web 系统基础功能的搭建,也实现了 API 接口、HTML 模板渲染等功能。接下来要做的就是使用 Swagger 工具,为这些 Api 接口生成一份好看的接口文档。 一、写注释 注释是 Swagger 的灵魂,Swagger 是通过…

SQLyog安装配置(注册码)连接MySQL

下载资源 博主给你打包好了安装包,在网盘里,只有几Mb,防止你下载到钓鱼软件 快说谢谢博主(然后心甘情愿的点个赞~😊) SQLyog.zip 安装流程 ①下载好压缩包后并解压 ②打开文件夹,双击安装包 ③…