AWS Trainium & Inferentia 文档
IP-Adapter
并获得增强的文档体验
开始使用
IP-Adapter
概述
IP-Adapter 是一种图像提示适配器,可以插入到扩散模型中,从而无需更改底层模型即可启用图像提示。此外,该适配器可以与从相同基础模型微调的其他模型重复使用,并且可以与其他适配器(如 ControlNet)结合使用。IP-Adapter 背后的关键思想是解耦交叉注意力机制,它为图像特征添加了一个单独的交叉注意力层,而不是将相同的交叉注意力层用于文本和图像特征。这使得模型能够学习更多特定于图像的特征。
🤗 Optimum
扩展了 Diffusers
以支持在第二代 Neuron 设备(支持 Trainium 和 Inferentia 2)上进行推理。它旨在继承 Diffusers 在 Neuron 上的易用性。
导出到 Neuron
要部署模型,您需要将它们编译为针对 AWS Neuron 优化的 TorchScript。
您可以通过 CLI 或 NeuronStableDiffusionPipeline
类编译和导出 Stable Diffusion 检查点。
选项 1:CLI
以下是使用 Optimum
CLI 导出 Stable Diffusion 组件的示例
optimum-cli export neuron --model stable-diffusion-v1-5/stable-diffusion-v1-5
--ip_adapter_id h94/IP-Adapter
--ip_adapter_subfolder models
--ip_adapter_weight_name ip-adapter-full-face_sd15.bin
--ip_adapter_scale 0.5
--batch_size 1 --height 512 --width 512 --num_images_per_prompt 1
--auto_cast matmul --auto_cast_type bf16 ip_adapter_neuron/
我们建议使用 inf2.8xlarge
或更大的实例进行模型编译。您也可以使用 Optimum CLI 在仅限 CPU 的实例上编译模型(需要约 35 GB 内存),然后通过 inf2.xlarge
运行预编译的模型以降低费用。在这种情况下,请不要忘记通过添加 --disable-validation
参数来禁用推理验证。
选项 2:Python API
以下是使用 NeuronStableDiffusionPipeline
导出 Stable Diffusion 组件的示例
from optimum.neuron import NeuronStableDiffusionPipeline
model_id = "stable-diffusion-v1-5/stable-diffusion-v1-5"
compiler_args = {"auto_cast": "matmul", "auto_cast_type": "bf16"}
input_shapes = {"batch_size": 1, "height": 512, "width": 512}
stable_diffusion = NeuronStableDiffusionPipeline.from_pretrained(
model_id,
export=True,
ip_adapter_id="h94/IP-Adapter",
ip_adapter_subfolder="models",
ip_adapter_weight_name="ip-adapter-full-face_sd15.bin",
ip_adapter_scale=0.5,
**compiler_args,
**input_shapes,
)
# Save locally or upload to the HuggingFace Hub
save_directory = "ip_adapter_neuron/"
stable_diffusion.save_pretrained(save_directory)
文本到图像
- 以
ip_adapter_image
作为输入
from optimum.neuron import NeuronStableDiffusionPipeline
model_id = "stable-diffusion-v1-5/stable-diffusion-v1-5"
compiler_args = {"auto_cast": "matmul", "auto_cast_type": "bf16"}
input_shapes = {"batch_size": 1, "height": 512, "width": 512}
stable_diffusion = NeuronStableDiffusionPipeline.from_pretrained(
model_id,
export=True,
ip_adapter_id="h94/IP-Adapter",
ip_adapter_subfolder="models",
ip_adapter_weight_name="ip-adapter-full-face_sd15.bin",
ip_adapter_scale=0.5,
**compiler_args,
**input_shapes,
)
# Save locally or upload to the HuggingFace Hub
save_directory = "ip_adapter_neuron/"
stable_diffusion.save_pretrained(save_directory)
- 以
ip_adapter_image_embeds
作为输入(首先对图像进行编码)
image_embeds = stable_diffusion.prepare_ip_adapter_image_embeds(
ip_adapter_image=image,
ip_adapter_image_embeds=None,
device=None,
num_images_per_prompt=1,
do_classifier_free_guidance=True,
)
torch.save(image_embeds, "image_embeds.ipadpt")
image_embeds = torch.load("image_embeds.ipadpt")
images = stable_diffusion(
prompt="a polar bear sitting in a chair drinking a milkshake",
ip_adapter_image_embeds=image_embeds,
negative_prompt="deformed, ugly, wrong proportion, low res, bad anatomy, worst quality, low quality",
num_inference_steps=100,
generator=generator,
).images[0]
image.save("polar_bear.png")
您希望我们在 🤗Optimum-neuron
中支持其他扩散功能吗?请向 Optimum-neuron
Github 仓库 提交问题或在 HuggingFace 社区论坛上与我们讨论,祝您愉快 🤗!