Class: Transformers::XlmRoberta::XLMRobertaEmbeddings
- Inherits:
-
Torch::NN::Module
- Object
- Torch::NN::Module
- Transformers::XlmRoberta::XLMRobertaEmbeddings
- Defined in:
- lib/transformers/models/xlm_roberta/modeling_xlm_roberta.rb
Instance Method Summary collapse
- #create_position_ids_from_input_ids(input_ids, padding_idx, past_key_values_length: 0) ⇒ Object
- #create_position_ids_from_inputs_embeds(inputs_embeds) ⇒ Object
- #forward(input_ids: nil, token_type_ids: nil, position_ids: nil, inputs_embeds: nil, past_key_values_length: 0) ⇒ Object
-
#initialize(config) ⇒ XLMRobertaEmbeddings
constructor
A new instance of XLMRobertaEmbeddings.
Constructor Details
#initialize(config) ⇒ XLMRobertaEmbeddings
Returns a new instance of XLMRobertaEmbeddings.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/transformers/models/xlm_roberta/modeling_xlm_roberta.rb', line 19 def initialize(config) super() @word_embeddings = Torch::NN::Embedding.new(config.vocab_size, config.hidden_size, padding_idx: config.pad_token_id) @position_embeddings = Torch::NN::Embedding.new(config., config.hidden_size) @token_type_embeddings = Torch::NN::Embedding.new(config.type_vocab_size, config.hidden_size) # self.LayerNorm is not snake-cased to stick with TensorFlow model variable name and be able to load # any TensorFlow checkpoint file @LayerNorm = Torch::NN::LayerNorm.new(config.hidden_size, eps: config.layer_norm_eps) @dropout = Torch::NN::Dropout.new(p: config.hidden_dropout_prob) # position_ids (1, len position emb) is contiguous in memory and exported when serialized @position_embedding_type = config.getattr("position_embedding_type", "absolute") register_buffer("position_ids", Torch.arange(config.).([1, -1]), persistent: false) register_buffer("token_type_ids", Torch.zeros(@position_ids.size, dtype: Torch.long), persistent: false) @padding_idx = config.pad_token_id @position_embeddings = Torch::NN::Embedding.new(config., config.hidden_size, padding_idx: @padding_idx) end |
Instance Method Details
#create_position_ids_from_input_ids(input_ids, padding_idx, past_key_values_length: 0) ⇒ Object
92 93 94 95 96 97 |
# File 'lib/transformers/models/xlm_roberta/modeling_xlm_roberta.rb', line 92 def create_position_ids_from_input_ids(input_ids, padding_idx, past_key_values_length: 0) # The series of casts and type-conversions here are carefully balanced to both work with ONNX export and XLA. mask = input_ids.ne(padding_idx).int incremental_indices = (Torch.cumsum(mask, dim: 1).type_as(mask) + past_key_values_length) * mask incremental_indices.long + padding_idx end |
#create_position_ids_from_inputs_embeds(inputs_embeds) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/transformers/models/xlm_roberta/modeling_xlm_roberta.rb', line 84 def () input_shape = .size[...-1] sequence_length = input_shape[1] position_ids = Torch.arange(@padding_idx + 1, sequence_length + @padding_idx + 1, dtype: Torch.long, device: .device) position_ids.unsqueeze(0).(input_shape) end |
#forward(input_ids: nil, token_type_ids: nil, position_ids: nil, inputs_embeds: nil, past_key_values_length: 0) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/transformers/models/xlm_roberta/modeling_xlm_roberta.rb', line 38 def forward(input_ids: nil, token_type_ids: nil, position_ids: nil, inputs_embeds: nil, past_key_values_length: 0) if position_ids.nil? if !input_ids.nil? # Create the position ids from the input token ids. Any padded tokens remain padded. position_ids = create_position_ids_from_input_ids(input_ids, @padding_idx, past_key_values_length:) else position_ids = () end end if !input_ids.nil? input_shape = input_ids.size else input_shape = .size[...-1] end seq_length = input_shape[1] # Setting the token_type_ids to the registered buffer in constructor where it is all zeros, which usually occurs # when its auto-generated, registered buffer helps users when tracing the model without passing token_type_ids, solves # issue #5664 if token_type_ids.nil? if respond_to?(:token_type_ids) buffered_token_type_ids = token_type_ids[0.., ...seq_length] = buffered_token_type_ids.(input_shape[0], seq_length) token_type_ids = else token_type_ids = Torch.zeros(input_shape, dtype: Torch.long, device: @position_ids.device) end end if .nil? = @word_embeddings.(input_ids) end = @token_type_embeddings.(token_type_ids) = + if @position_embedding_type == "absolute" = @position_embeddings.(position_ids) += end = @LayerNorm.() = @dropout.() end |