Skip to content

Commit 1cd7d14

Browse files
authored
fix typo for interp_v2,test=develop (#26843) (#27158)
* fix typo for interp_v2,test=develop * align with torch, test=develop * add area mode, test=develop * fix bug, test=develop * format notes, test=develop * update for converage, test=develop * fix bilinear, test=develop * fix bicubic, test=develop * fix typo, test=develop * fix coverage, test=develop * fix helper.input_dtype, test=develop * polish notes, test=develop * polish notes, test=develop * polish notes, test=develop
1 parent 73dbab3 commit 1cd7d14

17 files changed

+350
-125
lines changed

paddle/fluid/operators/interpolate_v2_op.cc

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static void Interpolate1DInferShapeCheck(framework::InferShapeContext* ctx) {
6767
scale_tensor[0], 1,
6868
platform::errors::InvalidArgument(
6969
"Scale's shape must be 1, but got shape = %d .", scale_tensor[0]));
70-
// out_w = -1;
70+
out_w = -1;
7171
} else {
7272
auto scale = ctx->Attrs().Get<std::vector<float>>("scale");
7373
if (scale.size() > 0) {
@@ -159,8 +159,8 @@ static void Interpolate2DInferShapeCheck(framework::InferShapeContext* ctx) {
159159
platform::errors::InvalidArgument(
160160
"Scale's shape must be 2 or 1, but got shape = %d .",
161161
scale_tensor[0]));
162-
// out_h = -1;
163-
// out_w = -1;
162+
out_h = -1;
163+
out_w = -1;
164164
} else {
165165
auto scale = ctx->Attrs().Get<std::vector<float>>("scale");
166166
if (scale.size() > 0) {
@@ -264,9 +264,9 @@ static void Interpolate3DInferShapeCheck(framework::InferShapeContext* ctx) {
264264
platform::errors::InvalidArgument(
265265
"Scale's shape must be 3 or 1, but got shape = %d .",
266266
scale_tensor[0]));
267-
// out_d = -1;
268-
// out_h = -1;
269-
// out_w = -1;
267+
out_d = -1;
268+
out_h = -1;
269+
out_w = -1;
270270
} else {
271271
auto scale = ctx->Attrs().Get<std::vector<float>>("scale");
272272
if (scale.size() > 0) {
@@ -633,6 +633,9 @@ DECLARE_NO_NEED_BUFFER_VARS_INFERER(InterpolateV2GradNoNeedBufferVarsInferer,
633633
} // namespace operators
634634
} // namespace paddle
635635

636+
// interp_v2 support scale_factor whose input type is list, this operation is
637+
// not
638+
// compatible with interp_op, so a new one is added in paddle2.0
636639
namespace ops = paddle::operators;
637640
REGISTER_OPERATOR(bilinear_interp_v2, ops::InterpolateV2Op,
638641
ops::InterpolateV2OpMaker,

paddle/fluid/operators/interpolate_v2_op.cu

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -836,12 +836,12 @@ static void Interpolate1DCUDAFwd(const framework::ExecutionContext& ctx,
836836
int out_w = ctx.Attr<int>("out_w");
837837

838838
auto list_new_shape_tensor = ctx.MultiInput<framework::Tensor>("SizeTensor");
839+
float scale_w = -1;
839840
if (list_new_shape_tensor.size() > 0) {
840841
// have size tensor
841842
auto new_size = get_new_shape(list_new_shape_tensor);
842843
out_w = new_size[0];
843844
} else {
844-
float scale_w = -1;
845845
auto scale_tensor = ctx.Input<Tensor>("Scale");
846846
auto scale = ctx.Attr<std::vector<float>>("scale");
847847
if (scale_tensor != nullptr) {
@@ -887,8 +887,11 @@ static void Interpolate1DCUDAFwd(const framework::ExecutionContext& ctx,
887887

888888
float ratio_w = 0.f;
889889
if (out_w > 1) {
890+
float new_scale_w = 0.f;
891+
new_scale_w = (scale_w > 0) ? static_cast<float>(1. / scale_w)
892+
: static_cast<float>(in_w) / out_w;
890893
ratio_w = (align_corners) ? static_cast<float>(in_w - 1.0) / (out_w - 1.0)
891-
: static_cast<float>(in_w) / out_w;
894+
: static_cast<float>(new_scale_w);
892895
}
893896

894897
int in_cw = c * in_w;
@@ -924,14 +927,14 @@ static void Interpolate2DCUDAFwd(const framework::ExecutionContext& ctx,
924927
int out_w = ctx.Attr<int>("out_w");
925928

926929
auto list_new_shape_tensor = ctx.MultiInput<framework::Tensor>("SizeTensor");
930+
float scale_w = -1;
931+
float scale_h = -1;
927932
if (list_new_shape_tensor.size() > 0) {
928933
// have size tensor
929934
auto new_size = get_new_shape(list_new_shape_tensor);
930935
out_h = new_size[0];
931936
out_w = new_size[1];
932937
} else {
933-
float scale_h = -1;
934-
float scale_w = -1;
935938
auto scale_tensor = ctx.Input<Tensor>("Scale");
936939
auto scale = ctx.Attr<std::vector<float>>("scale");
937940
if (scale_tensor != nullptr) {
@@ -993,12 +996,18 @@ static void Interpolate2DCUDAFwd(const framework::ExecutionContext& ctx,
993996
float ratio_h = 0.f;
994997
float ratio_w = 0.f;
995998
if (out_h > 1) {
999+
float new_scale_h = 0.f;
1000+
new_scale_h = (scale_h > 0) ? static_cast<float>(1. / scale_h)
1001+
: static_cast<float>(in_h) / out_h;
9961002
ratio_h = (align_corners) ? static_cast<float>(in_h - 1) / (out_h - 1)
997-
: static_cast<float>(in_h) / out_h;
1003+
: static_cast<float>(new_scale_h);
9981004
}
9991005
if (out_w > 1) {
1006+
float new_scale_w = 0.f;
1007+
new_scale_w = (scale_w > 0) ? static_cast<float>(1. / scale_w)
1008+
: static_cast<float>(in_w) / out_w;
10001009
ratio_w = (align_corners) ? static_cast<float>(in_w - 1) / (out_w - 1)
1001-
: static_cast<float>(in_w) / out_w;
1010+
: static_cast<float>(new_scale_w);
10021011
}
10031012

10041013
int in_hw = in_h * in_w;
@@ -1048,16 +1057,16 @@ static void Interpolate3DCUDAFwd(const framework::ExecutionContext& ctx,
10481057
int out_w = ctx.Attr<int>("out_w");
10491058

10501059
auto list_new_shape_tensor = ctx.MultiInput<framework::Tensor>("SizeTensor");
1060+
float scale_w = -1;
1061+
float scale_d = -1;
1062+
float scale_h = -1;
10511063
if (list_new_shape_tensor.size() > 0) {
10521064
// have size tensor
10531065
auto new_size = get_new_shape(list_new_shape_tensor);
10541066
out_d = new_size[0];
10551067
out_h = new_size[1];
10561068
out_w = new_size[2];
10571069
} else {
1058-
float scale_d = -1;
1059-
float scale_h = -1;
1060-
float scale_w = -1;
10611070
auto scale_tensor = ctx.Input<Tensor>("Scale");
10621071
auto scale = ctx.Attr<std::vector<float>>("scale");
10631072
if (scale_tensor != nullptr) {
@@ -1129,16 +1138,25 @@ static void Interpolate3DCUDAFwd(const framework::ExecutionContext& ctx,
11291138
float ratio_h = 0.f;
11301139
float ratio_w = 0.f;
11311140
if (out_d > 1) {
1141+
float new_scale_d = 0.f;
1142+
new_scale_d = (scale_d > 0) ? static_cast<float>(1. / scale_d)
1143+
: static_cast<float>(in_d) / out_d;
11321144
ratio_d = (align_corners) ? static_cast<float>(in_d - 1) / (out_d - 1)
1133-
: static_cast<float>(in_d) / out_d;
1145+
: static_cast<float>(new_scale_d);
11341146
}
11351147
if (out_h > 1) {
1148+
float new_scale_h = 0.f;
1149+
new_scale_h = (scale_h > 0) ? static_cast<float>(1. / scale_h)
1150+
: static_cast<float>(in_h) / out_h;
11361151
ratio_h = (align_corners) ? static_cast<float>(in_h - 1) / (out_h - 1)
1137-
: static_cast<float>(in_h) / out_h;
1152+
: static_cast<float>(new_scale_h);
11381153
}
11391154
if (out_w > 1) {
1155+
float new_scale_w = 0.f;
1156+
new_scale_w = (scale_w > 0) ? static_cast<float>(1. / scale_w)
1157+
: static_cast<float>(in_w) / out_w;
11401158
ratio_w = (align_corners) ? static_cast<float>(in_w - 1) / (out_w - 1)
1141-
: static_cast<float>(in_w) / out_w;
1159+
: static_cast<float>(new_scale_w);
11421160
}
11431161

11441162
int in_dhw = in_d * in_h * in_w;
@@ -1230,8 +1248,11 @@ static void Interpolate1DCUDABwd(const framework::ExecutionContext& ctx,
12301248

12311249
float ratio_w = 0.f;
12321250
if (out_w > 1) {
1251+
float new_scale_w = 0.f;
1252+
new_scale_w = (scale_w > 0) ? static_cast<float>(1. / scale_w)
1253+
: static_cast<float>(in_w) / out_w;
12331254
ratio_w = (align_corners) ? static_cast<float>(in_w - 1) / (out_w - 1)
1234-
: static_cast<float>(in_w) / out_w;
1255+
: static_cast<float>(new_scale_w);
12351256
}
12361257
int in_cw = c * in_w;
12371258
int out_cw = c * out_w;
@@ -1333,12 +1354,18 @@ static void Interpolate2DCUDABwd(const framework::ExecutionContext& ctx,
13331354
float ratio_h = 0.f;
13341355
float ratio_w = 0.f;
13351356
if (out_h > 1) {
1357+
float new_scale_h = 0.f;
1358+
new_scale_h = (scale_h > 0) ? static_cast<float>(1. / scale_h)
1359+
: static_cast<float>(in_h) / out_h;
13361360
ratio_h = (align_corners) ? static_cast<float>(in_h - 1) / (out_h - 1)
1337-
: static_cast<float>(in_h) / out_h;
1361+
: static_cast<float>(new_scale_h);
13381362
}
13391363
if (out_w > 1) {
1364+
float new_scale_w = 0.f;
1365+
new_scale_w = (scale_w > 0) ? static_cast<float>(1. / scale_w)
1366+
: static_cast<float>(in_w) / out_w;
13401367
ratio_w = (align_corners) ? static_cast<float>(in_w - 1) / (out_w - 1)
1341-
: static_cast<float>(in_w) / out_w;
1368+
: static_cast<float>(new_scale_w);
13421369
}
13431370

13441371
int in_hw = in_h * in_w;
@@ -1464,16 +1491,25 @@ static void Interpolate3DCUDABwd(const framework::ExecutionContext& ctx,
14641491
float ratio_h = 0.f;
14651492
float ratio_w = 0.f;
14661493
if (out_d > 1) {
1494+
float new_scale_d = 0.f;
1495+
new_scale_d = (scale_d > 0) ? static_cast<float>(1. / scale_d)
1496+
: static_cast<float>(in_d) / out_d;
14671497
ratio_d = (align_corners) ? static_cast<float>(in_d - 1) / (out_d - 1)
1468-
: static_cast<float>(in_d) / out_d;
1498+
: static_cast<float>(new_scale_d);
14691499
}
14701500
if (out_h > 1) {
1501+
float new_scale_h = 0.f;
1502+
new_scale_h = (scale_h > 0) ? static_cast<float>(1. / scale_h)
1503+
: static_cast<float>(in_h) / out_h;
14711504
ratio_h = (align_corners) ? static_cast<float>(in_h - 1) / (out_h - 1)
1472-
: static_cast<float>(in_h) / out_h;
1505+
: static_cast<float>(new_scale_h);
14731506
}
14741507
if (out_w > 1) {
1508+
float new_scale_w = 0.f;
1509+
new_scale_w = (scale_w > 0) ? static_cast<float>(1. / scale_w)
1510+
: static_cast<float>(in_w) / out_w;
14751511
ratio_w = (align_corners) ? static_cast<float>(in_w - 1) / (out_w - 1)
1476-
: static_cast<float>(in_w) / out_w;
1512+
: static_cast<float>(new_scale_w);
14771513
}
14781514

14791515
int in_dhw = in_d * in_h * in_w;

paddle/fluid/operators/interpolate_v2_op.h

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -783,12 +783,13 @@ static void Interpolate1DCPUFwd(const framework::ExecutionContext& ctx,
783783

784784
int out_w = ctx.Attr<int>("out_w");
785785
auto list_new_size_tensor = ctx.MultiInput<framework::Tensor>("SizeTensor");
786+
float scale_w = -1.;
786787
if (list_new_size_tensor.size() > 0) {
787788
// have size tensor
788789
auto new_size = get_new_shape(list_new_size_tensor);
789790
out_w = new_size[0];
790791
} else {
791-
float scale_w = -1;
792+
// float scale_w = -1;
792793
auto scale_tensor = ctx.Input<Tensor>("Scale");
793794
auto scale = ctx.Attr<std::vector<float>>("scale");
794795
if (scale_tensor != nullptr) {
@@ -833,8 +834,11 @@ static void Interpolate1DCPUFwd(const framework::ExecutionContext& ctx,
833834

834835
float ratio_w = 0.f;
835836
if (out_w > 1) {
837+
float new_scale_w = 0.f;
838+
new_scale_w = (scale_w > 0) ? static_cast<float>(1. / scale_w)
839+
: static_cast<float>(in_w) / out_w;
836840
ratio_w = (align_corners) ? static_cast<float>(in_w - 1) / (out_w - 1)
837-
: static_cast<float>(in_w) / out_w;
841+
: static_cast<float>(new_scale_w);
838842
}
839843
if ("linear" == interp_method) {
840844
LinearInterpolation<T>(input, output, ratio_w, in_w, n, c, out_w,
@@ -856,6 +860,8 @@ static void Interpolate2DCPUFwd(const framework::ExecutionContext& ctx,
856860

857861
int out_h = ctx.Attr<int>("out_h");
858862
int out_w = ctx.Attr<int>("out_w");
863+
float scale_h = -1;
864+
float scale_w = -1;
859865

860866
auto list_new_size_tensor = ctx.MultiInput<framework::Tensor>("SizeTensor");
861867
if (list_new_size_tensor.size() > 0) {
@@ -864,8 +870,6 @@ static void Interpolate2DCPUFwd(const framework::ExecutionContext& ctx,
864870
out_h = new_size[0];
865871
out_w = new_size[1];
866872
} else {
867-
float scale_h = -1;
868-
float scale_w = -1;
869873
auto scale_tensor = ctx.Input<Tensor>("Scale");
870874
auto scale = ctx.Attr<std::vector<float>>("scale");
871875
if (scale_tensor != nullptr) {
@@ -925,12 +929,18 @@ static void Interpolate2DCPUFwd(const framework::ExecutionContext& ctx,
925929
float ratio_h = 0.f;
926930
float ratio_w = 0.f;
927931
if (out_h > 1) {
932+
float new_scale_h = 0.f;
933+
new_scale_h = (scale_h > 0) ? static_cast<float>(1. / scale_h)
934+
: static_cast<float>(in_h) / out_h;
928935
ratio_h = (align_corners) ? static_cast<float>(in_h - 1) / (out_h - 1)
929-
: static_cast<float>(in_h) / out_h;
936+
: static_cast<float>(new_scale_h);
930937
}
931938
if (out_w > 1) {
939+
float new_scale_w = 0.f;
940+
new_scale_w = (scale_w > 0) ? static_cast<float>(1. / scale_w)
941+
: static_cast<float>(in_w) / out_w;
932942
ratio_w = (align_corners) ? static_cast<float>(in_w - 1) / (out_w - 1)
933-
: static_cast<float>(in_w) / out_w;
943+
: static_cast<float>(new_scale_w);
934944
}
935945

936946
if ("bilinear" == interp_method) {
@@ -962,6 +972,10 @@ static void Interpolate3DCPUFwd(const framework::ExecutionContext& ctx,
962972
int out_h = ctx.Attr<int>("out_h");
963973
int out_w = ctx.Attr<int>("out_w");
964974

975+
float scale_d = -1;
976+
float scale_h = -1;
977+
float scale_w = -1;
978+
965979
auto list_new_size_tensor = ctx.MultiInput<framework::Tensor>("SizeTensor");
966980
if (list_new_size_tensor.size() > 0) {
967981
// have size tensor
@@ -970,9 +984,6 @@ static void Interpolate3DCPUFwd(const framework::ExecutionContext& ctx,
970984
out_h = new_size[1];
971985
out_w = new_size[2];
972986
} else {
973-
float scale_d = -1;
974-
float scale_h = -1;
975-
float scale_w = -1;
976987
auto scale_tensor = ctx.Input<Tensor>("Scale");
977988
auto scale = ctx.Attr<std::vector<float>>("scale");
978989
if (scale_tensor != nullptr) {
@@ -1043,16 +1054,25 @@ static void Interpolate3DCPUFwd(const framework::ExecutionContext& ctx,
10431054
float ratio_h = 0.f;
10441055
float ratio_w = 0.f;
10451056
if (out_d > 1) {
1057+
float new_scale_d = 0.f;
1058+
new_scale_d = (scale_d > 0) ? static_cast<float>(1. / scale_d)
1059+
: static_cast<float>(in_d) / out_d;
10461060
ratio_d = (align_corners) ? static_cast<float>(in_d - 1) / (out_d - 1)
1047-
: static_cast<float>(in_d) / out_d;
1061+
: static_cast<float>(new_scale_d);
10481062
}
10491063
if (out_h > 1) {
1064+
float new_scale_h = 0.f;
1065+
new_scale_h = (scale_h > 0) ? static_cast<float>(1. / scale_h)
1066+
: static_cast<float>(in_h) / out_h;
10501067
ratio_h = (align_corners) ? static_cast<float>(in_h - 1) / (out_h - 1)
1051-
: static_cast<float>(in_h) / out_h;
1068+
: static_cast<float>(new_scale_h);
10521069
}
10531070
if (out_w > 1) {
1071+
float new_scale_w = 0.f;
1072+
new_scale_w = (scale_w > 0) ? static_cast<float>(1. / scale_w)
1073+
: static_cast<float>(in_w) / out_w;
10541074
ratio_w = (align_corners) ? static_cast<float>(in_w - 1) / (out_w - 1)
1055-
: static_cast<float>(in_w) / out_w;
1075+
: static_cast<float>(new_scale_w);
10561076
}
10571077

10581078
if ("trilinear" == interp_method) {
@@ -1127,8 +1147,11 @@ static void Interpolate1DCPUBwd(const framework::ExecutionContext& ctx,
11271147

11281148
float ratio_w = 0.f;
11291149
if (out_w > 1) {
1150+
float new_scale_w = 0.f;
1151+
new_scale_w = (scale_w > 0) ? static_cast<float>(1. / scale_w)
1152+
: static_cast<float>(in_w) / out_w;
11301153
ratio_w = (align_corners) ? static_cast<float>(in_w - 1) / (out_w - 1)
1131-
: static_cast<float>(in_w) / out_w;
1154+
: static_cast<float>(new_scale_w);
11321155
}
11331156
if ("linear" == interp_method) {
11341157
LinearInterpolationGrad<T>(output_grad, input_grad, ratio_w, in_w, n, c,
@@ -1216,12 +1239,18 @@ static void Interpolate2DCPUBwd(const framework::ExecutionContext& ctx,
12161239
float ratio_h = 0.f;
12171240
float ratio_w = 0.f;
12181241
if (out_h > 1) {
1242+
float new_scale_h = 0.f;
1243+
new_scale_h = (scale_h > 0) ? static_cast<float>(1. / scale_h)
1244+
: static_cast<float>(in_h) / out_h;
12191245
ratio_h = (align_corners) ? static_cast<float>(in_h - 1) / (out_h - 1)
1220-
: static_cast<float>(in_h) / out_h;
1246+
: static_cast<float>(new_scale_h);
12211247
}
12221248
if (out_w > 1) {
1249+
float new_scale_w = 0.f;
1250+
new_scale_w = (scale_w > 0) ? static_cast<float>(1. / scale_w)
1251+
: static_cast<float>(in_w) / out_w;
12231252
ratio_w = (align_corners) ? static_cast<float>(in_w - 1) / (out_w - 1)
1224-
: static_cast<float>(in_w) / out_w;
1253+
: static_cast<float>(new_scale_w);
12251254
}
12261255

12271256
if ("bilinear" == interp_method) {
@@ -1327,16 +1356,25 @@ static void Interpolate3DCPUBwd(const framework::ExecutionContext& ctx,
13271356
float ratio_h = 0.f;
13281357
float ratio_w = 0.f;
13291358
if (out_d > 1) {
1359+
float new_scale_d = 0.f;
1360+
new_scale_d = (scale_d > 0) ? static_cast<float>(1. / scale_d)
1361+
: static_cast<float>(in_d) / out_d;
13301362
ratio_d = (align_corners) ? static_cast<float>(in_d - 1) / (out_d - 1)
1331-
: static_cast<float>(in_d) / out_d;
1363+
: static_cast<float>(new_scale_d);
13321364
}
13331365
if (out_h > 1) {
1366+
float new_scale_h = 0.f;
1367+
new_scale_h = (scale_h > 0) ? static_cast<float>(1. / scale_h)
1368+
: static_cast<float>(in_h) / out_h;
13341369
ratio_h = (align_corners) ? static_cast<float>(in_h - 1) / (out_h - 1)
1335-
: static_cast<float>(in_h) / out_h;
1370+
: static_cast<float>(new_scale_h);
13361371
}
13371372
if (out_w > 1) {
1373+
float new_scale_w = 0.f;
1374+
new_scale_w = (scale_w > 0) ? static_cast<float>(1. / scale_w)
1375+
: static_cast<float>(in_w) / out_w;
13381376
ratio_w = (align_corners) ? static_cast<float>(in_w - 1) / (out_w - 1)
1339-
: static_cast<float>(in_w) / out_w;
1377+
: static_cast<float>(new_scale_w);
13401378
}
13411379

13421380
if ("trilinear" == interp_method) {

0 commit comments

Comments
 (0)