Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[Bug]: Shape mistmatch error at Paddle frontend with Scalars #20939

Closed
4 tasks
akshatvishu opened this issue Jul 28, 2023 · 5 comments
Closed
4 tasks

[Bug]: Shape mistmatch error at Paddle frontend with Scalars #20939

akshatvishu opened this issue Jul 28, 2023 · 5 comments
Assignees
Labels
Bug Report Report bugs detected in Ivy.

Comments

@akshatvishu
Copy link
Contributor

akshatvishu commented Jul 28, 2023

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) 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:

    value_test(
        ret_np_flat=ret_np_flat,
        ret_np_from_gt_flat=frontend_ret_np_flat,
        rtol=rtol,
        atol=atol,
        backend=backend_to_test,
        ground_truth_backend=frontend,
    )

refs: https://github.com/unifyai/ivy/blob/master/ivy_tests/test_ivy/helpers/function_testing.py#L833

Steps to Reproduce Bug

import numpy as np
import paddle
import ivy
import ivy.functional.frontends.paddle as ivy_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 tensors
to 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:

import numpy as np
import tensorflow as tf
import torch
import paddle

x = np.array(1)
print(x.shape) # ()
y = paddle.to_tensor(1)
print(y.shape) # []
z = y.numpy()
print(z.shape) # (1,)
tf_1 = tf.constant(1)
print(tf_1.shape) # ()
z1 = tf_1.numpy()
print(z1.shape) # ()
t_orch = torch.tensor(1)
z2 = t_orch.numpy()
print(z2.shape) # ()

Paddle Frontend PR's encountering this issue:

Environment

Linux, VsCode + docker

Ivy Version

1.1.9

Backend

  • NumPy
  • TensorFlow
  • PyTorch
  • JAX

Device

CPU

@akshatvishu akshatvishu added the Bug Report Report bugs detected in Ivy. label Jul 28, 2023
@NripeshN
Copy link
Contributor

NripeshN commented Jul 29, 2023

HI @akshatvishu,
I faced this issue in #19242 too

@akshatvishu
Copy link
Contributor Author

HI @akshatvishu, I faced this issue in #19242 too

added it to the list!

@CatB1t
Copy link
Contributor

CatB1t commented Jul 29, 2023

Thanks @akshatvishu, fixed in fabadb6

@CatB1t CatB1t closed this as completed Jul 29, 2023
@NripeshN
Copy link
Contributor

Hi @CatB1t, I am still facing this issue in #19242.

@akshatvishu
Copy link
Contributor Author

Thanks @akshatvishu, fixed in fabadb6

alt text

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Report Report bugs detected in Ivy.
Projects
None yet
Development

No branches or pull requests

4 participants