interpolate_v2_op.cc 30.9 KB
Newer Older
X
xiaoting 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
   http://www.apache.org/licenses/LICENSE-2.0
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License. */

#include <memory>
#include <string>
#include <vector>
15 16

#include "paddle/fluid/framework/infershape_utils.h"
X
xiaoting 已提交
17
#include "paddle/fluid/framework/op_registry.h"
18 19 20
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/multiary.h"

21 22 23
#ifdef PADDLE_WITH_MKLDNN
#include "paddle/fluid/platform/mkldnn_helper.h"
#endif
X
xiaoting 已提交
24 25 26 27 28 29 30 31 32 33 34

namespace paddle {
namespace operators {

using framework::Tensor;
using DataLayout = framework::DataLayout;

static void Interpolate1DInferShapeCheck(framework::InferShapeContext* ctx) {
  auto dim_x = ctx->GetInputDim("X");
  auto interp_method = ctx->Attrs().Get<std::string>("interp_method");

35 36
  PADDLE_ENFORCE_EQ("linear",
                    interp_method,
X
xiaoting 已提交
37 38 39 40 41 42
                    platform::errors::InvalidArgument(
                        "Interpolation method can only be \"linear\" when"
                        "Input(X) dimension is 3, but got method = %s .",
                        interp_method));
  const DataLayout data_layout = framework::StringToDataLayout(
      ctx->Attrs().Get<std::string>("data_layout"));
43
  for (int i = 0; i < dim_x.size(); ++i) {
44 45
    PADDLE_ENFORCE_NE(dim_x[i],
                      0,
46 47 48
                      platform::errors::InvalidArgument(
                          "The shape of input(x) should be larged "
                          "than 0, bug received shape[%d] is %d ",
49 50
                          i,
                          dim_x[i]));
51
  }
X
xiaoting 已提交
52 53 54 55
  if (ctx->HasInputs("SizeTensor")) {
    // top prority size
    auto inputs_name = ctx->Inputs("SizeTensor");
    PADDLE_ENFORCE_EQ(
56 57
        inputs_name.size(),
        1,
X
xiaoting 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
        platform::errors::InvalidArgument(
            "Input(SizeTensor)'size of Op(interpolate) must be 1. "
            "Attr(out_shape)'s length must be 1 for 3-D input tensor, but got "
            "size = %d .",
            inputs_name.size()));
    int out_w = ctx->Attrs().Get<int>("out_w");
    framework::DDim dim_out;
    if (data_layout == DataLayout::kNCHW) {
      dim_out = {dim_x[0], dim_x[1], out_w};
    } else {
      dim_out = {dim_x[0], out_w, dim_x[2]};
    }
    ctx->SetOutputDim("Out", dim_out);

    return;
  }

  int out_w;
  if (ctx->HasInput("Scale")) {
    auto scale_tensor = ctx->GetInputDim("Scale");
    PADDLE_ENFORCE_EQ(
79 80
        scale_tensor.size(),
        1,
X
xiaoting 已提交
81 82 83 84
        platform::errors::InvalidArgument(
            "Scale's dimension size must be 1, but got dimension = %d .",
            scale_tensor.size()));
    PADDLE_ENFORCE_EQ(
85 86
        scale_tensor[0],
        1,
X
xiaoting 已提交
87 88
        platform::errors::InvalidArgument(
            "Scale's shape must be 1, but got shape = %d .", scale_tensor[0]));
89
    out_w = -1;
X
xiaoting 已提交
90 91 92 93 94
  } else {
    auto scale = ctx->Attrs().Get<std::vector<float>>("scale");
    if (scale.size() > 0) {
      float scale_w = -1;
      scale_w = scale[0];
K
Kqnonrime 已提交
95
      PADDLE_ENFORCE_EQ(
96 97
          scale_w > 0,
          true,
K
Kqnonrime 已提交
98 99 100 101
          platform::errors::InvalidArgument(
              "The scale_w in Attr(scale) of Operator(interpolate) "
              "should be greater than 0, but received value is %d.",
              scale_w));
X
xiaoting 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
      if (scale_w > 0.) {
        // round down
        out_w = (data_layout == DataLayout::kNCHW
                     ? 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 {
      out_w = ctx->Attrs().Get<int>("out_w");
    }
  }

  if (ctx->HasInput("OutSize") && ctx->IsRuntime()) {
    auto out_size_dim = ctx->GetInputDim("OutSize");
    PADDLE_ENFORCE_EQ(
118 119
        out_size_dim.size(),
        1,
X
xiaoting 已提交
120 121 122
        platform::errors::InvalidArgument(
            "OutSize's dimension size must be 1, but got dimention = %d .",
            out_size_dim.size()));
K
Kqnonrime 已提交
123
    PADDLE_ENFORCE_EQ(
124 125
        out_size_dim[0],
        1,
K
Kqnonrime 已提交
126 127 128
        platform::errors::InvalidArgument(
            "OutSize's 0-th dimension's value must be 1, but got value = %d .",
            out_size_dim[0]));
X
xiaoting 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    ctx->ShareLoD("X", "Out");
    return;
  }

  framework::DDim dim_out;
  if (data_layout == DataLayout::kNCHW) {
    dim_out = {dim_x[0], dim_x[1], out_w};
  } else {
    dim_out = {dim_x[0], out_w, dim_x[2]};
  }
  ctx->SetOutputDim("Out", dim_out);
}

static void Interpolate2DInferShapeCheck(framework::InferShapeContext* ctx) {
  auto dim_x = ctx->GetInputDim("X");
  auto interp_method = ctx->Attrs().Get<std::string>("interp_method");

  PADDLE_ENFORCE(
      "bilinear" == interp_method || "nearest" == interp_method ||
          "bicubic" == interp_method,
149 150 151 152
      platform::errors::InvalidArgument(
          "Interpolation method can only be \"bilinear\" or \"nearest\" when "
          "Input(X) dimension is 4, but got method = %s.",
          interp_method));
X
xiaoting 已提交
153 154 155
  const DataLayout data_layout = framework::StringToDataLayout(
      ctx->Attrs().Get<std::string>("data_layout"));

156
  for (int i = 0; i < dim_x.size(); ++i) {
157 158
    PADDLE_ENFORCE_NE(dim_x[i],
                      0,
159 160 161
                      platform::errors::InvalidArgument(
                          "The shape of input(x) should be larged "
                          "than 0, bug received shape[%d] is %d ",
162 163
                          i,
                          dim_x[i]));
164 165
  }

X
xiaoting 已提交
166 167 168 169
  if (ctx->HasInputs("SizeTensor")) {
    // top prority size
    auto inputs_name = ctx->Inputs("SizeTensor");
    PADDLE_ENFORCE_EQ(
170 171
        inputs_name.size(),
        2,
X
xiaoting 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
        platform::errors::InvalidArgument(
            "Input(SizeTensor)'size of Op(interpolate) must be 2. "
            "Attr(out_shape)'s length must be 2 for 4-D input "
            "tensor, but got size = %d .",
            inputs_name.size()));
    int out_h = ctx->Attrs().Get<int>("out_h");
    int out_w = ctx->Attrs().Get<int>("out_w");
    framework::DDim dim_out;
    if (data_layout == DataLayout::kNCHW) {
      dim_out = {dim_x[0], dim_x[1], out_h, out_w};
    } else {
      dim_out = {dim_x[0], out_h, out_w, dim_x[3]};
    }
    ctx->SetOutputDim("Out", dim_out);

    return;
  }

  int out_h, out_w;
  if (ctx->HasInput("Scale")) {
    auto scale_tensor = ctx->GetInputDim("Scale");
    PADDLE_ENFORCE_EQ(
194 195
        scale_tensor.size(),
        1,
X
xiaoting 已提交
196 197 198
        platform::errors::InvalidArgument(
            "Scale's dimension size must be 1, but got dimension = %d .",
            scale_tensor.size()));
199 200
    PADDLE_ENFORCE_EQ(scale_tensor[0] == 2 || scale_tensor[0] == 1,
                      true,
X
xiaoting 已提交
201 202 203
                      platform::errors::InvalidArgument(
                          "Scale's shape must be 2 or 1, but got shape = %d .",
                          scale_tensor[0]));
204 205
    out_h = -1;
    out_w = -1;
X
xiaoting 已提交
206 207 208 209 210 211 212 213
  } else {
    auto scale = ctx->Attrs().Get<std::vector<float>>("scale");
    if (scale.size() > 0) {
      float scale_h = -1;
      float scale_w = -1;
      scale_h = scale[0];
      scale_w = scale[1];
      PADDLE_ENFORCE_EQ(
214 215
          scale_w > 0,
          true,
K
Kqnonrime 已提交
216 217 218 219 220
          platform::errors::InvalidArgument(
              "The scale_w in Attr(scale) of Operator(interpolate) "
              "should be greater than 0, but received value is %d.",
              scale_w));
      PADDLE_ENFORCE_EQ(
221 222
          scale_h > 0,
          true,
K
Kqnonrime 已提交
223 224 225 226
          platform::errors::InvalidArgument(
              "The scale_h in Attr(scale) of Operator(interpolate) "
              "should be greater than 0, but received value is %d.",
              scale_h));
X
xiaoting 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
      if (scale_h > 0. && scale_w > 0.) {
        // round down
        out_h = (data_layout == DataLayout::kNCHW
                     ? 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_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;
      }
    } else {
      out_h = ctx->Attrs().Get<int>("out_h");
      out_w = ctx->Attrs().Get<int>("out_w");
    }
  }

  if (ctx->HasInput("OutSize") && ctx->IsRuntime()) {
    auto out_size_dim = ctx->GetInputDim("OutSize");
    PADDLE_ENFORCE_EQ(
248 249
        out_size_dim.size(),
        1,
X
xiaoting 已提交
250 251 252 253
        platform::errors::InvalidArgument(
            "OutSize's dimension size must be 1, but got dimension = %d .",
            out_size_dim.size()));
    PADDLE_ENFORCE_EQ(
254 255
        out_size_dim[0],
        2,
X
xiaoting 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
        platform::errors::InvalidArgument(
            "OutSize's dim[0] must be 2, but got dimention = %d .",
            out_size_dim[0]));
    ctx->ShareLoD("X", "Out");
    return;
  }

  framework::DDim dim_out;
  if (data_layout == DataLayout::kNCHW) {
    dim_out = {dim_x[0], dim_x[1], out_h, out_w};
  } else {
    dim_out = {dim_x[0], out_h, out_w, dim_x[3]};
  }
  ctx->SetOutputDim("Out", dim_out);
}

static void Interpolate3DInferShapeCheck(framework::InferShapeContext* ctx) {
  auto dim_x = ctx->GetInputDim("X");
  auto interp_method = ctx->Attrs().Get<std::string>("interp_method");

276 277 278 279 280 281
  PADDLE_ENFORCE("nearest" == interp_method || "trilinear" == interp_method,
                 platform::errors::InvalidArgument(
                     "Interpolation method can only be \"trilinear\" or "
                     "\"nearest\" when Input(X) "
                     "dimension is 5, but got method = %s .",
                     interp_method));
X
xiaoting 已提交
282 283 284
  const DataLayout data_layout = framework::StringToDataLayout(
      ctx->Attrs().Get<std::string>("data_layout"));

285
  for (int i = 0; i < dim_x.size(); ++i) {
286 287
    PADDLE_ENFORCE_NE(dim_x[i],
                      0,
288 289 290
                      platform::errors::InvalidArgument(
                          "The shape of input(x) should be larged "
                          "than 0, bug received shape[%d] is %d ",
291 292
                          i,
                          dim_x[i]));
293 294
  }

X
xiaoting 已提交
295 296 297 298
  if (ctx->HasInputs("SizeTensor")) {
    // top prority size
    auto inputs_name = ctx->Inputs("SizeTensor");
    PADDLE_ENFORCE_EQ(
299 300
        inputs_name.size(),
        3,
X
xiaoting 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
        platform::errors::InvalidArgument(
            "Input(SizeTensor)'s size of Op(interpolate) must be 3. "
            "Attr(out_shape)'s length must be 3 for 5-D input "
            "tensor, but got size = %d .",
            inputs_name.size()));
    int out_d = ctx->Attrs().Get<int>("out_d");
    int out_h = ctx->Attrs().Get<int>("out_h");
    int out_w = ctx->Attrs().Get<int>("out_w");
    framework::DDim dim_out;
    if (data_layout == DataLayout::kNCHW) {
      dim_out = {dim_x[0], dim_x[1], out_d, out_h, out_w};
    } else {
      dim_out = {dim_x[0], out_d, out_h, out_w, dim_x[4]};
    }
    ctx->SetOutputDim("Out", dim_out);

    return;
  }

  int out_d, out_h, out_w;
  if (ctx->HasInput("Scale")) {
    auto scale_tensor = ctx->GetInputDim("Scale");
    PADDLE_ENFORCE_EQ(
324 325
        scale_tensor.size(),
        1,
X
xiaoting 已提交
326 327 328
        platform::errors::InvalidArgument(
            "Scale's dimension size must be 1, but got size = %d .",
            scale_tensor.size()));
329 330
    PADDLE_ENFORCE_EQ(scale_tensor[0] == 3 || scale_tensor[0] == 1,
                      true,
X
xiaoting 已提交
331 332 333
                      platform::errors::InvalidArgument(
                          "Scale's shape must be 3 or 1, but got shape = %d .",
                          scale_tensor[0]));
334 335 336
    out_d = -1;
    out_h = -1;
    out_w = -1;
X
xiaoting 已提交
337 338 339 340 341 342 343 344 345 346
  } else {
    auto scale = ctx->Attrs().Get<std::vector<float>>("scale");
    if (scale.size() > 0) {
      float scale_d = -1;
      float scale_h = -1;
      float scale_w = -1;
      scale_d = scale[0];
      scale_h = scale[1];
      scale_w = scale[2];
      PADDLE_ENFORCE_EQ(
347 348
          scale_w > 0,
          true,
K
Kqnonrime 已提交
349 350 351 352 353
          platform::errors::InvalidArgument(
              "The scale_w in Attr(scale) of Operator(interpolate) "
              "should be greater than 0, but received value is %d.",
              scale_w));
      PADDLE_ENFORCE_EQ(
354 355
          scale_h > 0,
          true,
K
Kqnonrime 已提交
356 357 358 359 360
          platform::errors::InvalidArgument(
              "The scale_h in Attr(scale) of Operator(interpolate) "
              "should be greater than 0, but received value is %d.",
              scale_h));
      PADDLE_ENFORCE_EQ(
361 362
          scale_d > 0,
          true,
K
Kqnonrime 已提交
363 364 365 366
          platform::errors::InvalidArgument(
              "The scale_d in Attr(scale) of Operator(interpolate) "
              "should be greater than 0, but received value is %d.",
              scale_d));
X
xiaoting 已提交
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
      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_d)
                     : static_cast<int>(dim_x[1] * scale_d));
        out_h = (data_layout == DataLayout::kNCHW
                     ? 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_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;
        out_w = out_w > 0 ? out_w : -1;
      }
    } else {
      out_d = ctx->Attrs().Get<int>("out_d");
      out_h = ctx->Attrs().Get<int>("out_h");
      out_w = ctx->Attrs().Get<int>("out_w");
    }
  }

  if (ctx->HasInput("OutSize") && ctx->IsRuntime()) {
    auto out_size_dim = ctx->GetInputDim("OutSize");
392
    PADDLE_ENFORCE_EQ(
393 394
        out_size_dim.size(),
        1,
395 396 397
        platform::errors::InvalidArgument(
            "OutSize's dimension size must be 1, but got size is %d.",
            out_size_dim.size()));
398 399
    PADDLE_ENFORCE_EQ(out_size_dim[0],
                      3,
400 401 402
                      platform::errors::InvalidArgument(
                          "OutSize's dim[0] must be 3, but got size is %d.",
                          out_size_dim[0]));
X
xiaoting 已提交
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
    ctx->ShareLoD("X", "Out");
    return;
  }

  framework::DDim dim_out;
  if (data_layout == DataLayout::kNCHW) {
    dim_out = {dim_x[0], dim_x[1], out_d, out_h, out_w};
  } else {
    dim_out = {dim_x[0], out_d, out_h, out_w, dim_x[4]};
  }
  ctx->SetOutputDim("Out", dim_out);
}

class InterpolateV2Op : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
  void InferShape(framework::InferShapeContext* ctx) const override {
422 423
    OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "Interpolate");
    OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "Interpolate");
X
xiaoting 已提交
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446

    auto dim_x = ctx->GetInputDim("X");  // NCHW format
    PADDLE_ENFORCE(
        dim_x.size() == 3 || dim_x.size() == 4 || dim_x.size() == 5,
        platform::errors::Unimplemented(
            "Input(X) dimension must be 3, 4 or 5, but got dimension = %d .",
            dim_x.size()));

    if (dim_x.size() == 3) {
      // shape check for 1D interpolate for input tensor shape NCHW
      Interpolate1DInferShapeCheck(ctx);
    } else if (dim_x.size() == 4) {
      // shape check for 2D interpolate for input tensor shape NCHW
      Interpolate2DInferShapeCheck(ctx);
    } else {  // dim_x.size() == 5
      // shape check for 3D interpolate for input tensor shape NCDHW
      Interpolate3DInferShapeCheck(ctx);
    }
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
447 448 449 450 451
    framework::DataLayout layout = framework::DataLayout::kAnyLayout;
    framework::LibraryType library = framework::LibraryType::kPlain;
    auto data_type = OperatorWithKernel::IndicateVarDataType(ctx, "X");

#ifdef PADDLE_WITH_MKLDNN
452
    const auto& interp_method = ctx.Attr<std::string>("interp_method");
453 454 455 456 457 458 459 460 461
    // TODO(danqing): support other interp_method
    if (this->CanMKLDNNBeUsed(ctx, data_type) &&
        (interp_method == "nearest" || interp_method == "bilinear")) {
      layout = framework::DataLayout::kMKLDNN;
      library = framework::LibraryType::kMKLDNN;
    }
#endif

    return framework::OpKernelType(data_type, ctx.GetPlace(), layout, library);
X
xiaoting 已提交
462 463 464
  }

