Safetensors 文件

Numpy API

您正在檢視的是需要從原始碼安裝。如果您想進行常規的 pip 安裝,請檢視最新的穩定版本 (v0.5.0-rc.0)。
Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

Numpy API

safetensors.numpy.load_file

< >

( filename: typing.Union[str, os.PathLike] ) Dict[str, np.ndarray]

引數

  • filename (stros.PathLike) — 包含張量的檔名

返回

Dict[str, np.ndarray]

一個字典,鍵為名稱,值為 np.ndarray

將 safetensors 檔案載入為 numpy 格式。

示例

from safetensors.numpy import load_file

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

safetensors.numpy.load

< >

( data: bytes ) Dict[str, np.ndarray]

引數

  • data (bytes) — safetensors 檔案的內容

返回

Dict[str, np.ndarray]

一個字典,鍵為名稱,值為在 cpu 上的 np.ndarray

從純位元組流載入 safetensors 檔案為 numpy 格式。

示例

from safetensors.numpy import load

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

loaded = load(data)

safetensors.numpy.save_file

< >

( tensor_dict: typing.Dict[str, numpy.ndarray] filename: typing.Union[str, os.PathLike] metadata: typing.Optional[typing.Dict[str, str]] = None ) None

引數

  • tensor_dict (Dict[str, np.ndarray]) — 傳入的張量。張量需要是連續且密集的。
  • filename (stros.PathLike) — 我們要儲存到的檔名。
  • metadata (Dict[str, str], 可選, 預設為 None) — 您可能想儲存在檔案頭中的可選純文字元資料。例如,它可以用於指定有關底層張量的更多資訊。這純粹是資訊性的,不影響張量載入。

返回

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

示例

from safetensors.numpy import save_file
import numpy as np

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

safetensors.numpy.save

< >

( tensor_dict: typing.Dict[str, numpy.ndarray] metadata: typing.Optional[typing.Dict[str, str]] = None ) bytes

引數

  • tensor_dict (Dict[str, np.ndarray]) — 傳入的張量。張量需要是連續且密集的。
  • metadata (Dict[str, str], 可選, 預設為 None) — 您可能想儲存在檔案頭中的可選純文字元資料。例如,它可以用於指定有關底層張量的更多資訊。這純粹是資訊性的,不影響張量載入。

返回

位元組

代表該格式的原始位元組

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

示例

from safetensors.numpy import save
import numpy as np

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

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