|
| 1 | +from typing import List |
| 2 | + |
| 3 | +import torch |
| 4 | +from transformers import AutoTokenizer, AutoModel |
| 5 | + |
| 6 | +from aloha.service.streamer import ManagedModel |
| 7 | + |
| 8 | +SEED = 0 |
| 9 | +torch.manual_seed(SEED) |
| 10 | +torch.cuda.manual_seed(SEED) |
| 11 | + |
| 12 | + |
| 13 | +class TextUnmaskModel: |
| 14 | + def __init__(self, max_sent_len=16, model_path="bert-base-uncased"): |
| 15 | + self.model_path = model_path |
| 16 | + self.tokenizer = AutoTokenizer.from_pretrained(self.model_path) |
| 17 | + self.transformer = AutoModel.from_pretrained(self.model_path) |
| 18 | + self.transformer.eval() |
| 19 | + self.transformer.to(device="cuda") |
| 20 | + self.max_sent_len = max_sent_len |
| 21 | + |
| 22 | + def predict(self, batch: List[str]) -> List[str]: |
| 23 | + """predict masked word""" |
| 24 | + batch_inputs = [] |
| 25 | + masked_indexes = [] |
| 26 | + |
| 27 | + for text in batch: |
| 28 | + tokenized_text = self.tokenizer.tokenize(text) |
| 29 | + if len(tokenized_text) > self.max_sent_len - 2: |
| 30 | + tokenized_text = tokenized_text[: self.max_sent_len - 2] |
| 31 | + |
| 32 | + tokenized_text = ['[CLS]'] + tokenized_text + ['[SEP]'] |
| 33 | + tokenized_text += ['[PAD]'] * (self.max_sent_len - len(tokenized_text)) |
| 34 | + |
| 35 | + indexed_tokens = self.tokenizer.convert_tokens_to_ids(tokenized_text) |
| 36 | + batch_inputs.append(indexed_tokens) |
| 37 | + masked_indexes.append(tokenized_text.index('[MASK]')) |
| 38 | + |
| 39 | + tokens_tensor = torch.tensor(batch_inputs).to("cuda") |
| 40 | + |
| 41 | + with torch.no_grad(): |
| 42 | + # prediction_scores: ``torch.FloatTensor`` of shape ``(batch_size, sequence_length, config.vocab_size)`` |
| 43 | + prediction_scores = self.transformer(tokens_tensor)[0] |
| 44 | + |
| 45 | + batch_outputs = [] |
| 46 | + for i in range(len(batch_inputs)): |
| 47 | + predicted_index = torch.argmax(prediction_scores[i, masked_indexes[i]]).item() |
| 48 | + predicted_token = self.tokenizer.convert_ids_to_tokens(predicted_index) |
| 49 | + batch_outputs.append(predicted_token) |
| 50 | + |
| 51 | + return batch_outputs |
| 52 | + |
| 53 | + |
| 54 | +class ManagedBertModel(ManagedModel): |
| 55 | + def init_model(self): |
| 56 | + self.model = TextUnmaskModel() |
| 57 | + |
| 58 | + def predict(self, batch): |
| 59 | + return self.model.predict(batch) |
| 60 | + |
| 61 | + |
| 62 | +def test_simple(): |
| 63 | + tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased") |
| 64 | + model = AutoModel.from_pretrained("bert-base-uncased") |
| 65 | + inputs = tokenizer("Hello! My name is [MASK]!", return_tensors="pt") |
| 66 | + outputs = model(**inputs) |
| 67 | + print(outputs) |
| 68 | + |
| 69 | + predicted_index = torch.argmax(outputs[1]).item() |
| 70 | + predicted_token = tokenizer.convert_ids_to_tokens(predicted_index) |
| 71 | + print(predicted_token) |
| 72 | + |
| 73 | + |
| 74 | +def test_batch(): |
| 75 | + batch_text = [ |
| 76 | + "twinkle twinkle [MASK] star.", |
| 77 | + "Happy birthday to [MASK].", |
| 78 | + 'the answer to life, the [MASK], and everything.' |
| 79 | + ] |
| 80 | + model = TextUnmaskModel() |
| 81 | + outputs = model.predict(batch_text) |
| 82 | + print(outputs) |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + test_simple() |
0 commit comments