import{s as ne,o as ie,n as ae}from"../chunks/scheduler.25b97de1.js";import{S as oe,i as re,g as s,s as o,r as g,A as se,h as p,f as l,c as r,j as ee,u as L,x as d,k as te,y as pe,a as n,v as Z,d as x,t as J,w as H}from"../chunks/index.d9030fc9.js";import{T as ce}from"../chunks/Tip.baa67368.js";import{C as le}from"../chunks/CodeBlock.e6cd0d95.js";import{H as me,E as Me}from"../chunks/EditOnGithub.91d95064.js";function fe(j){let i,_="某些 PyTorch 操作目前还未在 MPS 上实现，可能会抛出错误提示。可以通过设置环境变量<code>PYTORCH_ENABLE_MPS_FALLBACK=1</code>来使用CPU内核以避免这种情况发生（您仍然会看到一个<code>UserWarning</code>）。",M,h,c,m,f='如果您遇到任何其他错误，请在<a href="https://github.com/pytorch/pytorch/issues" rel="nofollow">PyTorch库</a>中创建一个 issue，因为<code>Trainer</code>类中只集成了 MPS 后端.';return{c(){i=s("p"),i.innerHTML=_,M=o(),h=s("br"),c=o(),m=s("p"),m.innerHTML=f},l(a){i=p(a,"P",{"data-svelte-h":!0}),d(i)!=="svelte-oq75au"&&(i.innerHTML=_),M=r(a),h=p(a,"BR",{}),c=r(a),m=p(a,"P",{"data-svelte-h":!0}),d(m)!=="svelte-1x8iya3"&&(m.innerHTML=f)},m(a,u){n(a,i,u),n(a,M,u),n(a,h,u),n(a,c,u),n(a,m,u)},p:ae,d(a){a&&(l(i),l(M),l(h),l(c),l(m))}}}function ue(j){let i,_,M,h,c,m,f,a='之前，在 Mac 上训练模型仅限于使用 CPU 训练。不过随着PyTorch v1.12的发布，您可以通过在 Apple Silicon 芯片的 GPU 上训练模型来显著提高性能和训练速度。这是通过将 Apple 的 Metal 性能着色器 (Metal Performance Shaders, MPS) 作为后端集成到PyTorch中实现的。<a href="https://pytorch.org/docs/stable/notes/mps.html" rel="nofollow">MPS后端</a> 将 PyTorch 操作视为自定义的 Metal 着色器来实现，并将对应模块部署到<code>mps</code>设备上。',u,y,X,T,B="配置好<code>mps</code>设备后，您可以：",E,U,I="<li>在本地训练更大的网络或更大的批量大小</li> <li>降低数据获取延迟，因为 GPU 的统一内存架构允许直接访问整个内存存储</li> <li>降低成本，因为您不需要再在云端 GPU 上训练或增加额外的本地 GPU</li>",N,$,z="在确保已安装PyTorch后就可以开始使用了。 MPS 加速支持macOS 12.3及以上版本。",G,w,Q,b,q='<code>TrainingArguments</code>类默认使用<code>mps</code>设备(如果可用)因此无需显式设置设备。例如，您可以直接运行<a href="https://github.com/huggingface/transformers/blob/main/examples/pytorch/text-classification/run_glue.py" rel="nofollow">run_glue.py</a>脚本，在无需进行任何修改的情况下自动启用 MPS 后端。',W,P,k,v,D='用于<a href="https://pytorch.org/docs/stable/distributed.html#backends" rel="nofollow">分布式设置</a>的后端(如<code>gloo</code>和<code>nccl</code>)不支持<code>mps</code>设备，这也意味着使用 MPS 后端时只能在单个 GPU 上进行训练。',R,C,K='您可以在<a href="https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/" rel="nofollow">Introducing Accelerated PyTorch Training on Mac</a>博客文章中了解有关 MPS 后端的更多信息。',F,S,V,A,Y;return c=new me({props:{title:"在 Apple Silicon 芯片上进行 PyTorch 训练",local:"在-apple-silicon-芯片上进行-pytorch-训练",headingTag:"h1"}}),y=new ce({props:{warning:!0,$$slots:{default:[fe]},$$scope:{ctx:j}}}),w=new le({props:{code:"cGlwJTIwaW5zdGFsbCUyMHRvcmNoJTIwdG9yY2h2aXNpb24lMjB0b3JjaGF1ZGlv",highlighted:"pip install torch torchvision torchaudio",wrap:!1}}),P=new le({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}}),S=new Me({props:{source:"https://github.com/huggingface/transformers/blob/main/docs/source/zh/perf_train_special.md"}}),{c(){i=s("meta"),_=o(),M=s("p"),h=o(),g(c.$$.fragment),m=o(),f=s("p"),f.innerHTML=a,u=o(),g(y.$$.fragment),X=o(),T=s("p"),T.innerHTML=B,E=o(),U=s("ul"),U.innerHTML=I,N=o(),$=s("p"),$.textContent=z,G=o(),g(w.$$.fragment),Q=o(),b=s("p"),b.innerHTML=q,W=o(),g(P.$$.fragment),k=o(),v=s("p"),v.innerHTML=D,R=o(),C=s("p"),C.innerHTML=K,F=o(),g(S.$$.fragment),V=o(),A=s("p"),this.h()},l(e){const t=se("svelte-u9bgzb",document.head);i=p(t,"META",{name:!0,content:!0}),t.forEach(l),_=r(e),M=p(e,"P",{}),ee(M).forEach(l),h=r(e),L(c.$$.fragment,e),m=r(e),f=p(e,"P",{"data-svelte-h":!0}),d(f)!=="svelte-1ve9m0l"&&(f.innerHTML=a),u=r(e),L(y.$$.fragment,e),X=r(e),T=p(e,"P",{"data-svelte-h":!0}),d(T)!=="svelte-xvtsug"&&(T.innerHTML=B),E=r(e),U=p(e,"UL",{"data-svelte-h":!0}),d(U)!=="svelte-5fu39o"&&(U.innerHTML=I),N=r(e),$=p(e,"P",{"data-svelte-h":!0}),d($)!=="svelte-7whwsu"&&($.textContent=z),G=r(e),L(w.$$.fragment,e),Q=r(e),b=p(e,"P",{"data-svelte-h":!0}),d(b)!=="svelte-1etdm8m"&&(b.innerHTML=q),W=r(e),L(P.$$.fragment,e),k=r(e),v=p(e,"P",{"data-svelte-h":!0}),d(v)!=="svelte-1m152z5"&&(v.innerHTML=D),R=r(e),C=p(e,"P",{"data-svelte-h":!0}),d(C)!=="svelte-qu3rh1"&&(C.innerHTML=K),F=r(e),L(S.$$.fragment,e),V=r(e),A=p(e,"P",{}),ee(A).forEach(l),this.h()},h(){te(i,"name","hf:doc:metadata"),te(i,"content",he)},m(e,t){pe(document.head,i),n(e,_,t),n(e,M,t),n(e,h,t),Z(c,e,t),n(e,m,t),n(e,f,t),n(e,u,t),Z(y,e,t),n(e,X,t),n(e,T,t),n(e,E,t),n(e,U,t),n(e,N,t),n(e,$,t),n(e,G,t),Z(w,e,t),n(e,Q,t),n(e,b,t),n(e,W,t),Z(P,e,t),n(e,k,t),n(e,v,t),n(e,R,t),n(e,C,t),n(e,F,t),Z(S,e,t),n(e,V,t),n(e,A,t),Y=!0},p(e,[t]){const O={};t&2&&(O.$$scope={dirty:t,ctx:e}),y.$set(O)},i(e){Y||(x(c.$$.fragment,e),x(y.$$.fragment,e),x(w.$$.fragment,e),x(P.$$.fragment,e),x(S.$$.fragment,e),Y=!0)},o(e){J(c.$$.fragment,e),J(y.$$.fragment,e),J(w.$$.fragment,e),J(P.$$.fragment,e),J(S.$$.fragment,e),Y=!1},d(e){e&&(l(_),l(M),l(h),l(m),l(f),l(u),l(X),l(T),l(E),l(U),l(N),l($),l(G),l(Q),l(b),l(W),l(k),l(v),l(R),l(C),l(F),l(V),l(A)),l(i),H(c,e),H(y,e),H(w,e),H(P,e),H(S,e)}}}const he='{"title":"在 Apple Silicon 芯片上进行 PyTorch 训练","local":"在-apple-silicon-芯片上进行-pytorch-训练","sections":[],"depth":1}';function de(j){return ie(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class we extends oe{constructor(i){super(),re(this,i,de,ue,ne,{})}}export{we as component};
