fully_connected.cc 2.55 KB
Newer Older
1 2 3 4 5 6 7 8
/*!
 *  Copyright (c) 2017 by Contributors
 * \file Use external nnpack library call.
 */
#include <tvm/runtime/registry.h>
#include <tvm/runtime/util.h>
#include <dmlc/logging.h>
#include <nnpack.h>
9
#include "nnpack_utils.h"
10 11 12 13 14 15 16 17 18

namespace tvm {
namespace contrib {

using namespace runtime;

// matrix multiplication for row major
TVM_REGISTER_GLOBAL("tvm.contrib.nnpack.fully_connected_inference")
.set_body([](TVMArgs args, TVMRetValue *ret) {
19
    NNPackThreadLocalEntry *entry = NNPackThreadLocalEntry::ThreadLocal();
20 21 22 23
    nnp_initialize();
    DLTensor* A = args[0];
    DLTensor* B = args[1];
    DLTensor* C = args[2];
24 25
    NNPackConfig(args[3]);

26 27 28 29 30 31 32 33
    CHECK_EQ(A->ndim, 1);
    CHECK_EQ(B->ndim, 2);
    CHECK_EQ(C->ndim, 1);
    CHECK_EQ(B->shape[0], C->shape[0]);
    CHECK_EQ(B->shape[1], A->shape[0]);
    CHECK(C->strides == nullptr);
    CHECK(B->strides == nullptr);
    CHECK(A->strides == nullptr);
34 35 36
    CHECK(TypeMatch(A->dtype, kDLFloat, 32));
    CHECK(TypeMatch(B->dtype, kDLFloat, 32));
    CHECK(TypeMatch(C->dtype, kDLFloat, 32));
37 38 39 40 41 42

    nnp_fully_connected_inference(B->shape[1],
                                  B->shape[0],
                                  static_cast<float*>(A->data),
                                  static_cast<float*>(B->data),
                                  static_cast<float*>(C->data),
43
                                  entry->threadpool);
44 45 46 47 48
  });


TVM_REGISTER_GLOBAL("tvm.contrib.nnpack.fully_connected_output")
.set_body([](TVMArgs args, TVMRetValue *ret) {
49
    NNPackThreadLocalEntry *entry = NNPackThreadLocalEntry::ThreadLocal();
50 51 52 53
    nnp_initialize();
    DLTensor* A = args[0];
    DLTensor* B = args[1];
    DLTensor* C = args[2];
54 55
    NNPackConfig(args[3]);

56 57 58 59 60 61 62 63 64
    CHECK_EQ(A->ndim, 2);
    CHECK_EQ(B->ndim, 2);
    CHECK_EQ(C->ndim, 2);
    CHECK_EQ(B->shape[0], C->shape[1]);
    CHECK_EQ(B->shape[1], A->shape[1]);
    CHECK_EQ(A->shape[0], C->shape[0]);
    CHECK(C->strides == nullptr);
    CHECK(B->strides == nullptr);
    CHECK(A->strides == nullptr);
65 66 67
    CHECK(TypeMatch(A->dtype, kDLFloat, 32));
    CHECK(TypeMatch(B->dtype, kDLFloat, 32));
    CHECK(TypeMatch(C->dtype, kDLFloat, 32));
68 69 70 71 72 73 74

    nnp_fully_connected_output(A->shape[0],
                               B->shape[1],
                               B->shape[0],
                               static_cast<float*>(A->data),
                               static_cast<float*>(B->data),
                               static_cast<float*>(C->data),
75
                               entry->threadpool,
76 77 78 79 80
                               NULL);
  });

}  // namespace contrib
}  // namespace tvm