code: def _get_ins_targets(in_tokens, out_tokens, padding_idx, unk_idx):
      libnat, use_cuda = load_libnat()
  
      def _get_ins_targets_cuda(
          in_tokens, out_tokens, padding_idx, unk_idx
      ):
          in_masks = in_tokens.ne(padding_idx)
          out_masks = out_tokens.ne(padding_idx)
          (
              mask_ins_targets,
              masked_tgt_masks,
          ) = libnat.generate_insertion_labels(
              out_tokens.int(),
              libnat.levenshtein_distance(
                  in_tokens.int(),
                  out_tokens.int(),
                  in_masks.sum(1).int(),
                  out_masks.sum(1).int(),
              ),
          )
          masked_tgt_masks = masked_tgt_masks.bool() & out_masks
          mask_ins_targets = mask_ins_targets.type_as(in_tokens)[
              :, 1 : in_masks.size(1)
          ].masked_fill_(~in_masks[:, 1:], 0)
          masked_tgt_tokens = out_tokens.masked_fill(
              masked_tgt_masks, unk_idx
          )
          return masked_tgt_masks, masked_tgt_tokens, mask_ins_targets
  
      def _get_ins_targets_cpu(
          in_tokens, out_tokens, padding_idx, unk_idx
      ):
          in_seq_len, out_seq_len = in_tokens.size(1), out_tokens.size(
              1
          )
  
          in_tokens_list = [
              [t for t in s if t != padding_idx]
              for i, s in enumerate(in_tokens.tolist())
          ]
          out_tokens_list = [
              [t for t in s if t != padding_idx]
              for i, s in enumerate(out_tokens.tolist())
          ]
  
          full_labels = libnat.suggested_ed2_path(
              in_tokens_list, out_tokens_list, padding_idx
          )
          mask_inputs = [
              [len(c) if c[0] != padding_idx else 0 for c in a[:-1]]
              for a in full_labels
          ]
  
          # generate labels
          masked_tgt_masks = []
          for mask_input in mask_inputs:
              mask_label = []
              for beam_size in mask_input[1:-1]:  # HACK 1:-1
                  mask_label += [0] + [1 for _ in range(beam_size)]
              masked_tgt_masks.append(
                  mask_label
                  + [0 for _ in range(out_seq_len - len(mask_label))]
              )
          mask_ins_targets = [
              mask_input[1:-1]
              + [
                  0
                  for _ in range(in_seq_len - 1 - len(mask_input[1:-1]))
              ]
              for mask_input in mask_inputs
          ]
  
          # transform to tensor
          masked_tgt_masks = torch.tensor(
              masked_tgt_masks, device=out_tokens.device
          ).bool()
          mask_ins_targets = torch.tensor(
              mask_ins_targets, device=in_tokens.device
          )
          masked_tgt_tokens = out_tokens.masked_fill(
              masked_tgt_masks, unk_idx
          )
          return masked_tgt_masks, masked_tgt_tokens, mask_ins_targets
  
      if use_cuda:
          return _get_ins_targets_cuda(
              in_tokens, out_tokens, padding_idx, unk_idx
          )
      return _get_ins_targets_cpu(
          in_tokens, out_tokens, padding_idx, unk_idx
      )
  
file_path: fairseq/fairseq/models/nat/levenshtein_utils.py
imports: import torch
  from fairseq.utils import new_arange
label_desc: Use image node_id 2 for calling a global function with example usage: _get_ins_targets(in_tokens, out_tokens, padding_idx, unk_idx) and returns: _get_ins_targets_cpu, masked_tgt_masks,  masked_tgt_tokens,  mask_ins_targets, masked_tgt_masks,  masked_tgt_tokens,  mask_ins_targets, _get_ins_targets_cuda