Buckets:

download
raw
9.36 kB
import"../chunks/DsnmJJEf.js";import{i as G,h as W,C as R,H as o,b as k,a as r,E as V,s as I}from"../chunks/CmJXCtRL.js";import{p as B,o as C,s as e,f as l,a,b as X,d as n,n as z}from"../chunks/DK803DsY.js";import{H as _}from"../chunks/BtTdhXOX.js";const E='{"title":"Reproducibility","local":"reproducibility","sections":[{"title":"Generator","local":"generator","sections":[],"depth":2},{"title":"Deterministic algorithms","local":"deterministic-algorithms","sections":[],"depth":2},{"title":"Resources","local":"resources","sections":[],"depth":2}],"depth":1}';var S=n('<meta name="hf:doc:metadata"/>'),F=n('<p>The GPU uses a different random number generator than the CPU. Diffusers solves this issue with the <a href="/docs/diffusers/pr_14407/en/api/utilities#diffusers.utils.torch_utils.randn_tensor">randn_tensor()</a> function to create the random tensor on a CPU and then moving it to the GPU. This function is used everywhere inside the pipeline and you don’t need to explicitly call it.</p> <p>Use <a href="https://docs.pytorch.org/docs/stable/generated/torch.manual_seed.html" rel="nofollow">manual_seed</a> as shown below to set a seed.</p> <!>',1),P=n('<p>Set <code>device="cpu"</code> in the <code>Generator</code> and use <a href="https://docs.pytorch.org/docs/stable/generated/torch.manual_seed.html" rel="nofollow">manual_seed</a> to set a seed for generating random numbers.</p> <!>',1),D=n("<!> <!>",1),Y=n('<p></p> <!> <!> <p>Diffusion is a random process that generates a different output every time. For certain situations like testing and replicating results, you want to generate the same result each time, across releases and platforms within a certain tolerance range.</p> <p>This guide will show you how to control sources of randomness and enable deterministic algorithms.</p> <!> <p>Pipelines rely on <a href="https://pytorch.org/docs/stable/generated/torch.randn.html" rel="nofollow">torch.randn</a>, which uses a different random seed each time, to create the initial noisy tensors. To generate the same output on a CPU or GPU, use a <a href="https://docs.pytorch.org/docs/stable/generated/torch.Generator.html" rel="nofollow">Generator</a> to manage how random values are generated.</p> <blockquote class="tip"><p>If reproducibility is important to your use case, we recommend always using a CPU <code>Generator</code>. The performance loss is often negligible and you’ll generate more similar values.</p></blockquote> <!> <p>The <code>Generator</code> object should be passed to the pipeline instead of an integer seed. <code>Generator</code> maintains a <em>random state</em> that is consumed and modified when used. Once consumed, the same <code>Generator</code> object produces different results in subsequent calls, even across different pipelines, because its <em>state</em> has changed.</p> <!> <!> <p>PyTorch supports <a href="https://docs.pytorch.org/docs/stable/notes/randomness.html#avoiding-nondeterministic-algorithms" rel="nofollow">deterministic algorithms</a> - where available - for certain operations so they produce the same results. Deterministic algorithms may be slower and decrease performance.</p> <p>Use Diffusers’ <a href="https://github.com/huggingface/diffusers/blob/142f353e1c638ff1d20bd798402b68f72c1ebbdd/src/diffusers/utils/testing_utils.py#L861" rel="nofollow">enable_full_determinism</a> function to enable deterministic algorithms.</p> <!> <p>Under the hood, <code>enable_full_determinism</code> works by:</p> <ul><li>Setting the environment variable <a href="https://docs.nvidia.com/cuda/cublas/index.html#results-reproducibility" rel="nofollow">CUBLAS_WORKSPACE_CONFIG</a> to <code>:16:8</code> to only use one buffer size during rntime. Non-deterministic behavior occurs when operations are used in more than one CUDA stream.</li> <li>Disabling benchmarking to find the fastest convolution operation by setting <code>torch.backends.cudnn.benchmark=False</code>. Non-deterministic behavior occurs because the benchmark may select different algorithms each time depending on hardware or benchmarking noise.</li> <li>Disabling TensorFloat32 (TF32) operations in favor of more precise and consistent full-precision operations.</li></ul> <!> <p>We strongly recommend reading PyTorch’s developer notes about <a href="https://docs.pytorch.org/docs/stable/notes/randomness.html" rel="nofollow">Reproducibility</a>. You can try to limit randomness, but it is not <em>guaranteed</em> even with an identical seed.</p> <!> <p></p>',1);function K(j,U){B(U,!1),C(()=>{new URLSearchParams(window.location.search).get("fw")}),G();var c=Y();W("1liluga",t=>{var i=S();I(i,"content",E),a(t,i)});var h=e(l(c),2);R(h,{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"});var m=e(h,2);o(m,{title:"Reproducibility",local:"reproducibility",headingTag:"h1"});var u=e(m,6);o(u,{title:"Generator",local:"generator",headingTag:"h2"});var g=e(u,6);k(g,{id:"generator",options:["GPU","CPU"],children:(t,i)=>{var M=D(),J=l(M);_(J,{id:"generator",option:"GPU",children:(d,v)=>{var s=F(),p=e(l(s),4);r(p,{code:"aW1wb3J0JTIwdG9yY2glMEFpbXBvcnQlMjBudW1weSUyMGFzJTIwbnAlMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwRERJTVBpcGVsaW5lJTBBJTBBZGRpbSUyMCUzRCUyMERESU1QaXBlbGluZS5mcm9tX3ByZXRyYWluZWQoJTIyZ29vZ2xlJTJGZGRwbS1jaWZhcjEwLTMyJTIyJTJDJTIwZGV2aWNlX21hcCUzRCUyMmN1ZGElMjIpJTBBZ2VuZXJhdG9yJTIwJTNEJTIwdG9yY2gubWFudWFsX3NlZWQoMCklMEFpbWFnZSUyMCUzRCUyMGRkaW0obnVtX2luZmVyZW5jZV9zdGVwcyUzRDIlMkMlMjBvdXRwdXRfdHlwZSUzRCUyMm5wJTIyJTJDJTIwZ2VuZXJhdG9yJTNEZ2VuZXJhdG9yKS5pbWFnZXMlMEFwcmludChucC5hYnMoaW1hZ2UpLnN1bSgpKQ==",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DDIMPipeline
ddim = DDIMPipeline.from_pretrained(<span class="hljs-string">&quot;google/ddpm-cifar10-32&quot;</span>, device_map=<span class="hljs-string">&quot;cuda&quot;</span>)
generator = torch.manual_seed(<span class="hljs-number">0</span>)
image = ddim(num_inference_steps=<span class="hljs-number">2</span>, output_type=<span class="hljs-string">&quot;np&quot;</span>, generator=generator).images
<span class="hljs-built_in">print</span>(np.<span class="hljs-built_in">abs</span>(image).<span class="hljs-built_in">sum</span>())`,lang:"py",wrap:!1}),a(d,s)},$$slots:{default:!0}});var Z=e(J,2);_(Z,{id:"generator",option:"CPU",children:(d,v)=>{var s=P(),p=e(l(s),2);r(p,{code:"aW1wb3J0JTIwdG9yY2glMEFpbXBvcnQlMjBudW1weSUyMGFzJTIwbnAlMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwRERJTVBpcGVsaW5lJTBBJTBBZGRpbSUyMCUzRCUyMERESU1QaXBlbGluZS5mcm9tX3ByZXRyYWluZWQoJTIyZ29vZ2xlJTJGZGRwbS1jaWZhcjEwLTMyJTIyKSUwQWdlbmVyYXRvciUyMCUzRCUyMHRvcmNoLkdlbmVyYXRvcihkZXZpY2UlM0QlMjJjcHUlMjIpLm1hbnVhbF9zZWVkKDApJTBBaW1hZ2UlMjAlM0QlMjBkZGltKG51bV9pbmZlcmVuY2Vfc3RlcHMlM0QyJTJDJTIwb3V0cHV0X3R5cGUlM0QlMjJucCUyMiUyQyUyMGdlbmVyYXRvciUzRGdlbmVyYXRvcikuaW1hZ2VzJTBBcHJpbnQobnAuYWJzKGltYWdlKS5zdW0oKSk=",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DDIMPipeline
ddim = DDIMPipeline.from_pretrained(<span class="hljs-string">&quot;google/ddpm-cifar10-32&quot;</span>)
generator = torch.Generator(device=<span class="hljs-string">&quot;cpu&quot;</span>).manual_seed(<span class="hljs-number">0</span>)
image = ddim(num_inference_steps=<span class="hljs-number">2</span>, output_type=<span class="hljs-string">&quot;np&quot;</span>, generator=generator).images
<span class="hljs-built_in">print</span>(np.<span class="hljs-built_in">abs</span>(image).<span class="hljs-built_in">sum</span>())`,lang:"py",wrap:!1}),a(d,s)},$$slots:{default:!0}}),a(t,M)},$$slots:{default:!0}});var b=e(g,4);r(b,{code:"Z2VuZXJhdG9yJTIwJTNEJTIwdG9yY2gubWFudWFsX3NlZWQoMCklMEElMEFmb3IlMjBfJTIwaW4lMjByYW5nZSg1KSUzQSUwQS0lMjAlMjAlMjAlMjBpbWFnZSUyMCUzRCUyMHBpcGVsaW5lKHByb21wdCUyQyUyMGdlbmVyYXRvciUzRGdlbmVyYXRvciklMEElMkIlMjAlMjAlMjAlMjBpbWFnZSUyMCUzRCUyMHBpcGVsaW5lKHByb21wdCUyQyUyMGdlbmVyYXRvciUzRHRvcmNoLm1hbnVhbF9zZWVkKDApKQ==",highlighted:`generator = torch.manual_seed(<span class="hljs-number">0</span>)
<span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">5</span>):
- image = pipeline(prompt, generator=generator)
+ image = pipeline(prompt, generator=torch.manual_seed(<span class="hljs-number">0</span>))`,lang:"py",wrap:!1});var f=e(b,2);o(f,{title:"Deterministic algorithms",local:"deterministic-algorithms",headingTag:"h2"});var y=e(f,6);r(y,{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzX3V0aWxzJTIwaW1wb3J0JTIwZW5hYmxlX2Z1bGxfZGV0ZXJtaW5pc20lMEElMEFlbmFibGVfZnVsbF9kZXRlcm1pbmlzbSgp",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers_utils <span class="hljs-keyword">import</span> enable_full_determinism
enable_full_determinism()`,lang:"py",wrap:!1});var w=e(y,6);o(w,{title:"Resources",local:"resources",headingTag:"h2"});var T=e(w,4);V(T,{source:"https://github.com/huggingface/diffusers/blob/main/docs/source/en/using-diffusers/reusing_seeds.md"}),z(2),a(j,c),X()}export{K as component};

Xet Storage Details

Size:
9.36 kB
·
Xet hash:
f7d1ceb582ad43326cb947e2a1ea8481d928c0ac7c9fc86f192fb3bcc4b35efb

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