import{s as De,o as Ke,n as et}from"../chunks/scheduler.56725da7.js";import{S as tt,i as nt,e as i,s,c as u,h as lt,a,d as n,b as o,f as Ye,g as c,j as r,k as Oe,l as st,m as l,n as d,t as f,o as h,p as y}from"../chunks/index.18a26576.js";import{T as ot}from"../chunks/Tip.5b941656.js";import{C as it}from"../chunks/CopyLLMTxtMenu.f644b58a.js";import{C as je}from"../chunks/CodeBlock.a5342303.js";import{H as Y}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.401933eb.js";function at(O){let p,M='We usually test smaller checkpoints to accelerate the CIs, you could find tiny models for testing under the <a href="https://huggingface.co/hf-internal-testing" rel="nofollow"><code>Hugging Face Internal Testing Organization</code></a>.';return{c(){p=i("p"),p.innerHTML=M},l(m){p=a(m,"P",{"data-svelte-h":!0}),r(p)!=="svelte-r7v6p2"&&(p.innerHTML=M)},m(m,A){l(m,p,A)},p:et,d(m){m&&n(p)}}}function rt(O){let p,M,m,A,b,D,T,K,w,Ne="<p><strong><em>NOTE:</em></strong> ❗This section does not apply to the decoder model’s inference with autoregressive sampling integrated through <code>transformers-neuronx</code>. If you want to add support for these models, please open an issue on the Optimum Neuron GitHub repo, and ping maintainers for help.</p>",ee,v,ke='You want to export and run a new model on AWS Inferentia or Trainium? Check the guideline, and submit a pull request to <a href="https://github.com/huggingface/optimum-neuron/" rel="nofollow">🤗 Optimum Neuron’s GitHub repo</a>!',te,C,He="To support a new model architecture in the Optimum Neuron library here are some steps to follow:",ne,$,Xe="<li>Implement a custom Neuron configuration.</li> <li>Export and validate the model.</li> <li>Contribute to the GitHub repo.</li>",le,_,se,x,Ze="To support the export of a new model to a Neuron compatible format, the first thing to do is to define a Neuron configuration, describing how to export the PyTorch model by specifying:",oe,U,Ve="<li>The input names.</li> <li>The output names.</li> <li>The dummy inputs used to trace the model: the Neuron Compiler records the computational graph via tracing and works on the resulting <code>TorchScript</code> module.</li> <li>The compilation arguments used to control the trade-off between hardware efficiency (latency, throughput) and accuracy.</li>",ie,J,Ie=`Depending on the choice of model and task, we represent the data above with configuration classes. Each configuration class is associated with
a specific model architecture, and follows the naming convention <code>ArchitectureNameNeuronConfig</code>. For instance, the configuration that specifies the Neuron
export of BERT models is <code>BertNeuronConfig</code>.`,ae,j,Le="Since many architectures share similar properties for their Neuron configuration, 🤗 Optimum adopts a 3-level class hierarchy:",re,N,Ge="<li>Abstract and generic base classes. These handle all the fundamental features, while being agnostic to the modality (text, image, audio, etc).</li> <li>Middle-end classes. These are aware of the modality. Multiple config classes could exist for the same modality, depending on the inputs they support. They specify which input generators should be used for generating the dummy inputs, but remain model-agnostic.</li> <li>Model-specific classes like the <code>BertNeuronConfig</code> mentioned above. These are the ones actually used to export models.</li>",pe,k,me,H,Ee='Here we take the support of <a href="https://huggingface.co/docs/transformers/model_doc/esm#esm" rel="nofollow">ESM models</a> as an example. Let’s create an <code>EsmNeuronConfig</code> class in the <code>optimum/exporters/neuron/model_configs.py</code>.',ue,X,Re=`When an Esm model interprets as a text encoder, we are able to inherit from the middle-end class <a href="https://github.com/huggingface/optimum-neuron/blob/v0.0.18/optimum/exporters/neuron/config.py#L36" rel="nofollow"><code>TextEncoderNeuronConfig</code></a>.
Since the modeling and configuration of Esm is almost the same as BERT when it is interpreted as an encoder, we can use the <code>NormalizedConfigManager</code> with <code>model_type=bert</code> to normalize the configuration to generate dummy inputs for tracing the model.`,ce,Z,Be='And one last step, since <code>optimum-neuron</code> is an extension of <code>optimum</code>, we need to register the Neuron config that we create to the <a href="https://huggingface.co/docs/optimum/main/en/exporters/task_manager#optimum.exporters.TasksManager" rel="nofollow">TasksManager</a> with the <code>register_in_tasks_manager</code> decorator by specifying the model type and supported tasks.',de,V,fe,I,he,L,qe="With the Neuron configuration class that you implemented, now do a quick test if it works as expected:",ye,G,We="<li>Export</li>",ge,E,Me,R,Fe='During the export <a href="https://github.com/huggingface/optimum-neuron/blob/7b18de9ddfa5c664c94051304c651eaf855c3e0b/optimum/exporters/neuron/convert.py#L136" rel="nofollow"><code>validate_model_outputs</code></a> will be called to validate the outputs of your exported Neuron model by comparing them to the results of PyTorch on the CPU. You could also validate the model manually with:',be,B,Te,q,Se="<li>Inference (optional)</li>",we,W,ve,F,Ce,S,ze="We are almost all set. Now submit a pull request to make your work accessible to all community members!",$e,z,Qe='<li>Open an issue in the <a href="https://github.com/huggingface/optimum-neuron/issues" rel="nofollow">Optimum Neuron GitHub repo</a> to describe the new feature and make it visible to Optimum Neuron’s maintainers.</li> <li>Add the model to the exporter test in <a href="https://github.com/huggingface/optimum-neuron/blob/v0.0.18/tests/exporters/exporters_utils.py" rel="nofollow"><code>optimum-neuron/tests/exporters/exporters_utils.py</code></a> and the inference test in <a href="https://github.com/huggingface/optimum-neuron/blob/v0.0.18/tests/inference/inference_utils.py" rel="nofollow"><code>optimum-neuron/tests/inference/inference_utils.py</code></a>.</li> <li>Open a pull request! (Don’t forget to link it to the issue you opened, so that the maintainers could better track it and provide help when needed.)</li>',_e,g,xe,Q,Ae="You have made a new model accessible on Neuron for the community! Thanks for joining us in the endeavor of democratizing good machine learning 🤗.",Ue,P,Je;return b=new it({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),T=new Y({props:{title:"Adding support for new architectures",local:"adding-support-for-new-architectures",headingTag:"h1"}}),_=new Y({props:{title:"Implement a custom Neuron configuration",local:"implement-a-custom-neuron-configuration",headingTag:"h2"}}),k=new Y({props:{title:"Example: Adding support for ESM models",local:"example-adding-support-for-esm-models",headingTag:"h3"}}),V=new je({props:{code:"JTBBJTQwcmVnaXN0ZXJfaW5fdGFza3NfbWFuYWdlciglMjJlc20lMjIlMkMlMjAqJTVCJTIyZmVhdHVyZS1leHRyYWN0aW9uJTIyJTJDJTIwJTIyZmlsbC1tYXNrJTIyJTJDJTIwJTIydGV4dC1jbGFzc2lmaWNhdGlvbiUyMiUyQyUyMCUyMnRva2VuLWNsYXNzaWZpY2F0aW9uJTIyJTVEKSUwQWNsYXNzJTIwRXNtTmV1cm9uQ29uZmlnKFRleHRFbmNvZGVyTmV1cm9uQ29uZmlnKSUzQSUwQSUyMCUyMCUyMCUyME5PUk1BTElaRURfQ09ORklHX0NMQVNTJTIwJTNEJTIwTm9ybWFsaXplZENvbmZpZ01hbmFnZXIuZ2V0X25vcm1hbGl6ZWRfY29uZmlnX2NsYXNzKCUyMmJlcnQlMjIpJTBBJTIwJTIwJTIwJTIwQVRPTF9GT1JfVkFMSURBVElPTiUyMCUzRCUyMDFlLTMlMjAlMjAlMjMlMjBhYnNvbHV0ZSUyMHRvbGVyYW5jZSUyMHRvJTIwY29tcGFyZSUyMGZvciUyMGNvbXBhcmluZyUyMG1vZGVsJTIwb24lMjBDUFVzJTBBJTBBJTIwJTIwJTIwJTIwJTQwcHJvcGVydHklMEElMjAlMjAlMjAlMjBkZWYlMjBpbnB1dHMoc2VsZiklMjAtJTNFJTIwTGlzdCU1QnN0ciU1RCUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHJldHVybiUyMCU1QiUyMmlucHV0X2lkcyUyMiUyQyUyMCUyMmF0dGVudGlvbl9tYXNrJTIyJTVEJTBB",highlighted:`
<span class="hljs-meta">@register_in_tasks_manager(<span class="hljs-params"><span class="hljs-string">&quot;esm&quot;</span>, *[<span class="hljs-string">&quot;feature-extraction&quot;</span>, <span class="hljs-string">&quot;fill-mask&quot;</span>, <span class="hljs-string">&quot;text-classification&quot;</span>, <span class="hljs-string">&quot;token-classification&quot;</span>]</span>)</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">EsmNeuronConfig</span>(<span class="hljs-title class_ inherited__">TextEncoderNeuronConfig</span>):
    NORMALIZED_CONFIG_CLASS = NormalizedConfigManager.get_normalized_config_class(<span class="hljs-string">&quot;bert&quot;</span>)
    ATOL_FOR_VALIDATION = <span class="hljs-number">1e-3</span>  <span class="hljs-comment"># absolute tolerance to compare for comparing model on CPUs</span>

<span class="hljs-meta">    @property</span>
    <span class="hljs-keyword">def</span> <span class="hljs-title function_">inputs</span>(<span class="hljs-params">self</span>) -&gt; <span class="hljs-type">List</span>[<span class="hljs-built_in">str</span>]:
        <span class="hljs-keyword">return</span> [<span class="hljs-string">&quot;input_ids&quot;</span>, <span class="hljs-string">&quot;attention_mask&quot;</span>]
`,wrap:!1}}),I=new Y({props:{title:"Export and validate the model",local:"export-and-validate-the-model",headingTag:"h2"}}),E=new je({props:{code:"b3B0aW11bS1jbGklMjBleHBvcnQlMjBuZXVyb24lMjAtLW1vZGVsJTIwZmFjZWJvb2slMkZlc20yX3QzM182NTBNX1VSNTBEJTIwLS10YXNrJTIwdGV4dC1jbGFzc2lmaWNhdGlvbiUyMC0tYmF0Y2hfc2l6ZSUyMDElMjAtLXNlcXVlbmNlX2xlbmd0aCUyMDE2JTIwZXNtX25ldXJvbiUyRg==",highlighted:'optimum-cli <span class="hljs-built_in">export</span> neuron --model facebook/esm2_t33_650M_UR50D --task text-classification --batch_size 1 --sequence_length 16 esm_neuron/',wrap:!1}}),B=new je({props:{code:"ZnJvbSUyMG9wdGltdW0uZXhwb3J0ZXJzLm5ldXJvbiUyMGltcG9ydCUyMHZhbGlkYXRlX21vZGVsX291dHB1dHMlMEElMEF2YWxpZGF0ZV9tb2RlbF9vdXRwdXRzKCUwQSUyMCUyMCUyMCUyMG5ldXJvbl9jb25maWclMkMlMjBiYXNlX21vZGVsJTJDJTIwbmV1cm9uX21vZGVsX3BhdGglMkMlMjBuZXVyb25fbmFtZWRfb3V0cHV0cyUyQyUyMG5ldXJvbl9jb25maWcuQVRPTF9GT1JfVkFMSURBVElPTiUwQSk=",highlighted:`<span class="hljs-keyword">from</span> optimum.exporters.neuron <span class="hljs-keyword">import</span> validate_model_outputs