  framework::OpKernelType GetKernelTypeForVar(
465 466
      const std::string& var_name,
      const Tensor& tensor,
X
xiaoting 已提交
467
      const framework::OpKernelType& expected_kernel_type) const override {
468 469 470 471 472 473 474 475 476 477
#ifdef PADDLE_WITH_MKLDNN
    if ((expected_kernel_type.data_layout_ == framework::DataLayout::kMKLDNN) &&
        (tensor.layout() != framework::DataLayout::kMKLDNN)) {
      auto attrs = Attrs();
      auto ar = paddle::framework::AttrReader(attrs);
      const std::string data_format = ar.Get<std::string>("data_layout");
      auto dl = framework::StringToDataLayout(data_format);
      // Some models may have intentionally set "AnyLayout" for pool
      // op. Treat this as NCHW (default data_format value)
      if (dl != framework::DataLayout::kAnyLayout) {
478 479
        return framework::OpKernelType(
            expected_kernel_type.data_type_, tensor.place(), dl);
480 481 482
      }
    }
#endif
X
xiaoting 已提交
483 484 485
    if (var_name == "SizeTensor" || var_name == "Scale") {
      return expected_kernel_type;
    }
486 487
    return framework::OpKernelType(
        expected_kernel_type.data_type_, tensor.place(), tensor.layout());
X
xiaoting 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
  }
};

class InterpolateV2OpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("X",
             "The input tensor of interpolate operator, "
             "This is a 4-D tensor with shape of [N, C, H, W] or a "
             "5-D tensor with shape of [N, C, D, H, W].");
    AddInput("OutSize",
             "This is a 1-D tensor with two numbers to specify output size. "
             "It should be [output_height, output_width] when input is a 4-D "
             "tensor and should be [output_depth, output_height, output_width] "
             "when input is a 5-D tensor. It has a higher priority than "
             "the attr(out_d), attr(out_h), attr(out_w) and attr(scale).")
        .AsDispensable();
    AddInput("SizeTensor",
             "(vector<Tensor<int32>>, optional). If provided, interpolate will "
             "use this. The shape of the tensor in vector MUST BE [1]. "
             "It has the highest priority compare with Input(OutSize) and "
             "attr(out_d), attr(out_h), attr(out_w) and attr(scale).")
        .AsDuplicable()
        .AsDispensable();
    AddInput("Scale",
             "This is a 1-D tensor with one number to specify output scale. "
             "It has the higher priority compare with attr(scale).")
        .AsDispensable();
    AddOutput("Out",
              "The output tensor of interpolate operator, "
              "This is a tensor in same rank with Input(X).");

