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