Safetensors: Safe and Efficient Tensor Storage
作者:XD / 发表: 2024年7月29日 22:12 / 更新: 2024年7月29日 22:13 / 编程笔记 / 阅读量:553
Safetensors is a library for safely and efficiently saving and loading PyTorch tensors. Unlike traditional formats like pickle
, Safetensors is designed to prevent arbitrary code execution, offering a more secure way to handle tensor data.
import torch
from safetensors import save_file, load_file
# Create a tensor
tensor = torch.rand(3, 3)
# Save the tensor
save_file({"tensor": tensor}, "tensor_data.safetensors")
# Load the tensor
loaded_tensors = load_file("tensor_data.safetensors")
loaded_tensor = loaded_tensors["tensor"]
相关标签