ivy
ivy copied to clipboard
added uniform_ function in paddle frontend
closes #17788
I have tested this implementation using the following code
import ivy
import ivy.functional.frontends.paddle as ivy_paddle
ivy.set_paddle_backend()
print("backend: ",ivy.backend)
data = ivy_paddle.arange(start=0, end=10, step=1, dtype=ivy_paddle.float32, name=None)
print("data: ",data)
print("data shape: ",data.shape)
print("data dtype: ",data.dtype)
y=ivy_paddle.uniform(shape=data.shape, dtype=None, min=-1.0, max=1.0, seed=42, name=None)
x=ivy_paddle.uniform_(data, min=-1.0, max=1.0, seed=42, name=None)
print("Data after process : ",data)
print("uniform_ result: ",x)
print("uniform result",y)
which gives correct output:
backend: paddle
data: ivy.frontends.paddle.Tensor([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
data shape: ivy.Shape(10)
data dtype: float32
Data after process : ivy.frontends.paddle.Tensor([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
uniform_ result: ivy.frontends.paddle.Tensor([ 0.69932854, 0.65509927, 0.36617076, 0.9909662 , -0.59841871,
-0.68482411, 0.68400383, 0.96803832, -0.62446624, 0.74022818])
uniform result ivy.frontends.paddle.Tensor([ 0.69932854, 0.65509927, 0.36617076, 0.9909662 , -0.59841871,
-0.68482411, 0.68400383, 0.96803832, -0.62446624, 0.74022818])
However when I am trying to implement testing and have written the following test function:
@handle_frontend_test(
fn_tree="paddle.uniform_",
min=st.floats(allow_nan=False, allow_infinity=False, width=32),
max=st.floats(allow_nan=False, allow_infinity=False, width=32),
seed=st.integers(min_value=2, max_value=5),
dtype_and_x=helpers.dtype_and_values(
available_dtypes=helpers.get_dtypes("float"),
min_value=0,
max_value=1000,
min_num_dims=1,
max_num_dims=1,
min_dim_size=2,
max_dim_size=2,
),
)
def test_paddle_uniform_(
frontend,
test_flags,
fn_tree,
min,
max,
seed,
dtype_and_x,
):
dtype, x = dtype_and_x
helpers.test_frontend_function(
input_dtypes=dtype,
frontend=frontend,
test_flags=test_flags,
fn_tree=fn_tree,
test_values=False,
x=x[0],
min=min,
max=max,
seed=seed,
)
However I keep getting "Key Error" indicating that the testing framework is unable to locate "uniform_" function. @soma2000-lang can you kindly guide me in this regard.
Testing logs are below:
test_paddle_random.py::test_paddle_uniform_[cpu-ivy.functional.backends.jax-False-False]
test_paddle_random.py::test_paddle_uniform_[cpu-ivy.functional.backends.tensorflow-False-False]
test_paddle_random.py::test_paddle_uniform_[cpu-ivy.functional.backends.torch-False-False]
test_paddle_random.py::test_paddle_uniform_[cpu-ivy.functional.backends.paddle-False-False]
======================== 5 failed, 2 warnings in 5.40s =========================
FAILED [ 20%]
ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_paddle_random.py:129 (test_paddle_uniform_[cpu-ivy.functional.backends.numpy-False-False])
frontend = 'paddle'
@handle_frontend_test(
> fn_tree="paddle.uniform_",
min=st.floats(allow_nan=False, allow_infinity=False, width=32),
max=st.floats(allow_nan=False, allow_infinity=False, width=32),
seed=st.integers(min_value=2, max_value=5),
dtype_and_x=helpers.dtype_and_values(
available_dtypes=helpers.get_dtypes("float"),
min_value=0,
max_value=1000,
min_num_dims=1,
max_num_dims=1,
min_dim_size=2,
max_dim_size=2,
),
)
test_paddle_random.py:131:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
test_paddle_random.py:161: in test_paddle_uniform_
helpers.test_frontend_function(
../../../helpers/function_testing.py:788: in test_frontend_function
raise e
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
input_dtypes = ['float64']
test_flags = num_positional_args=0. with_out=False. inplace=False. native_arrays=[False]. as_variable=[False]. generate_frontend_arrays=False.
on_device = 'cpu', frontend = 'paddle'
fn_tree = 'ivy.functional.frontends.paddle.uniform_', rtol = None, atol = 1e-06
test_values = False
def test_frontend_function(
*,
input_dtypes: Union[ivy.Dtype, List[ivy.Dtype]],
test_flags: pf.frontend_function_flags,
on_device="cpu",
frontend: str,
fn_tree: str,
rtol: float = None,
atol: float = 1e-06,
test_values: bool = True,
**all_as_kwargs_np,
):
"""
Test a frontend function for the current backend by comparing the result with the
function in the associated framework.
Parameters
----------
input_dtypes
data types of the input arguments in order.
all_aliases
a list of strings containing all aliases for that function
in the current frontend with their full namespaces.
frontend
current frontend (framework).
fn_tree
Path to function in frontend framework namespace.
rtol
relative tolerance value.
atol
absolute tolerance value.
test_values
if True, test for the correctness of the resulting values.
all_as_kwargs_np
input arguments to the function as keyword arguments.
Returns
-------
ret
optional, return value from the function
ret_np
optional, return value from the Numpy function
"""
assert (
not test_flags.with_out or not test_flags.inplace
), "only one of with_out or with_inplace can be set as True"
# split the arguments into their positional and keyword components
args_np, kwargs_np = kwargs_to_args_n_kwargs(
num_positional_args=test_flags.num_positional_args, kwargs=all_as_kwargs_np
)
create_frontend_array = importlib.import_module(
f"ivy.functional.frontends.{frontend}"
)._frontend_array
# extract all arrays from the arguments and keyword arguments
arg_np_vals, args_idxs, c_arg_vals = _get_nested_np_arrays(args_np)
kwarg_np_vals, kwargs_idxs, c_kwarg_vals = _get_nested_np_arrays(kwargs_np)
# make all lists equal in length
num_arrays = c_arg_vals + c_kwarg_vals
if len(input_dtypes) < num_arrays:
input_dtypes = [input_dtypes[0] for _ in range(num_arrays)]
if len(test_flags.as_variable) < num_arrays:
test_flags.as_variable = [test_flags.as_variable[0] for _ in range(num_arrays)]
if len(test_flags.native_arrays) < num_arrays:
test_flags.native_arrays = [
test_flags.native_arrays[0] for _ in range(num_arrays)
]
# update var flags to be compatible with float dtype and with_out args
test_flags.as_variable = [
v if ivy.is_float_dtype(d) and not test_flags.with_out else False
for v, d in zip(test_flags.as_variable, input_dtypes)
]
# frontend function
# parse function name and frontend submodules (jax.lax, jax.numpy etc.)
split_index = fn_tree.rfind(".")
frontend_submods, fn_name = fn_tree[:split_index], fn_tree[split_index + 1 :]
function_module = importlib.import_module(frontend_submods)
frontend_fn = getattr(function_module, fn_name)
# apply test flags etc.
args, kwargs = create_args_kwargs(
args_np=args_np,
arg_np_vals=arg_np_vals,
args_idxs=args_idxs,
kwargs_np=kwargs_np,
kwarg_np_vals=kwarg_np_vals,
kwargs_idxs=kwargs_idxs,
input_dtypes=input_dtypes,
test_flags=test_flags,
on_device=on_device,
)
if test_flags.generate_frontend_arrays:
args_for_test, kwargs_for_test = args_to_frontend(
*args, frontend_array_fn=create_frontend_array, **kwargs
)
else:
args_for_test, kwargs_for_test = ivy.args_to_ivy(*args, **kwargs)
# check and replace NativeClass object in arguments with ivy counterparts
from ivy_tests.test_ivy.test_frontends.test_numpy import convnumpy
convs = {"numpy": convnumpy}
if "torch" in available_frameworks:
from ivy_tests.test_ivy.test_frontends.test_torch import convtorch
convs["torch"] = convtorch
if "tensorflow" in available_frameworks:
from ivy_tests.test_ivy.test_frontends.test_tensorflow import convtensor
convs["tensorflow"] = convtensor
if "jax" in available_frameworks:
from ivy_tests.test_ivy.test_frontends.test_jax import convjax
convs["jax"] = convjax
if frontend in convs:
conv = convs[frontend]
args = ivy.nested_map(args, fn=conv, include_derived=True)
kwargs = ivy.nested_map(kwargs, fn=conv, include_derived=True)
# Make copy for arguments for functions that might use
# inplace update by default
copy_kwargs = copy.deepcopy(kwargs_for_test)
copy_args = copy.deepcopy(args_for_test)
# strip the decorator to get an Ivy array
# ToDo, fix testing for jax frontend for x32
if frontend == "jax":
importlib.import_module("ivy.functional.frontends.jax").config.update(
"jax_enable_x64", True
)
_as_ivy_arrays = not test_flags.generate_frontend_arrays
ret = get_frontend_ret(
frontend_fn, *args_for_test, as_ivy_arrays=_as_ivy_arrays, **kwargs_for_test
)
if test_flags.with_out:
if not inspect.isclass(ret):
is_ret_tuple = issubclass(ret.__class__, tuple)
else:
is_ret_tuple = issubclass(ret, tuple)
if test_flags.generate_frontend_arrays:
if is_ret_tuple:
ret = ivy.nested_map(
ret,
lambda _x: (
arrays_to_frontend(create_frontend_array)(_x)
if not _is_frontend_array(_x)
else _x
),
include_derived=True,
)
elif not _is_frontend_array(ret):
ret = arrays_to_frontend(create_frontend_array)(ret)
else:
if is_ret_tuple:
ret = ivy.nested_map(
ret,
lambda _x: ivy.array(_x) if not ivy.is_array(_x) else _x,
include_derived=True,
)
elif not ivy.is_array(ret):
ret = ivy.array(ret)
out = ret
# pass return value to out argument
# check if passed reference is correctly updated
kwargs["out"] = out
if is_ret_tuple:
if test_flags.generate_frontend_arrays:
flatten_ret = flatten_frontend(
ret=ret, frontend_array_fn=create_frontend_array
)
flatten_out = flatten_frontend(
ret=out, frontend_array_fn=create_frontend_array
)
else:
flatten_ret = flatten(ret=ret)
flatten_out = flatten(ret=out)
for ret_array, out_array in zip(flatten_ret, flatten_out):
if ivy.native_inplace_support and not any(
(ivy.isscalar(ret), ivy.isscalar(out))
):
if test_flags.generate_frontend_arrays:
assert ret_array.ivy_array.data is out_array.ivy_array.data
else:
assert ret_array.data is out_array.data
assert ret_array is out_array
else:
if ivy.native_inplace_support and not any(
(ivy.isscalar(ret), ivy.isscalar(out))
):
if test_flags.generate_frontend_arrays:
assert ret.ivy_array.data is out.ivy_array.data
else:
assert ret.data is out.data
assert ret is out
elif test_flags.inplace:
assert not isinstance(ret, tuple)
if test_flags.generate_frontend_arrays:
assert _is_frontend_array(ret)
else:
assert ivy.is_array(ret)
if test_flags.generate_frontend_arrays:
array_fn = _is_frontend_array
else:
array_fn = ivy.is_array
if "inplace" in list(inspect.signature(frontend_fn).parameters.keys()):
# the function provides optional inplace update
# set inplace update to be True and check
# if returned reference is inputted reference
# and if inputted reference's content is correctly updated
copy_kwargs["inplace"] = True
copy_kwargs["as_ivy_arrays"] = False
first_array = ivy.func_wrapper._get_first_array(
*copy_args, array_fn=array_fn, **copy_kwargs
)
ret_ = get_frontend_ret(frontend_fn, *copy_args, **copy_kwargs)
assert first_array is ret_
else:
# the function provides inplace update by default
# check if returned reference is inputted reference
copy_kwargs["as_ivy_arrays"] = False
first_array = ivy.func_wrapper._get_first_array(
*args, array_fn=array_fn, **kwargs
)
ret_ = get_frontend_ret(frontend_fn, *args, **kwargs)
assert first_array is ret_
# create NumPy args
def arrays_to_numpy(x):
if test_flags.generate_frontend_arrays:
return ivy.to_numpy(x.ivy_array) if _is_frontend_array(x) else x
return ivy.to_numpy(x._data) if isinstance(x, ivy.Array) else x
args_np = ivy.nested_map(
args_for_test,
arrays_to_numpy,
shallow=False,
)
kwargs_np = ivy.nested_map(
kwargs_for_test,
arrays_to_numpy,
shallow=False,
)
# temporarily set frontend framework as backend
ivy.set_backend(frontend)
try:
# create frontend framework args
args_frontend = ivy.nested_map(
args_np,
lambda x: (
ivy.native_array(x)
if isinstance(x, np.ndarray)
else ivy.as_native_dtype(x) if isinstance(x, ivy.Dtype) else x
),
shallow=False,
)
kwargs_frontend = ivy.nested_map(
kwargs_np,
lambda x: ivy.native_array(x) if isinstance(x, np.ndarray) else x,
shallow=False,
)
# change ivy dtypes to native dtypes
if "dtype" in kwargs_frontend:
kwargs_frontend["dtype"] = ivy.as_native_dtype(kwargs_frontend["dtype"])
# change ivy device to native devices
if "device" in kwargs_frontend:
kwargs_frontend["device"] = ivy.as_native_dev(kwargs_frontend["device"])
# check and replace the NativeClass objects in arguments
# with true counterparts
args_frontend = ivy.nested_map(
args_frontend, fn=convtrue, include_derived=True, max_depth=10
)
kwargs_frontend = ivy.nested_map(
kwargs_frontend, fn=convtrue, include_derived=True, max_depth=10
)
# wrap the frontend function objects in arguments to return native arrays
args_frontend = ivy.nested_map(
args_frontend, fn=wrap_frontend_function_args, max_depth=10
)
kwargs_frontend = ivy.nested_map(
kwargs_frontend, fn=wrap_frontend_function_args, max_depth=10
)
# compute the return via the frontend framework
module_name = fn_tree[25 : fn_tree.rfind(".")]
frontend_fw = importlib.import_module(module_name)
> frontend_ret = frontend_fw.__dict__[fn_name](*args_frontend, **kwargs_frontend)
E KeyError: 'uniform_'
E Falsifying example: test_paddle_uniform_(
E frontend='paddle',
E min=0.0,
E max=0.0,
E seed=2,
E dtype_and_x=(['float64'], [array([0., 0.])]),
E fn_tree='ivy.functional.frontends.paddle.uniform_',
E test_flags=FrontendFunctionTestFlags(
E num_positional_args=0,
E with_out=False,
E inplace=False,
E as_variable=[False],
E native_arrays=[False],
E generate_frontend_arrays=False,
E ),
E )
E
E You can reproduce this example by temporarily adding @reproduce_failure('6.78.2', b'AXicY2AgAwAAADUAAQ==') as a decorator on your test case
../../../helpers/function_testing.py:773: KeyError
FAILED [ 40%]
ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_paddle_random.py:129 (test_paddle_uniform_[cpu-ivy.functional.backends.jax-False-False])
frontend = 'paddle'
@handle_frontend_test(
> fn_tree="paddle.uniform_",
min=st.floats(allow_nan=False, allow_infinity=False, width=32),
max=st.floats(allow_nan=False, allow_infinity=False, width=32),
seed=st.integers(min_value=2, max_value=5),
dtype_and_x=helpers.dtype_and_values(
available_dtypes=helpers.get_dtypes("float"),
min_value=0,
max_value=1000,
min_num_dims=1,
max_num_dims=1,
min_dim_size=2,
max_dim_size=2,
),
)
test_paddle_random.py:131:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
test_paddle_random.py:161: in test_paddle_uniform_
helpers.test_frontend_function(
../../../helpers/function_testing.py:788: in test_frontend_function
raise e
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
input_dtypes = ['float64']
test_flags = num_positional_args=0. with_out=False. inplace=False. native_arrays=[False]. as_variable=[False]. generate_frontend_arrays=False.
on_device = 'cpu', frontend = 'paddle'
fn_tree = 'ivy.functional.frontends.paddle.uniform_', rtol = None, atol = 1e-06
test_values = False
def test_frontend_function(
*,
input_dtypes: Union[ivy.Dtype, List[ivy.Dtype]],
test_flags: pf.frontend_function_flags,
on_device="cpu",
frontend: str,
fn_tree: str,
rtol: float = None,
atol: float = 1e-06,
test_values: bool = True,
**all_as_kwargs_np,
):
"""
Test a frontend function for the current backend by comparing the result with the
function in the associated framework.
Parameters
----------
input_dtypes
data types of the input arguments in order.
all_aliases
a list of strings containing all aliases for that function
in the current frontend with their full namespaces.
frontend
current frontend (framework).
fn_tree
Path to function in frontend framework namespace.
rtol
relative tolerance value.
atol
absolute tolerance value.
test_values
if True, test for the correctness of the resulting values.
all_as_kwargs_np
input arguments to the function as keyword arguments.
Returns
-------
ret
optional, return value from the function
ret_np
optional, return value from the Numpy function
"""
assert (
not test_flags.with_out or not test_flags.inplace
), "only one of with_out or with_inplace can be set as True"
# split the arguments into their positional and keyword components
args_np, kwargs_np = kwargs_to_args_n_kwargs(
num_positional_args=test_flags.num_positional_args, kwargs=all_as_kwargs_np
)
create_frontend_array = importlib.import_module(
f"ivy.functional.frontends.{frontend}"
)._frontend_array
# extract all arrays from the arguments and keyword arguments
arg_np_vals, args_idxs, c_arg_vals = _get_nested_np_arrays(args_np)
kwarg_np_vals, kwargs_idxs, c_kwarg_vals = _get_nested_np_arrays(kwargs_np)
# make all lists equal in length
num_arrays = c_arg_vals + c_kwarg_vals
if len(input_dtypes) < num_arrays:
input_dtypes = [input_dtypes[0] for _ in range(num_arrays)]
if len(test_flags.as_variable) < num_arrays:
test_flags.as_variable = [test_flags.as_variable[0] for _ in range(num_arrays)]
if len(test_flags.native_arrays) < num_arrays:
test_flags.native_arrays = [
test_flags.native_arrays[0] for _ in range(num_arrays)
]
# update var flags to be compatible with float dtype and with_out args
test_flags.as_variable = [
v if ivy.is_float_dtype(d) and not test_flags.with_out else False
for v, d in zip(test_flags.as_variable, input_dtypes)
]
# frontend function
# parse function name and frontend submodules (jax.lax, jax.numpy etc.)
split_index = fn_tree.rfind(".")
frontend_submods, fn_name = fn_tree[:split_index], fn_tree[split_index + 1 :]
function_module = importlib.import_module(frontend_submods)
frontend_fn = getattr(function_module, fn_name)
# apply test flags etc.
args, kwargs = create_args_kwargs(
args_np=args_np,
arg_np_vals=arg_np_vals,
args_idxs=args_idxs,
kwargs_np=kwargs_np,
kwarg_np_vals=kwarg_np_vals,
kwargs_idxs=kwargs_idxs,
input_dtypes=input_dtypes,
test_flags=test_flags,
on_device=on_device,
)
if test_flags.generate_frontend_arrays:
args_for_test, kwargs_for_test = args_to_frontend(
*args, frontend_array_fn=create_frontend_array, **kwargs
)
else:
args_for_test, kwargs_for_test = ivy.args_to_ivy(*args, **kwargs)
# check and replace NativeClass object in arguments with ivy counterparts
from ivy_tests.test_ivy.test_frontends.test_numpy import convnumpy
convs = {"numpy": convnumpy}
if "torch" in available_frameworks:
from ivy_tests.test_ivy.test_frontends.test_torch import convtorch
convs["torch"] = convtorch
if "tensorflow" in available_frameworks:
from ivy_tests.test_ivy.test_frontends.test_tensorflow import convtensor
convs["tensorflow"] = convtensor
if "jax" in available_frameworks:
from ivy_tests.test_ivy.test_frontends.test_jax import convjax
convs["jax"] = convjax
if frontend in convs:
conv = convs[frontend]
args = ivy.nested_map(args, fn=conv, include_derived=True)
kwargs = ivy.nested_map(kwargs, fn=conv, include_derived=True)
# Make copy for arguments for functions that might use
# inplace update by default
copy_kwargs = copy.deepcopy(kwargs_for_test)
copy_args = copy.deepcopy(args_for_test)
# strip the decorator to get an Ivy array
# ToDo, fix testing for jax frontend for x32
if frontend == "jax":
importlib.import_module("ivy.functional.frontends.jax").config.update(
"jax_enable_x64", True
)
_as_ivy_arrays = not test_flags.generate_frontend_arrays
ret = get_frontend_ret(
frontend_fn, *args_for_test, as_ivy_arrays=_as_ivy_arrays, **kwargs_for_test
)
if test_flags.with_out:
if not inspect.isclass(ret):
is_ret_tuple = issubclass(ret.__class__, tuple)
else:
is_ret_tuple = issubclass(ret, tuple)
if test_flags.generate_frontend_arrays:
if is_ret_tuple:
ret = ivy.nested_map(
ret,
lambda _x: (
arrays_to_frontend(create_frontend_array)(_x)
if not _is_frontend_array(_x)
else _x
),
include_derived=True,
)
elif not _is_frontend_array(ret):
ret = arrays_to_frontend(create_frontend_array)(ret)
else:
if is_ret_tuple:
ret = ivy.nested_map(
ret,
lambda _x: ivy.array(_x) if not ivy.is_array(_x) else _x,
include_derived=True,
)
elif not ivy.is_array(ret):
ret = ivy.array(ret)
out = ret
# pass return value to out argument
# check if passed reference is correctly updated
kwargs["out"] = out
if is_ret_tuple:
if test_flags.generate_frontend_arrays:
flatten_ret = flatten_frontend(
ret=ret, frontend_array_fn=create_frontend_array
)
flatten_out = flatten_frontend(
ret=out, frontend_array_fn=create_frontend_array
)
else:
flatten_ret = flatten(ret=ret)
flatten_out = flatten(ret=out)
for ret_array, out_array in zip(flatten_ret, flatten_out):
if ivy.native_inplace_support and not any(
(ivy.isscalar(ret), ivy.isscalar(out))
):
if test_flags.generate_frontend_arrays:
assert ret_array.ivy_array.data is out_array.ivy_array.data
else:
assert ret_array.data is out_array.data
assert ret_array is out_array
else:
if ivy.native_inplace_support and not any(
(ivy.isscalar(ret), ivy.isscalar(out))
):
if test_flags.generate_frontend_arrays:
assert ret.ivy_array.data is out.ivy_array.data
else:
assert ret.data is out.data
assert ret is out
elif test_flags.inplace:
assert not isinstance(ret, tuple)
if test_flags.generate_frontend_arrays:
assert _is_frontend_array(ret)
else:
assert ivy.is_array(ret)
if test_flags.generate_frontend_arrays:
array_fn = _is_frontend_array
else:
array_fn = ivy.is_array
if "inplace" in list(inspect.signature(frontend_fn).parameters.keys()):
# the function provides optional inplace update
# set inplace update to be True and check
# if returned reference is inputted reference
# and if inputted reference's content is correctly updated
copy_kwargs["inplace"] = True
copy_kwargs["as_ivy_arrays"] = False
first_array = ivy.func_wrapper._get_first_array(
*copy_args, array_fn=array_fn, **copy_kwargs
)
ret_ = get_frontend_ret(frontend_fn, *copy_args, **copy_kwargs)
assert first_array is ret_
else:
# the function provides inplace update by default
# check if returned reference is inputted reference
copy_kwargs["as_ivy_arrays"] = False
first_array = ivy.func_wrapper._get_first_array(
*args, array_fn=array_fn, **kwargs
)
ret_ = get_frontend_ret(frontend_fn, *args, **kwargs)
assert first_array is ret_
# create NumPy args
def arrays_to_numpy(x):
if test_flags.generate_frontend_arrays:
return ivy.to_numpy(x.ivy_array) if _is_frontend_array(x) else x
return ivy.to_numpy(x._data) if isinstance(x, ivy.Array) else x
args_np = ivy.nested_map(
args_for_test,
arrays_to_numpy,
shallow=False,
)
kwargs_np = ivy.nested_map(
kwargs_for_test,
arrays_to_numpy,
shallow=False,
)
# temporarily set frontend framework as backend
ivy.set_backend(frontend)
try:
# create frontend framework args
args_frontend = ivy.nested_map(
args_np,
lambda x: (
ivy.native_array(x)
if isinstance(x, np.ndarray)
else ivy.as_native_dtype(x) if isinstance(x, ivy.Dtype) else x
),
shallow=False,
)
kwargs_frontend = ivy.nested_map(
kwargs_np,
lambda x: ivy.native_array(x) if isinstance(x, np.ndarray) else x,
shallow=False,
)
# change ivy dtypes to native dtypes
if "dtype" in kwargs_frontend:
kwargs_frontend["dtype"] = ivy.as_native_dtype(kwargs_frontend["dtype"])
# change ivy device to native devices
if "device" in kwargs_frontend:
kwargs_frontend["device"] = ivy.as_native_dev(kwargs_frontend["device"])
# check and replace the NativeClass objects in arguments
# with true counterparts
args_frontend = ivy.nested_map(
args_frontend, fn=convtrue, include_derived=True, max_depth=10
)
kwargs_frontend = ivy.nested_map(
kwargs_frontend, fn=convtrue, include_derived=True, max_depth=10
)
# wrap the frontend function objects in arguments to return native arrays
args_frontend = ivy.nested_map(
args_frontend, fn=wrap_frontend_function_args, max_depth=10
)
kwargs_frontend = ivy.nested_map(
kwargs_frontend, fn=wrap_frontend_function_args, max_depth=10
)
# compute the return via the frontend framework
module_name = fn_tree[25 : fn_tree.rfind(".")]
frontend_fw = importlib.import_module(module_name)
> frontend_ret = frontend_fw.__dict__[fn_name](*args_frontend, **kwargs_frontend)
E KeyError: 'uniform_'
E Falsifying example: test_paddle_uniform_(
E frontend='paddle',
E min=0.0,
E max=0.0,
E seed=2,
E dtype_and_x=(['float64'], [array([0., 0.])]),
E fn_tree='ivy.functional.frontends.paddle.uniform_',
E test_flags=FrontendFunctionTestFlags(
E num_positional_args=0,
E with_out=False,
E inplace=False,
E as_variable=[False],
E native_arrays=[False],
E generate_frontend_arrays=False,
E ),
E )
E
E You can reproduce this example by temporarily adding @reproduce_failure('6.78.2', b'AXicY2AgBwAAADYAAQ==') as a decorator on your test case
../../../helpers/function_testing.py:773: KeyError
FAILED [ 60%]
ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_paddle_random.py:129 (test_paddle_uniform_[cpu-ivy.functional.backends.tensorflow-False-False])
frontend = 'paddle'
@handle_frontend_test(
> fn_tree="paddle.uniform_",
min=st.floats(allow_nan=False, allow_infinity=False, width=32),
max=st.floats(allow_nan=False, allow_infinity=False, width=32),
seed=st.integers(min_value=2, max_value=5),
dtype_and_x=helpers.dtype_and_values(
available_dtypes=helpers.get_dtypes("float"),
min_value=0,
max_value=1000,
min_num_dims=1,
max_num_dims=1,
min_dim_size=2,
max_dim_size=2,
),
)
test_paddle_random.py:131:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
test_paddle_random.py:161: in test_paddle_uniform_
helpers.test_frontend_function(
../../../helpers/function_testing.py:788: in test_frontend_function
raise e
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
input_dtypes = ['float64']
test_flags = num_positional_args=0. with_out=False. inplace=False. native_arrays=[False]. as_variable=[False]. generate_frontend_arrays=False.
on_device = 'cpu', frontend = 'paddle'
fn_tree = 'ivy.functional.frontends.paddle.uniform_', rtol = None, atol = 1e-06
test_values = False
def test_frontend_function(
*,
input_dtypes: Union[ivy.Dtype, List[ivy.Dtype]],
test_flags: pf.frontend_function_flags,
on_device="cpu",
frontend: str,
fn_tree: str,
rtol: float = None,
atol: float = 1e-06,
test_values: bool = True,
**all_as_kwargs_np,
):
"""
Test a frontend function for the current backend by comparing the result with the
function in the associated framework.
Parameters
----------
input_dtypes
data types of the input arguments in order.
all_aliases
a list of strings containing all aliases for that function
in the current frontend with their full namespaces.
frontend
current frontend (framework).
fn_tree
Path to function in frontend framework namespace.
rtol
relative tolerance value.
atol
absolute tolerance value.
test_values
if True, test for the correctness of the resulting values.
all_as_kwargs_np
input arguments to the function as keyword arguments.
Returns
-------
ret
optional, return value from the function
ret_np
optional, return value from the Numpy function
"""
assert (
not test_flags.with_out or not test_flags.inplace
), "only one of with_out or with_inplace can be set as True"
# split the arguments into their positional and keyword components
args_np, kwargs_np = kwargs_to_args_n_kwargs(
num_positional_args=test_flags.num_positional_args, kwargs=all_as_kwargs_np
)
create_frontend_array = importlib.import_module(
f"ivy.functional.frontends.{frontend}"
)._frontend_array
# extract all arrays from the arguments and keyword arguments
arg_np_vals, args_idxs, c_arg_vals = _get_nested_np_arrays(args_np)
kwarg_np_vals, kwargs_idxs, c_kwarg_vals = _get_nested_np_arrays(kwargs_np)
# make all lists equal in length
num_arrays = c_arg_vals + c_kwarg_vals
if len(input_dtypes) < num_arrays:
input_dtypes = [input_dtypes[0] for _ in range(num_arrays)]
if len(test_flags.as_variable) < num_arrays:
test_flags.as_variable = [test_flags.as_variable[0] for _ in range(num_arrays)]
if len(test_flags.native_arrays) < num_arrays:
test_flags.native_arrays = [
test_flags.native_arrays[0] for _ in range(num_arrays)
]
# update var flags to be compatible with float dtype and with_out args
test_flags.as_variable = [
v if ivy.is_float_dtype(d) and not test_flags.with_out else False
for v, d in zip(test_flags.as_variable, input_dtypes)
]
# frontend function
# parse function name and frontend submodules (jax.lax, jax.numpy etc.)
split_index = fn_tree.rfind(".")
frontend_submods, fn_name = fn_tree[:split_index], fn_tree[split_index + 1 :]
function_module = importlib.import_module(frontend_submods)
frontend_fn = getattr(function_module, fn_name)
# apply test flags etc.
args, kwargs = create_args_kwargs(
args_np=args_np,
arg_np_vals=arg_np_vals,
args_idxs=args_idxs,
kwargs_np=kwargs_np,
kwarg_np_vals=kwarg_np_vals,
kwargs_idxs=kwargs_idxs,
input_dtypes=input_dtypes,
test_flags=test_flags,
on_device=on_device,
)
if test_flags.generate_frontend_arrays:
args_for_test, kwargs_for_test = args_to_frontend(
*args, frontend_array_fn=create_frontend_array, **kwargs
)
else:
args_for_test, kwargs_for_test = ivy.args_to_ivy(*args, **kwargs)
# check and replace NativeClass object in arguments with ivy counterparts
from ivy_tests.test_ivy.test_frontends.test_numpy import convnumpy
convs = {"numpy": convnumpy}
if "torch" in available_frameworks:
from ivy_tests.test_ivy.test_frontends.test_torch import convtorch
convs["torch"] = convtorch
if "tensorflow" in available_frameworks:
from ivy_tests.test_ivy.test_frontends.test_tensorflow import convtensor
convs["tensorflow"] = convtensor
if "jax" in available_frameworks:
from ivy_tests.test_ivy.test_frontends.test_jax import convjax
convs["jax"] = convjax
if frontend in convs:
conv = convs[frontend]
args = ivy.nested_map(args, fn=conv, include_derived=True)
kwargs = ivy.nested_map(kwargs, fn=conv, include_derived=True)
# Make copy for arguments for functions that might use
# inplace update by default
copy_kwargs = copy.deepcopy(kwargs_for_test)
copy_args = copy.deepcopy(args_for_test)
# strip the decorator to get an Ivy array
# ToDo, fix testing for jax frontend for x32
if frontend == "jax":
importlib.import_module("ivy.functional.frontends.jax").config.update(
"jax_enable_x64", True
)
_as_ivy_arrays = not test_flags.generate_frontend_arrays
ret = get_frontend_ret(
frontend_fn, *args_for_test, as_ivy_arrays=_as_ivy_arrays, **kwargs_for_test
)
if test_flags.with_out:
if not inspect.isclass(ret):
is_ret_tuple = issubclass(ret.__class__, tuple)
else:
is_ret_tuple = issubclass(ret, tuple)
if test_flags.generate_frontend_arrays:
if is_ret_tuple:
ret = ivy.nested_map(
ret,
lambda _x: (
arrays_to_frontend(create_frontend_array)(_x)
if not _is_frontend_array(_x)
else _x
),
include_derived=True,
)
elif not _is_frontend_array(ret):
ret = arrays_to_frontend(create_frontend_array)(ret)
else:
if is_ret_tuple:
ret = ivy.nested_map(
ret,
lambda _x: ivy.array(_x) if not ivy.is_array(_x) else _x,
include_derived=True,
)
elif not ivy.is_array(ret):
ret = ivy.array(ret)
out = ret
# pass return value to out argument
# check if passed reference is correctly updated
kwargs["out"] = out
if is_ret_tuple:
if test_flags.generate_frontend_arrays:
flatten_ret = flatten_frontend(
ret=ret, frontend_array_fn=create_frontend_array
)
flatten_out = flatten_frontend(
ret=out, frontend_array_fn=create_frontend_array
)
else:
flatten_ret = flatten(ret=ret)
flatten_out = flatten(ret=out)
for ret_array, out_array in zip(flatten_ret, flatten_out):
if ivy.native_inplace_support and not any(
(ivy.isscalar(ret), ivy.isscalar(out))
):
if test_flags.generate_frontend_arrays:
assert ret_array.ivy_array.data is out_array.ivy_array.data
else:
assert ret_array.data is out_array.data
assert ret_array is out_array
else:
if ivy.native_inplace_support and not any(
(ivy.isscalar(ret), ivy.isscalar(out))
):
if test_flags.generate_frontend_arrays:
assert ret.ivy_array.data is out.ivy_array.data
else:
assert ret.data is out.data
assert ret is out
elif test_flags.inplace:
assert not isinstance(ret, tuple)
if test_flags.generate_frontend_arrays:
assert _is_frontend_array(ret)
else:
assert ivy.is_array(ret)
if test_flags.generate_frontend_arrays:
array_fn = _is_frontend_array
else:
array_fn = ivy.is_array
if "inplace" in list(inspect.signature(frontend_fn).parameters.keys()):
# the function provides optional inplace update
# set inplace update to be True and check
# if returned reference is inputted reference
# and if inputted reference's content is correctly updated
copy_kwargs["inplace"] = True
copy_kwargs["as_ivy_arrays"] = False
first_array = ivy.func_wrapper._get_first_array(
*copy_args, array_fn=array_fn, **copy_kwargs
)
ret_ = get_frontend_ret(frontend_fn, *copy_args, **copy_kwargs)
assert first_array is ret_
else:
# the function provides inplace update by default
# check if returned reference is inputted reference
copy_kwargs["as_ivy_arrays"] = False
first_array = ivy.func_wrapper._get_first_array(
*args, array_fn=array_fn, **kwargs
)
ret_ = get_frontend_ret(frontend_fn, *args, **kwargs)
assert first_array is ret_
# create NumPy args
def arrays_to_numpy(x):
if test_flags.generate_frontend_arrays:
return ivy.to_numpy(x.ivy_array) if _is_frontend_array(x) else x
return ivy.to_numpy(x._data) if isinstance(x, ivy.Array) else x
args_np = ivy.nested_map(
args_for_test,
arrays_to_numpy,
shallow=False,
)
kwargs_np = ivy.nested_map(
kwargs_for_test,
arrays_to_numpy,
shallow=False,
)
# temporarily set frontend framework as backend
ivy.set_backend(frontend)
try:
# create frontend framework args
args_frontend = ivy.nested_map(
args_np,
lambda x: (
ivy.native_array(x)
if isinstance(x, np.ndarray)
else ivy.as_native_dtype(x) if isinstance(x, ivy.Dtype) else x
),
shallow=False,
)
kwargs_frontend = ivy.nested_map(
kwargs_np,
lambda x: ivy.native_array(x) if isinstance(x, np.ndarray) else x,
shallow=False,
)
# change ivy dtypes to native dtypes
if "dtype" in kwargs_frontend:
kwargs_frontend["dtype"] = ivy.as_native_dtype(kwargs_frontend["dtype"])
# change ivy device to native devices
if "device" in kwargs_frontend:
kwargs_frontend["device"] = ivy.as_native_dev(kwargs_frontend["device"])
# check and replace the NativeClass objects in arguments
# with true counterparts
args_frontend = ivy.nested_map(
args_frontend, fn=convtrue, include_derived=True, max_depth=10
)
kwargs_frontend = ivy.nested_map(
kwargs_frontend, fn=convtrue, include_derived=True, max_depth=10
)
# wrap the frontend function objects in arguments to return native arrays
args_frontend = ivy.nested_map(
args_frontend, fn=wrap_frontend_function_args, max_depth=10
)
kwargs_frontend = ivy.nested_map(
kwargs_frontend, fn=wrap_frontend_function_args, max_depth=10
)
# compute the return via the frontend framework
module_name = fn_tree[25 : fn_tree.rfind(".")]
frontend_fw = importlib.import_module(module_name)
> frontend_ret = frontend_fw.__dict__[fn_name](*args_frontend, **kwargs_frontend)
E KeyError: 'uniform_'
E Falsifying example: test_paddle_uniform_(
E frontend='paddle',
E min=0.0,
E max=0.0,
E seed=2,
E dtype_and_x=(['float64'], [array([0., 0.])]),
E fn_tree='ivy.functional.frontends.paddle.uniform_',
E test_flags=FrontendFunctionTestFlags(
E num_positional_args=0,
E with_out=False,
E inplace=False,
E as_variable=[False],
E native_arrays=[False],
E generate_frontend_arrays=False,
E ),
E )
E
E You can reproduce this example by temporarily adding @reproduce_failure('6.78.2', b'AXicY2AgBwAAADYAAQ==') as a decorator on your test case
../../../helpers/function_testing.py:773: KeyError
FAILED [ 80%]
ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_paddle_random.py:129 (test_paddle_uniform_[cpu-ivy.functional.backends.torch-False-False])
frontend = 'paddle'
@handle_frontend_test(
> fn_tree="paddle.uniform_",
min=st.floats(allow_nan=False, allow_infinity=False, width=32),
max=st.floats(allow_nan=False, allow_infinity=False, width=32),
seed=st.integers(min_value=2, max_value=5),
dtype_and_x=helpers.dtype_and_values(
available_dtypes=helpers.get_dtypes("float"),
min_value=0,
max_value=1000,
min_num_dims=1,
max_num_dims=1,
min_dim_size=2,
max_dim_size=2,
),
)
test_paddle_random.py:131:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
test_paddle_random.py:161: in test_paddle_uniform_
helpers.test_frontend_function(
../../../helpers/function_testing.py:788: in test_frontend_function
raise e
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
input_dtypes = ['float64']
test_flags = num_positional_args=0. with_out=False. inplace=False. native_arrays=[False]. as_variable=[False]. generate_frontend_arrays=False.
on_device = 'cpu', frontend = 'paddle'
fn_tree = 'ivy.functional.frontends.paddle.uniform_', rtol = None, atol = 1e-06
test_values = False
def test_frontend_function(
*,
input_dtypes: Union[ivy.Dtype, List[ivy.Dtype]],
test_flags: pf.frontend_function_flags,
on_device="cpu",
frontend: str,
fn_tree: str,
rtol: float = None,
atol: float = 1e-06,
test_values: bool = True,
**all_as_kwargs_np,
):
"""
Test a frontend function for the current backend by comparing the result with the
function in the associated framework.
Parameters
----------
input_dtypes
data types of the input arguments in order.
all_aliases
a list of strings containing all aliases for that function
in the current frontend with their full namespaces.
frontend
current frontend (framework).
fn_tree
Path to function in frontend framework namespace.
rtol
relative tolerance value.
atol
absolute tolerance value.
test_values
if True, test for the correctness of the resulting values.
all_as_kwargs_np
input arguments to the function as keyword arguments.
Returns
-------
ret
optional, return value from the function
ret_np
optional, return value from the Numpy function
"""
assert (
not test_flags.with_out or not test_flags.inplace
), "only one of with_out or with_inplace can be set as True"
# split the arguments into their positional and keyword components
args_np, kwargs_np = kwargs_to_args_n_kwargs(
num_positional_args=test_flags.num_positional_args, kwargs=all_as_kwargs_np
)
create_frontend_array = importlib.import_module(
f"ivy.functional.frontends.{frontend}"
)._frontend_array
# extract all arrays from the arguments and keyword arguments
arg_np_vals, args_idxs, c_arg_vals = _get_nested_np_arrays(args_np)
kwarg_np_vals, kwargs_idxs, c_kwarg_vals = _get_nested_np_arrays(kwargs_np)
# make all lists equal in length
num_arrays = c_arg_vals + c_kwarg_vals
if len(input_dtypes) < num_arrays:
input_dtypes = [input_dtypes[0] for _ in range(num_arrays)]
if len(test_flags.as_variable) < num_arrays:
test_flags.as_variable = [test_flags.as_variable[0] for _ in range(num_arrays)]
if len(test_flags.native_arrays) < num_arrays:
test_flags.native_arrays = [
test_flags.native_arrays[0] for _ in range(num_arrays)
]
# update var flags to be compatible with float dtype and with_out args
test_flags.as_variable = [
v if ivy.is_float_dtype(d) and not test_flags.with_out else False
for v, d in zip(test_flags.as_variable, input_dtypes)
]
# frontend function
# parse function name and frontend submodules (jax.lax, jax.numpy etc.)
split_index = fn_tree.rfind(".")
frontend_submods, fn_name = fn_tree[:split_index], fn_tree[split_index + 1 :]
function_module = importlib.import_module(frontend_submods)
frontend_fn = getattr(function_module, fn_name)
# apply test flags etc.
args, kwargs = create_args_kwargs(
args_np=args_np,
arg_np_vals=arg_np_vals,
args_idxs=args_idxs,
kwargs_np=kwargs_np,
kwarg_np_vals=kwarg_np_vals,
kwargs_idxs=kwargs_idxs,
input_dtypes=input_dtypes,
test_flags=test_flags,
on_device=on_device,
)
if test_flags.generate_frontend_arrays:
args_for_test, kwargs_for_test = args_to_frontend(
*args, frontend_array_fn=create_frontend_array, **kwargs
)
else:
args_for_test, kwargs_for_test = ivy.args_to_ivy(*args, **kwargs)
# check and replace NativeClass object in arguments with ivy counterparts
from ivy_tests.test_ivy.test_frontends.test_numpy import convnumpy
convs = {"numpy": convnumpy}
if "torch" in available_frameworks:
from ivy_tests.test_ivy.test_frontends.test_torch import convtorch
convs["torch"] = convtorch
if "tensorflow" in available_frameworks:
from ivy_tests.test_ivy.test_frontends.test_tensorflow import convtensor
convs["tensorflow"] = convtensor
if "jax" in available_frameworks:
from ivy_tests.test_ivy.test_frontends.test_jax import convjax
convs["jax"] = convjax
if frontend in convs:
conv = convs[frontend]
args = ivy.nested_map(args, fn=conv, include_derived=True)
kwargs = ivy.nested_map(kwargs, fn=conv, include_derived=True)
# Make copy for arguments for functions that might use
# inplace update by default
copy_kwargs = copy.deepcopy(kwargs_for_test)
copy_args = copy.deepcopy(args_for_test)
# strip the decorator to get an Ivy array
# ToDo, fix testing for jax frontend for x32
if frontend == "jax":
importlib.import_module("ivy.functional.frontends.jax").config.update(
"jax_enable_x64", True
)
_as_ivy_arrays = not test_flags.generate_frontend_arrays
ret = get_frontend_ret(
frontend_fn, *args_for_test, as_ivy_arrays=_as_ivy_arrays, **kwargs_for_test
)
if test_flags.with_out:
if not inspect.isclass(ret):
is_ret_tuple = issubclass(ret.__class__, tuple)
else:
is_ret_tuple = issubclass(ret, tuple)
if test_flags.generate_frontend_arrays:
if is_ret_tuple:
ret = ivy.nested_map(
ret,
lambda _x: (
arrays_to_frontend(create_frontend_array)(_x)
if not _is_frontend_array(_x)
else _x
),
include_derived=True,
)
elif not _is_frontend_array(ret):
ret = arrays_to_frontend(create_frontend_array)(ret)
else:
if is_ret_tuple:
ret = ivy.nested_map(
ret,
lambda _x: ivy.array(_x) if not ivy.is_array(_x) else _x,
include_derived=True,
)
elif not ivy.is_array(ret):
ret = ivy.array(ret)
out = ret
# pass return value to out argument
# check if passed reference is correctly updated
kwargs["out"] = out
if is_ret_tuple:
if test_flags.generate_frontend_arrays:
flatten_ret = flatten_frontend(
ret=ret, frontend_array_fn=create_frontend_array
)
flatten_out = flatten_frontend(
ret=out, frontend_array_fn=create_frontend_array
)
else:
flatten_ret = flatten(ret=ret)
flatten_out = flatten(ret=out)
for ret_array, out_array in zip(flatten_ret, flatten_out):
if ivy.native_inplace_support and not any(
(ivy.isscalar(ret), ivy.isscalar(out))
):
if test_flags.generate_frontend_arrays:
assert ret_array.ivy_array.data is out_array.ivy_array.data
else:
assert ret_array.data is out_array.data
assert ret_array is out_array
else:
if ivy.native_inplace_support and not any(
(ivy.isscalar(ret), ivy.isscalar(out))
):
if test_flags.generate_frontend_arrays:
assert ret.ivy_array.data is out.ivy_array.data
else:
assert ret.data is out.data
assert ret is out
elif test_flags.inplace:
assert not isinstance(ret, tuple)
if test_flags.generate_frontend_arrays:
assert _is_frontend_array(ret)
else:
assert ivy.is_array(ret)
if test_flags.generate_frontend_arrays:
array_fn = _is_frontend_array
else:
array_fn = ivy.is_array
if "inplace" in list(inspect.signature(frontend_fn).parameters.keys()):
# the function provides optional inplace update
# set inplace update to be True and check
# if returned reference is inputted reference
# and if inputted reference's content is correctly updated
copy_kwargs["inplace"] = True
copy_kwargs["as_ivy_arrays"] = False
first_array = ivy.func_wrapper._get_first_array(
*copy_args, array_fn=array_fn, **copy_kwargs
)
ret_ = get_frontend_ret(frontend_fn, *copy_args, **copy_kwargs)
assert first_array is ret_
else:
# the function provides inplace update by default
# check if returned reference is inputted reference
copy_kwargs["as_ivy_arrays"] = False
first_array = ivy.func_wrapper._get_first_array(
*args, array_fn=array_fn, **kwargs
)
ret_ = get_frontend_ret(frontend_fn, *args, **kwargs)
assert first_array is ret_
# create NumPy args
def arrays_to_numpy(x):
if test_flags.generate_frontend_arrays:
return ivy.to_numpy(x.ivy_array) if _is_frontend_array(x) else x
return ivy.to_numpy(x._data) if isinstance(x, ivy.Array) else x
args_np = ivy.nested_map(
args_for_test,
arrays_to_numpy,
shallow=False,
)
kwargs_np = ivy.nested_map(
kwargs_for_test,
arrays_to_numpy,
shallow=False,
)
# temporarily set frontend framework as backend
ivy.set_backend(frontend)
try:
# create frontend framework args
args_frontend = ivy.nested_map(
args_np,
lambda x: (
ivy.native_array(x)
if isinstance(x, np.ndarray)
else ivy.as_native_dtype(x) if isinstance(x, ivy.Dtype) else x
),
shallow=False,
)
kwargs_frontend = ivy.nested_map(
kwargs_np,
lambda x: ivy.native_array(x) if isinstance(x, np.ndarray) else x,
shallow=False,
)
# change ivy dtypes to native dtypes
if "dtype" in kwargs_frontend:
kwargs_frontend["dtype"] = ivy.as_native_dtype(kwargs_frontend["dtype"])
# change ivy device to native devices
if "device" in kwargs_frontend:
kwargs_frontend["device"] = ivy.as_native_dev(kwargs_frontend["device"])
# check and replace the NativeClass objects in arguments
# with true counterparts
args_frontend = ivy.nested_map(
args_frontend, fn=convtrue, include_derived=True, max_depth=10
)
kwargs_frontend = ivy.nested_map(
kwargs_frontend, fn=convtrue, include_derived=True, max_depth=10
)
# wrap the frontend function objects in arguments to return native arrays
args_frontend = ivy.nested_map(
args_frontend, fn=wrap_frontend_function_args, max_depth=10
)
kwargs_frontend = ivy.nested_map(
kwargs_frontend, fn=wrap_frontend_function_args, max_depth=10
)
# compute the return via the frontend framework
module_name = fn_tree[25 : fn_tree.rfind(".")]
frontend_fw = importlib.import_module(module_name)
> frontend_ret = frontend_fw.__dict__[fn_name](*args_frontend, **kwargs_frontend)
E KeyError: 'uniform_'
E Falsifying example: test_paddle_uniform_(
E frontend='paddle',
E min=0.0,
E max=0.0,
E seed=2,
E dtype_and_x=(['float64'], [array([0., 0.])]),
E fn_tree='ivy.functional.frontends.paddle.uniform_',
E test_flags=FrontendFunctionTestFlags(
E num_positional_args=0,
E with_out=False,
E inplace=False,
E as_variable=[False],
E native_arrays=[False],
E generate_frontend_arrays=False,
E ),
E )
E
E You can reproduce this example by temporarily adding @reproduce_failure('6.78.2', b'AXicY2AgBwAAADYAAQ==') as a decorator on your test case
../../../helpers/function_testing.py:773: KeyError
FAILED [100%]
ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_paddle_random.py:129 (test_paddle_uniform_[cpu-ivy.functional.backends.paddle-False-False])
frontend = 'paddle'
@handle_frontend_test(
> fn_tree="paddle.uniform_",
min=st.floats(allow_nan=False, allow_infinity=False, width=32),
max=st.floats(allow_nan=False, allow_infinity=False, width=32),
seed=st.integers(min_value=2, max_value=5),
dtype_and_x=helpers.dtype_and_values(
available_dtypes=helpers.get_dtypes("float"),
min_value=0,
max_value=1000,
min_num_dims=1,
max_num_dims=1,
min_dim_size=2,
max_dim_size=2,
),
)
test_paddle_random.py:131:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
test_paddle_random.py:161: in test_paddle_uniform_
helpers.test_frontend_function(
../../../helpers/function_testing.py:788: in test_frontend_function
raise e
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
input_dtypes = ['float64']
test_flags = num_positional_args=0. with_out=False. inplace=False. native_arrays=[False]. as_variable=[False]. generate_frontend_arrays=False.
on_device = 'cpu', frontend = 'paddle'
fn_tree = 'ivy.functional.frontends.paddle.uniform_', rtol = None, atol = 1e-06
test_values = False
def test_frontend_function(
*,
input_dtypes: Union[ivy.Dtype, List[ivy.Dtype]],
test_flags: pf.frontend_function_flags,
on_device="cpu",
frontend: str,
fn_tree: str,
rtol: float = None,
atol: float = 1e-06,
test_values: bool = True,
**all_as_kwargs_np,
):
"""
Test a frontend function for the current backend by comparing the result with the
function in the associated framework.
Parameters
----------
input_dtypes
data types of the input arguments in order.
all_aliases
a list of strings containing all aliases for that function
in the current frontend with their full namespaces.
frontend
current frontend (framework).
fn_tree
Path to function in frontend framework namespace.
rtol
relative tolerance value.
atol
absolute tolerance value.
test_values
if True, test for the correctness of the resulting values.
all_as_kwargs_np
input arguments to the function as keyword arguments.
Returns
-------
ret
optional, return value from the function
ret_np
optional, return value from the Numpy function
"""
assert (
not test_flags.with_out or not test_flags.inplace
), "only one of with_out or with_inplace can be set as True"
# split the arguments into their positional and keyword components
args_np, kwargs_np = kwargs_to_args_n_kwargs(
num_positional_args=test_flags.num_positional_args, kwargs=all_as_kwargs_np
)
create_frontend_array = importlib.import_module(
f"ivy.functional.frontends.{frontend}"
)._frontend_array
# extract all arrays from the arguments and keyword arguments
arg_np_vals, args_idxs, c_arg_vals = _get_nested_np_arrays(args_np)
kwarg_np_vals, kwargs_idxs, c_kwarg_vals = _get_nested_np_arrays(kwargs_np)
# make all lists equal in length
num_arrays = c_arg_vals + c_kwarg_vals
if len(input_dtypes) < num_arrays:
input_dtypes = [input_dtypes[0] for _ in range(num_arrays)]
if len(test_flags.as_variable) < num_arrays:
test_flags.as_variable = [test_flags.as_variable[0] for _ in range(num_arrays)]
if len(test_flags.native_arrays) < num_arrays:
test_flags.native_arrays = [
test_flags.native_arrays[0] for _ in range(num_arrays)
]
# update var flags to be compatible with float dtype and with_out args
test_flags.as_variable = [
v if ivy.is_float_dtype(d) and not test_flags.with_out else False
for v, d in zip(test_flags.as_variable, input_dtypes)
]
# frontend function
# parse function name and frontend submodules (jax.lax, jax.numpy etc.)
split_index = fn_tree.rfind(".")
frontend_submods, fn_name = fn_tree[:split_index], fn_tree[split_index + 1 :]
function_module = importlib.import_module(frontend_submods)
frontend_fn = getattr(function_module, fn_name)
# apply test flags etc.
args, kwargs = create_args_kwargs(
args_np=args_np,
arg_np_vals=arg_np_vals,
args_idxs=args_idxs,
kwargs_np=kwargs_np,
kwarg_np_vals=kwarg_np_vals,
kwargs_idxs=kwargs_idxs,
input_dtypes=input_dtypes,
test_flags=test_flags,
on_device=on_device,
)
if test_flags.generate_frontend_arrays:
args_for_test, kwargs_for_test = args_to_frontend(
*args, frontend_array_fn=create_frontend_array, **kwargs
)
else:
args_for_test, kwargs_for_test = ivy.args_to_ivy(*args, **kwargs)
# check and replace NativeClass object in arguments with ivy counterparts
from ivy_tests.test_ivy.test_frontends.test_numpy import convnumpy
convs = {"numpy": convnumpy}
if "torch" in available_frameworks:
from ivy_tests.test_ivy.test_frontends.test_torch import convtorch
convs["torch"] = convtorch
if "tensorflow" in available_frameworks:
from ivy_tests.test_ivy.test_frontends.test_tensorflow import convtensor
convs["tensorflow"] = convtensor
if "jax" in available_frameworks:
from ivy_tests.test_ivy.test_frontends.test_jax import convjax
convs["jax"] = convjax
if frontend in convs:
conv = convs[frontend]
args = ivy.nested_map(args, fn=conv, include_derived=True)
kwargs = ivy.nested_map(kwargs, fn=conv, include_derived=True)
# Make copy for arguments for functions that might use
# inplace update by default
copy_kwargs = copy.deepcopy(kwargs_for_test)
copy_args = copy.deepcopy(args_for_test)
# strip the decorator to get an Ivy array
# ToDo, fix testing for jax frontend for x32
if frontend == "jax":
importlib.import_module("ivy.functional.frontends.jax").config.update(
"jax_enable_x64", True
)
_as_ivy_arrays = not test_flags.generate_frontend_arrays
ret = get_frontend_ret(
frontend_fn, *args_for_test, as_ivy_arrays=_as_ivy_arrays, **kwargs_for_test
)
if test_flags.with_out:
if not inspect.isclass(ret):
is_ret_tuple = issubclass(ret.__class__, tuple)
else:
is_ret_tuple = issubclass(ret, tuple)
if test_flags.generate_frontend_arrays:
if is_ret_tuple:
ret = ivy.nested_map(
ret,
lambda _x: (
arrays_to_frontend(create_frontend_array)(_x)
if not _is_frontend_array(_x)
else _x
),
include_derived=True,
)
elif not _is_frontend_array(ret):
ret = arrays_to_frontend(create_frontend_array)(ret)
else:
if is_ret_tuple:
ret = ivy.nested_map(
ret,
lambda _x: ivy.array(_x) if not ivy.is_array(_x) else _x,
include_derived=True,
)
elif not ivy.is_array(ret):
ret = ivy.array(ret)
out = ret
# pass return value to out argument
# check if passed reference is correctly updated
kwargs["out"] = out
if is_ret_tuple:
if test_flags.generate_frontend_arrays:
flatten_ret = flatten_frontend(
ret=ret, frontend_array_fn=create_frontend_array
)
flatten_out = flatten_frontend(
ret=out, frontend_array_fn=create_frontend_array
)
else:
flatten_ret = flatten(ret=ret)
flatten_out = flatten(ret=out)
for ret_array, out_array in zip(flatten_ret, flatten_out):
if ivy.native_inplace_support and not any(
(ivy.isscalar(ret), ivy.isscalar(out))
):
if test_flags.generate_frontend_arrays:
assert ret_array.ivy_array.data is out_array.ivy_array.data
else:
assert ret_array.data is out_array.data
assert ret_array is out_array
else:
if ivy.native_inplace_support and not any(
(ivy.isscalar(ret), ivy.isscalar(out))
):
if test_flags.generate_frontend_arrays:
assert ret.ivy_array.data is out.ivy_array.data
else:
assert ret.data is out.data
assert ret is out
elif test_flags.inplace:
assert not isinstance(ret, tuple)
if test_flags.generate_frontend_arrays:
assert _is_frontend_array(ret)
else:
assert ivy.is_array(ret)
if test_flags.generate_frontend_arrays:
array_fn = _is_frontend_array
else:
array_fn = ivy.is_array
if "inplace" in list(inspect.signature(frontend_fn).parameters.keys()):
# the function provides optional inplace update
# set inplace update to be True and check
# if returned reference is inputted reference
# and if inputted reference's content is correctly updated
copy_kwargs["inplace"] = True
copy_kwargs["as_ivy_arrays"] = False
first_array = ivy.func_wrapper._get_first_array(
*copy_args, array_fn=array_fn, **copy_kwargs
)
ret_ = get_frontend_ret(frontend_fn, *copy_args, **copy_kwargs)
assert first_array is ret_
else:
# the function provides inplace update by default
# check if returned reference is inputted reference
copy_kwargs["as_ivy_arrays"] = False
first_array = ivy.func_wrapper._get_first_array(
*args, array_fn=array_fn, **kwargs
)
ret_ = get_frontend_ret(frontend_fn, *args, **kwargs)
assert first_array is ret_
# create NumPy args
def arrays_to_numpy(x):
if test_flags.generate_frontend_arrays:
return ivy.to_numpy(x.ivy_array) if _is_frontend_array(x) else x
return ivy.to_numpy(x._data) if isinstance(x, ivy.Array) else x
args_np = ivy.nested_map(
args_for_test,
arrays_to_numpy,
shallow=False,
)
kwargs_np = ivy.nested_map(
kwargs_for_test,
arrays_to_numpy,
shallow=False,
)
# temporarily set frontend framework as backend
ivy.set_backend(frontend)
try:
# create frontend framework args
args_frontend = ivy.nested_map(
args_np,
lambda x: (
ivy.native_array(x)
if isinstance(x, np.ndarray)
else ivy.as_native_dtype(x) if isinstance(x, ivy.Dtype) else x
),
shallow=False,
)
kwargs_frontend = ivy.nested_map(
kwargs_np,
lambda x: ivy.native_array(x) if isinstance(x, np.ndarray) else x,
shallow=False,
)
# change ivy dtypes to native dtypes
if "dtype" in kwargs_frontend:
kwargs_frontend["dtype"] = ivy.as_native_dtype(kwargs_frontend["dtype"])
# change ivy device to native devices
if "device" in kwargs_frontend:
kwargs_frontend["device"] = ivy.as_native_dev(kwargs_frontend["device"])
# check and replace the NativeClass objects in arguments
# with true counterparts
args_frontend = ivy.nested_map(
args_frontend, fn=convtrue, include_derived=True, max_depth=10
)
kwargs_frontend = ivy.nested_map(
kwargs_frontend, fn=convtrue, include_derived=True, max_depth=10
)
# wrap the frontend function objects in arguments to return native arrays
args_frontend = ivy.nested_map(
args_frontend, fn=wrap_frontend_function_args, max_depth=10
)
kwargs_frontend = ivy.nested_map(
kwargs_frontend, fn=wrap_frontend_function_args, max_depth=10
)
# compute the return via the frontend framework
module_name = fn_tree[25 : fn_tree.rfind(".")]
frontend_fw = importlib.import_module(module_name)
> frontend_ret = frontend_fw.__dict__[fn_name](*args_frontend, **kwargs_frontend)
E KeyError: 'uniform_'
E Falsifying example: test_paddle_uniform_(
E frontend='paddle',
E min=0.0,
E max=0.0,
E seed=2,
E dtype_and_x=(['float64'], [array([0., 0.])]),
E fn_tree='ivy.functional.frontends.paddle.uniform_',
E test_flags=FrontendFunctionTestFlags(
E num_positional_args=0,
E with_out=False,
E inplace=False,
E as_variable=[False],
E native_arrays=[False],
E generate_frontend_arrays=False,
E ),
E )
E
E You can reproduce this example by temporarily adding @reproduce_failure('6.78.2', b'AXicY2AgBwAAADYAAQ==') as a decorator on your test case
../../../helpers/function_testing.py:773: KeyError
Hi @humzakt,You have to write the test for the function that you are adding in this file ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_paddle_random.py
.
Hi @humzakt,You have to write the test for the function that you are adding in this file
ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_paddle_random.py
.Please follow the wayuniform
has been added for your reference
I did it exactly based on uniform but changed the params as both functions have some different params as mentioned in my above comment but it gives the KeyError
@soma2000-lang I have added the frontend tests and they all have passed. Kindly review my code whenever possible. Thanks