Safetensors 文件

Numpy API

您目前正在檢視 main 版本,這需要從原始碼安裝。如果您想要進行一般的 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]

以名稱為鍵,np.ndarray 為值(位於 CPU 上)的字典

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

返回

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

返回

bytes

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

將張量字典保存為 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.