upsampling.cc 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

masahi committed
20 21
/*!
 *  Copyright (c) 2017 by Contributors
22 23
 * \file upsampling.cc
 * \brief Property def of upsampling operators.
masahi committed
24
 */
25 26 27 28
#include <tvm/tvm.h>
#include <tvm/expr.h>
#include <nnvm/layout.h>
#include <nnvm/compiler/op_attr_types.h>
masahi committed
29 30 31 32
#include <nnvm/op.h>
#include <nnvm/node.h>
#include <nnvm/op_attr_types.h>
#include <nnvm/top/nn.h>
33
#include "nn_common.h"
masahi committed
34 35
#include "../op_common.h"
#include "../elemwise_op_common.h"
36 37 38
#include "topi/elemwise.h"
#include "topi/transform.h"
#include "topi/nn/upsampling.h"
masahi committed
39 40 41

namespace nnvm {
namespace top {
42 43 44 45
using tvm::Expr;
using tvm::Array;
using tvm::Tensor;
using nnvm::compiler::FTVMCompute;
masahi committed
46 47 48 49

DMLC_REGISTER_PARAMETER(UpSamplingParam);

inline bool UpSamplingInferShape(const nnvm::NodeAttrs& attrs,
50 51
                                 std::vector<TShape>* in_shape,
                                 std::vector<TShape>* out_shape) {
52
  static const Layout kNCHW("NCHW");
masahi committed
53 54 55 56 57
  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;
58

59
  dshape = ConvertLayout(dshape, param.layout, kNCHW);
masahi committed
60 61 62
  TShape oshape = dshape;
  oshape[2] = oshape[2] * param.scale;
  oshape[3] = oshape[3] * param.scale;
63
  oshape = ConvertLayout(oshape, kNCHW, param.layout);
masahi committed
64
  NNVM_ASSIGN_OUTPUT_SHAPE(attrs, *out_shape, 0, oshape);
65

masahi committed
66 67 68
  return true;
}

69 70 71 72 73 74 75 76 77 78 79 80 81
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
82
NNVM_REGISTER_OP(upsampling)
83 84 85 86 87
.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
88

89 90 91 92 93 94
- **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
95 96 97 98 99 100 101 102

)" 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>)
103
.set_attr<FCorrectLayout>("FCorrectLayout", UpsamplingLayout)
masahi committed
104 105
.set_num_outputs(1)
.set_num_inputs(1)
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
.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
122 123 124 125
.set_support_level(2);

}  // namespace top
}  // namespace nnvm