Buckets:
| # Người hướng dẫn | |
| [Classifier-free guidance](https://huggingface.co/papers/2207.12598) đ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](/docs/diffusers/main/vi/api/modular_diffusers/guiders#diffusers.ClassifierFreeGuidance) là guider mặc định và được tạo khi một pipeline được khởi tạo bằng [init_pipeline()](/docs/diffusers/main/vi/api/modular_diffusers/pipeline_blocks#diffusers.ModularPipelineBlocks.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()](/docs/diffusers/main/vi/api/modular_diffusers/pipeline#diffusers.ModularPipeline.get_component_spec) để kiểm tra một 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') | |
| ``` | |
| Chuyển sang một guider khác bằng cách truyền guider mới vào [update_components()](/docs/diffusers/main/vi/api/modular_diffusers/pipeline#diffusers.ModularPipeline.update_components). | |
| > [!TIP] | |
| > 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. | |
| > ```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) | |
| ``` | |
| Sử dụng [get_component_spec()](/docs/diffusers/main/vi/api/modular_diffusers/pipeline#diffusers.ModularPipeline.get_component_spec) một lần nữa để xác nhận loại guider đã khác. | |
| ```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') | |
| ``` | |
| ## 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`. | |
| ```json | |
| { | |
| "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()](/docs/diffusers/main/vi/api/modular_diffusers/pipeline#diffusers.ModularPipeline.load_components) dựa trên thông số nạp trong `modular_model_index.json`. | |
| ```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 | |
| ``` | |
| ## 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()](/docs/diffusers/main/vi/api/modular_diffusers/pipeline_components#diffusers.ComponentSpec.create) và [update_components()](/docs/diffusers/main/vi/api/modular_diffusers/pipeline#diffusers.ModularPipeline.update_components). Ví dụ bên dưới thay đổi giá trị của `guidance_scale`. | |
| ```py | |
| 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()](/docs/diffusers/main/vi/api/schedulers/overview#diffusers.utils.PushToHubMixin.push_to_hub) trên một guider tùy chỉnh để chia sẻ nó lên Hub. | |
| ```py | |
| 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()](/docs/diffusers/main/vi/api/modular_diffusers/pipeline#diffusers.ModularPipeline.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. | |
| ```json | |
| { | |
| "guider": [ | |
| "diffusers", | |
| "PerturbedAttentionGuidance", | |
| { | |
| "repo": "YiYiXu/modular-loader-t2i-guider", | |
| "revision": null, | |
| "subfolder": "pag_guider", | |
| "type_hint": [ | |
| "diffusers", | |
| "PerturbedAttentionGuidance" | |
| ], | |
| "variant": null | |
| } | |
| ], | |
| ``` | |
| Thay đổi `default_creation_method()` thành `from_pretrained` và sử dụng [update_components()](/docs/diffusers/main/vi/api/modular_diffusers/pipeline#diffusers.ModularPipeline.update_components) để cập nhật thông số của guider và component cũng như cấu hình pipeline. | |
| > [!TIP] | |
| > Việc thay đổi phương pháp tạo sẽ trả về văn bản thông báo cho bạn biết rằng bạn đang thay đổi kiểu tạo thành `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) | |
| ``` | |
| Để đặt nó làm guider mặc định cho một pipeline, hãy gọi [push_to_hub()](/docs/diffusers/main/vi/api/schedulers/overview#diffusers.utils.PushToHubMixin.push_to_hub). Đây là bước tùy chọn và không cần thiết nếu bạn chỉ thử nghiệm cục bộ. | |
| ```py | |
| t2i_pipeline.push_to_hub("YiYiXu/modular-doc-guider") | |
| ``` | |
Xet Storage Details
- Size:
- 8.01 kB
- Xet hash:
- 40c03dd8016a19f77c61f83b1ba4382d4ffe2563079cf32ec9691419189219c5
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.