Safetensors 文档

PaddlePaddle API

您正在查看的是需要从源码安装。如果您想通过 pip 常规安装,请查看最新的稳定版本 (v0.5.0-rc.0)。
Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

PaddlePaddle API

safetensors.paddle.load_file

< >

( filename: typing.Union[str, os.PathLike] device = 'cpu' ) Dict[str, paddle.Tensor]

参数

  • filename (stros.PathLike) — 包含张量的文件名
  • device (Union[Dict[str, any], str], 可选, 默认为 cpu) — 加载后张量需要位于的设备。可用选项是所有常规的 paddle 设备位置

返回

Dict[str, paddle.Tensor]

包含名称为键,值为 paddle.Tensor 的字典

将 safetensors 文件加载为 paddle 格式。

示例

from safetensors.paddle import load_file

file_path = "./my_folder/bert.safetensors"
loaded = load_file(file_path)

safetensors.paddle.load

< >

( data: bytes device: str = 'cpu' ) Dict[str, paddle.Tensor]

参数

  • data (bytes) — safetensors 文件的内容

返回

Dict[str, paddle.Tensor]

包含名称为键,值为 CPU 上 paddle.Tensor 的字典

从纯字节数据加载 safetensors 文件为 paddle 格式。

示例

from safetensors.paddle import load

file_path = "./my_folder/bert.safetensors"
with open(file_path, "rb") as f:
    data = f.read()

loaded = load(data)

safetensors.paddle.save_file

< >

( tensors: typing.Dict[str, paddle.Tensor] filename: typing.Union[str, os.PathLike] metadata: typing.Optional[typing.Dict[str, str]] = None ) None

参数

  • tensors (Dict[str, paddle.Tensor]) — 要保存的张量。张量需要是连续且密集的。
  • filename (stros.PathLike) — 我们要保存到的文件名。
  • metadata (Dict[str, str], 可选, 默认为 None) — 您可能想要保存在文件头中的可选纯文本元数据。例如,指定有关底层张量的更多信息可能很有用。这纯粹是信息性的,不影响张量加载。

返回

将张量字典以 safetensors 格式保存为原始字节。

示例

from safetensors.paddle import save_file
import paddle

tensors = {"embedding": paddle.zeros((512, 1024)), "attention": paddle.zeros((256, 256))}
save_file(tensors, "model.safetensors")

safetensors.paddle.save

< >

( tensors: typing.Dict[str, paddle.Tensor] metadata: typing.Optional[typing.Dict[str, str]] = None ) bytes

参数

  • tensors (Dict[str, paddle.Tensor]) — 要保存的张量。张量需要是连续且密集的。
  • metadata (Dict[str, str], 可选, 默认为 None) — 您可能想要保存在文件头中的可选纯文本元数据。例如,指定有关底层张量的更多信息可能很有用。这纯粹是信息性的,不影响张量加载。

返回

字节

代表该格式的原始字节

将张量字典以 safetensors 格式保存为原始字节。

示例

from safetensors.paddle import save
import paddle

tensors = {"embedding": paddle.zeros((512, 1024)), "attention": paddle.zeros((256, 256))}
byte_data = save(tensors)
< > 在 GitHub 上更新