Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"Attention 백엔드","local":"attention-backends","sections":[{"title":"set_attention_backend","local":"setattentionbackend","sections":[],"depth":2},{"title":"attention_backend 컨텍스트 매니저","local":"attentionbackend-context-manager","sections":[],"depth":2},{"title":"원격 커널에 대한 신뢰","local":"trusting-remote-kernels","sections":[],"depth":2},{"title":"확인","local":"checks","sections":[],"depth":2},{"title":"사용 가능한 백엔드","local":"available-backends","sections":[],"depth":2}],"depth":1}"/> | |
| <link href="/docs/diffusers/main/en/_app/immutable/entry/start.vZc-dOUR.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/Bafuik5c.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.B6c3Ml05.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.BQlx092V.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/326.qHPbgXyf.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/B4AMoTAu.js" rel="modulepreload"> | |
| <!--gxcprv--><meta name="hf:doc:metadata" content="{"title":"Attention backends","local":"attention-backends","sections":[{"title":"set_attention_backend","local":"setattentionbackend","sections":[],"depth":2},{"title":"attention_backend context manager","local":"attentionbackend-context-manager","sections":[],"depth":2},{"title":"Trusting remote kernels","local":"trusting-remote-kernels","sections":[],"depth":2},{"title":"Checks","local":"checks","sections":[],"depth":2},{"title":"Available backends","local":"available-backends","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="attention-backends" 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="#attention-backends"><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><!---->Attention 백엔드<!----></span></h1><!--]--><!----> <blockquote class="note"><p>Attention dispatcher는 실험적 기능입니다. 피드백이 있거나 문제가 발생하면 이슈를 열어 주세요.</p></blockquote> <p>Diffusers는 <em>attention dispatcher</em>를 통해 메모리와 계산 효율성이 더 높은 여러 최적화된 attention 알고리즘을 제공합니다. dispatcher는 다양한 attention 구현을 관리하고 전환하는 라우터 역할을 하며, 이러한 구현과 상호작용하기 위한 통합된 인터페이스를 제공합니다.</p> <p>사용 가능한 어텐션 패밀리 개요는 아래 표를, 더 완전한 목록은 <a href="#available-backends">사용 가능한 백엔드</a> 섹션을 참조하세요.</p> <table><thead><tr><th>attention 계열</th><th>주요 기능</th></tr></thead><tbody><tr><td>FlashAttention</td><td>tiling과 recomputation을 통해 메모리 읽기/쓰기를 최소화합니다</td></tr><tr><td>ROCm용 AI Tensor Engine</td><td>AMD ROCm 가속기를 위해 최적화된 FlashAttention 구현</td></tr><tr><td>SageAttention</td><td>quantizes attention to int8</td></tr><tr><td>PyTorch 네이티브</td><td><a href="./fp16#scaled-dot-product-attention">scaled_dot_product_attention</a>를 사용하는 내장 PyTorch 구현</td></tr><tr><td>xFormers</td><td>memory-efficient attention 및 다양한 attention kernel 지원</td></tr></tbody></table> <p>이 가이드에서는 다양한 attention backends를 설정하고 사용하는 방법을 보여줍니다.</p> <!--[1--><h2 class="relative group"><a id="setattentionbackend" 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="#setattentionbackend"><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><!---->set_attention_backend<!----></span></h2><!--]--><!----> <p><a href="/docs/diffusers/main/ko/api/models/overview#diffusers.ModelMixin.set_attention_backend">set_attention_backend()</a> 메서드는 모델의 모든 모듈을 순회하며 사용할 적절한 attention backend를 설정합니다. attention backend 설정은 <a href="/docs/diffusers/main/ko/api/models/overview#diffusers.ModelMixin.reset_attention_backend">reset_attention_backend()</a>이 호출될 때까지 유지됩니다.</p> <p>아래 예시는 <a href="https://github.com/huggingface/kernels" rel="nofollow"><code>kernels</code></a> 라이브러리에서 FlashAttention-3의 <code>_flash_3_hub</code> 구현을 활성화하는 방법을 보여줍니다. 이를 통해 설정 과정 없이 Hub의 최적화된 compute kernel을 즉시 사용할 수 있습니다.</p> <blockquote class="note"><p>FlashAttention-3는 최소 Ampere GPU가 필요합니다.</p></blockquote> <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> QwenImagePipeline | |
| pipeline = QwenImagePipeline.from_pretrained( | |
| <span class="hljs-string">"Qwen/Qwen-Image"</span>, dtype=torch.bfloat16, device_map=<span class="hljs-string">"cuda"</span> <span class="hljs-comment"># or "mps", "xpu", "cpu"</span> | |
| ) | |
| pipeline.transformer.set_attention_backend(<span class="hljs-string">"_flash_3_hub"</span>) | |
| prompt = <span class="hljs-string">""" | |
| cinematic film still of a cat sipping a margarita in a pool in Palm Springs, California | |
| highly detailed, high budget hollywood movie, cinemascope, moody, epic, gorgeous, film grain | |
| """</span> | |
| pipeline(prompt).images[<span class="hljs-number">0</span>]<!----></pre></div><!----> <p>기본 attention 백엔드를 복원하려면 <a href="/docs/diffusers/main/ko/api/models/overview#diffusers.ModelMixin.reset_attention_backend">reset_attention_backend()</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.reset_attention_backend()<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="attentionbackend-context-manager" 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="#attentionbackend-context-manager"><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><!---->attention_backend 컨텍스트 매니저<!----></span></h2><!--]--><!----> <p><a href="https://github.com/huggingface/diffusers/blob/5e181eddfe7e44c1444a2511b0d8e21d177850a0/src/diffusers/models/attention_dispatch.py#L225" rel="nofollow">attention_backend</a> 컨텍스트 매니저는 컨텍스트 내에서 모델의 어텐션 백엔드를 일시적으로 설정합니다. 컨텍스트 외부에서는 기본 어텐션(PyTorch의 네이티브 스케일드 도트 프로덕트 어텐션)이 사용됩니다. 파이프라인의 서로 다른 부분에 다른 백엔드를 사용하거나, 서로 다른 백엔드를 테스트하려는 경우에 유용합니다.</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> QwenImagePipeline | |
| pipeline = QwenImagePipeline.from_pretrained( | |
| <span class="hljs-string">"Qwen/Qwen-Image"</span>, dtype=torch.bfloat16, device_map=<span class="hljs-string">"cuda"</span> <span class="hljs-comment"># or "mps", "xpu", "cpu"</span> | |
| ) | |
| prompt = <span class="hljs-string">""" | |
| cinematic film still of a cat sipping a margarita in a pool in Palm Springs, California | |
| highly detailed, high budget hollywood movie, cinemascope, moody, epic, gorgeous, film grain | |
| """</span> | |
| <span class="hljs-keyword">with</span> attention_backend(<span class="hljs-string">"_flash_3_hub"</span>): | |
| image = pipeline(prompt).images[<span class="hljs-number">0</span>]<!----></pre></div><!----> <blockquote class="tip"><p>대부분의 attention 백엔드는 그래프 브레이크 없이 <code>torch.compile</code>을 지원하며, 이를 통해 추론 속도를 추가로 높일 수 있습니다.</p></blockquote> <!--[1--><h2 class="relative group"><a id="trusting-remote-kernels" 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="#trusting-remote-kernels"><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>Hub 백엔드 및 기타 커널 기반 기능(예: <a href="../quantization/gguf">GGUF</a>, <a href="../quantization/nunchaku">Nunchaku Lite</a>)은 <a href="https://github.com/huggingface/kernels" rel="nofollow"><code>kernels</code></a>를 통해 Hub에서 컴퓨트 커널을 다운로드하고 해당 코드를 로컬에서 실행합니다.</p> <p>기본적으로 <code>kernels</code>는 게시자가 Hub의 신뢰할 수 있는 커널 게시자인 경우에만 커널을 로드합니다. <a href="https://huggingface.co/kernels-community" rel="nofollow"><code>kernels-community</code></a> 조직 아래 게시된 커널은 신뢰할 수 있으므로 Diffusers는 추가 구성 없이 이를 로드합니다. <code>_flash_3_hub</code>, <code>flash_hub</code>, <code>sage_hub</code> 및 기타 Hub 어텐션 백엔드는 모두 <code>kernels-community</code> 저장소로 해석됩니다.</p> <p>다른 발행자의 Kernels은 검증되지 않습니다. 하나를 로드하면 Diffusers가 보증할 수 없는 코드가 다운로드되고 실행되므로, Diffusers는 <code>DIFFUSERS_TRUST_REMOTE_KERNELS</code> 환경 변수로 명시적으로 선택하지 않는 한 비활성화 상태로 유지합니다. 이 변수가 설정되면 Diffusers는 <code>kernels</code>에 <code>trust_remote_code=True</code>를 전달하여 신뢰할 수 없는 발행자의 Kernels도 로드합니다.</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 "><!----><span class="hljs-built_in">export</span> DIFFUSERS_TRUST_REMOTE_KERNELS=<span class="hljs-literal">true</span><!----></pre></div><!----> <p>커널 저장소를 검사한 후에만 이 옵션을 활성화하세요. 이 옵션은 다운로드된 코드가 사용자의 머신에서 실행될 수 있는 권한을 부여하기 때문입니다. 이 옵션이 없으면 신뢰할 수 없는 게시자로부터 커널을 로드하면 오류가 발생합니다. Diffusers는 이 검사를 자체적으로 수행하므로, <code>trust_remote_code</code> 인자가 도입되기 이전인 <code>kernels<0.14.0</code>에도 적용됩니다. <code>DIFFUSERS_DISABLE_REMOTE_CODE=true</code>를 설정하면 원격 코드가 전역적으로 비활성화되며, <code>DIFFUSERS_TRUST_REMOTE_KERNELS</code>보다 우선합니다.</p> <!--[1--><h2 class="relative group"><a id="checks" 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="#checks"><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>Attention dispatcher에는 문제가 발생하기 전에 일반적인 오류를 포착하는 디버깅 체크가 포함되어 있습니다.</p> <ol><li>Device checks는 query, key, value 텐서가 동일한 device에 존재하는지 확인합니다.</li> <li>Data type checks는 텐서가 동일한 dtype을 가지고 있는지 확인하며, bfloat16 또는 float16 중 하나를 사용해야 합니다.</li> <li>Shape checks는 텐서 차원을 검증하고 attention mask와 causal flag의 혼합을 방지합니다.</li></ol> <p><code>DIFFUSERS_ATTN_CHECKS</code> 환경 변수를 설정하여 이러한 검사를 활성화할 수 있습니다. 검사 기능은 모든 attention 연산에 오버헤드를 추가하므로 기본적으로 비활성화되어 있습니다.</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 "><!----><span class="hljs-built_in">export</span> DIFFUSERS_ATTN_CHECKS=<span class="hljs-built_in">yes</span><!----></pre></div><!----> <p>이제 모든 attention 연산 전에 체크가 실행됩니다.</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 | |
| query = torch.randn(<span class="hljs-number">1</span>, <span class="hljs-number">10</span>, <span class="hljs-number">8</span>, <span class="hljs-number">64</span>, dtype=torch.bfloat16, device=<span class="hljs-string">"cuda"</span>) <span class="hljs-comment"># or "mps", "xpu", "cpu"</span> | |
| key = torch.randn(<span class="hljs-number">1</span>, <span class="hljs-number">10</span>, <span class="hljs-number">8</span>, <span class="hljs-number">64</span>, dtype=torch.bfloat16, device=<span class="hljs-string">"cuda"</span>) | |
| value = torch.randn(<span class="hljs-number">1</span>, <span class="hljs-number">10</span>, <span class="hljs-number">8</span>, <span class="hljs-number">64</span>, dtype=torch.bfloat16, device=<span class="hljs-string">"cuda"</span>) | |
| <span class="hljs-keyword">try</span>: | |
| <span class="hljs-keyword">with</span> attention_backend(<span class="hljs-string">"flash"</span>): | |
| output = dispatch_attention_fn(query, key, value) | |
| <span class="hljs-built_in">print</span>(<span class="hljs-string">"✓ Flash Attention works with checks enabled"</span>) | |
| <span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e: | |
| <span class="hljs-built_in">print</span>(<span class="hljs-string">f"✗ Flash Attention failed: <span class="hljs-subst">{e}</span>"</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">from</span> diffusers.models.attention_dispatch <span class="hljs-keyword">import</span> _AttentionBackendRegistry | |
| _AttentionBackendRegistry._checks_enabled = <span class="hljs-literal">True</span><!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="available-backends" 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="#available-backends"><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>사용 가능한 어텐션 백엔드와 그 변형의 전체 목록은 아래 표를 참조하세요.</p> <details><summary>Expand</summary> <table><thead><tr><th>백엔드 이름</th><th>패밀리</th><th>설명</th></tr></thead><tbody><tr><td><code>native</code></td><td><a href="https://docs.pytorch.org/docs/stable/generated/torch.nn.attention.SDPBackend.html#torch.nn.attention.SDPBackend" rel="nofollow">PyTorch 네이티브</a></td><td>PyTorch의 scaled_dot_product_attention를 사용하는 기본 백엔드</td></tr><tr><td><code>flex</code></td><td><a href="https://docs.pytorch.org/docs/stable/nn.attention.flex_attention.html#module-torch.nn.attention.flex_attention" rel="nofollow">FlexAttention</a></td><td>PyTorch FlexAttention 구현</td></tr><tr><td><code>_native_cudnn</code></td><td><a href="https://docs.pytorch.org/docs/stable/generated/torch.nn.attention.SDPBackend.html#torch.nn.attention.SDPBackend" rel="nofollow">PyTorch 네이티브</a></td><td>CuDNN 최적화 어텐션</td></tr><tr><td><code>_native_efficient</code></td><td><a href="https://docs.pytorch.org/docs/stable/generated/torch.nn.attention.SDPBackend.html#torch.nn.attention.SDPBackend" rel="nofollow">PyTorch 네이티브</a></td><td>메모리 효율적인 어텐션</td></tr><tr><td><code>_native_flash</code></td><td><a href="https://docs.pytorch.org/docs/stable/generated/torch.nn.attention.SDPBackend.html#torch.nn.attention.SDPBackend" rel="nofollow">PyTorch 네이티브</a></td><td>PyTorch의 FlashAttention</td></tr><tr><td><code>_native_math</code></td><td><a href="https://docs.pytorch.org/docs/stable/generated/torch.nn.attention.SDPBackend.html#torch.nn.attention.SDPBackend" rel="nofollow">PyTorch 네이티브</a></td><td>수학 기반 어텐션(폴백)</td></tr><tr><td><code>_native_npu</code></td><td><a href="https://docs.pytorch.org/docs/stable/generated/torch.nn.attention.SDPBackend.html#torch.nn.attention.SDPBackend" rel="nofollow">PyTorch 네이티브</a></td><td>NPU 최적화 어텐션</td></tr><tr><td><code>_native_xla</code></td><td><a href="https://docs.pytorch.org/docs/stable/generated/torch.nn.attention.SDPBackend.html#torch.nn.attention.SDPBackend" rel="nofollow">PyTorch 네이티브</a></td><td>XLA 최적화 어텐션</td></tr><tr><td><code>flash</code></td><td><a href="https://github.com/Dao-AILab/flash-attention" rel="nofollow">FlashAttention</a></td><td>FlashAttention-2</td></tr><tr><td><code>flash_hub</code></td><td><a href="https://github.com/Dao-AILab/flash-attention" rel="nofollow">FlashAttention</a></td><td>kernels의 FlashAttention-2</td></tr><tr><td><code>flash_varlen</code></td><td><a href="https://github.com/Dao-AILab/flash-attention" rel="nofollow">FlashAttention</a></td><td>가변 길이 FlashAttention</td></tr><tr><td><code>flash_varlen_hub</code></td><td><a href="https://github.com/Dao-AILab/flash-attention" rel="nofollow">FlashAttention</a></td><td>kernels의 가변 길이 FlashAttention</td></tr><tr><td><code>aiter_fa2_hub</code></td><td><a href="https://github.com/ROCm/aiter" rel="nofollow">ROCm용 AI Tensor Engine</a></td><td>kernels의 AMD ROCm용 FlashAttention-2</td></tr><tr><td><code>aiter_sage_hub</code></td><td><a href="https://github.com/ROCm/aiter" rel="nofollow">ROCm용 AI Tensor Engine</a></td><td>kernels의 AMD RDNA4(gfx1201)용 양자화 어텐션(INT8 QK, FP8 PV)</td></tr><tr><td><code>flash_4_hub</code></td><td><a href="https://github.com/Dao-AILab/flash-attention" rel="nofollow">FlashAttention</a></td><td>FlashAttention-4</td></tr><tr><td><code>_flash_3</code></td><td><a href="https://github.com/Dao-AILab/flash-attention" rel="nofollow">FlashAttention</a></td><td>FlashAttention-3</td></tr><tr><td><code>_flash_varlen_3</code></td><td><a href="https://github.com/Dao-AILab/flash-attention" rel="nofollow">FlashAttention</a></td><td>가변 길이 FlashAttention-3</td></tr><tr><td><code>_flash_3_hub</code></td><td><a href="https://github.com/Dao-AILab/flash-attention" rel="nofollow">FlashAttention</a></td><td>kernels의 FlashAttention-3</td></tr><tr><td><code>_flash_3_varlen_hub</code></td><td><a href="https://github.com/Dao-AILab/flash-attention" rel="nofollow">FlashAttention</a></td><td>kernels의 가변 길이 FlashAttention-3</td></tr><tr><td><code>sage</code></td><td><a href="https://github.com/thu-ml/SageAttention" rel="nofollow">SageAttention</a></td><td>양자화 어텐션 (INT8 QK)</td></tr><tr><td><code>sage_hub</code></td><td><a href="https://github.com/thu-ml/SageAttention" rel="nofollow">SageAttention</a></td><td>kernels의 양자화 어텐션(INT8 QK)</td></tr><tr><td><code>sage_blackwell_hub</code></td><td><a href="https://github.com/thu-ml/SageAttention" rel="nofollow">SageAttention</a></td><td>kernels의 SM120 Blackwell GPU용 SageAttention3 FP4 어텐션</td></tr><tr><td><code>sage_varlen</code></td><td><a href="https://github.com/thu-ml/SageAttention" rel="nofollow">SageAttention</a></td><td>가변 길이 SageAttention</td></tr><tr><td><code>_sage_qk_int8_pv_fp8_cuda</code></td><td><a href="https://github.com/thu-ml/SageAttention" rel="nofollow">SageAttention</a></td><td>INT8 QK + FP8 PV (CUDA)</td></tr><tr><td><code>_sage_qk_int8_pv_fp8_cuda_sm90</code></td><td><a href="https://github.com/thu-ml/SageAttention" rel="nofollow">SageAttention</a></td><td>INT8 QK + FP8 PV (SM90)</td></tr><tr><td><code>_sage_qk_int8_pv_fp16_cuda</code></td><td><a href="https://github.com/thu-ml/SageAttention" rel="nofollow">SageAttention</a></td><td>INT8 QK + FP16 PV (CUDA)</td></tr><tr><td><code>_sage_qk_int8_pv_fp16_triton</code></td><td><a href="https://github.com/thu-ml/SageAttention" rel="nofollow">SageAttention</a></td><td>INT8 QK + FP16 PV (Triton)</td></tr><tr><td><code>xformers</code></td><td><a href="https://github.com/facebookresearch/xformers" rel="nofollow">xFormers</a></td><td>메모리 효율적인 어텐션</td></tr></tbody></table></details> <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/attention_backends.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_1rjuniz = { | |
| 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.vZc-dOUR.js"), | |
| import("/docs/diffusers/main/en/_app/immutable/entry/app.B6c3Ml05.js") | |
| ]).then(([kit, app]) => { | |
| kit.start(app, element, { | |
| node_ids: [0, 326], | |
| data: [null,null], | |
| form: null, | |
| error: null | |
| }); | |
| }); | |
| } | |
| </script> | |
Xet Storage Details
- Size:
- 37.9 kB
- Xet hash:
- 68d25a7c8fc818f5a81c64cd5241139aebddd9b1a87cb05d0376730b5e89eaa8
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.