Skip to content

Commit 52d4449

Browse files
Add a doc for AWS Neuron in Diffusers (#9766)
* start draft * add doc * Update docs/source/en/optimization/neuron.md Co-authored-by: Steven Liu <[email protected]> * Update docs/source/en/optimization/neuron.md Co-authored-by: Steven Liu <[email protected]> * Update docs/source/en/optimization/neuron.md Co-authored-by: Steven Liu <[email protected]> * Update docs/source/en/optimization/neuron.md Co-authored-by: Steven Liu <[email protected]> * Update docs/source/en/optimization/neuron.md Co-authored-by: Steven Liu <[email protected]> * Update docs/source/en/optimization/neuron.md Co-authored-by: Steven Liu <[email protected]> * Update docs/source/en/optimization/neuron.md Co-authored-by: Steven Liu <[email protected]> * bref intro of ON * Update docs/source/en/optimization/neuron.md Co-authored-by: Steven Liu <[email protected]> --------- Co-authored-by: Steven Liu <[email protected]>
1 parent df073ba commit 52d4449

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

docs/source/en/_toctree.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@
188188
title: Metal Performance Shaders (MPS)
189189
- local: optimization/habana
190190
title: Habana Gaudi
191+
- local: optimization/neuron
192+
title: AWS Neuron
191193
title: Optimized hardware
192194
title: Accelerate inference and reduce memory
193195
- sections:

docs/source/en/optimization/neuron.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<!--Copyright 2024 The HuggingFace Team. All rights reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
4+
the License. You may obtain a copy of the License at
5+
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
8+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
9+
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
10+
specific language governing permissions and limitations under the License.
11+
-->
12+
13+
# AWS Neuron
14+
15+
Diffusers functionalities are available on [AWS Inf2 instances](https://aws.amazon.com/ec2/instance-types/inf2/), which are EC2 instances powered by [Neuron machine learning accelerators](https://aws.amazon.com/machine-learning/inferentia/). These instances aim to provide better compute performance (higher throughput, lower latency) with good cost-efficiency, making them good candidates for AWS users to deploy diffusion models to production.
16+
17+
[Optimum Neuron](https://huggingface.co/docs/optimum-neuron/en/index) is the interface between Hugging Face libraries and AWS Accelerators, including AWS [Trainium](https://aws.amazon.com/machine-learning/trainium/) and AWS [Inferentia](https://aws.amazon.com/machine-learning/inferentia/). It supports many of the features in Diffusers with similar APIs, so it is easier to learn if you're already familiar with Diffusers. Once you have created an AWS Inf2 instance, install Optimum Neuron.
18+
19+
```bash
20+
python -m pip install --upgrade-strategy eager optimum[neuronx]
21+
```
22+
23+
<Tip>
24+
25+
We provide pre-built [Hugging Face Neuron Deep Learning AMI](https://aws.amazon.com/marketplace/pp/prodview-gr3e6yiscria2) (DLAMI) and Optimum Neuron containers for Amazon SageMaker. It's recommended to correctly set up your environment.
26+
27+
</Tip>
28+
29+
The example below demonstrates how to generate images with the Stable Diffusion XL model on an inf2.8xlarge instance (you can switch to cheaper inf2.xlarge instances once the model is compiled). To generate some images, use the [`~optimum.neuron.NeuronStableDiffusionXLPipeline`] class, which is similar to the [`StableDiffusionXLPipeline`] class in Diffusers.
30+
31+
Unlike Diffusers, you need to compile models in the pipeline to the Neuron format, `.neuron`. Launch the following command to export the model to the `.neuron` format.
32+
33+
```bash
34+
optimum-cli export neuron --model stabilityai/stable-diffusion-xl-base-1.0 \
35+
--batch_size 1 \
36+
--height 1024 `# height in pixels of generated image, eg. 768, 1024` \
37+
--width 1024 `# width in pixels of generated image, eg. 768, 1024` \
38+
--num_images_per_prompt 1 `# number of images to generate per prompt, defaults to 1` \
39+
--auto_cast matmul `# cast only matrix multiplication operations` \
40+
--auto_cast_type bf16 `# cast operations from FP32 to BF16` \
41+
sd_neuron_xl/
42+
```
43+
44+
Now generate some images with the pre-compiled SDXL model.
45+
46+
```python
47+
>>> from optimum.neuron import NeuronStableDiffusionXLPipeline
48+
49+
>>> stable_diffusion_xl = NeuronStableDiffusionXLPipeline.from_pretrained("sd_neuron_xl/")
50+
>>> prompt = "a pig with wings flying in floating US dollar banknotes in the air, skyscrapers behind, warm color palette, muted colors, detailed, 8k"
51+
>>> image = stable_diffusion_xl(prompt).images[0]
52+
```
53+
54+
<img
55+
src="https://huggingface.co/datasets/Jingya/document_images/resolve/main/optimum/neuron/sdxl_pig.png"
56+
width="256"
57+
height="256"
58+
alt="peggy generated by sdxl on inf2"
59+
/>
60+
61+
Feel free to check out more guides and examples on different use cases from the Optimum Neuron [documentation](https://huggingface.co/docs/optimum-neuron/en/inference_tutorials/stable_diffusion#generate-images-with-stable-diffusion-models-on-aws-inferentia)!

0 commit comments

Comments
 (0)