AWS Trainium & Inferentia 文档

加载适配器

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

加载适配器

有几种训练技术可以个性化扩散模型,以生成特定主题或特定风格的图像。这些训练方法中的每一种都会产生不同类型的适配器。有些适配器会生成一个全新的模型,而另一些适配器则只修改一小部分嵌入或权重。这意味着每个适配器的加载过程也不同。

本指南将向您展示如何加载 LoRA 权重。

LoRA

低秩适应是 Stable Diffusion 调整生成图像风格最快的方法。在 Optimum Neuron 中,我们支持通过在编译过程中将其参数融合到文本编码器和 unet 的原始参数中来使用一个或多个 LoRA 适配器。下面是一个示例,展示如何使用您选择的 LoRA 适配器编译 Stable Diffusion 模型,并使用编译好的工件生成风格化图像。


from diffusers import LCMScheduler
from optimum.neuron import NeuronStableDiffusionPipeline


model_id = "Lykon/dreamshaper-7"
adapter_id = "latent-consistency/lcm-lora-sdv1-5"
input_shapes = {"batch_size": 1, "height": 512, "width": 512, "num_images_per_prompt": 1}
compiler_args = {"auto_cast": "matmul", "auto_cast_type": "bf16"}

# Compile
pipe = NeuronStableDiffusionPipeline.from_pretrained(
    model_id,
    export=True,
    inline_weights_to_neff=True,  # caveat: performance drop if neff/weights separated, will be improved by a future Neuron sdk release.
    lora_model_ids=adapter_id,
    lora_weight_names="pytorch_lora_weights.safetensors",
    lora_adapter_names="lcm",
    **input_shapes,
    **compiler_args,
)
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)

# Save locally or upload to the HuggingFace Hub
pipe.save_pretrained("dreamshaper_7_lcm_lora_neuron/")


# Inference
prompt = "Self-portrait oil painting, a beautiful cyborg with golden hair, 8k"
image = pipe(prompt, num_inference_steps=4, guidance_scale=0).images[0]
stable diffusion generated image with LoRA adapter.

您希望我们在 🤗Optimum-neuron 中支持其他扩散功能吗?请向 Optimum-neuron Github 仓库 提交问题,或在 HuggingFace 的社区论坛 上与我们讨论,祝好 🤗!