Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"Quickstart","local":"quickstart","sections":[{"title":"Training","local":"training","sections":[{"title":"Running Training","local":"running-training","sections":[],"depth":3}],"depth":2},{"title":"Inference","local":"inference","sections":[{"title":"1. Export Your Model","local":"1-export-your-model","sections":[],"depth":3},{"title":"2. Run Inference","local":"2-run-inference","sections":[],"depth":3}],"depth":2},{"title":"Next Steps","local":"next-steps","sections":[],"depth":2}],"depth":1}"> | |
| <link href="/docs/optimum.neuron/v0.4.4/en/_app/immutable/assets/0.e3b0c442.css" rel="modulepreload"> | |
| <link rel="modulepreload" href="/docs/optimum.neuron/v0.4.4/en/_app/immutable/entry/start.40e9a376.js"> | |
| <link rel="modulepreload" href="/docs/optimum.neuron/v0.4.4/en/_app/immutable/chunks/scheduler.56725da7.js"> | |
| <link rel="modulepreload" href="/docs/optimum.neuron/v0.4.4/en/_app/immutable/chunks/singletons.803151f8.js"> | |
| <link rel="modulepreload" href="/docs/optimum.neuron/v0.4.4/en/_app/immutable/chunks/paths.316b9bfe.js"> | |
| <link rel="modulepreload" href="/docs/optimum.neuron/v0.4.4/en/_app/immutable/entry/app.0fb5ce66.js"> | |
| <link rel="modulepreload" href="/docs/optimum.neuron/v0.4.4/en/_app/immutable/chunks/preload-helper.b93cc304.js"> | |
| <link rel="modulepreload" href="/docs/optimum.neuron/v0.4.4/en/_app/immutable/chunks/index.18a26576.js"> | |
| <link rel="modulepreload" href="/docs/optimum.neuron/v0.4.4/en/_app/immutable/nodes/0.3130ff89.js"> | |
| <link rel="modulepreload" href="/docs/optimum.neuron/v0.4.4/en/_app/immutable/chunks/each.e59479a4.js"> | |
| <link rel="modulepreload" href="/docs/optimum.neuron/v0.4.4/en/_app/immutable/nodes/39.f0101ff3.js"> | |
| <link rel="modulepreload" href="/docs/optimum.neuron/v0.4.4/en/_app/immutable/chunks/CopyLLMTxtMenu.4513c8ed.js"> | |
| <link rel="modulepreload" href="/docs/optimum.neuron/v0.4.4/en/_app/immutable/chunks/globals.7f7f1b26.js"> | |
| <link rel="modulepreload" href="/docs/optimum.neuron/v0.4.4/en/_app/immutable/chunks/MermaidChart.svelte_svelte_type_style_lang.049405bf.js"> | |
| <link rel="modulepreload" href="/docs/optimum.neuron/v0.4.4/en/_app/immutable/chunks/CodeBlock.58e3e98b.js"><!-- HEAD_svelte-u9bgzb_START --><meta name="hf:doc:metadata" content="{"title":"Quickstart","local":"quickstart","sections":[{"title":"Training","local":"training","sections":[{"title":"Running Training","local":"running-training","sections":[],"depth":3}],"depth":2},{"title":"Inference","local":"inference","sections":[{"title":"1. Export Your Model","local":"1-export-your-model","sections":[],"depth":3},{"title":"2. Run Inference","local":"2-run-inference","sections":[],"depth":3}],"depth":2},{"title":"Next Steps","local":"next-steps","sections":[],"depth":2}],"depth":1}"><!-- HEAD_svelte-u9bgzb_END --> <p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"><div class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 max-sm:gap-0.5 h-6 max-sm:h-5 px-2 max-sm:px-1.5 text-[11px] max-sm:text-[9px] font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0"><svg class="w-3 h-3 max-sm:w-2.5 max-sm:h-2.5" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg></span> <span>Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-6 max-sm:h-5 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible w-3 h-3 max-sm:w-2.5 max-sm:h-2.5 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg></button></div> </div> <h1 class="relative group"><a id="quickstart" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#quickstart"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Quickstart</span></h1> <p data-svelte-h="svelte-rionjr">๐ค Optimum Neuron makes AWS accelerator adoption seamless for Hugging Face users with <strong>drop-in replacements</strong> for standard training and inference components.</p> <p data-svelte-h="svelte-4024ac"><strong>*๐ Need to set up your environment first?</strong> Check out our <a href="getting-started-on-ec2">Getting Started on EC2</a> page for complete installation and AWS setup instructions.*</p> <p data-svelte-h="svelte-dcww01"><strong>Key Features:</strong></p> <ul data-svelte-h="svelte-1x7ik4"><li>๐ <strong>Drop-in replacement</strong> for standard Transformers training and inference</li> <li>โก <strong>Distributed training</strong> support with minimal code changes</li> <li>๐ฏ <strong>Optimized models</strong> for AWS accelerators</li> <li>๐ <strong>Production-ready</strong> inference with compiled models</li></ul> <h2 class="relative group"><a id="training" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#training"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Training</span></h2> <p data-svelte-h="svelte-100vig6">Training on AWS Trainium requires minimal changes to your existing code - just swap in Optimum Neuronโs drop-in replacements:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">import</span> torch | |
| <span class="hljs-keyword">import</span> torch_xla.runtime <span class="hljs-keyword">as</span> xr | |
| <span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset | |
| <span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer | |
| <span class="hljs-comment"># Optimum Neuron's drop-in replacements for standard training components</span> | |
| <span class="hljs-keyword">from</span> optimum.neuron <span class="hljs-keyword">import</span> NeuronSFTConfig, NeuronSFTTrainer, NeuronTrainingArguments | |
| <span class="hljs-keyword">from</span> optimum.neuron.models.training <span class="hljs-keyword">import</span> NeuronModelForCausalLM | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">format_dolly_dataset</span>(<span class="hljs-params">example</span>): | |
| <span class="hljs-string">"""Format Dolly dataset into instruction-following format."""</span> | |
| instruction = <span class="hljs-string">f"### Instruction\n<span class="hljs-subst">{example[<span class="hljs-string">'instruction'</span>]}</span>"</span> | |
| context = <span class="hljs-string">f"### Context\n<span class="hljs-subst">{example[<span class="hljs-string">'context'</span>]}</span>"</span> <span class="hljs-keyword">if</span> example[<span class="hljs-string">"context"</span>] <span class="hljs-keyword">else</span> <span class="hljs-literal">None</span> | |
| response = <span class="hljs-string">f"### Answer\n<span class="hljs-subst">{example[<span class="hljs-string">'response'</span>]}</span>"</span> | |
| <span class="hljs-comment"># Combine all parts with double newlines</span> | |
| parts = [instruction, context, response] | |
| <span class="hljs-keyword">return</span> <span class="hljs-string">"\n\n"</span>.join(part <span class="hljs-keyword">for</span> part <span class="hljs-keyword">in</span> parts <span class="hljs-keyword">if</span> part) | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">main</span>(): | |
| <span class="hljs-comment"># Load instruction-following dataset</span> | |
| dataset = load_dataset(<span class="hljs-string">"databricks/databricks-dolly-15k"</span>, split=<span class="hljs-string">"train"</span>) | |
| <span class="hljs-comment"># Model configuration</span> | |
| model_id = <span class="hljs-string">"Qwen/Qwen3-1.7B"</span> | |
| output_dir = <span class="hljs-string">"qwen3-1.7b-finetuned"</span> | |
| <span class="hljs-comment"># Setup tokenizer</span> | |
| tokenizer = AutoTokenizer.from_pretrained(model_id) | |
| tokenizer.pad_token = tokenizer.eos_token | |
| <span class="hljs-comment"># Configure training for Trainium</span> | |
| training_args = NeuronTrainingArguments( | |
| learning_rate=<span class="hljs-number">1e-4</span>, | |
| tensor_parallel_size=<span class="hljs-number">8</span>, <span class="hljs-comment"># Split model across 8 accelerators</span> | |
| per_device_train_batch_size=<span class="hljs-number">1</span>, <span class="hljs-comment"># Batch size per device</span> | |
| gradient_accumulation_steps=<span class="hljs-number">8</span>, | |
| logging_steps=<span class="hljs-number">1</span>, | |
| output_dir=output_dir, | |
| ) | |
| <span class="hljs-comment"># Load model optimized for Trainium</span> | |
| model = NeuronModelForCausalLM.from_pretrained( | |
| model_id, | |
| training_args.trn_config, | |
| dtype=torch.bfloat16, | |
| attn_implementation=<span class="hljs-string">"flash_attention_2"</span>, <span class="hljs-comment"># Enable flash attention</span> | |
| ) | |
| <span class="hljs-comment"># Setup supervised fine-tuning</span> | |
| sft_config = NeuronSFTConfig( | |
| max_seq_length=<span class="hljs-number">2048</span>, | |
| packing=<span class="hljs-literal">True</span>, <span class="hljs-comment"># Pack multiple samples for efficiency</span> | |
| **training_args.to_dict(), | |
| ) | |
| <span class="hljs-comment"># Initialize trainer and start training</span> | |
| trainer = NeuronSFTTrainer( | |
| model=model, | |
| args=sft_config, | |
| tokenizer=tokenizer, | |
| train_dataset=dataset, | |
| formatting_func=format_dolly_dataset, | |
| ) | |
| trainer.train() | |
| <span class="hljs-comment"># Share your model with the community</span> | |
| trainer.push_to_hub( | |
| commit_message=<span class="hljs-string">"Fine-tuned on Databricks Dolly dataset"</span>, | |
| blocking=<span class="hljs-literal">True</span>, | |
| model_name=output_dir, | |
| ) | |
| <span class="hljs-keyword">if</span> xr.local_ordinal() == <span class="hljs-number">0</span>: | |
| <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Training complete! Model saved to <span class="hljs-subst">{output_dir}</span>"</span>) | |
| <span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">"__main__"</span>: | |
| main()<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-6vr389">This example demonstrates supervised fine-tuning on the <a href="https://huggingface.co/datasets/databricks/databricks-dolly-15k" rel="nofollow">Databricks Dolly dataset</a> using <code>NeuronSFTTrainer</code> and <code>NeuronModelForCausalLM</code> - the Trainium-optimized versions of standard Transformers components.</p> <h3 class="relative group"><a id="running-training" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#running-training"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Running Training</span></h3> <p data-svelte-h="svelte-p86113"><strong>Compilation</strong> (optional for first run):</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->NEURON_CC_FLAGS=<span class="hljs-string">"--model-type transformer"</span> neuron_parallel_compile torchrun --nproc_per_node 32 sft_finetune_qwen3.py<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-16jb019"><strong>Training:</strong></p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->NEURON_CC_FLAGS=<span class="hljs-string">"--model-type transformer"</span> torchrun --nproc_per_node 32 sft_finetune_qwen3.py<!-- HTML_TAG_END --></pre></div> <h2 class="relative group"><a id="inference" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#inference"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Inference</span></h2> <p data-svelte-h="svelte-18qqgpt">Optimized inference requires two steps: <strong>export</strong> your model to Neuron format, then <strong>run</strong> it with <code>NeuronModelForXXX</code> classes.</p> <h3 class="relative group"><a id="1-export-your-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#1-export-your-model"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>1. Export Your Model</span></h3> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->optimum-cli <span class="hljs-built_in">export</span> neuron \ | |
| --model distilbert-base-uncased-finetuned-sst-2-english \ | |
| --batch_size 1 \ | |
| --sequence_length 32 \ | |
| --auto_cast matmul \ | |
| --auto_cast_type bf16 \ | |
| distilbert_base_uncased_finetuned_sst2_english_neuron/<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1xuei7w">This exports the model with optimized settings: static shapes (<code>batch_size=1</code>, <code>sequence_length=32</code>) and BF16 precision for <code>matmul</code> operations. Check out the <a href="https://huggingface.co/docs/optimum-neuron/guides/export_model" rel="nofollow">exporter guide</a> for more compilation options.</p> <h3 class="relative group"><a id="2-run-inference" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#2-run-inference"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>2. Run Inference</span></h3> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><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 | |
| <span class="hljs-comment"># Load the compiled Neuron model</span> | |
| model = NeuronModelForSequenceClassification.from_pretrained( | |
| <span class="hljs-string">"distilbert_base_uncased_finetuned_sst2_english_neuron"</span> | |
| ) | |
| <span class="hljs-comment"># Setup tokenizer (same as original model)</span> | |
| tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"distilbert-base-uncased-finetuned-sst-2-english"</span>) | |
| <span class="hljs-comment"># Run inference</span> | |
| inputs = tokenizer(<span class="hljs-string">"Hamilton is considered to be the best musical of past years."</span>, return_tensors=<span class="hljs-string">"pt"</span>) | |
| logits = model(**inputs).logits | |
| <span class="hljs-built_in">print</span>(model.config.id2label[logits.argmax().item()]) | |
| <span class="hljs-comment"># 'POSITIVE'</span><!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-7flx79">The <code>NeuronModelForXXX</code> classes work as drop-in replacements for their <code>AutoModelForXXX</code> counterparts, making migration seamless.</p> <h2 class="relative group"><a id="next-steps" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#next-steps"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Next Steps</span></h2> <p data-svelte-h="svelte-f2vb7f">Ready to dive deeper? Check out our comprehensive guides:</p> <ul data-svelte-h="svelte-e9xbbf"><li>๐ <strong><a href="getting-started">Getting Started</a></strong> - Complete setup and installation</li> <li>๐๏ธ <strong><a href="training_tutorials/notebooks">Training Tutorials</a></strong> - End-to-end training examples</li> <li>๐ง <strong><a href="guides/export_model">Export Guide</a></strong> - Advanced model compilation options</li></ul> <p></p> | |
| <script> | |
| { | |
| __sveltekit_wi4mep = { | |
| assets: "/docs/optimum.neuron/v0.4.4/en", | |
| base: "/docs/optimum.neuron/v0.4.4/en", | |
| env: {} | |
| }; | |
| const element = document.currentScript.parentElement; | |
| const data = [null,null]; | |
| Promise.all([ | |
| import("/docs/optimum.neuron/v0.4.4/en/_app/immutable/entry/start.40e9a376.js"), | |
| import("/docs/optimum.neuron/v0.4.4/en/_app/immutable/entry/app.0fb5ce66.js") | |
| ]).then(([kit, app]) => { | |
| kit.start(app, element, { | |
| node_ids: [0, 39], | |
| data, | |
| form: null, | |
| error: null | |
| }); | |
| }); | |
| } | |
| </script> | |
Xet Storage Details
- Size:
- 29.4 kB
- Xet hash:
- 46ecc206fa3dbee1a522fc38bbc20d90f16bacd9ebf4b46f8a6a9108102029ca
ยท
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.