Skip to content

MAINT: work around arg{min,max} not implemented for booleans #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions torch_np/_detail/_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,22 @@ def count_nonzero(a, axis=None):

def argmax(tensor, axis=None):
axis = _util.allow_only_single_axis(axis)

if tensor.dtype == torch.bool:
# RuntimeError: "argmax_cpu" not implemented for 'Bool'
tensor = tensor.to(torch.uint8)

tensor = torch.argmax(tensor, axis)
return tensor


def argmin(tensor, axis=None):
axis = _util.allow_only_single_axis(axis)

if tensor.dtype == torch.bool:
# RuntimeError: "argmin_cpu" not implemented for 'Bool'
tensor = tensor.to(torch.uint8)

tensor = torch.argmin(tensor, axis)
return tensor

Expand Down
12 changes: 4 additions & 8 deletions torch_np/tests/test_ndarray_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,8 @@ def test_combinations(self, data):
# with suppress_warnings() as sup:
# sup.filter(RuntimeWarning,
# "invalid value encountered in reduce")
if np.asarray(arr).dtype.kind in "cb":
pytest.xfail(
reason="'max_values_cpu' not implemented for 'ComplexDouble', 'Bool'"
)
if np.asarray(arr).dtype.kind in "c":
pytest.xfail(reason="'max_values_cpu' not implemented for 'ComplexDouble'")

val = np.max(arr)

Expand Down Expand Up @@ -508,10 +506,8 @@ class TestArgmin:
def test_combinations(self, data):
arr, pos = data

if np.asarray(arr).dtype.kind in "cb":
pytest.xfail(
reason="'min_values_cpu' not implemented for 'ComplexDouble', 'Bool'"
)
if np.asarray(arr).dtype.kind in "c":
pytest.xfail(reason="'min_values_cpu' not implemented for 'ComplexDouble'")

# with suppress_warnings() as sup:
# sup.filter(RuntimeWarning, "invalid value encountered in reduce")
Expand Down