| <!--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/)์์ ๋ ๋ง์ ์์๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค. |
|
|
| ## ์๋ ค์ง ์ด์๋ค |
|
|
| - ์ฌ๋ฌ ํ๋กฌํํธ๋ฅผ ๋ฐฐ์น๋ก ์์ฑํ๋ฉด ๋๋ฌด ๋ง์ ๋ฉ๋ชจ๋ฆฌ๊ฐ ์ฌ์ฉ๋๋ ๊ฒ ๊ฐ์ต๋๋ค. ์ด๋ฅผ ์กฐ์ฌํ๋ ๋์, ๋ฐฐ์น ๋์ ๋ฐ๋ณต ๋ฐฉ๋ฒ์ด ํ์ํ ์๋ ์์ต๋๋ค. |
|
|