import{s as Le,n as Xe,o as Pe}from"../chunks/scheduler.23542ac5.js";import{S as Re,i as We,e as i,s as n,c,h as Be,a as p,d as l,b as a,f as ze,g as o,j as r,k as ye,l as xe,m as s,n as d,t as m,o as u,p as f}from"../chunks/index.9b1f405b.js";import{C as Ae,H as Je,E as Qe}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.cf8449cd.js";import{C as B}from"../chunks/CodeBlock.6a4e8f39.js";function Ne(je){let M,Q,x,N,h,Y,y,V,J,Te='분산 설정에서는 여러 개의 프롬프트를 동시에 생성할 때 유용한 🤗 <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에서 추론을 실행할 수 있습니다.',S,j,be="이 가이드에서는 분산 추론을 위해 🤗 Accelerate와 PyTorch Distributed를 사용하는 방법을 보여드립니다.",F,T,q,b,Ue='🤗 <a href="https://huggingface.co/docs/accelerate/index" rel="nofollow">Accelerate</a>는 분산 설정에서 추론을 쉽게 훈련하거나 실행할 수 있도록 설계된 라이브러리입니다. 분산 환경 설정 프로세스를 간소화하여 PyTorch 코드에 집중할 수 있도록 해줍니다.',D,U,$e="시작하려면 Python 파일을 생성하고 <code>accelerate.PartialState</code>를 초기화하여 분산 환경을 생성하면, 설정이 자동으로 감지되므로 <code>rank</code> 또는 <code>world_size</code>를 명시적으로 정의할 필요가 없습니다. [‘DiffusionPipeline`]을 <code>distributed_state.device</code>로 이동하여 각 프로세스에 GPU를 할당합니다.",K,$,_e="이제 컨텍스트 관리자로 <code>split_between_processes</code> 유틸리티를 사용하여 프로세스 수에 따라 프롬프트를 자동으로 분배합니다.",O,_,ee,Z,Ze="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:",te,g,le,w,ge='<p>자세한 내용은 <a href="https://huggingface.co/docs/accelerate/en/usage_guides/distributed_inference#distributed-inference-with-accelerate" rel="nofollow">🤗 Accelerate를 사용한 분산 추론</a> 가이드를 참조하세요.</p>',se,G,ne,k,Ge='PyTorch는 데이터 병렬 처리를 가능하게 하는 <a href="https://pytorch.org/docs/stable/generated/torch.nn.parallel.DistributedDataParallel.html" rel="nofollow"><code>DistributedDataParallel</code></a>을 지원합니다.',ae,v,ke="시작하려면 Python 파일을 생성하고 <code>torch.distributed</code> 및 <code>torch.multiprocessing</code>을 임포트하여 분산 프로세스 그룹을 설정하고 각 GPU에서 추론용 프로세스를 생성합니다. 그리고 <code>DiffusionPipeline</code>도 초기화해야 합니다:",ie,I,ve="확산 파이프라인을 <code>rank</code>로 이동하고 <code>get_rank</code>를 사용하여 각 프로세스에 GPU를 할당하면 각 프로세스가 다른 프롬프트를 처리합니다:",pe,H,re,C,Ie="사용할 백엔드 유형, 현재 프로세스의 <code>rank</code>, <code>world_size</code> 또는 참여하는 프로세스 수로 분산 환경 생성을 처리하는 함수<code>init_process_group</code>를 만들어 추론을 실행해야 합니다.",ce,E,He="2개의 GPU에서 추론을 병렬로 실행하는 경우 <code>world_size</code>는 2입니다.",oe,z,de,L,Ce='분산 추론을 실행하려면 <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> 함수를 실행합니다:',me,X,ue,P,Ee="추론 스크립트를 완료했으면 <code>--nproc_per_node</code> 인수를 사용하여 사용할 GPU 수를 지정하고 <code>torchrun</code>을 호출하여 스크립트를 실행합니다:",fe,R,Me,W,we,A,he;return h=new Ae({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),y=new Je({props:{title:"여러 GPU를 사용한 분산 추론",local:"여러-gpu를-사용한-분산-추론",headingTag:"h1"}}),T=new Je({props:{title:"🤗 Accelerate",local:"-accelerate",headingTag:"h2"}}),_=new B({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 B({props:{code:"YWNjZWxlcmF0ZSUyMGxhdW5jaCUyMHJ1bl9kaXN0cmlidXRlZC5weSUyMC0tbnVtX3Byb2Nlc3NlcyUzRDI=",highlighted:"accelerate launch run_distributed.py --num_processes=2",wrap:!1}}),G=new Je({props:{title:"Pytoerch 분산",local:"pytoerch-분산",headingTag:"h2"}}),H=new B({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}}),z=new B({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}}),X=new B({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}}),R=new B({props:{code:"dG9yY2hydW4lMjBydW5fZGlzdHJpYnV0ZWQucHklMjAtLW5wcm9jX3Blcl9ub2RlJTNEMg==",highlighted:"torchrun run_distributed.py --nproc_per_node=2",wrap:!1}}),W=new Qe({props:{source:"https://github.com/huggingface/diffusers/blob/main/docs/source/ko/training/distributed_inference.md"}}),{c(){M=i("meta"),Q=n(),x=i("p"),N=n(),c(h.$$.fragment),Y=n(),c(y.$$.fragment),V=n(),J=i("p"),J.innerHTML=Te,S=n(),j=i("p"),j.textContent=be,F=n(),c(T.$$.fragment),q=n(),b=i("p"),b.innerHTML=Ue,D=n(),U=i("p"),U.innerHTML=$e,K=n(),$=i("p"),$.innerHTML=_e,O=n(),c(_.$$.fragment),ee=n(),Z=i("p"),Z.innerHTML=Ze,te=n(),c(g.$$.fragment),le=n(),w=i("blockquote"),w.innerHTML=ge,se=n(),c(G.$$.fragment),ne=n(),k=i("p"),k.innerHTML=Ge,ae=n(),v=i("p"),v.innerHTML=ke,ie=n(),I=i("p"),I.innerHTML=ve,pe=n(),c(H.$$.fragment),re=n(),C=i("p"),C.innerHTML=Ie,ce=n(),E=i("p"),E.innerHTML=He,oe=n(),c(z.$$.fragment),de=n(),L=i("p"),L.innerHTML=Ce,me=n(),c(X.$$.fragment),ue=n(),P=i("p"),P.innerHTML=Ee,fe=n(),c(R.$$.fragment),Me=n(),c(W.$$.fragment),we=n(),A=i("p"),this.h()},l(e){const t=Be("svelte-u9bgzb",document.head);M=p(t,"META",{name:!0,content:!0}),t.forEach(l),Q=a(e),x=p(e,"P",{}),ze(x).forEach(l),N=a(e),o(h.$$.fragment,e),Y=a(e),o(y.$$.fragment,e),V=a(e),J=p(e,"P",{"data-svelte-h":!0}),r(J)!=="svelte-1pju33"&&(J.innerHTML=Te),S=a(e),j=p(e,"P",{"data-svelte-h":!0}),r(j)!=="svelte-mr3tus"&&(j.textContent=be),F=a(e),o(T.$$.fragment,e),q=a(e),b=p(e,"P",{"data-svelte-h":!0}),r(b)!=="svelte-gk0xcz"&&(b.innerHTML=Ue),D=a(e),U=p(e,"P",{"data-svelte-h":!0}),r(U)!=="svelte-t8zt9f"&&(U.innerHTML=$e),K=a(e),$=p(e,"P",{"data-svelte-h":!0}),r($)!=="svelte-1wde0xg"&&($.innerHTML=_e),O=a(e),o(_.$$.fragment,e),ee=a(e),Z=p(e,"P",{"data-svelte-h":!0}),r(Z)!=="svelte-1ohh8as"&&(Z.innerHTML=Ze),te=a(e),o(g.$$.fragment,e),le=a(e),w=p(e,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),r(w)!=="svelte-nm3t8q"&&(w.innerHTML=ge),se=a(e),o(G.$$.fragment,e),ne=a(e),k=p(e,"P",{"data-svelte-h":!0}),r(k)!=="svelte-db33v7"&&(k.innerHTML=Ge),ae=a(e),v=p(e,"P",{"data-svelte-h":!0}),r(v)!=="svelte-td2orp"&&(v.innerHTML=ke),ie=a(e),I=p(e,"P",{"data-svelte-h":!0}),r(I)!=="svelte-2xw9l6"&&(I.innerHTML=ve),pe=a(e),o(H.$$.fragment,e),re=a(e),C=p(e,"P",{"data-svelte-h":!0}),r(C)!=="svelte-60cuu7"&&(C.innerHTML=Ie),ce=a(e),E=p(e,"P",{"data-svelte-h":!0}),r(E)!=="svelte-1xq31ih"&&(E.innerHTML=He),oe=a(e),o(z.$$.fragment,e),de=a(e),L=p(e,"P",{"data-svelte-h":!0}),r(L)!=="svelte-1hoikpv"&&(L.innerHTML=Ce),me=a(e),o(X.$$.fragment,e),ue=a(e),P=p(e,"P",{"data-svelte-h":!0}),r(P)!=="svelte-1g25qdf"&&(P.innerHTML=Ee),fe=a(e),o(R.$$.fragment,e),Me=a(e),o(W.$$.fragment,e),we=a(e),A=p(e,"P",{}),ze(A).forEach(l),this.h()},h(){ye(M,"name","hf:doc:metadata"),ye(M,"content",Ye),ye(w,"class","tip")},m(e,t){xe(document.head,M),s(e,Q,t),s(e,x,t),s(e,N,t),d(h,e,t),s(e,Y,t),d(y,e,t),s(e,V,t),s(e,J,t),s(e,S,t),s(e,j,t),s(e,F,t),d(T,e,t),s(e,q,t),s(e,b,t),s(e,D,t),s(e,U,t),s(e,K,t),s(e,$,t),s(e,O,t),d(_,e,t),s(e,ee,t),s(e,Z,t),s(e,te,t),d(g,e,t),s(e,le,t),s(e,w,t),s(e,se,t),d(G,e,t),s(e,ne,t),s(e,k,t),s(e,ae,t),s(e,v,t),s(e,ie,t),s(e,I,t),s(e,pe,t),d(H,e,t),s(e,re,t),s(e,C,t),s(e,ce,t),s(e,E,t),s(e,oe,t),d(z,e,t),s(e,de,t),s(e,L,t),s(e,me,t),d(X,e,t),s(e,ue,t),s(e,P,t),s(e,fe,t),d(R,e,t),s(e,Me,t),d(W,e,t),s(e,we,t),s(e,A,t),he=!0},p:Xe,i(e){he||(m(h.$$.fragment,e),m(y.$$.fragment,e),m(T.$$.fragment,e),m(_.$$.fragment,e),m(g.$$.fragment,e),m(G.$$.fragment,e),m(H.$$.fragment,e),m(z.$$.fragment,e),m(X.$$.fragment,e),m(R.$$.fragment,e),m(W.$$.fragment,e),he=!0)},o(e){u(h.$$.fragment,e),u(y.$$.fragment,e),u(T.$$.fragment,e),u(_.$$.fragment,e),u(g.$$.fragment,e),u(G.$$.fragment,e),u(H.$$.fragment,e),u(z.$$.fragment,e),u(X.$$.fragment,e),u(R.$$.fragment,e),u(W.$$.fragment,e),he=!1},d(e){e&&(l(Q),l(x),l(N),l(Y),l(V),l(J),l(S),l(j),l(F),l(q),l(b),l(D),l(U),l(K),l($),l(O),l(ee),l(Z),l(te),l(le),l(w),l(se),l(ne),l(k),l(ae),l(v),l(ie),l(I),l(pe),l(re),l(C),l(ce),l(E),l(oe),l(de),l(L),l(me),l(ue),l(P),l(fe),l(Me),l(we),l(A)),l(M),f(h,e),f(y,e),f(T,e),f(_,e),f(g,e),f(G,e),f(H,e),f(z,e),f(X,e),f(R,e),f(W,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(je){return Pe(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Ke extends Re{constructor(M){super(),We(this,M,Ve,Ne,Le,{})}}export{Ke as component};
