(self, sentences_batch, instruction)
| 48 | |
| 49 | @torch.no_grad() |
| 50 | def encode(self, sentences_batch, instruction): |
| 51 | inputs = self.tokenizer( |
| 52 | sentences_batch, |
| 53 | padding=True, |
| 54 | truncation=True, |
| 55 | return_tensors="pt", |
| 56 | max_length=self.max_length, |
| 57 | add_special_tokens=True, |
| 58 | ).to(self.device) |
| 59 | |
| 60 | with torch.no_grad(): |
| 61 | outputs = self.model(**inputs) |
| 62 | last_hidden_state = outputs[0] |
| 63 | |
| 64 | instruction_tokens = self.tokenizer( |
| 65 | instruction, |
| 66 | padding=False, |
| 67 | truncation=True, |
| 68 | max_length=self.max_length, |
| 69 | add_special_tokens=True, |
| 70 | )["input_ids"] |
| 71 | if len(np.shape(np.array(instruction_tokens))) == 1: |
| 72 | inputs["attention_mask"][:, :len(instruction_tokens)] = 0 |
| 73 | else: |
| 74 | instruction_length = [len(item) for item in instruction_tokens] |
| 75 | assert len(instruction) == len(sentences_batch) |
| 76 | for idx in range(len(instruction_length)): |
| 77 | inputs["attention_mask"][idx, :instruction_length[idx]] = 0 |
| 78 | |
| 79 | embeddings = self.mean_pooling(last_hidden_state, inputs["attention_mask"]) |
| 80 | embeddings = torch.nn.functional.normalize(embeddings, dim=-1) |
| 81 | return embeddings |
| 82 | |
| 83 | def encode_queries(self, queries): |
| 84 | queries = queries if isinstance(queries, list) else [queries] |
no test coverage detected