The following NeuronBaseModel
class is available for instantiating a base Neuron model without a specific head.
( model: ScriptModule config: PretrainedConfig model_save_dir: Union = None model_file_name: Optional = None preprocessors: Optional = None neuron_config: Optional = None **kwargs )
Base class running compiled and optimized models on Neuron devices.
It implements generic methods for interacting with the Hugging Face Hub as well as compiling vanilla
transformers models to neuron-optimized TorchScript module and export it using optimum.exporters.neuron
toolchain.
Class attributes:
str
, optional, defaults to "neuron_model"
) — The name of the model type to use when
registering the NeuronBaseModel classes.Type
, optional, defaults to AutoModel
) — The AutoModel
class to be represented by the
current NeuronBaseModel class.Common attributes:
torch.jit._script.ScriptModule
) — The loaded ScriptModule
compiled for neuron devices.Path
) — The directory where a neuron compiled model is saved.
By default, if the loaded model is local, the directory where the original model will be used. Otherwise, the
cache directory will be used.Gets a dictionary of inputs with their valid static shapes.
( path: Union )
Loads a TorchScript module compiled by neuron(x)-cc compiler. It will be first loaded onto CPU and then moved to one or multiple NeuronCore.
( outputs: List dims: List indices: List )
Removes padding from output tensors.
The following Neuron model classes are available for natural language processing tasks.
( model: ScriptModule config: PretrainedConfig model_save_dir: Union = None model_file_name: Optional = None preprocessors: Optional = None neuron_config: Optional = None **kwargs )
Parameters
transformers.PretrainedConfig
) — PretrainedConfig is the Model configuration class with all the parameters of the model.
Initializing with a config file does not load the weights associated with the model, only the
configuration. Check out the optimum.neuron.modeling.NeuronBaseModel.from_pretrained
method to load the model weights.
torch.jit._script.ScriptModule
) — torch.jit._script.ScriptModule is the TorchScript graph compiled by neuron(x) compiler.
Neuron Model with a BaseModelOutput for feature-extraction tasks.
This model inherits from ~neuron.modeling.NeuronBaseModel
. Check the superclass documentation for the generic methods the
library implements for all its model (such as downloading or saving)
Feature Extraction model on Neuron devices.
( input_ids: Tensor attention_mask: Tensor token_type_ids: Optional = None **kwargs )
Parameters
torch.Tensor
of shape (batch_size, sequence_length)
) —
Indices of input sequence tokens in the vocabulary.
Indices can be obtained using AutoTokenizer
.
See PreTrainedTokenizer.encode
and
PreTrainedTokenizer.__call__
for details.
What are input IDs?
Union[torch.Tensor, None]
of shape (batch_size, sequence_length)
, defaults to None
) —
Mask to avoid performing attention on padding token indices. Mask values selected in [0, 1]
:Union[torch.Tensor, None]
of shape (batch_size, sequence_length)
, defaults to None
) —
Segment token indices to indicate first and second portions of the inputs. Indices are selected in [0, 1]
:The NeuronModelForFeatureExtraction forward method, overrides the __call__
special method.
Although the recipe for forward pass needs to be defined within this function, one should call the Module
instance afterwards instead of this since the former takes care of running the pre and post processing steps while
the latter silently ignores them.
Example of feature extraction: (Following model is compiled with neuronx compiler and can only be run on INF2. Replace “neuronx” with “neuron” if you are using INF1.)
>>> from transformers import AutoTokenizer
>>> from optimum.neuron import NeuronModelForFeatureExtraction
>>> tokenizer = AutoTokenizer.from_pretrained("optimum/all-MiniLM-L6-v2-neuronx")
>>> model = NeuronModelForFeatureExtraction.from_pretrained("optimum/all-MiniLM-L6-v2-neuronx")
>>> inputs = tokenizer("Dear Evan Hansen is the winner of six Tony Awards.", return_tensors="pt")
>>> outputs = model(**inputs)
>>> last_hidden_state = outputs.last_hidden_state
>>> list(last_hidden_state.shape)
[1, 13, 384]
( model: ScriptModule config: PretrainedConfig model_save_dir: Union = None model_file_name: Optional = None preprocessors: Optional = None neuron_config: Optional = None **kwargs )
Parameters
transformers.PretrainedConfig
) — PretrainedConfig is the Model configuration class with all the parameters of the model.
Initializing with a config file does not load the weights associated with the model, only the
configuration. Check out the optimum.neuron.modeling.NeuronBaseModel.from_pretrained
method to load the model weights.
torch.jit._script.ScriptModule
) — torch.jit._script.ScriptModule is the TorchScript graph compiled by neuron(x) compiler.
Neuron Model with a MaskedLMOutput for masked language modeling tasks.
This model inherits from ~neuron.modeling.NeuronBaseModel
. Check the superclass documentation for the generic methods the
library implements for all its model (such as downloading or saving)
Masked language model for on Neuron devices.
( input_ids: Tensor attention_mask: Tensor token_type_ids: Optional = None **kwargs )
Parameters
torch.Tensor
of shape (batch_size, sequence_length)
) —
Indices of input sequence tokens in the vocabulary.
Indices can be obtained using AutoTokenizer
.
See PreTrainedTokenizer.encode
and
PreTrainedTokenizer.__call__
for details.
What are input IDs?
Union[torch.Tensor, None]
of shape (batch_size, sequence_length)
, defaults to None
) —
Mask to avoid performing attention on padding token indices. Mask values selected in [0, 1]
:Union[torch.Tensor, None]
of shape (batch_size, sequence_length)
, defaults to None
) —
Segment token indices to indicate first and second portions of the inputs. Indices are selected in [0, 1]
:The NeuronModelForMaskedLM forward method, overrides the __call__
special method.
Although the recipe for forward pass needs to be defined within this function, one should call the Module
instance afterwards instead of this since the former takes care of running the pre and post processing steps while
the latter silently ignores them.
Example of fill mask: (Following model is compiled with neuronx compiler and can only be run on INF2. Replace “neuronx” with “neuron” if you are using INF1.)
>>> from transformers import AutoTokenizer
>>> from optimum.neuron import NeuronModelForMaskedLM
>>> import torch
>>> tokenizer = AutoTokenizer.from_pretrained("optimum/legal-bert-base-uncased-neuronx")
>>> model = NeuronModelForMaskedLM.from_pretrained("optimum/legal-bert-base-uncased-neuronx")
>>> inputs = tokenizer("This [MASK] Agreement is between General Motors and John Murray.", return_tensors="pt")
>>> outputs = model(**inputs)
>>> logits = outputs.logits
>>> list(logits.shape)
[1, 13, 30522]
( model: ScriptModule config: PretrainedConfig model_save_dir: Union = None model_file_name: Optional = None preprocessors: Optional = None neuron_config: Optional = None **kwargs )
Parameters
transformers.PretrainedConfig
) — PretrainedConfig is the Model configuration class with all the parameters of the model.
Initializing with a config file does not load the weights associated with the model, only the
configuration. Check out the optimum.neuron.modeling.NeuronBaseModel.from_pretrained
method to load the model weights.
torch.jit._script.ScriptModule
) — torch.jit._script.ScriptModule is the TorchScript graph compiled by neuron(x) compiler.
Neuron Model with a sequence classification/regression head on top (a linear layer on top of the pooled output) e.g. for GLUE tasks.
This model inherits from ~neuron.modeling.NeuronBaseModel
. Check the superclass documentation for the generic methods the
library implements for all its model (such as downloading or saving)
Sequence Classification model on Neuron devices.
( input_ids: Tensor attention_mask: Tensor token_type_ids: Optional = None **kwargs )
Parameters
torch.Tensor
of shape (batch_size, sequence_length)
) —
Indices of input sequence tokens in the vocabulary.
Indices can be obtained using AutoTokenizer
.
See PreTrainedTokenizer.encode
and
PreTrainedTokenizer.__call__
for details.
What are input IDs?
Union[torch.Tensor, None]
of shape (batch_size, sequence_length)
, defaults to None
) —
Mask to avoid performing attention on padding token indices. Mask values selected in [0, 1]
:Union[torch.Tensor, None]
of shape (batch_size, sequence_length)
, defaults to None
) —
Segment token indices to indicate first and second portions of the inputs. Indices are selected in [0, 1]
:The NeuronModelForSequenceClassification forward method, overrides the __call__
special method.
Although the recipe for forward pass needs to be defined within this function, one should call the Module
instance afterwards instead of this since the former takes care of running the pre and post processing steps while
the latter silently ignores them.
Example of single-label classification: (Following model is compiled with neuronx compiler and can only be run on INF2.)
>>> from transformers import AutoTokenizer
>>> from optimum.neuron import NeuronModelForSequenceClassification
>>> tokenizer = AutoTokenizer.from_pretrained("optimum/distilbert-base-uncased-finetuned-sst-2-english-neuronx")
>>> model = NeuronModelForSequenceClassification.from_pretrained("optimum/distilbert-base-uncased-finetuned-sst-2-english-neuronx")
>>> inputs = tokenizer("Hamilton is considered to be the best musical of human history.", return_tensors="pt")
>>> outputs = model(**inputs)
>>> logits = outputs.logits
>>> list(logits.shape)
[1, 2]
( model: ScriptModule config: PretrainedConfig model_save_dir: Union = None model_file_name: Optional = None preprocessors: Optional = None neuron_config: Optional = None **kwargs )
Parameters
transformers.PretrainedConfig
) — PretrainedConfig is the Model configuration class with all the parameters of the model.
Initializing with a config file does not load the weights associated with the model, only the
configuration. Check out the optimum.neuron.modeling.NeuronBaseModel.from_pretrained
method to load the model weights.
torch.jit._script.ScriptModule
) — torch.jit._script.ScriptModule is the TorchScript graph compiled by neuron(x) compiler.
Neuron Model with a QuestionAnsweringModelOutput for extractive question-answering tasks like SQuAD.
This model inherits from ~neuron.modeling.NeuronBaseModel
. Check the superclass documentation for the generic methods the
library implements for all its model (such as downloading or saving)
Question Answering model on Neuron devices.
( input_ids: Tensor attention_mask: Tensor token_type_ids: Optional = None **kwargs )
Parameters
torch.Tensor
of shape (batch_size, sequence_length)
) —
Indices of input sequence tokens in the vocabulary.
Indices can be obtained using AutoTokenizer
.
See PreTrainedTokenizer.encode
and
PreTrainedTokenizer.__call__
for details.
What are input IDs?
Union[torch.Tensor, None]
of shape (batch_size, sequence_length)
, defaults to None
) —
Mask to avoid performing attention on padding token indices. Mask values selected in [0, 1]
:Union[torch.Tensor, None]
of shape (batch_size, sequence_length)
, defaults to None
) —
Segment token indices to indicate first and second portions of the inputs. Indices are selected in [0, 1]
:The NeuronModelForQuestionAnswering forward method, overrides the __call__
special method.
Although the recipe for forward pass needs to be defined within this function, one should call the Module
instance afterwards instead of this since the former takes care of running the pre and post processing steps while
the latter silently ignores them.
Example of question answering: (Following model is compiled with neuronx compiler and can only be run on INF2.)
>>> import torch
>>> from transformers import AutoTokenizer
>>> from optimum.neuron import NeuronModelForQuestionAnswering
>>> tokenizer = AutoTokenizer.from_pretrained("optimum/roberta-base-squad2-neuronx")
>>> model = NeuronModelForQuestionAnswering.from_pretrained("optimum/roberta-base-squad2-neuronx")
>>> question, text = "Are there wheelchair spaces in the theatres?", "Yes, we have reserved wheelchair spaces with a good view."
>>> inputs = tokenizer(question, text, return_tensors="pt")
>>> start_positions = torch.tensor([1])
>>> end_positions = torch.tensor([12])
>>> outputs = model(**inputs, start_positions=start_positions, end_positions=end_positions)
>>> start_scores = outputs.start_logits
>>> end_scores = outputs.end_logits
( model: ScriptModule config: PretrainedConfig model_save_dir: Union = None model_file_name: Optional = None preprocessors: Optional = None neuron_config: Optional = None **kwargs )
Parameters
transformers.PretrainedConfig
) — PretrainedConfig is the Model configuration class with all the parameters of the model.
Initializing with a config file does not load the weights associated with the model, only the
configuration. Check out the optimum.neuron.modeling.NeuronBaseModel.from_pretrained
method to load the model weights.
torch.jit._script.ScriptModule
) — torch.jit._script.ScriptModule is the TorchScript graph compiled by neuron(x) compiler.
Neuron Model with a token classification head on top (a linear layer on top of the hidden-states output) e.g. for Named-Entity-Recognition (NER) tasks.
This model inherits from ~neuron.modeling.NeuronBaseModel
. Check the superclass documentation for the generic methods the
library implements for all its model (such as downloading or saving)
Token Classification model on Neuron devices.
( input_ids: Tensor attention_mask: Tensor token_type_ids: Optional = None **kwargs )
Parameters
torch.Tensor
of shape (batch_size, sequence_length)
) —
Indices of input sequence tokens in the vocabulary.
Indices can be obtained using AutoTokenizer
.
See PreTrainedTokenizer.encode
and
PreTrainedTokenizer.__call__
for details.
What are input IDs?
Union[torch.Tensor, None]
of shape (batch_size, sequence_length)
, defaults to None
) —
Mask to avoid performing attention on padding token indices. Mask values selected in [0, 1]
:Union[torch.Tensor, None]
of shape (batch_size, sequence_length)
, defaults to None
) —
Segment token indices to indicate first and second portions of the inputs. Indices are selected in [0, 1]
:The NeuronModelForTokenClassification forward method, overrides the __call__
special method.
Although the recipe for forward pass needs to be defined within this function, one should call the Module
instance afterwards instead of this since the former takes care of running the pre and post processing steps while
the latter silently ignores them.
Example of token classification: (Following model is compiled with neuronx compiler and can only be run on INF2.)
>>> from transformers import AutoTokenizer
>>> from optimum.neuron import NeuronModelForTokenClassification
>>> tokenizer = AutoTokenizer.from_pretrained("optimum/bert-base-NER-neuronx")
>>> model = NeuronModelForTokenClassification.from_pretrained("optimum/bert-base-NER-neuronx")
>>> inputs = tokenizer("Lin-Manuel Miranda is an American songwriter, actor, singer, filmmaker, and playwright.", return_tensors="pt")
>>> outputs = model(**inputs)
>>> logits = outputs.logits
>>> list(logits.shape)
[1, 20, 9]
( model: ScriptModule config: PretrainedConfig model_save_dir: Union = None model_file_name: Optional = None preprocessors: Optional = None neuron_config: Optional = None **kwargs )
Parameters
transformers.PretrainedConfig
) — PretrainedConfig is the Model configuration class with all the parameters of the model.
Initializing with a config file does not load the weights associated with the model, only the
configuration. Check out the optimum.neuron.modeling.NeuronBaseModel.from_pretrained
method to load the model weights.
torch.jit._script.ScriptModule
) — torch.jit._script.ScriptModule is the TorchScript graph compiled by neuron(x) compiler.
Neuron Model with a multiple choice classification head on top (a linear layer on top of the pooled output and a softmax) e.g. for RocStories/SWAG tasks.
This model inherits from ~neuron.modeling.NeuronBaseModel
. Check the superclass documentation for the generic methods the
library implements for all its model (such as downloading or saving)
Multiple choice model on Neuron devices.
( input_ids: Tensor attention_mask: Tensor token_type_ids: Optional = None **kwargs )
Parameters
torch.Tensor
of shape (batch_size, num_choices, sequence_length)
) —
Indices of input sequence tokens in the vocabulary.
Indices can be obtained using AutoTokenizer
.
See PreTrainedTokenizer.encode
and
PreTrainedTokenizer.__call__
for details.
What are input IDs?
Union[torch.Tensor, None]
of shape (batch_size, num_choices, sequence_length)
, defaults to None
) —
Mask to avoid performing attention on padding token indices. Mask values selected in [0, 1]
:Union[torch.Tensor, None]
of shape (batch_size, num_choices, sequence_length)
, defaults to None
) —
Segment token indices to indicate first and second portions of the inputs. Indices are selected in [0, 1]
:The NeuronModelForMultipleChoice forward method, overrides the __call__
special method.
Although the recipe for forward pass needs to be defined within this function, one should call the Module
instance afterwards instead of this since the former takes care of running the pre and post processing steps while
the latter silently ignores them.
Example of mutliple choice: (Following model is compiled with neuronx compiler and can only be run on INF2.)
>>> from transformers import AutoTokenizer
>>> from optimum.neuron import NeuronModelForMultipleChoice
>>> tokenizer = AutoTokenizer.from_pretrained("optimum/bert-base-uncased_SWAG-neuronx")
>>> model = NeuronModelForMultipleChoice.from_pretrained("optimum/bert-base-uncased_SWAG-neuronx", export=True)
>>> num_choices = 4
>>> first_sentence = ["Members of the procession walk down the street holding small horn brass instruments."] * num_choices
>>> second_sentence = [
... "A drum line passes by walking down the street playing their instruments.",
... "A drum line has heard approaching them.",
... "A drum line arrives and they're outside dancing and asleep.",
... "A drum line turns the lead singer watches the performance."
... ]
>>> inputs = tokenizer(first_sentence, second_sentence, truncation=True, padding=True)
# Unflatten the inputs values expanding it to the shape [batch_size, num_choices, seq_length]
>>> for k, v in inputs.items():
... inputs[k] = [v[i: i + num_choices] for i in range(0, len(v), num_choices)]
>>> inputs = dict(inputs.convert_to_tensors(tensor_type="pt"))
>>> outputs = model(**inputs)
>>> logits = outputs.logits
>>> logits.shape
[1, 4]