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] support input 0D Tensor as scalar attribute for some api #47689

Merged
merged 2 commits into from
Nov 14, 2022
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
84 changes: 30 additions & 54 deletions paddle/phi/infermeta/ternary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -259,48 +259,24 @@ void ArangeInferMeta(const MetaTensor& start,
const MetaTensor& end,
const MetaTensor& step,
MetaTensor* out) {
auto start_dims = start.dims();
auto end_dims = end.dims();
auto step_dims = step.dims();
PADDLE_ENFORCE_EQ(
start_dims.size(),
1,
phi::errors::InvalidArgument(
"The dim of the shape of Input(Start) should be 1, but got %d",
start_dims.size()));

PADDLE_ENFORCE_EQ(start_dims[0],
PADDLE_ENFORCE_EQ(phi::product(start.dims()),
1,
phi::errors::InvalidArgument(
"The first dim of the shape of Input(Start) should "
"be 1, but got %d",
start_dims[0]));
PADDLE_ENFORCE_EQ(
end_dims.size(),
1,
phi::errors::InvalidArgument(
"The dim of the shape of Input(End) should be 1, but got %d",
end_dims.size()));
"The numel of Input(start) should be 1, but got %d",
phi::product(start.dims())));

PADDLE_ENFORCE_EQ(
end_dims[0],
1,
phi::errors::InvalidArgument("The first dim of the shape of "
"Input(End) should be 1, but got %d",
end_dims[0]));
PADDLE_ENFORCE_EQ(
step_dims.size(),
1,
phi::errors::InvalidArgument(
"The dim of the shape of Input(Step) should be 1, but got %d",
step_dims.size()));
PADDLE_ENFORCE_EQ(phi::product(end.dims()),
1,
phi::errors::InvalidArgument(
"The numel of Input(end) should be 1, but got %d",
phi::product(end.dims())));

PADDLE_ENFORCE_EQ(step_dims[0],
PADDLE_ENFORCE_EQ(phi::product(step.dims()),
1,
phi::errors::InvalidArgument(
"The first dim of the shape of Input(Step) should "
"be 1, but got %d",
step_dims[0]));
"The numel of Input(step) should be 1, but got %d",
phi::product(step.dims())));

out->set_dims({-1});
out->set_dtype(start.dtype());
}
Expand Down Expand Up @@ -635,27 +611,27 @@ void LinspaceRawInferMeta(const MetaTensor& start,
const MetaTensor& stop,
const MetaTensor& number,
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));
auto e_dims = stop.dims();
phi::product(start.dims()),
1,
phi::errors::InvalidArgument("The size of Input(start) should be 1,"
"but got %d.",
phi::product(start.dims())));

PADDLE_ENFORCE_EQ(
(e_dims.size() == 1) && (e_dims[0] == 1),
true,
phi::errors::InvalidArgument("The shape of Input(Stop) must be [1],"
"but received input shape is [%s].",
e_dims));
auto step_dims = number.dims();
phi::product(stop.dims()),
1,
phi::errors::InvalidArgument("The size of Input(stop) should be 1,"
"but got %d.",
phi::product(stop.dims())));

PADDLE_ENFORCE_EQ(
(step_dims.size() == 1) && (step_dims[0] == 1),
true,
phi::errors::InvalidArgument("The shape of Input(Num) must be [1],"
"but received input shape is [%s].",
step_dims));
phi::product(number.dims()),
1,
phi::errors::InvalidArgument("The size of Input(number) should be 1,"
"but got %d.",
phi::product(number.dims())));

out->set_dims(phi::make_ddim({-1}));
out->set_dtype(start.dtype());
}
Expand Down
12 changes: 1 addition & 11 deletions python/paddle/fluid/layers/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -918,17 +918,7 @@ def fill_constant(shape, dtype, value, force_cpu=False, out=None, name=None):
if force_cpu:
place = core.CPUPlace()
if isinstance(shape, (list, tuple)):
for item in shape:
if not isinstance(item, Variable):
shape = list(
map(
lambda x: x.numpy().flat[0]
if isinstance(x, Variable)
else x,
shape,
)
)
break
shape = utils.convert_shape_to_list(shape)

if not isinstance(dtype, core.VarDesc.VarType):
dtype = convert_np_dtype_to_dtype_(dtype)
Expand Down
Loading