| |
| |
| |
| |
|
|
| import math |
| import sys |
| from typing import Dict, List, Optional |
| import torch |
| from torch import Tensor |
|
|
| from fairseq.sequence_generator import EnsembleModel as EnsembleModelBase |
| from fairseq.sequence_generator import SequenceGenerator as SequenceGeneratorBase |
|
|
|
|
| class SequenceGenerator(SequenceGeneratorBase): |
| def __init__( |
| self, |
| models, |
| tgt_dict, |
| beam_size=1, |
| max_len_a=0, |
| max_len_b=200, |
| max_len=0, |
| min_len=1, |
| normalize_scores=True, |
| len_penalty=1.0, |
| unk_penalty=0.0, |
| temperature=1.0, |
| match_source_len=False, |
| no_repeat_ngram_size=0, |
| search_strategy=None, |
| eos=None, |
| symbols_to_strip_from_output=None, |
| lm_model=None, |
| lm_weight=1.0, |
| tokens_to_suppress=(), |
| use_incremental_states=False, |
| ): |
| """Generates translations of a given source sentence. |
| |
| Args: |
| models (List[~fairseq.models.FairseqModel]): ensemble of models, |
| currently support fairseq.models.TransformerModel for scripting |
| beam_size (int, optional): beam width (default: 1) |
| max_len_a/b (int, optional): generate sequences of maximum length |
| ax + b, where x is the source length |
| max_len (int, optional): the maximum length of the generated output |
| (not including end-of-sentence) |
| min_len (int, optional): the minimum length of the generated output |
| (not including end-of-sentence) |
| normalize_scores (bool, optional): normalize scores by the length |
| of the output (default: True) |
| len_penalty (float, optional): length penalty, where <1.0 favors |
| shorter, >1.0 favors longer sentences (default: 1.0) |
| unk_penalty (float, optional): unknown word penalty, where <0 |
| produces more unks, >0 produces fewer (default: 0.0) |
| temperature (float, optional): temperature, where values |
| >1.0 produce more uniform samples and values <1.0 produce |
| sharper samples (default: 1.0) |
| match_source_len (bool, optional): outputs should match the source |
| length (default: False) |
| """ |
| super().__init__( |
| models=models, |
| tgt_dict=tgt_dict, |
| beam_size=beam_size, |
| max_len_a=max_len_a, |
| max_len_b=max_len_b, |
| max_len=max_len, |
| min_len=min_len, |
| normalize_scores=normalize_scores, |
| len_penalty=len_penalty, |
| unk_penalty=unk_penalty, |
| temperature=temperature, |
| match_source_len=match_source_len, |
| no_repeat_ngram_size=no_repeat_ngram_size, |
| search_strategy=search_strategy, |
| eos=eos, |
| symbols_to_strip_from_output=symbols_to_strip_from_output, |
| lm_model=lm_model, |
| lm_weight=lm_weight, |
| tokens_to_suppress=tokens_to_suppress, |
| ) |
|
|
| if isinstance(models, EnsembleModel): |
| self.model = models |
| else: |
| self.model = EnsembleModel(models) |
|
|
| self.model.set_decoder_beam_size(self.beam_size) |
| self.model.eval() |
| self.use_incremental_states = use_incremental_states |
| self.incremental_states = None |
| if not self.use_incremental_states: |
| self.model.has_incremental = False |
|
|
| def reset_incremental_states(self): |
| self.incremental_states = None |
|
|
| def _generate( |
| self, |
| sample: Dict[str, Dict[str, Tensor]], |
| prefix_tokens: Optional[Tensor] = None, |
| constraints: Optional[Tensor] = None, |
| bos_token: Optional[int] = None, |
| ): |
| net_input = sample["net_input"] |
|
|
| if "src_tokens" in net_input: |
| src_tokens = net_input["src_tokens"] |
| |
| |
| if "src_lengths" in net_input: |
| src_lengths = net_input["src_lengths"] |
| else: |
| src_lengths = ( |
| (src_tokens.ne(self.eos) & src_tokens.ne(self.pad)) |
| .long() |
| .sum(dim=1) |
| ) |
| elif "source" in net_input: |
| src_tokens = net_input["source"] |
| src_lengths = ( |
| net_input["padding_mask"].size(-1) - net_input["padding_mask"].sum(-1) |
| if net_input["padding_mask"] is not None |
| else torch.tensor(src_tokens.size(-1)).to(src_tokens) |
| ) |
| elif "features" in net_input: |
| src_tokens = net_input["features"] |
| src_lengths = ( |
| net_input["padding_mask"].size(-1) - net_input["padding_mask"].sum(-1) |
| if net_input["padding_mask"] is not None |
| else torch.tensor(src_tokens.size(-1)).to(src_tokens) |
| ) |
| else: |
| raise Exception( |
| "expected src_tokens or source in net input. input keys: " |
| + str(net_input.keys()) |
| ) |
|
|
| if constraints is not None and not self.search.supports_constraints: |
| raise NotImplementedError( |
| "Target-side constraints were provided, but search method doesn't support them" |
| ) |
|
|
| |
| self.search.init_constraints(constraints, self.beam_size) |
|
|
| |
| with torch.autograd.profiler.record_function("EnsembleModel: forward_encoder"): |
| encoder_outs = self.model.forward_encoder(net_input) |
|
|
| finalized = self.generate_decoder( |
| encoder_outs, |
| src_tokens, |
| src_lengths, |
| sample, |
| prefix_tokens, |
| constraints, |
| bos_token, |
| ) |
| return finalized |
|
|
| def generate_decoder( |
| self, |
| encoder_outs, |
| src_tokens, |
| src_lengths, |
| sample: Dict[str, Dict[str, Tensor]], |
| prefix_tokens: Optional[Tensor] = None, |
| constraints: Optional[Tensor] = None, |
| bos_token: Optional[int] = None, |
| aux_task_name="", |
| encoder_outs_aug: Optional[ |
| Tensor |
| ] = None, |
| max_new_tokens=-1, |
| **kwargs, |
| ): |
| if self.use_incremental_states: |
| if self.incremental_states is None: |
| incremental_states = torch.jit.annotate( |
| List[Dict[str, Dict[str, Optional[Tensor]]]], |
| [ |
| torch.jit.annotate(Dict[str, Dict[str, Optional[Tensor]]], {}) |
| for i in range(self.model.models_size) |
| ], |
| ) |
| self.incremental_states = incremental_states |
| else: |
| incremental_states = self.incremental_states |
| else: |
| incremental_states = None |
|
|
| |
| |
| bsz, src_len = src_tokens.size()[:2] |
| beam_size = self.beam_size |
|
|
| decoder_name = f"{aux_task_name}_decoder" if aux_task_name else "decoder" |
|
|
| start = prefix_tokens.size(-1) if prefix_tokens is not None else 0 |
|
|
| if max_new_tokens == -1: |
| max_len: int = -1 |
| if self.match_source_len: |
| max_len = src_lengths.max().item() |
| else: |
| max_len = min( |
| int(self.max_len_a * src_len + self.max_len_b), |
| self.max_len - 1, |
| ) |
| else: |
| max_len = start + max_new_tokens |
| assert ( |
| self.min_len <= max_len |
| ), "min_len cannot be larger than max_len, please adjust these!" |
|
|
| |
| new_order = ( |
| torch.arange(bsz, device=src_tokens.device) |
| .view(-1, 1) |
| .repeat(1, beam_size) |
| .view(-1) |
| ) |
| new_order = new_order.long() |
| encoder_outs = self.model.reorder_encoder_out(encoder_outs, new_order) |
| |
| assert encoder_outs is not None |
| if encoder_outs_aug is not None: |
| encoder_outs_aug = self.model.reorder_encoder_out( |
| encoder_outs_aug, new_order |
| ) |
|
|
| |
| scores = torch.zeros( |
| bsz * beam_size, max_len + 1, device=src_tokens.device |
| ).float() |
| if prefix_tokens is None: |
| tokens = ( |
| torch.zeros(bsz * beam_size, max_len + 2, device=src_tokens.device) |
| .long() |
| .fill_(self.pad) |
| ) |
| tokens[:, 0] = self.eos if bos_token is None else bos_token |
| else: |
| tokens = torch.cat( |
| ( |
| torch.full( |
| (bsz, 1), self.eos, dtype=torch.long, device=src_tokens.device |
| ), |
| prefix_tokens, |
| ), |
| dim=-1, |
| ) |
| tokens = tokens.repeat(beam_size, 1) |
| if tokens.size(-1) < max_len + 2: |
| tokens = torch.cat( |
| ( |
| tokens, |
| torch.zeros( |
| tokens.size(0), |
| max_len + 2 - tokens.size(-1), |
| device=src_tokens.device, |
| ).long(), |
| ), |
| dim=-1, |
| ) |
|
|
| attn: Optional[Tensor] = None |
|
|
| |
| |
| |
| |
| cands_to_ignore = torch.zeros(bsz, beam_size, device=src_tokens.device).eq( |
| -1 |
| ) |
|
|
| |
| finalized = torch.jit.annotate( |
| List[List[Dict[str, Tensor]]], |
| [torch.jit.annotate(List[Dict[str, Tensor]], []) for i in range(bsz)], |
| ) |
|
|
| |
| finished = [False for i in range(bsz)] |
| num_remaining_sent = bsz |
|
|
| |
| cand_size = 2 * beam_size |
|
|
| |
| bbsz_offsets = ( |
| (torch.arange(0, bsz, device=src_tokens.device) * beam_size) |
| .unsqueeze(1) |
| .type_as(tokens) |
| ) |
| cand_offsets = torch.arange(0, cand_size, device=src_tokens.device).type_as( |
| tokens |
| ) |
|
|
| reorder_state: Optional[Tensor] = None |
| batch_idxs: Optional[Tensor] = None |
|
|
| original_batch_idxs: Optional[Tensor] = None |
| if "id" in sample and isinstance(sample["id"], Tensor): |
| original_batch_idxs = sample["id"] |
| else: |
| original_batch_idxs = torch.arange(0, bsz).type_as(tokens) |
|
|
| for step in range(start, max_len + 1): |
| |
| if reorder_state is not None: |
| if batch_idxs is not None: |
| |
| corr = batch_idxs - torch.arange(batch_idxs.numel()).type_as( |
| batch_idxs |
| ) |
| reorder_state.view(-1, beam_size).add_( |
| corr.unsqueeze(-1) * beam_size |
| ) |
| original_batch_idxs = original_batch_idxs[batch_idxs] |
| self.model.reorder_incremental_state( |
| incremental_states, reorder_state, decoder_name |
| ) |
| encoder_outs = self.model.reorder_encoder_out( |
| encoder_outs, reorder_state |
| ) |
| if encoder_outs_aug is not None: |
| encoder_outs_aug = self.model.reorder_encoder_out( |
| encoder_outs_aug, reorder_state |
| ) |
| with torch.autograd.profiler.record_function( |
| "EnsembleModel: forward_decoder" |
| ): |
| lprobs, avg_attn_scores = self.model.forward_decoder( |
| tokens[:, : step + 1], |
| encoder_outs, |
| incremental_states, |
| self.temperature, |
| decoder_name=decoder_name, |
| encoder_outs_aug=encoder_outs_aug, |
| **kwargs, |
| ) |
|
|
| if self.lm_model is not None and not aux_task_name: |
| lm_out = self.lm_model(tokens[:, : step + 1]) |
| probs = self.lm_model.get_normalized_probs( |
| lm_out, log_probs=True, sample=None |
| ) |
| probs = probs[:, -1, :] * self.lm_weight |
| lprobs += probs |
|
|
| lprobs[lprobs != lprobs] = torch.tensor(-math.inf, device=lprobs.device) |
|
|
| lprobs[:, self.pad] = -math.inf |
| lprobs[:, self.unk] -= self.unk_penalty |
|
|
| |
| if step >= max_len: |
| lprobs[:, : self.eos] = -math.inf |
| lprobs[:, self.eos + 1 :] = -math.inf |
|
|
| |
| if ( |
| prefix_tokens is not None |
| and step < prefix_tokens.size(1) |
| and step < max_len |
| ): |
| lprobs, tokens, scores = self._prefix_tokens( |
| step, lprobs, scores, tokens, prefix_tokens, beam_size |
| ) |
| else: |
| if step < self.min_len: |
| |
| lprobs[:, self.eos] = -math.inf |
|
|
| if self.token_indices_to_suppress is not None: |
| lprobs[:, self.token_indices_to_suppress] = -math.inf |
|
|
| |
| if avg_attn_scores is not None: |
| if attn is None: |
| attn = torch.empty( |
| bsz * beam_size, |
| avg_attn_scores.size(1), |
| max_len + 2, |
| device=scores.device, |
| ) |
| attn[:, :, step + 1].copy_(avg_attn_scores) |
|
|
| scores = scores.type_as(lprobs) |
| eos_bbsz_idx = torch.empty(0, device=tokens.device).type_as( |
| tokens |
| ) |
| eos_scores = torch.empty(0, device=scores.device).type_as( |
| scores |
| ) |
|
|
| if self.should_set_src_lengths: |
| self.search.set_src_lengths(src_lengths) |
|
|
| if self.repeat_ngram_blocker is not None: |
| lprobs = self.repeat_ngram_blocker(tokens, lprobs, bsz, beam_size, step) |
|
|
| |
| cand_scores, cand_indices, cand_beams = self.search.step( |
| step, |
| lprobs.view(bsz, -1, self.vocab_size), |
| scores.view(bsz, beam_size, -1)[:, :, :step], |
| tokens[:, : step + 1], |
| original_batch_idxs, |
| ) |
|
|
| |
| |
| |
| cand_bbsz_idx = cand_beams.add(bbsz_offsets) |
|
|
| |
| |
| eos_mask = cand_indices.eq(self.eos) & cand_scores.ne(-math.inf) |
| eos_mask[:, :beam_size][cands_to_ignore] = torch.tensor( |
| 0, device=eos_mask.device |
| ).type_as(eos_mask) |
|
|
| |
| |
| |
| eos_bbsz_idx = torch.masked_select( |
| cand_bbsz_idx[:, :beam_size], mask=eos_mask[:, :beam_size] |
| ) |
|
|
| finalized_sents: List[int] = [] |
| if eos_bbsz_idx.numel() > 0: |
| eos_scores = torch.masked_select( |
| cand_scores[:, :beam_size], mask=eos_mask[:, :beam_size] |
| ) |
|
|
| finalized_sents = self.finalize_hypos( |
| step, |
| eos_bbsz_idx, |
| eos_scores, |
| tokens, |
| scores, |
| finalized, |
| finished, |
| beam_size, |
| attn, |
| src_lengths, |
| max_len, |
| ) |
| num_remaining_sent -= len(finalized_sents) |
|
|
| assert num_remaining_sent >= 0 |
| if num_remaining_sent == 0: |
| break |
| if self.search.stop_on_max_len and step >= max_len: |
| break |
| assert step < max_len, f"{step} < {max_len}" |
|
|
| |
| |
| if len(finalized_sents) > 0: |
| new_bsz = bsz - len(finalized_sents) |
|
|
| |
| batch_mask = torch.ones( |
| bsz, dtype=torch.bool, device=cand_indices.device |
| ) |
| batch_mask[finalized_sents] = False |
| |
| batch_idxs = ( |
| torch.arange(bsz, device=cand_indices.device) |
| .type_as(cand_indices) |
| .masked_select(batch_mask) |
| ) |
|
|
| |
| self.search.prune_sentences(batch_idxs) |
|
|
| eos_mask = eos_mask[batch_idxs] |
| cand_beams = cand_beams[batch_idxs] |
| bbsz_offsets.resize_(new_bsz, 1) |
| cand_bbsz_idx = cand_beams.add(bbsz_offsets) |
| cand_scores = cand_scores[batch_idxs] |
| cand_indices = cand_indices[batch_idxs] |
|
|
| if prefix_tokens is not None: |
| prefix_tokens = prefix_tokens[batch_idxs] |
| src_lengths = src_lengths[batch_idxs] |
| cands_to_ignore = cands_to_ignore[batch_idxs] |
|
|
| scores = scores.view(bsz, -1)[batch_idxs].view(new_bsz * beam_size, -1) |
| tokens = tokens.view(bsz, -1)[batch_idxs].view(new_bsz * beam_size, -1) |
| if attn is not None: |
| attn = attn.view(bsz, -1)[batch_idxs].view( |
| new_bsz * beam_size, attn.size(1), -1 |
| ) |
| bsz = new_bsz |
| else: |
| batch_idxs = None |
|
|
| |
| |
| |
|
|
| |
|
|
| eos_mask[:, :beam_size] = ~((~cands_to_ignore) & (~eos_mask[:, :beam_size])) |
| active_mask = torch.add( |
| eos_mask.type_as(cand_offsets) * cand_size, |
| cand_offsets[: eos_mask.size(1)], |
| ) |
|
|
| |
| |
| |
| |
| |
| new_cands_to_ignore, active_hypos = torch.topk( |
| active_mask, k=beam_size, dim=1, largest=False |
| ) |
|
|
| |
| cands_to_ignore = new_cands_to_ignore.ge(cand_size)[:, :beam_size] |
| |
| assert (~cands_to_ignore).any(dim=1).all() |
|
|
| |
|
|
| |
| |
| active_bbsz_idx = torch.gather(cand_bbsz_idx, dim=1, index=active_hypos) |
| active_scores = torch.gather(cand_scores, dim=1, index=active_hypos) |
|
|
| active_bbsz_idx = active_bbsz_idx.view(-1) |
| active_scores = active_scores.view(-1) |
|
|
| |
|
|
| |
| tokens[:, : step + 1] = torch.index_select( |
| tokens[:, : step + 1], dim=0, index=active_bbsz_idx |
| ) |
| |
| tokens.view(bsz, beam_size, -1)[:, :, step + 1] = torch.gather( |
| cand_indices, dim=1, index=active_hypos |
| ) |
| if step > 0: |
| scores[:, :step] = torch.index_select( |
| scores[:, :step], dim=0, index=active_bbsz_idx |
| ) |
| scores.view(bsz, beam_size, -1)[:, :, step] = torch.gather( |
| cand_scores, dim=1, index=active_hypos |
| ) |
|
|
| |
| self.search.update_constraints(active_hypos) |
|
|
| |
| if attn is not None: |
| attn[:, :, : step + 2] = torch.index_select( |
| attn[:, :, : step + 2], dim=0, index=active_bbsz_idx |
| ) |
|
|
| |
| reorder_state = active_bbsz_idx |
|
|
| |
| for sent in range(len(finalized)): |
| scores = torch.tensor( |
| [float(elem["score"].item()) for elem in finalized[sent]] |
| ) |
| _, sorted_scores_indices = torch.sort(scores, descending=True) |
| finalized[sent] = [finalized[sent][ssi] for ssi in sorted_scores_indices] |
| finalized[sent] = torch.jit.annotate( |
| List[Dict[str, Tensor]], finalized[sent] |
| ) |
| return finalized |
|
|
|
|
| class EnsembleModel(EnsembleModelBase): |
| """A wrapper around an ensemble of models.""" |
|
|
| def __init__(self, models): |
| super().__init__(models) |
|
|
| @torch.jit.export |
| def forward_decoder( |
| self, |
| tokens, |
| encoder_outs: List[Dict[str, List[Tensor]]], |
| incremental_states: List[Dict[str, Dict[str, Optional[Tensor]]]], |
| temperature: float = 1.0, |
| decoder_name="decoder", |
| encoder_outs_aug: List[Dict[str, List[Tensor]]] = None, |
| **kwargs, |
| ): |
| log_probs = [] |
| avg_attn: Optional[Tensor] = None |
| encoder_out: Optional[Dict[str, List[Tensor]]] = None |
| encoder_out_aug: Optional[Dict[str, List[Tensor]]] = None |
| for i, model in enumerate(self.models): |
| if self.has_encoder(): |
| encoder_out = encoder_outs[i] |
| if encoder_outs_aug is not None: |
| encoder_out_aug = encoder_outs_aug[i] |
| |
| if self.has_incremental_states() and incremental_states is not None: |
| if encoder_out_aug is not None: |
| decoder_out = getattr(model, decoder_name).forward( |
| tokens, |
| encoder_out=encoder_out, |
| encoder_out_aug=encoder_out_aug, |
| incremental_state=incremental_states[i], |
| ) |
| else: |
| decoder_out = getattr(model, decoder_name).forward( |
| tokens, |
| encoder_out=encoder_out, |
| incremental_state=incremental_states[i], |
| **kwargs, |
| ) |
| else: |
| if hasattr(model, decoder_name): |
| decoder_out = getattr(model, decoder_name).forward( |
| tokens, encoder_out=encoder_out |
| ) |
| else: |
| decoder_out = model.forward(tokens) |
|
|
| attn: Optional[Tensor] = None |
| decoder_len = len(decoder_out) |
| if decoder_len > 1 and decoder_out[1] is not None: |
| if isinstance(decoder_out[1], Tensor): |
| attn = decoder_out[1] |
| else: |
| attn_holder = decoder_out[1]["attn"] |
| if isinstance(attn_holder, Tensor): |
| attn = attn_holder |
| elif attn_holder is not None: |
| attn = attn_holder[0] |
| if attn is not None: |
| attn = attn[:, -1, :] |
|
|
| decoder_out_tuple = ( |
| decoder_out[0][:, -1:, :].div_(temperature), |
| None if decoder_len <= 1 else decoder_out[1], |
| ) |
| probs = getattr(model, decoder_name).get_normalized_probs( |
| decoder_out_tuple, log_probs=True, sample=None |
| ) |
| probs = probs[:, -1, :] |
| if self.models_size == 1: |
| return probs, attn |
|
|
| log_probs.append(probs) |
| if attn is not None: |
| if avg_attn is None: |
| avg_attn = attn |
| else: |
| avg_attn.add_(attn) |
|
|
| avg_probs = torch.logsumexp(torch.stack(log_probs, dim=0), dim=0) - math.log( |
| self.models_size |
| ) |
|
|
| if avg_attn is not None: |
| avg_attn.div_(self.models_size) |
| return avg_probs, avg_attn |
|
|
| @torch.jit.export |
| def reorder_incremental_state( |
| self, |
| incremental_states: List[Dict[str, Dict[str, Optional[Tensor]]]], |
| new_order, |
| decoder_name="decoder", |
| ): |
| if not self.has_incremental_states(): |
| return |
| for i, model in enumerate(self.models): |
| getattr(model, decoder_name).reorder_incremental_state_scripting( |
| incremental_states[i], new_order |
| ) |
|
|