upsampling.cc 5.6 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * 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
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12 13 14 15 16 17 18 19
 * 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.
 */

20 21 22 23 24
/*!
 *  Copyright (c) 2018 by Contributors
 * \file upsampling.cc
 * \brief upsampling operator
 */
25
#include <tvm/data_layout.h>
26 27
#include <tvm/relay/op.h>
#include <tvm/relay/attrs/nn.h>
28
#include <tvm/relay/op_attr_types.h>
29
#include <tvm/build_module.h>
30 31
#include <topi/elemwise.h>
#include <topi/nn/upsampling.h>
32 33
#include <vector>
#include "../op_common.h"
34 35 36 37 38 39

namespace tvm {
namespace relay {

TVM_REGISTER_NODE_TYPE(UpSamplingAttrs);

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
template <typename T>
Array<Array<Layout> > UpsamplingInferCorrectLayout(
    const Attrs& attrs,
    const Array<Layout>& new_in_layouts,
    const Array<Layout>& old_in_layouts,
    const Array<Array<IndexExpr>> &old_in_shapes) {
  // NOTE: Discard "const" qualifier here.
  T *params = const_cast<T*>(attrs.as<T>());

  if (new_in_layouts.defined()) {
    CHECK_EQ(new_in_layouts.size(), 1);

    Layout raw_layout(params->layout);
    Layout input = new_in_layouts[0];
    if (input.IndexOf(LayoutAxis::Get('W')) == raw_layout.IndexOf(LayoutAxis::Get('W')) &&
      input.IndexOf(LayoutAxis::Get('H')) == raw_layout.IndexOf(LayoutAxis::Get('H')) &&
        !input.Contains(LayoutAxis::Get('w')) && !input.Contains(LayoutAxis::Get('h'))) {
      params->layout = input.name();  // modify self to follow the input layout
    }
  }

  Layout inferred_layout(params->layout);
  return Array<Array<Layout> >{{inferred_layout}, {inferred_layout}};
}

65 66 67 68 69 70 71 72 73 74 75 76 77
bool UpSamplingRel(const Array<Type>& types,
                   int num_inputs,
                   const Attrs& attrs,
                   const TypeReporter& reporter) {
  CHECK_EQ(types.size(), 2);
  const auto* data = types[0].as<TensorTypeNode>();
  if (data == nullptr) return false;

  static const Layout kNCHW("NCHW");

  const UpSamplingAttrs* param = attrs.as<UpSamplingAttrs>();
  CHECK(param != nullptr);
  const Layout in_layout(param->layout);
78 79 80

  auto layout_converter = BijectiveLayoutNode::make(in_layout, kNCHW);
  CHECK(layout_converter.defined())
81 82 83
    << "UpSampling only support input layouts that are convertible from NCHW."
    << " But got " << in_layout;

84
  auto oshape = layout_converter.ForwardShape(data->shape);
85

86 87
  oshape.Set(2, oshape[2] * param->scale);
  oshape.Set(3, oshape[3] * param->scale);
88 89 90

  // assign output type
  reporter->Assign(types[1],
91
                   TensorTypeNode::make(layout_converter.BackwardShape(oshape),
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
                                        data->dtype));
  return true;
}


// Positional relay function to create upsampling operator
// used by frontend FFI.
Expr MakeUpSampling(Expr data,
                    int scale,
                    std::string layout,
                    std::string method) {
  auto attrs = make_node<UpSamplingAttrs>();
  attrs->layout = std::move(layout);
  attrs->method = std::move(method);
  attrs->scale = scale;
  static const Op& op = Op::Get("nn.upsampling");
  return CallNode::make(op, {data}, Attrs(attrs), {});
}


TVM_REGISTER_API("relay.op.nn._make.upsampling")
113
.set_body_typed(MakeUpSampling);
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130


RELAY_REGISTER_OP("nn.upsampling")
.describe(R"code(Perform upsampling on 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

- **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)

)code" TVM_ADD_FILELINE)
131
.set_attrs_type_key("relay.attrs.UpSamplingAttrs")
132 133 134
.set_num_inputs(1)
.add_argument("data", "Tensor", "The input tensor.")
.set_support_level(2)
135
.add_type_rel("UpSampling", UpSamplingRel)
136 137
.set_attr<FInferCorrectLayout>("FInferCorrectLayout",
  UpsamplingInferCorrectLayout<UpSamplingAttrs>)
138
.set_attr<TOpPattern>("TOpPattern", kInjective)
139 140
.set_attr<FTVMCompute>(
  "FTVMCompute", [](const Attrs& attrs,
141 142 143 144 145 146 147
                    const Array<Tensor>& inputs,
                    const Type& out_type,
                    const Target& target) {
    const auto* uattrs = attrs.as<UpSamplingAttrs>();
    CHECK(uattrs != nullptr);
    auto out_tt = out_type.as<TensorTypeNode>();
    CHECK(out_tt) << "expected a tensor type: " << out_type;
148 149 150
    const auto layout = uattrs->layout;
    const auto base_layout = layout.substr(0, 4);
    CHECK(base_layout == "NCHW" || layout == "NHWC")
151 152
      << "unknown layout: " << uattrs->layout;

153
    Array<IndexExpr> oshape;
154
    if (base_layout == "NCHW") {
155 156
      oshape.push_back(out_tt->shape[2]);
      oshape.push_back(out_tt->shape[3]);
157
    } else if (layout == "NHWC") {
158 159 160 161 162 163 164 165 166 167 168
      oshape.push_back(out_tt->shape[1]);
      oshape.push_back(out_tt->shape[2]);
    }

    return Array<Tensor>{
      topi::nn::upsampling(
        inputs[0],
        oshape,
        uattrs->layout,
        uattrs->method)
    };
169
});
170

171

172 173
}  // namespace relay
}  // namespace tvm