Safetensors documentation

Phân tích cú pháp Metadata

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v0.5.0-rc.0).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Phân tích cú pháp Metadata

Nhờ sự đơn giản của định dạng, việc lấy và phân tích metadata về trọng số Safetensors – tức là danh sách các tensor, kiểu của chúng và hình dạng hoặc số lượng tham số của chúng – bằng các (Range) HTTP requests nhỏ là rất đơn giản và hiệu quả.

Việc phân tích này đã được triển khai bằng JS trong huggingface.js (mã mẫu được đưa ra bên dưới), nhưng nó sẽ tương tự trong bất kỳ ngôn ngữ nào.

Ví dụ về trường hợp sử dụng

Có thể có nhiều trường hợp sử dụng tiềm năng. Ví dụ, chúng tôi sử dụng nó trên HuggingFace Hub để hiển thị thông tin về các model có trọng số safetensors:

Cách sử dụng

http
javascript
python

Từ 🤗 Hub, bạn có thể lấy metadata của một model bằng HTTP range requests thay vì tải toàn bộ file safetensors chứa tất cả các weights. Trong script python dưới đây (bạn có thể dùng bất kỳ ngôn ngữ nào hỗ trợ HTTP requests), chúng ta đang parse metadata của 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]
#   },
#   ...
# }

Ví dụ về kết quả

Ví dụ, dưới đây là số lượng tham số theo từng dtype cho một số mô hình trên HuggingFace Hub. Xem thêm vấn đề này để biết các ví dụ sử dụng khác.

modelsafetensorsparams
gpt2một tệp{ ‘F32’ => 137022720 }
roberta-basemột tệp{ ‘F32’ => 124697433, ‘I64’ => 514 }
Jean-Baptiste/camembert-nermột tệp{ ‘F32’ => 110035205, ‘I64’ => 514 }
roberta-largemột tệp{ ‘F32’ => 355412057, ‘I64’ => 514 }
distilbert-base-german-casedmột tệp{ ‘F32’ => 67431550 }
EleutherAI/gpt-neox-20bchia mảnh{ ‘F16’ => 20554568208, ‘U8’ => 184549376 }
bigscience/bloom-560mmột tệp{ ‘F16’ => 559214592 }
bigscience/bloomchia mảnh{ ‘BF16’ => 176247271424 }
bigscience/bloom-3bmột tệp{ ‘F16’ => 3002557440 }
Update on GitHub