    AddAttr<std::string>(
        "data_layout",
        "(string, default NCHW) Only used in "
        "an optional string from: \"NHWC\", \"NCHW\". "
        "Specify that the data format of the input and output data is "
        "channel_first or channel_last.")
        .SetDefault("NCHW");
    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<std::vector<float>>("scale", "scale_d factor of interpolate op.")
        .SetDefault(std::vector<float>{});
    AddAttr<std::string>("interp_method",
                         "(string, default \"bilinear\"), interpolation "
                         "method, can be \"linear\" for linear interpolation"
                         ",\"bilinear\" for "
                         "bilinear interpolation, \"trilinear\" for trilinear "
                         "interpolation and \"nearest\" for nearest "
                         "neighbor interpolation, and \"bicubic\" for bicubic"
                         "interpolation.")
        .SetDefault("bilinear");
    AddAttr<bool>(
        "align_corners",
        "an optional bool. Defaults to True. "
        "If True, the centers of 4 corner pixels of the input and output "
        "tensors are aligned, preserving the values at the corner pixels, "
        "If False, are not aligned")
        .SetDefault(true);
    AddAttr<int>("align_mode",
                 "(int, default \'1\'), optional for bilinear interpolation, "
                 "can be \'0\' for src_idx = scale*(dst_indx+0.5)-0.5 , "
                 "can be \'1\' for src_idx = scale*dst_index .")
        .SetDefault(1);
    AddComment(R"DOC(
          This operator samples input X to given output shape by using specified
          interpolation method, the interpolation methods can be \"nearest\"
556
          for nearest neighbor interpolation and \"bilinear\" for bilinear
X
xiaoting 已提交
557 558 559
          interpolation and \"linear\" for linear interpolation..

          Nearest neighbor interpolation is to perform nearest neighbor interpolation
560
          in both the 3rd dimension(in height direction) and the 4th dimension(in width
X
xiaoting 已提交
561
          direction) on input tensor.
562 563 564 565 566 567 568 569

          Linear interpolation is the method of using a line connecting two known quantities
          to determine the value of an unknown quantity between the two known quantities.

          Bilinear interpolation is an extension of linear interpolation for
          interpolating functions of two variables (e.g. H-direction and
          W-direction in this op) on a rectilinear 2D grid. The key idea is
          to perform linear interpolation first in one direction, and then
X
xiaoting 已提交
570 571
          again in the other direction.

572 573 574
          Trilinear interpolation is an extension of linear interpolation for
          interpolating functions of three variables (e.g. D-direction,
          H-direction and W-direction in this op) on a rectilinear 3D grid.
X
xiaoting 已提交
575 576 577 578 579 580 581
          The linear interpolation is performed on three directions.

          Bicubic interpolation is an extension of cubic interpolation for interpolating
          data points on a two-dimensional regular grid. The interpolated surface is
          smoother than corresponding surfaces obtained by bilinear interpolation or
          nearest-neighbor interpolation.

582
          Align_corners and align_mode are optional parameters,the calculation method
X
xiaoting 已提交
583
          of interpolation can be selected by them.
584

X
xiaoting 已提交
585 586 587
          Example:

          For scale:
588

X
xiaoting 已提交
589 590 591
            if align_corners = True and out_{size}>1 :

              scale_{factor} = (in_{size}-1.0)/(out_{size}-1.0)
592

X
xiaoting 已提交
593
            else:
594

X
xiaoting 已提交
595
              scale_{factor} = float(in_{size}/out_{size})
596 597


X
xiaoting 已提交
598
          Nearest neighbor interpolation:
599

X
xiaoting 已提交
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
          if:
              align_corners = False

              input : (N,C,H_in,W_in)
              output: (N,C,H_out,W_out) where:

              H_out = \left \lfloor {H_{in} * scale_{}factor}} \right \rfloor
              W_out = \left \lfloor {W_{in} * scale_{}factor}} \right \rfloor

          else:
              align_corners = True

              input : (N,C,H_in,W_in)
              output: (N,C,H_out,W_out) where:

              H_out = round(H_{in} * scale_{factor})
              W_out = round(W_{in} * scale_{factor})

          Bilinear interpolation:

          if:
              align_corners = False , align_mode = 0
