in lama/modules/bert_connector.py [0:0]
def get_contextual_embeddings(self, sentences_list, try_cuda=True):
# assume in input 1 or 2 sentences - in general, it considers only the first 2 sentences
if not sentences_list:
return None
if try_cuda:
self.try_cuda()
tokens_tensor, segments_tensor, attention_mask_tensor, masked_indices_list, tokenized_text_list = self.__get_input_tensors_batch(sentences_list)
with torch.no_grad():
all_encoder_layers, _ = self.bert_model(
tokens_tensor.to(self._model_device),
segments_tensor.to(self._model_device))
all_encoder_layers = [layer.cpu() for layer in all_encoder_layers]
sentence_lengths = [len(x) for x in tokenized_text_list]
# all_encoder_layers: a list of the full sequences of encoded-hidden-states at the end
# of each attention block (i.e. 12 full sequences for BERT-base, 24 for BERT-large), each
# encoded-hidden-state is a torch.FloatTensor of size [batch_size, sequence_length, hidden_size]
return all_encoder_layers, sentence_lengths, tokenized_text_list