|
<!--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. |
|
--> |
|
|
|
|
|
# μΆλ‘ μ μν΄ ONNX λ°νμμ μ¬μ©νλ λ°©λ² |
|
|
|
π€ Diffusersλ ONNX Runtimeκ³Ό νΈνλλ Stable Diffusion νμ΄νλΌμΈμ μ 곡ν©λλ€. μ΄λ₯Ό ν΅ν΄ ONNX(CPU ν¬ν¨)λ₯Ό μ§μνκ³ PyTorchμ κ°μ λ²μ μ μ¬μ©ν μ μλ λͺ¨λ νλμ¨μ΄μμ Stable Diffusionμ μ€νν μ μμ΅λλ€. |
|
|
|
## μ€μΉ |
|
|
|
λ€μ λͺ
λ Ήμ΄λ‘ ONNX Runtimeλ₯Ό μ§μνλ π€ Optimumλ₯Ό μ€μΉν©λλ€: |
|
|
|
```sh |
|
pip install optimum["onnxruntime"] |
|
``` |
|
|
|
## Stable Diffusion μΆλ‘ |
|
|
|
μλ μ½λλ ONNX λ°νμμ μ¬μ©νλ λ°©λ²μ 보μ¬μ€λλ€. `StableDiffusionPipeline` λμ `OnnxStableDiffusionPipeline`μ μ¬μ©ν΄μΌ ν©λλ€. |
|
PyTorch λͺ¨λΈμ λΆλ¬μ€κ³ μ¦μ ONNX νμμΌλ‘ λ³ννλ €λ κ²½μ° `export=True`λ‘ μ€μ ν©λλ€. |
|
|
|
```python |
|
from optimum.onnxruntime import ORTStableDiffusionPipeline |
|
|
|
model_id = "stable-diffusion-v1-5/stable-diffusion-v1-5" |
|
pipe = ORTStableDiffusionPipeline.from_pretrained(model_id, export=True) |
|
prompt = "a photo of an astronaut riding a horse on mars" |
|
images = pipe(prompt).images[0] |
|
pipe.save_pretrained("./onnx-stable-diffusion-v1-5") |
|
``` |
|
|
|
νμ΄νλΌμΈμ ONNX νμμΌλ‘ μ€νλΌμΈμΌλ‘ λ΄λ³΄λ΄κ³ λμ€μ μΆλ‘ μ μ¬μ©νλ €λ κ²½μ°, |
|
[`optimum-cli export`](https://huggingface.co/docs/optimum/main/en/exporters/onnx/usage_guides/export_a_model#exporting-a-model-to-onnx-using-the-cli) λͺ
λ Ήμ΄λ₯Ό μ¬μ©ν μ μμ΅λλ€: |
|
|
|
```bash |
|
optimum-cli export onnx --model stable-diffusion-v1-5/stable-diffusion-v1-5 sd_v15_onnx/ |
|
``` |
|
|
|
κ·Έ λ€μ μΆλ‘ μ μνν©λλ€: |
|
|
|
```python |
|
from optimum.onnxruntime import ORTStableDiffusionPipeline |
|
|
|
model_id = "sd_v15_onnx" |
|
pipe = ORTStableDiffusionPipeline.from_pretrained(model_id) |
|
prompt = "a photo of an astronaut riding a horse on mars" |
|
images = pipe(prompt).images[0] |
|
``` |
|
|
|
Notice that we didn't have to specify `export=True` above. |
|
|
|
[Optimum λ¬Έμ](https://huggingface.co/docs/optimum/)μμ λ λ§μ μμλ₯Ό μ°Ύμ μ μμ΅λλ€. |
|
|
|
## μλ €μ§ μ΄μλ€ |
|
|
|
- μ¬λ¬ ν둬ννΈλ₯Ό λ°°μΉλ‘ μμ±νλ©΄ λ무 λ§μ λ©λͺ¨λ¦¬κ° μ¬μ©λλ κ² κ°μ΅λλ€. μ΄λ₯Ό μ‘°μ¬νλ λμ, λ°°μΉ λμ λ°λ³΅ λ°©λ²μ΄ νμν μλ μμ΅λλ€. |
|
|