You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by jw...@apache.org on 2021/04/09 19:17:21 UTC

[tvm] branch checkpoint created (now 6f95da5)

This is an automated email from the ASF dual-hosted git repository.

jwfromm pushed a change to branch checkpoint
in repository https://gitbox.apache.org/repos/asf/tvm.git.


      at 6f95da5  Some fixes.

This branch includes the following new commits:

     new df61188  Initial commit for AMD proposal of ONNXRT<>TVM
     new bcb100a  [AMD:ONNXRT:TVM] Add packed function to import and compile an ONNX model which can be used as a JIT TVM interface for ONNXRT.
     new 388e1df  [AMD:ONNXRT:TVM] Include input shapes during compilation.
     new aed094d  [AMD:ONNXRT:TVM] Remove build_module.cc changes.
     new 0679ad4  [AMD:ONNXRT:TVM] Demonstrate output shape extraction from module and enable the application of tuning logs to the module.
     new dbcf2e4  Fix
     new 40d85a7  Fix driver
     new bea9d95  fix for onnxruntime
     new 6f95da5  Some fixes.

The 9 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[tvm] 09/09: Some fixes.

Posted by jw...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jwfromm pushed a commit to branch checkpoint
in repository https://gitbox.apache.org/repos/asf/tvm.git

commit 6f95da54b0a836f86d1636bcf38d98f9bfae0674
Author: Josh Fromm <jw...@uw.edu>
AuthorDate: Wed Mar 24 16:32:06 2021 -0700

    Some fixes.
---
 CMakeLists.txt                        | 4 ++--
 python/tvm/relay/frontend/jit/onnx.py | 4 +---
 src/driver/driver_api.cc              | 9 +++++++--
 src/runtime/graph/graph_runtime.cc    | 7 -------
 4 files changed, 10 insertions(+), 14 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 769a353..0a5122d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -5,7 +5,7 @@ project(tvm C CXX)
 include(cmake/utils/Utils.cmake)
 include(cmake/utils/FindCUDA.cmake)
 include(cmake/utils/FindOpenCL.cmake)
-include(cmake/utils/FindVulkan.cmake)
+#include(cmake/utils/FindVulkan.cmake)
 include(cmake/utils/FindLLVM.cmake)
 include(cmake/utils/FindROCM.cmake)
 include(cmake/utils/FindEthosN.cmake)
@@ -330,7 +330,7 @@ include(cmake/modules/CUDA.cmake)
 include(cmake/modules/Hexagon.cmake)
 include(cmake/modules/OpenCL.cmake)
 include(cmake/modules/OpenMP.cmake)
-include(cmake/modules/Vulkan.cmake)
+#include(cmake/modules/Vulkan.cmake)
 include(cmake/modules/Metal.cmake)
 include(cmake/modules/ROCM.cmake)
 include(cmake/modules/LLVM.cmake)
diff --git a/python/tvm/relay/frontend/jit/onnx.py b/python/tvm/relay/frontend/jit/onnx.py
index ae10915..e0f5b6b 100644
--- a/python/tvm/relay/frontend/jit/onnx.py
+++ b/python/tvm/relay/frontend/jit/onnx.py
@@ -46,9 +46,8 @@ def onnx_compile(model_string, target, target_host, opt_level, input_shapes):
     shape_dict = collections.OrderedDict(input_mapping)
 
     irmod, params = tvm.relay.frontend.from_onnx(model, shape_dict, opset=11)
-    print(irmod)
     # import ipdb; ipdb.set_trace()
-    with tvm.relay.build_config(opt_level=opt_level):
+    with tvm.relay.build_config(opt_level=opt_level, disabled_pass={"AlterOpLayout"}):
         tuning_logfile = os.getenv("AUTOTVM_TUNING_LOG")
         if tuning_logfile:
             with autotvm.apply_history_best(tuning_logfile):
@@ -57,7 +56,6 @@ def onnx_compile(model_string, target, target_host, opt_level, input_shapes):
         else:
             lib = tvm.relay.build(irmod, target_host=target_host, target=target)
 
-    print(lib.graph_json)
     ctx = tvm.context(target, 0)
     m = tvm.contrib.graph_runtime.GraphModule(lib["default"](ctx))
     # m.set_input(**params)
diff --git a/src/driver/driver_api.cc b/src/driver/driver_api.cc
index 0011dab..a892a86 100644
--- a/src/driver/driver_api.cc
+++ b/src/driver/driver_api.cc
@@ -364,8 +364,13 @@ void TVMRun(tvm::runtime::Module& mod, std::vector<DLTensor>& inputs, std::vecto
     set_input(i, &inputs[i]);
   }
 
-  const tvm::PackedFunc* run = tvm::runtime::Registry::Get("tvm_run_with_benchmark");
-  (*run)(mod);
+  // Dont include benchmarking in core run for now
+  //const tvm::PackedFunc* run = tvm::runtime::Registry::Get("tvm_run_with_benchmark");
+  //(*run)(mod);
+
+  // Just directly run the module
+  tvm::PackedFunc run = mod.GetFunction("run", false);
+  run();
 
   tvm::PackedFunc get_output = mod.GetFunction("get_output", false);
   for (size_t i = 0; i < outputs.size(); i++)
