You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When trying to test Paddle functions with scalar inputs, a shape mismatch error is encountered due to the conversion of scalars (0-D Tensors) to NumPy arrays. This issue arises because Paddle will be supporting 0-D tensors starting from version 2.6, and converting scalars to NumPy arrays cast them automatically to 1D at present.
Also, paddle started throwing this warning since version 2.5 when we try to do this conversion:
eager_method.cc:140] Warning:: 0D Tensor cannot be used as 'Tensor.numpy()[0]'. In order to avoid this problem, 0D Tensor will be changed to 1D numpy currently, but it's not correct and will be removed in release 2.6. For Tensor contain only one element, Please modify 'Tensor.numpy()[0]' to 'float(Tensor)' as soon as possible, otherwise 'Tensor.numpy()[0]' will raise error in release 2.6.
This probably happens because we convert the result from our current backend v/s the native function result for the same input during our value_test to numpy as part of the ivy frontend test suite for our frontend implementation:
importnumpyasnpimportpaddleimportivyimportivy.functional.frontends.paddleasivy_paddle# Ivy backend "NumPy"ivy.set_backend("numpy")
x_ivy=ivy_paddle.to_tensor([False], dtype="bool")
y_ivy=ivy_paddle.any(x_ivy, axis=0)
print(f"Ivy Output: {y_ivy}, Output Shape: {y_ivy.shape}\n")
# Ivy backend "Paddle"ivy.set_backend("paddle")
x_paddle_ivy=ivy_paddle.to_tensor([False], dtype="bool")
y_paddle_ivy=ivy_paddle.any(x_paddle_ivy, axis=0)
print(f"PaddlePaddle (via Ivy) Output: {y_paddle_ivy}, Output Shape: {y_paddle_ivy.shape}\n") ## PaddlePaddle x_paddle=paddle.to_tensor([False], dtype=paddle.bool)
y_paddle=paddle.any(x_paddle, axis=0)
y_numpy=y_paddle.numpy()
print(f"PaddlePaddle Output: {y_paddle}, Output Shape: {y_paddle.shape}\n")
print(f"PaddleNumpy Output: {y_numpy}, Output Shape: {y_numpy.shape}\n")
# NumPy: x_np=np.array([False])
y_np=np.any(x_np, axis=0)
print(f"NumPy Output: {y_np}, Output Shape: {y_np.shape}\n")
"""workaround for paddle converting 0-D tensorsto 1-D NumPy Array"""# PaddlePaddle x_paddle=paddle.to_tensor([False], dtype=paddle.bool)
y_paddle=paddle.any(x_paddle, axis=0)
y_numpy=y_paddle.numpy().reshape(())
print(f"PaddlePaddle Output: {y_paddle}, Output Shape: {y_paddle.shape}\n")
print(f"PaddleNumpy Output: {y_numpy}, Output Shape: {y_numpy.shape}\n")
# Print"""Ivy Output: ivy.frontends.paddle.Tensor(False), Output Shape: ivy.Shape()PaddlePaddle (via Ivy) Output: ivy.frontends.paddle.Tensor(False), Output Shape: ivy.Shape()I0727 18:09:04.001052 621063 eager_method.cc:140] Warning:: 0D Tensor cannot be used as 'Tensor.numpy()[0]' . In order to avoid this problem, 0D Tensor will be changed to 1D numpy currently, but it's not correct and will be removed in release 2.6. For Tensor contain only one element, Please modify 'Tensor.numpy()[0]' to 'float(Tensor)' as soon as possible, otherwise 'Tensor.numpy()[0]' will raise error in release 2.6.PaddlePaddle Output: Tensor(shape=[], dtype=bool, place=Place(cpu), stop_gradient=True, False), Output Shape: []PaddleNumpy Output: [False], Output Shape: (1,)NumPy Output: False, Output Shape: ()I0727 18:09:04.001331 621063 eager_method.cc:140] Warning:: 0D Tensor cannot be used as 'Tensor.numpy()[0]' . In order to avoid this problem, 0D Tensor will be changed to 1D numpy currently, but it's not correct and will be removed in release 2.6. For Tensor contain only one element, Please modify 'Tensor.numpy()[0]' to 'float(Tensor)' as soon as possible, otherwise 'Tensor.numpy()[0]' will raise error in release 2.6.PaddlePaddle Output: Tensor(shape=[], dtype=bool, place=Place(cpu), stop_gradient=True, False), Output Shape: []PaddleNumpy Output: False, Output Shape: ()"""
Also, paddle says to use float(input_tensor) if it's a scalar (0-D) tensor instead of .numpy() to access the single scalar value but we can also use the .item() for the same !
Additionally, only Paddle has this issue ; other frameworks handle this correctly:
Bug Explanation
When trying to test
Paddle
functions with scalar inputs, a shape mismatch error is encountered due to the conversion of scalars (0-D Tensors) toNumPy
arrays. This issue arises because Paddle will be supporting0-D
tensors starting from version2.6
, and converting scalars toNumPy
arrays cast them automatically to1D
at present.Also,
paddle
started throwing this warning since version2.5
when we try to do this conversion:This probably happens because we convert the result from our current backend v/s the native function result for the same input during our
value_test
to numpy as part of theivy
frontend test suite for our frontend implementation:refs: https://github.com/unifyai/ivy/blob/master/ivy_tests/test_ivy/helpers/function_testing.py#L833
Steps to Reproduce Bug
Also,
paddle
says to usefloat(input_tensor)
if it's ascalar
(0-D
) tensor instead of.numpy()
to access the single scalar value but we can also use the.item()
for the same !Additionally, only
Paddle
has this issue ; other frameworks handle this correctly:Paddle Frontend PR's encountering this issue:
any
added any to paddle math #19553rank
Rank paddle frontend #20185roll
add roll paddle frontend manipulation #19258clone
Paddle creation clone #19288kron
EHN: adding kron in paddle math..py #18280unstack
add unstack paddle frontend manipulation #19242Environment
Linux, VsCode + docker
Ivy Version
1.1.9
Backend
Device
CPU
The text was updated successfully, but these errors were encountered: