Skip to content

Enable applying min, max, argmin, argmax over nonzero dims of tensor containing dims of size 0 #28380

@LeonhardFeiner

Description

@LeonhardFeiner

🚀 Feature

Applying tensor.min(dim), tensor.max(dim), tensor.argmin(dim), tensor.argmax(dim) causes an error when any dimension of the tensor is of size 0.

>>> torch.zeros((2,0)).max(0)
RuntimeError: cannot perform reduction function max on tensor with no elements because the operation does not have an identity

In my opinion this error should only be thrown, if the dim is associated to a dimension which is of size 0.

Motivation

The current behavior is inconsistent with numpy and requires unnecessary case handling for empty tensors.

>>> np.zeros((2,0)).max(0)
array([], dtype=float64)

Pitch

Return empty tensors instead of raising an exception.

>>> torch.zeros((2,0)).max(0)
torch.return_types.max(
values=tensor([]),
indices=tensor([], dtype=torch.int64))

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNot as big of a feature, but technically not a bug. Should be easy to fixmodule: reductionstriagedThis 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