-
Couldn't load subscription status.
- Fork 6.5k
Add a doc for AWS Neuron in Diffusers #9766
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8ad2f24
start draft
JingyaHuang ca99541
add doc
JingyaHuang 1ac2983
Update docs/source/en/optimization/neuron.md
JingyaHuang 7bf5c8d
Update docs/source/en/optimization/neuron.md
JingyaHuang 76e422a
Update docs/source/en/optimization/neuron.md
JingyaHuang eab62e7
Update docs/source/en/optimization/neuron.md
JingyaHuang b589e1d
Update docs/source/en/optimization/neuron.md
JingyaHuang b507be6
Update docs/source/en/optimization/neuron.md
JingyaHuang e3db656
Update docs/source/en/optimization/neuron.md
JingyaHuang cb0bdd8
bref intro of ON
JingyaHuang 1d04bc9
Merge branch 'main' into add-neuron-doc
JingyaHuang 5a6153b
Update docs/source/en/optimization/neuron.md
JingyaHuang 98842df
Merge branch 'main' into add-neuron-doc
JingyaHuang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| <!--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. | ||
| --> | ||
|
|
||
| # AWS Neuron | ||
|
|
||
| 🤗 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 at providing better compute performance(higher throughput, lower latency) with good cost-efficiency, which makes them good candidates for AWS users to deploy diffusion models for production. | ||
|
|
||
| A wide range of features in 🤗 Diffusers are supported by [🤗 Optimum Neuron](https://huggingface.co/docs/optimum-neuron/en/index) via similar APIs. Once you have created an AWS Inf2 instance, you can install 🤗 Optimum Neuron: | ||
JingyaHuang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```bash | ||
| python -m pip install --upgrade-strategy eager optimum[neuronx] | ||
| ``` | ||
|
|
||
| <Tip> | ||
|
|
||
| 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 | ||
JingyaHuang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| </Tip> | ||
|
|
||
| Here below is an example of generating images with Stable Diffusion XL model on an inf2.8xlarge instance (you can switch to cheaper inf2.xlarge instances once the model is compiled). We will use the `NeuronStableDiffusionXLPipeline` class, a class similar to the `StableDiffusionXLPipeline` class in diffusers to generate some images for fun. | ||
JingyaHuang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Unlike in 🤗 Diffusers, we need to compile models in the pipeline to Neuron compatible format `.neuron`. To do this, you will need to launch the following command: | ||
JingyaHuang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```bash | ||
| optimum-cli export neuron --model stabilityai/stable-diffusion-xl-base-1.0 \ | ||
| --batch_size 1 \ | ||
| --height 1024 `# height in pixels of generated image, eg. 768, 1024` \ | ||
| --width 1024 `# width in pixels of generated image, eg. 768, 1024` \ | ||
| --num_images_per_prompt 1 `# number of images to generate per prompt, defaults to 1` \ | ||
| --auto_cast matmul `# cast only matrix multiplication operations` \ | ||
| --auto_cast_type bf16 `# cast operations from FP32 to BF16` \ | ||
| sd_neuron_xl/ | ||
| ``` | ||
|
|
||
| Now let's generate some images with pre-compiled SDXL models: | ||
JingyaHuang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```python | ||
| >>> from optimum.neuron import NeuronStableDiffusionXLPipeline | ||
|
|
||
| >>> stable_diffusion_xl = NeuronStableDiffusionXLPipeline.from_pretrained("sd_neuron_xl/") | ||
| >>> prompt = "a pig with wings flying in floating US dollar banknotes in the air, skyscrapers behind, warm color palette, muted colors, detailed, 8k" | ||
| >>> image = stable_diffusion_xl(prompt).images[0] | ||
| ``` | ||
|
|
||
| <img | ||
| src="https://huggingface.co/datasets/Jingya/document_images/resolve/main/optimum/neuron/sdxl_pig.png" | ||
| width="256" | ||
| height="256" | ||
| alt="peggy generated by sdxl on inf2" | ||
| /> | ||
|
|
||
| Feel free to check out more guides and examples on different use cases from the [🤗 Optimum Neuron's documentation](https://huggingface.co/docs/optimum-neuron/en/inference_tutorials/stable_diffusion#generate-images-with-stable-diffusion-models-on-aws-inferentia)! | ||
JingyaHuang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.