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