Buckets:

rtrm's picture
download
raw
25.3 kB
<meta charset="utf-8" /><meta http-equiv="content-security-policy" content=""><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;cpu&quot;,&quot;title&quot;:&quot;CPU&quot;},{&quot;local&quot;:&quot;gpu&quot;,&quot;title&quot;:&quot;GPU&quot;}],&quot;title&quot;:&quot;λ¬΄μž‘μœ„μ„± μ œμ–΄ν•˜κΈ°&quot;},{&quot;local&quot;:&quot;&quot;,&quot;title&quot;:&quot;결정둠적 μ•Œκ³ λ¦¬μ¦˜&quot;}],&quot;title&quot;:&quot;μž¬ν˜„ κ°€λŠ₯ν•œ νŒŒμ΄ν”„λΌμΈ μƒμ„±ν•˜κΈ°&quot;}" data-svelte="svelte-1phssyn">
<link rel="modulepreload" href="/docs/diffusers/v0.21.0/ko/_app/assets/pages/__layout.svelte-hf-doc-builder.css">
<link rel="modulepreload" href="/docs/diffusers/v0.21.0/ko/_app/start-hf-doc-builder.js">
<link rel="modulepreload" href="/docs/diffusers/v0.21.0/ko/_app/chunks/vendor-hf-doc-builder.js">
<link rel="modulepreload" href="/docs/diffusers/v0.21.0/ko/_app/chunks/paths-hf-doc-builder.js">
<link rel="modulepreload" href="/docs/diffusers/v0.21.0/ko/_app/pages/__layout.svelte-hf-doc-builder.js">
<link rel="modulepreload" href="/docs/diffusers/v0.21.0/ko/_app/pages/using-diffusers/reproducibility.mdx-hf-doc-builder.js">
<link rel="modulepreload" href="/docs/diffusers/v0.21.0/ko/_app/chunks/Tip-hf-doc-builder.js">
<link rel="modulepreload" href="/docs/diffusers/v0.21.0/ko/_app/chunks/IconCopyLink-hf-doc-builder.js">
<link rel="modulepreload" href="/docs/diffusers/v0.21.0/ko/_app/chunks/CodeBlock-hf-doc-builder.js">
<link rel="modulepreload" href="/docs/diffusers/v0.21.0/ko/_app/chunks/DocNotebookDropdown-hf-doc-builder.js">
<h1 id="">μž¬ν˜„ κ°€λŠ₯ν•œ νŒŒμ΄ν”„λΌμΈ μƒμ„±ν•˜κΈ°</h1>
<div class="flex space-x-1 absolute z-10 right-0 top-0">
<div class="relative colab-dropdown ">
<button class=" " type="button">
<img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg">
</button>
</div>
<div class="relative colab-dropdown ">
<button class=" " type="button">
<img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg">
</button>
</div></div>
<p>μž¬ν˜„μ„±μ€ ν…ŒμŠ€νŠΈ, κ²°κ³Ό μž¬ν˜„, 그리고 <a href="resuing_seeds">이미지 퀄리티 높이기</a>μ—μ„œ μ€‘μš”ν•©λ‹ˆλ‹€.
κ·ΈλŸ¬λ‚˜ diffusion λͺ¨λΈμ˜ λ¬΄μž‘μœ„μ„±μ€ 맀번 λͺ¨λΈμ΄ λŒμ•„κ°ˆ λ•Œλ§ˆλ‹€ νŒŒμ΄ν”„λΌμΈμ΄ λ‹€λ₯Έ 이미지λ₯Ό 생성할 수 μžˆλ„λ‘ ν•˜λŠ” 이유둜 ν•„μš”ν•©λ‹ˆλ‹€.
ν”Œλž«νΌ 간에 μ •ν™•ν•˜κ²Œ λ™μΌν•œ κ²°κ³Όλ₯Ό 얻을 μˆ˜λŠ” μ—†μ§€λ§Œ, νŠΉμ • ν—ˆμš© λ²”μœ„ λ‚΄μ—μ„œ 릴리슀 및 ν”Œλž«νΌ 간에 κ²°κ³Όλ₯Ό μž¬ν˜„ν•  μˆ˜λŠ” μžˆμŠ΅λ‹ˆλ‹€.
κ·ΈλŸΌμ—λ„ diffusion νŒŒμ΄ν”„λΌμΈκ³Ό μ²΄ν¬ν¬μΈνŠΈμ— 따라 ν—ˆμš© μ˜€μ°¨κ°€ λ‹¬λΌμ§‘λ‹ˆλ‹€.</p>
<p>diffusion λͺ¨λΈμ—μ„œ λ¬΄μž‘μœ„μ„±μ˜ μ›μ²œμ„ μ œμ–΄ν•˜κ±°λ‚˜ 결정둠적 μ•Œκ³ λ¦¬μ¦˜μ„ μ‚¬μš©ν•˜λŠ” 방법을 μ΄ν•΄ν•˜λŠ” 것이 μ€‘μš”ν•œ μ΄μœ μž…λ‹ˆλ‹€.</p>
<div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>πŸ’‘ Pytorch의 <a href="https://pytorch.org/docs/stable/notes/randomness.html" rel="nofollow">μž¬ν˜„μ„±μ— λŒ€ν•œ μ„ μ–Έ</a>λ₯Ό κΌ­ 읽어보길 μΆ”μ²œν•©λ‹ˆλ‹€:</p>
<blockquote><p>μ™„μ „ν•˜κ²Œ μž¬ν˜„κ°€λŠ₯ν•œ κ²°κ³ΌλŠ” Pytorch 배포, κ°œλ³„μ μΈ 컀밋, ν˜Ήμ€ λ‹€λ₯Έ ν”Œλž«νΌλ“€μ—μ„œ 보μž₯λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
λ˜ν•œ, κ²°κ³ΌλŠ” CPU와 GPU 싀행간에 심지어 같은 seedλ₯Ό μ‚¬μš©ν•  λ•Œλ„ μž¬ν˜„ κ°€λŠ₯ν•˜μ§€ μ•Šμ„ 수 μžˆμŠ΅λ‹ˆλ‹€.</p></blockquote></div>
<h2 id="">λ¬΄μž‘μœ„μ„± μ œμ–΄ν•˜κΈ°</h2>
<p>μΆ”λ‘ μ—μ„œ, νŒŒμ΄ν”„λΌμΈμ€ λ…Έμ΄μ¦ˆλ₯Ό 쀄이기 μœ„ν•΄ κ°€μš°μ‹œμ•ˆ λ…Έμ΄μ¦ˆλ₯Ό μƒμ„±ν•˜κ±°λ‚˜ μŠ€μΌ€μ€„λ§ 단계에 λ…Έμ΄μ¦ˆλ₯Ό λ”ν•˜λŠ” λ“±μ˜ 랜덀 μƒ˜ν”Œλ§ 싀행에 크게 μ˜μ‘΄ν•©λ‹ˆλ‹€,</p>
<p><a href="https://huggingface.co/docs/diffusers/v0.18.0/en/api/pipelines/ddim#diffusers.DDIMPipeline" rel="nofollow">DDIMPipeline</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 class="" 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>
Copied</div></button></div>
<pre><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DDIMPipeline
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
model_id = <span class="hljs-string">&quot;google/ddpm-cifar10-32&quot;</span>
<span class="hljs-comment"># λͺ¨λΈκ³Ό μŠ€μΌ€μ€„λŸ¬λ₯Ό 뢈러였기</span>
ddim = DDIMPipeline.from_pretrained(model_id)
<span class="hljs-comment"># 두 개의 단계에 λŒ€ν•΄μ„œ νŒŒμ΄ν”„λΌμΈμ„ μ‹€ν–‰ν•˜κ³  numpy tensor둜 값을 λ°˜ν™˜ν•˜κΈ°</span>
image = ddim(num_inference_steps=<span class="hljs-number">2</span>, output_type=<span class="hljs-string">&quot;np&quot;</span>).images
<span class="hljs-built_in">print</span>(np.<span class="hljs-built_in">abs</span>(image).<span class="hljs-built_in">sum</span>())<!-- HTML_TAG_END --></pre></div>
<p>μœ„μ˜ μ½”λ“œλ₯Ό μ‹€ν–‰ν•˜λ©΄ ν•˜λ‚˜μ˜ 값이 λ‚˜μ˜€μ§€λ§Œ, λ‹€μ‹œ μ‹€ν–‰ν•˜λ©΄ λ‹€λ₯Έ 값이 λ‚˜μ˜΅λ‹ˆλ‹€. 무슨 일이 μΌμ–΄λ‚˜κ³  μžˆλŠ” κ±ΈκΉŒμš”?</p>
<p>νŒŒμ΄ν”„λΌμΈμ΄ 싀행될 λ•Œλ§ˆλ‹€, <a href="https://pytorch.org/docs/stable/generated/torch.randn.html" rel="nofollow">torch.randn</a>은
λ‹¨κ³„μ μœΌλ‘œ λ…Έμ΄μ¦ˆ μ œκ±°λ˜λŠ” κ°€μš°μ‹œμ•ˆ λ…Έμ΄μ¦ˆκ°€ μƒμ„±ν•˜κΈ° μœ„ν•œ λ‹€λ₯Έ 랜덀 seedλ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€.</p>
<p>κ·ΈλŸ¬λ‚˜ λ™μΌν•œ 이미지λ₯Ό μ•ˆμ •μ μœΌλ‘œ 생성해야 ν•˜λŠ” κ²½μš°μ—λŠ” CPUμ—μ„œ νŒŒμ΄ν”„λΌμΈμ„ μ‹€ν–‰ν•˜λŠ”μ§€ GPUμ—μ„œ μ‹€ν–‰ν•˜λŠ”μ§€μ— 따라 λ‹¬λΌμ§‘λ‹ˆλ‹€.</p>
<h3 class="relative group"><a id="cpu" 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"><span><svg class="" 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>CPU
</span></h3>
<p>CPUμ—μ„œ μž¬ν˜„ κ°€λŠ₯ν•œ κ²°κ³Όλ₯Ό μƒμ„±ν•˜λ €λ©΄, PyTorch <a href="https://pytorch.org/docs/stable/generated/torch.randn.html" rel="nofollow">Generator</a>둜 seedλ₯Ό κ³ μ •ν•©λ‹ˆλ‹€:</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 class="" 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>
Copied</div></button></div>
<pre><!-- HTML_TAG_START --><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DDIMPipeline
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
model_id = <span class="hljs-string">&quot;google/ddpm-cifar10-32&quot;</span>
<span class="hljs-comment"># λͺ¨λΈκ³Ό μŠ€μΌ€μ€„λŸ¬ 뢈러였기</span>
ddim = DDIMPipeline.from_pretrained(model_id)
<span class="hljs-comment"># μž¬ν˜„μ„±μ„ μœ„ν•΄ generator λ§Œλ“€κΈ°</span>
generator = torch.Generator(device=<span class="hljs-string">&quot;cpu&quot;</span>).manual_seed(<span class="hljs-number">0</span>)
<span class="hljs-comment"># 두 개의 단계에 λŒ€ν•΄μ„œ νŒŒμ΄ν”„λΌμΈμ„ μ‹€ν–‰ν•˜κ³  numpy tensor둜 값을 λ°˜ν™˜ν•˜κΈ°</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>())<!-- HTML_TAG_END --></pre></div>
<p>이제 μœ„μ˜ μ½”λ“œλ₯Ό μ‹€ν–‰ν•˜λ©΄ seedλ₯Ό κ°€μ§„ <code>Generator</code> 객체가 νŒŒμ΄ν”„λΌμΈμ˜ λͺ¨λ“  랜덀 ν•¨μˆ˜μ— μ „λ‹¬λ˜λ―€λ‘œ 항상 <code>1491.1711</code> 값이 좜λ ₯λ©λ‹ˆλ‹€.</p>
<p>νŠΉμ • ν•˜λ“œμ›¨μ–΄ 및 PyTorch λ²„μ „μ—μ„œ 이 μ½”λ“œ 예제λ₯Ό μ‹€ν–‰ν•˜λ©΄ λ™μΌν•˜μ§€λŠ” μ•Šλ”λΌλ„ μœ μ‚¬ν•œ κ²°κ³Όλ₯Ό 얻을 수 μžˆμŠ΅λ‹ˆλ‹€.</p>
<div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>πŸ’‘ μ²˜μŒμ—λŠ” μ‹œλ“œλ₯Ό λ‚˜νƒ€λ‚΄λŠ” μ •μˆ˜κ°’ λŒ€μ‹ μ— <code>Generator</code> 개체λ₯Ό νŒŒμ΄ν”„λΌμΈμ— μ „λ‹¬ν•˜λŠ” 것이 μ•½κ°„ 비직관적일 수 μžˆμ§€λ§Œ,
<code>Generator</code>λŠ” 순차적으둜 μ—¬λŸ¬ νŒŒμ΄ν”„λΌμΈμ— 전달될 수 μžˆλŠ” \λžœλ€μƒνƒœ\이기 λ•Œλ¬Έμ— PyTorchμ—μ„œ ν™•λ₯ λ‘ μ  λͺ¨λΈμ„ λ‹€λ£° λ•Œ ꢌμž₯λ˜λŠ” μ„€κ³„μž…λ‹ˆλ‹€.</p></div>
<h3 class="relative group"><a id="gpu" 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="#gpu"><span><svg class="" 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>GPU
</span></h3>
<p>예λ₯Ό λ“€λ©΄, GPU μƒμ—μ„œ 같은 μ½”λ“œ μ˜ˆμ‹œλ₯Ό μ‹€ν–‰ν•˜λ©΄:</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 class="" 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>
Copied</div></button></div>
<pre><!-- HTML_TAG_START --><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DDIMPipeline
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
model_id = <span class="hljs-string">&quot;google/ddpm-cifar10-32&quot;</span>
<span class="hljs-comment"># λͺ¨λΈκ³Ό μŠ€μΌ€μ€„λŸ¬ 뢈러였기</span>
ddim = DDIMPipeline.from_pretrained(model_id)
ddim.to(<span class="hljs-string">&quot;cuda&quot;</span>)
<span class="hljs-comment"># μž¬ν˜„μ„±μ„ μœ„ν•œ generator λ§Œλ“€κΈ°</span>
generator = torch.Generator(device=<span class="hljs-string">&quot;cuda&quot;</span>).manual_seed(<span class="hljs-number">0</span>)
<span class="hljs-comment"># 두 개의 단계에 λŒ€ν•΄μ„œ νŒŒμ΄ν”„λΌμΈμ„ μ‹€ν–‰ν•˜κ³  numpy tensor둜 값을 λ°˜ν™˜ν•˜κΈ°</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>())<!-- HTML_TAG_END --></pre></div>
<p>GPUκ°€ CPU와 λ‹€λ₯Έ λ‚œμˆ˜ 생성기λ₯Ό μ‚¬μš©ν•˜κΈ° λ•Œλ¬Έμ— λ™μΌν•œ μ‹œλ“œλ₯Ό μ‚¬μš©ν•˜λ”λΌλ„ κ²°κ³Όκ°€ κ°™μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.</p>
<p>이 문제λ₯Ό ν”Όν•˜κΈ° μœ„ν•΄ 🧨 DiffusersλŠ” CPU에 μž„μ˜μ˜ λ…Έμ΄μ¦ˆλ₯Ό μƒμ„±ν•œ λ‹€μŒ ν•„μš”μ— 따라 ν…μ„œλ₯Ό GPU둜 μ΄λ™μ‹œν‚€λŠ”
<a href="https://huggingface.co/docs/diffusers/v0.18.0/en/api/utilities#diffusers.utils.randn_tensor" rel="nofollow">randn_tensor()</a>κΈ°λŠ₯을 κ°€μ§€κ³  μžˆμŠ΅λ‹ˆλ‹€.
<code>randn_tensor</code> κΈ°λŠ₯은 νŒŒμ΄ν”„λΌμΈ λ‚΄λΆ€ μ–΄λ””μ—μ„œλ‚˜ μ‚¬μš©λ˜λ―€λ‘œ νŒŒμ΄ν”„λΌμΈμ΄ GPUμ—μ„œ μ‹€ν–‰λ˜λ”λΌλ„ <strong>항상</strong> CPU <code>Generator</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 class="" 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>
Copied</div></button></div>
<pre><!-- HTML_TAG_START --><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DDIMPipeline
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
model_id = <span class="hljs-string">&quot;google/ddpm-cifar10-32&quot;</span>
<span class="hljs-comment"># λͺ¨λΈκ³Ό μŠ€μΌ€μ€„λŸ¬ 뢈러였기</span>
ddim = DDIMPipeline.from_pretrained(model_id)
ddim.to(<span class="hljs-string">&quot;cuda&quot;</span>)
<span class="hljs-comment">#μž¬ν˜„μ„±μ„ μœ„ν•œ generator λ§Œλ“€κΈ° (GPU에 μ˜¬λ¦¬μ§€ μ•Šλ„λ‘ μ‘°μ‹¬ν•œλ‹€!)</span>
generator = torch.manual_seed(<span class="hljs-number">0</span>)
<span class="hljs-comment"># 두 개의 단계에 λŒ€ν•΄μ„œ νŒŒμ΄ν”„λΌμΈμ„ μ‹€ν–‰ν•˜κ³  numpy tensor둜 값을 λ°˜ν™˜ν•˜κΈ°</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>())<!-- HTML_TAG_END --></pre></div>
<div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>πŸ’‘ μž¬ν˜„μ„±μ΄ μ€‘μš”ν•œ κ²½μš°μ—λŠ” 항상 CPU generatorλ₯Ό μ „λ‹¬ν•˜λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€.
μ„±λŠ₯ 손싀은 λ¬΄μ‹œν•  수 μ—†λŠ” κ²½μš°κ°€ 많으며 νŒŒμ΄ν”„λΌμΈμ΄ GPUμ—μ„œ μ‹€ν–‰λ˜μ—ˆμ„ λ•Œλ³΄λ‹€ 훨씬 더 λΉ„μŠ·ν•œ 값을 생성할 수 μžˆμŠ΅λ‹ˆλ‹€.</p></div>
<p>λ§ˆμ§€λ§‰μœΌλ‘œ <a href="https://huggingface.co/docs/diffusers/v0.18.0/en/api/pipelines/unclip#diffusers.UnCLIPPipeline" rel="nofollow">UnCLIPPipeline</a>κ³Ό 같은
더 λ³΅μž‘ν•œ νŒŒμ΄ν”„λΌμΈμ˜ 경우, 이듀은 μ’…μ’… μ •λ°€ 였차 μ „νŒŒμ— κ·Ήλ„λ‘œ μ·¨μ•½ν•©λ‹ˆλ‹€.
λ‹€λ₯Έ GPU ν•˜λ“œμ›¨μ–΄ λ˜λŠ” PyTorch λ²„μ „μ—μ„œ μœ μ‚¬ν•œ κ²°κ³Όλ₯Ό κΈ°λŒ€ν•˜μ§€ λ§ˆμ„Έμš”.
이 경우 μ™„μ „ν•œ μž¬ν˜„μ„±μ„ μœ„ν•΄ μ™„μ „νžˆ λ™μΌν•œ ν•˜λ“œμ›¨μ–΄ 및 PyTorch 버전을 μ‹€ν–‰ν•΄μ•Ό ν•©λ‹ˆλ‹€.</p>
<h2 id="">결정둠적 μ•Œκ³ λ¦¬μ¦˜</h2>
<p>결정둠적 μ•Œκ³ λ¦¬μ¦˜μ„ μ‚¬μš©ν•˜μ—¬ μž¬ν˜„ κ°€λŠ₯ν•œ νŒŒμ΄ν”„λΌμΈμ„ μƒμ„±ν•˜λ„λ‘ PyTorchλ₯Ό ꡬ성할 μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.
κ·ΈλŸ¬λ‚˜ 결정둠적 μ•Œκ³ λ¦¬μ¦˜μ€ 비결정둠적 μ•Œκ³ λ¦¬μ¦˜λ³΄λ‹€ 느리고 μ„±λŠ₯이 μ €ν•˜λ  수 μžˆμŠ΅λ‹ˆλ‹€.
ν•˜μ§€λ§Œ μž¬ν˜„μ„±μ΄ μ€‘μš”ν•˜λ‹€λ©΄, 이것이 μ΅œμ„ μ˜ λ°©λ²•μž…λ‹ˆλ‹€!</p>
<p>λ‘˜ μ΄μƒμ˜ CUDA μŠ€νŠΈλ¦Όμ—μ„œ μž‘μ—…μ΄ μ‹œμž‘λ  λ•Œ 비결정둠적 λ™μž‘μ΄ λ°œμƒν•©λ‹ˆλ‹€.
이 문제λ₯Ό λ°©μ§€ν•˜λ €λ©΄ ν™˜κ²½ λ³€μˆ˜ <a href="https://docs.nvidia.com/cuda/cublas/index.html#results-reproducibility" rel="nofollow">CUBLAS_WORKSPACE_CONFIG</a>λ₯Ό <code>:16:8</code>둜 μ„€μ •ν•΄μ„œ
λŸ°νƒ€μž„ 쀑에 였직 ν•˜λ‚˜μ˜ 버퍼 크리만 μ‚¬μš©ν•˜λ„λ‘ μ„€μ •ν•©λ‹ˆλ‹€.</p>
<p>PyTorchλŠ” 일반적으둜 κ°€μž₯ λΉ λ₯Έ μ•Œκ³ λ¦¬μ¦˜μ„ μ„ νƒν•˜κΈ° μœ„ν•΄ μ—¬λŸ¬ μ•Œκ³ λ¦¬μ¦˜μ„ λ²€μΉ˜λ§ˆν‚Ήν•©λ‹ˆλ‹€.
ν•˜μ§€λ§Œ μž¬ν˜„μ„±μ„ μ›ν•˜λŠ” 경우, λ²€μΉ˜λ§ˆν¬κ°€ λ§€ μˆœκ°„ λ‹€λ₯Έ μ•Œκ³ λ¦¬μ¦˜μ„ 선택할 수 있기 λ•Œλ¬Έμ— 이 κΈ°λŠ₯을 μ‚¬μš©ν•˜μ§€ μ•Šλ„λ‘ μ„€μ •ν•΄μ•Ό ν•©λ‹ˆλ‹€.
λ§ˆμ§€λ§‰μœΌλ‘œ, <a href="https://pytorch.org/docs/stable/generated/torch.use_deterministic_algorithms.html" rel="nofollow">torch.use_deterministic_algorithms</a>에
<code>True</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 class="" 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>
Copied</div></button></div>
<pre><!-- HTML_TAG_START --><span class="hljs-keyword">import</span> os
os.environ[<span class="hljs-string">&quot;CUBLAS_WORKSPACE_CONFIG&quot;</span>] = <span class="hljs-string">&quot;:16:8&quot;</span>
torch.backends.cudnn.benchmark = <span class="hljs-literal">False</span>
torch.use_deterministic_algorithms(<span class="hljs-literal">True</span>)<!-- HTML_TAG_END --></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 class="" 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>
Copied</div></button></div>
<pre><!-- HTML_TAG_START --><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DDIMScheduler, StableDiffusionPipeline
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
model_id = <span class="hljs-string">&quot;runwayml/stable-diffusion-v1-5&quot;</span>
pipe = StableDiffusionPipeline.from_pretrained(model_id).to(<span class="hljs-string">&quot;cuda&quot;</span>)
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
g = torch.Generator(device=<span class="hljs-string">&quot;cuda&quot;</span>)
prompt = <span class="hljs-string">&quot;A bear is playing a guitar on Times Square&quot;</span>
g.manual_seed(<span class="hljs-number">0</span>)
result1 = pipe(prompt=prompt, num_inference_steps=<span class="hljs-number">50</span>, generator=g, output_type=<span class="hljs-string">&quot;latent&quot;</span>).images
g.manual_seed(<span class="hljs-number">0</span>)
result2 = pipe(prompt=prompt, num_inference_steps=<span class="hljs-number">50</span>, generator=g, output_type=<span class="hljs-string">&quot;latent&quot;</span>).images
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;L_inf dist = &quot;</span>, <span class="hljs-built_in">abs</span>(result1 - result2).<span class="hljs-built_in">max</span>())
<span class="hljs-string">&quot;L_inf dist = tensor(0., device=&#x27;cuda:0&#x27;)&quot;</span><!-- HTML_TAG_END --></pre></div>
<script type="module" data-hydrate="kk57mo">
import { start } from "/docs/diffusers/v0.21.0/ko/_app/start-hf-doc-builder.js";
start({
target: document.querySelector('[data-hydrate="kk57mo"]').parentNode,
paths: {"base":"/docs/diffusers/v0.21.0/ko","assets":"/docs/diffusers/v0.21.0/ko"},
session: {},
route: false,
spa: false,
trailing_slash: "never",
hydrate: {
status: 200,
error: null,
nodes: [
import("/docs/diffusers/v0.21.0/ko/_app/pages/__layout.svelte-hf-doc-builder.js"),
import("/docs/diffusers/v0.21.0/ko/_app/pages/using-diffusers/reproducibility.mdx-hf-doc-builder.js")
],
params: {}
}
});
</script>

Xet Storage Details

Size:
25.3 kB
Β·
Xet hash:
9881fb8ce36b511bb518ff8089cdb478dd842e4add44b0e5047e07ec5c5c3140

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