validate_model_outputs(
    neuron_config, base_model, neuron_model_path, neuron_named_outputs, neuron_config.ATOL_FOR_VALIDATION
)`,wrap:!1}}),W=new je({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Ub2tlbml6ZXIlMEFmcm9tJTIwb3B0aW11bS5uZXVyb24lMjBpbXBvcnQlMjBOZXVyb25Nb2RlbEZvclNlcXVlbmNlQ2xhc3NpZmljYXRpb24lMEElMEFtb2RlbCUyMCUzRCUyME5ldXJvbk1vZGVsRm9yU2VxdWVuY2VDbGFzc2lmaWNhdGlvbi5mcm9tX3ByZXRyYWluZWQoJTIyZXNtX25ldXJvbiUyRiUyMiklMEF0b2tlbml6ZXIlMjAlM0QlMjBBdXRvVG9rZW5pemVyLmZyb21fcHJldHJhaW5lZCglMjJlc21fbmV1cm9uJTJGJTIyKSUwQWlucHV0cyUyMCUzRCUyMHRva2VuaXplciglMjJIZWxsbyUyQyUyMG15JTIwZG9nJTIwaXMlMjBjdXRlJTIyJTJDJTIwcmV0dXJuX3RlbnNvcnMlM0QlMjJwdCUyMiklMEFsb2dpdHMlMjAlM0QlMjBtb2RlbCgqKmlucHV0cykubG9naXRz",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer
<span class="hljs-keyword">from</span> optimum.neuron <span class="hljs-keyword">import</span> NeuronModelForSequenceClassification

model = NeuronModelForSequenceClassification.from_pretrained(<span class="hljs-string">&quot;esm_neuron/&quot;</span>)
tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">&quot;esm_neuron/&quot;</span>)
inputs = tokenizer(<span class="hljs-string">&quot;Hello, my dog is cute&quot;</span>, return_tensors=<span class="hljs-string">&quot;pt&quot;</span>)
logits = model(**inputs).logits`,wrap:!1}}),F=new Y({props:{title:"Contribute to the GitHub repo",local:"contribute-to-the-github-repo",headingTag:"h2"}}),g=new ot({props:{$$slots:{default:[at]},$$scope:{ctx:O}}}),{c(){p=i("meta"),M=s(),m=i("p"),A=s(),u(b.$$.fragment),D=s(),u(T.$$.fragment),K=s(),w=i("blockquote"),w.innerHTML=Ne,ee=s(),v=i("p"),v.innerHTML=ke,te=s(),C=i("p"),C.textContent=He,ne=s(),$=i("ol"),$.innerHTML=Xe,le=s(),u(_.$$.fragment),se=s(),x=i("p"),x.textContent=Ze,oe=s(),U=i("ol"),U.innerHTML=Ve,ie=s(),J=i("p"),J.innerHTML=Ie,ae=s(),j=i("p"),j.textContent=Le,re=s(),N=i("ol"),N.innerHTML=Ge,pe=s(),u(k.$$.fragment),me=s(),H=i("p"),H.innerHTML=Ee,ue=s(),X=i("p"),X.innerHTML=Re,ce=s(),Z=i("p"),Z.innerHTML=Be,de=s(),u(V.$$.fragment),fe=s(),u(I.$$.fragment),he=s(),L=i("p"),L.textContent=qe,ye=s(),G=i("ul"),G.innerHTML=We,ge=s(),u(E.$$.fragment),Me=s(),R=i("p"),R.innerHTML=Fe,be=s(),u(B.$$.fragment),Te=s(),q=i("ul"),q.innerHTML=Se,we=s(),u(W.$$.fragment),ve=s(),u(F.$$.fragment),Ce=s(),S=i("p"),S.textContent=ze,$e=s(),z=i("ul"),z.innerHTML=Qe,_e=s(),u(g.$$.fragment),xe=s(),Q=i("p"),Q.textContent=Ae,Ue=s(),P=i("p"),this.h()},l(e){const t=lt("svelte-u9bgzb",document.head);p=a(t,"META",{name:!0,content:!0}),t.forEach(n),M=o(e),m=a(e,"P",{}),Ye(m).forEach(n),A=o(e),c(b.$$.fragment,e),D=o(e),c(T.$$.fragment,e),K=o(e),w=a(e,"BLOCKQUOTE",{"data-svelte-h":!0}),r(w)!=="svelte-1iqy6mr"&&(w.innerHTML=Ne),ee=o(e),v=a(e,"P",{"data-svelte-h":!0}),r(v)!=="svelte-d0p6r1"&&(v.innerHTML=ke),te=o(e),C=a(e,"P",{"data-svelte-h":!0}),r(C)!=="svelte-1awdjch"&&(C.textContent=He),ne=o(e),$=a(e,"OL",{"data-svelte-h":!0}),r($)!=="svelte-1x19lno"&&($.innerHTML=Xe),le=o(e),c(_.$$.fragment,e),se=o(e),x=a(e,"P",{"data-svelte-h":!0}),r(x)!=="svelte-12rzy01"&&(x.textContent=Ze),oe=o(e),U=a(e,"OL",{"data-svelte-h":!0}),r(U)!=="svelte-1bbfvek"&&(U.innerHTML=Ve),ie=o(e),J=a(e,"P",{"data-svelte-h":!0}),r(J)!=="svelte-1ez6yaq"&&(J.innerHTML=Ie),ae=o(e),j=a(e,"P",{"data-svelte-h":!0}),r(j)!=="svelte-tqmfwt"&&(j.textContent=Le),re=o(e),N=a(e,"OL",{"data-svelte-h":!0}),r(N)!=="svelte-wqsfdl"&&(N.innerHTML=Ge),pe=o(e),c(k.$$.fragment,e),me=o(e),H=a(e,"P",{"data-svelte-h":!0}),r(H)!=="svelte-krly5h"&&(H.innerHTML=Ee),ue=o(e),X=a(e,"P",{"data-svelte-h":!0}),r(X)!=="svelte-3w7mfw"&&(X.innerHTML=Re),ce=o(e),Z=a(e,"P",{"data-svelte-h":!0}),r(Z)!=="svelte-11pzsso"&&(Z.innerHTML=Be),de=o(e),c(V.$$.fragment,e),fe=o(e),c(I.$$.fragment,e),he=o(e),L=a(e,"P",{"data-svelte-h":!0}),r(L)!=="svelte-1ca0k65"&&(L.textContent=qe),ye=o(e),G=a(e,"UL",{"data-svelte-h":!0}),r(G)!=="svelte-1j7x565"&&(G.innerHTML=We),ge=o(e),c(E.$$.fragment,e),Me=o(e),R=a(e,"P",{"data-svelte-h":!0}),r(R)!=="svelte-1hlus0h"&&(R.innerHTML=Fe),be=o(e),c(B.$$.fragment,e),Te=o(e),q=a(e,"UL",{"data-svelte-h":!0}),r(q)!=="svelte-7woav9"&&(q.innerHTML=Se),we=o(e),c(W.$$.fragment,e),ve=o(e),c(F.$$.fragment,e),Ce=o(e),S=a(e,"P",{"data-svelte-h":!0}),r(S)!=="svelte-bmbrzm"&&(S.textContent=ze),$e=o(e),z=a(e,"UL",{"data-svelte-h":!0}),r(z)!=="svelte-1tee105"&&(z.innerHTML=Qe),_e=o(e),c(g.$$.fragment,e),xe=o(e),Q=a(e,"P",{"data-svelte-h":!0}),r(Q)!=="svelte-bydw52"&&(Q.textContent=Ae),Ue=o(e),P=a(e,"P",{}),Ye(P).forEach(n),this.h()},h(){Oe(p,"name","hf:doc:metadata"),Oe(p,"content",pt)},m(e,t){st(document.head,p),l(e,M,t),l(e,m,t),l(e,A,t),d(b,e,t),l(e,D,t),d(T,e,t),l(e,K,t),l(e,w,t),l(e,ee,t),l(e,v,t),l(e,te,t),l(e,C,t),l(e,ne,t),l(e,$,t),l(e,le,t),d(_,e,t),l(e,se,t),l(e,x,t),l(e,oe,t),l(e,U,t),l(e,ie,t),l(e,J,t),l(e,ae,t),l(e,j,t),l(e,re,t),l(e,N,t),l(e,pe,t),d(k,e,t),l(e,me,t),l(e,H,t),l(e,ue,t),l(e,X,t),l(e,ce,t),l(e,Z,t),l(e,de,t),d(V,e,t),l(e,fe,t),d(I,e,t),l(e,he,t),l(e,L,t),l(e,ye,t),l(e,G,t),l(e,ge,t),d(E,e,t),l(e,Me,t),l(e,R,t),l(e,be,t),d(B,e,t),l(e,Te,t),l(e,q,t),l(e,we,t),d(W,e,t),l(e,ve,t),d(F,e,t),l(e,Ce,t),l(e,S,t),l(e,$e,t),l(e,z,t),l(e,_e,t),d(g,e,t),l(e,xe,t),l(e,Q,t),l(e,Ue,t),l(e,P,t),Je=!0},p(e,[t]){const Pe={};t&2&&(Pe.$$scope={dirty:t,ctx:e}),g.$set(Pe)},i(e){Je||(f(b.$$.fragment,e),f(T.$$.fragment,e),f(_.$$.fragment,e),f(k.$$.fragment,e),f(V.$$.fragment,e),f(I.$$.fragment,e),f(E.$$.fragment,e),f(B.$$.fragment,e),f(W.$$.fragment,e),f(F.$$.fragment,e),f(g.$$.fragment,e),Je=!0)},o(e){h(b.$$.fragment,e),h(T.$$.fragment,e),h(_.$$.fragment,e),h(k.$$.fragment,e),h(V.$$.fragment,e),h(I.$$.fragment,e),h(E.$$.fragment,e),h(B.$$.fragment,e),h(W.$$.fragment,e),h(F.$$.fragment,e),h(g.$$.fragment,e),Je=!1},d(e){e&&(n(M),n(m),n(A),n(D),n(K),n(w),n(ee),n(v),n(te),n(C),n(ne),n($),n(le),n(se),n(x),n(oe),n(U),n(ie),n(J),n(ae),n(j),n(re),n(N),n(pe),n(me),n(H),n(ue),n(X),n(ce),n(Z),n(de),n(fe),n(he),n(L),n(ye),n(G),n(ge),n(Me),n(R),n(be),n(Te),n(q),n(we),n(ve),n(Ce),n(S),n($e),n(z),n(_e),n(xe),n(Q),n(Ue),n(P)),n(p),y(b,e),y(T,e),y(_,e),y(k,e),y(V,e),y(I,e),y(E,e),y(B,e),y(W,e),y(F,e),y(g,e)}}}const pt='{"title":"Adding support for new architectures","local":"adding-support-for-new-architectures","sections":[{"title":"Implement a custom Neuron configuration","local":"implement-a-custom-neuron-configuration","sections":[{"title":"Example: Adding support for ESM models","local":"example-adding-support-for-esm-models","sections":[],"depth":3}],"depth":2},{"title":"Export and validate the model","local":"export-and-validate-the-model","sections":[],"depth":2},{"title":"Contribute to the GitHub repo","local":"contribute-to-the-github-repo","sections":[],"depth":2}],"depth":1}';function mt(O){return Ke(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class gt extends tt{constructor(p){super(),nt(this,p,mt,rt,De,{})}}export{gt as component};