diff --git a/src/runtime/graph/graph_runtime.cc b/src/runtime/graph/graph_runtime.cc
index 5df9068..8b697c9 100644
--- a/src/runtime/graph/graph_runtime.cc
+++ b/src/runtime/graph/graph_runtime.cc
@@ -90,7 +90,6 @@ void GraphRuntime::Init(const std::string& graph_json, tvm::runtime::Module modu
     for(int ind = 0; ind < old_t->ndim; ind++) {
       s << old_t->shape[ind] << " ";
     }
-    LOG(INFO) << s.str();
   }
 }
 /*!
@@ -128,12 +127,6 @@ void GraphRuntime::SetInputZeroCopy(int index, DLTensor* data_ref) {
   // check the consistency of input
   ICHECK_EQ(data_alignment_[eid], details::GetDataAlignment(*data_ref));
   // ICHECK_EQ(reinterpret_cast<size_t>(data_ref->data) % kAllocAlignment, 0) << data_ref->data;
-  for(int i = 0; i < old_t->ndim; i++) {
-    LOG(INFO) << "OLD " << old_t->shape[i];
-  }
-  for(int i = 0; i < data_ref->ndim; i++) {
-    LOG(INFO) << "DATA_REF " << data_ref->shape[i];
-  }
   ICHECK_EQ(old_t->ndim, static_cast<size_t>(data_ref->ndim));
   ICHECK_EQ(old_t->ctx.device_type, data_ref->ctx.device_type);
   ICHECK_EQ(old_t->ctx.device_id, data_ref->ctx.device_id);

[tvm] 06/09: Fix

Posted by jw...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jwfromm pushed a commit to branch checkpoint
in repository https://gitbox.apache.org/repos/asf/tvm.git

commit dbcf2e47eff4b818c08f972e5d351acdc4394615
Author: Jared Roesch <jr...@octoml.ai>
AuthorDate: Fri Mar 5 14:02:38 2021 -0800

    Fix
---
 include/tvm/driver/jit_interface.h | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/include/tvm/driver/jit_interface.h b/include/tvm/driver/jit_interface.h
index 47b98c9..c4596e9 100644
--- a/include/tvm/driver/jit_interface.h
+++ b/include/tvm/driver/jit_interface.h
@@ -4,9 +4,6 @@
 extern "C" {
     EXPORT_DLL tvm::runtime::Module TVMCompile(const std::string& onnx_txt, const std::string& target, const std::string& target_host, int opt_level, const std::vector<std::vector<int64_t>>& input_shapes);
     EXPORT_DLL void TVMExtractOutputShapes(tvm::runtime::Module& mod, size_t num_outputs, std::vector<std::vector<int64_t>>& output_shapes);
-
-    EXPORT_DLL void TVMRun(tvm::runtime::Module& mod, std::vector<DLTensor>& inputs, std::vector<DLTensor>& outputs);
-    
-    
+    EXPORT_DLL void TVMRun(tvm::runtime::Module& mod, std::vector<DLTensor>& inputs, std::vector<DLTensor>& outputs, tvm::runtime::TVMRetValue *ret);
 }  // TVM_EXTERN_C
 #endif

[tvm] 05/09: [AMD:ONNXRT:TVM] Demonstrate output shape extraction from module and enable the application of tuning logs to the module.

Posted by jw...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jwfromm pushed a commit to branch checkpoint
in repository https://gitbox.apache.org/repos/asf/tvm.git

commit 0679ad4e927bf644bb8499ee4f0f5c3d32707d52
Author: Chris Sullivan <cs...@octoml.ai>
AuthorDate: Thu Sep 3 23:16:49 2020 -0700

    [AMD:ONNXRT:TVM] Demonstrate output shape extraction from module
    and enable the application of tuning logs to the module.
---
 include/tvm/driver/jit_interface.h    |  4 +++-
 python/tvm/relay/frontend/jit/onnx.py | 25 ++++++++++++++++++++++++-
 src/driver/driver_api.cc              | 15 +++++++++++++--
 3 files changed, 40 insertions(+), 4 deletions(-)

diff --git a/include/tvm/driver/jit_interface.h b/include/tvm/driver/jit_interface.h
index e9203ee..47b98c9 100644
--- a/include/tvm/driver/jit_interface.h
+++ b/include/tvm/driver/jit_interface.h
@@ -3,7 +3,9 @@
 #ifdef __cplusplus
 extern "C" {
     EXPORT_DLL tvm::runtime::Module TVMCompile(const std::string& onnx_txt, const std::string& target, const std::string& target_host, int opt_level, const std::vector<std::vector<int64_t>>& input_shapes);
-    EXPORT_DLL void TVMRun(tvm::runtime::Module& mod, std::vector<DLTensor>& inputs, std::vector<DLTensor>& outputs, tvm::runtime::TVMRetValue* ret);
+    EXPORT_DLL void TVMExtractOutputShapes(tvm::runtime::Module& mod, size_t num_outputs, std::vector<std::vector<int64_t>>& output_shapes);
+
+    EXPORT_DLL void TVMRun(tvm::runtime::Module& mod, std::vector<DLTensor>& inputs, std::vector<DLTensor>& outputs);
     
     
 }  // TVM_EXTERN_C
diff --git a/python/tvm/relay/frontend/jit/onnx.py b/python/tvm/relay/frontend/jit/onnx.py
index 3672bbe..0f2a79c 100644
--- a/python/tvm/relay/frontend/jit/onnx.py
+++ b/python/tvm/relay/frontend/jit/onnx.py
@@ -14,9 +14,27 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
+import os
 import onnx
 import tvm
 import tvm.relay
+import tvm.autotvm as autotvm
+import timeit
+import numpy as np
+
+@tvm.register_func("tvm_run_with_benchmark")
+def run_with_benchmark(mod):
+    run = mod.get_function('run')
+    def benchmark(name):
+        t = timeit.Timer(lambda: run()).repeat(repeat=5, number=5)
+        ts = np.array(t) * 1000
+        print("{} benchmark results: {:.2f}ms mean, {:.2f}ms median, {:.2f}ms std".format(
+            name, np.mean(ts), np.median(ts), np.std(ts)
+        ))
+    if os.getenv("AUTOTVM_TUNING_LOG"):
+        benchmark("Tuned")
+    else:
+        benchmark("Baseline")
 
 @tvm.register_func("tvm_onnx_import_and_compile")
 def onnx_compile(model_string, target, target_host, opt_level, input_shapes):
@@ -26,7 +44,12 @@ def onnx_compile(model_string, target, target_host, opt_level, input_shapes):
 
     irmod, params = tvm.relay.frontend.from_onnx(model, input_shapes, opset=11)
     with tvm.relay.build_config(opt_level=opt_level):
-        graph, lib, params = tvm.relay.build(irmod, target_host=target_host, target=target, params=params)
+        tuning_logfile = os.getenv("AUTOTVM_TUNING_LOG")
+        if tuning_logfile:
+            with autotvm.apply_history_best(tuning_logfile):
+                graph, lib, params = tvm.relay.build(irmod, target_host=target_host, target=target, params=params)
+        else:
+            graph, lib, params = tvm.relay.build(irmod, target_host=target_host, target=target, params=params)
 
     ctx = tvm.context(target, 0)
     m = tvm.contrib.graph_runtime.create(graph, lib, ctx)
diff --git a/src/driver/driver_api.cc b/src/driver/driver_api.cc
index 4399855..9172283 100644
--- a/src/driver/driver_api.cc
+++ b/src/driver/driver_api.cc
@@ -346,7 +346,17 @@ tvm::runtime::Module TVMCompile(const std::string& onnx_txt, const std::string&
   return mod;
 }
 
-void TVMRun(tvm::runtime::Module& mod, std::vector<DLTensor>& inputs, std::vector<DLTensor>& outputs, tvm::runtime::TVMRetValue* ret)
+void TVMExtractOutputShapes(tvm::runtime::Module& mod, size_t num_outputs, std::vector<std::vector<int64_t>>& output_shapes)
+{
+  tvm::PackedFunc get_output = mod.GetFunction("get_output", false);
+  for (size_t i = 0; i < num_outputs; i++)
+  {
+    tvm::runtime::NDArray output_array = get_output(i);
+    output_shapes.push_back(output_array.Shape());
+  }
+}
+
+void TVMRun(tvm::runtime::Module& mod, std::vector<DLTensor>& inputs, std::vector<DLTensor>& outputs)
 {
   tvm::PackedFunc set_input = mod.GetFunction("set_input_zero_copy", false);
   for (size_t i = 0; i < inputs.size(); i++)
@@ -354,7 +364,8 @@ void TVMRun(tvm::runtime::Module& mod, std::vector<DLTensor>& inputs, std::vecto
     set_input(i, &inputs[i]);
   }
 
-  mod.GetFunction("run", false)();
+  const tvm::PackedFunc* run = tvm::runtime::Registry::Get("tvm_run_with_benchmark");
+  (*run)(mod);
 
   tvm::PackedFunc get_output = mod.GetFunction("get_output", false);
   for (size_t i = 0; i < outputs.size(); i++)

[tvm] 04/09: [AMD:ONNXRT:TVM] Remove build_module.cc changes.

Posted by jw...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jwfromm pushed a commit to branch checkpoint
in repository https://gitbox.apache.org/repos/asf/tvm.git

commit aed094d99d867eef59ce14ed824edcd84969f24a
Author: Chris Sullivan <cs...@octoml.ai>
AuthorDate: Mon Aug 24 08:41:18 2020 -0700

    [AMD:ONNXRT:TVM] Remove build_module.cc changes.
---
 src/driver/driver_api.cc          | 12 +---------
 src/relay/backend/build_module.cc | 46 ++-------------------------------------
 2 files changed, 3 insertions(+), 55 deletions(-)

diff --git a/src/driver/driver_api.cc b/src/driver/driver_api.cc
index d55c0ae..4399855 100644
--- a/src/driver/driver_api.cc
+++ b/src/driver/driver_api.cc
@@ -24,16 +24,7 @@
 #include <dmlc/thread_local.h>
 #include <tvm/driver/driver_api.h>
 #include <tvm/driver/jit_interface.h>
-#include <tvm/ir/module.h>
 #include <tvm/ir/transform.h>
-#include <tvm/relay/analysis.h>
-#include <tvm/relay/expr.h>
-#include <tvm/relay/op_attr_types.h>
-#include <tvm/relay/op_strategy.h>
-#include <tvm/relay/transform.h>
-#include <tvm/relay/type.h>
-#include <tvm/runtime/module.h>
-#include <tvm/runtime/packed_func.h>
 #include <tvm/runtime/container.h>
 #include <tvm/runtime/c_runtime_api.h>
 #include <tvm/runtime/registry.h>
@@ -41,8 +32,7 @@
 #include <tvm/te/operation.h>
 #include <tvm/tir/analysis.h>
 #include <tvm/tir/transform.h>
-#include <topi/generic/injective.h>
-#include <tvm/target/generic_func.h>
+#include <tvm/topi/generic/injective.h>
 
 #include <algorithm>
 #include <mutex>
diff --git a/src/relay/backend/build_module.cc b/src/relay/backend/build_module.cc
index 3c047e1..cd038a4 100644
--- a/src/relay/backend/build_module.cc
+++ b/src/relay/backend/build_module.cc
@@ -28,10 +28,6 @@
 #include <tvm/relay/qnn/transform.h>
 #include <tvm/relay/transform.h>
 #include <tvm/runtime/device_api.h>
-#include <tvm/relay/op_attr_types.h>
-#include <tvm/relay/op_strategy.h>
-#include <topi/broadcast.h>
-#include <topi/generic/injective.h>
 
 #include <memory>
 
@@ -555,47 +551,9 @@ class RelayBuildModule : public runtime::ModuleNode {
 runtime::Module RelayBuildCreate() {
   auto exec = make_object<RelayBuildModule>();
   return runtime::Module(exec);
-}
-
-#if 1
-TVM_REGISTER_GLOBAL("jit.strategy")
-    .set_body_typed([](const Attrs& attrs, const Array<te::Tensor>& inputs, const Type& out_type,
-                       const Target& target) {
-      FTVMCompute fcompute = [](const Attrs& attrs, const Array<te::Tensor>& inputs,
-                                const Type& out_type) -> Array<te::Tensor> {
-        CHECK_EQ(inputs.size(), 2U);
-        return {topi::multiply(inputs[0], inputs[1])};
-      };
-      FTVMSchedule fschedule = [](const Attrs& attrs, const Array<te::Tensor>& outs,
-                                  const Target& target) {
-        With<Target> target_scope(target);
-        return topi::generic::schedule_injective(target, outs);
-      };
-
-      auto n = make_object<OpStrategyNode>();
-      auto strategy = relay::OpStrategy(std::move(n));
-      strategy.AddImplementation(fcompute, fschedule, "jit.strategy", 10);
-      return strategy;
-});
+} 
+
 
-    
-TVM_REGISTER_GLOBAL("relay.backend.lower_call")
-    .set_body_typed([](const relay::Call& call, const Array<te::Tensor>& inputs,
-                       const Target& target) {
-      static auto fstrategy = Op::GetAttrMap<relay::FTVMStrategy>("FTVMStrategy");
-      Op op = Downcast<Op>(call->op);
-      auto out_type = call->checked_type();
-      OpStrategy strategy = fstrategy[op](call->attrs, inputs, out_type, target);
-      auto impl = strategy->specializations[0]->implementations[0];
-      auto outs = impl.Compute(call->attrs, inputs, out_type);
-      auto f = runtime::Registry::Get("relay.backend._make_LoweredOutput");
-      if (!f) {
-        LOG(FATAL) << "relay.backend._make_LoweredOutput is not registered";
-      }
-      return (*f)(outs, impl);
-});
-#endif    
-    
 TVM_REGISTER_GLOBAL("relay.build_module._BuildModule").set_body([](TVMArgs args, TVMRetValue* rv) {
   *rv = RelayBuildCreate();
 });

[tvm] 03/09: [AMD:ONNXRT:TVM] Include input shapes during compilation.

Posted by jw...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jwfromm pushed a commit to branch checkpoint
in repository https://gitbox.apache.org/repos/asf/tvm.git

commit 388e1df3aeab41fa18bec37a80d76c24f5210983
Author: Chris Sullivan <cs...@octoml.ai>
AuthorDate: Thu Aug 20 22:18:58 2020 -0700

    [AMD:ONNXRT:TVM] Include input shapes during compilation.
---
 include/tvm/driver/jit_interface.h    |  6 ++++--
 python/tvm/relay/frontend/jit/onnx.py |  7 +++----
 src/driver/driver_api.cc              | 19 ++++++++++++++-----
 3 files changed, 21 insertions(+), 11 deletions(-)

diff --git a/include/tvm/driver/jit_interface.h b/include/tvm/driver/jit_interface.h
index e0906f1..e9203ee 100644
--- a/include/tvm/driver/jit_interface.h
+++ b/include/tvm/driver/jit_interface.h
@@ -2,7 +2,9 @@
 
 #ifdef __cplusplus
 extern "C" {
-    EXPORT_DLL tvm::runtime::Module TVMCompile(const std::string& onnx_txt, const std::string& target, const std::string& target_host, int opt_level);
-    EXPORT_DLL void TVMRun(tvm::runtime::Module& mod,  std::vector<DLTensor> inputs, std::vector<DLTensor> outputs, tvm::runtime::TVMRetValue* ret);
+    EXPORT_DLL tvm::runtime::Module TVMCompile(const std::string& onnx_txt, const std::string& target, const std::string& target_host, int opt_level, const std::vector<std::vector<int64_t>>& input_shapes);
+    EXPORT_DLL void TVMRun(tvm::runtime::Module& mod, std::vector<DLTensor>& inputs, std::vector<DLTensor>& outputs, tvm::runtime::TVMRetValue* ret);
+    
+    
 }  // TVM_EXTERN_C
 #endif
diff --git a/python/tvm/relay/frontend/jit/onnx.py b/python/tvm/relay/frontend/jit/onnx.py
index 9545395..3672bbe 100644
--- a/python/tvm/relay/frontend/jit/onnx.py
+++ b/python/tvm/relay/frontend/jit/onnx.py
@@ -19,13 +19,12 @@ import tvm
 import tvm.relay
 
 @tvm.register_func("tvm_onnx_import_and_compile")
-def onnx_compile(model_string, target, target_host, opt_level):
+def onnx_compile(model_string, target, target_host, opt_level, input_shapes):
     model = onnx.load_model_from_string(bytes(model_string))
 
-    # input shape from data
-    input_shape = {model.graph.input[0].name: (6,)}
+    input_shapes = {name : shape for (name, shape) in zip([i.name for i in model.graph.input], input_shapes)}
 
-    irmod, params = tvm.relay.frontend.from_onnx(model, input_shape, opset=11)
+    irmod, params = tvm.relay.frontend.from_onnx(model, input_shapes, opset=11)
     with tvm.relay.build_config(opt_level=opt_level):
         graph, lib, params = tvm.relay.build(irmod, target_host=target_host, target=target, params=params)
 
diff --git a/src/driver/driver_api.cc b/src/driver/driver_api.cc
index b876c38..d55c0ae 100644
--- a/src/driver/driver_api.cc
+++ b/src/driver/driver_api.cc
@@ -338,16 +338,25 @@ runtime::Module build(const IRModule& funcs, const Target& target, const Target&
 
 }  // namespace tvm
 
-
-tvm::runtime::Module TVMCompile(const std::string& onnx_txt, const std::string& target, const std::string& target_host, int opt_level)
+tvm::runtime::Module TVMCompile(const std::string& onnx_txt, const std::string& target, const std::string& target_host, int opt_level, const std::vector<std::vector<int64_t>>& input_shapes)
 {
+  tvm::Array<tvm::Array<tvm::Integer>> shapes;
+  for (size_t i = 0; i < input_shapes.size(); i++)
+  {
+    tvm::Array<tvm::Integer> shape;
+    for (auto& dim : input_shapes[i])
+    {
+      shape.push_back(tvm::Integer(dim));
+    }
+    shapes.push_back(shape);
+  }
+
   const tvm::PackedFunc* compile = tvm::runtime::Registry::Get("tvm_onnx_import_and_compile");
-  tvm::runtime::Module mod = (*compile)(TVMByteArray{onnx_txt.data(), onnx_txt.size()}, target, target_host, opt_level);
+  tvm::runtime::Module mod = (*compile)(TVMByteArray{onnx_txt.data(), onnx_txt.size()}, target, target_host, opt_level, shapes);
   return mod;
-
 }
 
-void TVMRun(tvm::runtime::Module& mod, std::vector<DLTensor> inputs, std::vector<DLTensor> outputs, tvm::runtime::TVMRetValue* ret)
+void TVMRun(tvm::runtime::Module& mod, std::vector<DLTensor>& inputs, std::vector<DLTensor>& outputs, tvm::runtime::TVMRetValue* ret)
 {
   tvm::PackedFunc set_input = mod.GetFunction("set_input_zero_copy", false);
   for (size_t i = 0; i < inputs.size(); i++)

[tvm] 07/09: Fix driver

Posted by jw...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jwfromm pushed a commit to branch checkpoint
in repository https://gitbox.apache.org/repos/asf/tvm.git

commit 40d85a7d300976d48e9ec77e04dde4bc8bb99cd1
Author: Jared Roesch <ro...@gmail.com>
AuthorDate: Tue Mar 16 14:01:58 2021 -0700

    Fix driver
---
 src/driver/driver_api.cc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/driver/driver_api.cc b/src/driver/driver_api.cc
index 9172283..0011dab 100644
--- a/src/driver/driver_api.cc
+++ b/src/driver/driver_api.cc
@@ -356,7 +356,7 @@ void TVMExtractOutputShapes(tvm::runtime::Module& mod, size_t num_outputs, std::
   }
 }
 
-void TVMRun(tvm::runtime::Module& mod, std::vector<DLTensor>& inputs, std::vector<DLTensor>& outputs)
+void TVMRun(tvm::runtime::Module& mod, std::vector<DLTensor>& inputs, std::vector<DLTensor>& outputs, tvm::runtime::TVMRetValue *ret)
 {
   tvm::PackedFunc set_input = mod.GetFunction("set_input_zero_copy", false);
   for (size_t i = 0; i < inputs.size(); i++)

[tvm] 01/09: Initial commit for AMD proposal of ONNXRT<>TVM

Posted by jw...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jwfromm pushed a commit to branch checkpoint
in repository https://gitbox.apache.org/repos/asf/tvm.git

commit df61188d938af20063c07ace40314a80b7f2dc32
Author: mei-ye <me...@yahoo.com>
AuthorDate: Thu Aug 20 22:56:52 2020 -0700

    Initial commit for AMD proposal of ONNXRT<>TVM
---
 include/tvm/driver/jit_interface.h | 10 +++++++
 src/driver/driver_api.cc           | 58 ++++++++++++++++++++++++++++++++++++++
 src/relay/backend/build_module.cc  | 43 ++++++++++++++++++++++++++++
 3 files changed, 111 insertions(+)

diff --git a/include/tvm/driver/jit_interface.h b/include/tvm/driver/jit_interface.h
new file mode 100644
index 0000000..966d5a8
--- /dev/null
+++ b/include/tvm/driver/jit_interface.h
@@ -0,0 +1,10 @@
+#define EXPORT_DLL __attribute__((visibility("default")))
+
+#ifdef __cplusplus
+extern "C" {
+    EXPORT_DLL tvm::runtime::Module TVMCompile(const std::string& onnx_txt, const std::string& target, const std::string& target_host, int opt_level);
+    EXPORT_DLL void TVMRun(tvm::runtime::Module& mod, const std::string& name, tvm::runtime::TVMArgs& args, tvm::runtime::TVMRetValue* ret);
+    
+    
+}  // TVM_EXTERN_C
+#endif
diff --git a/src/driver/driver_api.cc b/src/driver/driver_api.cc
index bbbb7e3..758f019 100644
--- a/src/driver/driver_api.cc
+++ b/src/driver/driver_api.cc
@@ -23,13 +23,25 @@
  */
 #include <dmlc/thread_local.h>
 #include <tvm/driver/driver_api.h>