622

X
xiaoting 已提交
623 624
              input : (N,C,H_in,W_in)
              output: (N,C,H_out,W_out) where:
625

X
xiaoting 已提交
626 627 628 629 630
              H_out = (H_{in}+0.5) * scale_{factor} - 0.5
              W_out = (W_{in}+0.5) * scale_{factor} - 0.5


          else:
631

X
xiaoting 已提交
632 633 634 635 636 637 638 639 640 641
              input : (N,C,H_in,W_in)
              output: (N,C,H_out,W_out) where:

              H_out = H_{in} * scale_{factor}
              W_out = W_{in} * scale_{factor}

          Trilinear interpolation:

          if:
              align_corners = False , align_mode = 0
642

X
xiaoting 已提交
643 644
              input : (N,C,D_in,H_in,W_in)
              output: (N,C,D_out,H_out,W_out) where:
645

X
xiaoting 已提交
646 647 648 649 650 651
              D_out = (D_{in}+0.5) * scale_{factor} - 0.5
              H_out = (H_{in}+0.5) * scale_{factor} - 0.5
              W_out = (W_{in}+0.5) * scale_{factor} - 0.5


          else:
652

X
xiaoting 已提交
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
              input : (N,C,D_in,H_in,W_in)
              output: (N,C,D_out,H_out,W_out) where:

              D_out = D_{in} * scale_{factor}
              H_out = H_{in} * scale_{factor}
              W_out = W_{in} * scale_{factor}

          Bicubic interpolation:

          if:
              align_corners = False
              input : (N,C,H_in,W_in)
              output: (N,C,H_out,W_out) where:
              H_out = (H_{in}+0.5) * scale_{factor} - 0.5
              W_out = (W_{in}+0.5) * scale_{factor} - 0.5
          else:
              input : (N,C,H_in,W_in)
              output: (N,C,H_out,W_out) where:
              H_out = H_{in} * scale_{factor}
              W_out = W_{in} * scale_{factor}

674
          For details of nearest neighbor interpolation, please refer to Wikipedia:
X
xiaoting 已提交
675 676
          https://en.wikipedia.org/wiki/Nearest-neighbor_interpolation

677
          For details of bilinear interpolation, please refer to Wikipedia:
X
xiaoting 已提交
678 679
          https://en.wikipedia.org/wiki/Bilinear_interp_v2olation

680
          For details of trilinear interpolation, please refer to Wikipedia:
X
xiaoting 已提交
681 682 683 684 685 686 687 688 689 690 691 692 693 694
          https://en.wikipedia.org/wiki/Trilinear_interp_v2olation

          For details of bicubic interpolation, please refer to Wikipedia:
          https://en.wikipedia.org/wiki/Bicubic_interpolation
         )DOC");
  }
};

class InterpolateV2OpGrad : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
  void InferShape(framework::InferShapeContext* ctx) const override {
695
    OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "InterpolateGrad");
696 697 698 699
    OP_INOUT_CHECK(ctx->HasInput(framework::GradVarName("Out")),
                   "Input",
                   "Out@GRAD",
                   "InterpolateGrad");
700

X
xiaoting 已提交
701 702 703 704 705 706 707 708 709 710 711 712 713 714
    auto dim_x = ctx->GetInputDim("X");
    if (ctx->HasOutput(framework::GradVarName("X"))) {
      ctx->SetOutputDim(framework::GradVarName("X"), dim_x);
    }
  }

  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(OperatorWithKernel::IndicateVarDataType(
                                       ctx, framework::GradVarName("Out")),
                                   ctx.GetPlace());
  }

  framework::OpKernelType GetKernelTypeForVar(
715 716
      const std::string& var_name,
      const Tensor& tensor,
X
xiaoting 已提交
717 718 719 720
      const framework::OpKernelType& expected_kernel_type) const override {
    if (var_name == "SizeTensor" || var_name == "Scale") {
      return expected_kernel_type;
    }
721 722
    return framework::OpKernelType(
        expected_kernel_type.data_type_, tensor.place(), tensor.layout());
X
xiaoting 已提交
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
  }
};

