tvm_ext.cc 5.68 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 25 26 27

/*!
 * \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>
28
#include <tvm/runtime/ndarray.h>
29
#include <tvm/packed_func_ext.h>
30
#include <tvm/runtime/device_api.h>
31 32

namespace tvm_ext {
33
class NDSubClass;
34 35 36 37 38
}  // namespace tvm_ext

namespace tvm {
namespace runtime {
template<>
39 40 41
struct array_type_info<tvm_ext::NDSubClass> {
  static const int code = 1;
};
42 43 44
}  // namespace tvm
}  // namespace runtime

45 46 47
using namespace tvm;
using namespace tvm::runtime;

48
namespace tvm_ext {
49 50 51 52 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
/*!
 * \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_;
  }
};
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125


/*!
 * \brief Introduce additional extension data structures
 *        by sub-classing TVM's object system.
 */
class IntVectorObj : public Object {
 public:
  std::vector<int> vec;

  static constexpr const char* _type_key = "tvm_ext.IntVector";
  TVM_DECLARE_FINAL_OBJECT_INFO(IntVectorObj, Object);
};

/*!
 * \brief Int vector reference class.
 */
class IntVector : public ObjectRef {
 public:
  TVM_DEFINE_OBJECT_REF_METHODS(IntVector, ObjectRef, IntVectorObj);
};

TVM_REGISTER_OBJECT_TYPE(IntVectorObj);

126 127 128
}  // namespace tvm_ext

namespace tvm_ext {
129

130 131
TVM_REGISTER_GLOBAL("tvm_ext.ivec_create")
.set_body([](TVMArgs args, TVMRetValue *rv) {
132
    auto n = tvm::runtime::make_object<IntVectorObj>();
133
    for (int i = 0; i < args.size(); ++i) {
134
      n->vec.push_back(args[i].operator int());
135
    }
136
    *rv = IntVector(n);
137 138 139 140
  });

TVM_REGISTER_GLOBAL("tvm_ext.ivec_get")
.set_body([](TVMArgs args, TVMRetValue *rv) {
141 142
    IntVector p = args[0];
    *rv = p->vec[args[1].operator int()];
143 144 145
  });


146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
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;
  });
161 162 163 164 165

TVM_REGISTER_GLOBAL("device_api.ext_dev")
.set_body([](TVMArgs args, TVMRetValue *rv) {
    *rv = (*tvm::runtime::Registry::Get("device_api.cpu"))();
  });
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

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();
});

186
}  // namespace tvm_ext
187

188 189 190 191 192
// External function exposed to runtime.
extern "C" float TVMTestAddOne(float y) {
  return y + 1;
}

193 194 195 196 197 198 199 200 201 202 203 204 205 206
// 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;
}