def METHOD_NAME(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