template <typename T>
class InterpolateV2GradMaker : public framework::SingleGradOpMaker<T> {
 public:
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;

 protected:
  void Apply(GradOpPtr<T> op) const override {
    op->SetType(this->ForwardOpType() + "_grad");
    op->SetInput("X", this->Input("X"));
    if (this->HasInput("SizeTensor") > 0) {
      op->SetInput("SizeTensor", this->Input("SizeTensor"));
    }
    if (this->HasInput("OutSize") > 0) {
      op->SetInput("OutSize", this->Input("OutSize"));
    }
    if (this->HasInput("Scale") > 0) {
      op->SetInput("Scale", this->Input("Scale"));
    }
    op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
    op->SetOutput(framework::GradVarName("X"), this->InputGrad("X"));
    op->SetAttrMap(this->Attrs());
  }
};

DECLARE_NO_NEED_BUFFER_VARS_INFERER(InterpolateV2GradNoNeedBufferVarsInferer,
                                    "X");

}  // namespace operators
}  // namespace paddle

756 757 758
// interp_v2 support scale_factor whose input type is list, this operation is
// not
// compatible with interp_op, so a new one is added in paddle2.0
X
xiaoting 已提交
759
namespace ops = paddle::operators;
760

761 762
DECLARE_INFER_SHAPE_FUNCTOR(bilinear_interp_v2,
                            BilinearInterpInferShapeFunctor,
763
                            PD_INFER_META(phi::InterpolateInferMeta));
764 765
DECLARE_INFER_SHAPE_FUNCTOR(nearest_interp_v2,
                            NearestInterpInferShapeFunctor,
766 767 768 769
                            PD_INFER_META(phi::InterpolateInferMeta));
DECLARE_INFER_SHAPE_FUNCTOR(trilinear_interp_v2,
                            TrilinearInterpInferShapeFunctor,
                            PD_INFER_META(phi::InterpolateInferMeta));
770 771
DECLARE_INFER_SHAPE_FUNCTOR(bicubic_interp_v2,
                            BicubicInterpInferShapeFunctor,
772
                            PD_INFER_META(phi::InterpolateInferMeta));
773 774
DECLARE_INFER_SHAPE_FUNCTOR(linear_interp_v2,
                            LinearInterpInferShapeFunctor,
775 776
                            PD_INFER_META(phi::InterpolateInferMeta));

777 778
REGISTER_OPERATOR(bilinear_interp_v2,
                  ops::InterpolateV2Op,
X
xiaoting 已提交
779 780
                  ops::InterpolateV2OpMaker,
                  ops::InterpolateV2GradMaker<paddle::framework::OpDesc>,
781 782
                  ops::InterpolateV2GradMaker<paddle::imperative::OpBase>,
                  BilinearInterpInferShapeFunctor);
