From 9db3468e6decfd7ce8f4ef008b023ad5f51c627b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=82=85=E9=AA=8F?= Date: Wed, 12 Nov 2025 18:54:42 +0800 Subject: [PATCH] launch aclop2 --- single_op/compile/op_kernel_registry.cpp | 59 ++++++++++-------------- single_op/compile/op_kernel_registry.h | 7 +-- single_op/compile/op_kernel_selector.cpp | 6 +-- single_op/compile/op_kernel_selector.h | 3 +- single_op/executor/op_task.cpp | 14 +++--- single_op/executor/op_task.h | 5 +- single_op/executor/stream_executor.cpp | 2 +- 7 files changed, 44 insertions(+), 52 deletions(-) diff --git a/single_op/compile/op_kernel_registry.cpp b/single_op/compile/op_kernel_registry.cpp index 469d37b5..6d74fcbf 100644 --- a/single_op/compile/op_kernel_registry.cpp +++ b/single_op/compile/op_kernel_registry.cpp @@ -14,12 +14,13 @@ #include "utils/file_utils.h" #include "common/log_inner.h" #include "error_codes_inner.h" +#include "acl/acl_rt.h" namespace acl { namespace { const char_t *const STUB_NAME_PREFIX = "acl_dynamic_"; } // namespace -const void *OpKernelRegistry::GetStubFunc(const std::string &opType, const std::string &kernelId) +aclrtFuncHandle OpKernelRegistry::GetFuncHandle(const std::string &opType, const std::string &kernelId) { const std::lock_guard lk(mu_); std::map>>::const_iterator kernelsIter = @@ -37,19 +38,7 @@ const void *OpKernelRegistry::GetStubFunc(const std::string &opType, const std:: return nullptr; } - return it->second->stubName.c_str(); -} - -OpKernelRegistry::~OpKernelRegistry() -{ - const std::lock_guard lk(mu_); - for (auto &kernelsOfOp : kernels_) { - ACL_LOG_DEBUG("To unregister kernel of op: %s", kernelsOfOp.first.c_str()); - for (auto &it : kernelsOfOp.second) { - ACL_LOG_DEBUG("To unregister bin by handle: %p, kernelId = %s", it.second->binHandle, it.first.c_str()); - (void)rtDevBinaryUnRegister(it.second->binHandle); - } - } + return it->second->funcHandle; } aclError OpKernelRegistry::Register(std::unique_ptr &®istration) @@ -68,36 +57,36 @@ aclError OpKernelRegistry::Register(std::unique_ptr &®i registration->stubName = std::string(STUB_NAME_PREFIX); registration->stubName += registration->kernelId; - rtDevBinary_t binary; - binary.version = 0U; - binary.data = registration->binData; - binary.length = registration->binSize; + aclrtBinHandle binHandle = nullptr; + aclrtBinaryLoadOptionValue val{}; if (registration->enginetype == ACL_ENGINE_AICORE) { - binary.magic = RT_DEV_BINARY_MAGIC_ELF; + val.magic = ACL_RT_BINARY_MAGIC_ELF_AICORE; } else if (registration->enginetype == ACL_ENGINE_VECTOR) { - binary.magic = RT_DEV_BINARY_MAGIC_ELF_AIVEC; + val.magic = ACL_RT_BINARY_MAGIC_ELF_VECTOR_CORE; } else { return ACL_ERROR_INVALID_PARAM; } - void *binHandle = nullptr; - const auto ret = rtDevBinaryRegister(&binary, &binHandle); - if (ret != RT_ERROR_NONE) { - ACL_LOG_CALL_ERROR("[Register][Dev]rtDevBinaryRegister failed, runtime errorCode = %d", - static_cast(ret)); - return ACL_GET_ERRCODE_RTS(ret); + aclrtBinaryLoadOption opt{ACL_RT_BINARY_LOAD_OPT_MAGIC, val}; + aclrtBinaryLoadOptions options{&opt, 1U}; + auto ret = aclrtBinaryLoadFromData(registration->binData, registration->binSize, &options, &binHandle); + if (ret != ACL_ERROR_NONE) { + ACL_LOG_CALL_ERROR("[Register][Dev]aclrtBinaryLoadFromData failed. kernel name = %s, " + "runtime errorCode = %d", registration->kernelName.c_str(), + static_cast(ret)); + return ret; } - const auto rtRet = rtFunctionRegister(binHandle, registration->stubName.c_str(), registration->stubName.c_str(), - registration->kernelName.c_str(), static_cast(FUNC_MODE_NORMAL)); - if (rtRet != RT_ERROR_NONE) { - (void)rtDevBinaryUnRegister(binHandle); - ACL_LOG_CALL_ERROR("[Register][Dev]rtFunctionRegister failed. bin key = %s, kernel name = %s, " - "runtime errorCode = %d", registration->stubName.c_str(), registration->kernelName.c_str(), - static_cast(rtRet)); - return ACL_GET_ERRCODE_RTS(rtRet); + aclrtFuncHandle funcHandle = nullptr; + ret = aclrtBinaryGetFunction(binHandle, registration->kernelName.c_str(), &funcHandle); + if (ret != ACL_ERROR_NONE) { + aclrtBinaryUnLoad(binHandle); + ACL_LOG_CALL_ERROR("[Register][Dev]aclrtBinaryGetFunction failed. bin handle = %p, kernel name = %s, " + "runtime errorCode = %d", binHandle, registration->kernelName.c_str(), + static_cast(ret)); + return ret; } - registration->binHandle = binHandle; + registration->funcHandle = funcHandle; registration->deallocator = deallocator; (void)kernels_[registration->opType].emplace(registration->kernelId, std::move(registration)); return ACL_SUCCESS; diff --git a/single_op/compile/op_kernel_registry.h b/single_op/compile/op_kernel_registry.h index 891a9e5b..0f8af12d 100644 --- a/single_op/compile/op_kernel_registry.h +++ b/single_op/compile/op_kernel_registry.h @@ -18,6 +18,7 @@ #include #include "acl/acl_op.h" +#include "acl/acl_rt.h" namespace acl { struct OpKernelRegistration { @@ -37,12 +38,12 @@ struct OpKernelRegistration { void *binData = nullptr; uint64_t binSize = 0UL; void (*deallocator)(void *data, size_t length) = nullptr; - void *binHandle = nullptr; + aclrtFuncHandle funcHandle = nullptr; }; class OpKernelRegistry { public: - ~OpKernelRegistry(); + ~OpKernelRegistry() = default; static OpKernelRegistry &GetInstance() { @@ -50,7 +51,7 @@ public: return instance; } - const void *GetStubFunc(const std::string &opType, const std::string &kernelId); + aclrtFuncHandle GetFuncHandle(const std::string &opType, const std::string &kernelId); aclError Register(std::unique_ptr &®istration); diff --git a/single_op/compile/op_kernel_selector.cpp b/single_op/compile/op_kernel_selector.cpp index 6cc2674d..3e3e6de2 100644 --- a/single_op/compile/op_kernel_selector.cpp +++ b/single_op/compile/op_kernel_selector.cpp @@ -86,9 +86,9 @@ aclError OpKernelSelector::SelectOpKernel(const AclOp &op) } ACL_LOG_DEBUG("selecting kernel succeeded. kernelId = %s", desc->kernelId.c_str()); - desc->stubFunc = OpKernelRegistry::GetInstance().GetStubFunc(op.opType, desc->kernelId); - if (desc->stubFunc == nullptr) { - ACL_LOG_INNER_ERROR("Stub function not registered. kernelId = %s", desc->kernelId.c_str()); + desc->funcHandle = OpKernelRegistry::GetInstance().GetFuncHandle(op.opType, desc->kernelId); + if (desc->funcHandle == nullptr) { + ACL_LOG_INNER_ERROR("Kernel not registered. kernelId = %s", desc->kernelId.c_str()); return ACL_ERROR_KERNEL_NOT_FOUND; } desc->timestamp = attr_utils::GetCurrentTimestamp(); diff --git a/single_op/compile/op_kernel_selector.h b/single_op/compile/op_kernel_selector.h index ae5af322..f94eb82d 100644 --- a/single_op/compile/op_kernel_selector.h +++ b/single_op/compile/op_kernel_selector.h @@ -20,10 +20,11 @@ #include "types/acl_op.h" #include "utils/acl_op_map.h" #include "types/op_attr.h" +#include "acl/acl_rt.h" struct aclopKernelDesc { std::string kernelId; - const void *stubFunc = nullptr; // no need for deallocating + aclrtFuncHandle funcHandle = nullptr; uint32_t blockDim = 0U; std::vector workspaceSizes; std::string extendArgs; diff --git a/single_op/executor/op_task.cpp b/single_op/executor/op_task.cpp index 14ed333c..378c273e 100755 --- a/single_op/executor/op_task.cpp +++ b/single_op/executor/op_task.cpp @@ -10,11 +10,12 @@ #include "op_task.h" #include "runtime/rt.h" +#include "runtime/rts/rts_kernel.h" #include "error_codes_inner.h" namespace acl { -TbeOpTask::TbeOpTask(const void *const stubFunction, const uint32_t block) : OpTask(), - stubFunc_(stubFunction), blockDim_(block) +TbeOpTask::TbeOpTask(aclrtFuncHandle funcHandle, const uint32_t block) : OpTask(), + funcHandle_(funcHandle), blockDim_(block) { } @@ -37,11 +38,10 @@ aclError TbeOpTask::ExecuteAsync(const int32_t numInputs, } // launch kernel - ACL_LOG_DEBUG("To launch kernel, stubFunc = %s, block dim = %u, arg size = %u", - reinterpret_cast(stubFunc_), blockDim_, argSize_); - const auto ret = rtKernelLaunch(stubFunc_, blockDim_, const_cast(args_.get()), - argSize_, nullptr, stream); - return ACL_GET_ERRCODE_RTS(ret); + ACL_LOG_DEBUG("To launch kernel, funcHandle = %p, block dim = %u, arg size = %u", + reinterpret_cast(funcHandle_), blockDim_, argSize_); + return rtsLaunchKernelWithHostArgs(funcHandle_, blockDim_, stream, nullptr, const_cast(args_.get()), + argSize_, nullptr, 0U); } void OpTask::SetArgs(std::unique_ptr &&args, const uint32_t argSize) diff --git a/single_op/executor/op_task.h b/single_op/executor/op_task.h index f605b09e..363edb62 100755 --- a/single_op/executor/op_task.h +++ b/single_op/executor/op_task.h @@ -12,6 +12,7 @@ #define TRUNK_PROJ_OP_TASK_H #include "types/acl_op.h" +#include "acl/acl_rt.h" namespace acl { class OpTask { @@ -31,7 +32,7 @@ protected: class TbeOpTask : public OpTask { public: - TbeOpTask(const void *const stubFunction, const uint32_t block); + TbeOpTask(aclrtFuncHandle funcHandle, const uint32_t block); ~TbeOpTask() = default; aclError ExecuteAsync(const int32_t numInputs, @@ -41,7 +42,7 @@ public: const aclrtStream stream) override; private: - const void *stubFunc_; + aclrtFuncHandle funcHandle_; uint32_t blockDim_; }; } // namespace acl diff --git a/single_op/executor/stream_executor.cpp b/single_op/executor/stream_executor.cpp index f3fbb4b7..b9ce8eec 100755 --- a/single_op/executor/stream_executor.cpp +++ b/single_op/executor/stream_executor.cpp @@ -69,7 +69,7 @@ aclError StreamExecutor::ExecuteAsync(OpKernelDesc &kernelDesc, { ACL_LOG_DEBUG("Start to execute op by dynamic kernel"); kernelDesc.timestamp = attr_utils::GetCurrentTimestamp(); - TbeOpTask task(kernelDesc.stubFunc, kernelDesc.blockDim); + TbeOpTask task(kernelDesc.funcHandle, kernelDesc.blockDim); ACL_REQUIRES_OK(InitTbeTask(kernelDesc, numInputs, numOutputs, task)); ACL_REQUIRES_OK(task.ExecuteAsync(numInputs, inputs, numOutputs, outputs, stream_)); return ACL_SUCCESS; -- Gitee