Buckets:
| import"../chunks/DsnmJJEf.js";import{i as te,h as ae,C as ie,H as o,D as s,E as ne,s as re,a as X}from"../chunks/BtE7mKSK.js";import{p as le,o as de,s as e,f as k,a as d,b as ce,c as t,d as T,n as i,r as a}from"../chunks/jDjavuwI.js";import{E as W}from"../chunks/SrSJA0zO.js";const pe='{"title":"Utilities","local":"utilities","sections":[{"title":"numpy_to_pil","local":"diffusers.utils.numpy_to_pil","sections":[],"depth":2},{"title":"pt_to_pil","local":"diffusers.utils.pt_to_pil","sections":[],"depth":2},{"title":"load_image","local":"diffusers.utils.load_image","sections":[],"depth":2},{"title":"load_video","local":"diffusers.utils.load_video","sections":[],"depth":2},{"title":"export_to_gif","local":"diffusers.utils.export_to_gif","sections":[],"depth":2},{"title":"export_to_video","local":"diffusers.utils.export_to_video","sections":[],"depth":2},{"title":"encode_video","local":"diffusers.utils.encode_video","sections":[],"depth":2},{"title":"make_image_grid","local":"diffusers.utils.make_image_grid","sections":[],"depth":2},{"title":"randn_tensor","local":"diffusers.utils.torch_utils.randn_tensor","sections":[],"depth":2},{"title":"apply_layerwise_casting","local":"diffusers.hooks.apply_layerwise_casting","sections":[],"depth":2},{"title":"apply_group_offloading","local":"diffusers.hooks.apply_group_offloading","sections":[],"depth":2}],"depth":1}';var ue=T('<meta name="hf:doc:metadata"/>'),F=T("<p>Example:</p> <!>",1),me=T(`<p></p> <!> <!> <p>Utility and helper functions for working with 🤗 Diffusers.</p> <!> <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"><!> <p>Convert a numpy image or a batch of images to a PIL image.</p></div> <!> <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"><!> <p>Convert a torch image to a PIL image.</p></div> <!> <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"><!> <p>Loads <code>image</code> to a PIL Image.</p></div> <!> <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"><!> <p>Loads <code>video</code> to a list of PIL Image.</p></div> <!> <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"><!></div> <!> <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"><!> <p>quality: | |
| Video output quality. Default is 5. Uses variable bit rate. Highest quality is 10, lowest is 0. Set to None to | |
| prevent variable bitrate flags to FFMPEG so you can manually specify them using output_params instead. | |
| Specifying a fixed bitrate using <code>bitrate</code> disables this parameter.</p> <p>bitrate: | |
| Set a constant bitrate for the video encoding. Default is None causing <code>quality</code> parameter to be used instead. | |
| Better quality videos with smaller file sizes will result from using the <code>quality</code> variable bitrate parameter | |
| rather than specifying a fixed bitrate with this parameter.</p> <p>macro_block_size: | |
| Size constraint for video. Width and height, must be divisible by this number. If not divisible by this number | |
| imageio will tell ffmpeg to scale the image up to the next closest size divisible by this number. Most codecs | |
| are compatible with a macroblock size of 16 (default), some can go smaller (4, 8). To disable this automatic | |
| feature set it to None or 1, however be warned many players can’t decode videos that are odd in size and some | |
| codecs will produce poor results or fail. See <a href="https://en.wikipedia.org/wiki/Macroblock" rel="nofollow">https://en.wikipedia.org/wiki/Macroblock</a>.</p></div> <!> <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"><!> <p>Encodes a video with optional audio using the PyAV library. Based on code from the original LTX-2 repo: <a href="https://github.com/Lightricks/LTX-2/blob/4f410820b198e05074a1e92de793e3b59e9ab5a0/packages/ltx-pipelines/src/ltx_pipelines/utils/media_io.py#L182" rel="nofollow">https://github.com/Lightricks/LTX-2/blob/4f410820b198e05074a1e92de793e3b59e9ab5a0/packages/ltx-pipelines/src/ltx_pipelines/utils/media_io.py#L182</a></p></div> <!> <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"><!> <p>Prepares a single grid of images. Useful for visualization purposes.</p></div> <!> <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"><!> <p>A helper function to create random tensors on the desired <code>device</code> with the desired <code>dtype</code>. When | |
| passing a list of generators, you can seed each batch size individually. If CPU generators are passed, the tensor | |
| is always created on the CPU.</p></div> <!> <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"><!> <p>Applies layerwise casting to a given module. The module expected here is a Diffusers ModelMixin but it can be any | |
| nn.Module using diffusers layers or pytorch primitives.</p> <!></div> <!> <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"><!> <p>Applies group offloading to the internal layers of a torch.nn.Module. To understand what group offloading is, and | |
| where it is beneficial, we need to first provide some context on how other supported offloading methods work.</p> <p>Typically, offloading is done at two levels:</p> <ul><li>Module-level: In Diffusers, this can be enabled using the <code>ModelMixin::enable_model_cpu_offload()</code> method. It | |
| works by offloading each component of a pipeline to the CPU for storage, and onloading to the accelerator device | |
| when needed for computation. This method is more memory-efficient than keeping all components on the accelerator, | |
| but the memory requirements are still quite high. For this method to work, one needs memory equivalent to size of | |
| the model in runtime dtype + size of largest intermediate activation tensors to be able to complete the forward | |
| pass.</li> <li>Leaf-level: In Diffusers, this can be enabled using the <code>ModelMixin::enable_sequential_cpu_offload()</code> method. It | |
| works by offloading the lowest leaf-level parameters of the computation graph to the CPU for storage, and | |
| onloading only the leafs to the accelerator device for computation. This uses the lowest amount of accelerator | |
| memory, but can be slower due to the excessive number of device synchronizations.</li></ul> <p>Group offloading is a middle ground between the two methods. It works by offloading groups of internal layers, | |
| (either <code>torch.nn.ModuleList</code> or <code>torch.nn.Sequential</code>). This method uses lower memory than module-level | |
| offloading. It is also faster than leaf-level/sequential offloading, as the number of device synchronizations is | |
| reduced.</p> <p>Another supported feature (for CUDA devices with support for asynchronous data transfer streams) is the ability to | |
| overlap data transfer and computation to reduce the overall execution time compared to sequential offloading. This | |
| is enabled using layer prefetching with streams, i.e., the layer that is to be executed next starts onloading to | |
| the accelerator device while the current layer is being executed - this increases the memory requirements slightly. | |
| Note that this implementation also supports leaf-level offloading but can be made much faster when using streams.</p> <!></div> <!> <p></p>`,1);function ve(R,E){le(E,!1),de(()=>{new URLSearchParams(window.location.search).get("fw")}),te();var I=me();ae("pf6uyw",n=>{var l=ue();re(l,"content",pe),d(n,l)});var x=e(k(I),2);ie(x,{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"});var J=e(x,2);o(J,{title:"Utilities",local:"utilities",headingTag:"h1"});var M=e(J,4);o(M,{title:"numpy_to_pil",local:"diffusers.utils.numpy_to_pil",headingTag:"h2"});var c=e(M,2),z=t(c);s(z,{name:"diffusers.utils.numpy_to_pil",anchor:"diffusers.utils.numpy_to_pil",source:"https://github.com/huggingface/diffusers/blob/vr_14229/src/diffusers/utils/pil_utils.py#L35",parameters:[{name:"images",val:""}]}),i(2),a(c);var j=e(c,2);o(j,{title:"pt_to_pil",local:"diffusers.utils.pt_to_pil",headingTag:"h2"});var p=e(j,2),A=t(p);s(A,{name:"diffusers.utils.pt_to_pil",anchor:"diffusers.utils.pt_to_pil",source:"https://github.com/huggingface/diffusers/blob/vr_14229/src/diffusers/utils/pil_utils.py#L25",parameters:[{name:"images",val:""}]}),i(2),a(p);var N=e(p,2);o(N,{title:"load_image",local:"diffusers.utils.load_image",headingTag:"h2"});var u=e(N,2),S=t(u);s(S,{name:"diffusers.utils.load_image",anchor:"diffusers.utils.load_image",source:"https://github.com/huggingface/diffusers/blob/vr_14229/src/diffusers/utils/loading_utils.py#L14",parameters:[{name:"image",val:": str | PIL.Image.Image"},{name:"convert_method",val:": typing.Optional[typing.Callable[[PIL.Image.Image], PIL.Image.Image]] = None"}],parametersDescription:[{anchor:"diffusers.utils.load_image.image",description:`<strong>image</strong> (<code>str</code> or <code>PIL.Image.Image</code>) — | |
| The image to convert to the PIL Image format.`,name:"image"},{anchor:"diffusers.utils.load_image.convert_method",description:`<strong>convert_method</strong> (Callable[[PIL.Image.Image], PIL.Image.Image], <em>optional</em>) — | |
| A conversion method to apply to the image after loading it. When set to <code>None</code> the image will be converted | |
| “RGB”.`,name:"convert_method"}],returnDescription:`<script context="module">export const metadata = 'undefined';<\/script> | |
| <p>A PIL Image.</p> | |
| `,returnType:`<script context="module">export const metadata = 'undefined';<\/script> | |
| <p><code>PIL.Image.Image</code></p> | |
| `}),i(2),a(u);var U=e(u,2);o(U,{title:"load_video",local:"diffusers.utils.load_video",headingTag:"h2"});var m=e(U,2),H=t(m);s(H,{name:"diffusers.utils.load_video",anchor:"diffusers.utils.load_video",source:"https://github.com/huggingface/diffusers/blob/vr_14229/src/diffusers/utils/loading_utils.py#L57",parameters:[{name:"video",val:": str"},{name:"convert_method",val:": typing.Optional[typing.Callable[[list[PIL.Image.Image]], list[PIL.Image.Image]]] = None"}],parametersDescription:[{anchor:"diffusers.utils.load_video.video",description:`<strong>video</strong> (<code>str</code>) — | |
| A URL or Path to a video to convert to a list of PIL Image format.`,name:"video"},{anchor:"diffusers.utils.load_video.convert_method",description:`<strong>convert_method</strong> (Callable[[list[PIL.Image.Image]], list[PIL.Image.Image]], <em>optional</em>) — | |
| A conversion method to apply to the video after loading it. When set to <code>None</code> the images will be converted | |
| to “RGB”.`,name:"convert_method"}],returnDescription:`<script context="module">export const metadata = 'undefined';<\/script> | |
| <p>The video as a list of PIL images.</p> | |
| `,returnType:`<script context="module">export const metadata = 'undefined';<\/script> | |
| <p><code>list[PIL.Image.Image]</code></p> | |
| `}),i(2),a(m);var Z=e(m,2);o(Z,{title:"export_to_gif",local:"diffusers.utils.export_to_gif",headingTag:"h2"});var f=e(Z,2),Q=t(f);s(Q,{name:"diffusers.utils.export_to_gif",anchor:"diffusers.utils.export_to_gif",source:"https://github.com/huggingface/diffusers/blob/vr_14229/src/diffusers/utils/export_utils.py#L37",parameters:[{name:"image",val:": list[PIL.Image.Image]"},{name:"output_gif_path",val:": str = None"},{name:"fps",val:": int = 10"}]}),a(f);var C=e(f,2);o(C,{title:"export_to_video",local:"diffusers.utils.export_to_video",headingTag:"h2"});var h=e(C,2),Y=t(h);s(Y,{name:"diffusers.utils.export_to_video",anchor:"diffusers.utils.export_to_video",source:"https://github.com/huggingface/diffusers/blob/vr_14229/src/diffusers/utils/export_utils.py#L150",parameters:[{name:"video_frames",val:": list[np.ndarray] | list[PIL.Image.Image]"},{name:"output_video_path",val:": str = None"},{name:"fps",val:": int = 10"},{name:"quality",val:": float = 5.0"},{name:"bitrate",val:": int | None = None"},{name:"macro_block_size",val:": int | None = 16"}]}),i(6),a(h);var L=e(h,2);o(L,{title:"encode_video",local:"diffusers.utils.encode_video",headingTag:"h2"});var g=e(L,2),$=t(g);s($,{name:"diffusers.utils.encode_video",anchor:"diffusers.utils.encode_video",source:"https://github.com/huggingface/diffusers/blob/vr_14229/src/diffusers/utils/export_utils.py#L303",parameters:[{name:"video",val:": list[PIL.Image.Image] | np.ndarray | 'torch.Tensor' | Iterator['torch.Tensor']"},{name:"fps",val:": int"},{name:"output_path",val:": str"},{name:"audio",val:": 'torch.Tensor' | None = None"},{name:"audio_sample_rate",val:": int | None = None"},{name:"video_chunks_number",val:": int = 1"}],parametersDescription:[{anchor:"diffusers.utils.encode_video.video",description:`<strong>video</strong> (<code>List[PIL.Image.Image]</code> or <code>np.ndarray</code> or <code>torch.Tensor</code>) — | |
| A video tensor of shape [frames, height, width, channels] with integer pixel values in [0, 255]. If the | |
| input is a <code>np.ndarray</code>, it is expected to be a float array with values in [0, 1] (which is what pipelines | |
| usually return with <code>output_type="np"</code>).`,name:"video"},{anchor:"diffusers.utils.encode_video.fps",description:`<strong>fps</strong> (<code>int</code>) — | |
| The frames per second (FPS) of the encoded video.`,name:"fps"},{anchor:"diffusers.utils.encode_video.output_path",description:`<strong>output_path</strong> (<code>str</code>) — | |
| The path to save the encoded video to.`,name:"output_path"},{anchor:"diffusers.utils.encode_video.audio",description:`<strong>audio</strong> (<code>torch.Tensor</code>, <em>optional</em>) — | |
| An audio waveform of shape [audio_channels, samples].`,name:"audio"},{anchor:"diffusers.utils.encode_video.audio_sample_rate",description:`<strong>audio_sample_rate</strong> — (<code>int</code>, <em>optional</em>): | |
| The sampling rate of the audio waveform.`,name:"audio_sample_rate"},{anchor:"diffusers.utils.encode_video.video_chunks_number",description:`<strong>video_chunks_number</strong> (<code>int</code>, <em>optional</em>, defaults to <code>1</code>) — | |
| The number of chunks to split the video into for encoding. Each chunk will be encoded separately. The | |
| number of chunks to use often depends on the tiling config for the video VAE.`,name:"video_chunks_number"}]}),i(2),a(g);var V=e(g,2);o(V,{title:"make_image_grid",local:"diffusers.utils.make_image_grid",headingTag:"h2"});var _=e(V,2),K=t(_);s(K,{name:"diffusers.utils.make_image_grid",anchor:"diffusers.utils.make_image_grid",source:"https://github.com/huggingface/diffusers/blob/vr_14229/src/diffusers/utils/pil_utils.py#L51",parameters:[{name:"images",val:": list"},{name:"rows",val:": int"},{name:"cols",val:": int"},{name:"resize",val:": int = None"}]}),i(2),a(_);var B=e(_,2);o(B,{title:"randn_tensor",local:"diffusers.utils.torch_utils.randn_tensor",headingTag:"h2"});var v=e(B,2),O=t(v);s(O,{name:"diffusers.utils.torch_utils.randn_tensor",anchor:"diffusers.utils.torch_utils.randn_tensor",source:"https://github.com/huggingface/diffusers/blob/vr_14229/src/diffusers/utils/torch_utils.py#L183",parameters:[{name:"shape",val:": tuple | list"},{name:"generator",val:": list['torch.Generator'] | 'torch.Generator' | None = None"},{name:"device",val:": str | 'torch.device' | None = None"},{name:"dtype",val:": 'torch.dtype' | None = None"},{name:"layout",val:": 'torch.layout' | None = None"}]}),i(2),a(v);var D=e(v,2);o(D,{title:"apply_layerwise_casting",local:"diffusers.hooks.apply_layerwise_casting",headingTag:"h2"});var b=e(D,2),P=t(b);s(P,{name:"diffusers.hooks.apply_layerwise_casting",anchor:"diffusers.hooks.apply_layerwise_casting",source:"https://github.com/huggingface/diffusers/blob/vr_14229/src/diffusers/hooks/layerwise_casting.py#L101",parameters:[{name:"module",val:": Module"},{name:"storage_dtype",val:": dtype"},{name:"compute_dtype",val:": dtype"},{name:"skip_modules_pattern",val:": str | tuple[str, ...] = 'auto'"},{name:"skip_modules_classes",val:": tuple[typing.Type[torch.nn.Module], ...] | None = None"},{name:"non_blocking",val:": bool = False"}],parametersDescription:[{anchor:"diffusers.hooks.apply_layerwise_casting.module",description:`<strong>module</strong> (<code>torch.nn.Module</code>) — | |
| The module whose leaf modules will be cast to a high precision dtype for computation, and to a low | |
| precision dtype for storage.`,name:"module"},{anchor:"diffusers.hooks.apply_layerwise_casting.storage_dtype",description:`<strong>storage_dtype</strong> (<code>torch.dtype</code>) — | |
| The dtype to cast the module to before/after the forward pass for storage.`,name:"storage_dtype"},{anchor:"diffusers.hooks.apply_layerwise_casting.compute_dtype",description:`<strong>compute_dtype</strong> (<code>torch.dtype</code>) — | |
| The dtype to cast the module to during the forward pass for computation.`,name:"compute_dtype"},{anchor:"diffusers.hooks.apply_layerwise_casting.skip_modules_pattern",description:`<strong>skip_modules_pattern</strong> (<code>tuple[str, ...]</code>, defaults to <code>"auto"</code>) — | |
| A list of patterns to match the names of the modules to skip during the layerwise casting process. If set | |
| to <code>"auto"</code>, the default patterns are used. If set to <code>None</code>, no modules are skipped. If set to <code>None</code> | |
| alongside <code>skip_modules_classes</code> being <code>None</code>, the layerwise casting is applied directly to the module | |
| instead of its internal submodules.`,name:"skip_modules_pattern"},{anchor:"diffusers.hooks.apply_layerwise_casting.skip_modules_classes",description:`<strong>skip_modules_classes</strong> (<code>tuple[Type[torch.nn.Module], ...]</code>, defaults to <code>None</code>) — | |
| A list of module classes to skip during the layerwise casting process.`,name:"skip_modules_classes"},{anchor:"diffusers.hooks.apply_layerwise_casting.non_blocking",description:`<strong>non_blocking</strong> (<code>bool</code>, defaults to <code>False</code>) — | |
| If <code>True</code>, the weight casting operations are non-blocking.`,name:"non_blocking"}]});var ee=e(P,4);W(ee,{anchor:"diffusers.hooks.apply_layerwise_casting.example",children:(n,l)=>{var r=F(),w=e(k(r),2);X(w,{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwQ29nVmlkZW9YVHJhbnNmb3JtZXIzRE1vZGVsJTBBJTBBdHJhbnNmb3JtZXIlMjAlM0QlMjBDb2dWaWRlb1hUcmFuc2Zvcm1lcjNETW9kZWwuZnJvbV9wcmV0cmFpbmVkKCUwQSUyMCUyMCUyMCUyMG1vZGVsX2lkJTJDJTIwc3ViZm9sZGVyJTNEJTIydHJhbnNmb3JtZXIlMjIlMkMlMjB0b3JjaF9kdHlwZSUzRHRvcmNoLmJmbG9hdDE2JTBBKSUwQSUwQWFwcGx5X2xheWVyd2lzZV9jYXN0aW5nKCUwQSUyMCUyMCUyMCUyMHRyYW5zZm9ybWVyJTJDJTBBJTIwJTIwJTIwJTIwc3RvcmFnZV9kdHlwZSUzRHRvcmNoLmZsb2F0OF9lNG0zZm4lMkMlMEElMjAlMjAlMjAlMjBjb21wdXRlX2R0eXBlJTNEdG9yY2guYmZsb2F0MTYlMkMlMEElMjAlMjAlMjAlMjBza2lwX21vZHVsZXNfcGF0dGVybiUzRCU1QiUyMnBhdGNoX2VtYmVkJTIyJTJDJTIwJTIybm9ybSUyMiUyQyUyMCUyMnByb2pfb3V0JTIyJTVEJTJDJTBBJTIwJTIwJTIwJTIwbm9uX2Jsb2NraW5nJTNEVHJ1ZSUyQyUwQSk=",highlighted:`<span class="hljs-meta">>>> </span><span class="hljs-keyword">import</span> torch | |
| <span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> CogVideoXTransformer3DModel | |
| <span class="hljs-meta">>>> </span>transformer = CogVideoXTransformer3DModel.from_pretrained( | |
| <span class="hljs-meta">... </span> model_id, subfolder=<span class="hljs-string">"transformer"</span>, torch_dtype=torch.bfloat16 | |
| <span class="hljs-meta">... </span>) | |
| <span class="hljs-meta">>>> </span>apply_layerwise_casting( | |
| <span class="hljs-meta">... </span> transformer, | |
| <span class="hljs-meta">... </span> storage_dtype=torch.float8_e4m3fn, | |
| <span class="hljs-meta">... </span> compute_dtype=torch.bfloat16, | |
| <span class="hljs-meta">... </span> skip_modules_pattern=[<span class="hljs-string">"patch_embed"</span>, <span class="hljs-string">"norm"</span>, <span class="hljs-string">"proj_out"</span>], | |
| <span class="hljs-meta">... </span> non_blocking=<span class="hljs-literal">True</span>, | |
| <span class="hljs-meta">... </span>)`,lang:"python",wrap:!1}),d(n,r)},$$slots:{default:!0}}),a(b);var q=e(b,2);o(q,{title:"apply_group_offloading",local:"diffusers.hooks.apply_group_offloading",headingTag:"h2"});var y=e(q,2),G=t(y);s(G,{name:"diffusers.hooks.apply_group_offloading",anchor:"diffusers.hooks.apply_group_offloading",source:"https://github.com/huggingface/diffusers/blob/vr_14229/src/diffusers/hooks/group_offloading.py#L565",parameters:[{name:"module",val:": Module"},{name:"onload_device",val:": typing.Union[str, torch.device]"},{name:"offload_device",val:": typing.Union[str, torch.device] = torch.device()"},{name:"offload_type",val:": str | diffusers.hooks.group_offloading.GroupOffloadingType = 'block_level'"},{name:"num_blocks_per_group",val:": int | None = None"},{name:"non_blocking",val:": bool = False"},{name:"use_stream",val:": bool = False"},{name:"record_stream",val:": bool = False"},{name:"low_cpu_mem_usage",val:": bool = False"},{name:"offload_to_disk_path",val:": str | None = None"},{name:"block_modules",val:": list[str] | None = None"},{name:"exclude_kwargs",val:": list[str] | None = None"}],parametersDescription:[{anchor:"diffusers.hooks.apply_group_offloading.module",description:`<strong>module</strong> (<code>torch.nn.Module</code>) — | |
| The module to which group offloading is applied.`,name:"module"},{anchor:"diffusers.hooks.apply_group_offloading.onload_device",description:`<strong>onload_device</strong> (<code>torch.device</code>) — | |
| The device to which the group of modules are onloaded.`,name:"onload_device"},{anchor:"diffusers.hooks.apply_group_offloading.offload_device",description:`<strong>offload_device</strong> (<code>torch.device</code>, defaults to <code>torch.device("cpu")</code>) — | |
| The device to which the group of modules are offloaded. This should typically be the CPU. Default is CPU.`,name:"offload_device"},{anchor:"diffusers.hooks.apply_group_offloading.offload_type",description:`<strong>offload_type</strong> (<code>str</code> or <code>GroupOffloadingType</code>, defaults to “block_level”) — | |
| The type of offloading to be applied. Can be one of “block_level” or “leaf_level”. Default is | |
| “block_level”.`,name:"offload_type"},{anchor:"diffusers.hooks.apply_group_offloading.offload_to_disk_path",description:`<strong>offload_to_disk_path</strong> (<code>str</code>, <em>optional</em>, defaults to <code>None</code>) — | |
| The path to the directory where parameters will be offloaded. Setting this option can be useful in limited | |
| RAM environment settings where a reasonable speed-memory trade-off is desired.`,name:"offload_to_disk_path"},{anchor:"diffusers.hooks.apply_group_offloading.num_blocks_per_group",description:`<strong>num_blocks_per_group</strong> (<code>int</code>, <em>optional</em>) — | |
| The number of blocks per group when using offload_type=“block_level”. This is required when using | |
| offload_type=“block_level”.`,name:"num_blocks_per_group"},{anchor:"diffusers.hooks.apply_group_offloading.non_blocking",description:`<strong>non_blocking</strong> (<code>bool</code>, defaults to <code>False</code>) — | |
| If True, offloading and onloading is done with non-blocking data transfer.`,name:"non_blocking"},{anchor:"diffusers.hooks.apply_group_offloading.use_stream",description:`<strong>use_stream</strong> (<code>bool</code>, defaults to <code>False</code>) — | |
| If True, offloading and onloading is done asynchronously using a CUDA stream. This can be useful for | |
| overlapping computation and data transfer.`,name:"use_stream"},{anchor:"diffusers.hooks.apply_group_offloading.record_stream",description:`<strong>record_stream</strong> (<code>bool</code>, defaults to <code>False</code>) — When enabled with <code>use_stream</code>, it marks the current tensor | |
| as having been used by this stream. It is faster at the expense of slightly more memory usage. Refer to the | |
| <a href="https://pytorch.org/docs/stable/generated/torch.Tensor.record_stream.html" rel="nofollow">PyTorch official docs</a> more | |
| details.`,name:"record_stream"},{anchor:"diffusers.hooks.apply_group_offloading.low_cpu_mem_usage",description:`<strong>low_cpu_mem_usage</strong> (<code>bool</code>, defaults to <code>False</code>) — | |
| If True, the CPU memory usage is minimized by pinning tensors on-the-fly instead of pre-pinning them. This | |
| option only matters when using streamed CPU offloading (i.e. <code>use_stream=True</code>). This can be useful when | |
| the CPU memory is a bottleneck but may counteract the benefits of using streams.`,name:"low_cpu_mem_usage"},{anchor:"diffusers.hooks.apply_group_offloading.block_modules",description:`<strong>block_modules</strong> (<code>list[str]</code>, <em>optional</em>) — | |
| List of module names that should be treated as blocks for offloading. If provided, only these modules will | |
| be considered for block-level offloading. If not provided, the default block detection logic will be used.`,name:"block_modules"},{anchor:"diffusers.hooks.apply_group_offloading.exclude_kwargs",description:`<strong>exclude_kwargs</strong> (<code>list[str]</code>, <em>optional</em>) — | |
| List of kwarg keys that should not be processed by send_to_device. This is useful for mutable state like | |
| caching lists that need to maintain their object identity across forward passes. If not provided, will be | |
| inferred from the module’s <code>_skip_keys</code> attribute if it exists.`,name:"exclude_kwargs"}]});var oe=e(G,12);W(oe,{anchor:"diffusers.hooks.apply_group_offloading.example",children:(n,l)=>{var r=F(),w=e(k(r),2);X(w,{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMENvZ1ZpZGVvWFRyYW5zZm9ybWVyM0RNb2RlbCUwQWZyb20lMjBkaWZmdXNlcnMuaG9va3MlMjBpbXBvcnQlMjBhcHBseV9ncm91cF9vZmZsb2FkaW5nJTBBJTBBdHJhbnNmb3JtZXIlMjAlM0QlMjBDb2dWaWRlb1hUcmFuc2Zvcm1lcjNETW9kZWwuZnJvbV9wcmV0cmFpbmVkKCUwQSUyMCUyMCUyMCUyMCUyMlRIVURNJTJGQ29nVmlkZW9YLTViJTIyJTJDJTIwc3ViZm9sZGVyJTNEJTIydHJhbnNmb3JtZXIlMjIlMkMlMjB0b3JjaF9kdHlwZSUzRHRvcmNoLmJmbG9hdDE2JTBBKSUwQSUwQWFwcGx5X2dyb3VwX29mZmxvYWRpbmcoJTBBJTIwJTIwJTIwJTIwdHJhbnNmb3JtZXIlMkMlMEElMjAlMjAlMjAlMjBvbmxvYWRfZGV2aWNlJTNEdG9yY2guZGV2aWNlKCUyMmN1ZGElMjIpJTJDJTBBJTIwJTIwJTIwJTIwb2ZmbG9hZF9kZXZpY2UlM0R0b3JjaC5kZXZpY2UoJTIyY3B1JTIyKSUyQyUwQSUyMCUyMCUyMCUyMG9mZmxvYWRfdHlwZSUzRCUyMmJsb2NrX2xldmVsJTIyJTJDJTBBJTIwJTIwJTIwJTIwbnVtX2Jsb2Nrc19wZXJfZ3JvdXAlM0QyJTJDJTBBJTIwJTIwJTIwJTIwdXNlX3N0cmVhbSUzRFRydWUlMkMlMEEp",highlighted:`<span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> CogVideoXTransformer3DModel | |
| <span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> diffusers.hooks <span class="hljs-keyword">import</span> apply_group_offloading | |
| <span class="hljs-meta">>>> </span>transformer = CogVideoXTransformer3DModel.from_pretrained( | |
| <span class="hljs-meta">... </span> <span class="hljs-string">"THUDM/CogVideoX-5b"</span>, subfolder=<span class="hljs-string">"transformer"</span>, torch_dtype=torch.bfloat16 | |
| <span class="hljs-meta">... </span>) | |
| <span class="hljs-meta">>>> </span>apply_group_offloading( | |
| <span class="hljs-meta">... </span> transformer, | |
| <span class="hljs-meta">... </span> onload_device=torch.device(<span class="hljs-string">"cuda"</span>), | |
| <span class="hljs-meta">... </span> offload_device=torch.device(<span class="hljs-string">"cpu"</span>), | |
| <span class="hljs-meta">... </span> offload_type=<span class="hljs-string">"block_level"</span>, | |
| <span class="hljs-meta">... </span> num_blocks_per_group=<span class="hljs-number">2</span>, | |
| <span class="hljs-meta">... </span> use_stream=<span class="hljs-literal">True</span>, | |
| <span class="hljs-meta">... </span>)`,lang:"python",wrap:!1}),d(n,r)},$$slots:{default:!0}}),a(y);var se=e(y,2);ne(se,{source:"https://github.com/huggingface/diffusers/blob/main/docs/source/en/api/utilities.md"}),i(2),d(R,I),ce()}export{ve as component}; | |
Xet Storage Details
- Size:
- 28.2 kB
- Xet hash:
- cc1dec0ecd13bdd04ce38523a85d0350b9848e9cee4fa8b771e72df6f20eeb46
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.