783 784
REGISTER_OPERATOR(bilinear_interp_v2_grad,
                  ops::InterpolateV2OpGrad,
X
xiaoting 已提交
785
                  ops::InterpolateV2GradNoNeedBufferVarsInferer);
786 787
REGISTER_OPERATOR(nearest_interp_v2,
                  ops::InterpolateV2Op,
X
xiaoting 已提交
788 789
                  ops::InterpolateV2OpMaker,
                  ops::InterpolateV2GradMaker<paddle::framework::OpDesc>,
790 791
                  ops::InterpolateV2GradMaker<paddle::imperative::OpBase>,
                  NearestInterpInferShapeFunctor);
792 793
REGISTER_OPERATOR(nearest_interp_v2_grad,
                  ops::InterpolateV2OpGrad,
X
xiaoting 已提交
794
                  ops::InterpolateV2GradNoNeedBufferVarsInferer);
795 796
REGISTER_OPERATOR(trilinear_interp_v2,
                  ops::InterpolateV2Op,
X
xiaoting 已提交
797 798
                  ops::InterpolateV2OpMaker,
                  ops::InterpolateV2GradMaker<paddle::framework::OpDesc>,
799 800
                  ops::InterpolateV2GradMaker<paddle::imperative::OpBase>,
                  TrilinearInterpInferShapeFunctor);
801 802
REGISTER_OPERATOR(trilinear_interp_v2_grad,
                  ops::InterpolateV2OpGrad,
X
xiaoting 已提交
803
                  ops::InterpolateV2GradNoNeedBufferVarsInferer);
804 805
REGISTER_OPERATOR(bicubic_interp_v2,
                  ops::InterpolateV2Op,
X
xiaoting 已提交
806 807
                  ops::InterpolateV2OpMaker,
                  ops::InterpolateV2GradMaker<paddle::framework::OpDesc>,
808 809
                  ops::InterpolateV2GradMaker<paddle::imperative::OpBase>,
                  BicubicInterpInferShapeFunctor);
810 811
REGISTER_OPERATOR(bicubic_interp_v2_grad,
                  ops::InterpolateV2OpGrad,
X
xiaoting 已提交
812
                  ops::InterpolateV2GradNoNeedBufferVarsInferer);
813 814
REGISTER_OPERATOR(linear_interp_v2,
                  ops::InterpolateV2Op,
X
xiaoting 已提交
815 816
                  ops::InterpolateV2OpMaker,
                  ops::InterpolateV2GradMaker<paddle::framework::OpDesc>,
817 818
                  ops::InterpolateV2GradMaker<paddle::imperative::OpBase>,
                  LinearInterpInferShapeFunctor);
819 820
REGISTER_OPERATOR(linear_interp_v2_grad,
                  ops::InterpolateV2OpGrad,
X
xiaoting 已提交
821
                  ops::InterpolateV2GradNoNeedBufferVarsInferer);