class_docstr: Translation (Sequence Generation) task for Levenshtein Transformer
  See `"Levenshtein Transformer" <https://arxiv.org/abs/1905.11006>`_.
class_docstr_tok: ["Translation","(","Sequence","Generation",")","task","for","Levenshtein","Transformer","See","`","``","Levenshtein","Transformer","''","<","https",":","\/\/arxiv.org\/abs\/1905.11006",">","`","_","."]
code: def inject_noise(self, target_tokens):
      def _random_delete(target_tokens):
          pad = self.tgt_dict.pad()
          bos = self.tgt_dict.bos()
          eos = self.tgt_dict.eos()
  
          max_len = target_tokens.size(1)
          target_mask = target_tokens.eq(pad)
          target_score = target_tokens.clone().float().uniform_()
          target_score.masked_fill_(
              target_tokens.eq(bos) | target_tokens.eq(eos), 0.0
          )
          target_score.masked_fill_(target_mask, 1)
          target_score, target_rank = target_score.sort(1)
          target_length = target_mask.size(1) - target_mask.float().sum(
              1, keepdim=True
          )
  
          # do not delete <bos> and <eos> (we assign 0 score for them)
          target_cutoff = (
              2
              + (
                  (target_length - 2)
                  * target_score.new_zeros(
                      target_score.size(0), 1
                  ).uniform_()
              ).long()
          )
          target_cutoff = target_score.sort(1)[1] >= target_cutoff
  
          prev_target_tokens = (
              target_tokens.gather(1, target_rank)
              .masked_fill_(target_cutoff, pad)
              .gather(
                  1,
                  target_rank.masked_fill_(target_cutoff, max_len).sort(
                      1
                  )[1],
              )
          )
          prev_target_tokens = prev_target_tokens[
              :, : prev_target_tokens.ne(pad).sum(1).max()
          ]
  
          return prev_target_tokens
  
      def _random_mask(target_tokens):
          pad = self.tgt_dict.pad()
          bos = self.tgt_dict.bos()
          eos = self.tgt_dict.eos()
          unk = self.tgt_dict.unk()
  
          target_masks = (
              target_tokens.ne(pad)
              & target_tokens.ne(bos)
              & target_tokens.ne(eos)
          )
          target_score = target_tokens.clone().float().uniform_()
          target_score.masked_fill_(~target_masks, 2.0)
          target_length = target_masks.sum(1).float()
          target_length = (
              target_length * target_length.clone().uniform_()
          )
          target_length = (
              target_length + 1
          )  # make sure to mask at least one token.
  
          _, target_rank = target_score.sort(1)
          target_cutoff = (
              new_arange(target_rank) < target_length[:, None].long()
          )
          prev_target_tokens = target_tokens.masked_fill(
              target_cutoff.scatter(1, target_rank, target_cutoff), unk
          )
          return prev_target_tokens
  
      def _full_mask(target_tokens):
          pad = self.tgt_dict.pad()
          bos = self.tgt_dict.bos()
          eos = self.tgt_dict.eos()
          unk = self.tgt_dict.unk()
  
          target_mask = (
              target_tokens.eq(bos)
              | target_tokens.eq(eos)
              | target_tokens.eq(pad)
          )
          return target_tokens.masked_fill(~target_mask, unk)
  
      if self.cfg.noise == "random_delete":
          return _random_delete(target_tokens)
      elif self.cfg.noise == "random_mask":
          return _random_mask(target_tokens)
      elif self.cfg.noise == "full_mask":
          return _full_mask(target_tokens)
      elif self.cfg.noise == "no_noise":
          return target_tokens
      else:
          raise NotImplementedError
  
file_path: fairseq/fairseq/tasks/translation_lev.py
imports: from dataclasses import dataclass, field
  import torch
  from fairseq import utils
  from fairseq.data import LanguagePairDataset
  from fairseq.dataclass import ChoiceEnum
  from fairseq.tasks import register_task
  from fairseq.tasks.translation import TranslationConfig, TranslationTask, load_langpair_dataset
  from fairseq.utils import new_arange
label_desc: Use image node_id 2 for calling the TranslationLevenshteinTask obj's underlying member method code with example usage: obj.inject_noise(target_tokens) and returns: prev_target_tokens, prev_target_tokens, target_tokens, _random_delete, _random_mask, _full_mask, target_tokens