+#include <tvm/driver/jit_interface.h>
+#include <tvm/ir/module.h>
 #include <tvm/ir/transform.h>
+#include <tvm/relay/analysis.h>
+#include <tvm/relay/expr.h>
+#include <tvm/relay/op_attr_types.h>
+#include <tvm/relay/op_strategy.h>
+#include <tvm/relay/transform.h>
+#include <tvm/relay/type.h>
+#include <tvm/runtime/module.h>
+#include <tvm/runtime/packed_func.h>
 #include <tvm/runtime/container.h>
 #include <tvm/runtime/registry.h>
 #include <tvm/target/codegen.h>
 #include <tvm/te/operation.h>
 #include <tvm/tir/analysis.h>
 #include <tvm/tir/transform.h>
+#include <topi/generic/injective.h>
+#include <tvm/target/generic_func.h>
 
 #include <algorithm>
 #include <mutex>
@@ -324,3 +336,49 @@ runtime::Module build(const IRModule& funcs, const Target& target, const Target&
 }
 
 }  // namespace tvm
+
+
+tvm::runtime::Module TVMCompile(const std::string& onnx_txt, const std::string& target, const std::string& target_host, int opt_level)
+{
+    auto tensor_type = tvm::relay::TensorType({1, 6}, tvm::runtime::DataType::Float(32));
+    auto X1 = tvm::relay::Var("X1", tensor_type);
+    auto mul_op = tvm::relay::Op::Get("multiply");
+    auto mul1 = tvm::relay::Call(mul_op, {X1, X1}, tvm::Attrs(), {});
+    auto mul2 = tvm::relay::Call(mul_op, {X1, mul1}, tvm::Attrs(), {});
+    auto mul3 = tvm::relay::Call(mul_op, {X1, mul2}, tvm::Attrs(), {});
+    auto Y4 = tvm::relay::Call(mul_op, {X1, mul3}, tvm::Attrs(), {});
+    auto func = tvm::relay::Function(tvm::relay::FreeVars(Y4), Y4, tvm::relay::Type(), {});
+
+    auto reg = tvm::runtime::Registry::Get("ir.RegisterOpAttr");
+    if (!reg)
+        LOG(FATAL) << "no _Register";
+
+    auto fs = tvm::runtime::Registry::Get("jit.strategy");
+    if (!fs)
+        LOG(FATAL) << "No jit strategy registered.";
+
+    auto fgeneric = tvm::GenericFunc::Get("jit.strategy_generic").set_default(*fs);
+    (*reg)("multiply", "FTVMStrategy", fgeneric, 10);
+    (*reg)("multiply", "TShapeDataDependant", false, 10);
+   
+    auto pfb = tvm::runtime::Registry::Get("relay.build_module._BuildModule");
+    tvm::runtime::Module build_mod = (*pfb)();
+    auto build_f = build_mod.GetFunction("build", false);
+    auto mod_f = build_mod.GetFunction("get_module", false);
+    auto relay_mod = tvm::IRModule::FromExpr(func);
+    tvm::Map<tvm::Integer, tvm::Target> targets;
+    // tvm::Target tgt = tvm::Target::Create(target);
+    tvm::Target tgt = tvm::Target::Create("llvm");
+    targets.Set(0, tgt);
+    // tvm::Target host = (target == target_host) ? tgt : tvm::Target::Create(target_host);
+    build_f(relay_mod, targets, tgt);
+    tvm::runtime::Module mod = mod_f();
+    return mod;
+}
+
+void TVMRun(tvm::runtime::Module& mod, const std::string& name, tvm::runtime::TVMArgs& args, tvm::runtime::TVMRetValue* ret)
+{
+    mod.GetFunction(name).CallPacked(args, ret);
+    // process return value, refe to TVMFuncCall in c_runtime_api.cc
+    
+}
diff --git a/src/relay/backend/build_module.cc b/src/relay/backend/build_module.cc
index 0884692..3c047e1 100644
--- a/src/relay/backend/build_module.cc
+++ b/src/relay/backend/build_module.cc
@@ -28,6 +28,10 @@
 #include <tvm/relay/qnn/transform.h>
 #include <tvm/relay/transform.h>
 #include <tvm/runtime/device_api.h>
