areusch commented on a change in pull request #8423:
URL: https://github.com/apache/tvm/pull/8423#discussion_r692429689



##########
File path: src/target/target_kind.cc
##########
@@ -70,6 +70,15 @@ Optional<TargetKind> TargetKind::Get(const String& 
target_kind_name) {
   return reg->kind_;
 }
 
+const PackedFunc* TargetKind::GetRegisteredHook(const String& hook_name) const 
{
+  auto map = tvm::TargetKind::GetAttrMap<String>(hook_name);
+  if (map.count(*this)) {
+    std::string hook_function = map[*this];
+    return tvm::runtime::Registry::Get(hook_function);

Review comment:
       should we `CHECK` if the return value is not nullptr here?

##########
File path: tests/python/relay/utils/external_codegen.py
##########
@@ -0,0 +1,89 @@
+# 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.
+"""Utilities for testing external code generation"""
+
+import os
+
+import tvm
+from tvm import relay, runtime
+from tvm.contrib import utils
+from tests.python.relay.aot.aot_test_utils import AOTTestModel, compile_and_run
+
+
+def update_lib(lib):
+    test_dir = os.path.dirname(os.path.realpath(os.path.expanduser(__file__)))
+    source_dir = os.path.join(test_dir, "..", "..", "..")
+    contrib_path = os.path.join(source_dir, "src", "runtime", "contrib")
+
+    kwargs = {}
+    kwargs["options"] = ["-O2", "-std=c++14", "-I" + contrib_path]
+    tmp_path = utils.tempdir()
+    lib_name = "lib.so"
+    lib_path = tmp_path.relpath(lib_name)
+    lib.export_library(lib_path, fcompile=False, **kwargs)
+    lib = tvm.runtime.load_module(lib_path)
+
+    return lib
+
+
+def check_vm_result(mod, map_inputs, out_shape, result, tol=1e-5, 
target="llvm", device=tvm.cpu()):
+    with tvm.transform.PassContext(opt_level=3, 
disabled_pass=["AlterOpLayout"]):
+        exe = relay.vm.compile(mod, target=target)
+    code, lib = exe.save()
+    lib = update_lib(lib)
+    exe = runtime.vm.Executable.load_exec(code, lib)
+    vm = runtime.vm.VirtualMachine(exe, device)
+    out = vm.run(**map_inputs)
+    tvm.testing.assert_allclose(out.numpy(), result, rtol=tol, atol=tol)
+
+
+def check_graph_executor_result(
+    mod, map_inputs, out_shape, result, tol=1e-5, target="llvm", 
device=tvm.cpu()
+):
+    with tvm.transform.PassContext(opt_level=3, 
disabled_pass=["AlterOpLayout"]):
+        json, lib, _ = relay.build(mod, target=target)

Review comment:
       can you avoid the tuple syntax?

##########
File path: src/target/target_kind.cc
##########
@@ -431,6 +431,9 @@ TVM_REGISTER_TARGET_KIND("hybrid", kDLCPU)  // line break
 
 TVM_REGISTER_TARGET_KIND("composite", 
kDLCPU).add_attr_option<Array<Target>>("devices");
 
+TVM_REGISTER_TARGET_KIND("test", kDLCPU)
+    .set_attr<String>("relay_to_tir", "target.test.tir_lowering");
+

Review comment:
       would you guys be up for adding a context manager to tvm.testing to do 
this? it has bitten us elsewhere. might need to add a way to delete the 
function, and should verify/error in such a context manager if, on deletion, 
it's detected that the global func was overridden




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@tvm.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to