import{s as te,o as ne,n as le}from"../chunks/scheduler.9bc65507.js";import{S as ae,i as ie,g as r,s as o,r as X,A as oe,h as c,f as n,c as s,j as O,u as E,x as y,k as q,y as se,a as l,v as N,d as B,t as W,w as R}from"../chunks/index.707bf1b6.js";import{T as re}from"../chunks/Tip.c2ecdbf4.js";import{C as ee}from"../chunks/CodeBlock.54a9f38d.js";import{H as ce}from"../chunks/Heading.342b1fa6.js";function pe(S){let a,M="Some PyTorch operations are not implemented in MPS yet and will throw an error. To avoid this, you should set the environment variable <code>PYTORCH_ENABLE_MPS_FALLBACK=1</code> to use the CPU kernels instead (you’ll still see a <code>UserWarning</code>).",d,f,p,m,h='If you run into any other errors, please open an issue in the <a href="https://github.com/pytorch/pytorch/issues" rel="nofollow">PyTorch</a> repository because the <a href="/docs/transformers/main/en/main_classes/trainer#transformers.Trainer">Trainer</a> only integrates the MPS backend.';return{c(){a=r("p"),a.innerHTML=M,d=o(),f=r("br"),p=o(),m=r("p"),m.innerHTML=h},l(i){a=c(i,"P",{"data-svelte-h":!0}),y(a)!=="svelte-1lollff"&&(a.innerHTML=M),d=s(i),f=c(i,"BR",{}),p=s(i),m=c(i,"P",{"data-svelte-h":!0}),y(m)!=="svelte-1bifm0k"&&(m.innerHTML=h)},m(i,u){l(i,a,u),l(i,d,u),l(i,f,u),l(i,p,u),l(i,m,u)},p:le,d(i){i&&(n(a),n(d),n(f),n(p),n(m))}}}function me(S){let a,M,d,f,p,m,h,i='Previously, training models on a Mac was limited to the CPU only. With the release of PyTorch v1.12, you can take advantage of training models with Apple’s silicon GPUs for significantly faster performance and training. This is powered in PyTorch by integrating Apple’s Metal Performance Shaders (MPS) as a backend. The <a href="https://pytorch.org/docs/stable/notes/mps.html" rel="nofollow">MPS backend</a> implements PyTorch operations as custom Metal shaders and places these modules on a <code>mps</code> device.',u,T,L,w,Z="With the <code>mps</code> device set, you can:",A,b,z="<li>train larger networks or batch sizes locally</li> <li>reduce data retrieval latency because the GPU’s unified memory architecture allows direct access to the full memory store</li> <li>reduce costs because you don’t need to train on cloud-based GPUs or add additional local GPUs</li>",C,_,F="Get started by making sure you have PyTorch installed. MPS acceleration is supported on macOS 12.3+.",G,v,x,J,Y='<a href="/docs/transformers/main/en/main_classes/trainer#transformers.TrainingArguments">TrainingArguments</a> uses the <code>mps</code> device by default if it’s available which means you don’t need to explicitly set the device. For example, you can run the <a href="https://github.com/huggingface/transformers/blob/main/examples/pytorch/text-classification/run_glue.py" rel="nofollow">run_glue.py</a> script with the MPS backend automatically enabled without making any changes.',H,g,k,U,Q='Backends for <a href="https://pytorch.org/docs/stable/distributed.html#backends" rel="nofollow">distributed setups</a> like <code>gloo</code> and <code>nccl</code> are not supported by the <code>mps</code> device which means you can only train on a single GPU with the MPS backend.',j,$,D='You can learn more about the MPS backend in the <a href="https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/" rel="nofollow">Introducing Accelerated PyTorch Training on Mac</a> blog post.',I,P,V;return p=new ce({props:{title:"PyTorch training on Apple silicon",local:"pytorch-training-on-apple-silicon",headingTag:"h1"}}),T=new re({props:{warning:!0,$$slots:{default:[pe]},$$scope:{ctx:S}}}),v=new ee({props:{code:"cGlwJTIwaW5zdGFsbCUyMHRvcmNoJTIwdG9yY2h2aXNpb24lMjB0b3JjaGF1ZGlv",highlighted:"pip install torch torchvision torchaudio",wrap:!1}}),g=new ee({props:{code:"ZXhwb3J0JTIwVEFTS19OQU1FJTNEbXJwYyUwQSUwQXB5dGhvbiUyMGV4YW1wbGVzJTJGcHl0b3JjaCUyRnRleHQtY2xhc3NpZmljYXRpb24lMkZydW5fZ2x1ZS5weSUyMCU1QyUwQSUyMCUyMC0tbW9kZWxfbmFtZV9vcl9wYXRoJTIwYmVydC1iYXNlLWNhc2VkJTIwJTVDJTBBJTIwJTIwLS10YXNrX25hbWUlMjAlMjRUQVNLX05BTUUlMjAlNUMlMEEtJTIwLS11c2VfbXBzX2RldmljZSUyMCU1QyUwQSUyMCUyMC0tZG9fdHJhaW4lMjAlNUMlMEElMjAlMjAtLWRvX2V2YWwlMjAlNUMlMEElMjAlMjAtLW1heF9zZXFfbGVuZ3RoJTIwMTI4JTIwJTVDJTBBJTIwJTIwLS1wZXJfZGV2aWNlX3RyYWluX2JhdGNoX3NpemUlMjAzMiUyMCU1QyUwQSUyMCUyMC0tbGVhcm5pbmdfcmF0ZSUyMDJlLTUlMjAlNUMlMEElMjAlMjAtLW51bV90cmFpbl9lcG9jaHMlMjAzJTIwJTVDJTBBJTIwJTIwLS1vdXRwdXRfZGlyJTIwJTJGdG1wJTJGJTI0VEFTS19OQU1FJTJGJTIwJTVDJTBBJTIwJTIwLS1vdmVyd3JpdGVfb3V0cHV0X2Rpcg==",highlighted:`export TASK_NAME=mrpc

python examples/pytorch/text-classification/run_glue.py \\
  --model_name_or_path bert-base-cased \\
  --task_name $TASK_NAME \\
<span class="hljs-deletion">- --use_mps_device \\</span>
  --do_train \\
  --do_eval \\
  --max_seq_length 128 \\
  --per_device_train_batch_size 32 \\
  --learning_rate 2e-5 \\
  --num_train_epochs 3 \\
  --output_dir /tmp/$TASK_NAME/ \\
  --overwrite_output_dir`,wrap:!1}}),{c(){a=r("meta"),M=o(),d=r("p"),f=o(),X(p.$$.fragment),m=o(),h=r("p"),h.innerHTML=i,u=o(),X(T.$$.fragment),L=o(),w=r("p"),w.innerHTML=Z,A=o(),b=r("ul"),b.innerHTML=z,C=o(),_=r("p"),_.textContent=F,G=o(),X(v.$$.fragment),x=o(),J=r("p"),J.innerHTML=Y,H=o(),X(g.$$.fragment),k=o(),U=r("p"),U.innerHTML=Q,j=o(),$=r("p"),$.innerHTML=D,I=o(),P=r("p"),this.h()},l(e){const t=oe("svelte-u9bgzb",document.head);a=c(t,"META",{name:!0,content:!0}),t.forEach(n),M=s(e),d=c(e,"P",{}),O(d).forEach(n),f=s(e),E(p.$$.fragment,e),m=s(e),h=c(e,"P",{"data-svelte-h":!0}),y(h)!=="svelte-ascbzy"&&(h.innerHTML=i),u=s(e),E(T.$$.fragment,e),L=s(e),w=c(e,"P",{"data-svelte-h":!0}),y(w)!=="svelte-1oy5skh"&&(w.innerHTML=Z),A=s(e),b=c(e,"UL",{"data-svelte-h":!0}),y(b)!=="svelte-32rt2r"&&(b.innerHTML=z),C=s(e),_=c(e,"P",{"data-svelte-h":!0}),y(_)!=="svelte-zjtbz3"&&(_.textContent=F),G=s(e),E(v.$$.fragment,e),x=s(e),J=c(e,"P",{"data-svelte-h":!0}),y(J)!=="svelte-1ab1bzg"&&(J.innerHTML=Y),H=s(e),E(g.$$.fragment,e),k=s(e),U=c(e,"P",{"data-svelte-h":!0}),y(U)!=="svelte-12ihrft"&&(U.innerHTML=Q),j=s(e),$=c(e,"P",{"data-svelte-h":!0}),y($)!=="svelte-1nh5d6n"&&($.innerHTML=D),I=s(e),P=c(e,"P",{}),O(P).forEach(n),this.h()},h(){q(a,"name","hf:doc:metadata"),q(a,"content",de)},m(e,t){se(document.head,a),l(e,M,t),l(e,d,t),l(e,f,t),N(p,e,t),l(e,m,t),l(e,h,t),l(e,u,t),N(T,e,t),l(e,L,t),l(e,w,t),l(e,A,t),l(e,b,t),l(e,C,t),l(e,_,t),l(e,G,t),N(v,e,t),l(e,x,t),l(e,J,t),l(e,H,t),N(g,e,t),l(e,k,t),l(e,U,t),l(e,j,t),l(e,$,t),l(e,I,t),l(e,P,t),V=!0},p(e,[t]){const K={};t&2&&(K.$$scope={dirty:t,ctx:e}),T.$set(K)},i(e){V||(B(p.$$.fragment,e),B(T.$$.fragment,e),B(v.$$.fragment,e),B(g.$$.fragment,e),V=!0)},o(e){W(p.$$.fragment,e),W(T.$$.fragment,e),W(v.$$.fragment,e),W(g.$$.fragment,e),V=!1},d(e){e&&(n(M),n(d),n(f),n(m),n(h),n(u),n(L),n(w),n(A),n(b),n(C),n(_),n(G),n(x),n(J),n(H),n(k),n(U),n(j),n($),n(I),n(P)),n(a),R(p,e),R(T,e),R(v,e),R(g,e)}}}const de='{"title":"PyTorch training on Apple silicon","local":"pytorch-training-on-apple-silicon","sections":[],"depth":1}';function he(S){return ne(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class we extends ae{constructor(a){super(),ie(this,a,he,me,te,{})}}export{we as component};
