diff --git a/tf_adapter/tests/st/optimizers/testcase/om_partition_subgraphs_pass_test.cc b/tf_adapter/tests/st/optimizers/testcase/om_partition_subgraphs_pass_test.cc index 99f8d8ca85deb9e0966c4d2ac81bfcd396ce44a7..38e0704299108191e82e5d966ed9b68a867a4fcd 100644 --- a/tf_adapter/tests/st/optimizers/testcase/om_partition_subgraphs_pass_test.cc +++ b/tf_adapter/tests/st/optimizers/testcase/om_partition_subgraphs_pass_test.cc @@ -215,7 +215,7 @@ TEST_F(OmOptimizationPassTest, StringInputMaxSizeTest) { TEST_F(OmOptimizationPassTest, NpuOpsIdentifierTest01) { SetLogLevelForC(0); std::string opp_path = __FILE__; - opp_path = opp_path.substr(0, opp_path.rfind("/") + 1); + opp_path = opp_path.substr(0, opp_path.rfind("/") + 1) + "opp_path/"; setenv("ASCEND_OPP_PATH", opp_path.c_str(), 1); std::string path_builtin = opp_path + "built-in"; std::string path_vendors = opp_path + "vendors"; @@ -230,11 +230,12 @@ TEST_F(OmOptimizationPassTest, NpuOpsIdentifierTest01) { EXPECT_EQ(vendors[1], "mdc"); EXPECT_EQ(vendors[2], "lhisi"); ClearLogLevelForC(); + system(("rm -rf " + opp_path).c_str()); } TEST_F(OmOptimizationPassTest, NpuOpsIdentifierTest02) { SetLogLevelForC(0); std::string opp_path = __FILE__; - opp_path = opp_path.substr(0, opp_path.rfind("/") + 1); + opp_path = opp_path.substr(0, opp_path.rfind("/") + 1) + "opp_path/"; setenv("ASCEND_OPP_PATH", opp_path.c_str(), 1); std::string path_builtin = opp_path + "built-in"; std::string path_vendors = opp_path + "vendors"; @@ -247,11 +248,12 @@ TEST_F(OmOptimizationPassTest, NpuOpsIdentifierTest02) { EXPECT_NE(instance, nullptr); delete instance; ClearLogLevelForC(); + system(("rm -rf " + opp_path).c_str()); } TEST_F(OmOptimizationPassTest, NpuOpsIdentifierTest03) { SetLogLevelForC(0); std::string opp_path = __FILE__; - opp_path = opp_path.substr(0, opp_path.rfind("/") + 1); + opp_path = opp_path.substr(0, opp_path.rfind("/") + 1) + "opp_path/"; setenv("ASCEND_OPP_PATH", opp_path.c_str(), 1); std::string path_builtin = opp_path + "built-in"; std::string path_vendors = opp_path + "vendors"; @@ -264,6 +266,72 @@ TEST_F(OmOptimizationPassTest, NpuOpsIdentifierTest03) { EXPECT_NE(instance, nullptr); delete instance; ClearLogLevelForC(); + system(("rm -rf " + opp_path).c_str()); +} +TEST_F(OmOptimizationPassTest, NpuOpsIdentifierTest04) { + SetLogLevelForC(0); + std::string opp_path = __FILE__; + opp_path = opp_path.substr(0, opp_path.rfind("/") + 1) + "opp_path/"; + std::string custom_opp_path = opp_path + "custom_opp_path"; + setenv("ASCEND_OPP_PATH", opp_path.c_str(), 1); + setenv("ASCEND_CUSTOM_OPP_PATH", custom_opp_path.c_str(), 1); + std::string path_builtin = opp_path + "built-in"; + std::string path_vendors = opp_path + "vendors"; + std::string path_config = path_vendors + "/config.ini"; + system(("mkdir -p " + path_builtin).c_str()); + system(("mkdir -p " + path_vendors).c_str()); + system(("echo 'load_priority=customize,mdc,lhisi' > " + path_config).c_str()); + system(("mkdir -p " + custom_opp_path).c_str()); + nlohmann::json allJson; + NpuOpsIdentifier *instance = new NpuOpsIdentifier(false, allJson); + EXPECT_NE(instance, nullptr); + delete instance; + ClearLogLevelForC(); + system(("rm -rf " + opp_path).c_str()); +} +TEST_F(OmOptimizationPassTest, NpuOpsIdentifierTest05) { + SetLogLevelForC(0); + std::string opp_path = __FILE__; + opp_path = opp_path.substr(0, opp_path.rfind("/") + 1) + "opp_path/"; + std::string custom_opp_path = opp_path + "custom_opp_path"; + setenv("ASCEND_OPP_PATH", opp_path.c_str(), 1); + setenv("ASCEND_CUSTOM_OPP_PATH", "", 1); + std::string path_builtin = opp_path + "built-in"; + std::string path_vendors = opp_path + "vendors"; + std::string path_config = path_vendors + "/config.ini"; + system(("mkdir -p " + path_builtin).c_str()); + system(("mkdir -p " + path_vendors).c_str()); + system(("echo 'load_priority=customize,mdc,lhisi' > " + path_config).c_str()); + system(("mkdir -p " + custom_opp_path).c_str()); + nlohmann::json allJson; + NpuOpsIdentifier *instance = new NpuOpsIdentifier(false, allJson); + EXPECT_NE(instance, nullptr); + delete instance; + ClearLogLevelForC(); + system(("rm -rf " + opp_path).c_str()); +} +TEST_F(OmOptimizationPassTest, NpuOpsIdentifierTest06) { + SetLogLevelForC(0); + std::string opp_path = __FILE__; + opp_path = opp_path.substr(0, opp_path.rfind("/") + 1) + "opp_path/"; + std::string custom_opp_path_01 = opp_path + "custom_opp_path_01"; + std::string custom_opp_path_02 = opp_path + "custom_opp_path_02"; + setenv("ASCEND_OPP_PATH", opp_path.c_str(), 1); + setenv("ASCEND_CUSTOM_OPP_PATH", (custom_opp_path_01 + ":" + custom_opp_path_02).c_str(), 1); + std::string path_builtin = opp_path + "built-in"; + std::string path_vendors = opp_path + "vendors"; + std::string path_config = path_vendors + "/config.ini"; + system(("mkdir -p " + path_builtin).c_str()); + system(("mkdir -p " + path_vendors).c_str()); + system(("echo 'load_priority=customize,mdc,lhisi' > " + path_config).c_str()); + system(("mkdir -p " + custom_opp_path_01).c_str()); + system(("mkdir -p " + custom_opp_path_02).c_str()); + nlohmann::json allJson; + NpuOpsIdentifier *instance = new NpuOpsIdentifier(false, allJson); + EXPECT_NE(instance, nullptr); + delete instance; + ClearLogLevelForC(); + system(("rm -rf " + opp_path).c_str()); } } // end namespace } // end tensorflow diff --git a/tf_adapter/tests/ut/optimizers/testcase/om_partition_subgraphs_pass_test.cc b/tf_adapter/tests/ut/optimizers/testcase/om_partition_subgraphs_pass_test.cc index 99f8d8ca85deb9e0966c4d2ac81bfcd396ce44a7..38e0704299108191e82e5d966ed9b68a867a4fcd 100644 --- a/tf_adapter/tests/ut/optimizers/testcase/om_partition_subgraphs_pass_test.cc +++ b/tf_adapter/tests/ut/optimizers/testcase/om_partition_subgraphs_pass_test.cc @@ -215,7 +215,7 @@ TEST_F(OmOptimizationPassTest, StringInputMaxSizeTest) { TEST_F(OmOptimizationPassTest, NpuOpsIdentifierTest01) { SetLogLevelForC(0); std::string opp_path = __FILE__; - opp_path = opp_path.substr(0, opp_path.rfind("/") + 1); + opp_path = opp_path.substr(0, opp_path.rfind("/") + 1) + "opp_path/"; setenv("ASCEND_OPP_PATH", opp_path.c_str(), 1); std::string path_builtin = opp_path + "built-in"; std::string path_vendors = opp_path + "vendors"; @@ -230,11 +230,12 @@ TEST_F(OmOptimizationPassTest, NpuOpsIdentifierTest01) { EXPECT_EQ(vendors[1], "mdc"); EXPECT_EQ(vendors[2], "lhisi"); ClearLogLevelForC(); + system(("rm -rf " + opp_path).c_str()); } TEST_F(OmOptimizationPassTest, NpuOpsIdentifierTest02) { SetLogLevelForC(0); std::string opp_path = __FILE__; - opp_path = opp_path.substr(0, opp_path.rfind("/") + 1); + opp_path = opp_path.substr(0, opp_path.rfind("/") + 1) + "opp_path/"; setenv("ASCEND_OPP_PATH", opp_path.c_str(), 1); std::string path_builtin = opp_path + "built-in"; std::string path_vendors = opp_path + "vendors"; @@ -247,11 +248,12 @@ TEST_F(OmOptimizationPassTest, NpuOpsIdentifierTest02) { EXPECT_NE(instance, nullptr); delete instance; ClearLogLevelForC(); + system(("rm -rf " + opp_path).c_str()); } TEST_F(OmOptimizationPassTest, NpuOpsIdentifierTest03) { SetLogLevelForC(0); std::string opp_path = __FILE__; - opp_path = opp_path.substr(0, opp_path.rfind("/") + 1); + opp_path = opp_path.substr(0, opp_path.rfind("/") + 1) + "opp_path/"; setenv("ASCEND_OPP_PATH", opp_path.c_str(), 1); std::string path_builtin = opp_path + "built-in"; std::string path_vendors = opp_path + "vendors"; @@ -264,6 +266,72 @@ TEST_F(OmOptimizationPassTest, NpuOpsIdentifierTest03) { EXPECT_NE(instance, nullptr); delete instance; ClearLogLevelForC(); + system(("rm -rf " + opp_path).c_str()); +} +TEST_F(OmOptimizationPassTest, NpuOpsIdentifierTest04) { + SetLogLevelForC(0); + std::string opp_path = __FILE__; + opp_path = opp_path.substr(0, opp_path.rfind("/") + 1) + "opp_path/"; + std::string custom_opp_path = opp_path + "custom_opp_path"; + setenv("ASCEND_OPP_PATH", opp_path.c_str(), 1); + setenv("ASCEND_CUSTOM_OPP_PATH", custom_opp_path.c_str(), 1); + std::string path_builtin = opp_path + "built-in"; + std::string path_vendors = opp_path + "vendors"; + std::string path_config = path_vendors + "/config.ini"; + system(("mkdir -p " + path_builtin).c_str()); + system(("mkdir -p " + path_vendors).c_str()); + system(("echo 'load_priority=customize,mdc,lhisi' > " + path_config).c_str()); + system(("mkdir -p " + custom_opp_path).c_str()); + nlohmann::json allJson; + NpuOpsIdentifier *instance = new NpuOpsIdentifier(false, allJson); + EXPECT_NE(instance, nullptr); + delete instance; + ClearLogLevelForC(); + system(("rm -rf " + opp_path).c_str()); +} +TEST_F(OmOptimizationPassTest, NpuOpsIdentifierTest05) { + SetLogLevelForC(0); + std::string opp_path = __FILE__; + opp_path = opp_path.substr(0, opp_path.rfind("/") + 1) + "opp_path/"; + std::string custom_opp_path = opp_path + "custom_opp_path"; + setenv("ASCEND_OPP_PATH", opp_path.c_str(), 1); + setenv("ASCEND_CUSTOM_OPP_PATH", "", 1); + std::string path_builtin = opp_path + "built-in"; + std::string path_vendors = opp_path + "vendors"; + std::string path_config = path_vendors + "/config.ini"; + system(("mkdir -p " + path_builtin).c_str()); + system(("mkdir -p " + path_vendors).c_str()); + system(("echo 'load_priority=customize,mdc,lhisi' > " + path_config).c_str()); + system(("mkdir -p " + custom_opp_path).c_str()); + nlohmann::json allJson; + NpuOpsIdentifier *instance = new NpuOpsIdentifier(false, allJson); + EXPECT_NE(instance, nullptr); + delete instance; + ClearLogLevelForC(); + system(("rm -rf " + opp_path).c_str()); +} +TEST_F(OmOptimizationPassTest, NpuOpsIdentifierTest06) { + SetLogLevelForC(0); + std::string opp_path = __FILE__; + opp_path = opp_path.substr(0, opp_path.rfind("/") + 1) + "opp_path/"; + std::string custom_opp_path_01 = opp_path + "custom_opp_path_01"; + std::string custom_opp_path_02 = opp_path + "custom_opp_path_02"; + setenv("ASCEND_OPP_PATH", opp_path.c_str(), 1); + setenv("ASCEND_CUSTOM_OPP_PATH", (custom_opp_path_01 + ":" + custom_opp_path_02).c_str(), 1); + std::string path_builtin = opp_path + "built-in"; + std::string path_vendors = opp_path + "vendors"; + std::string path_config = path_vendors + "/config.ini"; + system(("mkdir -p " + path_builtin).c_str()); + system(("mkdir -p " + path_vendors).c_str()); + system(("echo 'load_priority=customize,mdc,lhisi' > " + path_config).c_str()); + system(("mkdir -p " + custom_opp_path_01).c_str()); + system(("mkdir -p " + custom_opp_path_02).c_str()); + nlohmann::json allJson; + NpuOpsIdentifier *instance = new NpuOpsIdentifier(false, allJson); + EXPECT_NE(instance, nullptr); + delete instance; + ClearLogLevelForC(); + system(("rm -rf " + opp_path).c_str()); } } // end namespace } // end tensorflow diff --git a/tf_adapter/util/npu_ops_identifier.cc b/tf_adapter/util/npu_ops_identifier.cc index 495c3e806ca8882b9f6028a1421f53261103a09d..15426716d62811c7e2f8d1f99198500b5c209a92 100644 --- a/tf_adapter/util/npu_ops_identifier.cc +++ b/tf_adapter/util/npu_ops_identifier.cc @@ -33,6 +33,7 @@ const static std::string kOpsInfoJsonV01 = "/framework/built-in/tensorflow/npu_s const static std::string kOpsInfoJsonV02 = "/built-in/framework/tensorflow/npu_supported_ops.json"; const static std::string kCustomOpsInfoJsonV01 = "/framework/custom/tensorflow/npu_supported_ops.json"; const static std::string kCustomOpsInfoJsonV02 = "/vendors/%s/framework/tensorflow/npu_supported_ops.json"; +const static std::string kCustomOpsInfoJsonV03 = "/framework/tensorflow/npu_supported_ops.json"; const size_t kVendorConfigPartsCount = 2U; const static std::string kGray = "isGray"; const static std::string kHeavy = "isHeavy"; @@ -80,6 +81,29 @@ bool NpuOpsIdentifier::IsNewOppPathStruct(const std::string &opp_path) { return mmIsDir((opp_path + "/built-in").c_str()) == EN_OK; } +bool NpuOpsIdentifier::GetCustomOpPathFromCustomOppPath(std::vector &custom_ops_json_path_vec) { + ADP_LOG(INFO) << "Start to get plugin path from ASCEND_CUSTOM_OPP_PATH schedule."; + const char *const custom_opp_path_env = std::getenv("ASCEND_CUSTOM_OPP_PATH"); + if (custom_opp_path_env == nullptr) { + ADP_LOG(INFO) << "env ASCEND_CUSTOM_OPP_PATH is not defined."; + return false; + } + const std::string custom_opp_path = custom_opp_path_env; + if (custom_opp_path.empty()) { + ADP_LOG(WARNING) << "env ASCEND_CUSTOM_OPP_PATH is defined but it's empty."; + return false; + } + std::vector custom_paths = ge::StringUtils::Split(custom_opp_path, ':'); + bool is_custom_path_valid = false; + for (const auto &custom_path : custom_paths) { + if ((!custom_path.empty()) && (mmIsDir(custom_path.c_str()) == EN_OK)) { + custom_ops_json_path_vec.push_back(custom_path + kCustomOpsInfoJsonV03); + is_custom_path_valid = true; + } + } + return is_custom_path_valid; +} + bool NpuOpsIdentifier::GetCustomOpPath(const std::string &ops_path, std::string &ops_json_path, std::vector &custom_ops_json_path_vec) { if (!IsNewOppPathStruct(ops_path)) { @@ -88,6 +112,8 @@ bool NpuOpsIdentifier::GetCustomOpPath(const std::string &ops_path, std::string return true; } ops_json_path = ops_path + kOpsInfoJsonV02; + const bool ret_val = GetCustomOpPathFromCustomOppPath(custom_ops_json_path_vec); + ADP_LOG(INFO) << "Run GetCustomOpPathFromCustomOppPath result is: " << (ret_val ? "true" : "false"); std::vector vendors; if (!GetOppPluginVendors(ops_path + "/vendors/config.ini", vendors)) { ADP_LOG(INFO) << "Can not get opp plugin vendors!"; diff --git a/tf_adapter/util/npu_ops_identifier.h b/tf_adapter/util/npu_ops_identifier.h index ce5fd8ca6fb30cc849eb63986b59946ee913eefb..ff1108c56079d457ea2f8fffef3e327a88f6e9d1 100644 --- a/tf_adapter/util/npu_ops_identifier.h +++ b/tf_adapter/util/npu_ops_identifier.h @@ -45,6 +45,7 @@ class NpuOpsIdentifier { int32_t ParseOps(const std::string &f, nlohmann::json &root) const; static bool GetOppPluginVendors(const std::string &vendors_config, std::vector &vendors); static bool IsNewOppPathStruct(const std::string &opp_path); + static bool GetCustomOpPathFromCustomOppPath(std::vector &custom_ops_json_path_vec); static bool GetCustomOpPath(const std::string &ops_path, std::string &ops_json_path, std::vector &custom_ops_json_path_vec); const bool is_mix_;