Diffusers documentation

AWS Neuron

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v0.35.1).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

AWS Neuron

Diffusers 功能可在 AWS Inf2 实例上使用,这些是由 Neuron 机器学习加速器驱动的 EC2 实例。这些实例旨在提供更好的计算性能(更高的吞吐量、更低的延迟)和良好的成本效益,使其成为 AWS 用户将扩散模型部署到生产环境的良好选择。

Optimum Neuron 是 Hugging Face 库与 AWS 加速器之间的接口,包括 AWS Trainium 和 AWS Inferentia。它支持 Diffusers 中的许多功能,并具有类似的 API,因此如果您已经熟悉 Diffusers,学习起来更容易。一旦您创建了 AWS Inf2 实例,请安装 Optimum Neuron。

python -m pip install --upgrade-strategy eager optimum[neuronx]

我们提供预构建的 Hugging Face Neuron 深度学习 AMI(DLAMI)和用于 Amazon SageMaker 的 Optimum Neuron 容器。建议正确设置您的环境。

下面的示例演示了如何在 inf2.8xlarge 实例上使用 Stable Diffusion XL 模型生成图像(一旦模型编译完成,您可以切换到更便宜的 inf2.xlarge 实例)。要生成一些图像,请使用 NeuronStableDiffusionXLPipeline 类,该类类似于 Diffusers 中的 StableDiffusionXLPipeline 类。

与 Diffusers 不同,您需要将管道中的模型编译为 Neuron 格式,即 .neuron。运行以下命令将模型导出为 .neuron 格式。

optimum-cli export neuron --model stabilityai/stable-diffusion-xl-base-1.0 \
  --batch_size 1 \
  --height 1024 `# 生成图像的高度(像素),例如 768, 1024` \
  --width 1024 `# 生成图像的宽度(像素),例如 768, 1024` \
  --num_images_per_prompt 1 `# 每个提示生成的图像数量,默认为 1` \
  --auto_cast matmul `# 仅转换矩阵乘法操作` \
  --auto_cast_type bf16 `# 将操作从 FP32 转换为 BF16` \
  sd_neuron_xl/

现在使用预编译的 SDXL 模型生成一些图像。

>>> from optimum.neuron import Neu
ronStableDiffusionXLPipeline

>>> 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]
peggy generated by sdxl on inf2

欢迎查看Optimum Neuron 文档中更多不同用例的指南和示例!

< > Update on GitHub