Buckets:

mishig's picture
download
raw
42.3 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;التخزين المؤقت&quot;,&quot;local&quot;:&quot;caching&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;بث الانتباه الهرمي&quot;,&quot;local&quot;:&quot;pyramid-attention-broadcast&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;FasterCache&quot;,&quot;local&quot;:&quot;fastercache&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;SeaCache&quot;,&quot;local&quot;:&quot;seacache&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Cosmos 3&quot;,&quot;local&quot;:&quot;cosmos-3&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;FirstBlockCache&quot;,&quot;local&quot;:&quot;firstblockcache&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;TaylorSeer ذاكرة مؤقتة&quot;,&quot;local&quot;:&quot;taylorseer-cache&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;MagCache&quot;,&quot;local&quot;:&quot;magcache&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"/>
<link href="/docs/diffusers/v0.43.0/en/_app/immutable/entry/start.1gaXe03k.js" rel="modulepreload">
<link href="/docs/diffusers/v0.43.0/en/_app/immutable/chunks/BObs6oQO.js" rel="modulepreload">
<link href="/docs/diffusers/v0.43.0/en/_app/immutable/chunks/DPnyVmXs.js" rel="modulepreload">
<link href="/docs/diffusers/v0.43.0/en/_app/immutable/entry/app.BwdYRbCn.js" rel="modulepreload">
<link href="/docs/diffusers/v0.43.0/en/_app/immutable/chunks/DSGUT_OW.js" rel="modulepreload">
<link href="/docs/diffusers/v0.43.0/en/_app/immutable/chunks/C045MWWx.js" rel="modulepreload">
<link href="/docs/diffusers/v0.43.0/en/_app/immutable/chunks/6tuJ3nyi.js" rel="modulepreload">
<link href="/docs/diffusers/v0.43.0/en/_app/immutable/chunks/DsnmJJEf.js" rel="modulepreload">
<link href="/docs/diffusers/v0.43.0/en/_app/immutable/nodes/0.CornNeQR.js" rel="modulepreload">
<link href="/docs/diffusers/v0.43.0/en/_app/immutable/chunks/IaDvMxLQ.js" rel="modulepreload">
<link href="/docs/diffusers/v0.43.0/en/_app/immutable/nodes/308.BXNeqIJG.js" rel="modulepreload">
<link href="/docs/diffusers/v0.43.0/en/_app/immutable/chunks/bKdwVYpU.js" rel="modulepreload">
<!--fs68f1--><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Caching&quot;,&quot;local&quot;:&quot;caching&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Pyramid Attention Broadcast&quot;,&quot;local&quot;:&quot;pyramid-attention-broadcast&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;FasterCache&quot;,&quot;local&quot;:&quot;fastercache&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;SeaCache&quot;,&quot;local&quot;:&quot;seacache&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Cosmos 3&quot;,&quot;local&quot;:&quot;cosmos-3&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;FirstBlockCache&quot;,&quot;local&quot;:&quot;firstblockcache&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;TaylorSeer Cache&quot;,&quot;local&quot;:&quot;taylorseer-cache&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;MagCache&quot;,&quot;local&quot;:&quot;magcache&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"/><!---->
<link href="/docs/diffusers/v0.43.0/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>تُسرع التخزين المؤقت (Caching) الاستدلال من خلال تخزين المخرجات الوسيطة للطبقات المختلفة، مثل طبقات الانتباه (attention) وطبقات التغذية الأمامية (feedforward)، وإعادة استخدامها، بدلاً من تنفيذ الحساب الكامل في كل خطوة من خطوات الاستدلال. وهو يحسّن سرعة التوليد بشكل كبير على حساب زيادة استهلاك الذاكرة، ولا يتطلب تدريبًا إضافيًا.</p> <p>يُظهر لك هذا الدليل كيفية استخدام طرق التخزين المؤقت المدعومة في Diffusers.</p> <!--[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">بث الانتباه الهرمي (PAB)</a> يستند إلى الملاحظة أن مخرجات الانتباه لا تختلف كثيرًا بين خطوات التوليد المتتالية. تكون فروق الانتباه أصغر في طبقات الانتباه المتقاطع، وتُخزَّن عمومًا عبر نطاق أوسع من الخطوات الزمنية. يتبع ذلك طبقات الانتباه الزمني وطبقات الانتباه المكاني.</p> <blockquote class="tip"><p>لا تمتلك جميع نماذج الفيديو ثلاثة أنواع من الانتباه (المتقاطع، والزمني، والمكاني)!</p></blockquote> <p>يمكن دمج PAB مع تقنيات أخرى مثل توازي التسلسل وتوازي التوجيه الحر من التصنيف (توازي البيانات) لتحقيق توليد الفيديو شبه الفوري.</p> <p>قم بإعداد <a href="/docs/diffusers/v0.43.0/ar/api/cache#diffusers.PyramidAttentionBroadcastConfig">PyramidAttentionBroadcastConfig</a> ومرره إلى محوّل خط الأنابيب لتفعيله. يتحكم <code>spatial_attention_block_skip_range</code> في تكرار تخطي حسابات الانتباه في كتل الانتباه المكانية، بينما يمثل <code>spatial_attention_timestep_skip_range</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> CogVideoXPipeline, PyramidAttentionBroadcastConfig
pipeline = CogVideoXPipeline.from_pretrained(<span class="hljs-string">&quot;THUDM/CogVideoX-5b&quot;</span>, dtype=torch.bfloat16)
pipeline.to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</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,
)
pipeline.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>قد يختار هذا الأسلوب أيضًا تخطي تنبؤ الفرع غير الشرطي، عند استخدام التوجيه الحر من التصنيف للعينة (وهو أمر شائع في معظم النماذج الأساسية)، وتقديره من تنبؤ الفرع الشرطي إذا كان هناك تكرار كبير في المخرجات الكامنة المتنبأ بها بين خطوات الزمن المتتالية.</p> <p>قم بإعداد <a href="/docs/diffusers/v0.43.0/ar/api/cache#diffusers.FasterCacheConfig">FasterCacheConfig</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, FasterCacheConfig
pipe line= CogVideoXPipeline.from_pretrained(<span class="hljs-string">&quot;THUDM/CogVideoX-5b&quot;</span>, dtype=torch.bfloat16)
pipeline.to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</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">&quot;BFCHW&quot;</span>,
)
pipeline.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>تتحكم حزم Cosmos 3 Transfer في التلميحات كسلاسل بصرية مستقلة، لذا يستثني مُكيّفها هذه التلميحات من المؤشر.
تقارن فروع Control-CFG مسار الإخراج نفسه مع الاحتفاظ بقاياها المخزنة مسبقاً. لا تزال التلميحات الشرطية تؤثر على محول التحويل.</p> <p>توفر التنفيذ محولات مدمجة للنماذج التالية:</p> <ul><li><strong>Cosmos 3</strong> هو التكامل الأساسي المُحسَّن والمُختبَر. يقوم بتخزين المكدس الكامل لفك الترميز عبر حدّ ما بعد التطبيع.</li> <li>يستخدم <strong>Wan T2V</strong> مسار الكتل المتكررة العام في وضع eager. تُظهر هذه التكيفية كيف يمكن لمحوّل فيديو أحادي التدفق آخر أن يوفر كامنات رؤية خام إلى SeaCache؛ وهي ليست ادعاءً بأن نفس معاملات التخزين المؤقت مثالية لـ Wan أو أن متغيرات Wan الأخرى مدعومة.</li></ul> <p>يمكن لحوّالات الفيديو الأخرى أن تتكامل مع المسار العام عندما تستخدم <code>CacheMixin</code>، وتكشف عن قائمة كتل متكررة معترف بها، وتسجّل تخطيط مدخلات/مخرجات الكتل في <code>TransformerBlockRegistry</code>. يجب أن تدخل خط الأنابيب في <code>cache_context</code> لكل استدعاء للحوّال، وتربط <code>step_index</code> و<code>sigma</code> و<code>num_inference_steps</code>، وتستخدم أسماء سياق منفصلة للمسارات المستقلة مثل التوجيه الشرطي وغير الشرطي. مرّر <code>raw_vision_callback</code> الذي يعيد الكامنات البصرية التي تشكّل المخرجات المولّدة عندما لا يتوفر مُكيّف مدمج. تحقّق من جودة المخرجات وضبط معاملات التخزين المؤقت لكل نموذج ومجدول؛ فالدعم ونتائج الاختبارات لا تنتقل تلقائيًا من Cosmos 3.</p> <!--[2--><h3 class="relative group"><a id="cosmos-3" 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="#cosmos-3"><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><!---->Cosmos 3<!----></span></h3><!--]--><!----> <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">&quot;nvidia/Cosmos3-Nano&quot;</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>يعمل هذا الواجهة البرمجية على مستوى النموذج مع <a href="/docs/diffusers/v0.43.0/ar/api/pipelines/cosmos3#diffusers.Cosmos3OmniPipeline">Cosmos3OmniPipeline</a> و<a href="/docs/diffusers/v0.43.0/ar/api/pipelines/cosmos3#diffusers.Cosmos3OmniModularPipeline">Cosmos3OmniModularPipeline</a> و<a href="/docs/diffusers/v0.43.0/ar/api/pipelines/cosmos3#diffusers.Cosmos3DistilledModularPipeline">Cosmos3DistilledModularPipeline</a>. يُعد SeaCache تحسينًا تقريبيًا وقد يُغيّر المخرجات المولّدة. استدعِ <code>pipe.transformer.disable_cache()</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="https://huggingface.co/docs/diffusers/main/en/api/cache#diffusers.FirstBlockCacheConfig" rel="nofollow">FirstBlock Cache</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> DiffusionPipeline
<span class="hljs-keyword">from</span> diffusers.hooks <span class="hljs-keyword">import</span> apply_first_block_cache, FirstBlockCacheConfig
pipeline = DiffusionPipeline.from_pretrained(
<span class="hljs-string">&quot;Qwen/Qwen-Image&quot;</span>, dtype=torch.bfloat16
)
apply_first_block_cache(pipeline.transformer, 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/2403.06923" rel="nofollow">TaylorSeer Cache</a> على تسريع استنتاج النماذج الانتشارية من خلال استخدام توسعات متسلسلة تايلور لتقريب وتخزين التفعيلات الوسيطة عبر خطوات إزالة الضوضاء. يتنبأ هذا المنهج بالمخرجات المستقبلية بناءً على الحسابات السابقة، ويعيد استخدامها عند فترات محددة لتقليل الحسابات المكررة.</p> <p>يقدّم هذا آلية التخزين المؤقت نتائج قوية مع حد أدنى من العبء الإضافي على الذاكرة. لتحليل الأداء التفصيلي، راجعوا <a href="https://github.com/huggingface/diffusers/pull/12648#issuecomment-3610615080" rel="nofollow">نتائجنا هنا</a>.</p> <p>لتفعيل TaylorSeer Cache، أنشئ <a href="/docs/diffusers/v0.43.0/ar/api/cache#diffusers.TaylorSeerCacheConfig">TaylorSeerCacheConfig</a> وقم بتمريره إلى نموذج التحويل الخاص بخط أنابيبك:</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">&quot;black-forest-labs/FLUX.1-dev&quot;</span>,
dtype=torch.bfloat16,
).to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</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> على تسريع الاستدلال عن طريق تخطي كتل التحويل (transformer blocks) بناءً على حجم التحديث المتبقي. يلاحظ أن حجم التحديثات (المخرجات - المدخلات) يتناقص بشكل يمكن التنبؤ به على مدى عملية الانتشار. من خلال تراكم "ميزانية خطأ" بناءً على نسب الأحجام المحسوبة مسبقًا، يقرر ديناميكيًا متى يتعين تخطي الحساب وإعادة استخدام المتبقي السابق.</p> <p>يعتمد MagCache على <strong>النسب الحجمية</strong> (<code>mag_ratios</code>)، والتي تصف منحنى التلاشي هذا. هذه النسب محددة لنقطة التفتيش النموذجية ومجدول الجدولة. النسب الحجمية المرفقة <code>FLUX_MAG_RATIOS</code> تم قياسها على FLUX.1؛ بينما تحتاج النماذج الأخرى، بما في ذلك Flux2 وFlux2 Klein، إلى عملية معايرة خاصة بها.</p> <p>لاستخدام MagCache، تتّبع عادةً عملية من خطوتين: <strong>المعايرة</strong> و<strong>الاستدلال</strong>.</p> <ol><li><strong>المعايرة</strong>: نفّذ الاستدلال مرة واحدة مع <code>calibrate=True</code>. سيقوم الخطاف بقياس أحجام البواقي وطباعة النسب المحسوبة في وحدة التحكم.</li> <li><strong>الاستدلال</strong>: مرر هذه النسب إلى <code>MagCacheConfig</code> لتفعيل التسريع.</li></ol> <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
pipe = FluxPipeline.from_pretrained(
<span class="hljs-string">&quot;black-forest-labs/FLUX.1-schnell&quot;</span>,
dtype=torch.bfloat16
).to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</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">&quot;A cat playing chess&quot;</span>, num_inference_steps=<span class="hljs-number">4</span>)
<span class="hljs-comment"># Logs will print something like: &quot;MagCache Calibration Results: [1.0, 1.37, 0.97, 0.87]&quot;</span>
<span class="hljs-comment"># 2. Inference Step</span>
<span class="hljs-comment"># Apply the specific ratios obtained from calibration for optimized speed.</span>
<span class="hljs-comment"># Note: For Flux models, you can also import defaults: </span>
<span class="hljs-comment"># from diffusers.hooks.mag_cache import 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">&quot;A cat playing chess&quot;</span>, num_inference_steps=<span class="hljs-number">4</span>).images[<span class="hljs-number">0</span>]<!----></pre></div><!----> <blockquote class="note"><p>تمثل <code>mag_ratios</code> منحنى التلاشي الجوهري للمقياس في النموذج. يمكن إعادة استخدام النسب المُعايرة لعدد كبير من الخطوات (مثل 50) مع عدد أقل من الخطوات (مثل 20). يستخدم التنفيذ الاستيفاء (Interpolation) لربط المنحنى بعدد خطوات الاستدلال الحالية.</p></blockquote> <blockquote class="tip"><p>في خطوط الأنابيب التي تعمل بتوجيه بلا مُصنِّف (Classifier-Free Guidance) بشكل تسلسلي (مثل Kandinsky 5.0)، قد يطبع سجل المعايرة مصفوفتين: واحدة للمرور الشرطي (Conditional) وأخرى للمرور غير الشرطي (Unconditional). في معظم الحالات، يجب عليك استخدام المصفوفة الأولى (الشرطية).</p></blockquote> <blockquote class="tip"><p>بالنسبة إلى خطوط الأنابيب التي تعمل بتوجيه بلا مُصنِّف (Classifier-Free Guidance) بطريقة <strong>مجمَّعة</strong> (مثل SDXL أو Flux)، فإن <code>hidden_states</code> التي يعالجها النموذج تحتوي على كلا الفرعين الشرطي وغير الشرطي مدموجين معًا. يأخذ عملية المعايرة هذا الأمر في الحسبان تلقائيًا، فتنتج عن ذلك مصفوفة واحدة من النسب تمثل السلوك المشترك. يمكنك استخدام هذه المصفوفة الناتجة مباشرة دون أي تعديل.</p></blockquote> <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_ywq9g6 = {
base: "/docs/diffusers/v0.43.0/en",
assets: "/docs/diffusers/v0.43.0/en"
};
const element = document.currentScript.parentElement;
Promise.all([
import("/docs/diffusers/v0.43.0/en/_app/immutable/entry/start.1gaXe03k.js"),
import("/docs/diffusers/v0.43.0/en/_app/immutable/entry/app.BwdYRbCn.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 308],
data: [null,null],
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
42.3 kB
·
Xet hash:
47895aee7d323f7d37112d24722d323c4a612d575deb37219f3d25f8b4f844d3

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.