Skip to content

torch.histogram test cases have out-of-order sequence as bins input #130916

@majing921201

Description

@majing921201

Issue description

torch.histogram test cases use random tensor as bins parameter input, which is not make sense. It should be an increasing sequence

Code example

https://github.com/pytorch/pytorch/blob/f6838d521a243dbedc50ae96575720bf2cc8a8ad/torch/testing/_internal/common_methods_invocations.py#L3127,

def sample_inputs_histogram(op_info, device, dtype, requires_grad, **kwargs):
    make_arg = partial(make_tensor, dtype=dtype, device=device, requires_grad=requires_grad)

    sizes = ((), (S,), (S, S), (S, S, S), (S, 1, S), (S, 0, S))

    for size, bin_ct, weighted, density in product(sizes, range(1, 5), [False, True], [False, True]):
        input_tensor = make_arg(size)
        weight_tensor = make_arg(size) if weighted else None

        yield SampleInput(input_tensor, bin_ct,
                          weight=weight_tensor, density=density)

        bins_tensor = make_arg((bin_ct + 1,))
        yield SampleInput(input_tensor, bins_tensor,
                          weight=weight_tensor, density=density)

here bins_tensor = make_arg((bin_ct + 1,)), bins_tensor is randomly generated and out-of-order.

For example, we could get such test case:
input=6.362441917740258
bins = tensor([ 6.3624, -3.1650, 7.6229, 4.4501]

result is:
torch.return_types.histogram(
hist=tensor([0., 0., 0.], dtype=torch.float64),
bin_edges=tensor([ 6.3624, -3.1650, 7.6229, 4.4501], dtype=torch.float64))

I think it's not make sense that torch.histogram input out-of-order bins.
1, The doc https://pytorch.org/docs/stable/generated/torch.histogram.html#torch-histogram mentions that "If bins is a 1D tensor, it specifies the sequence of bin edges including the rightmost edge. It should contain at least 2 elements and its elements should be increasing."
2, histogram operator implemented with std::upper_bound for binary search, see

pos = std::upper_bound(bin_seq[dim], bin_seq[dim] + num_bin_edges[dim], elt)
. std::upper_bound function requires increasing sequence for binary search.

cc @albanD

Metadata

Metadata

Assignees

No one assigned

    Labels

    actionablemodule: python frontendFor issues relating to PyTorch's Python frontendtriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions