C.W.K.
Stream
Lesson 01 of 08 · published

Safetensors: 왜 디폴트인가

~22 min · ops, safetensors, security

Level 0스카우트
0 XP0/50 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

Pickle 은 보안 구멍

HF 모델의 legacy .bin 포맷이 Python pickle. Pickle 파일 로드는 producer 의 Python 코드 실행과 동치. 악성 pickle 이 Python 프로세스가 할 수 있는 거 뭐든: socket 열기, 환경 변수 exfil, ~/.ssh 수정, 크립토 마이너 설치. 이론 X. Hub 가 야생에서 actively malicious 모델 잡았어.

Safetensors 가 그거 fix

Safetensors 가 flat tensor 컨테이너: 메타데이터 + offset 의 JSON 헤더 + raw tensor byte 의 binary blob. 코드 X, opcode X, 실행 표면 X. 로드는 memory-mapping. 더 빠름 — 더 적은 Python allocation, 많은 케이스 zero-copy path.

마이그레이션

transformers 4.34 (2023 말) 부터 save_pretrained 가 디폴트로 safetensors 씀. Hub 의 인기 모델 대부분 이제 .bin + .safetensors 둘 다 ship. 로딩 시 snapshot_downloadignore_patterns=["*.bin"] 넘기면 디스크에 진짜 안전한 버전 있는지 확실.

Code

로드 없이 safetensors 파일 inspect·python
from safetensors import safe_open

with safe_open("model.safetensors", framework="pt") as f:
    print("metadata:", f.metadata())
    for name in f.keys():
        tensor = f.get_tensor(name)
        print(name, tuple(tensor.shape), tensor.dtype)
        break
safetensors-only 다운로드 강제·python
from huggingface_hub import snapshot_download

path = snapshot_download(
    repo_id="meta-llama/Llama-3.1-8B-Instruct",
    allow_patterns=["*.safetensors", "*.json", "tokenizer*"],
    ignore_patterns=["*.bin", "*.h5", "*.ot"],
)

External links

Exercise

인기 모델 셋 골라. Hub 웹 UI 로 각각 파일 리스트 찾고 .bin + .safetensors 둘 다 ship 하는지 검증. snapshot_downloadignore_patterns=['*.bin'] 으로 safetensors 만 다운로드. 모델 정상 로드 검증.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.