(tensor, padding_idx, left_pad)
return tensor.clone().masked_scatter_(mask, positions[mask])
Replace non-padding symbols with their position numbers.
    Position numbers begin at padding_idx+1.
    Padding symbols are ignored, but it is necessary to specify whether padding
    is added on the left side (left_pad=True) or right side (left_pad=False).
Replace non-padding symbols with their position numbers.
    Position numbers begin at padding_idx+1.
    Padding symbols are ignored, but it is necessary to specify whether padding
    is added on the left side (left_pad=True) or right side (left_pad=False).
Replace non - padding symbols with their position numbers . Position numbers begin at padding_idx + 1 . Padding symbols are ignored but it is necessary to specify whether padding is added on the left side ( left_pad = True ) or right side ( left_pad = False ) .
def make_positions(tensor, padding_idx, left_pad):
    # Copy-pasted from
    # https://github.com/pytorch/fairseq/blob/master/fairseq/utils.py
    """Replace non-padding symbols with their position numbers.
    Position numbers begin at padding_idx+1.
    Padding symbols are ignored, but it is necessary to specify whether padding
    is added on the left side (left_pad=True) or right side (left_pad=False).
    """
    max_pos = padding_idx + 1 + tensor.size(1)
    if not hasattr(make_positions, 'range_buf'):
        make_positions.range_buf = tensor.new()
    make_positions.range_buf = make_positions.range_buf.type_as(tensor)
    if make_positions.range_buf.numel() < max_pos:
        torch.arange(padding_idx + 1, max_pos, out=make_positions.range_buf)
    mask = tensor.ne(padding_idx)
    positions = make_positions.range_buf[:tensor.size(1)].expand_as(tensor)
    if left_pad:
        positions = positions - mask.size(1) + \
                    mask.long().sum(dim=1).unsqueeze(1)
    return tensor.clone().masked_scatter_(mask, positions[mask])
def make_positions ( tensor , padding_idx , left_pad ) : # Copy-pasted from # https://github.com/pytorch/fairseq/blob/master/fairseq/utils.py max_pos = padding_idx + 1 + tensor . size ( 1 ) if not hasattr ( make_positions , 'range_buf' ) : make_positions . range_buf = tensor . new ( ) make_positions . range_buf = make_positions . range_buf . type_as ( tensor ) if make_positions . range_buf . numel ( ) < max_pos : torch . arange ( padding_idx + 1 , max_pos , out = make_positions . range_buf ) mask = tensor . ne ( padding_idx ) positions = make_positions . range_buf [ : tensor . size ( 1 ) ] . expand_as ( tensor ) if left_pad : positions = positions - mask . size ( 1 ) + mask . long ( ) . sum ( dim = 1 ) . unsqueeze ( 1 ) return tensor . clone ( ) . masked_scatter_ ( mask , positions [ mask ] )