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

[2.0] support list in interp scale_factor #26493

Closed
wants to merge 9 commits into from
Closed
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
69 changes: 50 additions & 19 deletions paddle/fluid/operators/interpolate_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,21 @@ static void Interpolate1DInferShapeCheck(framework::InferShapeContext* ctx) {
platform::errors::InvalidArgument(
"Scale's dimension size must be 1, but got dimension = %d .",
scale_tensor.size()));
PADDLE_ENFORCE_EQ(
scale_tensor[0], 1,
platform::errors::InvalidArgument(
"Scale's shape must be 1, but got shape = %d .", scale_tensor[0]));
out_w = -1;
} else {
float scale = ctx->Attrs().Get<float>("scale");
if (scale > 0) {
float scale_w = ctx->Attrs().Get<float>("scale_w");
PADDLE_ENFORCE_EQ(scale_w > 0, true, platform::errors::InvalidArgument(
"scale of Op(interpolate) "
"should be greater than 0."));
if (scale_w > 0) {
// round down
out_w = (data_layout == DataLayout::kNCHW
? static_cast<int>(dim_x[2] * scale)
: static_cast<int>(dim_x[1] * scale));
? static_cast<int>(dim_x[2] * scale_w)
: static_cast<int>(dim_x[1] * scale_w));
// protect when input shape is -1
out_w = out_w > 0 ? out_w : -1;
} else {
Expand Down Expand Up @@ -144,18 +151,27 @@ static void Interpolate2DInferShapeCheck(framework::InferShapeContext* ctx) {
platform::errors::InvalidArgument(
"Scale's dimension size must be 1, but got dimension = %d .",
scale_tensor.size()));
PADDLE_ENFORCE_EQ(scale_tensor[0] == 2 || scale_tensor[0] == 1, true,
platform::errors::InvalidArgument(
"Scale's shape must be 2 or 1, but got shape = %d .",
scale_tensor[0]));
out_h = -1;
out_w = -1;
} else {
float scale = ctx->Attrs().Get<float>("scale");
if (scale > 0) {
float scale_h = ctx->Attrs().Get<float>("scale_h");
float scale_w = ctx->Attrs().Get<float>("scale_w");
PADDLE_ENFORCE_EQ(
scale_w > 0 && scale_h > 0, true,
platform::errors::InvalidArgument("scale of Op(interpolate) "
"should be greater than 0."));
if (scale_h > 0 && scale_w > 0) {
// round down
out_h = (data_layout == DataLayout::kNCHW
? static_cast<int>(dim_x[2] * scale)
: static_cast<int>(dim_x[1] * scale));
? static_cast<int>(dim_x[2] * scale_h)
: static_cast<int>(dim_x[1] * scale_h));
out_w = (data_layout == DataLayout::kNCHW
? static_cast<int>(dim_x[3] * scale)
: static_cast<int>(dim_x[2] * scale));
? static_cast<int>(dim_x[3] * scale_w)
: static_cast<int>(dim_x[2] * scale_w));
// protect when input shape is -1
out_h = out_h > 0 ? out_h : -1;
out_w = out_w > 0 ? out_w : -1;
Expand Down Expand Up @@ -235,22 +251,32 @@ static void Interpolate3DInferShapeCheck(framework::InferShapeContext* ctx) {
platform::errors::InvalidArgument(
"Scale's dimension size must be 1, but got size = %d .",
scale_tensor.size()));
PADDLE_ENFORCE_EQ(scale_tensor[0] == 3 || scale_tensor[0] == 1, true,
platform::errors::InvalidArgument(
"Scale's shape must be 3 or 1, but got shape = %d .",
scale_tensor[0]));
out_d = -1;
out_h = -1;
out_w = -1;
} else {
float scale = ctx->Attrs().Get<float>("scale");
if (scale > 0) {
float scale_d = ctx->Attrs().Get<float>("scale_d");
float scale_h = ctx->Attrs().Get<float>("scale_h");
float scale_w = ctx->Attrs().Get<float>("scale_w");
PADDLE_ENFORCE_EQ(
scale_w > 0 && scale_h > 0 && scale_d > 0, true,
platform::errors::InvalidArgument("scale of Op(interpolate) "
"should be greater than 0."));
if (scale_d > 0 && scale_h > 0 && scale_w > 0) {
// round down
out_d = (data_layout == DataLayout::kNCHW
? static_cast<int>(dim_x[2] * scale)
: static_cast<int>(dim_x[1] * scale));
? static_cast<int>(dim_x[2] * scale_d)
: static_cast<int>(dim_x[1] * scale_d));
out_h = (data_layout == DataLayout::kNCHW
? static_cast<int>(dim_x[3] * scale)
: static_cast<int>(dim_x[2] * scale));
? static_cast<int>(dim_x[3] * scale_h)
: static_cast<int>(dim_x[2] * scale_h));
out_w = (data_layout == DataLayout::kNCHW
? static_cast<int>(dim_x[4] * scale)
: static_cast<int>(dim_x[3] * scale));
? static_cast<int>(dim_x[4] * scale_w)
: static_cast<int>(dim_x[3] * scale_w));
// protect when input shape is -1
out_d = out_d > 0 ? out_d : -1;
out_h = out_h > 0 ? out_h : -1;
Expand Down Expand Up @@ -370,7 +396,12 @@ class InterpolateOpMaker : public framework::OpProtoAndCheckerMaker {
AddAttr<int>("out_d", "output depth of interpolate op.").SetDefault(0);
AddAttr<int>("out_h", "output height of interpolate op.").SetDefault(0);
AddAttr<int>("out_w", "output width of interpolate op.").SetDefault(0);
AddAttr<float>("scale", "scale factor of interpolate op.").SetDefault(0.);
AddAttr<float>("scale_w", "scale_w factor of interpolate op.")
.SetDefault(0.);
AddAttr<float>("scale_h", "scale_h factor of interpolate op.")
.SetDefault(0.);
AddAttr<float>("scale_d", "scale_d factor of interpolate op.")
.SetDefault(0.);
AddAttr<std::string>("interp_method",
"(string, default \"bilinear\"), interpolation "
"method, can be \"linear\" for linear interpolation"
Expand Down
157 changes: 121 additions & 36 deletions paddle/fluid/operators/interpolate_op.cu
Original file line number Diff line number Diff line change
Expand Up @@ -841,16 +841,22 @@ static void Interpolate1DCUDAFwd(const framework::ExecutionContext& ctx,
auto new_size = get_new_shape(list_new_shape_tensor);
out_w = new_size[0];
} else {
float scale;
float scale_w;
auto scale_tensor = ctx.Input<Tensor>("Scale");
if (scale_tensor != nullptr) {
auto scale_data = get_new_data_from_tensor<float>(scale_tensor);
scale = scale_data[0];
scale_w = scale_data[0];
PADDLE_ENFORCE_EQ(scale_w > 0, true, platform::errors::InvalidArgument(
"scale of Op(interpolate) "
"should be greater than 0."));
} else {
scale = ctx.Attr<float>("scale");
scale_w = ctx.Attr<float>("scale_w");
PADDLE_ENFORCE_EQ(scale_w > 0, true, platform::errors::InvalidArgument(
"scale of Op(interpolate) "
"should be greater than 0."));
}
if (scale > 0) {
out_w = static_cast<int>(in_w * scale);
if (scale_w > 0) {
out_w = static_cast<int>(in_w * scale_w);
}
auto out_size = ctx.Input<Tensor>("OutSize");
if (out_size != nullptr) {
Expand Down Expand Up @@ -921,17 +927,33 @@ static void Interpolate2DCUDAFwd(const framework::ExecutionContext& ctx,
out_h = new_size[0];
out_w = new_size[1];
} else {
float scale;
float scale_h;
float scale_w;
auto scale_tensor = ctx.Input<Tensor>("Scale");
if (scale_tensor != nullptr) {
auto scale_data = get_new_data_from_tensor<float>(scale_tensor);
scale = scale_data[0];
if (scale_data.size() > 1) {
scale_h = scale_data[0];
scale_w = scale_data[1];
} else {
scale_h = scale_data[0];
scale_w = scale_data[0];
}
PADDLE_ENFORCE_EQ(
scale_w > 0 && scale_h > 0, true,
platform::errors::InvalidArgument("scale of Op(interpolate) "
"should be greater than 0."));
} else {
scale = ctx.Attr<float>("scale");
scale_w = ctx.Attr<float>("scale_w");
scale_h = ctx.Attr<float>("scale_h");
PADDLE_ENFORCE_EQ(
scale_w > 0 && scale_h > 0, true,
platform::errors::InvalidArgument("scale of Op(interpolate) "
"should be greater than 0."));
}
if (scale > 0) {
out_h = static_cast<int>(in_h * scale);
out_w = static_cast<int>(in_w * scale);
if (scale_w > 0 && scale_h > 0) {
out_h = static_cast<int>(in_h * scale_h);
out_w = static_cast<int>(in_w * scale_w);
}
auto out_size = ctx.Input<Tensor>("OutSize");
if (out_size != nullptr) {
Expand Down Expand Up @@ -1027,18 +1049,38 @@ static void Interpolate3DCUDAFwd(const framework::ExecutionContext& ctx,
out_h = new_size[1];
out_w = new_size[2];
} else {
float scale;
float scale_d;
float scale_h;
float scale_w;
auto scale_tensor = ctx.Input<Tensor>("Scale");
if (scale_tensor != nullptr) {
auto scale_data = get_new_data_from_tensor<float>(scale_tensor);
scale = scale_data[0];
if (scale_data.size() > 1) {
scale_d = scale_data[0];
scale_h = scale_data[1];
scale_w = scale_data[2];
} else {
scale_d = scale_data[0];
scale_h = scale_data[0];
scale_w = scale_data[0];
}
PADDLE_ENFORCE_EQ(
scale_w > 0 && scale_h > 0 && scale_d > 0, true,
platform::errors::InvalidArgument("scale of Op(interpolate) "
"should be greater than 0."));
} else {
scale = ctx.Attr<float>("scale");
scale_d = ctx.Attr<float>("scale_d");
scale_h = ctx.Attr<float>("scale_h");
scale_w = ctx.Attr<float>("scale_w");
PADDLE_ENFORCE_EQ(
scale_w > 0 && scale_h > 0 && scale_d > 0, true,
platform::errors::InvalidArgument("scale of Op(interpolate) "
"should be greater than 0."));
}
if (scale > 0) {
out_d = static_cast<int>(in_d * scale);
out_h = static_cast<int>(in_h * scale);
out_w = static_cast<int>(in_w * scale);
if (scale_d > 0 && scale_h > 0 && scale_w > 0) {
out_d = static_cast<int>(in_d * scale_d);
out_h = static_cast<int>(in_h * scale_h);
out_w = static_cast<int>(in_w * scale_w);
}
auto out_size = ctx.Input<Tensor>("OutSize");
if (out_size != nullptr) {
Expand Down Expand Up @@ -1122,16 +1164,22 @@ static void Interpolate1DCUDABwd(const framework::ExecutionContext& ctx,
int align_mode = ctx.Attr<int>("align_mode");

int out_w = ctx.Attr<int>("out_w");
float scale;
float scale_w;
auto scale_tensor = ctx.Input<Tensor>("Scale");
if (scale_tensor != nullptr) {
auto scale_data = get_new_data_from_tensor<float>(scale_tensor);
scale = scale_data[0];
scale_w = scale_data[0];
PADDLE_ENFORCE_EQ(scale_w > 0, true, platform::errors::InvalidArgument(
"scale of Op(interpolate) "
"should be greater than 0."));
} else {
scale = ctx.Attr<float>("scale");
scale_w = ctx.Attr<float>("scale_w");
PADDLE_ENFORCE_EQ(scale_w > 0, true, platform::errors::InvalidArgument(
"scale of Op(interpolate) "
"should be greater than 0."));
}
if (scale > 0) {
out_w = static_cast<int>(in_w * scale);
if (scale_w > 0) {
out_w = static_cast<int>(in_w * scale_w);
}

auto out_size = ctx.Input<Tensor>("OutSize");
Expand Down Expand Up @@ -1201,17 +1249,33 @@ static void Interpolate2DCUDABwd(const framework::ExecutionContext& ctx,

int out_h = ctx.Attr<int>("out_h");
int out_w = ctx.Attr<int>("out_w");
float scale;
float scale_h;
float scale_w;
auto scale_tensor = ctx.Input<Tensor>("Scale");
if (scale_tensor != nullptr) {
auto scale_data = get_new_data_from_tensor<float>(scale_tensor);
scale = scale_data[0];
if (scale_data.size() > 1) {
scale_h = scale_data[0];
scale_w = scale_data[1];
} else {
scale_h = scale_data[0];
scale_w = scale_data[0];
}
PADDLE_ENFORCE_EQ(
scale_w > 0 && scale_h > 0, true,
platform::errors::InvalidArgument("scale of Op(interpolate) "
"should be greater than 0."));
} else {
scale = ctx.Attr<float>("scale");
scale_w = ctx.Attr<float>("scale_w");
scale_h = ctx.Attr<float>("scale_h");
PADDLE_ENFORCE_EQ(
scale_w > 0 && scale_h > 0, true,
platform::errors::InvalidArgument("scale of Op(interpolate) "
"should be greater than 0."));
}
if (scale > 0) {
out_h = static_cast<int>(in_h * scale);
out_w = static_cast<int>(in_w * scale);
if (scale_w > 0 && scale_h > 0) {
out_h = static_cast<int>(in_h * scale_h);
out_w = static_cast<int>(in_w * scale_w);
}

auto out_size = ctx.Input<Tensor>("OutSize");
Expand Down Expand Up @@ -1305,18 +1369,38 @@ static void Interpolate3DCUDABwd(const framework::ExecutionContext& ctx,
int out_d = ctx.Attr<int>("out_d");
int out_h = ctx.Attr<int>("out_h");
int out_w = ctx.Attr<int>("out_w");
float scale;
float scale_d;
float scale_h;
float scale_w;
auto scale_tensor = ctx.Input<Tensor>("Scale");
if (scale_tensor != nullptr) {
auto scale_data = get_new_data_from_tensor<float>(scale_tensor);
scale = scale_data[0];
if (scale_data.size() > 1) {
scale_d = scale_data[0];
scale_h = scale_data[1];
scale_w = scale_data[2];
} else {
scale_d = scale_data[0];
scale_h = scale_data[0];
scale_w = scale_data[0];
}
PADDLE_ENFORCE_EQ(
scale_w > 0 && scale_h > 0 && scale_d > 0, true,
platform::errors::InvalidArgument("scale of Op(interpolate) "
"should be greater than 0."));
} else {
scale = ctx.Attr<float>("scale");
scale_d = ctx.Attr<float>("scale_d");
scale_h = ctx.Attr<float>("scale_h");
scale_w = ctx.Attr<float>("scale_w");
PADDLE_ENFORCE_EQ(
scale_w > 0 && scale_h > 0 && scale_d > 0, true,
platform::errors::InvalidArgument("scale of Op(interpolate) "
"should be greater than 0."));
}
if (scale > 0) {
out_d = static_cast<int>(in_d * scale);
out_h = static_cast<int>(in_h * scale);
out_w = static_cast<int>(in_w * scale);
if (scale_d > 0 && scale_h > 0 && scale_w > 0) {
out_d = static_cast<int>(in_d * scale_d);
out_h = static_cast<int>(in_h * scale_h);
out_w = static_cast<int>(in_w * scale_w);
}

auto out_size = ctx.Input<Tensor>("OutSize");
Expand Down Expand Up @@ -1435,6 +1519,7 @@ class InterpolateGradOpCUDAKernel : public framework::OpKernel<T> {
} // namespace paddle

namespace ops = paddle::operators;
namespace plat = paddle::platform;
REGISTER_OP_CUDA_KERNEL(bilinear_interp, ops::InterpolateOpCUDAKernel<float>,
ops::InterpolateOpCUDAKernel<double>,
ops::InterpolateOpCUDAKernel<int>);
Expand Down
Loading