Diffusers documentation

가이드

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

가이드

Classifier-free guidance는 프롬프트와 더 잘 일치하는 모델 생성을 유도하며, 생성 품질, 제어, 프롬프트 준수도를 향상시키는 데 일반적으로 사용됩니다. 다양한 유형의 guidance 방법이 있으며, Diffusers에서는 이를 guiders라고 부릅니다. 블록과 마찬가지로, 파이프라인을 다시 작성하지 않고도 다른 사용 사례에 따라 다른 guiders를 쉽게 전환하여 사용할 수 있습니다.

이 가이드에서는 guider를 전환하고, guider 매개변수를 조정하며, Hub에 로드하고 공유하는 방법을 보여줍니다.

가이더 전환

ClassifierFreeGuidance는 기본 guider이며, 파이프라인이 init_pipeline()로 초기화될 때 생성됩니다. from_config를 통해 생성되므로 모듈형 저장소에서 사양을 로드할 필요가 없습니다. guider는 modular_model_index.json에 나열되지 않습니다.

get_component_spec()을 사용하여 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')

새 guider를 update_components()에 전달하여 다른 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)

get_component_spec()를 다시 사용하여 guider 유형이 다른지 확인합니다.

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')

커스텀 가이더 로드

modular_model_index.json 파일이 포함된 Hub에 이미 저장된 Guider는 이제 from_config 컴포넌트가 아닌 from_pretrained 컴포넌트로 간주됩니다.

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

가이더는 modular_model_index.json의 로딩 사양에 따라 load_components()를 호출한 후에만 생성됩니다.

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

가이더 파라미터 변경

가이더 파라미터는 create() 메서드와 update_components()로 조정할 수 있습니다. 아래 예시에서는 guidance_scale 값을 변경합니다.

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

커스텀 가이더 업로드

커스텀 guider에서 push_to_hub() 메서드를 호출하여 Hub에 공유합니다.

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

이 guider를 pipeline에서 사용할 수 있도록 하려면 modular_model_index.json 파일을 수정하거나 update_components() 메서드를 사용하세요.

modular_model_index.json
update_components

modular_model_index.json 파일을 편집하여 guider 설정이 포함된 폴더를 가리키도록 guider의 로딩 사양을 추가합니다.

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