def make_positions(tensor, padding_idx: int, onnx_trace: bool = False, max_pos=None, external_qkv=False):
    """Replace non-padding symbols with their position numbers.

    Position numbers begin at padding_idx+1. Padding symbols are ignored.
    """
    # The series of casts and type-conversions here are carefully
    # balanced to both work with ONNX export and XLA. In particular XLA
    # prefers ints, cumsum defaults to output longs, and ONNX doesn't know
    # how to handle the dtype kwarg in cumsum.

    assert max_pos is not None
    mask = tensor.ne(padding_idx).int()

    if external_qkv == False and max_pos > 0:
        # left padding 
        pos = torch.cumsum(mask, dim=1).long()
        max_pos_cur = pos.max(-1)[0]
        pos += (max_pos - max_pos_cur + 1).unsqueeze(-1)
    else:
        mask = tensor.ne(padding_idx).int()
        pos = torch.cumsum(mask, dim=1) + max_pos
    pos = (pos.type_as(mask) * mask).long() + padding_idx
    pos[:,0] = padding_idx + 1
    return pos
Replace non-padding symbols with their position numbers.

    Position numbers begin at padding_idx+1. Padding symbols are ignored.
    
understand_icl/struprompting/models/gpt.py