Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"واجهات الخلفية للانتباه","local":"attention-backends","sections":[{"title":"تعيين واجهة خلفية على النموذج","local":"set-a-backend-on-the-model","sections":[],"depth":2},{"title":"جرّب واجهة خلفية مؤقتًا","local":"try-a-backend-temporarily","sections":[],"depth":2},{"title":"الثقة في النوى عن بُعد","local":"trusting-remote-kernels","sections":[],"depth":2},{"title":"فحوصات","local":"checks","sections":[],"depth":2},{"title":"الواجهات الخلفية المتاحة","local":"available-backends","sections":[],"depth":2}],"depth":1}"/> | |
| <link href="/docs/diffusers/main/en/_app/immutable/entry/start.B6roO9Zr.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/CjYFLaCl.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/Cc4T7sti.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/entry/app.CxxEVG8A.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/C8F28shN.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/BTnLXsn-.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/DnVIJfC5.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/DsnmJJEf.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/nodes/0.CmHT0Edy.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/C7Ky3Ped.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/nodes/7.CnCvnb8O.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/D2YP2_0L.js" rel="modulepreload"> | |
| <!--gxcprv--><meta name="hf:doc:metadata" content="{"title":"Attention backends","local":"attention-backends","sections":[{"title":"Set a backend on the model","local":"set-a-backend-on-the-model","sections":[],"depth":2},{"title":"Try a backend temporarily","local":"try-a-backend-temporarily","sections":[],"depth":2},{"title":"Trusting remote kernels","local":"trusting-remote-kernels","sections":[],"depth":2},{"title":"Checks","local":"checks","sections":[],"depth":2},{"title":"Available backends","local":"available-backends","sections":[],"depth":2}],"depth":1}"/><!----> | |
| <link href="/docs/diffusers/main/en/_app/immutable/assets/0.tn0RQdqM.css" rel="modulepreload"> <!--[--><!--[0--><!--[--><!--[0--><!--[--><p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: inline-end; margin-inline-start: 10px; display: inline-flex; position: relative; z-index: 10;"><div dir="ltr" class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----></span> <span dir="auto">Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg><!----></button></div> <!--[-1--><!--]--></div><!----> <!--[0--><h1 class="relative group"><a id="attention-backends" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#attention-backends"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->واجهات الخلفية للانتباه<!----></span></h1><!--]--><!----> <blockquote class="note"><p>موزّع الانتباه هو ميزة تجريبية. يرجى فتح مشكلة إذا كان لديك أي ملاحظات أو واجهت أي مشكلات.</p></blockquote> <p>توجّه معظم نماذج التحوّل في Diffusers الانتباه عبر <em>موزّع الانتباه</em> لتتمكن من تبديل الخلفيات المحسّنة خلف واجهة برمجة واحدة. يدير الموزّع التطبيقات المسجّلة ويعرض مسار استدعاء موحّد لها. بعض النماذج، مثل العديد من الترميز-فك الترميز التلقائي، لا تستخدم الموزّع بسبب بنيتها الداخلية، وبالتالي فإن تبديل الخلفية لا يؤثر على طبقات الانتباه فيها.</p> <p>راجع الجدول أدناه للحصول على نظرة عامة على عائلات الانتباه المتاحة، وقسم <a href="#available-backends">الواجهات الخلفية المتاحة</a> للحصول على قائمة أكثر اكتمالاً. يعتمد أسرع واجهة خلفية على النموذج، وبطاقة الرسوميات، ونوع البيانات، وشكل الإدخال.</p> <table><thead><tr><th>عائلة الانتباه</th><th>الميزة الرئيسية</th></tr></thead><tbody><tr><td>FlashAttention</td><td>يقلل عمليات القراءة/الكتابة للذاكرة من خلال التقطيع وإعادة الحساب</td></tr><tr><td>محرك AI Tensor لـ ROCm</td><td>تنفيذ FlashAttention مُحسّن لتسريعات AMD ROCm</td></tr><tr><td>SageAttention</td><td>يكمّم الانتباه إلى int8</td></tr><tr><td>FlexAttention</td><td>PyTorch FlexAttention</td></tr><tr><td>أصلي في PyTorch</td><td>التنفيذ المدمج في PyTorch باستخدام <a href="./fp16#scaled-dot-product-attention">scaled_dot_product_attention</a></td></tr><tr><td>xFormers</td><td>انتباه موفّر للذاكرة مع دعم لأنواع مختلفة من نواة الانتباه</td></tr></tbody></table> <p>تتطلب واجهات Hub الخلفية (الأسماء <code>*_hub</code>) مكتبة <a href="https://github.com/huggingface/kernels" rel="nofollow">Kernels</a> وتنزّل النواة عند الاستخدام الأول. كما تتطلب واجهات Hub الخلفية الخاصة بـ SageAttention ضبط <code>DIFFUSERS_TRUST_REMOTE_KERNELS=true</code> (انظر <a href="#trusting-remote-kernels">Trusting remote kernels</a>). أما الواجهات الخلفية الأخرى فتتطلب حزمها الخاصة، مثل <code>flash-attn</code> أو <code>sageattention</code>. ويحدد جدول <a href="#available-backends">Available backends</a> المتطلبات التي يتحقق منها Diffusers عند تفعيل واجهة خلفية.</p> <!--[1--><h2 class="relative group"><a id="set-a-backend-on-the-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#set-a-backend-on-the-model"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->تعيين واجهة خلفية على النموذج<!----></span></h2><!--]--><!----> <p>يتجوّل أسلوب <a href="/docs/diffusers/main/ar/api/models/overview#diffusers.ModelMixin.set_attention_backend">set_attention_backend()</a> في طبقات الانتباه في النموذج ويطبّق الخلفية المختارة على كل طبقة. كما يضبط الخلفية النشطة على مستوى العملية في الموزّع على القيمة نفسها.</p> <p>يمسح <a href="/docs/diffusers/main/ar/api/models/overview#diffusers.ModelMixin.reset_attention_backend">reset_attention_backend()</a> الخلفية (backend) على طبقات الانتباه فقط. لا يمسح الخلفية النشطة على مستوى العملية بأكملها. للتبديل المؤقت الذي يعيد الخلفية النشطة السابقة عند الخروج، استخدم مدير السياق <a href="#try-a-backend-temporarily">attention_backend</a>.</p> <p>يتيح المثال أدناه تفعيل <code>_flash_3_hub</code> (FlashAttention-3 من Hub) باستخدام <code>device_map="cuda"</code>.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> تم النسخ</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> torch | |
| <span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> QwenImagePipeline | |
| pipeline = QwenImagePipeline.from_pretrained( | |
| <span class="hljs-string">"Qwen/Qwen-Image"</span>, dtype=torch.bfloat16, device_map=<span class="hljs-string">"cuda"</span> | |
| ) | |
| pipeline.transformer.set_attention_backend(<span class="hljs-string">"_flash_3_hub"</span>) | |
| prompt = <span class="hljs-string">""" | |
| cinematic film still of a cat sipping a margarita in a pool in Palm Springs, California | |
| highly detailed, high budget hollywood movie, cinemascope, moody, epic, gorgeous, film grain | |
| """</span> | |
| pipeline(prompt).images[<span class="hljs-number">0</span>]<!----></pre></div><!----> <blockquote class="note"><p>تتطلب واجهات FlashAttention-3 غير المرتبطة بالمستودع (<code>_flash_3</code>، <code>_flash_varlen_3</code>) بناء FlashAttention-3 من المصدر، وسيتم إيقاف استخدامها قريباً. استخدم <code>_flash_3_hub</code> أو <code>_flash_3_varlen_hub</code> بدلاً من ذلك.</p></blockquote> <!--[1--><h2 class="relative group"><a id="try-a-backend-temporarily" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#try-a-backend-temporarily"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->جرّب واجهة خلفية مؤقتًا<!----></span></h2><!--]--><!----> <p>يُعيّن مُدير السياق <code>attention_backend()</code> الخلفية النشطة على مستوى العملية طوال مدة الكتلة، ويعيد الخلفية السابقة عند الخروج من الكتلة. استخدمه لتجربة خلفية في استدعاء واحد دون ترك خلفية مطبقة بشكل دائم من <a href="/docs/diffusers/main/ar/api/models/overview#diffusers.ModelMixin.set_attention_backend">set_attention_backend()</a>.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> تم النسخ</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> torch | |
| <span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> QwenImagePipeline, attention_backend | |
| pipeline = QwenImagePipeline.from_pretrained( | |
| <span class="hljs-string">"Qwen/Qwen-Image"</span>, dtype=torch.bfloat16, device_map=<span class="hljs-string">"cuda"</span> | |
| ) | |
| prompt = <span class="hljs-string">""" | |
| cinematic film still of a cat sipping a margarita in a pool in Palm Springs, California | |
| highly detailed, high budget hollywood movie, cinemascope, moody, epic, gorgeous, film grain | |
| """</span> | |
| <span class="hljs-keyword">with</span> attention_backend(<span class="hljs-string">"_flash_3_hub"</span>): | |
| image = pipeline(prompt).images[<span class="hljs-number">0</span>]<!----></pre></div><!----> <blockquote class="tip"><p>يعمل معظم محركات الانتباه الخلفية مع <code>torch.compile</code>. يعتمد ما إذا كان ذلك يسرّع خط أنابيبك على النموذج والمحرك الخلفي. راجع <a href="./fp16#torchcompile">الدقة والتجميع</a>.</p></blockquote> <!--[1--><h2 class="relative group"><a id="trusting-remote-kernels" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#trusting-remote-kernels"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->الثقة في النوى عن بُعد<!----></span></h2><!--]--><!----> <p>تحتاج واجهات Hub الخلفية إلى مكتبة <a href="https://github.com/huggingface/kernels" rel="nofollow">Kernels</a> أولاً، ثم تقوم Diffusers بجلب نواة Hub عند الاستخدام الأول.</p> <p>تنزّل واجهات الخلفية الخاصة بانتباه Hub نوى الحساب باستخدام مكتبة <a href="https://github.com/huggingface/kernels" rel="nofollow">Kernels</a> وتنفّذها محليًا. تُحلّ معظم أسماء انتباه Hub (<code>_flash_3_hub</code>، و<code>flash_hub</code>، وواجهات الخلفية الأخرى <code>*_hub</code>) إلى منظمة <a href="https://huggingface.co/kernels-community" rel="nofollow">kernels-community</a>. هذه المنظمة ناشر موثوق في Kernels، لذا يحمّل Diffusers تلك نوى الانتباه دون ضبط <code>DIFFUSERS_TRUST_REMOTE_KERNELS</code>.</p> <blockquote class="note"><p>تُحمّل واجهات SageAttention Hub الخلفية (<code>sage_hub</code> و<code>sage_blackwell_hub</code>) من منظمة <a href="https://huggingface.co/SageAttention" rel="nofollow">SageAttention</a> بدلاً من ذلك. اضبط <code>DIFFUSERS_TRUST_REMOTE_KERNELS=true</code> لاستخدامها.</p></blockquote> <p>تتميز ميزات أخرى مدعومة بالنواة، مثل <a href="../quantization/gguf">GGUF</a> و<a href="../quantization/nunchaku">Nunchaku Lite</a>، بقدرتها على جلب النوى من ناشرين خارج نطاق kernels-community. تبقى هذه المسارات محظورة ما لم تقم بالموافقة عليها باستخدام <code>DIFFUSERS_TRUST_REMOTE_KERNELS</code>. عند ضبط هذا الإعداد، يقوم Diffusers بتحويل <code>trust_remote_code=True</code> إلى Kernels حتى يتمكن الناشر غير الموثوق من التحميل أيضًا.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> تم النسخ</div><!----></button><!----></div> <pre class="language-bash "><!----><span class="hljs-built_in">export</span> DIFFUSERS_TRUST_REMOTE_KERNELS=<span class="hljs-literal">true</span><!----></pre></div><!----> <p>فعّل هذا الخيار فقط بعد فحص مستودع النواة. بدونه، يؤدي تحميل نواة من ناشر غير موثوق إلى رفع خطأ. يقوم Diffusers بإجراء هذا الفحص بنفسه، لذا ينطبق الأمر أيضاً على <code>kernels<0.14.0</code>، الذي يسبق وجود الحجة <code>trust_remote_code</code>. يؤدي تعيين <code>DIFFUSERS_DISABLE_REMOTE_CODE=true</code> إلى تعطيل الكود عن بُعد على مستوى النظام بالكامل، وله الأولوية على <code>DIFFUSERS_TRUST_REMOTE_KERNELS</code>.</p> <!--[1--><h2 class="relative group"><a id="checks" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#checks"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->فحوصات<!----></span></h2><!--]--><!----> <p>يمكن لمُوزِّع الانتباه (attention dispatcher) تشغيل فحوصات تصحيح الأخطاء قبل كل استدعاء انتباه مُوزَّع. تعتمد الفحوصات التي تُشغَّل على القيود المسجَّلة للخلفية النشطة (active backend).</p> <ol><li>تحقّق فحوصات الأجهزة من أن متجهات الاستعلام والمفتاح والقيمة موجودة على نفس الجهاز.</li> <li>تتحقق فحوصات أنواع البيانات، عند تسجيلها، من تطابق أنواع البيانات (dtypes) وغالباً ما تتطلب <code>bfloat16</code> أو <code>float16</code>.</li> <li>تحقّق فحوصات الأبعاد من أبعاد المصفوفات (tensors) وتمنع خلط أقنعة الانتباه (attention masks) مع أعلام السببية (causal flags).</li></ol> <p>فعّل الفحوصات باستخدام متغير البيئة <code>DIFFUSERS_ATTN_CHECKS</code>. تُضيف الفحوصات عبئًا إضافيًا، لذا فهي معطّلة افتراضيًا.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> تم النسخ</div><!----></button><!----></div> <pre class="language-bash "><!----><span class="hljs-built_in">export</span> DIFFUSERS_ATTN_CHECKS=<span class="hljs-built_in">yes</span><!----></pre></div><!----> <p>عند تفعيل الفحوصات، ينفّذ Diffusers تلك القيود قبل كل استدعاء انتباه مُوجَّه. يستدعي المثال منخفض المستوى أدناه <code>dispatch_attention_fn</code> مباشرةً. لا يحتاج الاستدلال عبر خط الأنابيب إلى ذلك الاستيراد. ما يحتاجه فقط هو ضبط الخلفية عبر <a href="/docs/diffusers/main/ar/api/models/overview#diffusers.ModelMixin.set_attention_backend">set_attention_backend()</a> أو <code>attention_backend()</code>.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> تم النسخ</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> torch | |
| <span class="hljs-keyword">from</span> diffusers.models.attention_dispatch <span class="hljs-keyword">import</span> attention_backend, dispatch_attention_fn | |
| query = torch.randn(<span class="hljs-number">1</span>, <span class="hljs-number">10</span>, <span class="hljs-number">8</span>, <span class="hljs-number">64</span>, dtype=torch.bfloat16, device=<span class="hljs-string">"cuda"</span>) | |
| key = torch.randn(<span class="hljs-number">1</span>, <span class="hljs-number">10</span>, <span class="hljs-number">8</span>, <span class="hljs-number">64</span>, dtype=torch.bfloat16, device=<span class="hljs-string">"cuda"</span>) | |
| value = torch.randn(<span class="hljs-number">1</span>, <span class="hljs-number">10</span>, <span class="hljs-number">8</span>, <span class="hljs-number">64</span>, dtype=torch.bfloat16, device=<span class="hljs-string">"cuda"</span>) | |
| <span class="hljs-keyword">try</span>: | |
| <span class="hljs-keyword">with</span> attention_backend(<span class="hljs-string">"flash"</span>): | |
| output = dispatch_attention_fn(query, key, value) | |
| <span class="hljs-built_in">print</span>(<span class="hljs-string">"✓ Flash Attention works with checks enabled"</span>) | |
| <span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e: | |
| <span class="hljs-built_in">print</span>(<span class="hljs-string">f"✗ Flash Attention failed: <span class="hljs-subst">{e}</span>"</span>)<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="available-backends" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#available-backends"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->الواجهات الخلفية المتاحة<!----></span></h2><!--]--><!----> <p>راجع الجدول أدناه للحصول على قائمة كاملة بالخوادم الخلفية المتاحة لانتباه (attention) ومتغيراتها. يتحقق Diffusers من توفر الحزم وتثبيت الإصدارات عند تفعيل خادم خلفي.</p> <table><thead><tr><th>اسم الخلفية</th><th>العائلة</th><th>الوصف</th><th>المتطلبات</th></tr></thead><tbody><tr><td><code>native</code></td><td><a href="https://docs.pytorch.org/docs/stable/generated/torch.nn.attention.SDPBackend.html#torch.nn.attention.SDPBackend" rel="nofollow">PyTorch الأصلي</a></td><td>الخلفية الافتراضية باستخدام scaled_dot_product_attention من PyTorch</td><td>لا شيء</td></tr><tr><td><code>flex</code></td><td><a href="https://docs.pytorch.org/docs/stable/nn.attention.flex_attention.html#module-torch.nn.attention.flex_attention" rel="nofollow">FlexAttention</a></td><td>PyTorch FlexAttention</td><td><code>torch>=2.5.0</code></td></tr><tr><td><code>_native_cudnn</code></td><td><a href="https://docs.pytorch.org/docs/stable/generated/torch.nn.attention.SDPBackend.html#torch.nn.attention.SDPBackend" rel="nofollow">PyTorch الأصلي</a></td><td>الانتباه المُحسَّن بواسطة CuDNN</td><td>CUDA + CuDNN</td></tr><tr><td><code>_native_efficient</code></td><td><a href="https://docs.pytorch.org/docs/stable/generated/torch.nn.attention.SDPBackend.html#torch.nn.attention.SDPBackend" rel="nofollow">PyTorch الأصلي</a></td><td>انتباه موفّر للذاكرة</td><td>لا شيء سوى PyTorch</td></tr><tr><td><code>_native_flash</code></td><td><a href="https://docs.pytorch.org/docs/stable/generated/torch.nn.attention.SDPBackend.html#torch.nn.attention.SDPBackend" rel="nofollow">PyTorch الأصلي</a></td><td>FlashAttention في PyTorch</td><td>CUDA</td></tr><tr><td><code>_native_math</code></td><td><a href="https://docs.pytorch.org/docs/stable/generated/torch.nn.attention.SDPBackend.html#torch.nn.attention.SDPBackend" rel="nofollow">PyTorch الأصلي</a></td><td>انتباه قائم على الرياضيات (بديل احتياطي)</td><td>لا شيء</td></tr><tr><td><code>_native_npu</code></td><td><a href="https://docs.pytorch.org/docs/stable/generated/torch.nn.attention.SDPBackend.html#torch.nn.attention.SDPBackend" rel="nofollow">PyTorch الأصلي</a></td><td>انتباه مُحسَّن لوحدة المعالجة العصبية</td><td><code>torch_npu</code></td></tr><tr><td><code>_native_xla</code></td><td><a href="https://docs.pytorch.org/docs/stable/generated/torch.nn.attention.SDPBackend.html#torch.nn.attention.SDPBackend" rel="nofollow">PyTorch الأصلي</a></td><td>انتباه مُحسَّن بواسطة XLA</td><td><code>torch_xla>=2.2</code></td></tr><tr><td><code>flash</code></td><td><a href="https://github.com/Dao-AILab/flash-attention" rel="nofollow">FlashAttention</a></td><td>FlashAttention-2</td><td><code>flash-attn>=2.6.3</code></td></tr><tr><td><code>flash_hub</code></td><td><a href="https://github.com/Dao-AILab/flash-attention" rel="nofollow">FlashAttention</a></td><td>FlashAttention-2 من نواة Hub</td><td><code>kernels>=0.12</code></td></tr><tr><td><code>flash_varlen</code></td><td><a href="https://github.com/Dao-AILab/flash-attention" rel="nofollow">FlashAttention</a></td><td>FlashAttention بطول متغير</td><td><code>flash-attn>=2.6.3</code></td></tr><tr><td><code>flash_varlen_hub</code></td><td><a href="https://github.com/Dao-AILab/flash-attention" rel="nofollow">FlashAttention</a></td><td>FlashAttention بطول متغير من نواة Hub</td><td><code>kernels>=0.12</code></td></tr><tr><td><code>aiter_fa2_hub</code></td><td><a href="https://github.com/ROCm/aiter" rel="nofollow">محرك AI Tensor لـ ROCm</a></td><td>FlashAttention-2 لـ AMD ROCm من نواة Hub (<code>bfloat16</code>)</td><td><code>kernels>=0.12</code>، ROCm</td></tr><tr><td><code>flash_4_hub</code></td><td><a href="https://github.com/Dao-AILab/flash-attention" rel="nofollow">FlashAttention</a></td><td>FlashAttention-4 من نواة Hub</td><td><code>kernels>=0.12.3</code></td></tr><tr><td><code>_flash_3</code></td><td><a href="https://github.com/Dao-AILab/flash-attention" rel="nofollow">FlashAttention</a></td><td>FlashAttention-3 (محلي؛ سيُتوقف عن استخدامه قريباً)</td><td>بناء FA3 من المصدر</td></tr><tr><td><code>_flash_varlen_3</code></td><td><a href="https://github.com/Dao-AILab/flash-attention" rel="nofollow">FlashAttention</a></td><td>FlashAttention-3 بطول متغير (محلي؛ سيُتخلى عنه قريباً)</td><td>بناء FA3 من المصدر</td></tr><tr><td><code>_flash_3_hub</code></td><td><a href="https://github.com/Dao-AILab/flash-attention" rel="nofollow">FlashAttention</a></td><td>FlashAttention-3 من نواة Hub</td><td><code>kernels>=0.12</code></td></tr><tr><td><code>_flash_3_varlen_hub</code></td><td><a href="https://github.com/Dao-AILab/flash-attention" rel="nofollow">FlashAttention</a></td><td>FlashAttention-3 بطول متغير من نواة Hub</td><td><code>kernels>=0.12</code></td></tr><tr><td><code>sage</code></td><td><a href="https://github.com/thu-ml/SageAttention" rel="nofollow">SageAttention</a></td><td>الانتباه المُكمَّم (INT8 QK)</td><td><code>sageattention>=2.1.1</code></td></tr><tr><td><code>sage_hub</code></td><td><a href="https://github.com/thu-ml/SageAttention" rel="nofollow">SageAttention</a></td><td>الانتباه المُكمَّم (INT8 QK) من نواة Hub</td><td><code>kernels>=0.12</code>, <code>DIFFUSERS_TRUST_REMOTE_KERNELS=true</code></td></tr><tr><td><code>sage_blackwell_hub</code></td><td><a href="https://github.com/thu-ml/SageAttention" rel="nofollow">SageAttention</a></td><td>انتباه SageAttention3 FP4 لشرائح SM120 Blackwell من نواة Hub</td><td><code>kernels>=0.12</code>, <code>DIFFUSERS_TRUST_REMOTE_KERNELS=true</code></td></tr><tr><td><code>sage_varlen</code></td><td><a href="https://github.com/thu-ml/SageAttention" rel="nofollow">SageAttention</a></td><td>SageAttention بطول متغير</td><td><code>sageattention>=2.1.1</code></td></tr><tr><td><code>_sage_qk_int8_pv_fp8_cuda</code></td><td><a href="https://github.com/thu-ml/SageAttention" rel="nofollow">SageAttention</a></td><td>INT8 QK + FP8 PV (CUDA)</td><td><code>sageattention>=2.1.1</code></td></tr><tr><td><code>_sage_qk_int8_pv_fp8_cuda_sm90</code></td><td><a href="https://github.com/thu-ml/SageAttention" rel="nofollow">SageAttention</a></td><td>INT8 QK + FP8 PV (SM90)</td><td><code>sageattention>=2.1.1</code>؛ SM90</td></tr><tr><td><code>_sage_qk_int8_pv_fp16_cuda</code></td><td><a href="https://github.com/thu-ml/SageAttention" rel="nofollow">SageAttention</a></td><td>INT8 QK + FP16 PV (CUDA)</td><td><code>sageattention>=2.1.1</code></td></tr><tr><td><code>_sage_qk_int8_pv_fp16_triton</code></td><td><a href="https://github.com/thu-ml/SageAttention" rel="nofollow">SageAttention</a></td><td>INT8 QK + FP16 PV (Triton)</td><td><code>sageattention>=2.1.1</code></td></tr><tr><td><code>xformers</code></td><td><a href="https://github.com/facebookresearch/xformers" rel="nofollow">xFormers</a></td><td>انتباه موفّر للذاكرة</td><td><code>xformers>=0.0.29</code></td></tr></tbody></table> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/diffusers/blob/main/docs/source/en/optimization/attention_backends.md" target="_blank"><svg class="mr-1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M31,16l-7,7l-1.41-1.41L28.17,16l-5.58-5.59L24,9l7,7z"></path><path d="M1,16l7-7l1.41,1.41L3.83,16l5.58,5.59L8,23l-7-7z"></path><path d="M12.419,25.484L17.639,6.552l1.932,0.518L14.351,26.002z"></path></svg><!----> <span><span class="underline">Update</span> on GitHub</span></a><!----> <p></p><!--]--><!----><!--]--><!--]--><!--]--> <!--[-1--><!--]--><!--]--> | |
| <script> | |
| { | |
| __sveltekit_kft2r9 = { | |
| base: "/docs/diffusers/main/en", | |
| assets: "/docs/diffusers/main/en" | |
| }; | |
| const element = document.currentScript.parentElement; | |
| Promise.all([ | |
| import("/docs/diffusers/main/en/_app/immutable/entry/start.B6roO9Zr.js"), | |
| import("/docs/diffusers/main/en/_app/immutable/entry/app.CxxEVG8A.js") | |
| ]).then(([kit, app]) => { | |
| kit.start(app, element, { | |
| node_ids: [0, 7], | |
| data: [null,null], | |
| form: null, | |
| error: null | |
| }); | |
| }); | |
| } | |
| </script> | |
Xet Storage Details
- Size:
- 39.8 kB
- Xet hash:
- ec3cb75814f6259b20413cfe91cc5f93d8bd97b56e08d1e9bbcc873640273561
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.