import{s as Lt,n as Jt,o as zt}from"../chunks/scheduler.25b97de1.js";import{S as At,i as Rt,g as p,s as i,r as o,A as kt,h as r,f as l,c as a,j as Xt,u as m,x as s,k as Et,y as Bt,a as n,v as u,d,t as c,w as h}from"../chunks/index.d9030fc9.js";import{C as Ht}from"../chunks/CodeBlock.e6cd0d95.js";import{H as S,E as jt}from"../chunks/EditOnGithub.91d95064.js";function St(ct){let f,q,B,G,_,V,$,ht="本指南将重点介绍如何在CPU上高效训练大型模型。",W,M,Y,b,_t="混合精度训练在模型中可以同时使用单精度（fp32）和半精度（bf16/fp16）的数据类型来加速训练或推理过程，并且仍然能保留大部分单精度的准确性。现代的CPU，例如第三代、第四代和第五代Intel® Xeon® Scalable处理器，原生支持bf16，而第六代Intel® Xeon® Scalable处理器原生支持bf16和fp16。您在训练时启用bf16或fp16的混合精度训练可以直接提高处理性能。",F,C,$t="为了进一步最大化训练性能，您可以使用Intel® PyTorch扩展（IPEX）。IPEX是一个基于PyTorch构建的库，增加了额外的CPU指令集架构（ISA）级别的支持，比如Intel®高级向量扩展512（Intel® AVX512-VNNI）和Intel®高级矩阵扩展（Intel® AMX）。这为Intel CPU提供额外的性能提升。然而，仅支持AVX2的CPU（例如AMD或较旧的Intel CPU）在使用IPEX时并不保证能提高性能。",Z,P,Mt="从PyTorch 1.10版本起，CPU后端已经启用了自动混合精度（AMP）。IPEX还支持bf16/fp16的AMP和bf16/fp16算子优化，并且部分功能已经上游到PyTorch主分支。通过IPEX AMP，您可以获得更好的性能和用户体验。",Q,T,bt='点击<a href="https://intel.github.io/intel-extension-for-pytorch/cpu/latest/tutorials/features/amp.html" rel="nofollow">这里</a>查看<strong>自动混合精度</strong>的更多详细信息。',N,x,D,g,Ct="IPEX 的发布与 PyTorch 一致，您可以通过 pip 安装：",K,y,Pt='<thead><tr><th align="center">PyTorch Version</th> <th align="center">IPEX version</th></tr></thead> <tbody><tr><td align="center">2.5.0</td> <td align="center">2.5.0+cpu</td></tr> <tr><td align="center">2.4.0</td> <td align="center">2.4.0+cpu</td></tr> <tr><td align="center">2.3.0</td> <td align="center">2.3.0+cpu</td></tr> <tr><td align="center">2.2.0</td> <td align="center">2.2.0+cpu</td></tr></tbody>',O,v,Tt="请运行 <code>pip list | grep torch</code> 以获取您的 <code>pytorch_version</code>，然后根据该版本安装相应的 <code>IPEX version_name</code>。",tt,w,et,I,xt='如果需要的话，您可以在 <a href="https://developer.intel.com/ipex-whl-stable-cpu" rel="nofollow">ipex-whl-stable-cpu</a> 查看最新版本。',lt,U,gt='查看更多 <a href="https://intel.github.io/intel-extension-for-pytorch/cpu/latest/tutorials/installation.html" rel="nofollow">安装IPEX</a> 的方法。',nt,X,it,E,yt="在 Trainer 中使用 IPEX 时，您应在训练命令参数中添加 <code>use_ipex</code>、<code>bf16</code> 或 <code>fp16</code> 以及 <code>no_cuda</code> 来启用自动混合精度。",at,H,vt='以 <a href="https://github.com/huggingface/transformers/tree/main/examples/pytorch/question-answering" rel="nofollow">Transformers 问答任务</a>为例：',pt,L,wt=`<li>在 CPU 上使用 BF16 自动混合精度训练 IPEX 的示例如下：<pre> python examples/pytorch/question-answering/run_qa.py \\
--model_name_or_path google-bert/bert-base-uncased \\
--dataset_name squad \\
--do_train \\
--do_eval \\
--per_device_train_batch_size 12 \\
--learning_rate 3e-5 \\
--num_train_epochs 2 \\
--max_seq_length 384 \\
--doc_stride 128 \\
--output_dir /tmp/debug_squad/ \\
<b>--use_ipex</b> \\
<b>--bf16</b> \\
<b>--use_cpu</b></pre></li>`,rt,J,It="如果您想在脚本中启用 <code>use_ipex</code> 和 <code>bf16</code>，请像下面这样将这些参数添加到 <code>TrainingArguments</code> 中：",st,z,ft,A,ot,R,Ut='博客: <a href="https://huggingface.co/blog/intel-sapphire-rapids" rel="nofollow">使用 Intel Sapphire Rapids 加速 PyTorch Transformers</a>',mt,k,ut,j,dt;return _=new S({props:{title:"在CPU上进行高效训练",local:"在cpu上进行高效训练",headingTag:"h1"}}),M=new S({props:{title:"使用IPEX进行混合精度训练",local:"使用ipex进行混合精度训练",headingTag:"h2"}}),x=new S({props:{title:"IPEX 安装:",local:"ipex-安装",headingTag:"h3"}}),w=new Ht({props:{code:"cGlwJTIwaW5zdGFsbCUyMGludGVsX2V4dGVuc2lvbl9mb3JfcHl0b3JjaCUzRCUzRCUzQ3ZlcnNpb25fbmFtZSUzRSUyMC1mJTIwaHR0cHMlM0ElMkYlMkZkZXZlbG9wZXIuaW50ZWwuY29tJTJGaXBleC13aGwtc3RhYmxlLWNwdQ==",highlighted:"pip install intel_extension_for_pytorch==&lt;version_name&gt; -f https://developer.intel.com/ipex-whl-stable-cpu",wrap:!1}}),X=new S({props:{title:"在 Trainer 中使用 IPEX",local:"在-trainer-中使用-ipex",headingTag:"h3"}}),z=new Ht({props:{code:"dHJhaW5pbmdfYXJncyUyMCUzRCUyMFRyYWluaW5nQXJndW1lbnRzKCUwQSUyMCUyMCUyMCUyMG91dHB1dF9kaXIlM0RhcmdzLm91dHB1dF9wYXRoJTJDJTBBJTJCJTIwJTIwJTIwYmYxNiUzRFRydWUlMkMlMEElMkIlMjAlMjAlMjB1c2VfaXBleCUzRFRydWUlMkMlMEElMkIlMjAlMjAlMjB1c2VfY3B1JTNEVHJ1ZSUyQyUwQSUyMCUyMCUyMCUyMCoqa3dhcmdzJTBBKQ==",highlighted:`training_args = TrainingArguments(
    output_dir=args.output_path,
<span class="hljs-addition">+   bf16=True,</span>
<span class="hljs-addition">+   use_ipex=True,</span>
<span class="hljs-addition">+   use_cpu=True,</span>
    **kwargs
)`,wrap:!1}}),A=new S({props:{title:"实践示例",local:"实践示例",headingTag:"h3"}}),k=new jt({props:{source:"https://github.com/huggingface/transformers/blob/main/docs/source/zh/perf_train_cpu.md"}}),{c(){f=p("meta"),q=i(),B=p("p"),G=i(),o(_.$$.fragment),V=i(),$=p("p"),$.textContent=ht,W=i(),o(M.$$.fragment),Y=i(),b=p("p"),b.textContent=_t,F=i(),C=p("p"),C.textContent=$t,Z=i(),P=p("p"),P.textContent=Mt,Q=i(),T=p("p"),T.innerHTML=bt,N=i(),o(x.$$.fragment),D=i(),g=p("p"),g.textContent=Ct,K=i(),y=p("table"),y.innerHTML=Pt,O=i(),v=p("p"),v.innerHTML=Tt,tt=i(),o(w.$$.fragment),et=i(),I=p("p"),I.innerHTML=xt,lt=i(),U=p("p"),U.innerHTML=gt,nt=i(),o(X.$$.fragment),it=i(),E=p("p"),E.innerHTML=yt,at=i(),H=p("p"),H.innerHTML=vt,pt=i(),L=p("ul"),L.innerHTML=wt,rt=i(),J=p("p"),J.innerHTML=It,st=i(),o(z.$$.fragment),ft=i(),o(A.$$.fragment),ot=i(),R=p("p"),R.innerHTML=Ut,mt=i(),o(k.$$.fragment),ut=i(),j=p("p"),this.h()},l(t){const e=kt("svelte-u9bgzb",document.head);f=r(e,"META",{name:!0,content:!0}),e.forEach(l),q=a(t),B=r(t,"P",{}),Xt(B).forEach(l),G=a(t),m(_.$$.fragment,t),V=a(t),$=r(t,"P",{"data-svelte-h":!0}),s($)!=="svelte-ki35fh"&&($.textContent=ht),W=a(t),m(M.$$.fragment,t),Y=a(t),b=r(t,"P",{"data-svelte-h":!0}),s(b)!=="svelte-1b1ob67"&&(b.textContent=_t),F=a(t),C=r(t,"P",{"data-svelte-h":!0}),s(C)!=="svelte-a9kmph"&&(C.textContent=$t),Z=a(t),P=r(t,"P",{"data-svelte-h":!0}),s(P)!=="svelte-177ubpm"&&(P.textContent=Mt),Q=a(t),T=r(t,"P",{"data-svelte-h":!0}),s(T)!=="svelte-ymi5hy"&&(T.innerHTML=bt),N=a(t),m(x.$$.fragment,t),D=a(t),g=r(t,"P",{"data-svelte-h":!0}),s(g)!=="svelte-z1n0yn"&&(g.textContent=Ct),K=a(t),y=r(t,"TABLE",{"data-svelte-h":!0}),s(y)!=="svelte-p1edgy"&&(y.innerHTML=Pt),O=a(t),v=r(t,"P",{"data-svelte-h":!0}),s(v)!=="svelte-38qae9"&&(v.innerHTML=Tt),tt=a(t),m(w.$$.fragment,t),et=a(t),I=r(t,"P",{"data-svelte-h":!0}),s(I)!=="svelte-1wtnrqq"&&(I.innerHTML=xt),lt=a(t),U=r(t,"P",{"data-svelte-h":!0}),s(U)!=="svelte-12at55l"&&(U.innerHTML=gt),nt=a(t),m(X.$$.fragment,t),it=a(t),E=r(t,"P",{"data-svelte-h":!0}),s(E)!=="svelte-czc5n2"&&(E.innerHTML=yt),at=a(t),H=r(t,"P",{"data-svelte-h":!0}),s(H)!=="svelte-1xmnpzz"&&(H.innerHTML=vt),pt=a(t),L=r(t,"UL",{"data-svelte-h":!0}),s(L)!=="svelte-x6yhj6"&&(L.innerHTML=wt),rt=a(t),J=r(t,"P",{"data-svelte-h":!0}),s(J)!=="svelte-15u687y"&&(J.innerHTML=It),st=a(t),m(z.$$.fragment,t),ft=a(t),m(A.$$.fragment,t),ot=a(t),R=r(t,"P",{"data-svelte-h":!0}),s(R)!=="svelte-639blr"&&(R.innerHTML=Ut),mt=a(t),m(k.$$.fragment,t),ut=a(t),j=r(t,"P",{}),Xt(j).forEach(l),this.h()},h(){Et(f,"name","hf:doc:metadata"),Et(f,"content",qt)},m(t,e){Bt(document.head,f),n(t,q,e),n(t,B,e),n(t,G,e),u(_,t,e),n(t,V,e),n(t,$,e),n(t,W,e),u(M,t,e),n(t,Y,e),n(t,b,e),n(t,F,e),n(t,C,e),n(t,Z,e),n(t,P,e),n(t,Q,e),n(t,T,e),n(t,N,e),u(x,t,e),n(t,D,e),n(t,g,e),n(t,K,e),n(t,y,e),n(t,O,e),n(t,v,e),n(t,tt,e),u(w,t,e),n(t,et,e),n(t,I,e),n(t,lt,e),n(t,U,e),n(t,nt,e),u(X,t,e),n(t,it,e),n(t,E,e),n(t,at,e),n(t,H,e),n(t,pt,e),n(t,L,e),n(t,rt,e),n(t,J,e),n(t,st,e),u(z,t,e),n(t,ft,e),u(A,t,e),n(t,ot,e),n(t,R,e),n(t,mt,e),u(k,t,e),n(t,ut,e),n(t,j,e),dt=!0},p:Jt,i(t){dt||(d(_.$$.fragment,t),d(M.$$.fragment,t),d(x.$$.fragment,t),d(w.$$.fragment,t),d(X.$$.fragment,t),d(z.$$.fragment,t),d(A.$$.fragment,t),d(k.$$.fragment,t),dt=!0)},o(t){c(_.$$.fragment,t),c(M.$$.fragment,t),c(x.$$.fragment,t),c(w.$$.fragment,t),c(X.$$.fragment,t),c(z.$$.fragment,t),c(A.$$.fragment,t),c(k.$$.fragment,t),dt=!1},d(t){t&&(l(q),l(B),l(G),l(V),l($),l(W),l(Y),l(b),l(F),l(C),l(Z),l(P),l(Q),l(T),l(N),l(D),l(g),l(K),l(y),l(O),l(v),l(tt),l(et),l(I),l(lt),l(U),l(nt),l(it),l(E),l(at),l(H),l(pt),l(L),l(rt),l(J),l(st),l(ft),l(ot),l(R),l(mt),l(ut),l(j)),l(f),h(_,t),h(M,t),h(x,t),h(w,t),h(X,t),h(z,t),h(A,t),h(k,t)}}}const qt='{"title":"在CPU上进行高效训练","local":"在cpu上进行高效训练","sections":[{"title":"使用IPEX进行混合精度训练","local":"使用ipex进行混合精度训练","sections":[{"title":"IPEX 安装:","local":"ipex-安装","sections":[],"depth":3},{"title":"在 Trainer 中使用 IPEX","local":"在-trainer-中使用-ipex","sections":[],"depth":3},{"title":"实践示例","local":"实践示例","sections":[],"depth":3}],"depth":2}],"depth":1}';function Gt(ct){return zt(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Zt extends At{constructor(f){super(),Rt(this,f,Gt,St,Lt,{})}}export{Zt as component};
