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

[Zero-Dim] paddle.count_nonzero/logspace support 0D and add some 0D test case #54581

Merged
merged 1 commit into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions paddle/phi/infermeta/multiary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2162,32 +2162,32 @@ void LogspaceInferMeta(const MetaTensor& start,
MetaTensor* out) {
auto s_dims = start.dims();
PADDLE_ENFORCE_EQ(
(s_dims.size() == 1) && (s_dims[0] == 1),
true,
phi::errors::InvalidArgument("The shape of Input(Start) must be [1],"
"but received input shape is [%s].",
s_dims));
phi::product(s_dims),
1,
phi::errors::InvalidArgument("The size of Input(Start) must be 1,"
"but received input size is %s.",
phi::product(s_dims)));
auto e_dims = stop.dims();
PADDLE_ENFORCE_EQ(
(e_dims.size() == 1) && (e_dims[0] == 1),
phi::product(e_dims),
true,
phi::errors::InvalidArgument("The shape of Input(Stop) must be [1],"
"but received input shape is [%s].",
e_dims));
phi::errors::InvalidArgument("The size of Input(Stop) must be 1,"
"but received input size is %s.",
phi::product(e_dims)));
auto num_dims = number.dims();
PADDLE_ENFORCE_EQ(
(num_dims.size() == 1) && (num_dims[0] == 1),
phi::product(num_dims),
true,
phi::errors::InvalidArgument("The shape of Input(Num) must be [1],"
"but received input shape is [%s].",
num_dims));
phi::errors::InvalidArgument("The size of Input(Num) must be 1,"
"but received input size is %s.",
phi::product(num_dims)));
auto b_dims = base.dims();
PADDLE_ENFORCE_EQ(
(b_dims.size() == 1) && (b_dims[0] == 1),
true,
phi::errors::InvalidArgument("The shape of Input(Base) must be [1],"
"but received input shape is [%s].",
b_dims));
PADDLE_ENFORCE_EQ(phi::product(b_dims),
true,
phi::errors::InvalidArgument(
"The size of Input(Base) must be 1,"
"but received input size is phi::product(b_dims).",
phi::product(b_dims)));
out->set_dims(phi::make_ddim({-1}));
out->set_dtype(dtype);
}
Expand Down
4 changes: 2 additions & 2 deletions python/paddle/nn/functional/distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def pairwise_distance(x, y, p=2.0, epsilon=1e-6, keepdim=False, name=None):
y = paddle.to_tensor([[5., 6.], [7., 8.]], dtype=paddle.float64)
distance = paddle.nn.functional.pairwise_distance(x, y)
print(distance)
# Tensor(shape=[2], dtype=float64, place=Place(gpu:0), stop_gradient=True,
# [4.99999860, 4.99999860])
# Tensor(shape=[2], dtype=float64, place=Place(gpu:0), stop_gradient=True,
# [4.99999860, 4.99999860])

"""
if in_dynamic_mode():
Expand Down
8 changes: 4 additions & 4 deletions python/paddle/tensor/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,15 +394,15 @@ def logspace(start, stop, num, base=10.0, dtype=None, name=None):

Args:
start(int|float|Tensor): The input :attr:`start` is exponent of first entry in \
the sequence. It is a scalar, or a Tensor of shape [1] with input data \
the sequence. It is a scalar, or a 0-D Tensor of shape [] with input data \
type int32, int64, float32 or float64.
stop(int|float|Tensor): The input :attr:`stop` is exponent of last entry in the \
sequence. It is a scalar, or a Tensor of shape [1] with input data \
sequence. It is a scalar, or a 0-D Tensor of shape [] with input data \
type int32, int64, float32 or float64.
num(int|Tensor): The input :attr:`num` is given number of items in the sequence. \
It is an int scalar, or a Tensor of shape [1] with data type int32.
It is an int scalar, or a 0-D Tensor of shape [] with data type int32.
base(int|float|Tensor): The input :attr:`base` is base of the logarithm function. \
It is a scalar, or a Tensor of shape [1] with input data type int32, int64, \
It is a scalar, or a 0-D Tensor of shape [] with input data type int32, int64, \
float32 or float64.
dtype(np.dtype|str, optional): The data type of output tensor, it could be \
int32, int64, float32 or float64. Default: if None, the data type is float32. \
Expand Down
15 changes: 3 additions & 12 deletions python/paddle/tensor/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -1617,7 +1617,7 @@ def count_nonzero(x, axis=None, keepdim=False, name=None):
# x is a 2-D Tensor:
x = paddle.to_tensor([[0., 1.1, 1.2], [0., 0., 1.3], [0., 0., 0.]])
out1 = paddle.count_nonzero(x)
# [3]
# 3
out2 = paddle.count_nonzero(x, axis=0)
# [0, 1, 2]
out3 = paddle.count_nonzero(x, axis=0, keepdim=True)
Expand All @@ -1638,17 +1638,8 @@ def count_nonzero(x, axis=None, keepdim=False, name=None):
# [1, 3, 5]
"""

if axis is not None:
if isinstance(axis, int):
axis = [axis]
dims = len(x.shape)
for i in range(len(axis)):
if not isinstance(axis[i], int) or not (
axis[i] < dims and axis[i] >= -dims
):
raise ValueError(
"Axis should be None, int, or a list, element should in range [-rank(x), rank(x))."
)
if isinstance(axis, int):
axis = [axis]

bool_tensor = paddle.cast(x, 'bool')
int_tensor = paddle.cast(bool_tensor, 'int64')
Expand Down
Loading