diff --git a/base/CMakeLists.txt b/base/CMakeLists.txt index 513eeb898f61eb46941b747426a1502dab8bd814..2c70dc70904a486a539347dcbb6ed78183437967 100644 --- a/base/CMakeLists.txt +++ b/base/CMakeLists.txt @@ -20,6 +20,8 @@ file(GLOB_RECURSE OPP_REGISTRY_SRC_LIST registry/op_impl_space_registry_v2_impl.cc registry/opp_package_utils.cc registry/opp_so_manager.cc + registry/op_binary_resource_manager_impl_v2.cc + registry/op_binary_resource_manager_impl.cc ) ############ libopp_registry.so ############ diff --git a/base/registry/op_binary_resource_manager_impl.cc b/base/registry/op_binary_resource_manager_impl.cc new file mode 100644 index 0000000000000000000000000000000000000000..22f389c90f5d8c6c9820ae915d1ee7a0bfba7b2d --- /dev/null +++ b/base/registry/op_binary_resource_manager_impl.cc @@ -0,0 +1,61 @@ +/* Copyright (c) 2024 Huawei Technologies Co., Ltd. + * This file is a part of the CANN Open Software. + * Licensed under CANN Open Software License Agreement Version 1.0 (the "License"). + * Please refer to the License for details. You may not use this file except in compliance with the License. + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. + * See LICENSE in the root of the software repository for the full text of the License. + * ===================================================================================================================*/ + +#include "register/op_binary_resource_manager.h" +#include "op_binary_resource_manager_impl_v2.h" // metadef + +namespace nnopbase { +using Binary = BinaryImpl; +void OpBinaryResourceManager::AddOpFuncHandle(const ge::AscendString &opType, + const std::vector &opResourceHandle) +{ + OpBinaryResourceManagerV2::GetInstance().AddOpFuncHandle(opType, opResourceHandle); +} + +// 首个信息一定存在,是算子描述json,后续是成对的二进制信息 +ge::graphStatus OpBinaryResourceManager::AddBinary(const ge::AscendString &opType, + const std::vector> &opBinary) +{ + return OpBinaryResourceManagerV2::GetInstance().AddBinary(opType, opBinary); +} + +ge::graphStatus OpBinaryResourceManager::AddRuntimeKB(const ge::AscendString &opType, + const std::vector> &opRuntimeKb) +{ + return OpBinaryResourceManagerV2::GetInstance().AddRuntimeKB(opType, opRuntimeKb); +} + +const std::map &OpBinaryResourceManager::GetAllOpBinaryDesc() const +{ + return OpBinaryResourceManagerV2::GetInstance().GetAllOpBinaryDesc(); +} + +ge::graphStatus OpBinaryResourceManager::GetOpBinaryDesc(const ge::AscendString &opType, nlohmann::json &binDesc) const +{ + return OpBinaryResourceManagerV2::GetInstance().GetAllOpBinaryDesc(opType, binDesc); +} + +ge::graphStatus OpBinaryResourceManager::GetOpBinaryDescByPath(const ge::AscendString &jsonFilePath, + std::tuple &binInfo) const +{ + return OpBinaryResourceManagerV2::GetInstance().GetOpBinaryDescByPath(jsonFilePath, binInfo); +} + +ge::graphStatus OpBinaryResourceManager::GetOpBinaryDescByKey(const ge::AscendString &simplifiedKey, + std::tuple &binInfo) const +{ + return OpBinaryResourceManagerV2::GetInstance().GetOpBinaryDescByKey(simplifiedKey, binInfo); +} + +ge::graphStatus OpBinaryResourceManager::GetOpRuntimeKB(const ge::AscendString &opType, + std::vector &kbList) const +{ + return OpBinaryResourceManagerV2::GetInstance().GetOpBinaryDescByKey(opType, kbList); +} +} // nnopbase diff --git a/base/registry/op_binary_resource_manager_impl_v2.cc b/base/registry/op_binary_resource_manager_impl_v2.cc new file mode 100644 index 0000000000000000000000000000000000000000..d28af458bd10af86e8b46066566cd0878c73ba96 --- /dev/null +++ b/base/registry/op_binary_resource_manager_impl_v2.cc @@ -0,0 +1,204 @@ +/* Copyright (c) 2024 Huawei Technologies Co., Ltd. + * This file is a part of the CANN Open Software. + * Licensed under CANN Open Software License Agreement Version 1.0 (the "License"). + * Please refer to the License for details. You may not use this file except in compliance with the License. + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. + * See LICENSE in the root of the software repository for the full text of the License. + * ===================================================================================================================*/ + +#include "op_binary_resource_manager_impl_v2.h" +#include "common/ge_common/debug/ge_log.h" +#include "common/checker.h" + +namespace nnopbase { +namespace { +ge::graphStatus GetStr(const std::tuple &input, std::string &str) +{ + const uint8_t *start = std::get<0U>(input); + const uint8_t *end = std::get<1U>(input); + if ((end < start) || (start == nullptr) || (end == nullptr)) { + GELOGE(ge::GRAPH_PARAM_INVALID, "Parse json failed, end is %p, start is %p!", end, start); + return ge::GRAPH_PARAM_INVALID; + } + const size_t len = end - start; + str = std::string((const char *)start, len); + GELOGD("Parse str addr is %p, len is %zu, %s.", start, len, str.c_str()); + return ge::GRAPH_SUCCESS; +} + +ge::graphStatus ParseJson(const std::tuple &input, nlohmann::json &res) +{ + std::string jsonStr; + GE_ASSERT_GRAPH_SUCCESS(GetStr(input, jsonStr)); + try { + res = nlohmann::json::parse(jsonStr); + } catch (const nlohmann::json::exception &e) { + GELOGE(ge::GRAPH_PARAM_INVALID, "Parse json failed, resion %s, json info %s.", e.what(), jsonStr.c_str()); + return ge::GRAPH_PARAM_INVALID; + } + return ge::GRAPH_SUCCESS; +} + +ge::graphStatus ParseBinary(const std::tuple &input, BinaryImpl &binaryInfo) +{ + const uint8_t *start = std::get<0U>(input); + const uint8_t *end = std::get<1U>(input); + if ((end < start) || (start == nullptr) || (end == nullptr)) { + GELOGE(ge::GRAPH_PARAM_INVALID, "Parse json failed, end is %p, start is %p!", end, start); + return ge::GRAPH_PARAM_INVALID; + } + binaryInfo.len = end - start; + binaryInfo.content = start; + GELOGD("Parse binary info, addr is %p, len is %us.", binaryInfo.content, binaryInfo.len); + return ge::GRAPH_SUCCESS; +} +} // namepsace + +void OpBinaryResourceManagerV2::AddOpFuncHandle(const ge::AscendString &opType, + const std::vector &opResourceHandle) +{ + const std::lock_guard lk(mutex_); + const auto &it = resourceHandle_.find(opType); + if (it != resourceHandle_.end()) { + return; + } + GELOGI("Add op %s func handle, num is %zu!", opType.GetString(), opResourceHandle.size()); + for (auto func : opResourceHandle) { + (void)resourceHandle_[opType].emplace_back(func); + } +} + +// 首个信息一定存在,是算子描述json,后续是成对的二进制信息 +ge::graphStatus OpBinaryResourceManagerV2::AddBinary(const ge::AscendString &opType, + const std::vector> &opBinary) +{ + const std::lock_guard lk(mutex_); + const auto &it = opBinaryDesc_.find(opType); + if (it != opBinaryDesc_.end()) { + return ge::GRAPH_SUCCESS; + } + + // 首个信息是op的描述信息 + if (opBinary.size() >= 1U) { + nlohmann::json opDesc; + GE_ASSERT_GRAPH_SUCCESS(ParseJson(opBinary[0], opDesc), "Parse op %s json failed!", opType.GetString()); + opBinaryDesc_[opType] = opDesc; + } + + for (size_t i = 1U; i + 1U < opBinary.size(); i += 2U) { // 2 for json & binary + nlohmann::json binaryDesc; + BinaryImpl binaryInfo; + GE_ASSERT_GRAPH_SUCCESS(ParseJson(opBinary[i], binaryDesc), "Parse op %s binary json file [%zu] failed!", + opType.GetString(), i / 2U); // 2 for idx + GE_ASSERT_GRAPH_SUCCESS(ParseBinary(opBinary[i + 1U], binaryInfo), "Parse op %s binary file [%zu] failed!", + opType.GetString(), i / 2U); // 2 for idx + + std::string filePath; + try { + filePath = binaryDesc["filePath"].get(); + } catch (const nlohmann::json::exception &e) { + GELOGE(ge::GRAPH_PARAM_INVALID, "Parse op %s json filePath from binary json failed, reason %s.", + opType.GetString(), e.what()); + return ge::GRAPH_PARAM_INVALID; + } + pathToBinary_[filePath.c_str()] = std::tuple(binaryDesc, binaryInfo); + GELOGI("Add op %s binary, filePath %s, bin addr is %p, bin len %u.", opType.GetString(), filePath.c_str(), + binaryInfo.content, binaryInfo.len); + + std::vector keys; + try { + auto supportInfo = binaryDesc["supportInfo"]; + keys = supportInfo["simplifiedKey"].get>(); + } catch (const nlohmann::json::exception &e) { + GELOGW("Get op %s json simplifiedKey from binary json failed, reason %s.", opType.GetString(), e.what()); + } + for (auto key : keys) { + GELOGI("Add op %s binary, simplifiedKey %s, filePath %s, bin addr %p, bin len %u.", opType.GetString(), + key.c_str(), filePath.c_str(), binaryInfo.content, binaryInfo.len); + keyToPath_[key.c_str()] = filePath.c_str(); + } + } + return ge::GRAPH_SUCCESS; +} + +ge::graphStatus OpBinaryResourceManagerV2::AddRuntimeKB(const ge::AscendString &opType, + const std::vector> &opRuntimeKb) +{ + const std::lock_guard lk(mutex_); + const auto &it = runtimeKb_.find(opType); + if (it != runtimeKb_.end()) { + return ge::GRAPH_SUCCESS; + } + for (const auto &kbInfo : opRuntimeKb) { + std::string kbStr; + GE_ASSERT_GRAPH_SUCCESS(GetStr(kbInfo, kbStr), "Parse op %s runtime kb json file!", opType.GetString()); + (void)runtimeKb_[opType].emplace_back(kbStr.c_str()); + } + GELOGI("Add op %s runtime kb num %zu!", opType.GetString(), runtimeKb_[opType].size()); + return ge::GRAPH_SUCCESS; +} + +const std::map &OpBinaryResourceManagerV2::GetAllOpBinaryDesc() const +{ + const std::lock_guard lk(mutex_); + GELOGI("Get all op binary desc, num is %zu.", opBinaryDesc_.size()); + return opBinaryDesc_; +} + +ge::graphStatus OpBinaryResourceManagerV2::GetOpBinaryDesc(const ge::AscendString &opType, nlohmann::json &binDesc) const +{ + const std::lock_guard lk(mutex_); + const auto &it = opBinaryDesc_.find(opType); + if (it == opBinaryDesc_.end()) { + // 返回错误码表示该optype不存在,但不打印error日志,可以在执行时调用,根据返回判断是否存在静态二进制 + GELOGW("Get op %s json info failed!", opType.GetString()); + return ge::GRAPH_PARAM_INVALID; + } + binDesc = it->second; + GELOGI("Get op %s binary desc.", opType.GetString()); + return ge::GRAPH_SUCCESS; +} + +ge::graphStatus OpBinaryResourceManagerV2::GetOpBinaryDescByPath(const ge::AscendString &jsonFilePath, + std::tuple &binInfo) const +{ + const std::lock_guard lk(mutex_); + const auto &it = pathToBinary_.find(jsonFilePath); + if (it == pathToBinary_.end()) { + GELOGW("Get binaryInfo by json path failed, path is %s.", jsonFilePath.GetString()); + return ge::GRAPH_PARAM_INVALID; + } + binInfo = it->second; + GELOGI("Get binary info, json path is %s.", jsonFilePath.GetString()); + return ge::GRAPH_SUCCESS; +} + +ge::graphStatus OpBinaryResourceManagerV2::GetOpBinaryDescByKey(const ge::AscendString &simplifiedKey, + std::tuple &binInfo) const +{ + const std::lock_guard lk(mutex_); + const auto &it = keyToPath_.find(simplifiedKey); + const auto &simplified = simplifiedKey; + if (it == keyToPath_.end()) { + GELOGW("Get binaryInfo by simplified failed, simplified is %s.", simplified.GetString()); + return ge::GRAPH_PARAM_INVALID; + } + GELOGI("Get binary info, simplified is %s.", simplified.GetString()); + return GetOpBinaryDescByPath(it->second, binInfo); +} + +ge::graphStatus OpBinaryResourceManagerV2::GetOpRuntimeKB(const ge::AscendString &opType, + std::vector &kbList) const +{ + const std::lock_guard lk(mutex_); + const auto &it = runtimeKb_.find(opType); + if (it == runtimeKb_.end()) { + GELOGW("Get op %s RuntimeKB info failed.", opType.GetString()); + return ge::GRAPH_PARAM_INVALID; + } + kbList = it->second; + GELOGI("Get op %s RuntimeKB info, num is %zu.", opType.GetString(), kbList.size()); + return ge::GRAPH_SUCCESS; +} +} // nnopbase diff --git a/base/registry/op_binary_resource_manager_impl_v2.h b/base/registry/op_binary_resource_manager_impl_v2.h new file mode 100644 index 0000000000000000000000000000000000000000..cc34477a903c7a68d36edb41a814234b6a7993d7 --- /dev/null +++ b/base/registry/op_binary_resource_manager_impl_v2.h @@ -0,0 +1,89 @@ +/* Copyright (c) 2024 Huawei Technologies Co., Ltd. + * This file is a part of the CANN Open Software. + * Licensed under CANN Open Software License Agreement Version 1.0 (the "License"). + * Please refer to the License for details. You may not use this file except in compliance with the License. + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. + * See LICENSE in the root of the software repository for the full text of the License. + * ===================================================================================================================*/ + +#ifndef METADEF_CXX_REGISTER_OP_BINARY_RESOURCE_MANAGER_IMPL_V2_H_ +#define METADEF_CXX_REGISTER_OP_BINARY_RESOURCE_MANAGER_IMPL_V2_H_ +#include +#include +#include +#include +#include "json.hpp" +#include "graph/ascend_string.h" +#include "graph/ge_error_codes.h" + +namespace nnopbase { + +// 二进制内容,用于内存中的二进制.o文件描述 +struct BinaryImpl { + const uint8_t *content; // 二进制内容的起始指针 + uint32_t len; // 二进制的长度 +}; + +class OpBinaryResourceManagerV2 { +public: + static OpBinaryResourceManagerV2 &GetInstance() + { + static OpBinaryResourceManagerV2 manager; + return manager; + } + + ~OpBinaryResourceManagerV2() = default; + + void AddOpFuncHandle(const ge::AscendString &opType, const std::vector &opResourceHandle); + ge::graphStatus AddBinary(const ge::AscendString &opType, + const std::vector> &opBinary); + ge::graphStatus AddRuntimeKB(const ge::AscendString &opType, + const std::vector> &opRuntimeKb); + +// 获取资源 +public: + // 获取所有算子的描述信息 + const std::map &GetAllOpBinaryDesc() const; + + // 获取某个算子的描述信息 + ge::graphStatus GetOpBinaryDesc(const ge::AscendString &opType, nlohmann::json &binDesc) const; + + // 根据json文件路径(算子json中存在)查找.json/.o的信息 + ge::graphStatus GetOpBinaryDescByPath(const ge::AscendString &jsonFilePath, + std::tuple &binInfo) const; + + // 根据simplifiedKey(算子json中存在)查找.json/.o的信息 + ge::graphStatus GetOpBinaryDescByKey(const ge::AscendString &simplifiedKey, + std::tuple &binInfo) const; + + // 二进制知识库 + ge::graphStatus GetOpRuntimeKB(const ge::AscendString &opType, std::vector &kbList) const; + +private: + OpBinaryResourceManagerV2() = default; // 单例,禁止外部创建对象 + OpBinaryResourceManagerV2 &operator=(const OpBinaryResourceManagerV2 &) = delete; // 禁止拷贝 + OpBinaryResourceManagerV2 &operator=(OpBinaryResourceManagerV2 &&) = delete; + OpBinaryResourceManagerV2(const OpBinaryResourceManagerV2 &) = delete; + OpBinaryResourceManagerV2(OpBinaryResourceManagerV2 &&) = delete; + + mutable std::recursive_mutex mutex_; + + // 二进制描述信息 opType -> xxx.json + std::map opBinaryDesc_; + + // 二进制jsonPath simplifiedKey -> jsonPath, 可能存在多个simplifiedKey对应同一个jsonPath + std::map keyToPath_; + + // 二进制信息 jsonPath -> xxx.json, xxx.o + std::map> pathToBinary_; + + // 二进制知识库 opType -> xxx1.json, xxx2.json + std::map> runtimeKb_; + + // infershape/op tiling/runtime kb parser等全局变量指针,注册类资源,仅需持有 + std::map> resourceHandle_; +}; +} // nnopbase + +#endif // METADEF_CXX_REGISTER_OP_BINARY_RESOURCE_MANAGER_IMPL_V2_H_ diff --git a/inc/external/register/op_binary_resource_manager.h b/inc/external/register/op_binary_resource_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..5948bf577875194cbc1216ce6667f81d0545f1eb --- /dev/null +++ b/inc/external/register/op_binary_resource_manager.h @@ -0,0 +1,89 @@ +/* Copyright (c) 2024 Huawei Technologies Co., Ltd. + * This file is a part of the CANN Open Software. + * Licensed under CANN Open Software License Agreement Version 1.0 (the "License"). + * Please refer to the License for details. You may not use this file except in compliance with the License. + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. + * See LICENSE in the root of the software repository for the full text of the License. + * ===================================================================================================================*/ + +#include +#include +#include +#include +#include +#include "graph/ascend_string.h" +#include "graph/ge_error_codes.h" + +#ifndef INC_EXTERNAL_REGISTER_OP_BINARY_RESOURCE_MANAGER_H_ +#define INC_EXTERNAL_REGISTER_OP_BINARY_RESOURCE_MANAGER_H_ + +namespace nnopbase { +// 二进制内容,用于内存中的二进制.o文件描述 +struct Binary { + const uint8_t *content; // 二进制内容的起始指针 + uint32_t len; // 二进制的长度 +}; + +class OpBinaryResourceManager { +public: + static OpBinaryResourceManager &GetInstance() + { + static OpBinaryResourceManager manager; + return manager; + } + + ~OpBinaryResourceManager() = default; + + void AddOpFuncHandle(const ge::AscendString &opType, const std::vector &opResourceHandle); + ge::graphStatus AddBinary(const ge::AscendString &opType, + const std::vector> &opBinary); + ge::graphStatus AddRuntimeKB(const ge::AscendString &opType, + const std::vector> &opRuntimeKb); + +// 获取资源 +public: + // 获取所有算子的描述信息 + const std::map &GetAllOpBinaryDesc() const; + + // 获取某个算子的描述信息 + ge::graphStatus GetOpBinaryDesc(const ge::AscendString &opType, nlohmann::json &binDesc) const; + + // 根据json文件路径(算子json中存在)查找.json/.o的信息 + ge::graphStatus GetOpBinaryDescByPath(const ge::AscendString &jsonFilePath, + std::tuple &binInfo) const; + + // 根据simplifiedKey(算子json中存在)查找.json/.o的信息 + ge::graphStatus GetOpBinaryDescByKey(const ge::AscendString &simplifiedKey, + std::tuple &binInfo) const; + + // 二进制知识库 + ge::graphStatus GetOpRuntimeKB(const ge::AscendString &opType, std::vector &kbList) const; + +private: + OpBinaryResourceManager() = default; // 单例,禁止外部创建对象 + OpBinaryResourceManager &operator=(const OpBinaryResourceManager &) = delete; // 禁止拷贝 + OpBinaryResourceManager &operator=(OpBinaryResourceManager &&) = delete; + OpBinaryResourceManager(const OpBinaryResourceManager &) = delete; + OpBinaryResourceManager(OpBinaryResourceManager &&) = delete; + + mutable std::recursive_mutex mutex_; + + // 二进制描述信息 opType -> xxx.json + std::map opBinaryDesc_; + + // 二进制jsonPath simplifiedKey -> jsonPath, 可能存在多个simplifiedKey对应同一个jsonPath + std::map keyToPath_; + + // 二进制信息 jsonPath -> xxx.json, xxx.o + std::map> pathToBinary_; + + // 二进制知识库 opType -> xxx1.json, xxx2.json + std::map> runtimeKb_; + + // infershape/op tiling/runtime kb parser等全局变量指针,注册类资源,仅需持有 + std::map> resourceHandle_; +}; +} // nnopbase + +#endif // INC_EXTERNAL_REGISTER_OP_BINARY_RESOURCE_MANAGER_H_