svjack's picture
Upload 1392 files
43b7e92 verified
|
raw
history blame
5.52 kB
# μ„Έμ΄ν”„ν…μ„œ λ‘œλ“œ
[safetensors](https://github.com/huggingface/safetensors)λŠ” ν…μ„œλ₯Ό μ €μž₯ν•˜κ³  λ‘œλ“œν•˜κΈ° μœ„ν•œ μ•ˆμ „ν•˜κ³  λΉ λ₯Έ 파일 ν˜•μ‹μž…λ‹ˆλ‹€. 일반적으둜 PyTorch λͺ¨λΈ κ°€μ€‘μΉ˜λŠ” Python의 [`pickle`](https://docs.python.org/3/library/pickle.html) μœ ν‹Έλ¦¬ν‹°λ₯Ό μ‚¬μš©ν•˜μ—¬ `.bin` νŒŒμΌμ— μ €μž₯λ˜κ±°λ‚˜ `피클`λ©λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ `피클`은 μ•ˆμ „ν•˜μ§€ μ•ŠμœΌλ©° ν”Όν΄λœ νŒŒμΌμ—λŠ” 싀행될 수 μžˆλŠ” μ•…μ„± μ½”λ“œκ°€ 포함될 수 μžˆμŠ΅λ‹ˆλ‹€. μ„Έμ΄ν”„ν…μ„œλŠ” `피클`의 μ•ˆμ „ν•œ λŒ€μ•ˆμœΌλ‘œ λͺ¨λΈ κ°€μ€‘μΉ˜λ₯Ό κ³΅μœ ν•˜λŠ” 데 μ΄μƒμ μž…λ‹ˆλ‹€.
이 κ°€μ΄λ“œμ—μ„œλŠ” `.safetensor` νŒŒμΌμ„ λ‘œλ“œν•˜λŠ” 방법과 λ‹€λ₯Έ ν˜•μ‹μœΌλ‘œ μ €μž₯된 μ•ˆμ •μ  ν™•μ‚° λͺ¨λΈ κ°€μ€‘μΉ˜λ₯Ό `.safetensor`둜 λ³€ν™˜ν•˜λŠ” 방법을 λ³΄μ—¬λ“œλ¦¬κ² μŠ΅λ‹ˆλ‹€. μ‹œμž‘ν•˜κΈ° 전에 μ„Έμ΄ν”„ν…μ„œκ°€ μ„€μΉ˜λ˜μ–΄ μžˆλŠ”μ§€ ν™•μΈν•˜μ„Έμš”:
```bash
!pip install safetensors
```
['runwayml/stable-diffusion-v1-5`] (https://huggingface.co/runwayml/stable-diffusion-v1-5/tree/main) 리포지토리λ₯Ό 보면 `text_encoder`, `unet` 및 `vae` ν•˜μœ„ 폴더에 κ°€μ€‘μΉ˜κ°€ `.safetensors` ν˜•μ‹μœΌλ‘œ μ €μž₯λ˜μ–΄ μžˆλŠ” 것을 λ³Ό 수 μžˆμŠ΅λ‹ˆλ‹€. 기본적으둜 πŸ€— λ””ν“¨μ €λŠ” λͺ¨λΈ μ €μž₯μ†Œμ—μ„œ μ‚¬μš©ν•  수 μžˆλŠ” 경우 ν•΄λ‹Ή ν•˜μœ„ ν΄λ”μ—μ„œ μ΄λŸ¬ν•œ '.safetensors` νŒŒμΌμ„ μžλ™μœΌλ‘œ λ‘œλ“œν•©λ‹ˆλ‹€.
보닀 λͺ…μ‹œμ μΈ μ œμ–΄λ₯Ό μœ„ν•΄ μ„ νƒμ μœΌλ‘œ `μ‚¬μš©_μ„Έμ΄ν”„ν…μ„œ=True`λ₯Ό μ„€μ •ν•  수 μžˆμŠ΅λ‹ˆλ‹€(`μ„Έμ΄ν”„ν…μ„œ`κ°€ μ„€μΉ˜λ˜μ§€ μ•Šμ€ 경우 μ„€μΉ˜ν•˜λΌλŠ” 였λ₯˜ λ©”μ‹œμ§€κ°€ ν‘œμ‹œλ¨):
```py
from diffusers import DiffusionPipeline
pipeline = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", use_safetensors=True)
```
κ·ΈλŸ¬λ‚˜ λͺ¨λΈ κ°€μ€‘μΉ˜κ°€ μœ„μ˜ μ˜ˆμ‹œμ²˜λŸΌ λ°˜λ“œμ‹œ λ³„λ„μ˜ ν•˜μœ„ 폴더에 μ €μž₯λ˜λŠ” 것은 μ•„λ‹™λ‹ˆλ‹€. λͺ¨λ“  κ°€μ€‘μΉ˜κ°€ ν•˜λ‚˜μ˜ '.safetensors` νŒŒμΌμ— μ €μž₯λ˜λŠ” κ²½μš°λ„ μžˆμŠ΅λ‹ˆλ‹€. 이 경우 κ°€μ€‘μΉ˜κ°€ Stable Diffusion κ°€μ€‘μΉ˜μΈ 경우 [`~diffusers.loaders.FromCkptMixin.from_ckpt`] λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ νŒŒμΌμ„ 직접 λ‘œλ“œν•  수 μžˆμŠ΅λ‹ˆλ‹€:
```py
from diffusers import StableDiffusionPipeline
pipeline = StableDiffusionPipeline.from_ckpt(
"https://huggingface.co/WarriorMama777/OrangeMixs/blob/main/Models/AbyssOrangeMix/AbyssOrangeMix.safetensors"
)
```
## μ„Έμ΄ν”„ν…μ„œλ‘œ λ³€ν™˜
ν—ˆλΈŒμ˜ λͺ¨λ“  κ°€μ€‘μΉ˜λ₯Ό '.safetensors` ν˜•μ‹μœΌλ‘œ μ‚¬μš©ν•  수 μžˆλŠ” 것은 μ•„λ‹ˆλ©°, '.bin`으둜 μ €μž₯된 κ°€μ€‘μΉ˜κ°€ μžˆμ„ 수 μžˆμŠ΅λ‹ˆλ‹€. 이 경우 [Convert Space](https://huggingface.co/spaces/diffusers/convert)을 μ‚¬μš©ν•˜μ—¬ κ°€μ€‘μΉ˜λ₯Ό '.safetensors'둜 λ³€ν™˜ν•˜μ„Έμš”. Convert SpaceλŠ” ν”Όν΄λœ κ°€μ€‘μΉ˜λ₯Ό λ‹€μš΄λ‘œλ“œν•˜μ—¬ λ³€ν™˜ν•œ ν›„ ν’€ λ¦¬ν€˜μŠ€νŠΈλ₯Ό μ—΄μ–΄ ν—ˆλΈŒμ— μƒˆλ‘œ λ³€ν™˜λœ `.safetensors` νŒŒμΌμ„ μ—…λ‘œλ“œν•©λ‹ˆλ‹€. μ΄λ ‡κ²Œ ν•˜λ©΄ ν”Όν΄λœ νŒŒμΌμ— μ•…μ„± μ½”λ“œκ°€ ν¬ν•¨λ˜μ–΄ μžˆλŠ” 경우, μ•ˆμ „ν•˜μ§€ μ•Šμ€ 파일과 μ˜μ‹¬μŠ€λŸ¬μš΄ 피클 κ°€μ Έμ˜€κΈ°λ₯Ό νƒμ§€ν•˜λŠ” [λ³΄μ•ˆ μŠ€μΊλ„ˆ](https://huggingface.co/docs/hub/security-pickle#hubs-security-scanner)κ°€ μžˆλŠ” ν—ˆλΈŒλ‘œ μ—…λ‘œλ“œλ©λ‹ˆλ‹€. - κ°œλ³„ 컴퓨터가 μ•„λ‹Œ.
κ°œμ •` λ§€κ°œλ³€μˆ˜μ— ν’€ λ¦¬ν€˜μŠ€νŠΈμ— λŒ€ν•œ μ°Έμ‘°λ₯Ό μ§€μ •ν•˜μ—¬ μƒˆλ‘œμš΄ '.safetensors` κ°€μ€‘μΉ˜κ°€ 적용된 λͺ¨λΈμ„ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€(ν—ˆλΈŒμ˜ [Check PR](https://huggingface.co/spaces/diffusers/check_pr) κ³΅κ°„μ—μ„œ ν…ŒμŠ€νŠΈν•  μˆ˜λ„ 있음)(예: `refs/pr/22`):
```py
from diffusers import DiffusionPipeline
pipeline = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1", revision="refs/pr/22")
```
## μ„Έμ΄ν”„μ„Όμ„œλ₯Ό μ‚¬μš©ν•˜λŠ” μ΄μœ λŠ” λ¬΄μ—‡μΈκ°€μš”?
세이프티 μ„Όμ„œλ₯Ό μ‚¬μš©ν•˜λŠ” λ°μ—λŠ” μ—¬λŸ¬ 가지 μ΄μœ κ°€ μžˆμŠ΅λ‹ˆλ‹€:
- μ„Έμ΄ν”„ν…μ„œλ₯Ό μ‚¬μš©ν•˜λŠ” κ°€μž₯ 큰 μ΄μœ λŠ” μ•ˆμ „μž…λ‹ˆλ‹€.μ˜€ν”ˆ μ†ŒμŠ€ 및 λͺ¨λΈ 배포가 증가함에 따라 λ‹€μš΄λ‘œλ“œν•œ λͺ¨λΈ κ°€μ€‘μΉ˜μ— μ•…μ„± μ½”λ“œκ°€ ν¬ν•¨λ˜μ–΄ μžˆμ§€ μ•Šλ‹€λŠ” 것을 μ‹ λ’°ν•  수 μžˆλŠ” 것이 μ€‘μš”ν•΄μ‘ŒμŠ΅λ‹ˆλ‹€.μ„Έμ΄ν”„μ„Όμ„œμ˜ ν˜„μž¬ 헀더 ν¬κΈ°λŠ” 맀우 큰 JSON νŒŒμΌμ„ ꡬ문 λΆ„μ„ν•˜μ§€ λͺ»ν•˜κ²Œ ν•©λ‹ˆλ‹€.
- λͺ¨λΈ μ „ν™˜ κ°„μ˜ λ‘œλ”© μ†λ„λŠ” ν…μ„œμ˜ 제둜 μΉ΄ν”Όλ₯Ό μˆ˜ν–‰ν•˜λŠ” μ„Έμ΄ν”„ν…μ„œλ₯Ό μ‚¬μš©ν•΄μ•Ό ν•˜λŠ” 또 λ‹€λ₯Έ μ΄μœ μž…λ‹ˆλ‹€. κ°€μ€‘μΉ˜λ₯Ό CPU(κΈ°λ³Έκ°’)둜 λ‘œλ“œν•˜λŠ” 경우 '피클'에 λΉ„ν•΄ 특히 λΉ λ₯΄λ©°, κ°€μ€‘μΉ˜λ₯Ό GPU둜 직접 λ‘œλ“œν•˜λŠ” κ²½μš°μ—λ„ λΉ λ₯΄μ§€λŠ” μ•Šλ”λΌλ„ λΉ„μŠ·ν•˜κ²Œ λΉ λ¦…λ‹ˆλ‹€. λͺ¨λΈμ΄ 이미 λ‘œλ“œλœ κ²½μš°μ—λ§Œ μ„±λŠ₯ 차이λ₯Ό λŠλ‚„ 수 있으며, κ°€μ€‘μΉ˜λ₯Ό λ‹€μš΄λ‘œλ“œν•˜κ±°λ‚˜ λͺ¨λΈμ„ 처음 λ‘œλ“œν•˜λŠ” κ²½μš°μ—λŠ” μ„±λŠ₯ 차이λ₯Ό λŠλΌμ§€ λͺ»ν•  κ²ƒμž…λ‹ˆλ‹€.
전체 νŒŒμ΄ν”„λΌμΈμ„ λ‘œλ“œν•˜λŠ” 데 κ±Έλ¦¬λŠ” μ‹œκ°„μž…λ‹ˆλ‹€:
```py
from diffusers import StableDiffusionPipeline
pipeline = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1")
"Loaded in safetensors 0:00:02.033658"
"Loaded in PyTorch 0:00:02.663379"
```
ν•˜μ§€λ§Œ μ‹€μ œλ‘œ 500MB의 λͺ¨λΈ κ°€μ€‘μΉ˜λ₯Ό λ‘œλ“œν•˜λŠ” 데 κ±Έλ¦¬λŠ” μ‹œκ°„μ€ μ–Όλ§ˆ λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€:
```bash
safetensors: 3.4873ms
PyTorch: 172.7537ms
```
지연 λ‘œλ”©μ€ μ„Έμ΄ν”„ν…μ„œμ—μ„œλ„ μ§€μ›λ˜λ©°, μ΄λŠ” λΆ„μ‚° μ„€μ •μ—μ„œ 일뢀 ν…μ„œλ§Œ λ‘œλ“œν•˜λŠ” 데 μœ μš©ν•©λ‹ˆλ‹€. 이 ν˜•μ‹μ„ μ‚¬μš©ν•˜λ©΄ [BLOOM](https://huggingface.co/bigscience/bloom) λͺ¨λΈμ„ 일반 PyTorch κ°€μ€‘μΉ˜λ₯Ό μ‚¬μš©ν•˜μ—¬ 10뢄이 걸리던 것을 8개의 GPUμ—μ„œ 45초 λ§Œμ— λ‘œλ“œν•  수 μžˆμŠ΅λ‹ˆλ‹€.