Diffusers documentation
الموجّهون
الموجّهون
يوجّه Classifier-free guidance توليد النموذج ليتوافق بشكل أفضل مع الأمر النصي، ويُستخدم بشكل شائع لتحسين جودة التوليد والتحكم فيه والالتزام بالأوامر النصية. توجد أنواع مختلفة من طرق التوجيه، وفي Diffusers تُعرف هذه الطرق باسم guiders. وكما هو الحال مع الكتل، من السهل التبديل بين مختلف الـ guiders واستخدامها لحالات استخدام مختلفة دون الحاجة إلى إعادة كتابة خط الأنابيب.
ستُظهر لك هذه الدليل كيفية التبديل بين الموجّهات، وضبط معاملات الموجّه، وتحميلها ومشاركتها على Hub.
تبديل المرشدين
ClassifierFreeGuidance هو الموجِّه الافتراضي ويُنشأ عند تهيئة خط أنابيب باستخدام init_pipeline(). يتم إنشاؤه عبر from_config مما يعني أنه لا يتطلب تحميل مواصفات من مستودع معياري. لن يُدرج الموجِّه في modular_model_index.json.
استخدم get_component_spec() لفحص مُوجِّه.
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')قم بالتبديل إلى مُوجِّه مختلف عن طريق تمرير المُوجِّه الجديد إلى update_components().
سيؤدي تغيير المرشد إلى إرجاع نص يُعلمك أنك تقوم بتغيير نوع المرشد.
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() مرة أخرى للتحقق من أن نوع الموجّه مختلف.
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')تحميل مُوجِّهات مخصصة
تُعتبر المُوجِّهات التي تم حفظها مسبقًا على Hub مع ملف modular_model_index.json مكوّنًا من نوع from_pretrained الآن بدلاً من كونه مكوّنًا من نوع from_config.
{
"guider": [
null,
null,
{
"repo": "YiYiXu/modular-loader-t2i-guider",
"revision": null,
"subfolder": "pag_guider",
"type_hint": [
"diffusers",
"PerturbedAttentionGuidance"
],
"variant": null
}
]
}يتم إنشاء المُوجِّه فقط بعد استدعاء load_components() بناءً على مواصفات التحميل في 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تغيير معلمات الموجّه
يمكن ضبط معلمات الموجّه باستخدام الطريقة 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)رفع مُوجِّهات مخصصة
استدعِ الطريقة push_to_hub() على مُوجِّه مخصص لمشاركته على Hub.
guider.push_to_hub("YiYiXu/modular-loader-t2i-guider", subfolder="pag_guider")لجعل هذا المُوجِّه متاحًا للخطّ الأنابيب، عدِّل ملف modular_model_index.json أو استخدم طريقة update_components().
قم بتحرير ملف modular_model_index.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() لتحديث مواصفات الموجّه والمكوّنات بالإضافة إلى إعدادات خط المعالجة.
سيؤدي تغيير طريقة الإنشاء إلى إرجاع نص يُعلمك أنك تقوم بتغيير نوع الإنشاء إلى
from_pretrained.ModularPipeline.update_components: changing the default_creation_method of guider from from_config to from_pretrained.
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)لجعلها موجّهًا افتراضيًا لخط أنابيب، استدعِ push_to_hub(). هذه خطوة اختيارية ولا تكون ضرورية إذا كنت تجري تجارب محلية فقط.
t2i_pipeline.push_to_hub("YiYiXu/modular-doc-guider")