+#include <tvm/relay/op_attr_types.h>
+#include <tvm/relay/op_strategy.h>
+#include <topi/broadcast.h>
+#include <topi/generic/injective.h>
 
 #include <memory>
 
@@ -553,6 +557,45 @@ runtime::Module RelayBuildCreate() {
   return runtime::Module(exec);
 }
 
+#if 1
+TVM_REGISTER_GLOBAL("jit.strategy")
+    .set_body_typed([](const Attrs& attrs, const Array<te::Tensor>& inputs, const Type& out_type,
+                       const Target& target) {
+      FTVMCompute fcompute = [](const Attrs& attrs, const Array<te::Tensor>& inputs,
+                                const Type& out_type) -> Array<te::Tensor> {
+        CHECK_EQ(inputs.size(), 2U);
+        return {topi::multiply(inputs[0], inputs[1])};
+      };
+      FTVMSchedule fschedule = [](const Attrs& attrs, const Array<te::Tensor>& outs,
+                                  const Target& target) {
+        With<Target> target_scope(target);
+        return topi::generic::schedule_injective(target, outs);
+      };
+
+      auto n = make_object<OpStrategyNode>();
+      auto strategy = relay::OpStrategy(std::move(n));
+      strategy.AddImplementation(fcompute, fschedule, "jit.strategy", 10);
+      return strategy;
+});
+
+    
+TVM_REGISTER_GLOBAL("relay.backend.lower_call")
+    .set_body_typed([](const relay::Call& call, const Array<te::Tensor>& inputs,
+                       const Target& target) {
+      static auto fstrategy = Op::GetAttrMap<relay::FTVMStrategy>("FTVMStrategy");
+      Op op = Downcast<Op>(call->op);
+      auto out_type = call->checked_type();
+      OpStrategy strategy = fstrategy[op](call->attrs, inputs, out_type, target);
+      auto impl = strategy->specializations[0]->implementations[0];
+      auto outs = impl.Compute(call->attrs, inputs, out_type);
+      auto f = runtime::Registry::Get("relay.backend._make_LoweredOutput");
+      if (!f) {
+        LOG(FATAL) << "relay.backend._make_LoweredOutput is not registered";
+      }
+      return (*f)(outs, impl);
+});
+#endif    
+    
 TVM_REGISTER_GLOBAL("relay.build_module._BuildModule").set_body([](TVMArgs args, TVMRetValue* rv) {
   *rv = RelayBuildCreate();
 });

