Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"تقليل استهلاك الذاكرة","local":"reduce-memory-usage","sections":[{"title":"بطاقات رسومات متعددة","local":"multiple-gpus","sections":[{"title":"نقاط التفتيش المجزأة","local":"sharded-checkpoints","sections":[],"depth":3},{"title":"توزيع الأجهزة","local":"device-placement","sections":[],"depth":3}],"depth":2},{"title":"تقسيم VAE","local":"vae-slicing","sections":[],"depth":2},{"title":"تقسيم VAE","local":"vae-tiling","sections":[],"depth":2},{"title":"إخلاء","local":"offloading","sections":[{"title":"إزاحة المعالجة إلى وحدة المعالجة المركزية","local":"cpu-offloading","sections":[],"depth":3},{"title":"تفريغ النموذج","local":"model-offloading","sections":[],"depth":3},{"title":"تفريغ المجموعات","local":"group-offloading","sections":[{"title":"بث CUDA","local":"cuda-stream","sections":[],"depth":4},{"title":"إفراغ البيانات على القرص","local":"offloading-to-disk","sections":[],"depth":4}],"depth":3}],"depth":2},{"title":"الإسناد على مستوى الطبقات","local":"layerwise-casting","sections":[],"depth":2},{"title":"torch.channels_last","local":"torchchannelslast","sections":[],"depth":2},{"title":"انتباه موفّر للذاكرة","local":"memory-efficient-attention","sections":[],"depth":2}],"depth":1}"/> | |
| <link href="/docs/diffusers/main/en/_app/immutable/entry/start.0S0_iEoG.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/I9eooN77.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/Bupkjr9f.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/entry/app.CPdOxCbw.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/CyvZ3FS5.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/l0QnnOED.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.DteTvmQZ.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/CoSrdQax.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/nodes/339.DJYaCibf.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/DSPDl1gz.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/D5eijdnr.js" rel="modulepreload"> | |
| <!--13d35im--><meta name="hf:doc:metadata" content="{"title":"Reduce memory usage","local":"reduce-memory-usage","sections":[{"title":"Multiple GPUs","local":"multiple-gpus","sections":[{"title":"Sharded checkpoints","local":"sharded-checkpoints","sections":[],"depth":3},{"title":"Device placement","local":"device-placement","sections":[],"depth":3}],"depth":2},{"title":"VAE slicing","local":"vae-slicing","sections":[],"depth":2},{"title":"VAE tiling","local":"vae-tiling","sections":[],"depth":2},{"title":"Offloading","local":"offloading","sections":[{"title":"CPU offloading","local":"cpu-offloading","sections":[],"depth":3},{"title":"Model offloading","local":"model-offloading","sections":[],"depth":3},{"title":"Group offloading","local":"group-offloading","sections":[{"title":"CUDA stream","local":"cuda-stream","sections":[],"depth":4},{"title":"Offloading to disk","local":"offloading-to-disk","sections":[],"depth":4}],"depth":3}],"depth":2},{"title":"Layerwise casting","local":"layerwise-casting","sections":[],"depth":2},{"title":"torch.channels_last","local":"torchchannelslast","sections":[],"depth":2},{"title":"Memory-efficient attention","local":"memory-efficient-attention","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="reduce-memory-usage" 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="#reduce-memory-usage"><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>تحتوي نماذج الانتشار الحديثة مثل <a href="../api/pipelines/flux">Flux</a> و<a href="../api/pipelines/wan">Wan</a> على مليارات المعاملات التي تستهلك مساحة كبيرة من الذاكرة على عتادك أثناء الاستدلال. وهذا أمر صعب لأن وحدات معالجة الرسومات الشائعة غالبًا لا تمتلك ذاكرة كافية. للتغلب على قيود الذاكرة، يمكنك استخدام أكثر من وحدة معالجة رسومات (إن توفرت)، ونقل بعض مكونات خط الأنابيب إلى وحدة المعالجة المركزية، وغير ذلك.</p> <p>ستُظهر لك هذه الدليل كيفية تقليل استهلاك الذاكرة.</p> <blockquote class="tip"><p>تذكر أن هذه التقنيات قد تحتاج إلى تعديل اعتمادًا على النموذج. على سبيل المثال، قد لا يستفيد نموذج الانتشار القائم على المحولات (Transformer) من تحسينات الذاكرة هذه بنفس القدر الذي يستفيد منه نموذج قائم على UNet.</p></blockquote> <!--[1--><h2 class="relative group"><a id="multiple-gpus" 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="#multiple-gpus"><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/docs/accelerate/index" rel="nofollow">Accelerate</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-bash "><!---->pip install -U accelerate<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="sharded-checkpoints" 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="#sharded-checkpoints"><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></h3><!--]--><!----> <p>يُعد تحميل نقاط التفتيش الكبيرة مقسمة إلى عدة أجزاء مفيدًا لأن الأجزاء تُحمَّل واحدًا تلو الآخر. وهذا يحافظ على استهلاك الذاكرة منخفضًا، حيث لا يتطلب سوى ما يكفي من الذاكرة لحجم النموذج وحجم أكبر جزء. ننصح بتقسيم نقاط التفتيش عندما يتجاوز حجم نقطة التفتيش بصيغة fp32 قيمة 5GB. حجم الجزء الافتراضي هو 5GB.</p> <p>قسّم نقطة التفتيش في <a href="/docs/diffusers/main/ar/api/pipelines/overview#diffusers.DiffusionPipeline.save_pretrained">save_pretrained()</a> باستخدام معامل <code>max_shard_size</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">from</span> diffusers <span class="hljs-keyword">import</span> AutoModel | |
| unet = AutoModel.from_pretrained( | |
| <span class="hljs-string">"stabilityai/stable-diffusion-xl-base-1.0"</span>, subfolder=<span class="hljs-string">"unet"</span> | |
| ) | |
| unet.save_pretrained(<span class="hljs-string">"sdxl-unet-sharded"</span>, max_shard_size=<span class="hljs-string">"5GB"</span>)<!----></pre></div><!----> <p>الآن يمكنك استخدام نقطة التفتيش المجزأة، بدلاً من نقطة التفتيش العادية، لتوفير الذاكرة.</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> AutoModel, StableDiffusionXLPipeline | |
| unet = AutoModel.from_pretrained( | |
| <span class="hljs-string">"username/sdxl-unet-sharded"</span>, dtype=torch.float16 | |
| ) | |
| pipeline = StableDiffusionXLPipeline.from_pretrained( | |
| <span class="hljs-string">"stabilityai/stable-diffusion-xl-base-1.0"</span>, | |
| unet=unet, | |
| dtype=torch.float16 | |
| ).to(<span class="hljs-string">"cuda"</span>) <span class="hljs-comment"># or "mps", "xpu", "cpu"</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="device-placement" 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="#device-placement"><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></h3><!--]--><!----> <blockquote class="warning"><p>توزيع الأجهزة هو ميزة تجريبية وقد تتغير واجهة برمجة التطبيقات. حاليًا، لا يُدعم سوى استراتيجية <code>balanced</code>. نخطط لدعم استراتيجيات تعيين إضافية في المستقبل.</p></blockquote> <p>يتحكم معامل <code>device_map</code> في كيفية توزيع مكونات النموذج داخل خط الأنابيب أو طبقات نموذج فردي عبر الأجهزة.</p> <div class="flex space-x-2 items-center my-1.5 mr-8 h-7 !pl-0 -mx-3 md:mx-0"><!--[--><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd border-gray-800 bg-black dark:bg-gray-700 text-white">مستوى خط الأنابيب</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">مستوى النموذج</div><!--]--></div> <div class="language-select"><div><p>توزّع استراتيجية وضع الأجهزة <code>balanced</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> AutoModel, StableDiffusionXLPipeline | |
| pipeline = StableDiffusionXLPipeline.from_pretrained( | |
| <span class="hljs-string">"stabilityai/stable-diffusion-xl-base-1.0"</span>, | |
| dtype=torch.float16, | |
| device_map=<span class="hljs-string">"balanced"</span> | |
| )<!----></pre></div><!----> <p>يمكنك فحص خريطة الأجهزة الخاصة بخط الأنابيب باستخدام <code>hf_device_map</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-built_in">print</span>(pipeline.hf_device_map) | |
| {<span class="hljs-string">'unet'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'vae'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'safety_checker'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'text_encoder'</span>: <span class="hljs-number">0</span>}<!----></pre></div><!----><!----></div><!----> <div hidden=""><p>يُفيد <code>device_map</code> في تحميل النماذج الكبيرة، مثل نموذج Flux diffusion transformer الذي يحتوي على 12.5 مليار معامل. اضبطه على <code>"auto"</code> لتوزيع النموذج تلقائيًا على أسرع جهاز أولاً قبل الانتقال إلى الأجهزة الأبطأ. راجع قسم <a href="#sharded-checkpoints">نقاط التفتيش المجزأة</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> AutoModel | |
| transformer = AutoModel.from_pretrained( | |
| <span class="hljs-string">"black-forest-labs/FLUX.1-dev"</span>, | |
| subfolder=<span class="hljs-string">"transformer"</span>, | |
| device_map=<span class="hljs-string">"auto"</span>, | |
| dtype=torch.bfloat16 | |
| )<!----></pre></div><!----> <p>يمكنك فحص خريطة الأجهزة للنموذج باستخدام <code>hf_device_map</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-built_in">print</span>(transformer.hf_device_map)<!----></pre></div><!----><!----></div><!----><!----></div><!----> <p>عند تصميم <code>device_map</code> الخاص بك، يجب أن يكون قاموسًا يربط بين اسم وحدة أو طبقة محددة في النموذج ومعرّف الجهاز (عدد صحيح للوحدات الرسومية، و<code>cpu</code> لوحات المعالجة المركزية، و<code>disk</code> للتخزين على القرص).</p> <p>استدعِ <code>hf_device_map</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-built_in">print</span>(transformer.hf_device_map) | |
| {<span class="hljs-string">'pos_embed'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'time_text_embed'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'context_embedder'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'x_embedder'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'transformer_blocks'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'single_transformer_blocks.0'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'single_transformer_blocks.1'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'single_transformer_blocks.2'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'single_transformer_blocks.3'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'single_transformer_blocks.4'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'single_transformer_blocks.5'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'single_transformer_blocks.6'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'single_transformer_blocks.7'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'single_transformer_blocks.8'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'single_transformer_blocks.9'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'single_transformer_blocks.10'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.11'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.12'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.13'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.14'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.15'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.16'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.17'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.18'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.19'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.20'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.21'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.22'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.23'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.24'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.25'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.26'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.27'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.28'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.29'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.30'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.31'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.32'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.33'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.34'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.35'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.36'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.37'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'norm_out'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'proj_out'</span>: <span class="hljs-string">'cpu'</span>}<!----></pre></div><!----> <p>على سبيل المثال، يضع <code>device_map</code> أدناه <code>single_transformer_blocks.10</code> حتى <code>single_transformer_blocks.20</code> على وحدة معالجة رسومية ثانية (<code>1</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> AutoModel | |
| device_map = { | |
| <span class="hljs-string">'pos_embed'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'time_text_embed'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'context_embedder'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'x_embedder'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'transformer_blocks'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'single_transformer_blocks.0'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'single_transformer_blocks.1'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'single_transformer_blocks.2'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'single_transformer_blocks.3'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'single_transformer_blocks.4'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'single_transformer_blocks.5'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'single_transformer_blocks.6'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'single_transformer_blocks.7'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'single_transformer_blocks.8'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'single_transformer_blocks.9'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'single_transformer_blocks.10'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'single_transformer_blocks.11'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'single_transformer_blocks.12'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'single_transformer_blocks.13'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'single_transformer_blocks.14'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'single_transformer_blocks.15'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'single_transformer_blocks.16'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'single_transformer_blocks.17'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'single_transformer_blocks.18'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'single_transformer_blocks.19'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'single_transformer_blocks.20'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'single_transformer_blocks.21'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.22'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.23'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.24'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.25'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.26'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.27'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.28'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.29'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.30'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.31'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.32'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.33'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.34'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.35'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.36'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'single_transformer_blocks.37'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'norm_out'</span>: <span class="hljs-string">'cpu'</span>, <span class="hljs-string">'proj_out'</span>: <span class="hljs-string">'cpu'</span> | |
| } | |
| transformer = AutoModel.from_pretrained( | |
| <span class="hljs-string">"black-forest-labs/FLUX.1-dev"</span>, | |
| subfolder=<span class="hljs-string">"transformer"</span>, | |
| device_map=device_map, | |
| dtype=torch.bfloat16 | |
| )<!----></pre></div><!----> <p>مرر قاموسًا يربط الحد الأقصى لاستخدام الذاكرة بكل جهاز لفرض حد. إذا لم يكن الجهاز مدرجًا في <code>max_memory</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> AutoModel, StableDiffusionXLPipeline | |
| max_memory = {<span class="hljs-number">0</span>:<span class="hljs-string">"1GB"</span>, <span class="hljs-number">1</span>:<span class="hljs-string">"1GB"</span>} | |
| pipeline = StableDiffusionXLPipeline.from_pretrained( | |
| <span class="hljs-string">"stabilityai/stable-diffusion-xl-base-1.0"</span>, | |
| dtype=torch.float16, | |
| device_map=<span class="hljs-string">"balanced"</span>, | |
| max_memory=max_memory | |
| )<!----></pre></div><!----> <p>يستخدم Diffusers الحد الأقصى لذاكرة جميع الأجهزة افتراضيًا، ولكن إذا لم تتسع على وحدات معالجة الرسوميات (GPU)، فستحتاج إلى استخدام وحدة معالجة رسوميات واحدة فقط ونقل الأحمال إلى وحدة المعالجة المركزية (CPU) باستخدام الأساليب أدناه.</p> <ul><li>يعمل <a href="/docs/diffusers/main/ar/api/pipelines/overview#diffusers.DiffusionPipeline.enable_model_cpu_offload">enable_model_cpu_offload()</a> على وحدة معالجة رسومات واحدة فقط، لكن نموذجًا كبيرًا جدًا قد لا يتسع عليها</li> <li>قد يعمل <a href="/docs/diffusers/main/ar/api/pipelines/overview#diffusers.DiffusionPipeline.enable_sequential_cpu_offload">enable_sequential_cpu_offload()</a> لكنه بطيء للغاية كما أنه محدود بوحدة معالجة رسومية واحدة</li></ul> <p>استخدم طريقة <a href="/docs/diffusers/main/ar/api/pipelines/overview#diffusers.DiffusionPipeline.reset_device_map">reset_device_map()</a> لإعادة تعيين <code>device_map</code>. هذا ضروري إذا كنت ترغب في استخدام طرق مثل <code>.to()</code> و<a href="/docs/diffusers/main/ar/api/pipelines/overview#diffusers.DiffusionPipeline.enable_sequential_cpu_offload">enable_sequential_cpu_offload()</a> و<a href="/docs/diffusers/main/ar/api/pipelines/overview#diffusers.DiffusionPipeline.enable_model_cpu_offload">enable_model_cpu_offload()</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 "><!---->pipeline.reset_device_map()<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="vae-slicing" 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="#vae-slicing"><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><!---->تقسيم VAE<!----></span></h2><!--]--><!----> <p>يحفظ تقطيع VAE الذاكرة من خلال تقسيم دفعات كبيرة من المدخلات إلى دفعة بيانات واحدة ومعالجتها بشكل منفصل. يعمل هذا الأسلوب بشكل أفضل عند توليد أكثر من صورة في كل مرة.</p> <p>على سبيل المثال، إذا كنت تولّد 4 صور في وقت واحد، فإن فك الترميز سيؤدي إلى زيادة ذروة ذاكرة التفعيل بمقدار 4 أضعاف. يقلّل تقطيع VAE من ذلك من خلال فك ترميز صورة واحدة في كل مرة بدلاً من فك ترميز جميع الصور الأربع في وقت واحد.</p> <p>استدعِ <code>enable_slicing()</code> لتفعيل VAE المقطّعة. يمكنك توقع زيادة طفيفة في الأداء عند فك ترميز دفعات متعددة الصور، دون أي تأثير على الأداء في حالة الدفعات أحادية الصورة.</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> AutoModel, StableDiffusionXLPipeline | |
| pipeline = StableDiffusionXLPipeline.from_pretrained( | |
| <span class="hljs-string">"stabilityai/stable-diffusion-xl-base-1.0"</span>, | |
| dtype=torch.float16, | |
| ).to(<span class="hljs-string">"cuda"</span>) <span class="hljs-comment"># or "mps", "xpu", "cpu"</span> | |
| pipeline.vae.enable_slicing() | |
| pipeline([<span class="hljs-string">"An astronaut riding a horse on Mars"</span>]*<span class="hljs-number">32</span>).images[<span class="hljs-number">0</span>] | |
| <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Max memory reserved: <span class="hljs-subst">{torch.cuda.max_memory_allocated() / <span class="hljs-number">1024</span>**<span class="hljs-number">3</span>:<span class="hljs-number">.2</span>f}</span> GB"</span>)<!----></pre></div><!----> <blockquote class="warning"><p>لا تدعم فئتا <a href="/docs/diffusers/main/ar/api/models/autoencoder_kl_wan#diffusers.AutoencoderKLWan">AutoencoderKLWan</a> و<a href="/docs/diffusers/main/ar/api/models/asymmetricautoencoderkl#diffusers.AsymmetricAutoencoderKL">AsymmetricAutoencoderKL</a> التقطيع.</p></blockquote> <!--[1--><h2 class="relative group"><a id="vae-tiling" 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="#vae-tiling"><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><!---->تقسيم VAE<!----></span></h2><!--]--><!----> <p>يحفظ تقطيع VAE الذاكرة من خلال تقسيم الصورة إلى بلاطات أصغر متداخلة بدلاً من معالجة الصورة بأكملها دفعة واحدة. كما يقلل هذا من ذروة استهلاك الذاكرة لأن وحدة معالجة الرسوميات (GPU) تعالج بلاطة واحدة في كل مرة.</p> <p>استدعِ <code>enable_tiling()</code> لتفعيل تقطيع VAE. قد تظهر بعض التباينات في درجات اللون من قطعة إلى أخرى لأن كل قطعة تُفكّك بشكل مستقل، لكن لا ينبغي أن تظهر أي فواصل واضحة بين القطع. يتم تعطيل التقطيع عند الدقات الأقل من حد مسبق التحديد (ولكن قابل للضبط). على سبيل المثال، هذا الحد هو 512x512 لـ VAE في <a href="/docs/diffusers/main/ar/api/pipelines/stable_diffusion/text2img#diffusers.StableDiffusionPipeline">StableDiffusionPipeline</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> AutoPipelineForImage2Image | |
| <span class="hljs-keyword">from</span> diffusers.utils <span class="hljs-keyword">import</span> load_image | |
| pipeline = AutoPipelineForImage2Image.from_pretrained( | |
| <span class="hljs-string">"stabilityai/stable-diffusion-xl-base-1.0"</span>, dtype=torch.float16 | |
| ).to(<span class="hljs-string">"cuda"</span>) <span class="hljs-comment"># or "mps", "xpu", "cpu"</span> | |
| pipeline.vae.enable_tiling() | |
| init_image = load_image(<span class="hljs-string">"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-sdxl-init.png"</span>) | |
| prompt = <span class="hljs-string">"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"</span> | |
| pipeline(prompt, image=init_image, strength=<span class="hljs-number">0.5</span>).images[<span class="hljs-number">0</span>] | |
| <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Max memory reserved: <span class="hljs-subst">{torch.cuda.max_memory_allocated() / <span class="hljs-number">1024</span>**<span class="hljs-number">3</span>:<span class="hljs-number">.2</span>f}</span> GB"</span>)<!----></pre></div><!----> <blockquote class="warning"><p>لا يدعم <a href="/docs/diffusers/main/ar/api/models/autoencoder_kl_wan#diffusers.AutoencoderKLWan">AutoencoderKLWan</a> و <a href="/docs/diffusers/main/ar/api/models/asymmetricautoencoderkl#diffusers.AsymmetricAutoencoderKL">AsymmetricAutoencoderKL</a> التقطيع (tiling).</p></blockquote> <p>لتخطي ذاكرة VAE على وحدة معالجة الرسوميات الخاصة بك بالكامل، قم بالتشفير وفك التشفير على نقطة نهاية عن بُعد باستخدام <a href="../hybrid_inference/overview">الاستدلال عن بُعد</a>.</p> <!--[1--><h2 class="relative group"><a id="offloading" 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="#offloading"><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>تنقل استراتيجيات الإزاحة الطبقات أو النماذج غير النشطة حاليًا إلى وحدة المعالجة المركزية (CPU) لتجنب زيادة ذاكرة وحدة معالجة الرسوميات (GPU). يمكن دمج هذه الاستراتيجيات مع التكميم وtorch.compile لتحقيق التوازن بين سرعة الاستدلال واستخدام الذاكرة.</p> <p>راجع دليل <a href="./speed-memory-optims">تجميع نماذج الكمّية وإزاحتها</a> لمزيد من التفاصيل.</p> <!--[2--><h3 class="relative group"><a id="cpu-offloading" 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="#cpu-offloading"><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></h3><!--]--><!----> <p>يعمل إزاحة المعالِج المركزي (CPU offloading) على نقل الأوزان من وحدة معالجة الرسوميات (GPU) إلى المعالج المركزي بشكل انتقائي. عندما يُطلب مكوّن ما، يتم نقله إلى وحدة معالجة الرسوميات، وعندما لا يكون مطلوبًا، يتم نقله إلى المعالج المركزي. تعمل هذه الطريقة على الوحدات الفرعية (submodules) بدلاً من النماذج الكاملة. فهي توفر الذاكرة من خلال تجنب تخزين النموذج بأكمله على وحدة معالجة الرسوميات.</p> <p>يُقلّل تفريغ المعالجة على وحدة المعالجة المركزية (CPU) بشكل كبير من استهلاك الذاكرة، لكنه أيضًا <strong>بطيء للغاية</strong> لأن الوحدات الفرعية تُمرَّر ذهابًا وإيابًا عدة مرات بين الأجهزة. وغالبًا ما يكون غير عملي بسبب بطئه الشديد.</p> <blockquote class="warning"><p>لا تُنقل خط الأنابيب إلى CUDA قبل استدعاء <a href="/docs/diffusers/main/ar/api/pipelines/overview#diffusers.DiffusionPipeline.enable_sequential_cpu_offload">enable_sequential_cpu_offload()</a>، وإلا فإن كمية الذاكرة الموفَّرة ستكون ضئيلة فقط (راجع هذا <a href="https://github.com/huggingface/diffusers/issues/1934" rel="nofollow">المشكلة</a> لمزيد من التفاصيل). هذه عملية ذات حالة تُثبّت الخطافات (hooks) على النموذج.</p></blockquote> <p>استدعِ <a href="/docs/diffusers/main/ar/api/pipelines/overview#diffusers.DiffusionPipeline.enable_sequential_cpu_offload">enable_sequential_cpu_offload()</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 | |
| pipeline = DiffusionPipeline.from_pretrained( | |
| <span class="hljs-string">"black-forest-labs/FLUX.1-schnell"</span>, dtype=torch.bfloat16 | |
| ) | |
| pipeline.enable_sequential_cpu_offload() | |
| pipeline( | |
| prompt=<span class="hljs-string">"An astronaut riding a horse on Mars"</span>, | |
| guidance_scale=<span class="hljs-number">0.</span>, | |
| height=<span class="hljs-number">768</span>, | |
| width=<span class="hljs-number">1360</span>, | |
| num_inference_steps=<span class="hljs-number">4</span>, | |
| max_sequence_length=<span class="hljs-number">256</span>, | |
| ).images[<span class="hljs-number">0</span>] | |
| <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Max memory reserved: <span class="hljs-subst">{torch.cuda.max_memory_allocated() / <span class="hljs-number">1024</span>**<span class="hljs-number">3</span>:<span class="hljs-number">.2</span>f}</span> GB"</span>)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="model-offloading" 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="#model-offloading"><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></h3><!--]--><!----> <p>ينقل تحميل النموذج (Model offloading) النماذج كاملةً إلى وحدة معالجة الرسوميات (GPU) بدلاً من نقل <em>بعض</em> الطبقات أو مكونات النموذج بشكل انتقائي. يتم وضع أحد نماذج خطوط الأنابيب الرئيسية، وعادةً ما يكون مُشفّر النص (text encoder) وUNet وVAE، على وحدة معالجة الرسوميات، بينما تُبقي المكونات الأخرى على وحدة المعالجة المركزية (CPU). تبقى المكونات التي تعمل عدة مرات، مثل UNet، على وحدة معالجة الرسوميات حتى تكتمل مهامها بالكامل وتُصبح غير مطلوبة بعد ذلك. هذا يلغي عبء الاتصال المرتبط بـ<a href="#cpu-offloading">تحميل وحدة المعالجة المركزية (CPU offloading)</a> ويجعل من تحميل النموذج بديلاً أسرع. الثمن هو أن توفير الذاكرة لن يكون بنفس الحجم الكبير.</p> <blockquote class="warning"><p>تذكر أنه إذا تم إعادة استخدام النماذج خارج خط الأنابيب بعد تثبيت الخطافات (انظر <a href="https://huggingface.co/docs/accelerate/en/package_reference/big_modeling#accelerate.hooks.remove_hook_from_module" rel="nofollow">إزالة الخطافات</a> لمزيد من التفاصيل)، فستحتاج إلى تشغيل خط الأنابيب بأكمله والنماذج بالترتيب المتوقع لتفريغها بشكل صحيح. هذه عملية ذات حالة تقوم بتثبيت الخطافات على النموذج.</p></blockquote> <p>استدعِ <a href="/docs/diffusers/main/ar/api/pipelines/overview#diffusers.DiffusionPipeline.enable_model_cpu_offload">enable_model_cpu_offload()</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 | |
| pipeline = DiffusionPipeline.from_pretrained( | |
| <span class="hljs-string">"black-forest-labs/FLUX.1-schnell"</span>, dtype=torch.bfloat16 | |
| ) | |
| pipeline.enable_model_cpu_offload() | |
| pipeline( | |
| prompt=<span class="hljs-string">"An astronaut riding a horse on Mars"</span>, | |
| guidance_scale=<span class="hljs-number">0.</span>, | |
| height=<span class="hljs-number">768</span>, | |
| width=<span class="hljs-number">1360</span>, | |
| num_inference_steps=<span class="hljs-number">4</span>, | |
| max_sequence_length=<span class="hljs-number">256</span>, | |
| ).images[<span class="hljs-number">0</span>] | |
| <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Max memory reserved: <span class="hljs-subst">{torch.cuda.max_memory_allocated() / <span class="hljs-number">1024</span>**<span class="hljs-number">3</span>:<span class="hljs-number">.2</span>f}</span> GB"</span>)<!----></pre></div><!----> <p>يساعدك <a href="/docs/diffusers/main/ar/api/pipelines/overview#diffusers.DiffusionPipeline.enable_model_cpu_offload">enable_model_cpu_offload()</a> أيضًا عند استخدامك طريقة <a href="/docs/diffusers/main/ar/api/pipelines/stable_diffusion/stable_diffusion_xl#diffusers.StableDiffusionXLPipeline.encode_prompt">encode_prompt()</a> بمفردها لتوليد حالة المخفية لمشفّرات النص.</p> <!--[2--><h3 class="relative group"><a id="group-offloading" 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="#group-offloading"><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></h3><!--]--><!----> <p>ينقل تفريغ المجموعات مجموعات من الطبقات الداخلية (<a href="https://pytorch.org/docs/stable/generated/torch.nn.ModuleList.html" rel="nofollow">torch.nn.ModuleList</a> أو <a href="https://pytorch.org/docs/stable/generated/torch.nn.Sequential.html" rel="nofollow">torch.nn.Sequential</a>) إلى وحدة المعالجة المركزية. وهو يستخدم ذاكرة أقل من <a href="#model-offloading">تفريغ النموذج</a> وهو أسرع من <a href="#cpu-offloading">تفريغ وحدة المعالجة المركزية</a> لأنه يقلل من عبء الاتصال.</p> <blockquote class="warning"><p>قد لا يعمل تفريغ المجموعات مع جميع النماذج إذا تضمن تنفيذ الأمامي (forward) تحويل الأجهزة المعتمد على الأوزان للمدخلات، لأن ذلك قد يتعارض مع آلية تحويل الأجهزة الخاصة بتفريغ المجموعات.</p></blockquote> <p>فعّل تفريغ المجموعات عن طريق ضبط معامل <code>offload_type</code> على <code>block_level</code> أو <code>leaf_level</code>.</p> <ul><li><code>block_level</code> يُحمّل مجموعات من الطبقات بناءً على معامل <code>num_blocks_per_group</code>. على سبيل المثال، إذا كان <code>num_blocks_per_group=2</code> في نموذج يحتوي على 40 طبقة، يتم تحميل 2 طبقة وإخراجهما في كل مرة (20 عملية تحميل/إخراج إجمالاً). وهذا يقلل بشكل كبير من متطلبات الذاكرة.</li> <li><code>leaf_level</code> يُحمّل الطبقات الفردية على المستوى الأدنى وهو مكافئ لـ <a href="#cpu-offloading">إزاحة المعالجة إلى وحدة المعالجة المركزية (CPU offloading)</a>. لكن يمكن تسريعه باستخدام التدفقات (streams) دون التنازل عن سرعة الاستدلال.</li></ul> <p>يُؤمَّن دعم تفريغ المجموعات (Group offloading) للأنابيب (pipelines) بأكملها أو للنماذج الفردية. يُعد تطبيق تفريغ المجموعات على الأنبوب بأكمله الخيار الأسهل، بينما يمنح تطبيقه الانتقائي على نماذج فردية المستخدمين مرونة أكبر لاستخدام تقنيات تفريغ مختلفة لكل نموذج.</p> <div class="flex space-x-2 items-center my-1.5 mr-8 h-7 !pl-0 -mx-3 md:mx-0"><!--[--><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd border-gray-800 bg-black dark:bg-gray-700 text-white">pipeline</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">نموذج</div><!--]--></div> <div class="language-select"><div><p>استدعِ <a href="/docs/diffusers/main/ar/api/pipelines/overview#diffusers.DiffusionPipeline.enable_group_offload">enable_group_offload()</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> CogVideoXPipeline | |
| <span class="hljs-keyword">from</span> diffusers.hooks <span class="hljs-keyword">import</span> apply_group_offloading | |
| <span class="hljs-keyword">from</span> diffusers.utils <span class="hljs-keyword">import</span> export_to_video | |
| onload_device = torch.device(<span class="hljs-string">"cuda"</span>) | |
| offload_device = torch.device(<span class="hljs-string">"cpu"</span>) | |
| pipeline = CogVideoXPipeline.from_pretrained(<span class="hljs-string">"THUDM/CogVideoX-5b"</span>, dtype=torch.bfloat16) | |
| pipeline.enable_group_offload( | |
| onload_device=onload_device, | |
| offload_device=offload_device, | |
| offload_type=<span class="hljs-string">"leaf_level"</span>, | |
| use_stream=<span class="hljs-literal">True</span> | |
| ) | |
| prompt = ( | |
| <span class="hljs-string">"A panda, dressed in a small, red jacket and a tiny hat, sits on a wooden stool in a serene bamboo forest. "</span> | |
| <span class="hljs-string">"The panda's fluffy paws strum a miniature acoustic guitar, producing soft, melodic tunes. Nearby, a few other "</span> | |
| <span class="hljs-string">"pandas gather, watching curiously and some clapping in rhythm. Sunlight filters through the tall bamboo, "</span> | |
| <span class="hljs-string">"casting a gentle glow on the scene. The panda's face is expressive, showing concentration and joy as it plays. "</span> | |
| <span class="hljs-string">"The background includes a small, flowing stream and vibrant green foliage, enhancing the peaceful and magical "</span> | |
| <span class="hljs-string">"atmosphere of this unique musical performance."</span> | |
| ) | |
| video = pipeline(prompt=prompt, guidance_scale=<span class="hljs-number">6</span>, num_inference_steps=<span class="hljs-number">50</span>).frames[<span class="hljs-number">0</span>] | |
| <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Max memory reserved: <span class="hljs-subst">{torch.cuda.max_memory_allocated() / <span class="hljs-number">1024</span>**<span class="hljs-number">3</span>:<span class="hljs-number">.2</span>f}</span> GB"</span>) | |
| export_to_video(video, <span class="hljs-string">"output.mp4"</span>, fps=<span class="hljs-number">8</span>)<!----></pre></div><!----><!----></div><!----> <div hidden=""><p>استدعِ <a href="/docs/diffusers/main/ar/api/models/overview#diffusers.ModelMixin.enable_group_offload">enable_group_offload()</a> على مكوّنات نماذج Diffusers القياسية التي ترث من <a href="/docs/diffusers/main/ar/api/models/overview#diffusers.ModelMixin">ModelMixin</a>. أما بالنسبة لمكوّنات النماذج الأخرى التي لا ترث من <a href="/docs/diffusers/main/ar/api/models/overview#diffusers.ModelMixin">ModelMixin</a>، مثل <a href="https://pytorch.org/docs/stable/generated/torch.nn.Module.html" rel="nofollow">torch.nn.Module</a> العام، فاستخدمي <a href="/docs/diffusers/main/ar/api/utilities#diffusers.hooks.apply_group_offloading">apply_group_offloading()</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> CogVideoXPipeline | |
| <span class="hljs-keyword">from</span> diffusers.hooks <span class="hljs-keyword">import</span> apply_group_offloading | |
| <span class="hljs-keyword">from</span> diffusers.utils <span class="hljs-keyword">import</span> export_to_video | |
| onload_device = torch.device(<span class="hljs-string">"cuda"</span>) | |
| offload_device = torch.device(<span class="hljs-string">"cpu"</span>) | |
| pipeline = CogVideoXPipeline.from_pretrained(<span class="hljs-string">"THUDM/CogVideoX-5b"</span>, dtype=torch.bfloat16) | |
| <span class="hljs-comment"># Use the enable_group_offload method for Diffusers model implementations</span> | |
| pipeline.transformer.enable_group_offload(onload_device=onload_device, offload_device=offload_device, offload_type=<span class="hljs-string">"leaf_level"</span>) | |
| pipeline.vae.enable_group_offload(onload_device=onload_device, offload_type=<span class="hljs-string">"leaf_level"</span>) | |
| <span class="hljs-comment"># Use the apply_group_offloading method for other model components</span> | |
| apply_group_offloading(pipeline.text_encoder, onload_device=onload_device, offload_type=<span class="hljs-string">"block_level"</span>, num_blocks_per_group=<span class="hljs-number">2</span>) | |
| prompt = ( | |
| <span class="hljs-string">"A panda, dressed in a small, red jacket and a tiny hat, sits on a wooden stool in a serene bamboo forest. "</span> | |
| <span class="hljs-string">"The panda's fluffy paws strum a miniature acoustic guitar, producing soft, melodic tunes. Nearby, a few other "</span> | |
| <span class="hljs-string">"pandas gather, watching curiously and some clapping in rhythm. Sunlight filters through the tall bamboo, "</span> | |
| <span class="hljs-string">"casting a gentle glow on the scene. The panda's face is expressive, showing concentration and joy as it plays. "</span> | |
| <span class="hljs-string">"The background includes a small, flowing stream and vibrant green foliage, enhancing the peaceful and magical "</span> | |
| <span class="hljs-string">"atmosphere of this unique musical performance."</span> | |
| ) | |
| video = pipeline(prompt=prompt, guidance_scale=<span class="hljs-number">6</span>, num_inference_steps=<span class="hljs-number">50</span>).frames[<span class="hljs-number">0</span>] | |
| <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Max memory reserved: <span class="hljs-subst">{torch.cuda.max_memory_allocated() / <span class="hljs-number">1024</span>**<span class="hljs-number">3</span>:<span class="hljs-number">.2</span>f}</span> GB"</span>) | |
| export_to_video(video, <span class="hljs-string">"output.mp4"</span>, fps=<span class="hljs-number">8</span>)<!----></pre></div><!----><!----></div><!----><!----></div><!----> <!--[3--><h4 class="relative group"><a id="cuda-stream" 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="#cuda-stream"><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><!---->بث CUDA<!----></span></h4><!--]--><!----> <p>يمكن تفعيل معامل <code>use_stream</code> لأجهزة CUDA التي تدعم تدفقات نقل البيانات غير المتزامنة لتقليل وقت التنفيذ الإجمالي مقارنةً بـ <a href="#cpu-offloading">إزاحة المعالجة إلى وحدة المعالجة المركزية</a>. يتداخل نقل البيانات والحساب باستخدام استباق الطبقات. يتم تحميل الطبقة التالية التي سيتم تنفيذها على وحدة معالجة الرسوميات بينما لا تزال الطبقة الحالية قيد التنفيذ. قد يؤدي ذلك إلى زيادة كبيرة في ذاكرة وحدة المعالجة المركزية، لذا تأكد من توفر ضعف حجم ذاكرة حجم النموذج.</p> <p>اضبط <code>record_stream=True</code> للحصول على تسريع أكبر على حساب زيادة طفيفة في استخدام الذاكرة. راجع وثائق <a href="https://pytorch.org/docs/stable/generated/torch.Tensor.record_stream.html" rel="nofollow">torch.Tensor.record_stream</a> لمعرفة المزيد.</p> <blockquote class="tip"><p>عند استخدام <code>use_stream=True</code> مع نماذج VAE التي تم تفعيل خاصية التقطيع (tiling) فيها، تأكد من إجراء تمريرة أمامية تجريبية (dummy forward pass) (يمكن استخدام مدخلات تجريبية أيضًا) قبل الاستدلال لتجنب أخطاء عدم تطابق الجهاز. قد لا يعمل هذا الحل في جميع التطبيقات، لذا لا تتردد في فتح مشكلة (issue) إذا واجهت أي مشاكل.</p></blockquote> <p>إذا كنت تستخدم تفريغ المجموعات على مستوى <code>block_level</code> مع تفعيل <code>use_stream</code>، فيجب تعيين معامل <code>num_blocks_per_group</code> إلى <code>1</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 "><!---->pipeline.transformer.enable_group_offload(onload_device=onload_device, offload_device=offload_device, offload_type=<span class="hljs-string">"leaf_level"</span>, use_stream=<span class="hljs-literal">True</span>, record_stream=<span class="hljs-literal">True</span>)<!----></pre></div><!----> <p>يمكن ضبط معامل <code>low_cpu_mem_usage</code> على <code>True</code> لتقليل استخدام ذاكرة المعالج المركزي عند استخدام التدفقات أثناء تفريغ المجموعات. وهو الخيار الأفضل لتفريغ <code>leaf_level</code> وعند كون ذاكرة المعالج المركزي هي عنق الزجاجة. يتم توفير الذاكرة من خلال إنشاء متجهات مثبتة على الطاير بدلاً من تثبيتها مسبقاً. ومع ذلك، قد يؤدي ذلك إلى زيادة وقت التنفيذ الإجمالي.</p> <!--[3--><h4 class="relative group"><a id="offloading-to-disk" 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="#offloading-to-disk"><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></h4><!--]--><!----> <p>يمكن أن يستهلك تفريغ المجموعات ذاكرة نظام كبيرة اعتماداً على حجم النموذج. على الأنظمة ذات الذاكرة المحدودة، جرّب تفريغ المجموعات على القرص كذاكرة ثانوية.</p> <p>اضبط الحجة <code>offload_to_disk_path</code> في <a href="/docs/diffusers/main/ar/api/models/overview#diffusers.ModelMixin.enable_group_offload">enable_group_offload()</a> أو <a href="/docs/diffusers/main/ar/api/utilities#diffusers.hooks.apply_group_offloading">apply_group_offloading()</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 "><!---->pipeline.transformer.enable_group_offload(onload_device=onload_device, offload_device=offload_device, offload_type=<span class="hljs-string">"leaf_level"</span>, offload_to_disk_path=<span class="hljs-string">"path/to/disk"</span>) | |
| apply_group_offloading(pipeline.text_encoder, onload_device=onload_device, offload_type=<span class="hljs-string">"block_level"</span>, num_blocks_per_group=<span class="hljs-number">2</span>, offload_to_disk_path=<span class="hljs-string">"path/to/disk"</span>)<!----></pre></div><!----> <p>راجع هاتين <a href="https://github.com/huggingface/diffusers/pull/11682#issue-3129365363" rel="nofollow">جدولتين</a> <a href="https://github.com/huggingface/diffusers/pull/11682#issuecomment-2955715126" rel="nofollow">للمقارنة</a> بين التنازلات بين السرعة والذاكرة.</p> <!--[1--><h2 class="relative group"><a id="layerwise-casting" 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="#layerwise-casting"><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><!--]--><!----> <blockquote class="tip"><p>اجمع بين الصب على مستوى الطبقات مع <a href="#group-offloading">إزاحة المجموعات</a> لتحقيق توفير أكبر في الذاكرة.</p></blockquote> <p>يحفظ التحويل على مستوى الطبقات الأوزان في صيغة بيانات أصغر (مثل <code>torch.float8_e4m3fn</code> و<code>torch.float8_e5m2</code>) لتقليل استهلاك الذاكرة، ثم يرفع دقة تلك الأوزان إلى دقة أعلى مثل <code>torch.float16</code> أو <code>torch.bfloat16</code> لأغراض الحساب. يتم تجاوز طبقات معينة (الأوزان المتعلقة بالتطبيع والتعديل) لأن تخزينها بصيغة fp8 قد يُضعف جودة التوليد.</p> <blockquote class="warning"><p>قد لا يعمل التحويل على مستوى الطبقات مع جميع النماذج إذا تضمنت عملية التنفيذ الأمامي تحويلًا داخليًا لأنواع الأوزان. يفترض التنفيذ الحالي للتحويل على مستوى الطبقات أن المرور الأمامي مستقل عن دقة الأوزان وأن أنواع بيانات الإدخال محددة دائمًا في <code>compute_dtype</code> (انظر <a href="https://github.com/huggingface/transformers/blob/7f5077e53682ca855afc826162b204ebf809f1f9/src/transformers/models/t5/modeling_t5.py#L294-L299" rel="nofollow">هنا</a> لمعرفة مثال على تنفيذ غير متوافق).</p> <p>قد يفشل أيضًا التحويل على مستوى الطبقات في تطبيقات النمذجة المخصصة التي تحتوي على طبقات <a href="https://huggingface.co/docs/peft/index" rel="nofollow">PEFT</a>. هناك بعض الفحوصات المتاحة، لكنها غير مُختبرة بشكل شامل ولا يُكفَل عملها في جميع الحالات.</p></blockquote> <p>استدعِ <a href="/docs/diffusers/main/ar/api/models/overview#diffusers.ModelMixin.enable_layerwise_casting">enable_layerwise_casting()</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> CogVideoXPipeline, CogVideoXTransformer3DModel | |
| <span class="hljs-keyword">from</span> diffusers.utils <span class="hljs-keyword">import</span> export_to_video | |
| transformer = CogVideoXTransformer3DModel.from_pretrained( | |
| <span class="hljs-string">"THUDM/CogVideoX-5b"</span>, | |
| subfolder=<span class="hljs-string">"transformer"</span>, | |
| dtype=torch.bfloat16 | |
| ) | |
| transformer.enable_layerwise_casting(storage_dtype=torch.float8_e4m3fn, compute_dtype=torch.bfloat16) | |
| pipeline = CogVideoXPipeline.from_pretrained(<span class="hljs-string">"THUDM/CogVideoX-5b"</span>, | |
| transformer=transformer, | |
| dtype=torch.bfloat16 | |
| ).to(<span class="hljs-string">"cuda"</span>) <span class="hljs-comment"># or "mps", "xpu", "cpu"</span> | |
| prompt = ( | |
| <span class="hljs-string">"A panda, dressed in a small, red jacket and a tiny hat, sits on a wooden stool in a serene bamboo forest. "</span> | |
| <span class="hljs-string">"The panda's fluffy paws strum a miniature acoustic guitar, producing soft, melodic tunes. Nearby, a few other "</span> | |
| <span class="hljs-string">"pandas gather, watching curiously and some clapping in rhythm. Sunlight filters through the tall bamboo, "</span> | |
| <span class="hljs-string">"casting a gentle glow on the scene. The panda's face is expressive, showing concentration and joy as it plays. "</span> | |
| <span class="hljs-string">"The background includes a small, flowing stream and vibrant green foliage, enhancing the peaceful and magical "</span> | |
| <span class="hljs-string">"atmosphere of this unique musical performance."</span> | |
| ) | |
| video = pipeline(prompt=prompt, guidance_scale=<span class="hljs-number">6</span>, num_inference_steps=<span class="hljs-number">50</span>).frames[<span class="hljs-number">0</span>] | |
| <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Max memory reserved: <span class="hljs-subst">{torch.cuda.max_memory_allocated() / <span class="hljs-number">1024</span>**<span class="hljs-number">3</span>:<span class="hljs-number">.2</span>f}</span> GB"</span>) | |
| export_to_video(video, <span class="hljs-string">"output.mp4"</span>, fps=<span class="hljs-number">8</span>)<!----></pre></div><!----> <p>يمكن أيضًا استخدام طريقة <a href="/docs/diffusers/main/ar/api/utilities#diffusers.hooks.apply_layerwise_casting">apply_layerwise_casting()</a> إذا كنت بحاجة إلى مزيد من التحكم والمرونة. يمكن تطبيقها جزئيًا على طبقات النموذج من خلال استدعائها على وحدات داخلية محددة. استخدم المعاملات <code>skip_modules_pattern</code> أو <code>skip_modules_classes</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> CogVideoXTransformer3DModel | |
| <span class="hljs-keyword">from</span> diffusers.hooks <span class="hljs-keyword">import</span> apply_layerwise_casting | |
| transformer = CogVideoXTransformer3DModel.from_pretrained( | |
| <span class="hljs-string">"THUDM/CogVideoX-5b"</span>, | |
| subfolder=<span class="hljs-string">"transformer"</span>, | |
| dtype=torch.bfloat16 | |
| ) | |
| <span class="hljs-comment"># skip the normalization layer</span> | |
| apply_layerwise_casting( | |
| transformer, | |
| storage_dtype=torch.float8_e4m3fn, | |
| compute_dtype=torch.bfloat16, | |
| skip_modules_classes=[<span class="hljs-string">"norm"</span>], | |
| non_blocking=<span class="hljs-literal">True</span>, | |
| )<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="torchchannelslast" 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="#torchchannelslast"><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><!---->torch.channels_last<!----></span></h2><!--]--><!----> <p>يعكس <a href="https://pytorch.org/tutorials/intermediate/memory_format_tutorial.html" rel="nofollow">torch.channels_last</a> طريقة تخزين المصفوفات من <code>(batch size, channels, height, width)</code> إلى <code>(batch size, height, width, channels)</code>. وهذا يُحاذا المصفوفات مع الطريقة التي تصل بها العتاد بشكل تسلسلي إلى المصفوفات المخزنة في الذاكرة، ويتجنب القفزات في الذاكرة للوصول إلى قيم البكسل.</p> <p>لا تدعم جميع العمليات حاليًا تنسيق القنوات الأخيرة، وقد يؤدي ذلك إلى أداء أسوأ، لكن لا يزال من الجدير بالمحاولة.</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-built_in">print</span>(pipeline.unet.conv_out.state_dict()[<span class="hljs-string">"weight"</span>].stride()) <span class="hljs-comment"># (2880, 9, 3, 1)</span> | |
| pipeline.unet.to(memory_format=torch.channels_last) <span class="hljs-comment"># in-place operation</span> | |
| <span class="hljs-built_in">print</span>( | |
| pipeline.unet.conv_out.state_dict()[<span class="hljs-string">"weight"</span>].stride() | |
| ) <span class="hljs-comment"># (2880, 1, 960, 320) having a stride of 1 for the 2nd dimension proves that it works</span><!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="memory-efficient-attention" 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="#memory-efficient-attention"><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>يدعم Diffusers عدة واجهات خلفية لانتباه فعّال من حيث الذاكرة (FlashAttention وxFormers وSageAttention وغيرها) من خلال <a href="/docs/diffusers/main/ar/api/models/overview#diffusers.ModelMixin.set_attention_backend">set_attention_backend()</a>. راجع دليل <a href="./attention_backends">واجهات خلفية الانتباه</a> لمعرفة كيفية التبديل بينها.</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/memory.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_1qsdbwf = { | |
| 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.0S0_iEoG.js"), | |
| import("/docs/diffusers/main/en/_app/immutable/entry/app.CPdOxCbw.js") | |
| ]).then(([kit, app]) => { | |
| kit.start(app, element, { | |
| node_ids: [0, 339], | |
| data: [null,null], | |
| form: null, | |
| error: null | |
| }); | |
| }); | |
| } | |
| </script> | |
Xet Storage Details
- Size:
- 109 kB
- Xet hash:
- 6963624a1e633c6b9b0de0fc3066ed728c9d4ab0b51fe5a5fe305dadf6d18a56
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.