# 가이드

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

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

## 가이더 전환

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

[get_component_spec()](/docs/diffusers/main/ko/api/modular_diffusers/pipeline#diffusers.ModularPipeline.get_component_spec)을 사용하여 guider를 검사합니다.

```py
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()](/docs/diffusers/main/ko/api/modular_diffusers/pipeline#diffusers.ModularPipeline.update_components)에 전달하여 다른 guider로 전환합니다.

> [!TIP]
> 가이더를 변경하면 가이더 유형이 변경되었음을 알려주는 텍스트가 반환됩니다.
> ```bash
> ModularPipeline.update_components: adding guider with new type: PerturbedAttentionGuidance, previous type: ClassifierFreeGuidance
> ```

```py
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()](/docs/diffusers/main/ko/api/modular_diffusers/pipeline#diffusers.ModularPipeline.get_component_spec)를 다시 사용하여 guider 유형이 다른지 확인합니다.

```py
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` 컴포넌트로 간주됩니다.

```json
{
  "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()](/docs/diffusers/main/ko/api/modular_diffusers/pipeline#diffusers.ModularPipeline.load_components)를 호출한 후에만 생성됩니다.

```py
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()](/docs/diffusers/main/ko/api/modular_diffusers/pipeline_components#diffusers.ComponentSpec.create) 메서드와 [update_components()](/docs/diffusers/main/ko/api/modular_diffusers/pipeline#diffusers.ModularPipeline.update_components)로 조정할 수 있습니다. 아래 예시에서는 `guidance_scale` 값을 변경합니다.

```py
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()](/docs/diffusers/main/ko/api/schedulers/overview#diffusers.utils.PushToHubMixin.push_to_hub) 메서드를 호출하여 Hub에 공유합니다.

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

이 guider를 pipeline에서 사용할 수 있도록 하려면 `modular_model_index.json` 파일을 수정하거나 [update_components()](/docs/diffusers/main/ko/api/modular_diffusers/pipeline#diffusers.ModularPipeline.update_components) 메서드를 사용하세요.

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

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

`default_creation_method()`을 `from_pretrained`(으)로 변경하고 [update_components()](/docs/diffusers/main/ko/api/modular_diffusers/pipeline#diffusers.ModularPipeline.update_components)를 사용하여 guider와 component 사양 및 pipeline config도 업데이트합니다.

> [!TIP]
> 생성 방법을 변경하면 생성 유형이 `from_pretrained`(으)로 변경됨을 알려주는 텍스트가 반환됩니다.
> ```bash
> ModularPipeline.update_components: changing the default_creation_method of guider from from_config to from_pretrained.
> ```

```py
guider_spec = t2i_pipeline.get_component_spec("guider")
guider_spec.default_creation_method="from_pretrained"
guider_spec.pretrained_model_name_or_path="YiYiXu/modular-loader-t2i-guider"
guider_spec.subfolder="pag_guider"
pag_guider = guider_spec.load()
t2i_pipeline.update_components(guider=pag_guider)
```

파이프라인의 기본 guider로 설정하려면 [push_to_hub()](/docs/diffusers/main/ko/api/schedulers/overview#diffusers.utils.PushToHubMixin.push_to_hub)를 호출하세요. 이 단계는 선택 사항이며, 로컬에서만 실험하는 경우 불필요합니다.

```py
t2i_pipeline.push_to_hub("YiYiXu/modular-doc-guider")
```

