Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"التخزين المؤقت","local":"caching","sections":[{"title":"اختر طريقة تخزين مؤقت","local":"choose-a-cache-method","sections":[],"depth":2},{"title":"بث الانتباه الهرمي","local":"pyramid-attention-broadcast","sections":[],"depth":2},{"title":"FasterCache","local":"fastercache","sections":[],"depth":2},{"title":"SeaCache","local":"seacache","sections":[],"depth":2},{"title":"FirstBlockCache","local":"firstblockcache","sections":[],"depth":2},{"title":"TaylorSeer ذاكرة مؤقتة","local":"taylorseer-cache","sections":[],"depth":2},{"title":"MagCache","local":"magcache","sections":[],"depth":2},{"title":"ذاكرة التخزين المؤقت لنص KV","local":"text-kv-cache","sections":[],"depth":2},{"title":"إضافة التخزين المؤقت إلى نموذج جديد","local":"add-caching-to-a-new-model","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/8.DhsC3tNN.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/D2YP2_0L.js" rel="modulepreload"> | |
| <!--fs68f1--><meta name="hf:doc:metadata" content="{"title":"Caching","local":"caching","sections":[{"title":"Choose a cache method","local":"choose-a-cache-method","sections":[],"depth":2},{"title":"Pyramid Attention Broadcast","local":"pyramid-attention-broadcast","sections":[],"depth":2},{"title":"FasterCache","local":"fastercache","sections":[],"depth":2},{"title":"SeaCache","local":"seacache","sections":[],"depth":2},{"title":"FirstBlockCache","local":"firstblockcache","sections":[],"depth":2},{"title":"TaylorSeer Cache","local":"taylorseer-cache","sections":[],"depth":2},{"title":"MagCache","local":"magcache","sections":[],"depth":2},{"title":"Text KV Cache","local":"text-kv-cache","sections":[],"depth":2},{"title":"Add caching to a new model","local":"add-caching-to-a-new-model","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="caching" 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="#caching"><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><!--]--><!----> <p>يعمل التخزين المؤقت على إعادة استخدام مخرجات الطبقات الوسيطة عبر خطوات إزالة الضوضاء لتسريع الاستدلال. يستهلك هذا الأسلوب ذاكرة أكبر ولا يتطلب تدريبًا. فعّل طريقة ما عبر إعدادات على محوّل يدعم التخزين المؤقت.</p> <!--[1--><h2 class="relative group"><a id="choose-a-cache-method" 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="#choose-a-cache-method"><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>اختر طريقةً بناءً على مقدار الإعدادات التي ستضبطها، وعلى مدى الملاءمة التي تحتاجها. كل طريقة توازن بين السرعة والذاكرة ومدى تطابق المخرجات مع التشغيل غير المخزّن بطريقة مختلفة. قارن المخرجات مع وجود التخزين المؤقت وبدونه على أوامرك الخاصة قبل الاعتماد على أي طريقة.</p> <table><thead><tr><th>الطريقة</th><th>استخدم عند</th><th>ملاحظات</th></tr></thead><tbody><tr><td><a href="#text-kv-cache">ذاكرة KV النصية</a></td><td>صورة NucleusMoE، إعادة استخدام إسقاطات K/V للنص عبر الخطوات</td><td>حاليًا، يربط كتل صور NucleusMoE</td></tr><tr><td><a href="#seacache">SeaCache</a></td><td>توليد الفيديو في Cosmos 3</td><td>غالبًا لا تنتقل الإعدادات عبر النماذج</td></tr><tr><td><a href="#firstblockcache">FirstBlockCache</a></td><td>هل تريد وجود مقبض رئيسي واحد للتحكم في السرعة/الجودة في نموذج تحويلي مسجّل</td><td>يتحكم <code>threshold</code> واحد في تكرار تخطي الخطوات</td></tr><tr><td><a href="#magcache">MagCache</a></td><td>هل لديك نسب السعة لنقطة الحفظ وجدولك الزمني، أم ستقوم بالمعايرة أولاً</td><td>تختلف النسب باختلاف نقطة الحفظ ومجدول التعلم</td></tr><tr><td><a href="#taylorseer-cache">TaylorSeer</a></td><td>هل تريد التنبؤ بتنشيطات لاحقة من خطوات سابقة</td><td>استخدام قيمة أعلى لـ <code>max_order</code> يستهلك ذاكرة أكبر</td></tr><tr><td><a href="#pyramid-attention-broadcast">PAB</a></td><td>فيديو، على استعداد لضبط إعادة استخدام الانتباه (نطاقات تخطي الكتلة والخطوة الزمنية لكل نوع من أنواع الانتباه)</td><td>تتطلب نطاقات التخطي ضبطاً لكل نموذج</td></tr><tr><td><a href="#fastercache">FasterCache</a></td><td>مثل PAB، مع خيار إضافي لتخطي فرع CFG</td><td>تجريبي</td></tr></tbody></table> <!--[1--><h2 class="relative group"><a id="pyramid-attention-broadcast" 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="#pyramid-attention-broadcast"><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="https://huggingface.co/papers/2408.12588" rel="nofollow">Pyramid Attention Broadcast (PAB)</a> بتقريب الانتباه عبر خطوات إزالة الضوضاء من خلال إعادة استخدام مخرجات الانتباه لبعض الكتل وخطوات الزمن بدلاً من إعادة الحساب في كل خطوة. يميّز الإعداد بين أنواع الانتباه (المكانية، الزمنية، المتقاطعة) عندما تتوفر في النموذج. لا يكشف كل نموذج فيديو عن الأنواع الثلاثة، لذا اضبط فقط النطاقات التي تتطابق مع الكتل المتوفرة لديك.</p> <p>يستخدم كل نوع <code>*_attention_block_skip_range</code> (تكرار إعادة الحساب مقابل إعادة الاستخدام داخل النافذة) و<code>*_attention_timestep_skip_range</code> (خطوات إزالة الضوضاء التي يمكن تجاوزها). يجب عليك تمرير <code>current_timestep_callback</code> حتى يتمكن الخطاف من قراءة الخطوة الزمنية الحالية للخط. عادةً ما تعني التجاوزات الأوسع أو الأكثر عدوانية مزيدًا من السرعة ومزيدًا من مخاطر الجودة.</p> <p>مرّر <a href="/docs/diffusers/main/ar/api/cache#diffusers.PyramidAttentionBroadcastConfig">PyramidAttentionBroadcastConfig</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-python "><!----><span class="hljs-keyword">import</span> torch | |
| <span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> CogVideoXPipeline, PyramidAttentionBroadcastConfig | |
| pipe = CogVideoXPipeline.from_pretrained(<span class="hljs-string">"THUDM/CogVideoX-5b"</span>, dtype=torch.bfloat16) | |
| pipe.to(<span class="hljs-string">"cuda"</span>) <span class="hljs-comment"># or "mps", "xpu", "cpu"</span> | |
| config = PyramidAttentionBroadcastConfig( | |
| spatial_attention_block_skip_range=<span class="hljs-number">2</span>, | |
| spatial_attention_timestep_skip_range=(<span class="hljs-number">100</span>, <span class="hljs-number">800</span>), | |
| current_timestep_callback=<span class="hljs-keyword">lambda</span>: pipe.current_timestep, | |
| ) | |
| pipe.transformer.enable_cache(config)<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="fastercache" 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="#fastercache"><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><!---->FasterCache<!----></span></h2><!--]--><!----> <p>يخزّن <a href="https://huggingface.co/papers/2410.19355" rel="nofollow">FasterCache</a> ميزات الانتباه ويعيد استخدامها بطريقة مشابهة لـ <a href="#pyramid-attention-broadcast">PAB</a>. كما يمكنه تجاوز الفرع غير الشرطي في التوجيه الحر من التصنيف وتقديره من الفرع الشرطي عندما تكون الكمونيات المتتالية متكررة بما يكفي.</p> <p>مرّر <a href="/docs/diffusers/main/ar/api/cache#diffusers.FasterCacheConfig">FasterCacheConfig</a> لتفعيله. وبالمثل مع PAB، اضبط <code>*_attention_block_skip_range</code> و<code>*_attention_timestep_skip_range</code> لأنواع الانتباه التي لديك، بالإضافة إلى خيارات تخطي فرع CFG عندما ترغب في ذلك.</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-python "><!----><span class="hljs-keyword">import</span> torch | |
| <span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> CogVideoXPipeline, FasterCacheConfig | |
| pipe = CogVideoXPipeline.from_pretrained(<span class="hljs-string">"THUDM/CogVideoX-5b"</span>, dtype=torch.bfloat16) | |
| pipe.to(<span class="hljs-string">"cuda"</span>) <span class="hljs-comment"># or "mps", "xpu", "cpu"</span> | |
| config = FasterCacheConfig( | |
| spatial_attention_block_skip_range=<span class="hljs-number">2</span>, | |
| spatial_attention_timestep_skip_range=(-<span class="hljs-number">1</span>, <span class="hljs-number">681</span>), | |
| current_timestep_callback=<span class="hljs-keyword">lambda</span>: pipe.current_timestep, | |
| attention_weight_callback=<span class="hljs-keyword">lambda</span> _: <span class="hljs-number">0.3</span>, | |
| unconditional_batch_skip_range=<span class="hljs-number">5</span>, | |
| unconditional_batch_timestep_skip_range=(-<span class="hljs-number">1</span>, <span class="hljs-number">781</span>), | |
| tensor_format=<span class="hljs-string">"BFCHW"</span>, | |
| ) | |
| pipe.transformer.enable_cache(config)<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="seacache" 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="#seacache"><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><!---->SeaCache<!----></span></h2><!--]--><!----> <p>يقارن <a href="https://huggingface.co/papers/2602.18993" rel="nofollow">SeaCache</a> مؤشرات التطور الطيفي الواعي (SEA) بين خطوات إزالة الضوضاء المتتالية. عندما يبقى التغيير المتراكم دون حد معين، يتخطى مجموعة كتل التحويلي ويتنبأ بالمخرجات من بقايا مخزنة مسبقًا. الطريقة تقريبية ومصممة لتوليد الفيديو.</p> <p>تشمل المكيّفات المدمجة في SeaCache ما يلي:</p> <ul><li>Cosmos 3 هو التكامل الأساسي المُحسَّن والمُقيَّم.</li> <li>يستخدم Wan T2V مسار كتل التكرار العام كنموذج توضيحي لكيفية توفير كامنات الرؤية الخام إلى SeaCache. قد لا تنطبق نفس معاملات التخزين المؤقت على Wan أو غيرها من متغيرات Wan.</li></ul> <p>فعّل SeaCache على المحوّل. يربط حلقة إزالة الضوضاء في Cosmos 3 خطوة المجدول، وقيمة sigma، وعدد الخطوات إلى كل <code>cache_context</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-python "><!----><span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> Cosmos3OmniPipeline, SeaCacheConfig | |
| pipe = Cosmos3OmniPipeline.from_pretrained(<span class="hljs-string">"nvidia/Cosmos3-Nano"</span>) | |
| pipe.transformer.enable_cache(SeaCacheConfig(threshold=<span class="hljs-number">0.2</span>, max_consecutive_cached=<span class="hljs-number">2</span>))<!----></pre></div><!----> <p>قد تُغيّر SeaCache المخرجات. استدعِ <code>pipe.transformer.disable_cache()</code> عندما تحتاج إلى تشغيل كل خطوة عبر المحوّل الكامل. يعمل نفس استدعاء التفعيل مع <a href="/docs/diffusers/main/ar/api/pipelines/cosmos3#diffusers.Cosmos3OmniPipeline">Cosmos3OmniPipeline</a> و<a href="/docs/diffusers/main/ar/api/pipelines/cosmos3#diffusers.Cosmos3OmniModularPipeline">Cosmos3OmniModularPipeline</a> و<a href="/docs/diffusers/main/ar/api/pipelines/cosmos3#diffusers.Cosmos3DistilledModularPipeline">Cosmos3DistilledModularPipeline</a>.</p> <p>لاستخدام SeaCache مع محوّل فيديو آخر، اتبع <a href="#add-caching-to-a-new-model">إضافة التخزين المؤقت إلى نموذج جديد</a>. كما يحتاج SeaCache إلى <code>step_index</code> و<code>sigma</code> و<code>num_inference_steps</code> في كل <code>cache_context</code>، وإلى <code>raw_vision_callback</code> عندما لا يتوفر مُكيّف مدمج. اضبط المعلمات لكل نموذج ومجدول.</p> <!--[1--><h2 class="relative group"><a id="firstblockcache" 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="#firstblockcache"><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><!---->FirstBlockCache<!----></span></h2><!--]--><!----> <p>يتحقق <a href="/docs/diffusers/main/ar/api/cache#diffusers.FirstBlockCacheConfig">FirstBlockCacheConfig</a> من مقدار تغيّر الطبقات المبكرة في مُزيل الضوضاء من خطوة زمنية إلى التالية. إذا كان التغير صغيراً، يتخطّى النموذج الطبقات اللاحقة المكلفة ويعيد استخدام المخرجات السابقة.</p> <p>فعّله عبر <code>enable_cache</code> حتى تبقى <code>disable_cache</code> و<code>is_cache_enabled</code> متزامنة. القيمة الافتراضية لـ<code>threshold</code> هي <code>0.05</code>. القيمة الأعلى مثل <code>0.2</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-python "><!----><span class="hljs-keyword">import</span> torch | |
| <span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline, FirstBlockCacheConfig | |
| pipe = DiffusionPipeline.from_pretrained( | |
| <span class="hljs-string">"Qwen/Qwen-Image"</span>, dtype=torch.bfloat16 | |
| ) | |
| pipe.transformer.enable_cache(FirstBlockCacheConfig(threshold=<span class="hljs-number">0.2</span>))<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="taylorseer-cache" 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="#taylorseer-cache"><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><!---->TaylorSeer ذاكرة مؤقتة<!----></span></h2><!--]--><!----> <p>يعمل <a href="https://huggingface.co/papers/2503.06923" rel="nofollow">TaylorSeer Cache</a> على تسريع استنتاج النماذج الانتشارية (Diffusion) باستخدام توسعات متسلسلة تايلور عبر خطوات إزالة الضوضاء. فهو يتنبأ بتفعيلات الخطوات اللاحقة بناءً على تفعيلات الخطوات السابقة، ويعيد استخدام تلك التنبؤات لعدة خطوات، مما يقلل من حجم العمل الكامل الذي يقوم به نموذج التحويل (Transformer).</p> <ul><li><code>cache_interval</code>: عدد الخطوات لإعادة استخدام المخرجات المخزنة مؤقتًا قبل إجراء تمرير أمامي كامل</li> <li><code>disable_cache_before_step</code>: الخطوات الأولية التي تستخدم الحسابات الكاملة لجمع البيانات للتقديرات</li> <li><code>max_order</code>: يمكن أن تكون رتب تايلور الأعلى أكثر دقة لكنها تستهلك ذاكرة أكبر. حافظ على هذا عند <code>1</code> ما لم يكن لديك سبب لتغييره.</li></ul> <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-python "><!----><span class="hljs-keyword">import</span> torch | |
| <span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> FluxPipeline, TaylorSeerCacheConfig | |
| pipe = FluxPipeline.from_pretrained( | |
| <span class="hljs-string">"black-forest-labs/FLUX.1-dev"</span>, | |
| dtype=torch.bfloat16, | |
| ).to(<span class="hljs-string">"cuda"</span>) <span class="hljs-comment"># or "mps", "xpu", "cpu"</span> | |
| config = TaylorSeerCacheConfig( | |
| cache_interval=<span class="hljs-number">5</span>, | |
| max_order=<span class="hljs-number">1</span>, | |
| disable_cache_before_step=<span class="hljs-number">10</span>, | |
| taylor_factors_dtype=torch.bfloat16, | |
| ) | |
| pipe.transformer.enable_cache(config)<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="magcache" 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="#magcache"><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><!---->MagCache<!----></span></h2><!--]--><!----> <p>يتخطى <a href="https://github.com/Zehong-Ma/MagCache" rel="nofollow">MagCache</a> كتل التحويل بناءً على مقدار التحديث المتبقي. تتناقص مقادير التحديث بشكل متوقع عبر عملية إزالة الضوضاء، ويتتبع MagCache ميزانية خطأ من نسب المقادير المحسوبة مسبقًا (<code>mag_ratios</code>) لتحديد متى يكون إعادة الاستخدام آمنة. هذه النسب مرتبطة بنقطة الحفظ ومجدول التدرج المحدد. يمكن استيفاء النسب المستمدة من عدد خطوات عالٍ لتقليل عدد الخطوات.</p> <blockquote class="tip"><p>تم قياس <code>FLUX_MAG_RATIOS</code> المرفقة على FLUX.1. تحتاج النماذج الأخرى، بما في ذلك Flux2 وFlux2 Klein، إلى إجراء معايرة خاص بها.</p></blockquote> <p>يتبع MagCache خطوتين:</p> <ol><li>المعايرة: نفّذ الاستدلال مرة واحدة مع <code>calibrate=True</code>. يقيس الخطاف (hook) أحجام البواقي (residuals) ويُطبع النسب المحسوبة.</li> <li>الاستدلال: عطّل ذاكرة التخزين المؤقت للمعايرة، ثم مرّر تلك النسب إلى <code>MagCacheConfig</code> للتسريع.</li></ol> <p>قد يؤثر التوجيه الحر من التصنيف على المعايرة. تدخل خطوط الأنابيب التي تستخدم CFG الحقيقي مع سياقات متسلسلة، مثل Flux عندما يكون <code>true_cfg_scale > 1</code>، إلى <code>cache_context("cond")</code> و<code>cache_context("uncond")</code> بشكل منفصل. قد تطبع المعايرة مصفوفة واحدة لكل سياق، لكن عليك استخدام المصفوفة الشرطية في معظم الحالات. تنتج خطوط الأنابيب التي تجمع CFG عن طريق دمج المدخلات الشرطية وغير الشرطية (على سبيل المثال، CogVideoX) مصفوفة مشتركة واحدة يمكنك استخدامها مباشرة.</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-python "><!----><span class="hljs-keyword">import</span> torch | |
| <span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> FluxPipeline, MagCacheConfig | |
| <span class="hljs-keyword">from</span> diffusers.hooks.mag_cache <span class="hljs-keyword">import</span> FLUX_MAG_RATIOS | |
| pipe = FluxPipeline.from_pretrained( | |
| <span class="hljs-string">"black-forest-labs/FLUX.1-schnell"</span>, | |
| dtype=torch.bfloat16 | |
| ).to(<span class="hljs-string">"cuda"</span>) <span class="hljs-comment"># or "mps", "xpu", "cpu"</span> | |
| <span class="hljs-comment"># 1. Calibration Step</span> | |
| <span class="hljs-comment"># Run full inference to measure model behavior.</span> | |
| calib_config = MagCacheConfig(calibrate=<span class="hljs-literal">True</span>, num_inference_steps=<span class="hljs-number">4</span>) | |
| pipe.transformer.enable_cache(calib_config) | |
| <span class="hljs-comment"># Run a prompt to trigger calibration</span> | |
| pipe(<span class="hljs-string">"A cat playing chess"</span>, num_inference_steps=<span class="hljs-number">4</span>) | |
| <span class="hljs-comment"># Prints: [MagCache] Calibration Complete. Copy these values to MagCacheConfig(mag_ratios=...):</span> | |
| <span class="hljs-comment"># 2. Inference Step</span> | |
| <span class="hljs-comment"># Disable calibration hooks before enabling MagCache for inference.</span> | |
| pipe.transformer.disable_cache() | |
| <span class="hljs-comment"># Apply ratios from calibration, or use the Flux defaults:</span> | |
| <span class="hljs-comment"># mag_ratios=FLUX_MAG_RATIOS</span> | |
| mag_config = MagCacheConfig( | |
| mag_ratios=[<span class="hljs-number">1.0</span>, <span class="hljs-number">1.37</span>, <span class="hljs-number">0.97</span>, <span class="hljs-number">0.87</span>], | |
| num_inference_steps=<span class="hljs-number">4</span> | |
| ) | |
| pipe.transformer.enable_cache(mag_config) | |
| image = pipe(<span class="hljs-string">"A cat playing chess"</span>, num_inference_steps=<span class="hljs-number">4</span>).images[<span class="hljs-number">0</span>]<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="text-kv-cache" 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="#text-kv-cache"><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><!---->ذاكرة التخزين المؤقت لنص KV<!----></span></h2><!--]--><!----> <p>يحسب <a href="/docs/diffusers/main/ar/api/cache#diffusers.TextKVCacheConfig">TextKVCacheConfig</a> إسقاطات المفاتيح والقيم للنص مرة واحدة ويعيد استخدامها في كل خطوة إزالة ضوضاء بدلاً من إعادة حسابها. حالياً، لا يربط <a href="/docs/diffusers/main/ar/api/cache#diffusers.apply_text_kv_cache">apply_text_kv_cache()</a> سوى <code>NucleusMoEImageTransformerBlock</code>. فعّله باستخدام <code>enable_cache</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-python "><!----><span class="hljs-keyword">import</span> torch | |
| <span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> NucleusMoEImagePipeline, TextKVCacheConfig | |
| pipe = NucleusMoEImagePipeline.from_pretrained( | |
| <span class="hljs-string">"NucleusAI/NucleusMoE-Image"</span>, dtype=torch.bfloat16 | |
| ) | |
| pipe.to(<span class="hljs-string">"cuda"</span>) <span class="hljs-comment"># or "mps", "xpu", "cpu"</span> | |
| pipe.transformer.enable_cache(TextKVCacheConfig()) | |
| image = pipe(<span class="hljs-string">"A cat holding a sign that says hello world"</span>, num_inference_steps=<span class="hljs-number">50</span>).images[<span class="hljs-number">0</span>]<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="add-caching-to-a-new-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="#add-caching-to-a-new-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>تُربط طرق التخزين المؤقت (Cache) بالنموذج عبر الخطافات (hooks)، وتعتمد هذه الخطافات على فئة النموذج، ومكاتب التحويل (transformer blocks) الخاصة به، وحلقة إزالة الضوضاء (denoising loop) في خط المعالجة. قم بإعداد جميع العناصر الثلاثة الثلاثة قبل استدعاء <code>enable_cache</code> على نموذج جديد.</p> <ol><li><p>ورث من <a href="/docs/diffusers/main/ar/api/cache#diffusers.CacheMixin">CacheMixin</a> في فئة التحويل. يضيف هذا <code>enable_cache</code> و<code>disable_cache</code> و<code>cache_context</code>.</p></li> <li><p>سجّل تخطيط المخرجات للكتلة المحوّلة (transformer block) في <code>_register_transformer_blocks_metadata</code> داخل <code>hooks/_helpers.py</code>. تستخدم الطرق التي تتخطى أو تعيد استخدام كتل كاملة، مثل FirstBlockCache وMagCache وSeaCache، هذا التخطيط للعثور على الحالات المخفية (hidden states) في مخرجات الكتلة.</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 "><!---->TransformerBlockRegistry.register( | |
| model_class=MyTransformerBlock, | |
| metadata=TransformerBlockMetadata( | |
| return_hidden_states_index=<span class="hljs-number">0</span>, | |
| return_encoder_hidden_states_index=<span class="hljs-literal">None</span>, | |
| ), | |
| )<!----></pre></div><!----></li> <li><p>غلّف كل استدعاء لمحوّل داخل حلقة إزالة الضوضاء في خط المعالجة بـ<code>cache_context</code>. استخدم أسماء منفصلة، مثل <code>"cond"</code> و<code>"uncond"</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">with</span> <span class="hljs-variable language_">self</span>.transformer.cache_context(<span class="hljs-string">"cond"</span>): | |
| noise_pred = <span class="hljs-variable language_">self</span>.transformer(hidden_states=latents, timestep=timestep, ...)[<span class="hljs-number">0</span>]<!----></pre></div><!----></li></ol> <p>تحتاج PAB وFasterCache أيضًا إلى <code>current_timestep_callback</code> حتى تتمكن الخطافات من قراءة الخطوة الزمنية الحالية. تحقق من جودة المخرجات على النموذج الجديد، لأن إعدادات الكاش المُحسَّنة لنموذج واحد غالبًا لا تنتقل إلى نموذج آخر.</p> <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/cache.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, 8], | |
| data: [null,null], | |
| form: null, | |
| error: null | |
| }); | |
| }); | |
| } | |
| </script> | |
Xet Storage Details
- Size:
- 51.2 kB
- Xet hash:
- 033672236df002b4cbf4fa1c55da1c781e00caac378c347c2d4180031799ed82
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.