Safetensors 文件

PaddlePaddle API

您目前正在檢視 main 版本,這需要從原始碼安裝。如果您想使用常規的 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 (str, 或 os.PathLike)) — 包含張量的檔案名稱
  • device (Union[Dict[str, any], str], 選填, 預設為 cpu) — 載入後張量所需存放的裝置。可用的選項為所有標準的 Paddle 裝置位置

返回

Dict[str, paddle.Tensor]

以名稱為鍵 (key)、paddle.Tensor 為值 (value) 的字典

將 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]

以名稱為鍵 (key)、位於 cpu 上的 paddle.Tensor 為值 (value) 的字典

從原始位元組 (pure bytes) 中將 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]) — 輸入的張量。張量必須是連續的 (contiguous) 且密集的 (dense)。
  • filename (str, 或 os.PathLike)) — 我們要儲存到的檔案名稱。
  • metadata (Dict[str, str], 選填, 預設為 None) — 您可能希望儲存在標頭 (header) 中的可選純文字元數據。例如,這對於指定關於底層張量的更多資訊很有用。這純粹是資訊性質的,不會影響張量的載入。

返回

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]) — 輸入的張量。張量必須是連續的 (contiguous) 且密集的 (dense)。
  • metadata (Dict[str, str], 選填, 預設為 None) — 您可能希望儲存在標頭 (header) 中的可選純文字元數據。例如,這對於指定關於底層張量的更多資訊很有用。這純粹是資訊性質的,不會影響張量的載入。

返回

bytes

代表該格式的原始位元組。

將張量字典保存為 safetensors 格式的原始位元組。

範例

from safetensors.paddle import save
import paddle

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

© . This site is unofficial and not affiliated with Hugging Face, Inc.