From 69d080d831fd23a0d8b1435b3358e4115dfa3940 Mon Sep 17 00:00:00 2001 From: Yong Wu Date: Tue, 8 Aug 2023 15:54:49 -0700 Subject: [PATCH 1/3] Remove IRModule Dependency from Target --- include/tvm/relay/transform.h | 12 +++++++++ include/tvm/target/target.h | 10 ------- include/tvm/target/target_kind.h | 27 +------------------ include/tvm/tir/usmp/utils.h | 1 + src/driver/driver_api.cc | 17 ++++++++++++ src/relay/backend/contrib/cmsisnn/target.cc | 4 +-- src/relay/backend/contrib/codegen_c/target.cc | 2 +- src/relay/backend/contrib/cutlass/target.cc | 2 +- .../contrib/example_target_hooks/target.cc | 6 +++-- src/relay/backend/contrib/tensorrt/target.cc | 2 +- src/target/codegen.cc | 3 +++ src/target/target.cc | 15 +++-------- 12 files changed, 46 insertions(+), 55 deletions(-) diff --git a/include/tvm/relay/transform.h b/include/tvm/relay/transform.h index 4f5b5d146d92..6fbb88fc4ce8 100644 --- a/include/tvm/relay/transform.h +++ b/include/tvm/relay/transform.h @@ -47,6 +47,18 @@ using PassInfoNode = tvm::transform::PassInfoNode; using PassContext = tvm::transform::PassContext; using PassContextNode = tvm::transform::PassContextNode; using Sequential = tvm::transform::Sequential; +using FTVMRelayToTIR = tvm::transform::Pass; +/*! + * \brief TIRToRuntime conversion specific to a TargetKind + * + * This function is responsible for scanning an IRModule for appropriate Target-specific functions + and generating a Runtime module representing the compiled output + * + * \param ir_module Unified IRModule + * \param target Target to filter on or retrieve arguments from + * \return Runtime Module containing compiled functions + */ +using FTVMTIRToRuntime = tvm::runtime::TypedPackedFunc; /* * \brief Create a function pass. diff --git a/include/tvm/target/target.h b/include/tvm/target/target.h index 56d6a596b9b2..d47ac94e067e 100644 --- a/include/tvm/target/target.h +++ b/include/tvm/target/target.h @@ -25,7 +25,6 @@ #define TVM_TARGET_TARGET_H_ #include -#include #include #include #include @@ -284,14 +283,5 @@ class Target : public ObjectRef { */ void CheckAndUpdateHostConsistency(Target* target, Target* host); -/*! - * \brief Check and update host field of the given legacy heterogeneous targets and - * target host.Note that this function is for legacy target api compatibility issue only, - * not recommended for other use. - * \param ir_modules The pointer to a Map objects with keys being Target objects - * \param host The Target typed object for target host to be updated - */ -void CheckAndUpdateHostConsistency(Map* ir_modules, Target* host); - } // namespace tvm #endif // TVM_TARGET_TARGET_H_ diff --git a/include/tvm/target/target_kind.h b/include/tvm/target/target_kind.h index 19bcce3116b2..86f386abb827 100644 --- a/include/tvm/target/target_kind.h +++ b/include/tvm/target/target_kind.h @@ -24,7 +24,6 @@ #ifndef TVM_TARGET_TARGET_KIND_H_ #define TVM_TARGET_TARGET_KIND_H_ -#include #include #include @@ -50,31 +49,7 @@ using TargetFeatures = Map; * \return The transformed Target JSON object. */ using TargetJSON = Map; -using FTVMTargetParser = TypedPackedFunc; - -/*! - * \brief RelayToTIR tvm::transform::Pass specific to a TargetKind - * - * Called before the default lowering passes. - * - * \param mod The module that an optimization pass runs on. - * \param pass_ctx The pass context that can provide information for the optimization. - * - * \return The transformed module. - */ -using FTVMRelayToTIR = transform::Pass; - -/*! - * \brief TIRToRuntime conversion specific to a TargetKind - * - * This function is responsible for scanning an IRModule for appropriate Target-specific functions - and generating a Runtime module representing the compiled output - * - * \param ir_module Unified IRModule - * \param target Target to filter on or retrieve arguments from - * \return Runtime Module containing compiled functions - */ -using FTVMTIRToRuntime = runtime::TypedPackedFunc; +using FTVMTargetParser = runtime::TypedPackedFunc; namespace detail { template diff --git a/include/tvm/tir/usmp/utils.h b/include/tvm/tir/usmp/utils.h index f49e9ceef794..a67350a2bb13 100644 --- a/include/tvm/tir/usmp/utils.h +++ b/include/tvm/tir/usmp/utils.h @@ -27,6 +27,7 @@ #include #include +#include #include #include #include diff --git a/src/driver/driver_api.cc b/src/driver/driver_api.cc index d46fab716814..b7ba0ffe4468 100644 --- a/src/driver/driver_api.cc +++ b/src/driver/driver_api.cc @@ -431,6 +431,23 @@ std::pair SplitMixedModule(IRModule mod_mixed, const Target& return {host_mod, device_mod}; } +/*! + * \brief Check and update host field of the given legacy heterogeneous targets and + * target host.Note that this function is for legacy target api compatibility issue only, + * not recommended for other use. + * \param ir_modules The pointer to a Map objects with keys being Target objects + * \param host The Target typed object for target host to be updated + */ +void CheckAndUpdateHostConsistency(Map* targets, Target* host) { + Map new_targets; + for (auto& it : *targets) { + auto target = it.first; + CheckAndUpdateHostConsistency(&target, host); + new_targets.Set(target, it.second); + } + *targets = new_targets; +} + runtime::Module TIRToRuntime(const Map& inputs_arg, const Target& target_host_arg) { std::vector device_modules; diff --git a/src/relay/backend/contrib/cmsisnn/target.cc b/src/relay/backend/contrib/cmsisnn/target.cc index f14c106703b3..527fba98c029 100644 --- a/src/relay/backend/contrib/cmsisnn/target.cc +++ b/src/relay/backend/contrib/cmsisnn/target.cc @@ -37,8 +37,8 @@ TVM_REGISTER_TARGET_KIND("cmsis-nn", kDLCPU) .add_attr_option>("mattr") .add_attr_option("mcpu") .add_attr_option("debug_last_error") - .set_attr(tvm::attr::kRelayToTIR, RelayToTIR()) - .set_attr("TIRToRuntime", TIRToRuntime) + .set_attr(tvm::attr::kRelayToTIR, RelayToTIR()) + .set_attr("TIRToRuntime", TIRToRuntime) .set_target_parser(tvm::target::parsers::cpu::ParseTarget); } // namespace cmsisnn diff --git a/src/relay/backend/contrib/codegen_c/target.cc b/src/relay/backend/contrib/codegen_c/target.cc index 623057ac1762..cd1e0283df28 100644 --- a/src/relay/backend/contrib/codegen_c/target.cc +++ b/src/relay/backend/contrib/codegen_c/target.cc @@ -34,7 +34,7 @@ namespace contrib { */ TVM_REGISTER_TARGET_KIND("ccompiler", kDLCPU) .set_attr(tvm::attr::kIsExternalCodegen, Bool(true)) - .set_attr(tvm::attr::kRelayToTIR, CCompilerPass()) + .set_attr(tvm::attr::kRelayToTIR, CCompilerPass()) // Value is prepended to every output CModule. .add_attr_option("header", String("")); diff --git a/src/relay/backend/contrib/cutlass/target.cc b/src/relay/backend/contrib/cutlass/target.cc index 7b377f340a57..50c8b84a9069 100644 --- a/src/relay/backend/contrib/cutlass/target.cc +++ b/src/relay/backend/contrib/cutlass/target.cc @@ -40,7 +40,7 @@ namespace cutlass { */ TVM_REGISTER_TARGET_KIND("cutlass", kDLCUDA) .set_attr(tvm::attr::kIsExternalCodegen, Bool(true)) - .set_attr("RelayToTIR", CompileForCutlass()) + .set_attr("RelayToTIR", CompileForCutlass()) // An integer specifying the compute capability. For example, 75 for Turing and // 80 or 86 for Ampere. .add_attr_option("sm", Integer(80)) diff --git a/src/relay/backend/contrib/example_target_hooks/target.cc b/src/relay/backend/contrib/example_target_hooks/target.cc index b01c23ed806a..275efaa93336 100644 --- a/src/relay/backend/contrib/example_target_hooks/target.cc +++ b/src/relay/backend/contrib/example_target_hooks/target.cc @@ -33,8 +33,10 @@ runtime::Module TIRToRuntime(IRModule mod, Target target); TVM_REGISTER_TARGET_KIND("example_target_hook", kDLCPU) .set_attr("use_device_api", Bool(true)) - .set_attr(attr::kRelayToTIR, relay::contrib::example_target_hooks::RelayToTIR()) - .set_attr("TIRToRuntime", relay::contrib::example_target_hooks::TIRToRuntime) + .set_attr(attr::kRelayToTIR, + relay::contrib::example_target_hooks::RelayToTIR()) + .set_attr( + "TIRToRuntime", relay::contrib::example_target_hooks::TIRToRuntime) .add_attr_option("example_attribute", Integer(0)); } // namespace tvm diff --git a/src/relay/backend/contrib/tensorrt/target.cc b/src/relay/backend/contrib/tensorrt/target.cc index 2e4581d30a3c..0277787a8c12 100644 --- a/src/relay/backend/contrib/tensorrt/target.cc +++ b/src/relay/backend/contrib/tensorrt/target.cc @@ -39,7 +39,7 @@ namespace tensorrt { */ TVM_REGISTER_TARGET_KIND("tensorrt", kDLCUDA) .set_attr(tvm::attr::kIsExternalCodegen, Bool(true)) - .set_attr("RelayToTIR", CompileForTensorRT()) + .set_attr("RelayToTIR", CompileForTensorRT()) // A array of three integers given the major, minor, and patch numbers for the supported // TensorRT compiler version. If empty will be auto-detected from linked library. Default empty. .add_attr_option>("tensorrt_version", Array()) diff --git a/src/target/codegen.cc b/src/target/codegen.cc index bbb2c15a647f..55af8889e120 100644 --- a/src/target/codegen.cc +++ b/src/target/codegen.cc @@ -38,6 +38,9 @@ #include namespace tvm { + +using FTVMTIRToRuntime = runtime::TypedPackedFunc; + namespace codegen { runtime::Module Build(IRModule mod, Target target) { diff --git a/src/target/target.cc b/src/target/target.cc index 2f585188d0d3..cd2e3714e422 100644 --- a/src/target/target.cc +++ b/src/target/target.cc @@ -21,6 +21,7 @@ * \file src/target/target.cc */ #include +#include #include #include #include @@ -91,16 +92,6 @@ void CheckAndUpdateHostConsistency(Target* target, Target* host) { *host = (*target)->GetHost().value_or(Target()); } -void CheckAndUpdateHostConsistency(Map* targets, Target* host) { - Map new_targets; - for (auto& it : *targets) { - auto target = it.first; - CheckAndUpdateHostConsistency(&target, host); - new_targets.Set(target, it.second); - } - *targets = new_targets; -} - static std::vector DeduplicateKeys(const std::vector& keys) { std::vector new_keys; for (size_t i = 0; i < keys.size(); ++i) { @@ -614,8 +605,8 @@ Target::Target(TargetKind kind, Optional host, String tag, Array is_external_codegen_map = TargetKind::GetAttrMap(tvm::attr::kIsExternalCodegen); - TargetKindAttrMap relay_to_tir_map = - TargetKind::GetAttrMap(tvm::attr::kRelayToTIR); + TargetKindAttrMap relay_to_tir_map = + TargetKind::GetAttrMap(tvm::attr::kRelayToTIR); return is_external_codegen_map.get(get()->kind, Bool(false)) || relay_to_tir_map.count(get()->kind); } From e696b34c913f602447a6943f14fba7f0c990c937 Mon Sep 17 00:00:00 2001 From: Yong Wu Date: Tue, 8 Aug 2023 18:02:07 -0700 Subject: [PATCH 2/3] fix build --- src/relay/backend/contrib/ethosu/codegen.cc | 4 +-- src/relay/backend/contrib/uma/targets.cc | 28 ++++++++++----------- tests/cpp/target_test.cc | 3 ++- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/relay/backend/contrib/ethosu/codegen.cc b/src/relay/backend/contrib/ethosu/codegen.cc index f35d4c6d48b2..2e635455e9f5 100644 --- a/src/relay/backend/contrib/ethosu/codegen.cc +++ b/src/relay/backend/contrib/ethosu/codegen.cc @@ -320,8 +320,8 @@ runtime::Module TIRToRuntime(IRModule mod, Target target) { TVM_REGISTER_TARGET_KIND("ethos-u", kDLCPU) .set_attr("use_device_api", Bool(true)) - .set_attr(tvm::attr::kRelayToTIR, RelayToTIR()) - .set_attr("TIRToRuntime", TIRToRuntime); + .set_attr(tvm::attr::kRelayToTIR, RelayToTIR()) + .set_attr("TIRToRuntime", TIRToRuntime); } // namespace ethosu } // namespace contrib diff --git a/src/relay/backend/contrib/uma/targets.cc b/src/relay/backend/contrib/uma/targets.cc index e2fe644cb9bf..d01f5b4c7349 100644 --- a/src/relay/backend/contrib/uma/targets.cc +++ b/src/relay/backend/contrib/uma/targets.cc @@ -46,20 +46,20 @@ TVM_REGISTER_GLOBAL("relay.backend.contrib.uma.RegisterTarget") } } - auto target_kind = - TargetKindRegEntry::RegisterOrGet(target_name) - .set_name() - .set_default_device_type(kDLCPU) - .add_attr_option>("keys") - .add_attr_option("tag") - .add_attr_option("device") - .add_attr_option("model") - .add_attr_option>("libs") - .add_attr_option("host") - .add_attr_option("from_device") - .set_attr(attr::kRelayToTIR, - relay::contrib::uma::RelayToTIR(target_name)) - .set_attr("TIRToRuntime", relay::contrib::uma::TIRToRuntime); + auto target_kind = TargetKindRegEntry::RegisterOrGet(target_name) + .set_name() + .set_default_device_type(kDLCPU) + .add_attr_option>("keys") + .add_attr_option("tag") + .add_attr_option("device") + .add_attr_option("model") + .add_attr_option>("libs") + .add_attr_option("host") + .add_attr_option("from_device") + .set_attr( + attr::kRelayToTIR, relay::contrib::uma::RelayToTIR(target_name)) + .set_attr( + "TIRToRuntime", relay::contrib::uma::TIRToRuntime); // target kind attrs inventory auto kind = TargetKind::Get(target_name).value(); diff --git a/tests/cpp/target_test.cc b/tests/cpp/target_test.cc index 37a8eeb44840..50a6f2f2ac16 100644 --- a/tests/cpp/target_test.cc +++ b/tests/cpp/target_test.cc @@ -458,7 +458,8 @@ TVM_REGISTER_TARGET_KIND("test_external_codegen_2", kDLMetal) .set_attr(tvm::attr::kIsExternalCodegen, Bool(true)); TVM_REGISTER_TARGET_KIND("test_external_codegen_3", kDLCPU) - .set_attr(tvm::attr::kRelayToTIR, tvm::relay::transform::InferType()); + .set_attr(tvm::attr::kRelayToTIR, + tvm::relay::transform::InferType()); TEST(Target, ExternalCodegen) { Target regular("cuda"); From 0e268d1df989ad2d5099443bb5ad6236929d78f0 Mon Sep 17 00:00:00 2001 From: Yong Wu Date: Thu, 10 Aug 2023 13:08:32 -0700 Subject: [PATCH 3/3] fix comments --- include/tvm/relay/transform.h | 16 +++++----- src/relay/backend/contrib/cmsisnn/target.cc | 3 +- src/relay/backend/contrib/ethosu/codegen.cc | 4 ++- .../contrib/example_target_hooks/target.cc | 5 ++-- src/relay/backend/contrib/uma/targets.cc | 30 ++++++++++--------- src/target/codegen.cc | 14 +++++++-- 6 files changed, 44 insertions(+), 28 deletions(-) diff --git a/include/tvm/relay/transform.h b/include/tvm/relay/transform.h index 6fbb88fc4ce8..8e25574b7652 100644 --- a/include/tvm/relay/transform.h +++ b/include/tvm/relay/transform.h @@ -47,18 +47,18 @@ using PassInfoNode = tvm::transform::PassInfoNode; using PassContext = tvm::transform::PassContext; using PassContextNode = tvm::transform::PassContextNode; using Sequential = tvm::transform::Sequential; -using FTVMRelayToTIR = tvm::transform::Pass; + /*! - * \brief TIRToRuntime conversion specific to a TargetKind + * \brief RelayToTIR tvm::transform::Pass specific to a TargetKind * - * This function is responsible for scanning an IRModule for appropriate Target-specific functions - and generating a Runtime module representing the compiled output + * Called before the default lowering passes. * - * \param ir_module Unified IRModule - * \param target Target to filter on or retrieve arguments from - * \return Runtime Module containing compiled functions + * \param mod The module that an optimization pass runs on. + * \param pass_ctx The pass context that can provide information for the optimization. + * + * \return The transformed module. */ -using FTVMTIRToRuntime = tvm::runtime::TypedPackedFunc; +using FTVMRelayToTIR = tvm::transform::Pass; /* * \brief Create a function pass. diff --git a/src/relay/backend/contrib/cmsisnn/target.cc b/src/relay/backend/contrib/cmsisnn/target.cc index 527fba98c029..10125bf814ad 100644 --- a/src/relay/backend/contrib/cmsisnn/target.cc +++ b/src/relay/backend/contrib/cmsisnn/target.cc @@ -32,13 +32,14 @@ namespace cmsisnn { tvm::transform::Pass RelayToTIR(); runtime::Module TIRToRuntime(IRModule mod, Target target); +using FTVMTIRToRuntime = tvm::runtime::TypedPackedFunc; TVM_REGISTER_TARGET_KIND("cmsis-nn", kDLCPU) .add_attr_option>("mattr") .add_attr_option("mcpu") .add_attr_option("debug_last_error") .set_attr(tvm::attr::kRelayToTIR, RelayToTIR()) - .set_attr("TIRToRuntime", TIRToRuntime) + .set_attr("TIRToRuntime", TIRToRuntime) .set_target_parser(tvm::target::parsers::cpu::ParseTarget); } // namespace cmsisnn diff --git a/src/relay/backend/contrib/ethosu/codegen.cc b/src/relay/backend/contrib/ethosu/codegen.cc index 2e635455e9f5..54d0595c4634 100644 --- a/src/relay/backend/contrib/ethosu/codegen.cc +++ b/src/relay/backend/contrib/ethosu/codegen.cc @@ -47,6 +47,8 @@ namespace relay { namespace contrib { namespace ethosu { +using FTVMTIRToRuntime = tvm::runtime::TypedPackedFunc; + /*! * \brief This mutator outlines functions that are marked with a named * "Compiler" attribute. Functions that do not match this condition remain @@ -321,7 +323,7 @@ runtime::Module TIRToRuntime(IRModule mod, Target target) { TVM_REGISTER_TARGET_KIND("ethos-u", kDLCPU) .set_attr("use_device_api", Bool(true)) .set_attr(tvm::attr::kRelayToTIR, RelayToTIR()) - .set_attr("TIRToRuntime", TIRToRuntime); + .set_attr("TIRToRuntime", TIRToRuntime); } // namespace ethosu } // namespace contrib diff --git a/src/relay/backend/contrib/example_target_hooks/target.cc b/src/relay/backend/contrib/example_target_hooks/target.cc index 275efaa93336..b45987f6be33 100644 --- a/src/relay/backend/contrib/example_target_hooks/target.cc +++ b/src/relay/backend/contrib/example_target_hooks/target.cc @@ -22,6 +22,8 @@ namespace tvm { +using FTVMTIRToRuntime = tvm::runtime::TypedPackedFunc; + namespace relay { namespace contrib { namespace example_target_hooks { @@ -35,8 +37,7 @@ TVM_REGISTER_TARGET_KIND("example_target_hook", kDLCPU) .set_attr("use_device_api", Bool(true)) .set_attr(attr::kRelayToTIR, relay::contrib::example_target_hooks::RelayToTIR()) - .set_attr( - "TIRToRuntime", relay::contrib::example_target_hooks::TIRToRuntime) + .set_attr("TIRToRuntime", relay::contrib::example_target_hooks::TIRToRuntime) .add_attr_option("example_attribute", Integer(0)); } // namespace tvm diff --git a/src/relay/backend/contrib/uma/targets.cc b/src/relay/backend/contrib/uma/targets.cc index d01f5b4c7349..244f243749c1 100644 --- a/src/relay/backend/contrib/uma/targets.cc +++ b/src/relay/backend/contrib/uma/targets.cc @@ -28,6 +28,8 @@ namespace tvm { +using FTVMTIRToRuntime = tvm::runtime::TypedPackedFunc; + namespace relay { namespace contrib { namespace uma { @@ -46,20 +48,20 @@ TVM_REGISTER_GLOBAL("relay.backend.contrib.uma.RegisterTarget") } } - auto target_kind = TargetKindRegEntry::RegisterOrGet(target_name) - .set_name() - .set_default_device_type(kDLCPU) - .add_attr_option>("keys") - .add_attr_option("tag") - .add_attr_option("device") - .add_attr_option("model") - .add_attr_option>("libs") - .add_attr_option("host") - .add_attr_option("from_device") - .set_attr( - attr::kRelayToTIR, relay::contrib::uma::RelayToTIR(target_name)) - .set_attr( - "TIRToRuntime", relay::contrib::uma::TIRToRuntime); + auto target_kind = + TargetKindRegEntry::RegisterOrGet(target_name) + .set_name() + .set_default_device_type(kDLCPU) + .add_attr_option>("keys") + .add_attr_option("tag") + .add_attr_option("device") + .add_attr_option("model") + .add_attr_option>("libs") + .add_attr_option("host") + .add_attr_option("from_device") + .set_attr( + attr::kRelayToTIR, relay::contrib::uma::RelayToTIR(target_name)) + .set_attr("TIRToRuntime", relay::contrib::uma::TIRToRuntime); // target kind attrs inventory auto kind = TargetKind::Get(target_name).value(); diff --git a/src/target/codegen.cc b/src/target/codegen.cc index 55af8889e120..6e31db4f608f 100644 --- a/src/target/codegen.cc +++ b/src/target/codegen.cc @@ -39,10 +39,20 @@ namespace tvm { -using FTVMTIRToRuntime = runtime::TypedPackedFunc; - namespace codegen { +/*! + * \brief TIRToRuntime conversion specific to a TargetKind + * + * This function is responsible for scanning an IRModule for appropriate Target-specific functions + and generating a Runtime module representing the compiled output + * + * \param ir_module Unified IRModule + * \param target Target to filter on or retrieve arguments from + * \return Runtime Module containing compiled functions + */ +using FTVMTIRToRuntime = tvm::runtime::TypedPackedFunc; + runtime::Module Build(IRModule mod, Target target) { if (transform::PassContext::Current() ->GetConfig("tir.disable_assert", Bool(false))