From df0138c79cd31b8b6509f95abbed735c7d134442 Mon Sep 17 00:00:00 2001 From: Novendobi Date: Sun, 3 Sep 2023 12:59:33 -0400 Subject: [PATCH 1/2] random_uniform --- .../frontends/numpy/random/functions.py | 16 ++-- ivy/functional/frontends/paddle/fft.py | 45 +++++----- .../frontends/paddle/tensor/creation.py | 9 ++ .../frontends/torch/comparison_ops.py | 2 +- .../test_ivy/helpers/available_frameworks.py | 2 +- .../test_numpy/test_random/test_functions.py | 90 +++++++++---------- .../test_paddle/test_tensor/test_creation.py | 34 ++++++- 7 files changed, 118 insertions(+), 80 deletions(-) diff --git a/ivy/functional/frontends/numpy/random/functions.py b/ivy/functional/frontends/numpy/random/functions.py index d66fbd41ae475..6d3c1e1365e19 100644 --- a/ivy/functional/frontends/numpy/random/functions.py +++ b/ivy/functional/frontends/numpy/random/functions.py @@ -87,6 +87,14 @@ def gumbel(loc=0.0, scale=1.0, size=None): return x +@to_ivy_arrays_and_back +@from_zero_dim_arrays_to_scalar +def laplace(loc=0.0, scale=1.0, size=None): + u = ivy.random_uniform(low=0.0, high=0.0, shape=size, dtype="float64") + u = loc - scale * ivy.sign(u - 0.5) * ivy.log(1 - 2 * ivy.abs(u - 0.5)) + return u + + @to_ivy_arrays_and_back @from_zero_dim_arrays_to_scalar def logistic(loc=0.0, scale=1.0, size=None): @@ -266,11 +274,3 @@ def weibull(a, size=None): return 0 u = ivy.random_uniform(low=0.0, high=1.0, shape=size, dtype="float64") return ivy.pow(-ivy.log(1 - u), 1 / a) - - -@to_ivy_arrays_and_back -@from_zero_dim_arrays_to_scalar -def laplace(loc=0.0, scale=1.0, size=None): - u = ivy.random_uniform(low=0.0, high=0.0, shape=size, dtype="float64") - u = loc - scale * ivy.sign(u - 0.5) * ivy.log(1 - 2 * ivy.abs(u - 0.5)) - return u diff --git a/ivy/functional/frontends/paddle/fft.py b/ivy/functional/frontends/paddle/fft.py index 0f14f289bb321..bd40f59060eb0 100644 --- a/ivy/functional/frontends/paddle/fft.py +++ b/ivy/functional/frontends/paddle/fft.py @@ -46,6 +46,28 @@ def fftshift(x, axes=None, name=None): return roll +@with_supported_dtypes( + {"2.5.1 and below": ("complex64", "complex128")}, + "paddle", +) +@to_ivy_arrays_and_back +def hfft(x, n=None, axis=-1, norm="backward", name=None): + """Compute the FFT of a signal that has Hermitian symmetry, resulting in a real + spectrum.""" + # Determine the input shape and axis length + input_shape = x.shape + input_len = input_shape[axis] + + # Calculate n if not provided + if n is None: + n = 2 * (input_len - 1) + + # Perform the FFT along the specified axis + result = ivy.fft(x, axis, n=n, norm=norm) + + return ivy.real(result) + + @with_supported_dtypes( {"2.5.1 and below": ("complex64", "complex128")}, "paddle", @@ -84,28 +106,6 @@ def ifftshift(x, axes=None, name=None): return roll -@with_supported_dtypes( - {"2.5.1 and below": ("complex64", "complex128")}, - "paddle", -) -@to_ivy_arrays_and_back -def hfft(x, n=None, axis=-1, norm="backward", name=None): - """Compute the FFT of a signal that has Hermitian symmetry, resulting in a real - spectrum.""" - # Determine the input shape and axis length - input_shape = x.shape - input_len = input_shape[axis] - - # Calculate n if not provided - if n is None: - n = 2 * (input_len - 1) - - # Perform the FFT along the specified axis - result = ivy.fft(x, axis, n=n, norm=norm) - - return ivy.real(result) - - @with_supported_dtypes( {"2.5.1 and below": ("complex64", "complex128")}, "paddle", @@ -122,4 +122,3 @@ def irfft(x, n=None, axis=-1.0, norm="backward", name=None): if ivy.isreal(x): time_domain = ivy.real(time_domain) return time_domain - diff --git a/ivy/functional/frontends/paddle/tensor/creation.py b/ivy/functional/frontends/paddle/tensor/creation.py index 7ed0e10847fac..5cb513124dda5 100644 --- a/ivy/functional/frontends/paddle/tensor/creation.py +++ b/ivy/functional/frontends/paddle/tensor/creation.py @@ -144,6 +144,15 @@ def ones_like(x, /, *, dtype=None, name=None): return ivy.ones_like(x, dtype=dtype) +# []-22965 +@with_supported_dtypes("paddle") +@to_ivy_arrays_and_back +def random_uniform(low, high, shape, device=None, dtype=None): + return ivy.random_uniform( + low=low, high=high, shape=shape, device=device, dtype=dtype + ) + + @to_ivy_arrays_and_back def to_tensor(data, /, *, dtype=None, place=None, stop_gradient=True): array = ivy.array(data, dtype=dtype, device=place) diff --git a/ivy/functional/frontends/torch/comparison_ops.py b/ivy/functional/frontends/torch/comparison_ops.py index b743b38d135f0..eeaadadfbf864 100644 --- a/ivy/functional/frontends/torch/comparison_ops.py +++ b/ivy/functional/frontends/torch/comparison_ops.py @@ -290,7 +290,7 @@ def topk(input, k, dim=None, largest=True, sorted=True, *, out=None): gt = greater +ne = not_equal ge = greater_equal le = less_equal lt = less -ne = not_equal diff --git a/ivy_tests/test_ivy/helpers/available_frameworks.py b/ivy_tests/test_ivy/helpers/available_frameworks.py index cea6487752a91..8bcd2d793a1b2 100644 --- a/ivy_tests/test_ivy/helpers/available_frameworks.py +++ b/ivy_tests/test_ivy/helpers/available_frameworks.py @@ -4,7 +4,7 @@ # A list of available backends that can be used for testing. -def _available_frameworks(path='/opt/fw/'): +def _available_frameworks(path="/opt/fw/"): ret = [] for backend in ["numpy", "jax", "tensorflow", "torch", "paddle"]: if find_spec(backend) is not None: diff --git a/ivy_tests/test_ivy/test_frontends/test_numpy/test_random/test_functions.py b/ivy_tests/test_ivy/test_frontends/test_numpy/test_random/test_functions.py index 2175c52161c57..5b809c32f807e 100644 --- a/ivy_tests/test_ivy/test_frontends/test_numpy/test_random/test_functions.py +++ b/ivy_tests/test_ivy/test_frontends/test_numpy/test_random/test_functions.py @@ -354,6 +354,51 @@ def test_numpy_gumbel( ) +@handle_frontend_test( + fn_tree="numpy.random.laplace", + input_dtypes=helpers.get_dtypes("float", full=False), + loc=st.floats( + allow_nan=False, + allow_infinity=False, + width=32, + min_value=0, + exclude_min=True, + ), + scale=st.floats( + allow_nan=False, + allow_infinity=False, + width=32, + min_value=0, + exclude_min=True, + ), + size=helpers.get_shape(allow_none=True), + test_with_out=st.just(False), +) +def test_numpy_laplace( + input_dtypes, + size, + frontend, + test_flags, + fn_tree, + on_device, + backend_fw, + loc, + scale, +): + helpers.test_frontend_function( + input_dtypes=input_dtypes, + backend_to_test=backend_fw, + test_flags=test_flags, + frontend=frontend, + fn_tree=fn_tree, + on_device=on_device, + test_values=False, + loc=loc, + scale=scale, + size=size, + ) + + # logistic @handle_frontend_test( fn_tree="numpy.random.logistic", @@ -1056,48 +1101,3 @@ def test_numpy_weibull( a=a, size=size, ) - - -@handle_frontend_test( - fn_tree="numpy.random.laplace", - input_dtypes=helpers.get_dtypes("float", full=False), - loc=st.floats( - allow_nan=False, - allow_infinity=False, - width=32, - min_value=0, - exclude_min=True, - ), - scale=st.floats( - allow_nan=False, - allow_infinity=False, - width=32, - min_value=0, - exclude_min=True, - ), - size=helpers.get_shape(allow_none=True), - test_with_out=st.just(False), -) -def test_numpy_laplace( - input_dtypes, - size, - frontend, - test_flags, - fn_tree, - on_device, - backend_fw, - loc, - scale, -): - helpers.test_frontend_function( - input_dtypes=input_dtypes, - backend_to_test=backend_fw, - test_flags=test_flags, - frontend=frontend, - fn_tree=fn_tree, - on_device=on_device, - test_values=False, - loc=loc, - scale=scale, - size=size, - ) diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_creation.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_creation.py index 805aea49ee07f..9418eab6e132d 100644 --- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_creation.py +++ b/ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_creation.py @@ -559,6 +559,37 @@ def test_paddle_ones_like( ) +# random_uniform +@handle_frontend_test( + fn_tree="paddle.random_uniform", + dtype_and_shape=helpers.dtype_and_values( + available_dtypes=helpers.get_dtypes("valid"), ret_shape=True + ), + test_with_out=st.just(False), +) +def test_paddle_random_uniform( + *, + dtype_and_shape, + on_device, + fn_tree, + backend_fw, + frontend, + test_flags, +): + input_dtype, x, shape = dtype_and_shape + + helpers.test_frontend_function( + input_dtypes=input_dtype, + backend_to_test=backend_fw, + frontend=frontend, + test_flags=test_flags, + fn_tree=fn_tree, + on_device=on_device, + test_values=False, + shape=shape, + ) + + # Tests # # ----- # @@ -767,8 +798,7 @@ def test_paddle_zeros( dtype_and_x=helpers.dtype_and_values( available_dtypes=helpers.get_dtypes("valid"), ), - dtype=helpers.get_dtypes("valid"), - test_with_out=st.just(False), + dtype=helpers.get_dtypes("valid") ) def test_paddle_zeros_like( dtype_and_x, From 679c559da702988671ec1e8184619ac5d62823ac Mon Sep 17 00:00:00 2001 From: Novendobi Date: Mon, 4 Sep 2023 05:52:20 -0400 Subject: [PATCH 2/2] ru precommit error fixed --- .../paddle/experimental/manipulation.py | 8 +++++-- .../tensorflow/experimental/manipulation.py | 6 +++-- .../torch/experimental/manipulation.py | 6 +++-- .../test_paddle/test_tensor/test_creation.py | 2 +- .../test_core/test_manipulation.py | 23 ------------------- 5 files changed, 15 insertions(+), 30 deletions(-) diff --git a/ivy/functional/backends/paddle/experimental/manipulation.py b/ivy/functional/backends/paddle/experimental/manipulation.py index 8c81c2e8dc920..9f65cb19b2ca2 100644 --- a/ivy/functional/backends/paddle/experimental/manipulation.py +++ b/ivy/functional/backends/paddle/experimental/manipulation.py @@ -148,8 +148,12 @@ def pad( ) -pad.partial_mixed_handler = lambda *args, mode="constant", constant_values=0, reflect_type="even", **kwargs: _check_paddle_pad( - mode, reflect_type, args[1], args[0].shape, constant_values, 3 +pad.partial_mixed_handler = ( + lambda *args, mode="constant", constant_values=0, reflect_type="even", **kwargs: ( + _check_paddle_pad( + mode, reflect_type, args[1], args[0].shape, constant_values, 3 + ) + ) ) diff --git a/ivy/functional/backends/tensorflow/experimental/manipulation.py b/ivy/functional/backends/tensorflow/experimental/manipulation.py index 32df062bc26e8..0c421fa5b17d2 100644 --- a/ivy/functional/backends/tensorflow/experimental/manipulation.py +++ b/ivy/functional/backends/tensorflow/experimental/manipulation.py @@ -294,8 +294,10 @@ def pad( ) -pad.partial_mixed_handler = lambda *args, mode="constant", constant_values=0, reflect_type="even", **kwargs: _check_tf_pad( - args[0].shape, args[1], mode, constant_values, reflect_type +pad.partial_mixed_handler = ( + lambda *args, mode="constant", constant_values=0, reflect_type="even", **kwargs: ( + _check_tf_pad(args[0].shape, args[1], mode, constant_values, reflect_type) + ) ) diff --git a/ivy/functional/backends/torch/experimental/manipulation.py b/ivy/functional/backends/torch/experimental/manipulation.py index 74bcd42c0a098..1d7345b505903 100644 --- a/ivy/functional/backends/torch/experimental/manipulation.py +++ b/ivy/functional/backends/torch/experimental/manipulation.py @@ -110,8 +110,10 @@ def pad( ).squeeze(0) -pad.partial_mixed_handler = lambda *args, mode="constant", constant_values=0, reflect_type="even", **kwargs: _check_torch_pad( - mode, reflect_type, args[1], args[0].shape, constant_values +pad.partial_mixed_handler = ( + lambda *args, mode="constant", constant_values=0, reflect_type="even", **kwargs: ( + _check_torch_pad(mode, reflect_type, args[1], args[0].shape, constant_values) + ) ) diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_creation.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_creation.py index 9418eab6e132d..8fdc3b82fcb96 100644 --- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_creation.py +++ b/ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_creation.py @@ -798,7 +798,7 @@ def test_paddle_zeros( dtype_and_x=helpers.dtype_and_values( available_dtypes=helpers.get_dtypes("valid"), ), - dtype=helpers.get_dtypes("valid") + dtype=helpers.get_dtypes("valid"), ) def test_paddle_zeros_like( dtype_and_x, diff --git a/ivy_tests/test_ivy/test_functional/test_core/test_manipulation.py b/ivy_tests/test_ivy/test_functional/test_core/test_manipulation.py index 6ae42c6de5ca6..dc854e98fd595 100644 --- a/ivy_tests/test_ivy/test_functional/test_core/test_manipulation.py +++ b/ivy_tests/test_ivy/test_functional/test_core/test_manipulation.py @@ -658,29 +658,6 @@ def test_stack(*, dtypes_arrays, axis, test_flags, backend_fw, fn_name, on_devic ) -# stack -@handle_test( - fn_tree="functional.ivy.stack", - dtypes_arrays=_stack_helper(), - axis=helpers.get_axis( - shape=st.shared(helpers.get_shape(min_num_dims=1), key="values_shape"), - force_int=True, - ), -) -def test_stack(*, dtypes_arrays, axis, test_flags, backend_fw, fn_name, on_device): - dtypes, arrays = dtypes_arrays - - helpers.test_function( - input_dtypes=dtypes, - test_flags=test_flags, - backend_to_test=backend_fw, - fn_name=fn_name, - on_device=on_device, - arrays=arrays, - axis=axis, - ) - - # swapaxes @handle_test( fn_tree="functional.ivy.swapaxes",