Run the forward pass for an encoder-decoder model. First feed a batch of source tokens through the encoder. Then, feed the encoder output and previous decoder outputs (i.e., teacher forcing) to the decoder to produce the next outputs:: encoder_out = sel
(self, src_tokens, src_lengths, prev_output_tokens, **kwargs)
| 286 | assert isinstance(self.decoder, FairseqDecoder) |
| 287 | |
| 288 | def forward(self, src_tokens, src_lengths, prev_output_tokens, **kwargs): |
| 289 | """ |
| 290 | Run the forward pass for an encoder-decoder model. |
| 291 | |
| 292 | First feed a batch of source tokens through the encoder. Then, feed the |
| 293 | encoder output and previous decoder outputs (i.e., teacher forcing) to |
| 294 | the decoder to produce the next outputs:: |
| 295 | |
| 296 | encoder_out = self.encoder(src_tokens, src_lengths) |
| 297 | return self.decoder(prev_output_tokens, encoder_out) |
| 298 | |
| 299 | Args: |
| 300 | src_tokens (LongTensor): tokens in the source language of shape |
| 301 | `(batch, src_len)` |
| 302 | src_lengths (LongTensor): source sentence lengths of shape `(batch)` |
| 303 | prev_output_tokens (LongTensor): previous decoder outputs of shape |
| 304 | `(batch, tgt_len)`, for teacher forcing |
| 305 | |
| 306 | Returns: |
| 307 | tuple: |
| 308 | - the decoder's output of shape `(batch, tgt_len, vocab)` |
| 309 | - a dictionary with any model-specific outputs |
| 310 | """ |
| 311 | encoder_out = self.encoder(src_tokens, src_lengths=src_lengths, **kwargs) |
| 312 | decoder_out = self.decoder( |
| 313 | prev_output_tokens, encoder_out=encoder_out, **kwargs |
| 314 | ) |
| 315 | return decoder_out |
| 316 | |
| 317 | def forward_decoder(self, prev_output_tokens, **kwargs): |
| 318 | return self.decoder(prev_output_tokens, **kwargs) |