Buckets:

HuggingFaceDocBuilder's picture
download
raw
24.2 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Inference pipelines with AWS Neuron (Inf2/Trn1)&quot;,&quot;local&quot;:&quot;inference-pipelines-with-aws-neuron-inf2trn1&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Optimum pipeline usage&quot;,&quot;local&quot;:&quot;optimum-pipeline-usage&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Using vanilla Transformers model and converting to AWS Neuron&quot;,&quot;local&quot;:&quot;using-vanilla-transformers-model-and-converting-to-aws-neuron&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Defining Input Shapes&quot;,&quot;local&quot;:&quot;defining-input-shapes&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2}],&quot;depth&quot;:1}">
<link href="/docs/optimum.neuron/pr_1109/en/_app/immutable/assets/0.e3b0c442.css" rel="modulepreload">
<link rel="modulepreload" href="/docs/optimum.neuron/pr_1109/en/_app/immutable/entry/start.4745cc40.js">
<link rel="modulepreload" href="/docs/optimum.neuron/pr_1109/en/_app/immutable/chunks/scheduler.56725da7.js">
<link rel="modulepreload" href="/docs/optimum.neuron/pr_1109/en/_app/immutable/chunks/singletons.1772a741.js">
<link rel="modulepreload" href="/docs/optimum.neuron/pr_1109/en/_app/immutable/chunks/paths.bd433f4a.js">
<link rel="modulepreload" href="/docs/optimum.neuron/pr_1109/en/_app/immutable/entry/app.1de09e9b.js">
<link rel="modulepreload" href="/docs/optimum.neuron/pr_1109/en/_app/immutable/chunks/preload-helper.a7c86d6c.js">
<link rel="modulepreload" href="/docs/optimum.neuron/pr_1109/en/_app/immutable/chunks/index.18a26576.js">
<link rel="modulepreload" href="/docs/optimum.neuron/pr_1109/en/_app/immutable/nodes/0.7622671e.js">
<link rel="modulepreload" href="/docs/optimum.neuron/pr_1109/en/_app/immutable/chunks/each.e59479a4.js">
<link rel="modulepreload" href="/docs/optimum.neuron/pr_1109/en/_app/immutable/nodes/13.f9a3e735.js">
<link rel="modulepreload" href="/docs/optimum.neuron/pr_1109/en/_app/immutable/chunks/Tip.5b941656.js">
<link rel="modulepreload" href="/docs/optimum.neuron/pr_1109/en/_app/immutable/chunks/CopyLLMTxtMenu.a1f2bcd7.js">
<link rel="modulepreload" href="/docs/optimum.neuron/pr_1109/en/_app/immutable/chunks/globals.7f7f1b26.js">
<link rel="modulepreload" href="/docs/optimum.neuron/pr_1109/en/_app/immutable/chunks/MermaidChart.svelte_svelte_type_style_lang.9f98faf7.js">
<link rel="modulepreload" href="/docs/optimum.neuron/pr_1109/en/_app/immutable/chunks/CodeBlock.d6d1e300.js"><!-- HEAD_svelte-u9bgzb_START --><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Inference pipelines with AWS Neuron (Inf2/Trn1)&quot;,&quot;local&quot;:&quot;inference-pipelines-with-aws-neuron-inf2trn1&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Optimum pipeline usage&quot;,&quot;local&quot;:&quot;optimum-pipeline-usage&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Using vanilla Transformers model and converting to AWS Neuron&quot;,&quot;local&quot;:&quot;using-vanilla-transformers-model-and-converting-to-aws-neuron&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Defining Input Shapes&quot;,&quot;local&quot;:&quot;defining-input-shapes&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2}],&quot;depth&quot;: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 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm 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 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" 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-7 max-sm:h-7 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 sm:size-3.5 size-3 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="inference-pipelines-with-aws-neuron-inf2trn1" 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-pipelines-with-aws-neuron-inf2trn1"><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 pipelines with AWS Neuron (Inf2/Trn1)</span></h1> <p data-svelte-h="svelte-grle1x">The <code>pipeline()</code> function makes it simple to use models from the <a href="https://huggingface.co/models" rel="nofollow">Model Hub</a>
for accelerated inference on a variety of tasks such as text classification, question answering and image classification.</p> <blockquote class="tip"><p data-svelte-h="svelte-zitk2r">You can also use the
<a href="https://huggingface.co/docs/transformers/main/en/main_classes/pipelines#pipelines" rel="nofollow">pipeline()</a> function from
Transformers and provide your NeuronModel model class.</p></blockquote> <p data-svelte-h="svelte-1rl24tl">Currently the supported tasks are:</p> <ul data-svelte-h="svelte-4fbr41"><li><code>feature-extraction</code></li> <li><code>fill-mask</code></li> <li><code>text-classification</code></li> <li><code>token-classification</code></li> <li><code>question-answering</code></li> <li><code>text-generation</code></li> <li><code>image-classification</code></li> <li><code>image-segmentation</code></li> <li><code>object-detection</code></li> <li><code>automatic-speech-recognition</code></li> <li><code>audio-classification</code></li></ul> <h2 class="relative group"><a id="optimum-pipeline-usage" 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="#optimum-pipeline-usage"><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>Optimum pipeline usage</span></h2> <p data-svelte-h="svelte-j14w9v">While each task has an associated pipeline class, it is simpler to use the general <code>pipeline()</code> function which wraps all the task-specific pipelines in one object.
The <code>pipeline()</code> function automatically loads a default model and tokenizer/feature-extractor capable of performing inference for your task.</p> <ol data-svelte-h="svelte-khfcvb"><li>Start by creating a pipeline by specifying an inference task:</li></ol> <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="language-python "><!-- HTML_TAG_START --><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> optimum.neuron.pipelines <span class="hljs-keyword">import</span> pipeline
<span class="hljs-meta">&gt;&gt;&gt; </span>classifier = pipeline(task=<span class="hljs-string">&quot;text-classification&quot;</span>)<!-- HTML_TAG_END --></pre></div> <ol start="2" data-svelte-h="svelte-8j6vxl"><li>Pass your input text/image to the <code>pipeline()</code> function:</li></ol> <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="language-python "><!-- HTML_TAG_START --><span class="hljs-meta">&gt;&gt;&gt; </span>classifier(<span class="hljs-string">&quot;I like you. I love you.&quot;</span>)
[{<span class="hljs-string">&#x27;label&#x27;</span>: <span class="hljs-string">&#x27;POSITIVE&#x27;</span>, <span class="hljs-string">&#x27;score&#x27;</span>: <span class="hljs-number">0.9998838901519775</span>}]<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1o2tkpw"><em>Note: The default models used in the <code>pipeline()</code> function are not optimized for inference or quantized, so there won’t be a performance improvement compared to their PyTorch counterparts.</em></p> <h3 class="relative group"><a id="using-vanilla-transformers-model-and-converting-to-aws-neuron" 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="#using-vanilla-transformers-model-and-converting-to-aws-neuron"><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>Using vanilla Transformers model and converting to AWS Neuron</span></h3> <p data-svelte-h="svelte-ddng3e">The <code>pipeline()</code> function accepts any supported model from the <a href="https://huggingface.co/models" rel="nofollow">Hugging Face Hub</a>.
There are tags on the Model Hub that allow you to filter for a model you’d like to use for your task.</p> <blockquote class="tip"><p data-svelte-h="svelte-1fvrwrq">To be able to load the model with the Neuron Runtime, the export to neuron needs
to be supported for the considered architecture.</p> <p data-svelte-h="svelte-1w5ukc2">You can check the list of supported architectures
<a href="../package_reference/configuration#supported-architectures">here</a>.</p></blockquote> <p data-svelte-h="svelte-1ig4n3p">Once you have picked an appropriate model, you can create the <code>pipeline()</code> by specifying the model repo:</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="language-python "><!-- HTML_TAG_START --><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> optimum.neuron.pipelines <span class="hljs-keyword">import</span> pipeline
<span class="hljs-comment"># The model will be loaded to an NeuronModelForQuestionAnswering.</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>neuron_qa = pipeline(<span class="hljs-string">&quot;question-answering&quot;</span>, model=<span class="hljs-string">&quot;deepset/roberta-base-squad2&quot;</span>, export=<span class="hljs-literal">True</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>question = <span class="hljs-string">&quot;What&#x27;s my name?&quot;</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>context = <span class="hljs-string">&quot;My name is Philipp and I live in Nuremberg.&quot;</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>pred = neuron_qa(question=question, context=context)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-djskr">It is also possible to load it with the <code>from_pretrained(model_name_or_path, export=True)</code>
method associated with the <code>NeuronModelForXXX</code> class.</p> <p data-svelte-h="svelte-1iyza81">For example, here is how you can load the <code>~neuron.NeuronModelForQuestionAnswering</code> class for question answering:</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="language-python "><!-- HTML_TAG_START --><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> optimum.neuron <span class="hljs-keyword">import</span> NeuronModelForQuestionAnswering, pipeline
<span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">&quot;deepset/roberta-base-squad2&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># Loading the PyTorch checkpoint and converting to the neuron format by providing export=True</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>model = NeuronModelForQuestionAnswering.from_pretrained(
<span class="hljs-meta">... </span> <span class="hljs-string">&quot;deepset/roberta-base-squad2&quot;</span>,
<span class="hljs-meta">... </span> export=<span class="hljs-literal">True</span>
<span class="hljs-meta">... </span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>neuron_qa = pipeline(<span class="hljs-string">&quot;question-answering&quot;</span>, model=model, tokenizer=tokenizer)
<span class="hljs-meta">&gt;&gt;&gt; </span>question = <span class="hljs-string">&quot;What&#x27;s my name?&quot;</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>context = <span class="hljs-string">&quot;My name is Philipp and I live in Nuremberg.&quot;</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>pred = neuron_qa(question=question, context=context)<!-- HTML_TAG_END --></pre></div> <h3 class="relative group"><a id="defining-input-shapes" 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="#defining-input-shapes"><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>Defining Input Shapes</span></h3> <p data-svelte-h="svelte-xm6kib">NeuronModels currently require static <code>input_shapes</code> to run inference. The default input shapes will be used if you are not providing input shapes when providing the <code>export=True</code> parameter.
Below is an example of how to specify the input shapes for the sequence length and batch size.</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="language-python "><!-- HTML_TAG_START --><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> optimum.neuron.pipelines <span class="hljs-keyword">import</span> pipeline
<span class="hljs-meta">&gt;&gt;&gt; </span>input_shapes = {<span class="hljs-string">&quot;batch_size&quot;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&quot;sequence_length&quot;</span>: <span class="hljs-number">64</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>clt = pipeline(<span class="hljs-string">&quot;token-classification&quot;</span>, model=<span class="hljs-string">&quot;dslim/bert-base-NER&quot;</span>, export=<span class="hljs-literal">True</span>,input_shapes=input_shapes)
<span class="hljs-meta">&gt;&gt;&gt; </span>context = <span class="hljs-string">&quot;My name is Philipp and I live in Nuremberg.&quot;</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>pred = clt(context)<!-- HTML_TAG_END --></pre></div> <p></p>
<script>
{
__sveltekit_15tsq1n = {
assets: "/docs/optimum.neuron/pr_1109/en",
base: "/docs/optimum.neuron/pr_1109/en",
env: {}
};
const element = document.currentScript.parentElement;
const data = [null,null];
Promise.all([
import("/docs/optimum.neuron/pr_1109/en/_app/immutable/entry/start.4745cc40.js"),
import("/docs/optimum.neuron/pr_1109/en/_app/immutable/entry/app.1de09e9b.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 13],
data,
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
24.2 kB
·
Xet hash:
16541b9250fc4806783e0e5c925c95e4f525bd3632883e824fa14e2a90ed28b3

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.