[tvm] 08/09: fix for onnxruntime

Posted by jw...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jwfromm pushed a commit to branch checkpoint
in repository https://gitbox.apache.org/repos/asf/tvm.git

commit bea9d9506a3881fa2b5ff63c7fa81e2096e110d4
Author: Tristan Konolige <tr...@gmail.com>
AuthorDate: Wed Mar 17 17:23:18 2021 -0700

    fix for onnxruntime
---
 cmake/modules/Vulkan.cmake            | 12 ++++++++----
 cmake/utils/FindVulkan.cmake          |  6 +++---
 python/tvm/relay/frontend/jit/onnx.py | 19 +++++++++++++------
 src/runtime/graph/graph_runtime.cc    | 15 ++++++++++++++-
 src/target/spirv/build_vulkan.cc      |  2 +-
 src/target/spirv/intrin_rule_spirv.cc |  2 +-
 src/target/spirv/ir_builder.h         |  2 +-
 7 files changed, 41 insertions(+), 17 deletions(-)

diff --git a/cmake/modules/Vulkan.cmake b/cmake/modules/Vulkan.cmake
index 4df8986..abed0ab 100644
--- a/cmake/modules/Vulkan.cmake
+++ b/cmake/modules/Vulkan.cmake
@@ -16,7 +16,11 @@
 # under the License.
 
 # Be compatible with older version of CMake
