Diffusers documentation

Người hướng dẫn

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

and get access to the augmented documentation experience

to get started

Người hướng dẫn

Classifier-free guidance điều hướng quá trình tạo của mô hình sao cho khớp hơn với prompt và thường được sử dụng để cải thiện chất lượng, khả năng kiểm soát và mức độ tuân thủ prompt. Có nhiều loại phương pháp guidance khác nhau, và trong Diffusers, chúng được gọi là guiders. Giống như blocks, việc chuyển đổi và sử dụng các guider khác nhau cho các trường hợp sử dụng khác nhau mà không cần viết lại pipeline là rất dễ dàng.

Hướng dẫn này sẽ cho bạn thấy cách chuyển đổi guider, điều chỉnh các tham số của guider, cũng như tải và chia sẻ chúng lên Hub.

Chuyển đổi bộ dẫn

ClassifierFreeGuidance là guider mặc định và được tạo khi một pipeline được khởi tạo bằng init_pipeline(). Nó được tạo bởi from_config, điều này có nghĩa là nó không yêu cầu nạp thông số từ một kho lưu trữ modular. Một guider sẽ không được liệt kê trong modular_model_index.json.

Sử dụng get_component_spec() để kiểm tra một guider.

t2i_pipeline.get_component_spec("guider")
ComponentSpec(name='guider', type_hint=<class 'diffusers.guiders.classifier_free_guidance.ClassifierFreeGuidance'>, description=None, config=FrozenDict([('guidance_scale', 7.5), ('guidance_rescale', 0.0), ('use_original_formulation', False), ('start', 0.0), ('stop', 1.0), ('_use_default_values', ['start', 'guidance_rescale', 'stop', 'use_original_formulation'])]), repo=None, subfolder=None, variant=None, revision=None, default_creation_method='from_config')

Chuyển sang một guider khác bằng cách truyền guider mới vào update_components().

Việc thay đổi guider sẽ trả về văn bản thông báo cho bạn biết rằng bạn đang thay đổi loại guider.

ModularPipeline.update_components: adding guider with new type: PerturbedAttentionGuidance, previous type: ClassifierFreeGuidance
from diffusers import LayerSkipConfig, PerturbedAttentionGuidance

config = LayerSkipConfig(indices=[2, 9], fqn="mid_block.attentions.0.transformer_blocks", skip_attention=False, skip_attention_scores=True, skip_ff=False)
guider = PerturbedAttentionGuidance(
    guidance_scale=5.0, perturbed_guidance_scale=2.5, perturbed_guidance_config=config
)
t2i_pipeline.update_components(guider=guider)

Sử dụng get_component_spec() một lần nữa để xác nhận loại guider đã khác.

t2i_pipeline.get_component_spec("guider")
ComponentSpec(name='guider', type_hint=<class 'diffusers.guiders.perturbed_attention_guidance.PerturbedAttentionGuidance'>, description=None, config=FrozenDict([('guidance_scale', 5.0), ('perturbed_guidance_scale', 2.5), ('perturbed_guidance_start', 0.01), ('perturbed_guidance_stop', 0.2), ('perturbed_guidance_layers', None), ('perturbed_guidance_config', LayerSkipConfig(indices=[2, 9], fqn='mid_block.attentions.0.transformer_blocks', skip_attention=False, skip_attention_scores=True, skip_ff=False, dropout=1.0)), ('guidance_rescale', 0.0), ('use_original_formulation', False), ('start', 0.0), ('stop', 1.0), ('_use_default_values', ['perturbed_guidance_start', 'use_original_formulation', 'perturbed_guidance_layers', 'stop', 'start', 'guidance_rescale', 'perturbed_guidance_stop']), ('_class_name', 'PerturbedAttentionGuidance'), ('_diffusers_version', '0.35.0.dev0')]), repo=None, subfolder=None, variant=None, revision=None, default_creation_method='from_config')

Tải các bộ hướng dẫn tùy chỉnh

Guiders đã được lưu trên Hub với tệp modular_model_index.json hiện được coi là thành phần from_pretrained thay vì thành phần from_config.

{
  "guider": [
    null,
    null,
    {
      "repo": "YiYiXu/modular-loader-t2i-guider",
      "revision": null,
      "subfolder": "pag_guider",
      "type_hint": [
        "diffusers",
        "PerturbedAttentionGuidance"
      ],
      "variant": null
    }
  ]
}

Guider chỉ được tạo ra sau khi gọi load_components() dựa trên thông số nạp trong modular_model_index.json.

t2i_pipeline = t2i_blocks.init_pipeline("YiYiXu/modular-doc-guider")
# not created during init
assert t2i_pipeline.guider is None
t2i_pipeline.load_components()
# loaded as PAG guider
t2i_pipeline.guider

Thay đổi các tham số của guider

Các tham số của guider có thể được điều chỉnh bằng phương thức create() và update_components(). Ví dụ bên dưới thay đổi giá trị của guidance_scale.

guider_spec = t2i_pipeline.get_component_spec("guider")
guider = guider_spec.create(guidance_scale=10)
t2i_pipeline.update_components(guider=guider)

Tải lên các guider tùy chỉnh

Gọi phương thức push_to_hub() trên một guider tùy chỉnh để chia sẻ nó lên Hub.

guider.push_to_hub("YiYiXu/modular-loader-t2i-guider", subfolder="pag_guider")

Để guider này có sẵn cho pipeline, hãy sửa tệp modular_model_index.json hoặc sử dụng phương thức update_components().

modular_model_index.json
update_components

Sửa tệp modular_model_index.json và thêm thông số nạp cho guider bằng cách trỏ đến thư mục chứa cấu hình guider.

{
  "guider": [
    "diffusers",
    "PerturbedAttentionGuidance",
    {
      "repo": "YiYiXu/modular-loader-t2i-guider",
      "revision": null,
      "subfolder": "pag_guider",
      "type_hint": [
        "diffusers",
        "PerturbedAttentionGuidance"
      ],
      "variant": null
    }
  ],
Update on GitHub