| 11 | |
| 12 | @R.register("models.MyESM") |
| 13 | class MyESM(models.EvolutionaryScaleModeling): |
| 14 | |
| 15 | def forward(self, graph, input, all_loss=None, metric=None): |
| 16 | """ |
| 17 | Compute the residue representations and the graph representation(s). |
| 18 | |
| 19 | Parameters: |
| 20 | graph (Protein): :math:`n` protein(s) |
| 21 | input (Tensor): input node representations |
| 22 | all_loss (Tensor, optional): if specified, add loss to this tensor |
| 23 | metric (dict, optional): if specified, output metrics to this dict |
| 24 | |
| 25 | Returns: |
| 26 | dict with ``residue_feature`` and ``graph_feature`` fields: |
| 27 | residue representations of shape :math:`(|V_{res}|, d)`, graph representations of shape :math:`(n, d)` |
| 28 | """ |
| 29 | input = graph.residue_type |
| 30 | input = self.mapping[input] |
| 31 | input[input == -1] = graph.residue_type[input == -1] |
| 32 | size = graph.num_residues |
| 33 | if (size > self.max_input_length).any(): |
| 34 | warnings.warn("ESM can only encode proteins within %d residues. Truncate the input to fit into ESM." |
| 35 | % self.max_input_length) |
| 36 | starts = size.cumsum(0) - size |
| 37 | size = size.clamp(max=self.max_input_length) |
| 38 | ends = starts + size |
| 39 | mask = functional.multi_slice_mask(starts, ends, graph.num_residue) |
| 40 | input = input[mask] |
| 41 | graph = graph.subresidue(mask) |
| 42 | size_ext = size |
| 43 | if self.alphabet.prepend_bos: |
| 44 | bos = torch.ones(graph.batch_size, dtype=torch.long, device=self.device) * self.alphabet.cls_idx |
| 45 | input, size_ext = functional._extend(bos, torch.ones_like(size_ext), input, size_ext) |
| 46 | if self.alphabet.append_eos: |
| 47 | eos = torch.ones(graph.batch_size, dtype=torch.long, device=self.device) * self.alphabet.eos_idx |
| 48 | input, size_ext = functional._extend(input, size_ext, eos, torch.ones_like(size_ext)) |
| 49 | input = functional.variadic_to_padded(input, size_ext, value=self.alphabet.padding_idx)[0] |
| 50 | |
| 51 | output = self.model(input, repr_layers=[self.repr_layer]) |
| 52 | residue_feature = output["representations"][self.repr_layer] |
| 53 | logits = output["logits"] |
| 54 | |
| 55 | residue_feature = functional.padded_to_variadic(residue_feature, size_ext) |
| 56 | logits = functional.padded_to_variadic(logits, size_ext) |
| 57 | starts = size_ext.cumsum(0) - size_ext |
| 58 | if self.alphabet.prepend_bos: |
| 59 | starts = starts + 1 |
| 60 | ends = starts + size |
| 61 | mask = functional.multi_slice_mask(starts, ends, len(residue_feature)) |
| 62 | residue_feature = residue_feature[mask] |
| 63 | logits = logits[mask] |
| 64 | residue_type_index = torch.arange(20, dtype=torch.long, device=logits.device) |
| 65 | logits = logits[:, self.mapping[residue_type_index]] |
| 66 | graph_feature = self.readout(graph, residue_feature) |
| 67 | |
| 68 | return { |
| 69 | "graph_feature": graph_feature, |
| 70 | "residue_feature": residue_feature, |
nothing calls this directly
no outgoing calls
no test coverage detected