Safetensors 文档
Torch API
并获得增强的文档体验
开始使用
Torch API
safetensors.torch.load_file
< source >( filename: typing.Union[str, os.PathLike] device: typing.Union[str, int] = 'cpu' ) → Dict[str, torch.Tensor]
将 safetensors 文件加载到 torch 格式。
safetensors.torch.load
< source >( data: bytes ) → Dict[str, torch.Tensor]
从纯字节将 safetensors 文件加载到 torch 格式。
safetensors.torch.save_file
< source >( tensors: typing.Dict[str, torch.Tensor] filename: typing.Union[str, os.PathLike] metadata: typing.Optional[typing.Dict[str, str]] = None ) → None
以 safetensors 格式将张量字典保存为原始字节。
safetensors.torch.save
< source >( tensors: typing.Dict[str, torch.Tensor] metadata: typing.Optional[typing.Dict[str, str]] = None ) → bytes
以 safetensors 格式将张量字典保存为原始字节。
safetensors.torch.load_model
< source >( model: Module filename: typing.Union[str, os.PathLike] strict: bool = True device: typing.Union[str, int] = 'cpu' ) → `(missing, unexpected)
参数
- model (
torch.nn.Module
) — 要加载到的模型。 - filename (
str
, oros.PathLike
) — 要从中加载文件的文件名位置。 - strict (
bool
, 可选, 默认为 True) — 是否在缺少键或有意外键时失败。当为 false 时,该函数仅返回缺失和意外的名称。 - device (
Union[str, int]
, 可选, 默认为cpu
) — 张量加载后需要放置的设备。可用选项是所有常规 torch 设备位置。
返回
`(missing, unexpected)
(List[str], List[str])
missing是模型中在加载期间未修改的名称
unexpected` 是文件上存在的名称,但在加载期间未使用。
将给定的文件名加载到 torch 模型上。此方法专门用于避免 safetensors
中不允许的张量共享问题。有关张量共享的更多信息
safetensors.torch.save_model
< source >( model: Module filename: str metadata: typing.Optional[typing.Dict[str, str]] = None force_contiguous: bool = True )
将给定的 torch 模型保存到指定的文件名。此方法专门用于避免 safetensors
中不允许的张量共享问题。 关于张量共享的更多信息