Base class for encoders.
| 24 | |
| 25 | |
| 26 | class FairseqEncoder(nn.Module): |
| 27 | """Base class for encoders.""" |
| 28 | |
| 29 | def __init__(self, dictionary): |
| 30 | super().__init__() |
| 31 | self.dictionary = dictionary |
| 32 | |
| 33 | def forward(self, src_tokens, src_lengths=None, **kwargs): |
| 34 | """ |
| 35 | Args: |
| 36 | src_tokens (LongTensor): tokens in the source language of shape |
| 37 | `(batch, src_len)` |
| 38 | src_lengths (LongTensor): lengths of each source sentence of shape |
| 39 | `(batch)` |
| 40 | """ |
| 41 | raise NotImplementedError |
| 42 | |
| 43 | def forward_torchscript(self, net_input: Dict[str, Tensor]): |
| 44 | """A TorchScript-compatible version of forward. |
| 45 | |
| 46 | Encoders which use additional arguments may want to override |
| 47 | this method for TorchScript compatibility. |
| 48 | """ |
| 49 | if torch.jit.is_scripting(): |
| 50 | return self.forward( |
| 51 | src_tokens=net_input["src_tokens"], |
| 52 | src_lengths=net_input["src_lengths"], |
| 53 | ) |
| 54 | else: |
| 55 | return self.forward_non_torchscript(net_input) |
| 56 | |
| 57 | @torch.jit.unused |
| 58 | def forward_non_torchscript(self, net_input: Dict[str, Tensor]): |
| 59 | encoder_input = { |
| 60 | k: v for k, v in net_input.items() if k != "prev_output_tokens" |
| 61 | } |
| 62 | return self.forward(**encoder_input) |
| 63 | |
| 64 | def reorder_encoder_out(self, encoder_out, new_order): |
| 65 | """ |
| 66 | Reorder encoder output according to `new_order`. |
| 67 | |
| 68 | Args: |
| 69 | encoder_out: output from the ``forward()`` method |
| 70 | new_order (LongTensor): desired order |
| 71 | |
| 72 | Returns: |
| 73 | `encoder_out` rearranged according to `new_order` |
| 74 | """ |
| 75 | raise NotImplementedError |
| 76 | |
| 77 | def max_positions(self): |
| 78 | """Maximum input length supported by the encoder.""" |
| 79 | return 1e6 # an arbitrary large number |
| 80 | |
| 81 | def upgrade_state_dict_named(self, state_dict, name): |
| 82 | """Upgrade old state dicts to work with newer code.""" |
| 83 | return state_dict |
nothing calls this directly
no outgoing calls
no test coverage detected