71 lines
3.3 KiB
Plaintext
71 lines
3.3 KiB
Plaintext
|
<!--Copyright 2022 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.
|
||
|
-->
|
||
|
|
||
|
# How to use Stable Diffusion on Habana Gaudi
|
||
|
|
||
|
🤗 Diffusers is compatible with Habana Gaudi through 🤗 [Optimum Habana](https://huggingface.co/docs/optimum/habana/usage_guides/stable_diffusion).
|
||
|
|
||
|
## Requirements
|
||
|
|
||
|
- Optimum Habana 1.3 or later, [here](https://huggingface.co/docs/optimum/habana/installation) is how to install it.
|
||
|
- SynapseAI 1.7.
|
||
|
|
||
|
|
||
|
## Inference Pipeline
|
||
|
|
||
|
To generate images with Stable Diffusion 1 and 2 on Gaudi, you need to instantiate two instances:
|
||
|
- A pipeline with [`GaudiStableDiffusionPipeline`](https://huggingface.co/docs/optimum/habana/package_reference/stable_diffusion_pipeline). This pipeline supports *text-to-image generation*.
|
||
|
- A scheduler with [`GaudiDDIMScheduler`](https://huggingface.co/docs/optimum/habana/package_reference/stable_diffusion_pipeline#optimum.habana.diffusers.GaudiDDIMScheduler). This scheduler has been optimized for Habana Gaudi.
|
||
|
|
||
|
When initializing the pipeline, you have to specify `use_habana=True` to deploy it on HPUs.
|
||
|
Furthermore, in order to get the fastest possible generations you should enable **HPU graphs** with `use_hpu_graphs=True`.
|
||
|
Finally, you will need to specify a [Gaudi configuration](https://huggingface.co/docs/optimum/habana/package_reference/gaudi_config) which can be downloaded from the [Hugging Face Hub](https://huggingface.co/Habana).
|
||
|
|
||
|
```python
|
||
|
from optimum.habana import GaudiConfig
|
||
|
from optimum.habana.diffusers import GaudiDDIMScheduler, GaudiStableDiffusionPipeline
|
||
|
|
||
|
model_name = "stabilityai/stable-diffusion-2-base"
|
||
|
scheduler = GaudiDDIMScheduler.from_pretrained(model_name, subfolder="scheduler")
|
||
|
pipeline = GaudiStableDiffusionPipeline.from_pretrained(
|
||
|
model_name,
|
||
|
scheduler=scheduler,
|
||
|
use_habana=True,
|
||
|
use_hpu_graphs=True,
|
||
|
gaudi_config="Habana/stable-diffusion",
|
||
|
)
|
||
|
```
|
||
|
|
||
|
You can then call the pipeline to generate images by batches from one or several prompts:
|
||
|
```python
|
||
|
outputs = pipeline(
|
||
|
prompt=[
|
||
|
"High quality photo of an astronaut riding a horse in space",
|
||
|
"Face of a yellow cat, high resolution, sitting on a park bench",
|
||
|
],
|
||
|
num_images_per_prompt=10,
|
||
|
batch_size=4,
|
||
|
)
|
||
|
```
|
||
|
|
||
|
For more information, check out Optimum Habana's [documentation](https://huggingface.co/docs/optimum/habana/usage_guides/stable_diffusion) and the [example](https://github.com/huggingface/optimum-habana/tree/main/examples/stable-diffusion) provided in the official Github repository.
|
||
|
|
||
|
|
||
|
## Benchmark
|
||
|
|
||
|
Here are the latencies for Habana Gaudi 1 and Gaudi 2 with the [Habana/stable-diffusion](https://huggingface.co/Habana/stable-diffusion) Gaudi configuration (mixed precision bf16/fp32):
|
||
|
|
||
|
| | Latency | Batch size |
|
||
|
| ------- |:-------:|:----------:|
|
||
|
| Gaudi 1 | 4.37s | 4/8 |
|
||
|
| Gaudi 2 | 1.19s | 4/8 |
|