File size: 12,884 Bytes
ef4d689
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<!--Copyright 2024 The HuggingFace Team. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
-->

# Core ML๋กœ Stable Diffusion์„ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•

[Core ML](https://developer.apple.com/documentation/coreml)์€ Apple ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ ์ง€์›ํ•˜๋Š” ๋ชจ๋ธ ํ˜•์‹ ๋ฐ ๋จธ์‹  ๋Ÿฌ๋‹ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค. macOS ๋˜๋Š” iOS/iPadOS ์•ฑ ๋‚ด์—์„œ Stable Diffusion ๋ชจ๋ธ์„ ์‹คํ–‰ํ•˜๋Š” ๋ฐ ๊ด€์‹ฌ์ด ์žˆ๋Š” ๊ฒฝ์šฐ, ์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” ๊ธฐ์กด PyTorch ์ฒดํฌํฌ์ธํŠธ๋ฅผ Core ML ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ์ด๋ฅผ Python ๋˜๋Š” Swift๋กœ ์ถ”๋ก ์— ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

Core ML ๋ชจ๋ธ์€ Apple ๊ธฐ๊ธฐ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์ปดํ“จํŒ… ์—”์ง„๋“ค, ์ฆ‰ CPU, GPU, Apple Neural Engine(๋˜๋Š” Apple Silicon Mac ๋ฐ ์ตœ์‹  iPhone/iPad์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ…์„œ ์ตœ์ ํ™” ๊ฐ€์†๊ธฐ์ธ ANE)์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ๊ณผ ์‹คํ–‰ ์ค‘์ธ ๊ธฐ๊ธฐ์— ๋”ฐ๋ผ Core ML์€ ์ปดํ“จํŒ… ์—”์ง„๋„ ํ˜ผํ•ฉํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ์˜ˆ๋ฅผ ๋“ค์–ด ๋ชจ๋ธ์˜ ์ผ๋ถ€๊ฐ€ CPU์—์„œ ์‹คํ–‰๋˜๋Š” ๋ฐ˜๋ฉด ๋‹ค๋ฅธ ๋ถ€๋ถ„์€ GPU์—์„œ ์‹คํ–‰๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

<Tip>

PyTorch์— ๋‚ด์žฅ๋œ `mps` ๊ฐ€์†๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Apple Silicon Macs์—์„œ `diffusers` Python ์ฝ”๋“œ๋ฒ ์ด์Šค๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ [mps ๊ฐ€์ด๋“œ]์— ์ž์„ธํžˆ ์„ค๋ช…๋˜์–ด ์žˆ์ง€๋งŒ ๋„ค์ดํ‹ฐ๋ธŒ ์•ฑ๊ณผ ํ˜ธํ™˜๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

</Tip>

## Stable Diffusion Core ML ์ฒดํฌํฌ์ธํŠธ

Stable Diffusion ๊ฐ€์ค‘์น˜(๋˜๋Š” ์ฒดํฌํฌ์ธํŠธ)๋Š” PyTorch ํ˜•์‹์œผ๋กœ ์ €์žฅ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋„ค์ดํ‹ฐ๋ธŒ ์•ฑ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” Core ML ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๋‹คํ–‰ํžˆ๋„ Apple ์—”์ง€๋‹ˆ์–ด๋“ค์ด `diffusers`๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ [๋ณ€ํ™˜ ํˆด](https://github.com/apple/ml-stable-diffusion#-converting-models-to-core-ml)์„ ๊ฐœ๋ฐœํ•˜์—ฌ PyTorch ์ฒดํฌํฌ์ธํŠธ๋ฅผ Core ML๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ชจ๋ธ์„ ๋ณ€ํ™˜ํ•˜๊ธฐ ์ „์— ์ž ์‹œ ์‹œ๊ฐ„์„ ๋‚ด์–ด Hugging Face Hub๋ฅผ ์‚ดํŽด๋ณด์„ธ์š”. ๊ด€์‹ฌ ์žˆ๋Š” ๋ชจ๋ธ์ด ์ด๋ฏธ Core ML ํ˜•์‹์œผ๋กœ ์ œ๊ณต๋˜๊ณ  ์žˆ์„ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์Šต๋‹ˆ๋‹ค:

- [Apple](https://huggingface.co/apple) organization์—๋Š” Stable Diffusion ๋ฒ„์ „ 1.4, 1.5, 2.0 base ๋ฐ 2.1 base๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
- [coreml](https://huggingface.co/coreml) organization์—๋Š” ์ปค์Šคํ…€ DreamBooth๊ฐ€ ์ ์šฉ๋˜๊ฑฐ๋‚˜, ํŒŒ์ธํŠœ๋‹๋œ ๋ชจ๋ธ์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
- ์ด [ํ•„ํ„ฐ](https://huggingface.co/models?pipeline_tag=text-to-image&library=coreml&p=2&sort=likes)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  Core ML ์ฒดํฌํฌ์ธํŠธ๋“ค์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์›ํ•˜๋Š” ๋ชจ๋ธ์„ ์ฐพ์„ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ Apple์˜ [๋ชจ๋ธ์„ Core ML๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ](https://github.com/apple/ml-stable-diffusion#-converting-models-to-core-ml) ์ง€์นจ์„ ๋”ฐ๋ฅด๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

## ์‚ฌ์šฉํ•  Core ML ๋ณ€ํ˜•(Variant) ์„ ํƒํ•˜๊ธฐ

Stable Diffusion ๋ชจ๋ธ์€ ๋‹ค์–‘ํ•œ ๋ชฉ์ ์— ๋”ฐ๋ผ ๋‹ค๋ฅธ Core ML ๋ณ€ํ˜•์œผ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

- ์‚ฌ์šฉ๋˜๋Š” ์–ดํ…์…˜ ๋ธ”๋ก ์œ ํ˜•. ์–ดํ…์…˜ ์—ฐ์‚ฐ์€ ์ด๋ฏธ์ง€ ํ‘œํ˜„์˜ ์—ฌ๋Ÿฌ ์˜์—ญ ๊ฐ„์˜ ๊ด€๊ณ„์— '์ฃผ์˜๋ฅผ ๊ธฐ์šธ์ด๊ณ ' ์ด๋ฏธ์ง€์™€ ํ…์ŠคํŠธ ํ‘œํ˜„์ด ์–ด๋–ป๊ฒŒ ์—ฐ๊ด€๋˜์–ด ์žˆ๋Š”์ง€ ์ดํ•ดํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์–ดํ…์…˜ ์—ฐ์‚ฐ์€ ์ปดํ“จํŒ… ๋ฐ ๋ฉ”๋ชจ๋ฆฌ ์ง‘์•ฝ์ ์ด๋ฏ€๋กœ ๋‹ค์–‘ํ•œ ์žฅ์น˜์˜ ํ•˜๋“œ์›จ์–ด ํŠน์„ฑ์„ ๊ณ ๋ คํ•œ ๋‹ค์–‘ํ•œ ๊ตฌํ˜„์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. Core ML Stable Diffusion ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ ๋‘ ๊ฐ€์ง€ ์ฃผ์˜ ๋ณ€ํ˜•์ด ์žˆ์Šต๋‹ˆ๋‹ค:
    * `split_einsum` ([Apple์—์„œ ๋„์ž…](https://machinelearning.apple.com/research/neural-engine-transformers)์€ ์ตœ์‹  iPhone, iPad ๋ฐ M ์‹œ๋ฆฌ์ฆˆ ์ปดํ“จํ„ฐ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ANE ์žฅ์น˜์— ์ตœ์ ํ™”๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
    * "์›๋ณธ" ์–ดํ…์…˜(`diffusers`์— ์‚ฌ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ๊ตฌํ˜„)๋Š” CPU/GPU์™€๋งŒ ํ˜ธํ™˜๋˜๋ฉฐ ANE์™€๋Š” ํ˜ธํ™˜๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. "์›๋ณธ" ์–ดํ…์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ CPU + GPU์—์„œ ๋ชจ๋ธ์„ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์ด ANE๋ณด๋‹ค *๋”* ๋น ๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ [์ด ์„ฑ๋Šฅ ๋ฒค์น˜๋งˆํฌ](https://huggingface.co/blog/fast-mac-diffusers#performance-benchmarks)์™€ ์ปค๋ฎค๋‹ˆํ‹ฐ์—์„œ ์ œ๊ณตํ•˜๋Š” ์ผ๋ถ€ [์ถ”๊ฐ€ ์ธก์ •](https://github.com/huggingface/swift-coreml-diffusers/issues/31)์„ ์ฐธ์กฐํ•˜์‹ญ์‹œ์˜ค.

- ์ง€์›๋˜๋Š” ์ถ”๋ก  ํ”„๋ ˆ์ž„์›Œํฌ
    * `packages`๋Š” Python ์ถ”๋ก ์— ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. ๋„ค์ดํ‹ฐ๋ธŒ ์•ฑ์— ํ†ตํ•ฉํ•˜๊ธฐ ์ „์— ๋ณ€ํ™˜๋œ Core ML ๋ชจ๋ธ์„ ํ…Œ์ŠคํŠธํ•˜๊ฑฐ๋‚˜, Core ML ์„ฑ๋Šฅ์„ ์•Œ๊ณ  ์‹ถ์ง€๋งŒ ๋„ค์ดํ‹ฐ๋ธŒ ์•ฑ์„ ์ง€์›ํ•  ํ•„์š”๋Š” ์—†๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์›น UI๊ฐ€ ์žˆ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์€ Python Core ML ๋ฐฑ์—”๋“œ๋ฅผ ์™„๋ฒฝํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    * Swift ์ฝ”๋“œ์—๋Š” `์ปดํŒŒ์ผ๋œ` ๋ชจ๋ธ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. Hub์˜ `์ปดํŒŒ์ผ๋œ` ๋ชจ๋ธ์€ iOS ๋ฐ iPadOS ๊ธฐ๊ธฐ์™€์˜ ํ˜ธํ™˜์„ฑ์„ ์œ„ํ•ด ํฐ UNet ๋ชจ๋ธ ๊ฐ€์ค‘์น˜๋ฅผ ์—ฌ๋Ÿฌ ํŒŒ์ผ๋กœ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” [`--chunk-unet` ๋ณ€ํ™˜ ์˜ต์…˜](https://github.com/apple/ml-stable-diffusion#-converting-models-to-core-ml)์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. ๋„ค์ดํ‹ฐ๋ธŒ ์•ฑ์„ ์ง€์›ํ•˜๋ ค๋ฉด `์ปดํŒŒ์ผ๋œ` ๋ณ€ํ˜•์„ ์„ ํƒํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๊ณต์‹ Core ML Stable Diffusion [๋ชจ๋ธ](https://huggingface.co/apple/coreml-stable-diffusion-v1-4/tree/main)์—๋Š” ์ด๋Ÿฌํ•œ ๋ณ€ํ˜•์ด ํฌํ•จ๋˜์–ด ์žˆ์ง€๋งŒ ์ปค๋ฎค๋‹ˆํ‹ฐ ๋ฒ„์ „์€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

```
coreml-stable-diffusion-v1-4
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ original
โ”‚   โ”œโ”€โ”€ compiled
โ”‚   โ””โ”€โ”€ packages
โ””โ”€โ”€ split_einsum
    โ”œโ”€โ”€ compiled
    โ””โ”€โ”€ packages
```

์•„๋ž˜์™€ ๊ฐ™์ด ํ•„์š”ํ•œ ๋ณ€ํ˜•์„ ๋‹ค์šด๋กœ๋“œํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

## Python์—์„œ Core ML ์ถ”๋ก 

Python์—์„œ Core ML ์ถ”๋ก ์„ ์‹คํ–‰ํ•˜๋ ค๋ฉด ๋‹ค์Œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•˜์„ธ์š”:

```bash
pip install huggingface_hub
pip install git+https://github.com/apple/ml-stable-diffusion
```

### ๋ชจ๋ธ ์ฒดํฌํฌ์ธํŠธ ๋‹ค์šด๋กœ๋“œํ•˜๊ธฐ

`์ปดํŒŒ์ผ๋œ` ๋ฒ„์ „์€ Swift์™€๋งŒ ํ˜ธํ™˜๋˜๋ฏ€๋กœ Python์—์„œ ์ถ”๋ก ์„ ์‹คํ–‰ํ•˜๋ ค๋ฉด `packages` ํด๋”์— ์ €์žฅ๋œ ๋ฒ„์ „ ์ค‘ ํ•˜๋‚˜๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. `์›๋ณธ` ๋˜๋Š” `split_einsum` ์–ดํ…์…˜ ์ค‘ ์–ด๋Š ๊ฒƒ์„ ์‚ฌ์šฉํ• ์ง€ ์„ ํƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ์€ Hub์—์„œ 'models'๋ผ๋Š” ๋””๋ ‰ํ† ๋ฆฌ๋กœ 'original' ์–ดํ…์…˜ ๋ณ€ํ˜•์„ ๋‹ค์šด๋กœ๋“œํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค:

```Python
from huggingface_hub import snapshot_download
from pathlib import Path

repo_id = "apple/coreml-stable-diffusion-v1-4"
variant = "original/packages"

model_path = Path("./models") / (repo_id.split("/")[-1] + "_" + variant.replace("/", "_"))
snapshot_download(repo_id, allow_patterns=f"{variant}/*", local_dir=model_path, local_dir_use_symlinks=False)
print(f"Model downloaded at {model_path}")
```


### ์ถ”๋ก [[python-inference]]

๋ชจ๋ธ์˜ snapshot์„ ๋‹ค์šด๋กœ๋“œํ•œ ํ›„์—๋Š” Apple์˜ Python ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ…Œ์ŠคํŠธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

```shell
python -m python_coreml_stable_diffusion.pipeline --prompt "a photo of an astronaut riding a horse on mars" -i models/coreml-stable-diffusion-v1-4_original_packages -o </path/to/output/image> --compute-unit CPU_AND_GPU --seed 93
```

`<output-mlpackages-directory>`๋Š” ์œ„ ๋‹จ๊ณ„์—์„œ ๋‹ค์šด๋กœ๋“œํ•œ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๊ฐ€๋ฆฌ์ผœ์•ผ ํ•˜๋ฉฐ, `--compute-unit`์€ ์ถ”๋ก ์„ ํ—ˆ์šฉํ•  ํ•˜๋“œ์›จ์–ด๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ด๋Š” ๋‹ค์Œ ์˜ต์…˜ ์ค‘ ํ•˜๋‚˜์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: `ALL`, `CPU_AND_GPU`, `CPU_ONLY`, `CPU_AND_NE`. ์„ ํƒ์  ์ถœ๋ ฅ ๊ฒฝ๋กœ์™€ ์žฌํ˜„์„ฑ์„ ์œ„ํ•œ ์‹œ๋“œ๋ฅผ ์ œ๊ณตํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

์ถ”๋ก  ์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” Stable Diffusion ๋ชจ๋ธ์˜ ์›๋ž˜ ๋ฒ„์ „์ธ `CompVis/stable-diffusion-v1-4`๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์ถ”๋ก  ๋ช…๋ น์ค„์—์„œ `--model-version` ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ํ—ˆ๋ธŒ ID๋ฅผ *์ง€์ •*ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ด๋ฏธ ์ง€์›๋˜๋Š” ๋ชจ๋ธ๊ณผ ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ํ•™์Šตํ•˜๊ฑฐ๋‚˜ ํŒŒ์ธํŠœ๋‹ํ•œ ์‚ฌ์šฉ์ž ์ง€์ • ๋ชจ๋ธ์— ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, [`runwayml/stable-diffusion-v1-5`](https://huggingface.co/runwayml/stable-diffusion-v1-5)๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค:

```shell
python -m python_coreml_stable_diffusion.pipeline --prompt "a photo of an astronaut riding a horse on mars" --compute-unit ALL -o output --seed 93 -i models/coreml-stable-diffusion-v1-5_original_packages --model-version runwayml/stable-diffusion-v1-5
```


## Swift์—์„œ Core ML ์ถ”๋ก ํ•˜๊ธฐ

Swift์—์„œ ์ถ”๋ก ์„ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์€ ๋ชจ๋ธ์ด ์ด๋ฏธ `mlmodelc` ํ˜•์‹์œผ๋กœ ์ปดํŒŒ์ผ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— Python๋ณด๋‹ค ์•ฝ๊ฐ„ ๋น ๋ฆ…๋‹ˆ๋‹ค. ์ด๋Š” ์•ฑ์ด ์‹œ์ž‘๋  ๋•Œ ๋ชจ๋ธ์ด ๋ถˆ๋Ÿฌ์™€์ง€๋Š” ๊ฒƒ์ด ๋ˆˆ์— ๋„์ง€๋งŒ, ์ดํ›„ ์—ฌ๋Ÿฌ ๋ฒˆ ์‹คํ–‰ํ•˜๋ฉด ๋ˆˆ์— ๋„์ง€ ์•Š์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

### ๋‹ค์šด๋กœ๋“œ

Mac์—์„œ Swift์—์„œ ์ถ”๋ก ์„ ์‹คํ–‰ํ•˜๋ ค๋ฉด `์ปดํŒŒ์ผ๋œ` ์ฒดํฌํฌ์ธํŠธ ๋ฒ„์ „ ์ค‘ ํ•˜๋‚˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด์ „ ์˜ˆ์ œ์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ `์ปดํŒŒ์ผ๋œ` ๋ณ€ํ˜• ์ค‘ ํ•˜๋‚˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Python ์ฝ”๋“œ๋ฅผ ๋กœ์ปฌ๋กœ ๋‹ค์šด๋กœ๋“œํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค:

```Python
from huggingface_hub import snapshot_download
from pathlib import Path

repo_id = "apple/coreml-stable-diffusion-v1-4"
variant = "original/compiled"

model_path = Path("./models") / (repo_id.split("/")[-1] + "_" + variant.replace("/", "_"))
snapshot_download(repo_id, allow_patterns=f"{variant}/*", local_dir=model_path, local_dir_use_symlinks=False)
print(f"Model downloaded at {model_path}")
```

### ์ถ”๋ก [[swift-inference]]

์ถ”๋ก ์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ, Apple์˜ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋ฅผ ๋ณต์ œํ•˜์„ธ์š”:

```bash
git clone https://github.com/apple/ml-stable-diffusion
cd ml-stable-diffusion
```

๊ทธ ๋‹ค์Œ Apple์˜ ๋ช…๋ น์–ด ๋„๊ตฌ์ธ [Swift ํŒจํ‚ค์ง€ ๊ด€๋ฆฌ์ž](https://www.swift.org/package-manager/#)๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค:

```bash
swift run StableDiffusionSample --resource-path models/coreml-stable-diffusion-v1-4_original_compiled --compute-units all "a photo of an astronaut riding a horse on mars"
```

`--resource-path`์— ์ด์ „ ๋‹จ๊ณ„์—์„œ ๋‹ค์šด๋กœ๋“œํ•œ ์ฒดํฌํฌ์ธํŠธ ์ค‘ ํ•˜๋‚˜๋ฅผ ์ง€์ •ํ•ด์•ผ ํ•˜๋ฏ€๋กœ ํ™•์žฅ์ž๊ฐ€ `.mlmodelc`์ธ ์ปดํŒŒ์ผ๋œ Core ML ๋ฒˆ๋“ค์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. `--compute-units`๋Š” ๋‹ค์Œ ๊ฐ’ ์ค‘ ํ•˜๋‚˜์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: `all`, `cpuOnly`, `cpuAndGPU`, `cpuAndNeuralEngine`.

์ž์„ธํ•œ ๋‚ด์šฉ์€ [Apple์˜ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ ์•ˆ์˜ ์ง€์นจ](https://github.com/apple/ml-stable-diffusion)์„ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.


## ์ง€์›๋˜๋Š” Diffusers ๊ธฐ๋Šฅ

Core ML ๋ชจ๋ธ๊ณผ ์ถ”๋ก  ์ฝ”๋“œ๋Š” ๐Ÿงจ Diffusers์˜ ๋งŽ์€ ๊ธฐ๋Šฅ, ์˜ต์…˜ ๋ฐ ์œ ์—ฐ์„ฑ์„ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์œ ์˜ํ•ด์•ผ ํ•  ๋ช‡ ๊ฐ€์ง€ ์ œํ•œ ์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค:

- Core ML ๋ชจ๋ธ์€ ์ถ”๋ก ์—๋งŒ ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. ํ•™์Šต์ด๋‚˜ ํŒŒ์ธํŠœ๋‹์—๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
- Swift์— ํฌํŒ…๋œ ์Šค์ผ€์ค„๋Ÿฌ๋Š” Stable Diffusion์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋ณธ ์Šค์ผ€์ค„๋Ÿฌ์™€ `diffusers` ๊ตฌํ˜„์—์„œ Swift๋กœ ํฌํŒ…ํ•œ `DPMSolverMultistepScheduler` ๋‘ ๊ฐœ๋ฟ์ž…๋‹ˆ๋‹ค. ์ด๋“ค ์ค‘ ์•ฝ ์ ˆ๋ฐ˜์˜ ์Šคํ…์œผ๋กœ ๋™์ผํ•œ ํ’ˆ์งˆ์„ ์ƒ์„ฑํ•˜๋Š” `DPMSolverMultistepScheduler`๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.
- ์ถ”๋ก  ์ฝ”๋“œ์—์„œ ๋„ค๊ฑฐํ‹ฐ๋ธŒ ํ”„๋กฌํ”„ํŠธ, classifier-free guidance scale ๋ฐ image-to-image ์ž‘์—…์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. depth guidance, ControlNet, latent upscalers์™€ ๊ฐ™์€ ๊ณ ๊ธ‰ ๊ธฐ๋Šฅ์€ ์•„์ง ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

Apple์˜ [๋ณ€ํ™˜ ๋ฐ ์ถ”๋ก  ๋ฆฌํฌ์ง€ํ† ๋ฆฌ](https://github.com/apple/ml-stable-diffusion)์™€ ์ž์ฒด [swift-coreml-diffusers](https://github.com/huggingface/swift-coreml-diffusers) ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋Š” ๋‹ค๋ฅธ ๊ฐœ๋ฐœ์ž๋“ค์ด ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ์ˆ ์ ์ธ ๋ฐ๋ชจ์ž…๋‹ˆ๋‹ค. 

๋ˆ„๋ฝ๋œ ๊ธฐ๋Šฅ์ด ์žˆ๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋ฉด ์–ธ์ œ๋“ ์ง€ ๊ธฐ๋Šฅ์„ ์š”์ฒญํ•˜๊ฑฐ๋‚˜, ๋” ์ข‹์€ ๋ฐฉ๋ฒ•์€ ๊ธฐ์—ฌ PR์„ ์—ด์–ด์ฃผ์„ธ์š”. :)


## ๋„ค์ดํ‹ฐ๋ธŒ Diffusers Swift ์•ฑ

์ž์ฒด Apple ํ•˜๋“œ์›จ์–ด์—์„œ Stable Diffusion์„ ์‹คํ–‰ํ•˜๋Š” ์‰ฌ์šด ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋Š” `diffusers`์™€ Apple์˜ ๋ณ€ํ™˜ ๋ฐ ์ถ”๋ก  ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” [์ž์ฒด ์˜คํ”ˆ ์†Œ์Šค Swift ๋ฆฌํฌ์ง€ํ† ๋ฆฌ](https://github.com/huggingface/swift-coreml-diffusers)๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ฝ”๋“œ๋ฅผ ๊ณต๋ถ€ํ•˜๊ณ  [Xcode](https://developer.apple.com/xcode/)๋กœ ์ปดํŒŒ์ผํ•˜์—ฌ ํ•„์š”์— ๋งž๊ฒŒ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŽธ์˜๋ฅผ ์œ„ํ•ด ์•ฑ์Šคํ† ์–ด์— [๋…๋ฆฝํ˜• Mac ์•ฑ](https://apps.apple.com/app/diffusers/id1666309574)๋„ ์žˆ์œผ๋ฏ€๋กœ ์ฝ”๋“œ๋‚˜ IDE๋ฅผ ๋‹ค๋ฃจ์ง€ ์•Š๊ณ ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐœ๋ฐœ์ž๋กœ์„œ Core ML์ด Stable Diffusion ์•ฑ์„ ๊ตฌ์ถ•ํ•˜๋Š” ๋ฐ ๊ฐ€์žฅ ์ ํ•ฉํ•œ ์†”๋ฃจ์…˜์ด๋ผ๊ณ  ํŒ๋‹จํ–ˆ๋‹ค๋ฉด, ์ด ๊ฐ€์ด๋“œ์˜ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์„ ์‚ฌ์šฉํ•˜์—ฌ ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์ด ๋ฌด์—‡์„ ๋นŒ๋“œํ• ์ง€ ๊ธฐ๋Œ€๋ฉ๋‹ˆ๋‹ค. :)