-find_vulkan(${USE_VULKAN})
+find_package(Vulkan REQUIRED)
+find_package(PkgConfig REQUIRED)
+
+pkg_check_modules(SPIRV REQUIRED spirv)
+pkg_check_modules(SPIRV_TOOLS REQUIRED SPIRV-Tools)
 
 # Extra Vulkan runtime options, exposed for advanced users.
 tvm_option(USE_VULKAN_IMMEDIATE_MODE "Use Vulkan Immediate mode
@@ -29,7 +33,7 @@ tvm_option(USE_VULKAN_VALIDATION "Enable Vulkan API validation layers" OFF
 if(Vulkan_FOUND)
   # always set the includedir
   # avoid global retrigger of cmake
-  include_directories(SYSTEM ${Vulkan_INCLUDE_DIRS})
+  include_directories(SYSTEM ${Vulkan_INCLUDE_DIRS} ${SPIRV_INCLUDEDIR} ${SPIRV_TOOLS_INCLUDEDIR})
 endif(Vulkan_FOUND)
 
 if(USE_VULKAN)
@@ -41,8 +45,8 @@ if(USE_VULKAN)
   file(GLOB COMPILER_VULKAN_SRCS src/target/spirv/*.cc)
   list(APPEND RUNTIME_SRCS ${RUNTIME_VULKAN_SRCS})
   list(APPEND COMPILER_SRCS ${COMPILER_VULKAN_SRCS})
-  list(APPEND TVM_LINKER_LIBS ${Vulkan_SPIRV_TOOLS_LIBRARY})
-  list(APPEND TVM_RUNTIME_LINKER_LIBS ${Vulkan_LIBRARY})
+  list(APPEND TVM_LINKER_LIBS ${Vulkan_LIBRARIES} ${SPIRV_LIBRARIES} ${SPIRV_TOOLS_LIBRARIES})
+  list(APPEND TVM_RUNTIME_LINKER_LIBS ${Vulkan_LIBRARIES})
 
   if(USE_VULKAN_IMMEDIATE_MODE)
     message(STATUS "Build with Vulkan immediate mode")
diff --git a/cmake/utils/FindVulkan.cmake b/cmake/utils/FindVulkan.cmake
index feb5eec..ac2e32a 100644
--- a/cmake/utils/FindVulkan.cmake
+++ b/cmake/utils/FindVulkan.cmake
@@ -65,9 +65,9 @@ macro(find_vulkan use_vulkan)
         HINTS ${VULKAN_LIBRARY_PATH} ${VULKAN_LIBRARY_PATH}/spirv-tools ${VULKAN_SDK}/lib)
 
     find_path(_libspirv libspirv.h HINTS ${Vulkan_INCLUDE_DIRS} PATH_SUFFIXES vulkan spirv-tools)
-    find_path(_spirv spirv.hpp HINTS ${Vulkan_INCLUDE_DIRS} PATH_SUFFIXES vulkan SPIRV spirv/unified1 spirv-headers)
-    find_path(_glsl_std GLSL.std.450.h HINTS ${Vulkan_INCLUDE_DIRS} PATH_SUFFIXES vulkan SPIRV spirv/unified1 spirv-headers)
-    list(APPEND Vulkan_INCLUDE_DIRS ${_libspirv} ${_spirv} ${_glsl_std})
+    # find_path(_spirv spirv.hpp HINTS ${Vulkan_INCLUDE_DIRS} PATH_SUFFIXES vulkan SPIRV spirv/unified1 spirv-headers)
+    # find_path(_glsl_std GLSL.std.450.h HINTS ${Vulkan_INCLUDE_DIRS} PATH_SUFFIXES vulkan SPIRV spirv/unified1 spirv-headers)
+    list(APPEND Vulkan_INCLUDE_DIRS ${_libspirv})
     message(STATUS "Vulkan_INCLUDE_DIRS=" ${Vulkan_INCLUDE_DIRS})
     message(STATUS "Vulkan_LIBRARY=" ${Vulkan_LIBRARY})
     message(STATUS "Vulkan_SPIRV_TOOLS_LIBRARY=" ${Vulkan_SPIRV_TOOLS_LIBRARY})
diff --git a/python/tvm/relay/frontend/jit/onnx.py b/python/tvm/relay/frontend/jit/onnx.py
index 0f2a79c..ae10915 100644
--- a/python/tvm/relay/frontend/jit/onnx.py
+++ b/python/tvm/relay/frontend/jit/onnx.py
@@ -21,6 +21,7 @@ import tvm.relay
 import tvm.autotvm as autotvm
 import timeit
 import numpy as np
+import collections
 
 @tvm.register_func("tvm_run_with_benchmark")
 def run_with_benchmark(mod):
@@ -40,18 +41,24 @@ def run_with_benchmark(mod):
 def onnx_compile(model_string, target, target_host, opt_level, input_shapes):
     model = onnx.load_model_from_string(bytes(model_string))
 
-    input_shapes = {name : shape for (name, shape) in zip([i.name for i in model.graph.input], input_shapes)}
+    input_mapping = [(name , shape) for (name, shape) in zip([i.name for i in model.graph.input], input_shapes)]
+    # Using an ordereddict maintains input ordering.
+    shape_dict = collections.OrderedDict(input_mapping)
 
-    irmod, params = tvm.relay.frontend.from_onnx(model, input_shapes, opset=11)
+    irmod, params = tvm.relay.frontend.from_onnx(model, shape_dict, opset=11)
+    print(irmod)
+    # import ipdb; ipdb.set_trace()
     with tvm.relay.build_config(opt_level=opt_level):
         tuning_logfile = os.getenv("AUTOTVM_TUNING_LOG")
         if tuning_logfile:
             with autotvm.apply_history_best(tuning_logfile):
-                graph, lib, params = tvm.relay.build(irmod, target_host=target_host, target=target, params=params)
+                # XXX: do not pass parameters to relay.build otherwise they will be inline into the module
+                lib = tvm.relay.build(irmod, target_host=target_host, target=target)
         else:
-            graph, lib, params = tvm.relay.build(irmod, target_host=target_host, target=target, params=params)
+            lib = tvm.relay.build(irmod, target_host=target_host, target=target)
 
+    print(lib.graph_json)
     ctx = tvm.context(target, 0)
-    m = tvm.contrib.graph_runtime.create(graph, lib, ctx)
-    m.set_input(**params)
+    m = tvm.contrib.graph_runtime.GraphModule(lib["default"](ctx))
+    # m.set_input(**params)
     return m.module
diff --git a/src/runtime/graph/graph_runtime.cc b/src/runtime/graph/graph_runtime.cc
index 6d586cf..5df9068 100644
--- a/src/runtime/graph/graph_runtime.cc
+++ b/src/runtime/graph/graph_runtime.cc
@@ -84,6 +84,13 @@ void GraphRuntime::Init(const std::string& graph_json, tvm::runtime::Module modu
     const uint32_t nid = input_nodes_[i];
     std::string& name = nodes_[nid].name;
     input_map_[name] = i;
+    uint32_t eid = this->entry_id(input_nodes_[i], 0);
+    const DLTensor* old_t = data_entry_[eid].operator->();
+    std::stringstream s;
+    for(int ind = 0; ind < old_t->ndim; ind++) {
+      s << old_t->shape[ind] << " ";
+    }
+    LOG(INFO) << s.str();
   }
 }
 /*!
@@ -120,7 +127,13 @@ void GraphRuntime::SetInputZeroCopy(int index, DLTensor* data_ref) {
 
   // check the consistency of input
   ICHECK_EQ(data_alignment_[eid], details::GetDataAlignment(*data_ref));
-  ICHECK_EQ(reinterpret_cast<size_t>(data_ref->data) % kAllocAlignment, 0);
+  // ICHECK_EQ(reinterpret_cast<size_t>(data_ref->data) % kAllocAlignment, 0) << data_ref->data;
+  for(int i = 0; i < old_t->ndim; i++) {
+    LOG(INFO) << "OLD " << old_t->shape[i];
+  }
+  for(int i = 0; i < data_ref->ndim; i++) {
+    LOG(INFO) << "DATA_REF " << data_ref->shape[i];
+  }
   ICHECK_EQ(old_t->ndim, static_cast<size_t>(data_ref->ndim));
   ICHECK_EQ(old_t->ctx.device_type, data_ref->ctx.device_type);
   ICHECK_EQ(old_t->ctx.device_id, data_ref->ctx.device_id);
diff --git a/src/target/spirv/build_vulkan.cc b/src/target/spirv/build_vulkan.cc
index a0f0b76..0b12c1c 100644
--- a/src/target/spirv/build_vulkan.cc
+++ b/src/target/spirv/build_vulkan.cc
@@ -23,7 +23,7 @@
  */
 // Use libspirv for parsing and validating code.
 #include <dmlc/memory_io.h>
-#include <libspirv.h>
+#include <spirv-tools/libspirv.h>
 #include <tvm/tir/transform.h>
 
 #include "../../runtime/vulkan/vulkan_module.h"
diff --git a/src/target/spirv/intrin_rule_spirv.cc b/src/target/spirv/intrin_rule_spirv.cc
index 90b2eb2..601f74a 100644
--- a/src/target/spirv/intrin_rule_spirv.cc
+++ b/src/target/spirv/intrin_rule_spirv.cc
@@ -20,7 +20,7 @@
 /*!
  * \file intrin_rule_spirv.cc
  */
-#include <GLSL.std.450.h>
+#include <SPIRV/GLSL.std.450.h>
 #include <tvm/runtime/registry.h>
 #include <tvm/tir/builtin.h>
 #include <tvm/tir/expr.h>
diff --git a/src/target/spirv/ir_builder.h b/src/target/spirv/ir_builder.h
index 8a08048..20e2f1b 100644
--- a/src/target/spirv/ir_builder.h
+++ b/src/target/spirv/ir_builder.h
@@ -34,7 +34,7 @@
 #include <unordered_map>
 #include <utility>
 #include <vector>
-#include <spirv.hpp>
+#include <SPIRV/spirv.hpp>
 // clang-format on
 
 namespace tvm {

[tvm] 02/09: [AMD:ONNXRT:TVM] Add packed function to import and compile an ONNX model which can be used as a JIT TVM interface for ONNXRT.

Posted by jw...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jwfromm pushed a commit to branch checkpoint
in repository https://gitbox.apache.org/repos/asf/tvm.git

commit bcb100a069897e73f799416aca863f6147c83c0e
Author: Chris Sullivan <cs...@octoml.ai>
AuthorDate: Thu Aug 20 14:29:23 2020 -0700

    [AMD:ONNXRT:TVM] Add packed function to import and compile
    an ONNX model which can be used as a JIT TVM interface for ONNXRT.
---
 include/tvm/driver/jit_interface.h                 |  4 +-
 python/tvm/relay/frontend/__init__.py              |  1 +
 python/tvm/relay/frontend/{ => jit}/__init__.py    | 22 ++-------
 .../relay/frontend/{__init__.py => jit/onnx.py}    | 34 ++++++-------
 src/driver/driver_api.cc                           | 57 ++++++++--------------
 5 files changed, 40 insertions(+), 78 deletions(-)

diff --git a/include/tvm/driver/jit_interface.h b/include/tvm/driver/jit_interface.h
index 966d5a8..e0906f1 100644
--- a/include/tvm/driver/jit_interface.h
+++ b/include/tvm/driver/jit_interface.h
@@ -3,8 +3,6 @@
 #ifdef __cplusplus
 extern "C" {
     EXPORT_DLL tvm::runtime::Module TVMCompile(const std::string& onnx_txt, const std::string& target, const std::string& target_host, int opt_level);
-    EXPORT_DLL void TVMRun(tvm::runtime::Module& mod, const std::string& name, tvm::runtime::TVMArgs& args, tvm::runtime::TVMRetValue* ret);
-    
-    
+    EXPORT_DLL void TVMRun(tvm::runtime::Module& mod,  std::vector<DLTensor> inputs, std::vector<DLTensor> outputs, tvm::runtime::TVMRetValue* ret);
 }  // TVM_EXTERN_C
 #endif
diff --git a/python/tvm/relay/frontend/__init__.py b/python/tvm/relay/frontend/__init__.py
index 7e16499..460da78 100644
--- a/python/tvm/relay/frontend/__init__.py
+++ b/python/tvm/relay/frontend/__init__.py
@@ -35,3 +35,4 @@ from .darknet import from_darknet
 from .pytorch import from_pytorch
 from .caffe import from_caffe
 from .change_datatype import ChangeDatatype
+from . import jit
diff --git a/python/tvm/relay/frontend/__init__.py b/python/tvm/relay/frontend/jit/__init__.py
similarity index 57%
copy from python/tvm/relay/frontend/__init__.py
copy to python/tvm/relay/frontend/jit/__init__.py
index 7e16499..40cd1fd 100644
--- a/python/tvm/relay/frontend/__init__.py
+++ b/python/tvm/relay/frontend/jit/__init__.py
@@ -15,23 +15,7 @@
 # specific language governing permissions and limitations
 # under the License.
 """
-Frontends for constructing Relay programs.
-
-Contains the model importers currently defined
-for Relay.
+JIT interface implementing packed functions that
+import and compile frontend models
 """
-
-from __future__ import absolute_import
-
-from .mxnet import from_mxnet
-from .mxnet_qnn_op_utils import quantize_conv_bias_mkldnn_from_var
-from .keras import from_keras
-from .onnx import from_onnx
-from .tflite import from_tflite
-from .coreml import from_coreml
-from .caffe2 import from_caffe2
-from .tensorflow import from_tensorflow
-from .darknet import from_darknet
-from .pytorch import from_pytorch
-from .caffe import from_caffe
-from .change_datatype import ChangeDatatype
+from .onnx import *
diff --git a/python/tvm/relay/frontend/__init__.py b/python/tvm/relay/frontend/jit/onnx.py
similarity index 53%
copy from python/tvm/relay/frontend/__init__.py
copy to python/tvm/relay/frontend/jit/onnx.py
index 7e16499..9545395 100644
--- a/python/tvm/relay/frontend/__init__.py
+++ b/python/tvm/relay/frontend/jit/onnx.py
@@ -14,24 +14,22 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
-"""
-Frontends for constructing Relay programs.
+import onnx
+import tvm
+import tvm.relay
 
-Contains the model importers currently defined
-for Relay.
-"""
+@tvm.register_func("tvm_onnx_import_and_compile")
+def onnx_compile(model_string, target, target_host, opt_level):
+    model = onnx.load_model_from_string(bytes(model_string))
 
-from __future__ import absolute_import
+    # input shape from data
+    input_shape = {model.graph.input[0].name: (6,)}
 
-from .mxnet import from_mxnet
-from .mxnet_qnn_op_utils import quantize_conv_bias_mkldnn_from_var
-from .keras import from_keras
-from .onnx import from_onnx
-from .tflite import from_tflite
-from .coreml import from_coreml
-from .caffe2 import from_caffe2
-from .tensorflow import from_tensorflow
-from .darknet import from_darknet
-from .pytorch import from_pytorch
-from .caffe import from_caffe
-from .change_datatype import ChangeDatatype
+    irmod, params = tvm.relay.frontend.from_onnx(model, input_shape, opset=11)
+    with tvm.relay.build_config(opt_level=opt_level):
+        graph, lib, params = tvm.relay.build(irmod, target_host=target_host, target=target, params=params)
+
+    ctx = tvm.context(target, 0)
+    m = tvm.contrib.graph_runtime.create(graph, lib, ctx)
+    m.set_input(**params)
+    return m.module
diff --git a/src/driver/driver_api.cc b/src/driver/driver_api.cc
index 758f019..b876c38 100644
--- a/src/driver/driver_api.cc
+++ b/src/driver/driver_api.cc
@@ -35,6 +35,7 @@
 #include <tvm/runtime/module.h>
 #include <tvm/runtime/packed_func.h>
 #include <tvm/runtime/container.h>
+#include <tvm/runtime/c_runtime_api.h>
 #include <tvm/runtime/registry.h>
 #include <tvm/target/codegen.h>
 #include <tvm/te/operation.h>
@@ -340,45 +341,25 @@ runtime::Module build(const IRModule& funcs, const Target& target, const Target&
 
 tvm::runtime::Module TVMCompile(const std::string& onnx_txt, const std::string& target, const std::string& target_host, int opt_level)
 {
-    auto tensor_type = tvm::relay::TensorType({1, 6}, tvm::runtime::DataType::Float(32));
-    auto X1 = tvm::relay::Var("X1", tensor_type);
-    auto mul_op = tvm::relay::Op::Get("multiply");
-    auto mul1 = tvm::relay::Call(mul_op, {X1, X1}, tvm::Attrs(), {});
-    auto mul2 = tvm::relay::Call(mul_op, {X1, mul1}, tvm::Attrs(), {});
-    auto mul3 = tvm::relay::Call(mul_op, {X1, mul2}, tvm::Attrs(), {});
-    auto Y4 = tvm::relay::Call(mul_op, {X1, mul3}, tvm::Attrs(), {});
-    auto func = tvm::relay::Function(tvm::relay::FreeVars(Y4), Y4, tvm::relay::Type(), {});
-
-    auto reg = tvm::runtime::Registry::Get("ir.RegisterOpAttr");
-    if (!reg)
-        LOG(FATAL) << "no _Register";
-
-    auto fs = tvm::runtime::Registry::Get("jit.strategy");
-    if (!fs)
-        LOG(FATAL) << "No jit strategy registered.";
-
-    auto fgeneric = tvm::GenericFunc::Get("jit.strategy_generic").set_default(*fs);
-    (*reg)("multiply", "FTVMStrategy", fgeneric, 10);
-    (*reg)("multiply", "TShapeDataDependant", false, 10);
-   
-    auto pfb = tvm::runtime::Registry::Get("relay.build_module._BuildModule");
-    tvm::runtime::Module build_mod = (*pfb)();
-    auto build_f = build_mod.GetFunction("build", false);
-    auto mod_f = build_mod.GetFunction("get_module", false);
-    auto relay_mod = tvm::IRModule::FromExpr(func);
-    tvm::Map<tvm::Integer, tvm::Target> targets;
-    // tvm::Target tgt = tvm::Target::Create(target);
-    tvm::Target tgt = tvm::Target::Create("llvm");
-    targets.Set(0, tgt);
-    // tvm::Target host = (target == target_host) ? tgt : tvm::Target::Create(target_host);
-    build_f(relay_mod, targets, tgt);
-    tvm::runtime::Module mod = mod_f();
-    return mod;
+  const tvm::PackedFunc* compile = tvm::runtime::Registry::Get("tvm_onnx_import_and_compile");
+  tvm::runtime::Module mod = (*compile)(TVMByteArray{onnx_txt.data(), onnx_txt.size()}, target, target_host, opt_level);
+  return mod;
+
 }
 
-void TVMRun(tvm::runtime::Module& mod, const std::string& name, tvm::runtime::TVMArgs& args, tvm::runtime::TVMRetValue* ret)
+void TVMRun(tvm::runtime::Module& mod, std::vector<DLTensor> inputs, std::vector<DLTensor> outputs, tvm::runtime::TVMRetValue* ret)
 {
-    mod.GetFunction(name).CallPacked(args, ret);
-    // process return value, refe to TVMFuncCall in c_runtime_api.cc
-    
+  tvm::PackedFunc set_input = mod.GetFunction("set_input_zero_copy", false);
+  for (size_t i = 0; i < inputs.size(); i++)
+  {
+    set_input(i, &inputs[i]);
+  }
+
+  mod.GetFunction("run", false)();
+
+  tvm::PackedFunc get_output = mod.GetFunction("get_output", false);
+  for (size_t i = 0; i < outputs.size(); i++)
+  {
+    get_output(i, &outputs[i]);
+  }
 }