tvm_ext.cc 5.33 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.
 */

20 21 22 23 24 25 26 27 28

/*!
 *  Copyright (c) 2017 by Contributors
 * \brief Example package that uses TVM.
 * \file tvm_ext.cc
 */
#include <tvm/runtime/packed_func.h>
#include <tvm/runtime/module.h>
#include <tvm/runtime/registry.h>
29
#include <tvm/runtime/ndarray.h>
30
#include <tvm/packed_func_ext.h>
31
#include <tvm/runtime/device_api.h>
32 33

namespace tvm_ext {
34
using IntVector = std::vector<int>;
35
class NDSubClass;
36 37 38 39 40
}  // namespace tvm_ext

namespace tvm {
namespace runtime {
template<>
41
struct extension_type_info<tvm_ext::IntVector> {
42 43
  static const int code = 17;
};
44 45 46 47
template<>
struct array_type_info<tvm_ext::NDSubClass> {
  static const int code = 1;
};
48 49 50
}  // namespace tvm
}  // namespace runtime

51 52 53
using namespace tvm;
using namespace tvm::runtime;

54
namespace tvm_ext {
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
/*!
 * \brief A subclass of TVM's NDArray.
 *
 * To use this extension, an external library should
 *
 * 1) Inherit TVM's NDArray and NDArray container,
 *    and define the trait `array_type_info` for this class.
 *
 * 2) Define a constructor in the inherited class that accepts
 *    a pointer to TVM's Container, which is nullable.
 *
 * 3) On Python frontend, inherit `tvm.nd.NDArrayBase`,
 *    define the class attribute `_array_type_code` consistent to
 *    the C++ type trait, and register the subclass using `tvm.register_extension`.
 */
class NDSubClass : public tvm::runtime::NDArray {
 public:
  class SubContainer : public NDArray::Container {
   public:
    SubContainer(int addtional_info) :
      addtional_info_(addtional_info) {
      array_type_code_ = array_type_info<NDSubClass>::code;
    }
    static bool Is(NDArray::Container *container) {
      SubContainer *c = static_cast<SubContainer*>(container);
      return c->array_type_code_ == array_type_info<NDSubClass>::code;
    }
    int addtional_info_{0};
  };
  NDSubClass(NDArray::Container *container) {
    if (container == nullptr) {
      data_ = nullptr;
      return;
    }
    CHECK(SubContainer::Is(container));
    container->IncRef();
    data_ = container;
  }
  ~NDSubClass() {
    this->reset();
  }
  NDSubClass AddWith(const NDSubClass &other) const {
    SubContainer *a = static_cast<SubContainer*>(data_);
    SubContainer *b = static_cast<SubContainer*>(other.data_);
    CHECK(a != nullptr && b != nullptr);
    return NDSubClass(new SubContainer(a->addtional_info_ + b->addtional_info_));
  }
  int get_additional_info() const {
    SubContainer *self = static_cast<SubContainer*>(data_);
    CHECK(self != nullptr);
    return self->addtional_info_;
  }
};
}  // namespace tvm_ext

namespace tvm_ext {
111

112
TVM_REGISTER_EXT_TYPE(IntVector);
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

TVM_REGISTER_GLOBAL("tvm_ext.ivec_create")
.set_body([](TVMArgs args, TVMRetValue *rv) {
    IntVector vec;
    for (int i = 0; i < args.size(); ++i) {
      vec.push_back(args[i].operator int());
    }
    *rv = vec;
  });

TVM_REGISTER_GLOBAL("tvm_ext.ivec_get")
.set_body([](TVMArgs args, TVMRetValue *rv) {
    *rv = args[0].AsExtension<IntVector>()[args[1].operator int()];
  });


129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
TVM_REGISTER_GLOBAL("tvm_ext.bind_add")
.set_body([](TVMArgs args_, TVMRetValue *rv_) {
    PackedFunc pf = args_[0];
    int b = args_[1];
    *rv_ = PackedFunc([pf, b](TVMArgs args, TVMRetValue *rv) {
        *rv = pf(b, args[0]);
      });
  });

TVM_REGISTER_GLOBAL("tvm_ext.sym_add")
.set_body([](TVMArgs args, TVMRetValue *rv) {
    Var a = args[0];
    Var b = args[1];
    *rv = a + b;
  });
144 145 146 147 148

TVM_REGISTER_GLOBAL("device_api.ext_dev")
.set_body([](TVMArgs args, TVMRetValue *rv) {
    *rv = (*tvm::runtime::Registry::Get("device_api.cpu"))();
  });
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

TVM_REGISTER_GLOBAL("tvm_ext.nd_create")
.set_body([](TVMArgs args, TVMRetValue *rv) {
  int addtional_info = args[0];
  *rv = NDSubClass(new NDSubClass::SubContainer(addtional_info));
});

TVM_REGISTER_GLOBAL("tvm_ext.nd_add_two")
.set_body([](TVMArgs args, TVMRetValue *rv) {
  NDSubClass a = args[0];
  NDSubClass b = args[1];
  *rv = a.AddWith(b);
});

TVM_REGISTER_GLOBAL("tvm_ext.nd_get_addtional_info")
.set_body([](TVMArgs args, TVMRetValue *rv) {
  NDSubClass a = args[0];
  *rv = a.get_additional_info();
});

169
}  // namespace tvm_ext
170

171 172 173 174 175
// External function exposed to runtime.
extern "C" float TVMTestAddOne(float y) {
  return y + 1;
}

176 177 178 179 180 181 182 183 184 185 186 187 188 189
// This callback approach allows extension allows tvm to extract
// This way can be helpful when we want to use a header only
// minimum version of TVM Runtime.
extern "C" int TVMExtDeclare(TVMFunctionHandle pregister) {
  const PackedFunc& fregister =
      *static_cast<PackedFunc*>(pregister);
  auto mul = [](TVMArgs args, TVMRetValue *rv) {
    int x = args[0];
    int y = args[1];
    *rv = x * y;
  };
  fregister("mul", PackedFunc(mul));
  return 0;
}