import{s as ze,o as Ce}from"../chunks/scheduler.94020406.js";import{S as Xe,i as Pe,g as i,s as a,r as o,E as Re,h as p,f as l,c as n,j as Ee,u as d,x as c,k as He,y as We,a as s,v as m,d as u,t as f,w as M,m as Le,n as Be}from"../chunks/index.a08c8d92.js";import{T as Ae}from"../chunks/Tip.7c41dfd5.js";import{C as A}from"../chunks/CodeBlock.f1fae7de.js";import{H as he,E as xe}from"../chunks/getInferenceSnippets.a623b4cf.js";function Ne(N){let r;return{c(){r=Le("자세한 내용은 [🤗 Accelerate를 사용한 분산 추론](https://huggingface.co/docs/accelerate/en/usage_guides/distributed_inference#distributed-inference-with-accelerate) 가이드를 참조하세요.")},l(w){r=Be(w,"자세한 내용은 [🤗 Accelerate를 사용한 분산 추론](https://huggingface.co/docs/accelerate/en/usage_guides/distributed_inference#distributed-inference-with-accelerate) 가이드를 참조하세요.")},m(w,y){s(w,r,y)},d(w){w&&l(r)}}}function Qe(N){let r,w,y,Q,J,Y,j,ye='분산 설정에서는 여러 개의 프롬프트를 동시에 생성할 때 유용한 🤗 <a href="https://huggingface.co/docs/accelerate/index" rel="nofollow">Accelerate</a> 또는 <a href="https://pytorch.org/tutorials/beginner/dist_overview.html" rel="nofollow">PyTorch Distributed</a>를 사용하여 여러 GPU에서 추론을 실행할 수 있습니다.',V,T,Je="이 가이드에서는 분산 추론을 위해 🤗 Accelerate와 PyTorch Distributed를 사용하는 방법을 보여드립니다.",F,b,S,$,je='🤗 <a href="https://huggingface.co/docs/accelerate/index" rel="nofollow">Accelerate</a>는 분산 설정에서 추론을 쉽게 훈련하거나 실행할 수 있도록 설계된 라이브러리입니다. 분산 환경 설정 프로세스를 간소화하여 PyTorch 코드에 집중할 수 있도록 해줍니다.',q,_,Te="시작하려면 Python 파일을 생성하고 <code>accelerate.PartialState</code>를 초기화하여 분산 환경을 생성하면, 설정이 자동으로 감지되므로 <code>rank</code> 또는 <code>world_size</code>를 명시적으로 정의할 필요가 없습니다. [‘DiffusionPipeline`]을 <code>distributed_state.device</code>로 이동하여 각 프로세스에 GPU를 할당합니다.",D,U,be="이제 컨텍스트 관리자로 <code>split_between_processes</code> 유틸리티를 사용하여 프로세스 수에 따라 프롬프트를 자동으로 분배합니다.",K,g,O,Z,$e="Use the <code>--num_processes</code> argument to specify the number of GPUs to use, and call <code>accelerate launch</code> to run the script:",ee,G,te,h,le,k,se,I,_e='PyTorch는 데이터 병렬 처리를 가능하게 하는 <a href="https://pytorch.org/docs/stable/generated/torch.nn.parallel.DistributedDataParallel.html" rel="nofollow"><code>DistributedDataParallel</code></a>을 지원합니다.',ae,v,Ue="시작하려면 Python 파일을 생성하고 <code>torch.distributed</code> 및 <code>torch.multiprocessing</code>을 임포트하여 분산 프로세스 그룹을 설정하고 각 GPU에서 추론용 프로세스를 생성합니다. 그리고 <code>DiffusionPipeline</code>도 초기화해야 합니다:",ne,E,ge="확산 파이프라인을 <code>rank</code>로 이동하고 <code>get_rank</code>를 사용하여 각 프로세스에 GPU를 할당하면 각 프로세스가 다른 프롬프트를 처리합니다:",ie,H,pe,z,Ze="사용할 백엔드 유형, 현재 프로세스의 <code>rank</code>, <code>world_size</code> 또는 참여하는 프로세스 수로 분산 환경 생성을 처리하는 함수<code>init_process_group</code>를 만들어 추론을 실행해야 합니다.",re,C,Ge="2개의 GPU에서 추론을 병렬로 실행하는 경우 <code>world_size</code>는 2입니다.",ce,X,oe,P,ke='분산 추론을 실행하려면 <a href="https://pytorch.org/docs/stable/multiprocessing.html#torch.multiprocessing.spawn" rel="nofollow"><code>mp.spawn</code></a>을 호출하여 <code>world_size</code>에 정의된 GPU 수에 대해 <code>run_inference</code> 함수를 실행합니다:',de,R,me,W,Ie="추론 스크립트를 완료했으면 <code>--nproc_per_node</code> 인수를 사용하여 사용할 GPU 수를 지정하고 <code>torchrun</code>을 호출하여 스크립트를 실행합니다:",ue,L,fe,B,Me,x,we;return J=new he({props:{title:"여러 GPU를 사용한 분산 추론",local:"여러-gpu를-사용한-분산-추론",headingTag:"h1"}}),b=new he({props:{title:"🤗 Accelerate",local:"-accelerate",headingTag:"h2"}}),g=new A({props:{code:"ZnJvbSUyMGFjY2VsZXJhdGUlMjBpbXBvcnQlMjBQYXJ0aWFsU3RhdGUlMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwRGlmZnVzaW9uUGlwZWxpbmUlMEElMEFwaXBlbGluZSUyMCUzRCUyMERpZmZ1c2lvblBpcGVsaW5lLmZyb21fcHJldHJhaW5lZCglMjJzdGFibGUtZGlmZnVzaW9uLXYxLTUlMkZzdGFibGUtZGlmZnVzaW9uLXYxLTUlMjIlMkMlMjB0b3JjaF9kdHlwZSUzRHRvcmNoLmZsb2F0MTYpJTBBZGlzdHJpYnV0ZWRfc3RhdGUlMjAlM0QlMjBQYXJ0aWFsU3RhdGUoKSUwQXBpcGVsaW5lLnRvKGRpc3RyaWJ1dGVkX3N0YXRlLmRldmljZSklMEElMEF3aXRoJTIwZGlzdHJpYnV0ZWRfc3RhdGUuc3BsaXRfYmV0d2Vlbl9wcm9jZXNzZXMoJTVCJTIyYSUyMGRvZyUyMiUyQyUyMCUyMmElMjBjYXQlMjIlNUQpJTIwYXMlMjBwcm9tcHQlM0ElMEElMjAlMjAlMjAlMjByZXN1bHQlMjAlM0QlMjBwaXBlbGluZShwcm9tcHQpLmltYWdlcyU1QjAlNUQlMEElMjAlMjAlMjAlMjByZXN1bHQuc2F2ZShmJTIycmVzdWx0XyU3QmRpc3RyaWJ1dGVkX3N0YXRlLnByb2Nlc3NfaW5kZXglN0QucG5nJTIyKQ==",highlighted:`<span class="hljs-keyword">from</span> accelerate <span class="hljs-keyword">import</span> PartialState
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline

pipeline = DiffusionPipeline.from_pretrained(<span class="hljs-string">&quot;stable-diffusion-v1-5/stable-diffusion-v1-5&quot;</span>, torch_dtype=torch.float16)
distributed_state = PartialState()
pipeline.to(distributed_state.device)

<span class="hljs-keyword">with</span> distributed_state.split_between_processes([<span class="hljs-string">&quot;a dog&quot;</span>, <span class="hljs-string">&quot;a cat&quot;</span>]) <span class="hljs-keyword">as</span> prompt:
    result = pipeline(prompt).images[<span class="hljs-number">0</span>]
    result.save(<span class="hljs-string">f&quot;result_<span class="hljs-subst">{distributed_state.process_index}</span>.png&quot;</span>)`,wrap:!1}}),G=new A({props:{code:"YWNjZWxlcmF0ZSUyMGxhdW5jaCUyMHJ1bl9kaXN0cmlidXRlZC5weSUyMC0tbnVtX3Byb2Nlc3NlcyUzRDI=",highlighted:"accelerate launch run_distributed.py --num_processes=2",wrap:!1}}),h=new Ae({props:{$$slots:{default:[Ne]},$$scope:{ctx:N}}}),k=new he({props:{title:"Pytoerch 분산",local:"pytoerch-분산",headingTag:"h2"}}),H=new A({props:{code:"aW1wb3J0JTIwdG9yY2glMEFpbXBvcnQlMjB0b3JjaC5kaXN0cmlidXRlZCUyMGFzJTIwZGlzdCUwQWltcG9ydCUyMHRvcmNoLm11bHRpcHJvY2Vzc2luZyUyMGFzJTIwbXAlMEElMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwRGlmZnVzaW9uUGlwZWxpbmUlMEElMEFzZCUyMCUzRCUyMERpZmZ1c2lvblBpcGVsaW5lLmZyb21fcHJldHJhaW5lZCglMjJzdGFibGUtZGlmZnVzaW9uLXYxLTUlMkZzdGFibGUtZGlmZnVzaW9uLXYxLTUlMjIlMkMlMjB0b3JjaF9kdHlwZSUzRHRvcmNoLmZsb2F0MTYp",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">import</span> torch.distributed <span class="hljs-keyword">as</span> dist
<span class="hljs-keyword">import</span> torch.multiprocessing <span class="hljs-keyword">as</span> mp

<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline

sd = DiffusionPipeline.from_pretrained(<span class="hljs-string">&quot;stable-diffusion-v1-5/stable-diffusion-v1-5&quot;</span>, torch_dtype=torch.float16)`,wrap:!1}}),X=new A({props:{code:"ZGVmJTIwcnVuX2luZmVyZW5jZShyYW5rJTJDJTIwd29ybGRfc2l6ZSklM0ElMEElMjAlMjAlMjAlMjBkaXN0LmluaXRfcHJvY2Vzc19ncm91cCglMjJuY2NsJTIyJTJDJTIwcmFuayUzRHJhbmslMkMlMjB3b3JsZF9zaXplJTNEd29ybGRfc2l6ZSklMEElMEElMjAlMjAlMjAlMjBzZC50byhyYW5rKSUwQSUwQSUyMCUyMCUyMCUyMGlmJTIwdG9yY2guZGlzdHJpYnV0ZWQuZ2V0X3JhbmsoKSUyMCUzRCUzRCUyMDAlM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBwcm9tcHQlMjAlM0QlMjAlMjJhJTIwZG9nJTIyJTBBJTIwJTIwJTIwJTIwZWxpZiUyMHRvcmNoLmRpc3RyaWJ1dGVkLmdldF9yYW5rKCklMjAlM0QlM0QlMjAxJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwcHJvbXB0JTIwJTNEJTIwJTIyYSUyMGNhdCUyMiUwQSUwQSUyMCUyMCUyMCUyMGltYWdlJTIwJTNEJTIwc2QocHJvbXB0KS5pbWFnZXMlNUIwJTVEJTBBJTIwJTIwJTIwJTIwaW1hZ2Uuc2F2ZShmJTIyLiUyRiU3QidfJy5qb2luKHByb21wdCklN0QucG5nJTIyKQ==",highlighted:`<span class="hljs-keyword">def</span> <span class="hljs-title function_">run_inference</span>(<span class="hljs-params">rank, world_size</span>):
    dist.init_process_group(<span class="hljs-string">&quot;nccl&quot;</span>, rank=rank, world_size=world_size)

    sd.to(rank)

    <span class="hljs-keyword">if</span> torch.distributed.get_rank() == <span class="hljs-number">0</span>:
        prompt = <span class="hljs-string">&quot;a dog&quot;</span>
    <span class="hljs-keyword">elif</span> torch.distributed.get_rank() == <span class="hljs-number">1</span>:
        prompt = <span class="hljs-string">&quot;a cat&quot;</span>

    image = sd(prompt).images[<span class="hljs-number">0</span>]
    image.save(<span class="hljs-string">f&quot;./<span class="hljs-subst">{<span class="hljs-string">&#x27;_&#x27;</span>.join(prompt)}</span>.png&quot;</span>)`,wrap:!1}}),R=new A({props:{code:"ZGVmJTIwbWFpbigpJTNBJTBBJTIwJTIwJTIwJTIwd29ybGRfc2l6ZSUyMCUzRCUyMDIlMEElMjAlMjAlMjAlMjBtcC5zcGF3bihydW5faW5mZXJlbmNlJTJDJTIwYXJncyUzRCh3b3JsZF9zaXplJTJDKSUyQyUyMG5wcm9jcyUzRHdvcmxkX3NpemUlMkMlMjBqb2luJTNEVHJ1ZSklMEElMEElMEFpZiUyMF9fbmFtZV9fJTIwJTNEJTNEJTIwJTIyX19tYWluX18lMjIlM0ElMEElMjAlMjAlMjAlMjBtYWluKCk=",highlighted:`<span class="hljs-keyword">def</span> <span class="hljs-title function_">main</span>():
    world_size = <span class="hljs-number">2</span>
    mp.spawn(run_inference, args=(world_size,), nprocs=world_size, join=<span class="hljs-literal">True</span>)


<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&quot;__main__&quot;</span>:
    main()`,wrap:!1}}),L=new A({props:{code:"dG9yY2hydW4lMjBydW5fZGlzdHJpYnV0ZWQucHklMjAtLW5wcm9jX3Blcl9ub2RlJTNEMg==",highlighted:"torchrun run_distributed.py --nproc_per_node=2",wrap:!1}}),B=new xe({props:{source:"https://github.com/huggingface/diffusers/blob/main/docs/source/ko/training/distributed_inference.md"}}),{c(){r=i("meta"),w=a(),y=i("p"),Q=a(),o(J.$$.fragment),Y=a(),j=i("p"),j.innerHTML=ye,V=a(),T=i("p"),T.textContent=Je,F=a(),o(b.$$.fragment),S=a(),$=i("p"),$.innerHTML=je,q=a(),_=i("p"),_.innerHTML=Te,D=a(),U=i("p"),U.innerHTML=be,K=a(),o(g.$$.fragment),O=a(),Z=i("p"),Z.innerHTML=$e,ee=a(),o(G.$$.fragment),te=a(),o(h.$$.fragment),le=a(),o(k.$$.fragment),se=a(),I=i("p"),I.innerHTML=_e,ae=a(),v=i("p"),v.innerHTML=Ue,ne=a(),E=i("p"),E.innerHTML=ge,ie=a(),o(H.$$.fragment),pe=a(),z=i("p"),z.innerHTML=Ze,re=a(),C=i("p"),C.innerHTML=Ge,ce=a(),o(X.$$.fragment),oe=a(),P=i("p"),P.innerHTML=ke,de=a(),o(R.$$.fragment),me=a(),W=i("p"),W.innerHTML=Ie,ue=a(),o(L.$$.fragment),fe=a(),o(B.$$.fragment),Me=a(),x=i("p"),this.h()},l(e){const t=Re("svelte-u9bgzb",document.head);r=p(t,"META",{name:!0,content:!0}),t.forEach(l),w=n(e),y=p(e,"P",{}),Ee(y).forEach(l),Q=n(e),d(J.$$.fragment,e),Y=n(e),j=p(e,"P",{"data-svelte-h":!0}),c(j)!=="svelte-1pju33"&&(j.innerHTML=ye),V=n(e),T=p(e,"P",{"data-svelte-h":!0}),c(T)!=="svelte-mr3tus"&&(T.textContent=Je),F=n(e),d(b.$$.fragment,e),S=n(e),$=p(e,"P",{"data-svelte-h":!0}),c($)!=="svelte-gk0xcz"&&($.innerHTML=je),q=n(e),_=p(e,"P",{"data-svelte-h":!0}),c(_)!=="svelte-t8zt9f"&&(_.innerHTML=Te),D=n(e),U=p(e,"P",{"data-svelte-h":!0}),c(U)!=="svelte-1wde0xg"&&(U.innerHTML=be),K=n(e),d(g.$$.fragment,e),O=n(e),Z=p(e,"P",{"data-svelte-h":!0}),c(Z)!=="svelte-1ohh8as"&&(Z.innerHTML=$e),ee=n(e),d(G.$$.fragment,e),te=n(e),d(h.$$.fragment,e),le=n(e),d(k.$$.fragment,e),se=n(e),I=p(e,"P",{"data-svelte-h":!0}),c(I)!=="svelte-db33v7"&&(I.innerHTML=_e),ae=n(e),v=p(e,"P",{"data-svelte-h":!0}),c(v)!=="svelte-td2orp"&&(v.innerHTML=Ue),ne=n(e),E=p(e,"P",{"data-svelte-h":!0}),c(E)!=="svelte-2xw9l6"&&(E.innerHTML=ge),ie=n(e),d(H.$$.fragment,e),pe=n(e),z=p(e,"P",{"data-svelte-h":!0}),c(z)!=="svelte-60cuu7"&&(z.innerHTML=Ze),re=n(e),C=p(e,"P",{"data-svelte-h":!0}),c(C)!=="svelte-1xq31ih"&&(C.innerHTML=Ge),ce=n(e),d(X.$$.fragment,e),oe=n(e),P=p(e,"P",{"data-svelte-h":!0}),c(P)!=="svelte-1hoikpv"&&(P.innerHTML=ke),de=n(e),d(R.$$.fragment,e),me=n(e),W=p(e,"P",{"data-svelte-h":!0}),c(W)!=="svelte-1g25qdf"&&(W.innerHTML=Ie),ue=n(e),d(L.$$.fragment,e),fe=n(e),d(B.$$.fragment,e),Me=n(e),x=p(e,"P",{}),Ee(x).forEach(l),this.h()},h(){He(r,"name","hf:doc:metadata"),He(r,"content",Ye)},m(e,t){We(document.head,r),s(e,w,t),s(e,y,t),s(e,Q,t),m(J,e,t),s(e,Y,t),s(e,j,t),s(e,V,t),s(e,T,t),s(e,F,t),m(b,e,t),s(e,S,t),s(e,$,t),s(e,q,t),s(e,_,t),s(e,D,t),s(e,U,t),s(e,K,t),m(g,e,t),s(e,O,t),s(e,Z,t),s(e,ee,t),m(G,e,t),s(e,te,t),m(h,e,t),s(e,le,t),m(k,e,t),s(e,se,t),s(e,I,t),s(e,ae,t),s(e,v,t),s(e,ne,t),s(e,E,t),s(e,ie,t),m(H,e,t),s(e,pe,t),s(e,z,t),s(e,re,t),s(e,C,t),s(e,ce,t),m(X,e,t),s(e,oe,t),s(e,P,t),s(e,de,t),m(R,e,t),s(e,me,t),s(e,W,t),s(e,ue,t),m(L,e,t),s(e,fe,t),m(B,e,t),s(e,Me,t),s(e,x,t),we=!0},p(e,[t]){const ve={};t&2&&(ve.$$scope={dirty:t,ctx:e}),h.$set(ve)},i(e){we||(u(J.$$.fragment,e),u(b.$$.fragment,e),u(g.$$.fragment,e),u(G.$$.fragment,e),u(h.$$.fragment,e),u(k.$$.fragment,e),u(H.$$.fragment,e),u(X.$$.fragment,e),u(R.$$.fragment,e),u(L.$$.fragment,e),u(B.$$.fragment,e),we=!0)},o(e){f(J.$$.fragment,e),f(b.$$.fragment,e),f(g.$$.fragment,e),f(G.$$.fragment,e),f(h.$$.fragment,e),f(k.$$.fragment,e),f(H.$$.fragment,e),f(X.$$.fragment,e),f(R.$$.fragment,e),f(L.$$.fragment,e),f(B.$$.fragment,e),we=!1},d(e){e&&(l(w),l(y),l(Q),l(Y),l(j),l(V),l(T),l(F),l(S),l($),l(q),l(_),l(D),l(U),l(K),l(O),l(Z),l(ee),l(te),l(le),l(se),l(I),l(ae),l(v),l(ne),l(E),l(ie),l(pe),l(z),l(re),l(C),l(ce),l(oe),l(P),l(de),l(me),l(W),l(ue),l(fe),l(Me),l(x)),l(r),M(J,e),M(b,e),M(g,e),M(G,e),M(h,e),M(k,e),M(H,e),M(X,e),M(R,e),M(L,e),M(B,e)}}}const Ye='{"title":"여러 GPU를 사용한 분산 추론","local":"여러-gpu를-사용한-분산-추론","sections":[{"title":"🤗 Accelerate","local":"-accelerate","sections":[],"depth":2},{"title":"Pytoerch 분산","local":"pytoerch-분산","sections":[],"depth":2}],"depth":1}';function Ve(N){return Ce(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Oe extends Xe{constructor(r){super(),Pe(this,r,Ve,Qe,ze,{})}}export{Oe as component};
