본문 바로가기
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 가중치는 데이터를 불러오는 일이 아니야

예전 .bin 형식은 Python pickle을 사용해. pickle을 읽는 과정은 제작자가 넣은 코드를 실행할 수 있으므로 악성 파일은 네트워크 연결, 환경 변수 유출, SSH 파일 변조까지 Python 프로세스 권한으로 할 수 있어. 실제 Hub에서도 악성 모델이 발견됐어.

Safetensors는 실행할 코드를 담지 않아

Safetensors는 메타데이터와 offset이 든 JSON 헤더, 원시 tensor 바이트만 저장해. opcode가 없어 임의 코드 실행 표면이 없고 memory mapping을 지원해 Python 할당을 줄이며 빠르게 읽을 수 있어.

안전한 파일만 받도록 명시해

Transformers 4.34부터 save_pretrained의 기본값은 safetensors야. 저장소에 .bin.safetensors가 함께 있다면 snapshot_downloadignore_patterns=['*.bin']을 넘겨 pickle 파일을 아예 받지 마.

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

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

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