import{s as le,o as ae,n as ie}from"../chunks/scheduler.25b97de1.js";import{S as se,i as re,g as o,s,r as S,A as oe,h as c,f as n,c as r,j as ee,u as L,x as y,k as te,y as ce,a as l,v as k,d as Z,t as x,w as J}from"../chunks/index.d9030fc9.js";import{T as pe}from"../chunks/Tip.baa67368.js";import{C as ne}from"../chunks/CodeBlock.e6cd0d95.js";import{H as me,E as de}from"../chunks/EditOnGithub.91d95064.js";function he(H){let a,b="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/pr_28071/en/main_classes/trainer#transformers.Trainer">Trainer</a> only integrates the MPS backend.';return{c(){a=o("p"),a.innerHTML=b,d=s(),f=o("br"),p=s(),m=o("p"),m.innerHTML=h},l(i){a=c(i,"P",{"data-svelte-h":!0}),y(a)!=="svelte-1lollff"&&(a.innerHTML=b),d=r(i),f=c(i,"BR",{}),p=r(i),m=c(i,"P",{"data-svelte-h":!0}),y(m)!=="svelte-1aul7sa"&&(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:ie,d(i){i&&(n(a),n(d),n(f),n(p),n(m))}}}function ue(H){let a,b,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,M,j,_,V="With the <code>mps</code> device set, you can:",X,T,I="<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>",E,w,z="Get started by making sure you have PyTorch installed. MPS acceleration is supported on macOS 12.3+.",G,U,N,g,D='<a href="/docs/transformers/pr_28071/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.',W,$,Q,v,K='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.',F,P,O='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.',R,C,Y,A,B;return p=new me({props:{title:"PyTorch training on Apple silicon",local:"pytorch-training-on-apple-silicon",headingTag:"h1"}}),M=new pe({props:{warning:!0,$$slots:{default:[he]},$$scope:{ctx:H}}}),U=new ne({props:{code:"cGlwJTIwaW5zdGFsbCUyMHRvcmNoJTIwdG9yY2h2aXNpb24lMjB0b3JjaGF1ZGlv",highlighted:"pip install torch torchvision torchaudio",wrap:!1}}),$=new ne({props:{code:"ZXhwb3J0JTIwVEFTS19OQU1FJTNEbXJwYyUwQSUwQXB5dGhvbiUyMGV4YW1wbGVzJTJGcHl0b3JjaCUyRnRleHQtY2xhc3NpZmljYXRpb24lMkZydW5fZ2x1ZS5weSUyMCU1QyUwQSUyMCUyMC0tbW9kZWxfbmFtZV9vcl9wYXRoJTIwZ29vZ2xlLWJlcnQlMkZiZXJ0LWJhc2UtY2FzZWQlMjAlNUMlMEElMjAlMjAtLXRhc2tfbmFtZSUyMCUyNFRBU0tfTkFNRSUyMCU1QyUwQS0lMjAtLXVzZV9tcHNfZGV2aWNlJTIwJTVDJTBBJTIwJTIwLS1kb190cmFpbiUyMCU1QyUwQSUyMCUyMC0tZG9fZXZhbCUyMCU1QyUwQSUyMCUyMC0tbWF4X3NlcV9sZW5ndGglMjAxMjglMjAlNUMlMEElMjAlMjAtLXBlcl9kZXZpY2VfdHJhaW5fYmF0Y2hfc2l6ZSUyMDMyJTIwJTVDJTBBJTIwJTIwLS1sZWFybmluZ19yYXRlJTIwMmUtNSUyMCU1QyUwQSUyMCUyMC0tbnVtX3RyYWluX2Vwb2NocyUyMDMlMjAlNUMlMEElMjAlMjAtLW91dHB1dF9kaXIlMjAlMkZ0bXAlMkYlMjRUQVNLX05BTUUlMkYlMjAlNUMlMEElMjAlMjAtLW92ZXJ3cml0ZV9vdXRwdXRfZGly",highlighted:`export TASK_NAME=mrpc

python examples/pytorch/text-classification/run_glue.py \\
  --model_name_or_path google-bert/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=new de({props:{source:"https://github.com/huggingface/transformers/blob/main/docs/source/en/perf_train_special.md"}}),{c(){a=o("meta"),b=s(),d=o("p"),f=s(),S(p.$$.fragment),m=s(),h=o("p"),h.innerHTML=i,u=s(),S(M.$$.fragment),j=s(),_=o("p"),_.innerHTML=V,X=s(),T=o("ul"),T.innerHTML=I,E=s(),w=o("p"),w.textContent=z,G=s(),S(U.$$.fragment),N=s(),g=o("p"),g.innerHTML=D,W=s(),S($.$$.fragment),Q=s(),v=o("p"),v.innerHTML=K,F=s(),P=o("p"),P.innerHTML=O,R=s(),S(C.$$.fragment),Y=s(),A=o("p"),this.h()},l(e){const t=oe("svelte-u9bgzb",document.head);a=c(t,"META",{name:!0,content:!0}),t.forEach(n),b=r(e),d=c(e,"P",{}),ee(d).forEach(n),f=r(e),L(p.$$.fragment,e),m=r(e),h=c(e,"P",{"data-svelte-h":!0}),y(h)!=="svelte-ascbzy"&&(h.innerHTML=i),u=r(e),L(M.$$.fragment,e),j=r(e),_=c(e,"P",{"data-svelte-h":!0}),y(_)!=="svelte-1oy5skh"&&(_.innerHTML=V),X=r(e),T=c(e,"UL",{"data-svelte-h":!0}),y(T)!=="svelte-32rt2r"&&(T.innerHTML=I),E=r(e),w=c(e,"P",{"data-svelte-h":!0}),y(w)!=="svelte-zjtbz3"&&(w.textContent=z),G=r(e),L(U.$$.fragment,e),N=r(e),g=c(e,"P",{"data-svelte-h":!0}),y(g)!=="svelte-3gvc9y"&&(g.innerHTML=D),W=r(e),L($.$$.fragment,e),Q=r(e),v=c(e,"P",{"data-svelte-h":!0}),y(v)!=="svelte-12ihrft"&&(v.innerHTML=K),F=r(e),P=c(e,"P",{"data-svelte-h":!0}),y(P)!=="svelte-1nh5d6n"&&(P.innerHTML=O),R=r(e),L(C.$$.fragment,e),Y=r(e),A=c(e,"P",{}),ee(A).forEach(n),this.h()},h(){te(a,"name","hf:doc:metadata"),te(a,"content",fe)},m(e,t){ce(document.head,a),l(e,b,t),l(e,d,t),l(e,f,t),k(p,e,t),l(e,m,t),l(e,h,t),l(e,u,t),k(M,e,t),l(e,j,t),l(e,_,t),l(e,X,t),l(e,T,t),l(e,E,t),l(e,w,t),l(e,G,t),k(U,e,t),l(e,N,t),l(e,g,t),l(e,W,t),k($,e,t),l(e,Q,t),l(e,v,t),l(e,F,t),l(e,P,t),l(e,R,t),k(C,e,t),l(e,Y,t),l(e,A,t),B=!0},p(e,[t]){const q={};t&2&&(q.$$scope={dirty:t,ctx:e}),M.$set(q)},i(e){B||(Z(p.$$.fragment,e),Z(M.$$.fragment,e),Z(U.$$.fragment,e),Z($.$$.fragment,e),Z(C.$$.fragment,e),B=!0)},o(e){x(p.$$.fragment,e),x(M.$$.fragment,e),x(U.$$.fragment,e),x($.$$.fragment,e),x(C.$$.fragment,e),B=!1},d(e){e&&(n(b),n(d),n(f),n(m),n(h),n(u),n(j),n(_),n(X),n(T),n(E),n(w),n(G),n(N),n(g),n(W),n(Q),n(v),n(F),n(P),n(R),n(Y),n(A)),n(a),J(p,e),J(M,e),J(U,e),J($,e),J(C,e)}}}const fe='{"title":"PyTorch training on Apple silicon","local":"pytorch-training-on-apple-silicon","sections":[],"depth":1}';function ye(H){return ae(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Ue extends se{constructor(a){super(),re(this,a,ye,ue,le,{})}}export{Ue as component};
