upsampling.cc 3.51 KB
Newer Older
masahi committed
1 2
/*!
 *  Copyright (c) 2017 by Contributors
3 4
 * \file upsampling.cc
 * \brief Property def of upsampling operators.
masahi committed
5
 */
6 7 8 9
#include <tvm/tvm.h>
#include <tvm/expr.h>
#include <nnvm/layout.h>
#include <nnvm/compiler/op_attr_types.h>
masahi committed
10 11 12 13
#include <nnvm/op.h>
#include <nnvm/node.h>
#include <nnvm/op_attr_types.h>
#include <nnvm/top/nn.h>
14
#include "nn_common.h"
masahi committed
15 16
#include "../op_common.h"
#include "../elemwise_op_common.h"
17 18 19
#include "topi/elemwise.h"
#include "topi/transform.h"
#include "topi/nn/upsampling.h"
masahi committed
20 21 22

namespace nnvm {
namespace top {
23 24 25 26
using tvm::Expr;
using tvm::Array;
using tvm::Tensor;
using nnvm::compiler::FTVMCompute;
masahi committed
27 28 29 30

DMLC_REGISTER_PARAMETER(UpSamplingParam);

inline bool UpSamplingInferShape(const nnvm::NodeAttrs& attrs,
31 32
                                 std::vector<TShape>* in_shape,
                                 std::vector<TShape>* out_shape) {
33
  static const Layout kNCHW("NCHW");
masahi committed
34 35 36 37 38
  const UpSamplingParam& param = nnvm::get<UpSamplingParam>(attrs.parsed);
  CHECK_EQ(in_shape->size(), 1U);
  CHECK_EQ(out_shape->size(), 1U);
  TShape dshape = (*in_shape)[0];
  if (dshape.ndim() ==  0) return false;
39

40
  dshape = ConvertLayout(dshape, param.layout, kNCHW);
masahi committed
41 42 43
  TShape oshape = dshape;
  oshape[2] = oshape[2] * param.scale;
  oshape[3] = oshape[3] * param.scale;
44
  oshape = ConvertLayout(oshape, kNCHW, param.layout);
masahi committed
45
  NNVM_ASSIGN_OUTPUT_SHAPE(attrs, *out_shape, 0, oshape);
46

masahi committed
47 48 49
  return true;
}

50 51 52 53 54 55 56 57 58 59 60 61 62
inline bool UpsamplingLayout(const NodeAttrs& attrs,
                             std::vector<Layout> *in_layouts,
                             const std::vector<Layout> *last_in_layouts,
                             std::vector<Layout> *out_layouts) {
  const UpSamplingParam& param = nnvm::get<UpSamplingParam>(attrs.parsed);
  CHECK_EQ(in_layouts->size(), 1U);
  CHECK_EQ(out_layouts->size(), 1U);
  const Layout layout(param.layout);
  NNVM_ASSIGN_LAYOUT(*in_layouts, 0, layout);
  NNVM_ASSIGN_LAYOUT(*out_layouts, 0, layout);
  return true;
}

masahi committed
63
NNVM_REGISTER_OP(upsampling)
64 65 66 67 68
.describe(R"(Perform upsampling to input array with nearest neighbour or bilinear interpolation.

- **data**: data is 4D array of shape
            (batch_size, channels, in_height, in_width) for NCHW
            (batch_size, in_height, in_width, channels) for NHWC
masahi committed
69

70 71 72 73 74 75
- **out**: Output is 4D array of shape
           for layout NCHW
           (batch_size, channels, in_height*scale, in_width*scale)

           for layout NHWC
           (batch_size, in_height*scale, in_width*scale, channels)
masahi committed
76 77 78 79 80 81 82 83

)" NNVM_ADD_FILELINE)
.add_argument("data", "4D Tensor", "Input data.")
.add_arguments(UpSamplingParam::__FIELDS__())
.set_attr_parser(ParamParser<UpSamplingParam>)
.set_attr<FGetAttrDict>("FGetAttrDict", ParamGetAttrDict<UpSamplingParam>)
.set_attr<FInferShape>("FInferShape", UpSamplingInferShape)
.set_attr<FInferType>("FInferType", ElemwiseType<1, 1>)
84
.set_attr<FCorrectLayout>("FCorrectLayout", UpsamplingLayout)
masahi committed
85 86
.set_num_outputs(1)
.set_num_inputs(1)
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
.set_attr<FTVMCompute>(
  "FTVMCompute", [](const NodeAttrs& attrs,
                    const Array<Tensor>& inputs,
                    const Array<Tensor>& out_info) {
  const UpSamplingParam& param = nnvm::get<UpSamplingParam>(attrs.parsed);
  Array<Expr> oshape;
  if (param.layout == "NCHW") {
    oshape.push_back(out_info[0]->shape[2]);
    oshape.push_back(out_info[0]->shape[3]);
  } else {
    oshape.push_back(out_info[0]->shape[1]);
    oshape.push_back(out_info[0]->shape[2]);
  }

  return Array<Tensor>{ topi::nn::upsampling(inputs[0], oshape, param.layout, param.method)};
})
masahi committed
103 104 105 106
.set_support_level(2);

}  // namespace top
}  // namespace nnvm