Safetensors 文件

詮釋資料 (Metadata) 解析

您目前正在檢視 main 版本,這需要從原始碼安裝。如果您希望使用一般的 pip 安裝,請查看最新的穩定版本 (v0.5.0-rc.0)。
Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

詮釋資料解析

鑑於該格式的簡單性,使用小型 (Range) HTTP 請求來獲取並解析 Safetensors 權重的詮釋資料(即張量列表、其類型、形狀或參數數量)是非常簡單且高效的。

此解析功能已在 huggingface.js 中以 JS 實作(範例程式碼如下),但在任何程式語言中實作方式皆類似。

使用案例範例

潛在的使用案例有很多。例如,我們在 HuggingFace Hub 上使用它來顯示具有 safetensors 權重的模型資訊。

使用方式

http
javascript
python

您可以從 🤗 Hub 使用 HTTP Range 請求來獲取模型詮釋資料,而無需下載包含所有權重的完整 safetensors 檔案。在下方的 Python 腳本範例中(您可以使用任何支援 HTTP 請求的語言),我們正在解析 gpt2 的詮釋資料。

import requests # pip install requests
import struct

def parse_single_file(url):
    # Fetch the first 8 bytes of the file
    headers = {'Range': 'bytes=0-7'}
    response = requests.get(url, headers=headers)
    # Interpret the bytes as a little-endian unsigned 64-bit integer
    length_of_header = struct.unpack('<Q', response.content)[0]
    # Fetch length_of_header bytes starting from the 9th byte
    headers = {'Range': f'bytes=8-{7 + length_of_header}'}
    response = requests.get(url, headers=headers)
    # Interpret the response as a JSON object
    header = response.json()
    return header

url = "https://huggingface.co/gpt2/resolve/main/model.safetensors"
header = parse_single_file(url)

print(header)
# {
#   "__metadata__": { "format": "pt" },
#   "h.10.ln_1.weight": {
#     "dtype": "F32",
#     "shape": [768],
#     "data_offsets": [223154176, 223157248]
#   },
#   ...
# }

輸出範例

例如,以下是 HuggingFace Hub 上幾個模型按資料類型 (dtype) 計算的參數數量。另請參閱此議題以獲取更多使用範例。

model safetensors params
gpt2 單一檔案 { ‘F32’ => 137022720 }
roberta-base 單一檔案 { ‘F32’ => 124697433, ‘I64’ => 514 }
Jean-Baptiste/camembert-ner 單一檔案 { ‘F32’ => 110035205, ‘I64’ => 514 }
roberta-large 單一檔案 { ‘F32’ => 355412057, ‘I64’ => 514 }
distilbert-base-german-cased 單一檔案 { ‘F32’ => 67431550 }
EleutherAI/gpt-neox-20b 分片檔案 (Sharded) { ‘F16’ => 20554568208, ‘U8’ => 184549376 }
bigscience/bloom-560m 單一檔案 { ‘F16’ => 559214592 }
bigscience/bloom 分片檔案 (Sharded) { ‘BF16’ => 176247271424 }
bigscience/bloom-3b 單一檔案 { ‘F16’ => 3002557440 }
在 GitHub 上更新

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