convolution.cc 11.4 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
/*!
 * \file Use external nnpack library call.
 */
23
#include <tvm/runtime/device_api.h>
24
#include <tvm/runtime/registry.h>
25
#include <tvm/runtime/data_type.h>
26 27
#include <dmlc/logging.h>
#include <nnpack.h>
28
#include "nnpack_utils.h"
29 30 31 32 33 34

namespace tvm {
namespace contrib {
using namespace runtime;

TVM_REGISTER_GLOBAL("tvm.contrib.nnpack.convolution_inference")
35 36 37 38 39 40 41 42
    .set_body([](TVMArgs args, TVMRetValue *ret) {
      NNPackThreadLocalEntry *entry = NNPackThreadLocalEntry::ThreadLocal();
      static std::once_flag flag;
      std::call_once(flag,
                     []() { CHECK_EQ(nnp_initialize(), nnp_status_success); });
      DLTensor *input = args[0];
      DLTensor *kernel = args[1];
      DLTensor *bias = nullptr;
43
      if (args[2].type_code() == kTVMDLTensorHandle) {
44 45 46 47 48 49 50 51 52
        bias = args[2];
      }
      DLTensor *output = args[3];
      uint64_t pad_top = args[4], pad_right = args[5], pad_bottom = args[6],
               pad_left = args[7];
      nnp_padding input_padding{pad_top, pad_right, pad_bottom, pad_left};
      uint64_t stride_width = args[8], stride_height = args[9];
      nnp_size stride_size{stride_width, stride_height};
      NNPackConfig(args[10]);
53

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
      uint64_t algo_ = args[11];
      nnp_convolution_algorithm algo =
          static_cast<nnp_convolution_algorithm>(algo_);
      CHECK_EQ(input->ndim, 4);
      CHECK_EQ(kernel->ndim, 4);
      if (bias) {
        CHECK_EQ(bias->ndim, 1);
      }
      CHECK_EQ(output->ndim, 4);
      CHECK_EQ(input->shape[1], kernel->shape[1]);
      CHECK_EQ(input->shape[0], output->shape[0]);
      size_t input_channels = input->shape[1];
      CHECK_EQ(output->shape[1], kernel->shape[0]);
      if (bias) {
        CHECK_EQ(output->shape[1], bias->shape[0]);
      }
      size_t output_channels = output->shape[1];
      nnp_size input_size{static_cast<size_t>(input->shape[2]),
                          static_cast<size_t>(input->shape[3])};
      nnp_size kernel_size{static_cast<size_t>(kernel->shape[2]),
                           static_cast<size_t>(kernel->shape[3])};
      CHECK(input->strides == nullptr);
      CHECK(kernel->strides == nullptr);
      if (bias) {
        CHECK(bias->strides == nullptr);
      }
80

81 82 83 84 85 86
      CHECK(TypeMatch(input->dtype, kDLFloat, 32));
      CHECK(TypeMatch(kernel->dtype, kDLFloat, 32));
      if (bias) {
        CHECK(TypeMatch(bias->dtype, kDLFloat, 32));
      }
      CHECK(TypeMatch(output->dtype, kDLFloat, 32));
87

88 89 90 91 92
      // Allocate a zero-bias if we don't pass one in.
      std::unique_ptr<std::vector<float>> zero_bias;
      if (!bias) {
        zero_bias.reset(new std::vector<float>(output->shape[1], 0.0));
      }
93

94 95 96 97 98 99 100 101 102 103 104 105
      size_t workspace_size = 0;
      nnp_status status = nnp_convolution_inference(
          algo, nnp_convolution_transform_strategy_compute, input_channels,
          output_channels, input_size, input_padding, kernel_size, stride_size,
          nullptr, nullptr, nullptr, nullptr, nullptr, &workspace_size,
          nnp_activation_identity, nullptr, entry->threadpool, nullptr);
      CHECK_EQ(status, nnp_status_success);

      // Division with rounding up, in case size is not multiple of sizeof(float)
      const size_t workspace_elements = (workspace_size + sizeof(float) - 1) / sizeof(float);

      TVMContext ctx = input->ctx;
106
      DLDataType type_hint = input->dtype;
107 108 109 110 111 112

      DeviceAPI* cpu_api = DeviceAPI::Get(ctx);
      void* workspace_buffer =
        cpu_api->AllocWorkspace(ctx, workspace_elements * sizeof(float), type_hint);
      CHECK(workspace_buffer != nullptr);

113 114 115 116 117 118 119 120 121 122 123 124 125
      for (auto n = 0; n < input->shape[0]; ++n) {
        nnp_status status = nnp_convolution_inference(
            algo, nnp_convolution_transform_strategy_compute, input_channels,
            output_channels, input_size, input_padding, kernel_size,
            stride_size,
            static_cast<float *>(input->data) + n * input->shape[1] *
                                                   input->shape[2] *
                                                   input->shape[3],
            static_cast<float *>(kernel->data),
            bias ? static_cast<float *>(bias->data) : zero_bias->data(),
            static_cast<float *>(output->data) + n * output->shape[1] *
                                                    output->shape[2] *
                                                    output->shape[3],
126 127
            workspace_buffer, &workspace_size,
            nnp_activation_identity, nullptr, entry->threadpool, nullptr);
128 129 130

        CHECK_EQ(status, nnp_status_success);
      }
131
      cpu_api->FreeWorkspace(ctx, workspace_buffer);
132 133 134 135 136 137 138 139 140 141 142
    });

TVM_REGISTER_GLOBAL("tvm.contrib.nnpack.convolution_inference_without_weight_transform")
    .set_body([](TVMArgs args, TVMRetValue *ret) {
      NNPackThreadLocalEntry *entry = NNPackThreadLocalEntry::ThreadLocal();
      static std::once_flag flag;
      std::call_once(flag,
                     []() { CHECK_EQ(nnp_initialize(), nnp_status_success); });
      DLTensor *input = args[0];
      DLTensor *transformed_kernel = args[1];
      DLTensor *bias = nullptr;
143
      if (args[2].type_code() == kTVMDLTensorHandle) {
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
        bias = args[2];
      }
      DLTensor *output = args[3];
      uint64_t pad_top = args[4], pad_right = args[5], pad_bottom = args[6],
               pad_left = args[7];
      nnp_padding input_padding{pad_top, pad_right, pad_bottom, pad_left};
      uint64_t stride_width = args[8], stride_height = args[9];
      nnp_size stride_size{stride_width, stride_height};
      NNPackConfig(args[10]);

      uint64_t algo_ = args[11];
      nnp_convolution_algorithm algo =
          static_cast<nnp_convolution_algorithm>(algo_);
      CHECK_EQ(input->ndim, 4);
      if (bias) {
        CHECK_EQ(bias->ndim, 1);
      }
      CHECK_EQ(output->ndim, 4);
      CHECK_EQ(input->shape[0], output->shape[0]);
      size_t input_channels = input->shape[1];
      if (bias) {
        CHECK_EQ(output->shape[1], bias->shape[0]);
      }
      size_t output_channels = output->shape[1];
      nnp_size input_size{static_cast<size_t>(input->shape[2]),
                          static_cast<size_t>(input->shape[3])};
      nnp_size kernel_size{3, 3};
      CHECK(input->strides == nullptr);
      CHECK(transformed_kernel->strides == nullptr);
      if (bias) {
        CHECK(bias->strides == nullptr);
      }

      CHECK(TypeMatch(input->dtype, kDLFloat, 32));
      CHECK(TypeMatch(transformed_kernel->dtype, kDLFloat, 32));
      if (bias) {
        CHECK(TypeMatch(bias->dtype, kDLFloat, 32));
      }
      CHECK(TypeMatch(output->dtype, kDLFloat, 32));

      // Allocate a zero-bias if we don't pass one in.
      std::unique_ptr<std::vector<float>> zero_bias;
      if (!bias) {
        zero_bias.reset(new std::vector<float>(output->shape[1], 0.0));
      }

190 191 192 193 194 195 196 197 198 199 200 201
      size_t workspace_size = 0;
      nnp_status status = nnp_convolution_inference(
          algo, nnp_convolution_transform_strategy_reuse, input_channels,
          output_channels, input_size, input_padding, kernel_size, stride_size,
          nullptr, nullptr, nullptr, nullptr, nullptr, &workspace_size,
          nnp_activation_identity, nullptr, entry->threadpool, nullptr);
      CHECK_EQ(status, nnp_status_success);

      // Division with rounding up, in case size is not multiple of sizeof(float)
      const size_t workspace_elements = (workspace_size + sizeof(float) - 1) / sizeof(float);

      TVMContext ctx = input->ctx;
202
      DLDataType type_hint = input->dtype;
203 204 205 206 207 208

      DeviceAPI* cpu_api = DeviceAPI::Get(ctx);
      void* workspace_buffer =
        cpu_api->AllocWorkspace(ctx, workspace_elements * sizeof(float), type_hint);
      CHECK(workspace_buffer != nullptr);

209
      for (auto n = 0; n < input->shape[0]; ++n) {
hlu1 committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223
        nnp_status status = nnp_convolution_inference(
            algo, nnp_convolution_transform_strategy_reuse, input_channels, output_channels,
            input_size, input_padding, kernel_size, stride_size,
            static_cast<float *>(input->data) + n * input->shape[1] *
                                input->shape[2] *
                                input->shape[3],
            static_cast<float *>(transformed_kernel->data),
            bias ? static_cast<float *>(bias->data) : zero_bias->data(),
            static_cast<float *>(output->data) + n * output->shape[1] *
                                output->shape[2] *
                                output->shape[3],
            workspace_buffer, &workspace_size,
            nnp_activation_identity, nullptr, entry->threadpool, nullptr);
        CHECK_EQ(status, nnp_status_success);
224
      }
225 226

      cpu_api->FreeWorkspace(ctx, workspace_buffer);
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
    });

TVM_REGISTER_GLOBAL(
    "tvm.contrib.nnpack.convolution_inference_weight_transform")
    .set_body([](TVMArgs args, TVMRetValue *ret) {
      NNPackThreadLocalEntry *entry = NNPackThreadLocalEntry::ThreadLocal();
      static std::once_flag flag;
      std::call_once(flag,
                     []() { CHECK_EQ(nnp_initialize(), nnp_status_success); });
      DLTensor *kernel = args[0];
      DLTensor *transformed_kernel = args[1];
      // Dummy sizes
      nnp_padding input_padding{1, 1, 1, 1};
      nnp_size stride_size{1, 1};

      nnp_size input_size{100, 100};

      NNPackConfig(args[2]);

      uint64_t algo_ = args[3];
      nnp_convolution_algorithm algo =
          static_cast<nnp_convolution_algorithm>(algo_);
      CHECK_EQ(kernel->ndim, 4);
      size_t input_channels = kernel->shape[1];
      size_t output_channels = kernel->shape[0];
      CHECK_EQ(kernel->shape[2], 3);
      CHECK_EQ(kernel->shape[3], 3);
      nnp_size kernel_size{static_cast<size_t>(kernel->shape[2]),
                           static_cast<size_t>(kernel->shape[3])};
      CHECK(kernel->strides == nullptr);
      CHECK(TypeMatch(kernel->dtype, kDLFloat, 32));

      size_t transformed_kernel_size = 0;
      nnp_status status;
      status = nnp_convolution_inference(
          algo, nnp_convolution_transform_strategy_precompute, input_channels,
          output_channels, input_size, input_padding, kernel_size, stride_size,
          nullptr, nullptr, nullptr, nullptr, nullptr, &transformed_kernel_size,
          nnp_activation_identity, nullptr, entry->threadpool, nullptr);
      CHECK_EQ(status, nnp_status_success);

      CHECK_LE(transformed_kernel_size, GetDataSize(*transformed_kernel));

      status = nnp_convolution_inference(
          algo, nnp_convolution_transform_strategy_precompute, input_channels,
          output_channels, input_size, input_padding, kernel_size, stride_size,
          nullptr, static_cast<float *>(kernel->data), nullptr, nullptr,
          static_cast<float *>(transformed_kernel->data),
          &transformed_kernel_size, nnp_activation_identity, nullptr,
          entry->threadpool, nullptr);
      CHECK_EQ(status, nnp_status_success);
    });
279 280
}  // namespace contrib
}  // namespace tvm