From 34bb74e3b2379b2d7831dbac3e008af0ab37c733 Mon Sep 17 00:00:00 2001 From: Ahmed Omar <40790298+ahmedo42@users.noreply.github.com> Date: Thu, 1 Sep 2022 11:21:52 +0000 Subject: [PATCH 1/6] implmented 4 reduction ops --- .../frontends/torch/reduction_ops.py | 17 ++ .../test_torch/test_reduction_ops.py | 152 ++++++++++++++++++ 2 files changed, 169 insertions(+) diff --git a/ivy/functional/frontends/torch/reduction_ops.py b/ivy/functional/frontends/torch/reduction_ops.py index e69de29bb2d1d..6b50df18c873f 100644 --- a/ivy/functional/frontends/torch/reduction_ops.py +++ b/ivy/functional/frontends/torch/reduction_ops.py @@ -0,0 +1,17 @@ +import ivy + + +def argmax(input, dim=None, keepdim=False): + return ivy.argmax(input, axis=dim, keepdims=keepdim) + + +def argmin(input, dim=None, keepdim=False): + return ivy.argmin(input, axis=dim, keepdims=keepdim) + + +def amax(input, dim, keepdim=False, *, out=None): + return ivy.max(input, axis=dim, keepdims=keepdim, out=out) + + +def amin(input, dim, keepdim=False, *, out=None): + return ivy.min(input, axis=dim, keepdims=keepdim, out=out) diff --git a/ivy_tests/test_ivy/test_frontends/test_torch/test_reduction_ops.py b/ivy_tests/test_ivy/test_frontends/test_torch/test_reduction_ops.py index e69de29bb2d1d..0c0c7f17f7a56 100644 --- a/ivy_tests/test_ivy/test_frontends/test_torch/test_reduction_ops.py +++ b/ivy_tests/test_ivy/test_frontends/test_torch/test_reduction_ops.py @@ -0,0 +1,152 @@ +# global +from typing import Sequence +import numpy as np +from hypothesis import given, strategies as st + +# local +import ivy_tests.test_ivy.helpers as helpers +from ivy_tests.test_ivy.helpers import handle_cmd_line_args + + +@handle_cmd_line_args +@given( + dtype_and_x=helpers.statistical_dtype_values(function="argmax"), + as_variable=helpers.array_bools(num_arrays=1), + num_positional_args=helpers.num_positional_args( + fn_name="ivy.functional.frontends.torch.argmax" + ), + native_array=helpers.array_bools(num_arrays=1), + keepdims=st.booleans(), +) +def test_torch_argmax( + dtype_and_x, + as_variable, + num_positional_args, + native_array, + keepdims, + fw, +): + input_dtype, x, dim = dtype_and_x + dim = dim[0] if isinstance(dim, Sequence) else dim + helpers.test_frontend_function( + input_dtypes=input_dtype, + as_variable_flags=as_variable, + with_out=False, + num_positional_args=num_positional_args, + native_array_flags=native_array, + fw=fw, + frontend="torch", + fn_tree="argmax", + input=np.asarray(x, dtype=input_dtype), + dim=dim, + keepdim=keepdims, + ) + + +@handle_cmd_line_args +@given( + dtype_and_x=helpers.statistical_dtype_values(function="argmin"), + as_variable=helpers.array_bools(num_arrays=1), + num_positional_args=helpers.num_positional_args( + fn_name="ivy.functional.frontends.torch.argmin" + ), + native_array=helpers.array_bools(num_arrays=1), + keepdims=st.booleans(), +) +def test_torch_argmin( + dtype_and_x, + as_variable, + num_positional_args, + native_array, + keepdims, + fw, +): + input_dtype, x, dim = dtype_and_x + dim = dim[0] if isinstance(dim, Sequence) else dim + helpers.test_frontend_function( + input_dtypes=input_dtype, + as_variable_flags=as_variable, + with_out=False, + num_positional_args=num_positional_args, + native_array_flags=native_array, + fw=fw, + frontend="torch", + fn_tree="argmin", + input=np.asarray(x, dtype=input_dtype), + dim=dim, + keepdim=keepdims, + ) + + +@handle_cmd_line_args +@given( + dtype_and_x=helpers.statistical_dtype_values(function="amax"), + as_variable=helpers.array_bools(num_arrays=1), + num_positional_args=helpers.num_positional_args( + fn_name="ivy.functional.frontends.torch.amax" + ), + native_array=helpers.array_bools(num_arrays=1), + keepdims=st.booleans(), + with_out=st.booleans(), +) +def test_torch_amax( + dtype_and_x, + as_variable, + num_positional_args, + native_array, + keepdims, + with_out, + fw, +): + input_dtype, x, dim = dtype_and_x + helpers.test_frontend_function( + input_dtypes=input_dtype, + as_variable_flags=as_variable, + with_out=with_out, + num_positional_args=num_positional_args, + native_array_flags=native_array, + fw=fw, + frontend="torch", + fn_tree="amax", + input=np.asarray(x, dtype=input_dtype), + dim=dim, + keepdim=keepdims, + out=None, + ) + + +@handle_cmd_line_args +@given( + dtype_and_x=helpers.statistical_dtype_values(function="amin"), + as_variable=helpers.array_bools(num_arrays=1), + num_positional_args=helpers.num_positional_args( + fn_name="ivy.functional.frontends.torch.amin" + ), + native_array=helpers.array_bools(num_arrays=1), + keepdims=st.booleans(), + with_out=st.booleans(), +) +def test_torch_amin( + dtype_and_x, + as_variable, + num_positional_args, + native_array, + keepdims, + with_out, + fw, +): + input_dtype, x, dim = dtype_and_x + helpers.test_frontend_function( + input_dtypes=input_dtype, + as_variable_flags=as_variable, + with_out=with_out, + num_positional_args=num_positional_args, + native_array_flags=native_array, + fw=fw, + frontend="torch", + fn_tree="amin", + input=np.asarray(x, dtype=input_dtype), + dim=dim, + keepdim=keepdims, + out=None, + ) From 20a6c0d5d2fb4edbda708658685cec8bdb263d57 Mon Sep 17 00:00:00 2001 From: Ahmed Omar <40790298+ahmedo42@users.noreply.github.com> Date: Mon, 5 Sep 2022 11:42:45 +0000 Subject: [PATCH 2/6] added more functions, tests --- .../frontends/torch/reduction_ops.py | 83 ++++ .../test_torch/test_reduction_ops.py | 374 ++++++++++++++++-- 2 files changed, 430 insertions(+), 27 deletions(-) diff --git a/ivy/functional/frontends/torch/reduction_ops.py b/ivy/functional/frontends/torch/reduction_ops.py index 6b50df18c873f..3a3d4d4d54fcb 100644 --- a/ivy/functional/frontends/torch/reduction_ops.py +++ b/ivy/functional/frontends/torch/reduction_ops.py @@ -1,4 +1,5 @@ import ivy +from collections import namedtuple def argmax(input, dim=None, keepdim=False): @@ -15,3 +16,85 @@ def amax(input, dim, keepdim=False, *, out=None): def amin(input, dim, keepdim=False, *, out=None): return ivy.min(input, axis=dim, keepdims=keepdim, out=out) + + +def aminmax(input, *, dim=None, keepdim=False, out=None): + out_arrays = out is not None + if out_arrays: + if not isinstance(out_arrays, tuple): + raise TypeError("out must be a tuple") + if len(out) != 2: + raise ValueError("out must be a tuple of length 2") + + min_array = out[0] if out_arrays else None + max_array = out[1] if out_arrays else None + tuple_key = "aminmax_out" if out_arrays else "aminmax" + + min_out = ivy.min(input, axis=dim, keepdims=keepdim, out=min_array) + + max_out = ivy.max(input, axis=dim, keepdims=keepdim, out=max_array) + + ret = namedtuple(tuple_key, ["min", "max"])(min_out, max_out) + + return ret + + +aminmax.unsupported_dtypes = ("float16",) + + +def all(input, dim=None, keepdim=False, *, out=None): + input_dtype = ivy.as_ivy_dtype(input.dtype) + ret = ivy.all(input, axis=dim, keepdims=keepdim, out=out) + if input_dtype == ivy.uint8: + ret = ivy.astype(ret, ivy.uint8, out=out) + return ret + + +def any(input, dim=None, keepdim=False, *, out=None): + input_dtype = ivy.as_ivy_dtype(input.dtype) + ret = ivy.any(input, axis=dim, keepdims=keepdim, out=out) + if input_dtype == ivy.uint8: + ret = ivy.astype(ret, ivy.uint8, out=out) + return ret + + +def max(input, dim=None, keepdim=False, *, out=None): + out_arrays = out is not None + if out_arrays: + if not isinstance(out_arrays, tuple): + raise TypeError("out must be a tuple") + if len(out) != 2: + raise ValueError("out must be a tuple of length 2") + + values_array = out[0] if out_arrays else None + indices_array = out[1] if out_arrays else None + tuple_key = "max_out" if out is not None else "max" + + values = ivy.max(input, axis=dim, keepdims=keepdim, out=values_array) + indices = ivy.argmax(input, axis=dim, keepdims=keepdim, out=indices_array) + + ret = namedtuple(tuple_key, ["values", "indices"])(values, indices) + + return ret + + +def min(input, dim=None, keepdim=False, *, out=None): + out_arrays = out is not None + if out_arrays: + if not isinstance(out_arrays, tuple): + raise TypeError("out must be a tuple") + if len(out) != 2: + raise ValueError("out must be a tuple of length 2") + + values_array = out[0] if out_arrays else None + indices_array = out[1] if out_arrays else None + tuple_key = "min_out" if out is not None else "min" + + values = ivy.min(input, axis=dim, keepdims=keepdim, out=values_array) + indices = ivy.argmin(input, axis=dim, keepdims=keepdim, out=indices_array) + + tuple_key = "min_out" if out is not None else "min" + + ret = namedtuple(tuple_key, ["values", "indices"])(values, indices) + + return ret diff --git a/ivy_tests/test_ivy/test_frontends/test_torch/test_reduction_ops.py b/ivy_tests/test_ivy/test_frontends/test_torch/test_reduction_ops.py index 0c0c7f17f7a56..dd5975d3ddb35 100644 --- a/ivy_tests/test_ivy/test_frontends/test_torch/test_reduction_ops.py +++ b/ivy_tests/test_ivy/test_frontends/test_torch/test_reduction_ops.py @@ -1,33 +1,39 @@ # global -from typing import Sequence import numpy as np from hypothesis import given, strategies as st # local import ivy_tests.test_ivy.helpers as helpers from ivy_tests.test_ivy.helpers import handle_cmd_line_args +import ivy.functional.backends.numpy as ivy_np +import ivy.functional.backends.torch as ivy_torch @handle_cmd_line_args @given( - dtype_and_x=helpers.statistical_dtype_values(function="argmax"), - as_variable=helpers.array_bools(num_arrays=1), + dtype_input_axis=helpers.dtype_values_axis( + available_dtypes=tuple( + set(ivy_np.valid_numeric_dtypes).intersection( + set(ivy_torch.valid_numeric_dtypes) + ), + ), + min_axis=-1, + max_axis=0, + ), num_positional_args=helpers.num_positional_args( fn_name="ivy.functional.frontends.torch.argmax" ), - native_array=helpers.array_bools(num_arrays=1), keepdims=st.booleans(), ) def test_torch_argmax( - dtype_and_x, + dtype_input_axis, as_variable, num_positional_args, native_array, keepdims, fw, ): - input_dtype, x, dim = dtype_and_x - dim = dim[0] if isinstance(dim, Sequence) else dim + input_dtype, x, axis = dtype_input_axis helpers.test_frontend_function( input_dtypes=input_dtype, as_variable_flags=as_variable, @@ -38,31 +44,36 @@ def test_torch_argmax( frontend="torch", fn_tree="argmax", input=np.asarray(x, dtype=input_dtype), - dim=dim, + dim=axis, keepdim=keepdims, ) @handle_cmd_line_args @given( - dtype_and_x=helpers.statistical_dtype_values(function="argmin"), - as_variable=helpers.array_bools(num_arrays=1), + dtype_input_axis=helpers.dtype_values_axis( + available_dtypes=tuple( + set(ivy_np.valid_numeric_dtypes).intersection( + set(ivy_torch.valid_numeric_dtypes) + ), + ), + min_axis=-1, + max_axis=0, + ), num_positional_args=helpers.num_positional_args( fn_name="ivy.functional.frontends.torch.argmin" ), - native_array=helpers.array_bools(num_arrays=1), keepdims=st.booleans(), ) def test_torch_argmin( - dtype_and_x, + dtype_input_axis, as_variable, num_positional_args, native_array, keepdims, fw, ): - input_dtype, x, dim = dtype_and_x - dim = dim[0] if isinstance(dim, Sequence) else dim + input_dtype, x, axis = dtype_input_axis helpers.test_frontend_function( input_dtypes=input_dtype, as_variable_flags=as_variable, @@ -73,24 +84,30 @@ def test_torch_argmin( frontend="torch", fn_tree="argmin", input=np.asarray(x, dtype=input_dtype), - dim=dim, + dim=axis, keepdim=keepdims, ) @handle_cmd_line_args @given( - dtype_and_x=helpers.statistical_dtype_values(function="amax"), - as_variable=helpers.array_bools(num_arrays=1), + dtype_input_axis=helpers.dtype_values_axis( + available_dtypes=tuple( + set(ivy_np.valid_numeric_dtypes).intersection( + set(ivy_torch.valid_numeric_dtypes) + ), + ), + min_axis=-1, + max_axis=0, + ), num_positional_args=helpers.num_positional_args( fn_name="ivy.functional.frontends.torch.amax" ), - native_array=helpers.array_bools(num_arrays=1), keepdims=st.booleans(), with_out=st.booleans(), ) def test_torch_amax( - dtype_and_x, + dtype_input_axis, as_variable, num_positional_args, native_array, @@ -98,7 +115,7 @@ def test_torch_amax( with_out, fw, ): - input_dtype, x, dim = dtype_and_x + input_dtype, x, axis = dtype_input_axis helpers.test_frontend_function( input_dtypes=input_dtype, as_variable_flags=as_variable, @@ -109,7 +126,7 @@ def test_torch_amax( frontend="torch", fn_tree="amax", input=np.asarray(x, dtype=input_dtype), - dim=dim, + dim=axis, keepdim=keepdims, out=None, ) @@ -117,17 +134,23 @@ def test_torch_amax( @handle_cmd_line_args @given( - dtype_and_x=helpers.statistical_dtype_values(function="amin"), - as_variable=helpers.array_bools(num_arrays=1), + dtype_input_axis=helpers.dtype_values_axis( + available_dtypes=tuple( + set(ivy_np.valid_numeric_dtypes).intersection( + set(ivy_torch.valid_numeric_dtypes) + ), + ), + min_axis=-1, + max_axis=0, + ), num_positional_args=helpers.num_positional_args( fn_name="ivy.functional.frontends.torch.amin" ), - native_array=helpers.array_bools(num_arrays=1), keepdims=st.booleans(), with_out=st.booleans(), ) def test_torch_amin( - dtype_and_x, + dtype_input_axis, as_variable, num_positional_args, native_array, @@ -135,7 +158,7 @@ def test_torch_amin( with_out, fw, ): - input_dtype, x, dim = dtype_and_x + input_dtype, x, axis = dtype_input_axis helpers.test_frontend_function( input_dtypes=input_dtype, as_variable_flags=as_variable, @@ -146,7 +169,304 @@ def test_torch_amin( frontend="torch", fn_tree="amin", input=np.asarray(x, dtype=input_dtype), - dim=dim, + dim=axis, + keepdim=keepdims, + out=None, + ) + + +@handle_cmd_line_args +@given( + dtypes_arrays=helpers.dtype_and_values( + available_dtypes=tuple( + set(ivy_np.valid_numeric_dtypes).intersection( + set(ivy_torch.valid_numeric_dtypes) + ), + ), + shape=st.shared(helpers.get_shape(min_num_dims=1), key="shape"), + num_arrays=3, + shared_dtype=True, + ), + num_positional_args=helpers.num_positional_args( + fn_name="ivy.functional.frontends.torch.aminmax" + ), + keepdims=st.booleans(), + with_out=st.booleans(), + axis=helpers.get_axis( + shape=st.shared(helpers.get_shape(min_num_dims=1), key="shape"), + min_size=1, + max_size=1, + force_int=True, + ), +) +def test_torch_aminmax( + dtypes_arrays, + as_variable, + num_positional_args, + native_array, + keepdims, + with_out, + axis, + fw, +): + input_dtype, x = dtypes_arrays + out = None + if with_out: + if fw == "numpy" and len(x[0]) == 1: + out = tuple( + [ + np.asarray(x[1][0], dtype=input_dtype[0]), + np.asarray(x[2][0], dtype=input_dtype[0]), + ] + ) + else: + out = tuple( + [ + np.asarray(x[1], dtype=input_dtype[0]), + np.asarray(x[2], dtype=input_dtype[0]), + ] + ) + + input_dtypes = input_dtype[0] if not with_out else input_dtype + helpers.test_frontend_function( + input_dtypes=input_dtypes, + as_variable_flags=as_variable, + with_out=with_out, + num_positional_args=num_positional_args, + native_array_flags=native_array, + fw=fw, + frontend="torch", + fn_tree="aminmax", + input=np.asarray(x[0], dtype=input_dtype[0]), + dim=axis, + keepdim=keepdims, + out=out, + ) + + +@handle_cmd_line_args +@given( + dtype_input_axis=helpers.dtype_values_axis( + available_dtypes=tuple( + set(ivy_np.valid_numeric_dtypes).intersection( + set(ivy_torch.valid_numeric_dtypes) + ), + ), + min_axis=-1, + max_axis=0, + min_num_dims=1, + allow_inf=False, + ), + num_positional_args=helpers.num_positional_args( + fn_name="ivy.functional.frontends.torch.all" + ), + keepdims=st.booleans(), + with_out=st.booleans(), +) +def test_torch_all( + dtype_input_axis, + as_variable, + num_positional_args, + native_array, + keepdims, + with_out, + fw, +): + input_dtype, x, axis = dtype_input_axis + helpers.test_frontend_function( + input_dtypes=input_dtype, + as_variable_flags=as_variable, + with_out=with_out, + num_positional_args=num_positional_args, + native_array_flags=native_array, + fw=fw, + frontend="torch", + fn_tree="all", + input=np.asarray(x, dtype=input_dtype), + dim=axis, + keepdim=keepdims, + out=None, + ) + + +@handle_cmd_line_args +@given( + dtype_input_axis=helpers.dtype_values_axis( + available_dtypes=tuple( + set(ivy_np.valid_numeric_dtypes).intersection( + set(ivy_torch.valid_numeric_dtypes) + ), + ), + min_axis=-1, + max_axis=0, + min_num_dims=1, + allow_inf=False, + ), + num_positional_args=helpers.num_positional_args( + fn_name="ivy.functional.frontends.torch.any" + ), + keepdims=st.booleans(), + with_out=st.booleans(), +) +def test_torch_any( + dtype_input_axis, + as_variable, + num_positional_args, + native_array, + keepdims, + with_out, + fw, +): + input_dtype, x, axis = dtype_input_axis + helpers.test_frontend_function( + input_dtypes=input_dtype, + as_variable_flags=as_variable, + with_out=with_out, + num_positional_args=num_positional_args, + native_array_flags=native_array, + fw=fw, + frontend="torch", + fn_tree="any", + input=np.asarray(x, dtype=input_dtype), + dim=axis, keepdim=keepdims, out=None, ) + + +@handle_cmd_line_args +@given( + dtypes_arrays=helpers.dtype_and_values( + available_dtypes=tuple( + set(ivy_np.valid_numeric_dtypes).intersection( + set(ivy_torch.valid_numeric_dtypes) + ), + ), + shape=st.shared(helpers.get_shape(min_num_dims=1), key="shape"), + num_arrays=3, + shared_dtype=True, + ), + num_positional_args=helpers.num_positional_args( + fn_name="ivy.functional.frontends.torch.max" + ), + keepdims=st.booleans(), + with_out=st.booleans(), + axis=helpers.get_axis( + shape=st.shared(helpers.get_shape(min_num_dims=1), key="shape"), + min_size=1, + max_size=1, + force_int=True, + ), +) +def test_torch_max( + dtypes_arrays, + as_variable, + num_positional_args, + native_array, + keepdims, + with_out, + axis, + fw, +): + input_dtype, x = dtypes_arrays + out = None + if with_out: + if fw == "numpy" and len(x[0]) == 1: + out = tuple( + [ + np.asarray(x[1][0], dtype=input_dtype[0]), + np.asarray(x[2][0], dtype=input_dtype[0]), + ] + ) + else: + out = tuple( + [ + np.asarray(x[1], dtype=input_dtype[0]), + np.asarray(x[2], dtype=input_dtype[0]), + ] + ) + + input_dtypes = input_dtype[0] if not with_out else input_dtype + helpers.test_frontend_function( + input_dtypes=input_dtypes, + as_variable_flags=as_variable, + with_out=with_out, + num_positional_args=num_positional_args, + native_array_flags=native_array, + fw=fw, + frontend="torch", + fn_tree="max", + input=np.asarray(x[0], dtype=input_dtype[0]), + dim=axis, + keepdim=keepdims, + out=out, + ) + + +@handle_cmd_line_args +@given( + dtypes_arrays=helpers.dtype_and_values( + available_dtypes=tuple( + set(ivy_np.valid_numeric_dtypes).intersection( + set(ivy_torch.valid_numeric_dtypes) + ), + ), + shape=st.shared(helpers.get_shape(min_num_dims=1), key="shape"), + num_arrays=3, + shared_dtype=True, + ), + num_positional_args=helpers.num_positional_args( + fn_name="ivy.functional.frontends.torch.min" + ), + keepdims=st.booleans(), + with_out=st.booleans(), + axis=helpers.get_axis( + shape=st.shared(helpers.get_shape(min_num_dims=1), key="shape"), + min_size=1, + max_size=1, + force_int=True, + ), +) +def test_torch_min( + dtypes_arrays, + as_variable, + num_positional_args, + native_array, + keepdims, + with_out, + axis, + fw, +): + input_dtype, x = dtypes_arrays + out = None + if with_out: + if fw == "numpy" and len(x[0]) == 1: + out = tuple( + [ + np.asarray(x[1][0], dtype=input_dtype[0]), + np.asarray(x[2][0], dtype=input_dtype[0]), + ] + ) + else: + out = tuple( + [ + np.asarray(x[1], dtype=input_dtype[0]), + np.asarray(x[2], dtype=input_dtype[0]), + ] + ) + + input_dtypes = input_dtype[0] if not with_out else input_dtype + helpers.test_frontend_function( + input_dtypes=input_dtypes, + as_variable_flags=as_variable, + with_out=with_out, + num_positional_args=num_positional_args, + native_array_flags=native_array, + fw=fw, + frontend="torch", + fn_tree="min", + input=np.asarray(x[0], dtype=input_dtype[0]), + dim=axis, + keepdim=keepdims, + out=out, + ) From 3a91c0598d22ee0f296aef7dba32b20e4e463fdb Mon Sep 17 00:00:00 2001 From: Ahmed Omar <40790298+ahmedo42@users.noreply.github.com> Date: Fri, 9 Sep 2022 13:38:54 +0000 Subject: [PATCH 3/6] removed functions and tests,will do another PR --- .../frontends/torch/reduction_ops.py | 75 +------ .../test_torch/test_reduction_ops.py | 207 ------------------ 2 files changed, 4 insertions(+), 278 deletions(-) diff --git a/ivy/functional/frontends/torch/reduction_ops.py b/ivy/functional/frontends/torch/reduction_ops.py index 3a3d4d4d54fcb..f32e711771589 100644 --- a/ivy/functional/frontends/torch/reduction_ops.py +++ b/ivy/functional/frontends/torch/reduction_ops.py @@ -1,5 +1,4 @@ import ivy -from collections import namedtuple def argmax(input, dim=None, keepdim=False): @@ -18,83 +17,17 @@ def amin(input, dim, keepdim=False, *, out=None): return ivy.min(input, axis=dim, keepdims=keepdim, out=out) -def aminmax(input, *, dim=None, keepdim=False, out=None): - out_arrays = out is not None - if out_arrays: - if not isinstance(out_arrays, tuple): - raise TypeError("out must be a tuple") - if len(out) != 2: - raise ValueError("out must be a tuple of length 2") - - min_array = out[0] if out_arrays else None - max_array = out[1] if out_arrays else None - tuple_key = "aminmax_out" if out_arrays else "aminmax" - - min_out = ivy.min(input, axis=dim, keepdims=keepdim, out=min_array) - - max_out = ivy.max(input, axis=dim, keepdims=keepdim, out=max_array) - - ret = namedtuple(tuple_key, ["min", "max"])(min_out, max_out) - - return ret - - -aminmax.unsupported_dtypes = ("float16",) - - def all(input, dim=None, keepdim=False, *, out=None): input_dtype = ivy.as_ivy_dtype(input.dtype) ret = ivy.all(input, axis=dim, keepdims=keepdim, out=out) - if input_dtype == ivy.uint8: - ret = ivy.astype(ret, ivy.uint8, out=out) + if ivy.is_uint_dtype(input_dtype): + ret = ivy.astype(ret, input_dtype, out=out) return ret def any(input, dim=None, keepdim=False, *, out=None): input_dtype = ivy.as_ivy_dtype(input.dtype) ret = ivy.any(input, axis=dim, keepdims=keepdim, out=out) - if input_dtype == ivy.uint8: - ret = ivy.astype(ret, ivy.uint8, out=out) - return ret - - -def max(input, dim=None, keepdim=False, *, out=None): - out_arrays = out is not None - if out_arrays: - if not isinstance(out_arrays, tuple): - raise TypeError("out must be a tuple") - if len(out) != 2: - raise ValueError("out must be a tuple of length 2") - - values_array = out[0] if out_arrays else None - indices_array = out[1] if out_arrays else None - tuple_key = "max_out" if out is not None else "max" - - values = ivy.max(input, axis=dim, keepdims=keepdim, out=values_array) - indices = ivy.argmax(input, axis=dim, keepdims=keepdim, out=indices_array) - - ret = namedtuple(tuple_key, ["values", "indices"])(values, indices) - - return ret - - -def min(input, dim=None, keepdim=False, *, out=None): - out_arrays = out is not None - if out_arrays: - if not isinstance(out_arrays, tuple): - raise TypeError("out must be a tuple") - if len(out) != 2: - raise ValueError("out must be a tuple of length 2") - - values_array = out[0] if out_arrays else None - indices_array = out[1] if out_arrays else None - tuple_key = "min_out" if out is not None else "min" - - values = ivy.min(input, axis=dim, keepdims=keepdim, out=values_array) - indices = ivy.argmin(input, axis=dim, keepdims=keepdim, out=indices_array) - - tuple_key = "min_out" if out is not None else "min" - - ret = namedtuple(tuple_key, ["values", "indices"])(values, indices) - + if ivy.is_uint_dtype(input_dtype): + ret = ivy.astype(ret, input_dtype, out=out) return ret diff --git a/ivy_tests/test_ivy/test_frontends/test_torch/test_reduction_ops.py b/ivy_tests/test_ivy/test_frontends/test_torch/test_reduction_ops.py index dd5975d3ddb35..99068aa4bc82d 100644 --- a/ivy_tests/test_ivy/test_frontends/test_torch/test_reduction_ops.py +++ b/ivy_tests/test_ivy/test_frontends/test_torch/test_reduction_ops.py @@ -175,75 +175,6 @@ def test_torch_amin( ) -@handle_cmd_line_args -@given( - dtypes_arrays=helpers.dtype_and_values( - available_dtypes=tuple( - set(ivy_np.valid_numeric_dtypes).intersection( - set(ivy_torch.valid_numeric_dtypes) - ), - ), - shape=st.shared(helpers.get_shape(min_num_dims=1), key="shape"), - num_arrays=3, - shared_dtype=True, - ), - num_positional_args=helpers.num_positional_args( - fn_name="ivy.functional.frontends.torch.aminmax" - ), - keepdims=st.booleans(), - with_out=st.booleans(), - axis=helpers.get_axis( - shape=st.shared(helpers.get_shape(min_num_dims=1), key="shape"), - min_size=1, - max_size=1, - force_int=True, - ), -) -def test_torch_aminmax( - dtypes_arrays, - as_variable, - num_positional_args, - native_array, - keepdims, - with_out, - axis, - fw, -): - input_dtype, x = dtypes_arrays - out = None - if with_out: - if fw == "numpy" and len(x[0]) == 1: - out = tuple( - [ - np.asarray(x[1][0], dtype=input_dtype[0]), - np.asarray(x[2][0], dtype=input_dtype[0]), - ] - ) - else: - out = tuple( - [ - np.asarray(x[1], dtype=input_dtype[0]), - np.asarray(x[2], dtype=input_dtype[0]), - ] - ) - - input_dtypes = input_dtype[0] if not with_out else input_dtype - helpers.test_frontend_function( - input_dtypes=input_dtypes, - as_variable_flags=as_variable, - with_out=with_out, - num_positional_args=num_positional_args, - native_array_flags=native_array, - fw=fw, - frontend="torch", - fn_tree="aminmax", - input=np.asarray(x[0], dtype=input_dtype[0]), - dim=axis, - keepdim=keepdims, - out=out, - ) - - @handle_cmd_line_args @given( dtype_input_axis=helpers.dtype_values_axis( @@ -332,141 +263,3 @@ def test_torch_any( keepdim=keepdims, out=None, ) - - -@handle_cmd_line_args -@given( - dtypes_arrays=helpers.dtype_and_values( - available_dtypes=tuple( - set(ivy_np.valid_numeric_dtypes).intersection( - set(ivy_torch.valid_numeric_dtypes) - ), - ), - shape=st.shared(helpers.get_shape(min_num_dims=1), key="shape"), - num_arrays=3, - shared_dtype=True, - ), - num_positional_args=helpers.num_positional_args( - fn_name="ivy.functional.frontends.torch.max" - ), - keepdims=st.booleans(), - with_out=st.booleans(), - axis=helpers.get_axis( - shape=st.shared(helpers.get_shape(min_num_dims=1), key="shape"), - min_size=1, - max_size=1, - force_int=True, - ), -) -def test_torch_max( - dtypes_arrays, - as_variable, - num_positional_args, - native_array, - keepdims, - with_out, - axis, - fw, -): - input_dtype, x = dtypes_arrays - out = None - if with_out: - if fw == "numpy" and len(x[0]) == 1: - out = tuple( - [ - np.asarray(x[1][0], dtype=input_dtype[0]), - np.asarray(x[2][0], dtype=input_dtype[0]), - ] - ) - else: - out = tuple( - [ - np.asarray(x[1], dtype=input_dtype[0]), - np.asarray(x[2], dtype=input_dtype[0]), - ] - ) - - input_dtypes = input_dtype[0] if not with_out else input_dtype - helpers.test_frontend_function( - input_dtypes=input_dtypes, - as_variable_flags=as_variable, - with_out=with_out, - num_positional_args=num_positional_args, - native_array_flags=native_array, - fw=fw, - frontend="torch", - fn_tree="max", - input=np.asarray(x[0], dtype=input_dtype[0]), - dim=axis, - keepdim=keepdims, - out=out, - ) - - -@handle_cmd_line_args -@given( - dtypes_arrays=helpers.dtype_and_values( - available_dtypes=tuple( - set(ivy_np.valid_numeric_dtypes).intersection( - set(ivy_torch.valid_numeric_dtypes) - ), - ), - shape=st.shared(helpers.get_shape(min_num_dims=1), key="shape"), - num_arrays=3, - shared_dtype=True, - ), - num_positional_args=helpers.num_positional_args( - fn_name="ivy.functional.frontends.torch.min" - ), - keepdims=st.booleans(), - with_out=st.booleans(), - axis=helpers.get_axis( - shape=st.shared(helpers.get_shape(min_num_dims=1), key="shape"), - min_size=1, - max_size=1, - force_int=True, - ), -) -def test_torch_min( - dtypes_arrays, - as_variable, - num_positional_args, - native_array, - keepdims, - with_out, - axis, - fw, -): - input_dtype, x = dtypes_arrays - out = None - if with_out: - if fw == "numpy" and len(x[0]) == 1: - out = tuple( - [ - np.asarray(x[1][0], dtype=input_dtype[0]), - np.asarray(x[2][0], dtype=input_dtype[0]), - ] - ) - else: - out = tuple( - [ - np.asarray(x[1], dtype=input_dtype[0]), - np.asarray(x[2], dtype=input_dtype[0]), - ] - ) - - input_dtypes = input_dtype[0] if not with_out else input_dtype - helpers.test_frontend_function( - input_dtypes=input_dtypes, - as_variable_flags=as_variable, - with_out=with_out, - num_positional_args=num_positional_args, - native_array_flags=native_array, - fw=fw, - frontend="torch", - fn_tree="min", - input=np.asarray(x[0], dtype=input_dtype[0]), - dim=axis, - keepdim=keepdims, - out=out, - ) From 3c79314eab7b73a491f3292e4bbf7c724372a698 Mon Sep 17 00:00:00 2001 From: Ahmed Omar <40790298+ahmedo42@users.noreply.github.com> Date: Fri, 9 Sep 2022 20:48:02 +0000 Subject: [PATCH 4/6] remove None --- ivy/functional/frontends/torch/reduction_ops.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ivy/functional/frontends/torch/reduction_ops.py b/ivy/functional/frontends/torch/reduction_ops.py index f32e711771589..50bb15f3cdbb5 100644 --- a/ivy/functional/frontends/torch/reduction_ops.py +++ b/ivy/functional/frontends/torch/reduction_ops.py @@ -1,11 +1,11 @@ import ivy -def argmax(input, dim=None, keepdim=False): +def argmax(input, dim, keepdim=False): return ivy.argmax(input, axis=dim, keepdims=keepdim) -def argmin(input, dim=None, keepdim=False): +def argmin(input, dim, keepdim=False): return ivy.argmin(input, axis=dim, keepdims=keepdim) From 9fdc6f32276f1bbb517017796b4e4aac62e99d25 Mon Sep 17 00:00:00 2001 From: Ahmed Omar <40790298+ahmedo42@users.noreply.github.com> Date: Sat, 10 Sep 2022 23:17:10 +0200 Subject: [PATCH 5/6] revert change --- ivy/functional/frontends/torch/reduction_ops.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ivy/functional/frontends/torch/reduction_ops.py b/ivy/functional/frontends/torch/reduction_ops.py index 50bb15f3cdbb5..f32e711771589 100644 --- a/ivy/functional/frontends/torch/reduction_ops.py +++ b/ivy/functional/frontends/torch/reduction_ops.py @@ -1,11 +1,11 @@ import ivy -def argmax(input, dim, keepdim=False): +def argmax(input, dim=None, keepdim=False): return ivy.argmax(input, axis=dim, keepdims=keepdim) -def argmin(input, dim, keepdim=False): +def argmin(input, dim=None, keepdim=False): return ivy.argmin(input, axis=dim, keepdims=keepdim) From 4db748ac37c20fb9c61fda73e490fb8fd0fbb4a5 Mon Sep 17 00:00:00 2001 From: Ahmed Omar <40790298+ahmedo42@users.noreply.github.com> Date: Mon, 12 Sep 2022 07:38:08 +0000 Subject: [PATCH 6/6] refactoring tests and adding None to amax,amin --- .../frontends/torch/reduction_ops.py | 4 +- .../test_torch/test_reduction_ops.py | 48 +++++-------------- 2 files changed, 14 insertions(+), 38 deletions(-) diff --git a/ivy/functional/frontends/torch/reduction_ops.py b/ivy/functional/frontends/torch/reduction_ops.py index f32e711771589..eb700ee099942 100644 --- a/ivy/functional/frontends/torch/reduction_ops.py +++ b/ivy/functional/frontends/torch/reduction_ops.py @@ -9,11 +9,11 @@ def argmin(input, dim=None, keepdim=False): return ivy.argmin(input, axis=dim, keepdims=keepdim) -def amax(input, dim, keepdim=False, *, out=None): +def amax(input, dim=None, keepdim=False, *, out=None): return ivy.max(input, axis=dim, keepdims=keepdim, out=out) -def amin(input, dim, keepdim=False, *, out=None): +def amin(input, dim=None, keepdim=False, *, out=None): return ivy.min(input, axis=dim, keepdims=keepdim, out=out) diff --git a/ivy_tests/test_ivy/test_frontends/test_torch/test_reduction_ops.py b/ivy_tests/test_ivy/test_frontends/test_torch/test_reduction_ops.py index 99068aa4bc82d..2ac3786d4acf1 100644 --- a/ivy_tests/test_ivy/test_frontends/test_torch/test_reduction_ops.py +++ b/ivy_tests/test_ivy/test_frontends/test_torch/test_reduction_ops.py @@ -5,18 +5,14 @@ # local import ivy_tests.test_ivy.helpers as helpers from ivy_tests.test_ivy.helpers import handle_cmd_line_args -import ivy.functional.backends.numpy as ivy_np -import ivy.functional.backends.torch as ivy_torch @handle_cmd_line_args @given( dtype_input_axis=helpers.dtype_values_axis( - available_dtypes=tuple( - set(ivy_np.valid_numeric_dtypes).intersection( - set(ivy_torch.valid_numeric_dtypes) - ), - ), + available_dtypes=helpers.get_dtypes("numeric"), + force_int_axis=True, + min_num_dims=1, min_axis=-1, max_axis=0, ), @@ -52,11 +48,9 @@ def test_torch_argmax( @handle_cmd_line_args @given( dtype_input_axis=helpers.dtype_values_axis( - available_dtypes=tuple( - set(ivy_np.valid_numeric_dtypes).intersection( - set(ivy_torch.valid_numeric_dtypes) - ), - ), + available_dtypes=helpers.get_dtypes("numeric"), + force_int_axis=True, + min_num_dims=1, min_axis=-1, max_axis=0, ), @@ -92,11 +86,8 @@ def test_torch_argmin( @handle_cmd_line_args @given( dtype_input_axis=helpers.dtype_values_axis( - available_dtypes=tuple( - set(ivy_np.valid_numeric_dtypes).intersection( - set(ivy_torch.valid_numeric_dtypes) - ), - ), + available_dtypes=helpers.get_dtypes("numeric"), + min_num_dims=1, min_axis=-1, max_axis=0, ), @@ -104,7 +95,6 @@ def test_torch_argmin( fn_name="ivy.functional.frontends.torch.amax" ), keepdims=st.booleans(), - with_out=st.booleans(), ) def test_torch_amax( dtype_input_axis, @@ -135,11 +125,8 @@ def test_torch_amax( @handle_cmd_line_args @given( dtype_input_axis=helpers.dtype_values_axis( - available_dtypes=tuple( - set(ivy_np.valid_numeric_dtypes).intersection( - set(ivy_torch.valid_numeric_dtypes) - ), - ), + available_dtypes=helpers.get_dtypes("numeric"), + min_num_dims=1, min_axis=-1, max_axis=0, ), @@ -147,7 +134,6 @@ def test_torch_amax( fn_name="ivy.functional.frontends.torch.amin" ), keepdims=st.booleans(), - with_out=st.booleans(), ) def test_torch_amin( dtype_input_axis, @@ -178,11 +164,7 @@ def test_torch_amin( @handle_cmd_line_args @given( dtype_input_axis=helpers.dtype_values_axis( - available_dtypes=tuple( - set(ivy_np.valid_numeric_dtypes).intersection( - set(ivy_torch.valid_numeric_dtypes) - ), - ), + available_dtypes=helpers.get_dtypes("valid"), min_axis=-1, max_axis=0, min_num_dims=1, @@ -192,7 +174,6 @@ def test_torch_amin( fn_name="ivy.functional.frontends.torch.all" ), keepdims=st.booleans(), - with_out=st.booleans(), ) def test_torch_all( dtype_input_axis, @@ -223,11 +204,7 @@ def test_torch_all( @handle_cmd_line_args @given( dtype_input_axis=helpers.dtype_values_axis( - available_dtypes=tuple( - set(ivy_np.valid_numeric_dtypes).intersection( - set(ivy_torch.valid_numeric_dtypes) - ), - ), + available_dtypes=helpers.get_dtypes("valid"), min_axis=-1, max_axis=0, min_num_dims=1, @@ -237,7 +214,6 @@ def test_torch_all( fn_name="ivy.functional.frontends.torch.any" ), keepdims=st.booleans(), - with_out=st.booleans(), ) def test_torch_any( dtype_input_axis,