"""NNVM compiler toolchain. User only need to use :any:`build` and :any:`build_config` to do the compilation, and :any:`save_param_dict` to save the parameters into bytes. The other APIs are for more advanced interaction with the compiler toolchain. """ from __future__ import absolute_import import tvm from . import build_module from . build_module import build, optimize, build_config from . compile_engine import engine, graph_key from . param_dict import save_param_dict, load_param_dict from .. import symbol as _symbol from .. import graph as _graph from .. import top as _top tvm.register_extension(_symbol.Symbol, _symbol.Symbol) tvm.register_extension(_graph.Graph, _graph.Graph)
def get_bool(self, key): """Get bool from attr dict Parameters ---------- key : str The attr key Returns ------- value : bool The result value """ lowercase = self[key].lower() if lowercase == "1": return True elif lowercase == "0": return False elif lowercase == "true": return True elif lowercase == "false": return False else: raise ValueError("Wrong bool format for key %s" % key) def __repr__(self): return str({k : self[k] for k in self.keys()}) tvm.register_extension(AttrDict, AttrDict)
if lowercase == "1": return True if lowercase == "0": return False if lowercase == "true": return True if lowercase == "false": return False raise ValueError("Wrong bool format for key %s" % key) def get_str(self, key): """Get string from attr dict Parameters ---------- key : str The attr key Returns ------- value : str The result value """ return self[key] def __repr__(self): return str({k: self[k] for k in self.keys()}) tvm.register_extension(AttrDict, AttrDict)
def __del__(self): # You can also call your own customized # deleter if you can free it via your own FFI. tvm.nd.free_extension_handle(self.handle, self.__class__._tvm_tcode) @property def _tvm_handle(self): return self.handle.value def __getitem__(self, idx): return ivec_get(self, idx) # Register IntVec extension on python side. tvm.register_extension(IntVec, IntVec) nd_create = tvm.get_global_func("tvm_ext.nd_create") nd_add_two = tvm.get_global_func("tvm_ext.nd_add_two") nd_get_addtional_info = tvm.get_global_func("tvm_ext.nd_get_addtional_info") class NDSubClass(tvm.nd.NDArrayBase): """Example for subclassing TVM's NDArray infrastructure. By inheriting TMV's NDArray, external libraries could leverage TVM's FFI without any modification. """ # Should be consistent with the type-trait set in the backend _array_type_code = 1
def __init__(self, handle): self.handle = handle def __del__(self): _tvm.nd.free_extension_handle(self.handle, 27) @property def _tvm_handle(self): return self.handle.value def __getitem__(self, idx): return ivec_get(self, idx) _tvm.register_extension(IntVector, IntVector) class Target(object): """Handle to C++ Target instance """ _tvm_tcode = 28 def __init__(self, handle): self.handle = handle def __del__(self): _tvm.nd.free_extension_handle(self.handle, 28) @property def _tvm_handle(self): return self.handle.value
# Expose two functions into python bind_add = tvm.get_global_func("tvm_ext.bind_add") sym_add = tvm.get_global_func("tvm_ext.sym_add") ivec_create = tvm.get_global_func("tvm_ext.ivec_create") ivec_get = tvm.get_global_func("tvm_ext.ivec_get") class IntVec(object): """Example for using extension class in c++ """ _tvm_tcode = 17 def __init__(self, handle): self.handle = handle def __del__(self): # You can also call your own customized # deleter if you can free it via your own FFI. tvm.nd.free_extension_handle(self.handle, 17) @property def _tvm_handle(self): return self.handle.value def __getitem__(self, idx): return ivec_get(self, idx) # Register IntVec extension on python side. tvm.register_extension(IntVec, IntVec)
self.handle = handle def __del__(self): # You can also call your own customized # deleter if you can free it via your own FFI. tvm.nd.free_extension_handle(self.handle, self.__class__._tvm_tcode) @property def _tvm_handle(self): return self.handle.value def __getitem__(self, idx): return ivec_get(self, idx) # Register IntVec extension on python side. tvm.register_extension(IntVec, IntVec) nd_create = tvm.get_global_func("tvm_ext.nd_create") nd_add_two = tvm.get_global_func("tvm_ext.nd_add_two") nd_get_addtional_info = tvm.get_global_func("tvm_ext.nd_get_addtional_info") class NDSubClass(tvm.nd.NDArrayBase): """Example for subclassing TVM's NDArray infrastructure. By inheriting TMV's NDArray, external libraries could leverage TVM's FFI without any modification. """ # Should be consistent with the type-trait set in the backend _array_type_code = 1
_LIB = load_lib() # Expose two functions into python bind_add = tvm.get_global_func("tvm_ext.bind_add") sym_add = tvm.get_global_func("tvm_ext.sym_add") ivec_create = tvm.get_global_func("tvm_ext.ivec_create") ivec_get = tvm.get_global_func("tvm_ext.ivec_get") class IntVec(object): """Example for using extension class in c++ """ _tvm_tcode = 17 def __init__(self, handle): self.handle = handle def __del__(self): # You can also call your own customized # deleter if you can free it via your own FFI. tvm.nd.free_extension_handle(self.handle, 17) @property def _tvm_handle(self): return self.handle.value def __getitem__(self, idx): return ivec_get(self, idx) # Register IntVec extension on python side. tvm.register_extension(IntVec, IntVec)