diff --git a/common/json_parser.cpp b/common/json_parser.cpp index 081d5bdf20afdf123f64b1869d6a184076c4545e..4c27b3fb30d401ac1fcbad404cc5225a55ecc360 100755 --- a/common/json_parser.cpp +++ b/common/json_parser.cpp @@ -273,16 +273,12 @@ namespace acl { try { for (auto &item : cannInfoMap) { auto &cannInfo = item.second; - const auto config = js.find(cannInfo.configKeyName); + const auto config = js.find(cannInfo.readableAttrName); if (config != js.end()) { const auto runtimeIter = config->find(SW_CONFIG_RUNTIME); if (runtimeIter != config->end()) { ACL_REQUIRES_OK(CannInfoUtils::ParseVersionValue( - runtimeIter->get(), &cannInfo.runtimeVersion)); - } - const auto socIter = config->find(SW_CONFIG_SOC_VERSION); - if (socIter != config->end()) { - cannInfo.socVersions = socIter->get>(); + runtimeIter->get(), &cannInfo.minimumRuntimeVersion)); } } } diff --git a/config/swFeatureList.json b/config/swFeatureList.json index 9e94d9e28211a88e1b8a4bb2605037623680a159..09e99fdfb1ea81112fb5eb47fc9921ff1c4c1ebb 100644 --- a/config/swFeatureList.json +++ b/config/swFeatureList.json @@ -1,25 +1,9 @@ { "BF16": { - "runtimeVersion": "7.1", - "socVersionList": [ - "Ascend910B1", - "Ascend910B2", - "Ascend910B2C", - "Ascend910B3", - "Ascend910B4", - "Ascend910_9" - ] + "runtimeVersion": "7.1" }, "INF_NAN": { - "runtimeVersion": "7.1", - "socVersionList": [ - "Ascend910B1", - "Ascend910B2", - "Ascend910B2C", - "Ascend910B3", - "Ascend910B4", - "Ascend910_9" - ] + "runtimeVersion": "7.1" }, "JIT_COMPILE": { "runtimeVersion": "6.4" diff --git a/tests/depends/acl_stub.h b/tests/depends/acl_stub.h index 7b803de4017eec16792c7ad2eb17785a1c4981aa..24af6b6f7f7c73afc533788f823192f5fa27ed2b 100755 --- a/tests/depends/acl_stub.h +++ b/tests/depends/acl_stub.h @@ -294,6 +294,7 @@ public: virtual rtError_t rtKernelLaunch(const void *stubFunc, uint32_t blockDim, void *args, uint32_t argsSize, rtSmDesc_t *smDesc, rtStream_t stream); virtual rtError_t rtGetSocVersion(char *version, const uint32_t maxLen); + virtual rtError_t rtGetSocSpec(const char *label, const char *key, char *value, uint32_t maxLen); virtual rtError_t rtGetGroupCount(uint32_t *count); virtual rtError_t rtGetGroupInfo(int32_t groupid, rtGroupInfo_t *groupInfo, uint32_t count); virtual rtError_t rtSetGroup(int32_t groupid); @@ -818,6 +819,7 @@ public: MOCK_METHOD6(rtKernelLaunch, rtError_t(const void *stubFunc, uint32_t blockDim, void *args, uint32_t argsSize, rtSmDesc_t *smDesc, rtStream_t stream)); MOCK_METHOD2(rtGetSocVersion, rtError_t(char *version, const uint32_t maxLen)); + MOCK_METHOD4(rtGetSocSpec, rtError_t(const char *label, const char *key, char *value, uint32_t maxLen)); MOCK_METHOD1(rtGetGroupCount, rtError_t(uint32_t *count)); MOCK_METHOD3(rtGetGroupInfo, rtError_t(int32_t groupid, rtGroupInfo_t *groupInfo, uint32_t count)); MOCK_METHOD1(rtSetGroup, rtError_t(int32_t groupid)); diff --git a/tests/depends/runtime/src/runtime_stub.cpp b/tests/depends/runtime/src/runtime_stub.cpp index 1fe0f2f27719c4b686126ad55c01fde0853fcdcb..299edca7f9858b327b28be8718a3ccef62f28ebb 100755 --- a/tests/depends/runtime/src/runtime_stub.cpp +++ b/tests/depends/runtime/src/runtime_stub.cpp @@ -447,6 +447,13 @@ rtError_t aclStub::rtGetSocVersion(char *version, const uint32_t maxLen) return RT_ERROR_NONE; } +rtError_t aclStub::rtGetSocSpec(const char *label, const char *key, char *value, uint32_t maxLen) +{ + value[0] = '1'; + value[1] = '\0'; + return RT_ERROR_NONE; +} + rtError_t aclStub::rtGetGroupCount(uint32_t *count) { *count = 2; @@ -2179,6 +2186,13 @@ rtError_t rtGetSocVersion(char *version, const uint32_t maxLen) return MockFunctionTest::aclStubInstance().rtGetSocVersion(version, maxLen); } +rtError_t rtGetSocSpec(const char *label, const char *key, char *value, uint32_t maxLen) +{ + value[0] = '1'; + value[1] = '\0'; + return MockFunctionTest::aclStubInstance().rtGetSocSpec(label, key, value, maxLen); +} + rtError_t rtGetGroupCount(uint32_t *count) { *count = 2; diff --git a/tests/ut/acl/testcase/acl_capability_unittest.cpp b/tests/ut/acl/testcase/acl_capability_unittest.cpp index d3a1087ef38f8054d95eb7fd911f8d973cfbb6d5..17bcdb18bbc7f0123796cab78815a20ac0b399ce 100644 --- a/tests/ut/acl/testcase/acl_capability_unittest.cpp +++ b/tests/ut/acl/testcase/acl_capability_unittest.cpp @@ -52,7 +52,7 @@ namespace { { std::ofstream ofs(fakeConfigFile, std::ios::trunc); // soc version is not a list, parse failed - nlohmann::json js = R"({"INF_NAN": {"runtimeVersion" : "7.1","socVersionList" : "910B"}})"_json; + nlohmann::json js = R"({"INF_NAN": {"runtimeVersion" : "7.1"}})"_json; ofs << js.dump(); ofs.close(); } diff --git a/utils/cann_info_utils.cpp b/utils/cann_info_utils.cpp index ab96d498f2e0b86bbbe5cd269845efe3543642c6..2dbc65627620d4e62b83055c7180acc0ef8d96eb 100644 --- a/utils/cann_info_utils.cpp +++ b/utils/cann_info_utils.cpp @@ -30,17 +30,16 @@ namespace acl { std::mutex CannInfoUtils::mutex_; bool CannInfoUtils::initFlag_ = false; - CannInfo CannInfoUtils::currCannInfo_("", UNKNOWN_VERSION); + int32_t CannInfoUtils::currentRuntimeVersion_ = UNKNOWN_VERSION; std::string CannInfoUtils::swConfigPath_; std::string CannInfoUtils::defaultInstallPath_; aclCannAttr CannInfoUtils::attrArray_[MAX_CANN_ATTR_SIZE]; size_t CannInfoUtils::attrNum_ = 0; - // INF_NAN, BF16 and JIT_COMPILE are not required to check runtime and driver version std::map CannInfoUtils::attrToCannInfo_ = { - {ACL_CANN_ATTR_INF_NAN, CannInfo("INF_NAN", UNKNOWN_VERSION)}, - {ACL_CANN_ATTR_BF16, CannInfo("BF16", UNKNOWN_VERSION)}, - {ACL_CANN_ATTR_JIT_COMPILE, CannInfo("JIT_COMPILE", UNKNOWN_VERSION)}, + {ACL_CANN_ATTR_INF_NAN, CannInfo("INF_NAN", "SocInfo", "support_bf16")}, + {ACL_CANN_ATTR_BF16, CannInfo("BF16", "SocInfo", "support_inf_nan")}, + {ACL_CANN_ATTR_JIT_COMPILE, CannInfo("JIT_COMPILE", "", "")}, }; aclError CannInfoUtils::GetAttributeList(const aclCannAttr **cannAttr, size_t *num) @@ -95,14 +94,8 @@ namespace acl { } // parse current CannInfo - const char *socName = aclrtGetSocNameImpl(); - std::string socVersion; - if (socName != nullptr) { - socVersion = std::string(socName); - } - currCannInfo_.socVersions.emplace_back(socVersion); const std::string runtimeVersionPath = defaultInstallPath_ + RUNTIME_VERSION_PATH; - ret = ParseVersionInfo(runtimeVersionPath, &currCannInfo_.runtimeVersion); + ret = ParseVersionInfo(runtimeVersionPath, ¤tRuntimeVersion_); if (ret != ACL_SUCCESS) { ACL_LOG_WARN("cannot get runtime version in current environment!"); return ACL_ERROR_INTERNAL_ERROR; @@ -111,8 +104,8 @@ namespace acl { // check and update attr availability CheckAndUpdateAttrAvailability(); initFlag_ = true; - ACL_LOG_INFO( - "Successfully initialized CannInfoUtils: current CannInfo[runtime = %d]", currCannInfo_.runtimeVersion); + ACL_LOG_INFO("Successfully initialized CannInfoUtils: current CannInfo[runtime = %d, attrNum=%zu]", + currentRuntimeVersion_, attrNum_); return ACL_SUCCESS; } @@ -177,33 +170,33 @@ namespace acl { bool CannInfoUtils::MatchVersionInfo(const CannInfo &configCannInfo) { // if version is not set, skip matching and return true - if (configCannInfo.runtimeVersion == UNKNOWN_VERSION) { + if (configCannInfo.minimumRuntimeVersion == UNKNOWN_VERSION) { return true; } - return (currCannInfo_.runtimeVersion >= configCannInfo.runtimeVersion); + return (currentRuntimeVersion_ >= configCannInfo.minimumRuntimeVersion); } - bool CannInfoUtils::MatchSocVersion(const std::vector &swConfigSocVersions) + bool CannInfoUtils::CheckNPUFeatures(const CannInfo &configInfo) { - if (swConfigSocVersions.empty()) { + if (configInfo.socSpecLabel.empty() || configInfo.socSpecKey.empty()) { + // label 或 key 为空说明特性与芯片无关, 无需查询 return true; } - const auto &target = currCannInfo_.socVersions.front(); - for (const auto &pattern : swConfigSocVersions) { - if (target.find(pattern) == 0UL) { - return true; - } - } - return false; + constexpr uint32_t kMaxValueLen = 16UL; + char_t value[kMaxValueLen] = {0}; + ACL_REQUIRES_CALL_RTS_OK( + rtGetSocSpec(configInfo.socSpecLabel.c_str(), configInfo.socSpecKey.c_str(), value, kMaxValueLen), rtGetSocSpec); + // value "0" 或空 或非法内容 都认为 false + const std::string strVal(value); + return strVal == "1"; } void CannInfoUtils::CheckAndUpdateAttrAvailability() { for (auto &item : attrToCannInfo_) { auto &swConfigInfo = item.second; - const auto &swConfigSocVersions = swConfigInfo.socVersions; - if (MatchVersionInfo(swConfigInfo) && MatchSocVersion(swConfigSocVersions)) { - ACL_LOG_DEBUG("support attribute aclCannAttr(%d)", static_cast(item.first)); + if (MatchVersionInfo(swConfigInfo) && CheckNPUFeatures(swConfigInfo)) { + ACL_LOG_INFO("support cann attribute [%s]", swConfigInfo.readableAttrName.c_str()); swConfigInfo.isAvailable = 1; attrArray_[attrNum_] = item.first; ++attrNum_; diff --git a/utils/cann_info_utils.h b/utils/cann_info_utils.h index a3107301ae7ffb1fab967560f72bc32bea2e983b..9a1d91ce50af5350926f11ec60d656a5a88c21b0 100644 --- a/utils/cann_info_utils.h +++ b/utils/cann_info_utils.h @@ -25,17 +25,17 @@ namespace acl { // if version info is not set in swFeatureList.json, use UNKNOWN_VERSION as default value constexpr const int32_t UNKNOWN_VERSION = -1; constexpr const char_t *const SW_CONFIG_RUNTIME = "runtimeVersion"; - constexpr const char_t *const SW_CONFIG_SOC_VERSION = "socVersionList"; struct CannInfo { - std::string configKeyName; - int32_t runtimeVersion; - std::vector socVersions; + std::string readableAttrName; + std::string socSpecLabel; + std::string socSpecKey; + int32_t minimumRuntimeVersion = UNKNOWN_VERSION; int32_t isAvailable = 0; - explicit CannInfo(const std::string &name, const int32_t runtime = UNKNOWN_VERSION) noexcept - : configKeyName(name), runtimeVersion(runtime) - { - } + explicit CannInfo(const std::string &attrName, const std::string &specLabel, + const std::string &specKey) noexcept + : readableAttrName(attrName), socSpecLabel(specLabel), socSpecKey(specKey) + {} }; class CannInfoUtils { @@ -49,12 +49,12 @@ namespace acl { static aclError GetConfigInstallPath(); static aclError ParseVersionInfo(const std::string &path, int32_t *version); static bool MatchVersionInfo(const CannInfo &configCannInfo); - static bool MatchSocVersion(const std::vector &swConfigSocVersions); + static bool CheckNPUFeatures(const CannInfo &configInfo); static void CheckAndUpdateAttrAvailability(); static std::mutex mutex_; static bool initFlag_; - static CannInfo currCannInfo_; + static int32_t currentRuntimeVersion_; static std::string swConfigPath_; static std::string defaultInstallPath_; static aclCannAttr attrArray_[MAX_CANN_ATTR_SIZE];