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