import torch


def input_length_to_nonpadding(lengths, max_len, dtype=None):
    """ Creates a bias tensor according to the non-padding tensor for cross entropy.

    Args:
        length: A Tensor with shape [batch_size, ], indicating the true length.
        max_len: A scalar tensor indicating the maximum length.

    Returns:
        A float tensor with shape [batch_size, max_len],
        indicating the padding positions, where 0.0 for padding and
        1.0 for non-padding.
    """
    row_vector = torch.arange(0, max_len)
    matrix = torch.unsqueeze(lengths, dim=-1)
    mask = (row_vector < matrix).to(dtype or torch.float)
    return mask