| 492 | self.decoding = decoding |
| 493 | |
| 494 | def forward(self, input_token, inputs_embeds, beam_size, max_seq_len, |
| 495 | top_k, top_p, beam_search_diversity_rate = 0.0, |
| 496 | temperature=1.0, len_penalty=0.0, repetition_penalty=None, presence_penalty=None, min_length=0, random_seed=0, |
| 497 | is_return_output_log_probs=False, is_return_cum_log_probs=False, is_return_cross_attentions=False, |
| 498 | bad_words_list=None, stop_words_list=None): |
| 499 | input_ids = input_token.input_ids.to("cuda").type(torch.int32) |
| 500 | mem_seq_len = 0 |
| 501 | if hasattr(input_token, "attention_mask"): |
| 502 | mem_seq_len = torch.sum(input_token.attention_mask, dim=1).type(torch.int32).to("cuda") |
| 503 | else: |
| 504 | mem_seq_len = input_token.seq_len.type(torch.int32).to("cuda") |
| 505 | |
| 506 | ft_encoder_outputs = self.encoder.forward(input_ids, mem_seq_len, inputs_embeds) |
| 507 | results = self.decoding.forward(beam_size, # optional, can be None |
| 508 | max_seq_len, |
| 509 | top_k, # optional, can be None |
| 510 | top_p, # optional, can be None |
| 511 | beam_search_diversity_rate, # optional, can be None |
| 512 | temperature, # optional, can be None |
| 513 | len_penalty, # optional, can be None |
| 514 | repetition_penalty, # optional, can be None |
| 515 | presence_penalty, # optional, can be None |
| 516 | min_length, # optional, can be None |
| 517 | random_seed, # optional, can be None |
| 518 | ft_encoder_outputs, |
| 519 | mem_seq_len, |
| 520 | is_return_output_log_probs, # optional, can be None |
| 521 | is_return_cum_log_probs, # optional, can be None |
| 522 | is_return_cross_attentions, # optional, can be None |
| 523 | bad_words_list, # optional, can be None |
| 524 | stop_words_list, # optional, can be None |
| 525 | ) |
| 526 | ft_decoding_outputs = results.pop(0).reshape([-1, beam_size, max_seq_len]) |
| 527 | ft_decoding_seq_lens = results.pop(0).reshape([-1, beam_size]) |
| 528 | if is_return_output_log_probs: |
| 529 | ft_output_log_probs = results.pop(0) |
| 530 | if is_return_cum_log_probs: |
| 531 | ft_cum_log_probs = results.pop(0) |
| 532 | if is_return_cross_attentions: |
| 533 | ft_cross_attentions = results.pop(0) |
| 534 | return ft_decoding_outputs.cpu().numpy(), ft_decoding_seq_lens.cpu().numpy(), ft_cross_attentions.cpu().numpy() |
| 535 | |
| 536 | return ft_decoding_outputs.cpu().numpy(), ft_decoding_seq_lens.cpu().numpy() |