Package: optimum-neuron
Optimum Neuron🤗 Optimum Neuron is the interface between the 🤗 Transformers library and AWS Accelerators includingAWS TrainiumandAWS Inferentia.
It provides a set of tools enabling easy model loading, training and inference on single- and multi-Accelerator settings for different downstream tasks.
The list of officially validated models and tasks is availablehere. Users can try other models and tasks with only few changes.InstallTo install the latest release of this package:For AWS Trainium (trn1) or AWS inferentia2 (inf2)pipinstalloptimum[neuronx]For AWS inferentia (inf1)pipinstalloptimum[neuron]Optimum Neuron is a fast-moving project, and you may want to install it from source:pipinstallgit+https://github.com/huggingface/optimum-neuron.gitAlternatively, you can install the package without pip as follows:gitclonehttps://github.com/huggingface/optimum-neuron.gitcdoptimum-neuron
pythonsetup.pyinstallMake sure that you have installed the Neuron driver and tools before installingoptimum-neuron,more extensive guide here.Last but not least, don't forget to install the requirements for every example:cd<example-folder>
pipinstall-rrequirements.txtQuick Start🤗 Optimum Neuron was designed with one goal in mind:to make training and inference straightforward for any 🤗 Transformers user while leveraging the complete power of AWS Accelerators.TrainingThere are two main classes one needs to know:TrainiumArgumentParser: inherits the originalHfArgumentParserin Transformers with additional checks on the argument values to make sure that they will work well with AWS Trainium instances.NeuronTrainer: this version trainer takes care of doing the proper checks and changes to the supported models to make them trainable on AWS Trainium instances.TheNeuronTraineris very similar to the🤗 Transformers Trainer, and adapting a script using the Trainer to make it work with Trainium will mostly consist in simply swapping the Trainer class for the NeuronTrainer one.
That's how most of theexample scriptswere adapted from theiroriginal counterparts.from transformers import TrainingArguments+from optimum.neuron import NeuronTrainer as Trainertraining_args = TrainingArguments(# training arguments...
)

# A lot of code here

# Initialize our Trainer
trainer = Trainer(model=model,args=training_args,  # Original training arguments.train_dataset=train_dataset if training_args.do_train else None,eval_dataset=eval_dataset if training_args.do_eval else None,compute_metrics=compute_metrics,tokenizer=tokenizer,data_collator=data_collator,
)InferenceYou can compile and export your 🤗 Transformers models to a serialized format before inference on Neuron devices:optimum-cliexportneuron\--modeldistilbert-base-uncased-finetuned-sst-2-english\--batch_size1\--sequence_length32\--auto_castmatmul\--auto_cast_typebf16\distilbert_base_uncased_finetuned_sst2_english_neuron/The command above will exportdistilbert-base-uncased-finetuned-sst-2-englishwith static shapes:batch_size=1andsequence_length=32, and cast allmatmuloperations from FP32 to BF16. Check out theexporter guidefor more compilation options.Then you can run the exported Neuron model on Neuron devices withNeuronModelForXXXclasses which are similar toAutoModelForXXXclasses in 🤗 Transformers:from transformers import AutoTokenizer-from transformers import AutoModelForSequenceClassification+from optimum.neuron import NeuronModelForSequenceClassification# PyTorch checkpoint-model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")+model = NeuronModelForSequenceClassification.from_pretrained("distilbert_base_uncased_finetuned_sst2_english_neuron")tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
inputs = tokenizer("Hamilton is considered to be the best musical of past years.", return_tensors="pt")

logits = model(**inputs).logits
print(model.config.id2label[logits.argmax().item()])
# 'POSITIVE'DocumentationCheck outthe documentation of Optimum Neuronfor more advanced usage.If you find any issue while using those, please open an issue or a pull request.Text-generation-inferenceThis repository maintains atext-generation-inference (TGI)docker image for deployment on AWS inferentia2.