intel_extension_for_transformers.transformers.modeling.modeling_roberta_dynamic
PyTorch RoBERTa model.
Classes
Same as BertEmbeddings with a tiny tweak for positional embeddings indexing. |
|
Roberta self attention. |
|
Roberta self output. |
|
Roberta attenion. |
|
Roberta intermediate. |
|
Roberta output. |
|
Basic layer of roberta. |
|
The encoder for Roberata. |
|
Roberta pooler. |
|
Roberta pretrained model. |
|
Basic roberta model. |
|
Roberta for causal language model task. |
|
Roberta for masked language model task. |
|
Roberta Head for masked language modeling. |
|
Roberta for sequence classification task. |
|
Roberta for multiple choice task. |
|
Roberta for token classification task. |
|
Head for sentence-level classification tasks. |
|
Roberta model for quanstion answering task. |
Functions
|
Replace non-padding symbols with their position numbers. |
|
Expand gather. |
Module Contents
- class intel_extension_for_transformers.transformers.modeling.modeling_roberta_dynamic.RobertaEmbeddings(config)[source]
Same as BertEmbeddings with a tiny tweak for positional embeddings indexing.
- class intel_extension_for_transformers.transformers.modeling.modeling_roberta_dynamic.RobertaSelfAttention(config, position_embedding_type=None)[source]
Roberta self attention.
- forward(hidden_states: torch.Tensor, attention_mask: torch.FloatTensor | None = None, head_mask: torch.FloatTensor | None = None, encoder_hidden_states: torch.FloatTensor | None = None, encoder_attention_mask: torch.FloatTensor | None = None, past_key_value: Tuple[Tuple[torch.FloatTensor]] | None = None, output_attentions: bool | None = False) Tuple[torch.Tensor][source]
The main entry point for the class.
- class intel_extension_for_transformers.transformers.modeling.modeling_roberta_dynamic.RobertaSelfOutput(config)[source]
Roberta self output.
- class intel_extension_for_transformers.transformers.modeling.modeling_roberta_dynamic.RobertaAttention(config, position_embedding_type=None)[source]
Roberta attenion.
- forward(hidden_states: torch.Tensor, attention_mask: torch.FloatTensor | None = None, head_mask: torch.FloatTensor | None = None, encoder_hidden_states: torch.FloatTensor | None = None, encoder_attention_mask: torch.FloatTensor | None = None, past_key_value: Tuple[Tuple[torch.FloatTensor]] | None = None, output_attentions: bool | None = False) Tuple[torch.Tensor][source]
The main entry point for the class.
- class intel_extension_for_transformers.transformers.modeling.modeling_roberta_dynamic.RobertaIntermediate(config)[source]
Roberta intermediate.
- class intel_extension_for_transformers.transformers.modeling.modeling_roberta_dynamic.RobertaOutput(config)[source]
Roberta output.
- class intel_extension_for_transformers.transformers.modeling.modeling_roberta_dynamic.RobertaLayer(config)[source]
Basic layer of roberta.
- forward(hidden_states: torch.Tensor, attention_mask: torch.FloatTensor | None = None, head_mask: torch.FloatTensor | None = None, encoder_hidden_states: torch.FloatTensor | None = None, encoder_attention_mask: torch.FloatTensor | None = None, past_key_value: Tuple[Tuple[torch.FloatTensor]] | None = None, output_attentions: bool | None = False, output_length=None, always_keep_cls_token: bool | None = True) Tuple[torch.Tensor][source]
The main entry point for the class.
- class intel_extension_for_transformers.transformers.modeling.modeling_roberta_dynamic.RobertaEncoder(config)[source]
The encoder for Roberata.
- forward(hidden_states: torch.Tensor, attention_mask: torch.FloatTensor | None = None, head_mask: torch.FloatTensor | None = None, encoder_hidden_states: torch.FloatTensor | None = None, encoder_attention_mask: torch.FloatTensor | None = None, past_key_values: Tuple[Tuple[torch.FloatTensor]] | None = None, use_cache: bool | None = None, output_attentions: bool | None = False, output_hidden_states: bool | None = False, return_dict: bool | None = True, layer_config=None, length_config=None, always_keep_cls_token=True) Tuple[torch.Tensor] | transformers.modeling_outputs.BaseModelOutputWithPastAndCrossAttentions[source]
The main entry point for the class.
- class intel_extension_for_transformers.transformers.modeling.modeling_roberta_dynamic.RobertaPooler(config)[source]
Roberta pooler.
- class intel_extension_for_transformers.transformers.modeling.modeling_roberta_dynamic.RobertaPreTrainedModel[source]
Roberta pretrained model.
An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models.
- class intel_extension_for_transformers.transformers.modeling.modeling_roberta_dynamic.RobertaModel(config, add_pooling_layer=True)[source]
Basic roberta model.
The model can behave as an encoder (with only self-attention) as well as a decoder, in which case a layer of cross-attention is added between the self-attention layers, following the architecture described in *Attention is all you need*_ by Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Lukasz Kaiser and Illia Polosukhin.
To behave as an decoder the model needs to be initialized with the is_decoder argument of the configuration set to True. To be used in a Seq2Seq model, the model needs to initialized with both is_decoder argument and add_cross_attention set to True; an encoder_hidden_states is then expected as an input to the forward pass.
- forward(input_ids: torch.Tensor | None = None, attention_mask: torch.Tensor | None = None, token_type_ids: torch.Tensor | None = None, position_ids: torch.Tensor | None = None, head_mask: torch.Tensor | None = None, inputs_embeds: torch.Tensor | None = None, encoder_hidden_states: torch.Tensor | None = None, encoder_attention_mask: torch.Tensor | None = None, past_key_values: List[torch.FloatTensor] | None = None, use_cache: bool | None = None, output_attentions: bool | None = None, output_hidden_states: bool | None = None, return_dict: bool | None = None, layer_config=None, length_config=None, always_keep_cls_token=True) Tuple[torch.Tensor] | transformers.modeling_outputs.BaseModelOutputWithPoolingAndCrossAttentions[source]
The main entry point for the class.
- encoder_hidden_states (torch.FloatTensor of shape (batch_size, sequence_length, hidden_size), optional):
Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if the model is configured as a decoder.
- encoder_attention_mask (torch.FloatTensor of shape (batch_size, sequence_length), optional):
Mask to avoid performing attention on the padding token indices of the encoder input. This mask is used in the cross-attention if the model is configured as a decoder. Mask values selected in [0, 1]:
1 for tokens that are not masked,
0 for tokens that are masked.
- past_key_values (tuple(tuple(torch.FloatTensor)) of length config.n_layers with each tuple having 4 tensors
- of shape (batch_size, num_heads, sequence_length - 1, embed_size_per_head)):
Contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding.
If past_key_values are used, the user can optionally input only the last decoder_input_ids (those that don’t have their past key value states given to this model) of shape (batch_size, 1) instead of all decoder_input_ids of shape (batch_size, sequence_length).
- use_cache (bool, optional):
If set to True, past_key_values key value states are returned and can be used to speed up decoding (see past_key_values).
- class intel_extension_for_transformers.transformers.modeling.modeling_roberta_dynamic.RobertaForCausalLM(config)[source]
Roberta for causal language model task.
- forward(input_ids: torch.LongTensor | None = None, attention_mask: torch.FloatTensor | None = None, token_type_ids: torch.LongTensor | None = None, position_ids: torch.LongTensor | None = None, head_mask: torch.FloatTensor | None = None, inputs_embeds: torch.FloatTensor | None = None, encoder_hidden_states: torch.FloatTensor | None = None, encoder_attention_mask: torch.FloatTensor | None = None, labels: torch.LongTensor | None = None, past_key_values: Tuple[Tuple[torch.FloatTensor]] = None, use_cache: bool | None = None, output_attentions: bool | None = None, output_hidden_states: bool | None = None, return_dict: bool | None = None) Tuple[torch.Tensor] | transformers.modeling_outputs.CausalLMOutputWithCrossAttentions[source]
The main entry point for the class.
- encoder_hidden_states (torch.FloatTensor of shape (batch_size, sequence_length, hidden_size), optional):
Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if the model is configured as a decoder.
- encoder_attention_mask (torch.FloatTensor of shape (batch_size, sequence_length), optional):
Mask to avoid performing attention on the padding token indices of the encoder input. This mask is used in the cross-attention if the model is configured as a decoder. Mask values selected in [0, 1]:
1 for tokens that are not masked,
0 for tokens that are masked.
- labels (torch.LongTensor of shape (batch_size, sequence_length), optional):
Labels for computing the left-to-right language modeling loss (next word prediction). Indices should be in [-100, 0, …, config.vocab_size] (see input_ids docstring) Tokens with indices set to -100 are ignored (masked), the loss is only computed for the tokens with labels in [0, …, config.vocab_size]
- past_key_values (tuple(tuple(torch.FloatTensor)) of length config.n_layers with each tuple having 4
tensors of shape (batch_size, num_heads, sequence_length - 1, embed_size_per_head)): Contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If past_key_values are used, the user can optionally input only the last decoder_input_ids (those that don’t have their past key value states given to this model) of shape (batch_size, 1) instead of all decoder_input_ids of shape (batch_size, sequence_length).
- use_cache (bool, optional):
If set to True, past_key_values key value states are returned and can be used to speed up decoding (see past_key_values).
Example
```python >>> from transformers import RobertaTokenizer, RobertaForCausalLM, RobertaConfig >>> import torch
>>> tokenizer = RobertaTokenizer.from_pretrained("roberta-base") >>> config = RobertaConfig.from_pretrained("roberta-base") >>> config.is_decoder = True >>> model = RobertaForCausalLM.from_pretrained("roberta-base", config=config)
>>> inputs = tokenizer("Hello, my dog is cute", return_tensors="pt") >>> outputs = model(**inputs)
>>> prediction_logits = outputs.logits ```
- Returns:
CausalLMOutputWithCrossAttentions.
- class intel_extension_for_transformers.transformers.modeling.modeling_roberta_dynamic.RobertaForMaskedLM(config)[source]
Roberta for masked language model task.
- forward(input_ids: torch.LongTensor | None = None, attention_mask: torch.FloatTensor | None = None, token_type_ids: torch.LongTensor | None = None, position_ids: torch.LongTensor | None = None, head_mask: torch.FloatTensor | None = None, inputs_embeds: torch.FloatTensor | None = None, encoder_hidden_states: torch.FloatTensor | None = None, encoder_attention_mask: torch.FloatTensor | None = None, labels: torch.LongTensor | None = None, output_attentions: bool | None = None, output_hidden_states: bool | None = None, return_dict: bool | None = None) Tuple[torch.Tensor] | transformers.modeling_outputs.MaskedLMOutput[source]
The main entry point for the class.
- labels (torch.LongTensor of shape (batch_size, sequence_length), optional):
Labels for computing the masked language modeling loss. Indices should be in [-100, 0, …, config.vocab_size] (see input_ids docstring) Tokens with indices set to -100 are ignored (masked), the loss is only computed for the tokens with labels in [0, …, config.vocab_size]
- kwargs (Dict[str, any], optional, defaults to {}):
Used to hide legacy arguments that have been deprecated.
- class intel_extension_for_transformers.transformers.modeling.modeling_roberta_dynamic.RobertaLMHead(config)[source]
Roberta Head for masked language modeling.
- class intel_extension_for_transformers.transformers.modeling.modeling_roberta_dynamic.RobertaForSequenceClassification(config)[source]
Roberta for sequence classification task.
- forward(input_ids: torch.LongTensor | None = None, attention_mask: torch.FloatTensor | None = None, token_type_ids: torch.LongTensor | None = None, position_ids: torch.LongTensor | None = None, head_mask: torch.FloatTensor | None = None, inputs_embeds: torch.FloatTensor | None = None, labels: torch.LongTensor | None = None, output_attentions: bool | None = None, output_hidden_states: bool | None = None, return_dict: bool | None = None, layer_config=None, length_config=None, always_keep_cls_token=True) Tuple[torch.Tensor] | transformers.modeling_outputs.SequenceClassifierOutput[source]
The main entry point for the class.
- labels (torch.LongTensor of shape (batch_size,), optional):
Labels for computing the sequence classification/regression loss. Indices should be in [0, …, config.num_labels - 1]. If config.num_labels == 1 a regression loss is computed (Mean-Square loss), If config.num_labels > 1 a classification loss is computed (Cross-Entropy).
- class intel_extension_for_transformers.transformers.modeling.modeling_roberta_dynamic.RobertaForMultipleChoice(config)[source]
Roberta for multiple choice task.
- forward(input_ids: torch.LongTensor | None = None, token_type_ids: torch.LongTensor | None = None, attention_mask: torch.FloatTensor | None = None, labels: torch.LongTensor | None = None, position_ids: torch.LongTensor | None = None, head_mask: torch.FloatTensor | None = None, inputs_embeds: torch.FloatTensor | None = None, output_attentions: bool | None = None, output_hidden_states: bool | None = None, return_dict: bool | None = None) Tuple[torch.Tensor] | transformers.modeling_outputs.MultipleChoiceModelOutput[source]
The main entry point for the class.
- labels (torch.LongTensor of shape (batch_size,), optional):
Labels for computing the multiple choice classification loss. Indices should be in [0, …, num_choices-1] where num_choices is the size of the second dimension of the input tensors. (See input_ids above)
- class intel_extension_for_transformers.transformers.modeling.modeling_roberta_dynamic.RobertaForTokenClassification(config)[source]
Roberta for token classification task.
- forward(input_ids: torch.LongTensor | None = None, attention_mask: torch.FloatTensor | None = None, token_type_ids: torch.LongTensor | None = None, position_ids: torch.LongTensor | None = None, head_mask: torch.FloatTensor | None = None, inputs_embeds: torch.FloatTensor | None = None, labels: torch.LongTensor | None = None, output_attentions: bool | None = None, output_hidden_states: bool | None = None, return_dict: bool | None = None) Tuple[torch.Tensor] | transformers.modeling_outputs.TokenClassifierOutput[source]
The main entry point for the class.
- labels (torch.LongTensor of shape (batch_size, sequence_length), optional):
Labels for computing the token classification loss. Indices should be in [0, …, config.num_labels - 1].
- class intel_extension_for_transformers.transformers.modeling.modeling_roberta_dynamic.RobertaClassificationHead(config)[source]
Head for sentence-level classification tasks.
- class intel_extension_for_transformers.transformers.modeling.modeling_roberta_dynamic.RobertaForQuestionAnswering(config)[source]
Roberta model for quanstion answering task.
- forward(input_ids: torch.LongTensor | None = None, attention_mask: torch.FloatTensor | None = None, token_type_ids: torch.LongTensor | None = None, position_ids: torch.LongTensor | None = None, head_mask: torch.FloatTensor | None = None, inputs_embeds: torch.FloatTensor | None = None, start_positions: torch.LongTensor | None = None, end_positions: torch.LongTensor | None = None, output_attentions: bool | None = None, output_hidden_states: bool | None = None, return_dict: bool | None = None, layer_config=None, length_config=None, always_keep_cls_token=False) Tuple[torch.Tensor] | transformers.modeling_outputs.QuestionAnsweringModelOutput[source]
The main entry point for the class.
- start_positions (torch.LongTensor of shape (batch_size,), optional):
Labels for position (index) of the start of the labelled span for computing the token classification loss. Positions are clamped to the length of the sequence (sequence_length). Position outside of the sequence are not taken into account for computing the loss.
- end_positions (torch.LongTensor of shape (batch_size,), optional):
Labels for position (index) of the end of the labelled span for computing the token classification loss. Positions are clamped to the length of the sequence (sequence_length). Position outside of the sequence are not taken into account for computing the loss.
- intel_extension_for_transformers.transformers.modeling.modeling_roberta_dynamic.create_position_ids_from_input_ids(input_ids, padding_idx, past_key_values_length=0)[source]
Replace non-padding symbols with their position numbers.
Position numbers begin at padding_idx+1. Padding symbols are ignored. This is modified from fairseq’s utils.make_positions.