| @@ -0,0 +1,330 @@ |
| | 1 | +/** |
| | 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. |
| | 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of |
| | 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). |
| | 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. |
| | 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, |
| | 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. |
| | 8 | + * See LICENSE in the root of the software repository for the full text of the License. |
| | 9 | + */ |
| | 10 | + |
| | 11 | +#include "opdev/direct_invoke_task.h" |
| | 12 | + |
| | 13 | +#include <limits> |
| | 14 | +#include <new> |
| | 15 | + |
| | 16 | +#include "bridge_graph.h" |
| | 17 | +#include "kernel_launcher.h" |
| | 18 | +#include "op_dfx_internal.h" |
| | 19 | +#include "opdev/op_arg_def.h" |
| | 20 | +#include "opdev/op_def.h" |
| | 21 | +#include "opdev/op_dfx.h" |
| | 22 | +#include "opdev/op_executor.h" |
| | 23 | +#include "opdev/op_log.h" |
| | 24 | + |
| | 25 | +namespace op { |
| | 26 | + |
| | 27 | +namespace { |
| | 28 | + |
| | 29 | +// GetOpArg never returns null for the arg kinds used here; a malformed list |
| | 30 | +// is one that claims entries without backing storage. |
| | 31 | +bool IsWellFormedArgList(const OpArgList* list) |
| | 32 | +{ |
| | 33 | + return list != nullptr && (list->count == 0U || list->args != nullptr); |
| | 34 | +} |
| | 35 | + |
| | 36 | +// launcher must own args. This function consumes launcher and args on every |
| | 37 | +// return path: failure deletes launcher, success transfers it to executor. |
| | 38 | +aclnnStatus RegisterKernelLauncher(uint32_t opType, aclOpExecutor* executor, OpArgContext* args, |
| | 39 | + KernelLauncher* launcher, bool includeOutShape = false) |
| | 40 | +{ |
| | 41 | + if (launcher == nullptr) { |
| | 42 | + DestroyOpArgContext(args); |
| | 43 | + OP_LOGE(ACLNN_ERR_PARAM_NULLPTR, "Kernel launcher must not be null."); |
| | 44 | + return ACLNN_ERR_PARAM_NULLPTR; |
| | 45 | + } |
| | 46 | + if (executor == nullptr || args == nullptr) { |
| | 47 | + delete launcher; |
| | 48 | + OP_LOGE(ACLNN_ERR_PARAM_NULLPTR, "Executor and launcher arguments must not be null."); |
| | 49 | + return ACLNN_ERR_PARAM_NULLPTR; |
| | 50 | + } |
| | 51 | + |
| | 52 | + OpArgList* inputs = args->GetOpArg(OP_INPUT_ARG); |
| | 53 | + OpArgList* outputs = args->GetOpArg(OP_OUTPUT_ARG); |
| | 54 | + OpArgList* workspaces = args->GetOpArg(OP_WORKSPACE_ARG); |
| | 55 | + OpArgList* outShapes = args->GetOpArg(OP_OUTSHAPE_ARG); |
| | 56 | + if (!IsWellFormedArgList(inputs) || !IsWellFormedArgList(outputs) || !IsWellFormedArgList(workspaces) || |
| | 57 | + (includeOutShape && !IsWellFormedArgList(outShapes))) { |
| | 58 | + delete launcher; |
| | 59 | + OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Launcher graph arguments are malformed."); |
| | 60 | + return ACLNN_ERR_PARAM_INVALID; |
| | 61 | + } |
| | 62 | + |
| | 63 | + aclnnStatus status = includeOutShape ? |
| | 64 | + internal::BuildGraph(executor->GetGraph(), opType, *inputs, *outputs, *workspaces, |
| | 65 | + *outShapes) : |
| | 66 | + internal::BuildGraph(executor->GetGraph(), opType, *inputs, *outputs, *workspaces); |
| | 67 | + if (status != ACLNN_SUCCESS) { |
| | 68 | + delete launcher; |
| | 69 | + return status; |
| | 70 | + } |
| | 71 | + executor->AddToKernelLauncherList(launcher); |
| | 72 | + return ACLNN_SUCCESS; |
| | 73 | +} |
| | 74 | + |
| | 75 | +// Matches the framework graph builder: a null input tensor means an absent |
| | 76 | +// optional input and is tolerated; outputs must be present. |
| | 77 | +enum class TensorRequirement : uint32_t { |
| | 78 | + OptionalInput = 0U, |
| | 79 | + Required = 1U, |
| | 80 | +}; |
| | 81 | + |
| | 82 | +bool IsValidTensor(const aclTensor* tensor, TensorRequirement requirement) |
| | 83 | +{ |
| | 84 | + if (tensor == nullptr) { |
| | 85 | + return requirement == TensorRequirement::OptionalInput; |
| | 86 | + } |
| | 87 | + return true; |
| | 88 | +} |
| | 89 | + |
| | 90 | +bool IsValidTensorArg(const OpArg& arg, TensorRequirement requirement) |
| | 91 | +{ |
| | 92 | + if (arg.type == OpArgType::OPARG_ACLTENSOR) { |
| | 93 | + return IsValidTensor(static_cast<const aclTensor*>(arg.value.data.pointer), requirement); |
| | 94 | + } |
| | 95 | + if (arg.type != OpArgType::OPARG_ACLTENSOR_LIST) { |
| | 96 | + return false; |
| | 97 | + } |
| | 98 | + const auto* tensors = static_cast<const aclTensorList*>(arg.value.data.pointer); |
| | 99 | + if (tensors == nullptr) { |
| | 100 | + return false; |
| | 101 | + } |
| | 102 | + for (uint64_t index = 0U; index < tensors->Size(); ++index) { |
| | 103 | + if (!IsValidTensor((*tensors)[index], requirement)) { |
| | 104 | + return false; |
| | 105 | + } |
| | 106 | + } |
| | 107 | + return true; |
| | 108 | +} |
| | 109 | + |
| | 110 | +bool IsValidTensorList(const OpArgList& args, TensorRequirement requirement) |
| | 111 | +{ |
| | 112 | + if (args.count != 0U && args.args == nullptr) { |
| | 113 | + return false; |
| | 114 | + } |
| | 115 | + for (size_t index = 0U; index < args.count; ++index) { |
| | 116 | + if (!IsValidTensorArg(args.args[index], requirement)) { |
| | 117 | + return false; |
| | 118 | + } |
| | 119 | + } |
| | 120 | + return true; |
| | 121 | +} |
| | 122 | + |
| | 123 | +bool IsValidArgs(const OpArgContext& args) |
| | 124 | +{ |
| | 125 | + const OpArgList* inputs = args.GetOpArg(OP_INPUT_ARG); |
| | 126 | + const OpArgList* outputs = args.GetOpArg(OP_OUTPUT_ARG); |
| | 127 | + const OpArgList* attributes = args.GetOpArg(OP_ATTR_ARG); |
| | 128 | + const OpArgList* workspaces = args.GetOpArg(OP_WORKSPACE_ARG); |
| | 129 | + // Workspace buffers are requested through the prepare callback and |
| | 130 | + // allocated by opbase; the entry workspace list must be absent or empty. |
| | 131 | + return inputs != nullptr && outputs != nullptr && attributes != nullptr && |
| | 132 | + (attributes->count == 0U || attributes->args != nullptr) && |
| | 133 | + IsValidTensorList(*inputs, TensorRequirement::OptionalInput) && |
| | 134 | + IsValidTensorList(*outputs, TensorRequirement::Required) && |
| | 135 | + (workspaces == nullptr || workspaces->count == 0U); |
| | 136 | +} |
| | 137 | + |
| | 138 | +// Mirrors the AI Core GetWorkspace flow: one DT_UINT8 tensor per requested |
| | 139 | +// size, allocated from the executor so the memory planner applies its |
| | 140 | +// alignment and lifetime reuse, then appended as the workspace argument. |
| | 141 | +aclnnStatus AppendRequestedWorkspaces(aclOpExecutor* executor, const DirectInvokeWorkspaceRequest& request, |
| | 142 | + OpArgContext* args) |
| | 143 | +{ |
| | 144 | + if (request.count == 0U) { |
| | 145 | + return ACLNN_SUCCESS; |
| | 146 | + } |
| | 147 | + if (request.sizes == nullptr) { |
| | 148 | + OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Workspace request count is non-zero but sizes is null."); |
| | 149 | + return ACLNN_ERR_PARAM_INVALID; |
| | 150 | + } |
| | 151 | + FVector<aclTensor*> workspaces; |
| | 152 | + for (uint32_t index = 0U; index < request.count; ++index) { |
| | 153 | + const uint64_t size = request.sizes[index]; |
| | 154 | + if (size == 0U || size > static_cast<uint64_t>(std::numeric_limits<int64_t>::max())) { |
| | 155 | + OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Workspace request contains an invalid size %llu at index %u.", |
| | 156 | + static_cast<unsigned long long>(size), index); |
| | 157 | + return ACLNN_ERR_PARAM_INVALID; |
| | 158 | + } |
| | 159 | + aclTensor* tensor = executor->AllocTensor(Shape({static_cast<int64_t>(size)}), DataType::DT_UINT8); |
| | 160 | + if (tensor == nullptr) { |
| | 161 | + OP_LOGE(ACLNN_ERR_INNER, "Failed to allocate requested workspace of %llu bytes.", |
| | 162 | + static_cast<unsigned long long>(size)); |
| | 163 | + return ACLNN_ERR_INNER; |
| | 164 | + } |
| | 165 | + workspaces.push_back(tensor); |
| | 166 | + } |
| | 167 | + aclTensorList* workspaceList = executor->AllocTensorList(workspaces.data(), workspaces.size()); |
| | 168 | + if (workspaceList == nullptr) { |
| | 169 | + OP_LOGE(ACLNN_ERR_INNER, "Failed to allocate requested workspace list."); |
| | 170 | + return ACLNN_ERR_INNER; |
| | 171 | + } |
| | 172 | + args->AppendOpWorkspaceArg(workspaceList); |
| | 173 | + return ACLNN_SUCCESS; |
| | 174 | +} |
| | 175 | + |
| | 176 | +class DirectInvokeKernelLauncher final : public KernelLauncher { |
| | 177 | +public: |
| | 178 | + DirectInvokeKernelLauncher(uint32_t opType, const aclOpExecutor* executor, DirectInvokeLaunchFn launch, |
| | 179 | + DirectInvokeDestroyFn destroy, void* launchState, OpArgContext* args) |
| | 180 | + : KernelLauncher(opType, DIRECT_INVOKE, executor, internal::ProfilingInfoId{}), |
| | 181 | + launch_(launch), |
| | 182 | + destroy_(destroy), |
| | 183 | + launchState_(launchState), |
| | 184 | + args_(args) |
| | 185 | + { |
| | 186 | + // Attribution comes from the enclosing DFX context captured by the |
| | 187 | + // base class (TLS names are macro stringification literals) and from |
| | 188 | + // opType, same as the AI Core path; no name override is accepted. |
| | 189 | + } |
| | 190 | + |
| | 191 | + ~DirectInvokeKernelLauncher() override |
| | 192 | + { |
| | 193 | + if (launchState_ != nullptr) { |
| | 194 | + destroy_(launchState_); |
| | 195 | + launchState_ = nullptr; |
| | 196 | + } |
| | 197 | + DestroyOpArgContext(args_); |
| | 198 | + args_ = nullptr; |
| | 199 | + } |
| | 200 | + |
| | 201 | + aclnnStatus Launch() override |
| | 202 | + { |
| | 203 | + if (launch_ == nullptr || args_ == nullptr) { |
| | 204 | + OP_LOGE(ACLNN_ERR_INNER, "DirectInvoke task launcher is not initialized."); |
| | 205 | + return ACLNN_ERR_INNER; |
| | 206 | + } |
| | 207 | + // Same TLS discipline as the AI Core path: phase 2 entry already |
| | 208 | + // restored the L2 context (InitL2Phase2Context), so the launcher only |
| | 209 | + // fills in the l0Name captured at registration time. |
| | 210 | + auto& threadLocalCtx = op::internal::GetThreadLocalContext(); |
| | 211 | + threadLocalCtx.logInfo_.l0Name = opLogInfo_.l0Name; |
| | 212 | + threadLocalCtx.profilingInfoId_ = profilingInfoId_; |
| | 213 | + |
| | 214 | + // DirectInvoke callbacks always submit compute tasks. |
| | 215 | + if (threadLocalCtx.opConfigInfo_.isOpDumpEnable_) { |
| | 216 | + op::internal::DumpL0(*args_->GetOpArg(op::OP_INPUT_ARG), opLogInfo_, OpInputType, executor_->GetStream()); |
| | 217 | + } |
| | 218 | + |
| | 219 | + const aclnnStatus ret = launch_(launchState_, args_, executor_->GetStream()); |
| | 220 | + |
| | 221 | + if (threadLocalCtx.opConfigInfo_.isOpDumpEnable_) { |
| | 222 | + op::internal::DumpL0(*args_->GetOpArg(op::OP_OUTPUT_ARG), opLogInfo_, OpOutputType, executor_->GetStream()); |
| | 223 | + } |
| | 224 | + if (ret == ACLNN_SUCCESS && op::internal::IsOverflowDumpEnable()) { |
| | 225 | + CheckOverflowDump(); |
| | 226 | + } |
| | 227 | + return ret; |
| | 228 | + } |
| | 229 | + |
| | 230 | + internal::OpKernelBin* GetBin() override { return nullptr; } |
| | 231 | + |
| | 232 | + // The task contract requires launchState to be address-independent, so |
| | 233 | + // repeatability only depends on the argument storages, same as the AI CPU |
| | 234 | + // path. |
| | 235 | + bool CheckRepeatable(const std::unordered_map<const aclStorage*, const aclStorage*>& relation, |
| | 236 | + const std::vector<const aclStorage*>& oriStorage) override |
| | 237 | + { |
| | 238 | + if (args_ == nullptr) { |
| | 239 | + return false; |
| | 240 | + } |
| | 241 | + LauncherRepeatableChecker checker(relation, oriStorage); |
| | 242 | + return checker.CheckLauncherRepeatable(args_); |
| | 243 | + } |
| | 244 | + |
| | 245 | +private: |
| | 246 | + void CheckOverflowDump() |
| | 247 | + { |
| | 248 | + aclmdlRICaptureStatus status; |
| | 249 | + aclmdlRI captureMdl; |
| | 250 | + if ((aclmdlRICaptureGetInfo(executor_->GetStream(), &status, &captureMdl) == ACL_SUCCESS) && |
| | 251 | + (status == ACL_MODEL_RI_CAPTURE_STATUS_ACTIVE)) { |
| | 252 | + OP_LOGI("No need to perform overflow check in the capture scenario."); |
| | 253 | + return; |
| | 254 | + } |
| | 255 | + (void)op::internal::OverflowDumpProcess(args_, const_cast<aclOpExecutor*>(executor_), executor_->GetStream(), |
| | 256 | + opLogInfo_); |
| | 257 | + } |
| | 258 | + |
| | 259 | + DirectInvokeLaunchFn launch_; |
| | 260 | + DirectInvokeDestroyFn destroy_; |
| | 261 | + void* launchState_; |
| | 262 | + OpArgContext* args_; |
| | 263 | +}; |
| | 264 | + |
| | 265 | +} // namespace |
| | 266 | + |
| | 267 | +aclnnStatus AddDirectInvokeTask([[maybe_unused]] const char* l0Name, uint32_t opType, DirectInvokePrepareFn prepare, |
| | 268 | + DirectInvokeLaunchFn launch, DirectInvokeDestroyFn destroy, aclOpExecutor* executor, |
| | 269 | + OpArgContext* args) |
| | 270 | +{ |
| | 271 | + if (prepare == nullptr || launch == nullptr || destroy == nullptr || executor == nullptr || args == nullptr) { |
| | 272 | + DestroyOpArgContext(args); |
| | 273 | + OP_LOGE(ACLNN_ERR_PARAM_NULLPTR, "DirectInvoke callbacks, executor and arguments must not be null."); |
| | 274 | + return ACLNN_ERR_PARAM_NULLPTR; |
| | 275 | + } |
| | 276 | + |
| | 277 | + if (opType == 0U || opType >= OpTypeDict::GetAllOpTypeSize() || !IsValidArgs(*args)) { |
| | 278 | + DestroyOpArgContext(args); |
| | 279 | + OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Invalid DirectInvoke task or arguments."); |
| | 280 | + return ACLNN_ERR_PARAM_INVALID; |
| | 281 | + } |
| | 282 | + |
| | 283 | + DirectInvokeWorkspaceRequest workspaceRequest{nullptr, 0U, 0U}; |
| | 284 | + void* launchState = nullptr; |
| | 285 | + aclnnStatus status = prepare(args, &workspaceRequest, &launchState); |
| | 286 | + if (status != ACLNN_SUCCESS) { |
| | 287 | + if (launchState != nullptr) { |
| | 288 | + destroy(launchState); |
| | 289 | + } |
| | 290 | + DestroyOpArgContext(args); |
| | 291 | + return status; |
| | 292 | + } |
| | 293 | + |
| | 294 | + status = AppendRequestedWorkspaces(executor, workspaceRequest, args); |
| | 295 | + if (status != ACLNN_SUCCESS) { |
| | 296 | + if (launchState != nullptr) { |
| | 297 | + destroy(launchState); |
| | 298 | + } |
| | 299 | + DestroyOpArgContext(args); |
| | 300 | + return status; |
| | 301 | + } |
| | 302 | + |
| | 303 | + // The constructor only stores pointers and cannot throw, so nothrow new |
| | 304 | + // fully covers launcher creation failures. |
| | 305 | + auto* launcher = new (std::nothrow) |
| | 306 | + DirectInvokeKernelLauncher(opType, executor, launch, destroy, launchState, args); |
| | 307 | + if (launcher == nullptr) { |
| | 308 | + if (launchState != nullptr) { |
| | 309 | + destroy(launchState); |
| | 310 | + } |
| | 311 | + DestroyOpArgContext(args); |
| | 312 | + return ACLNN_ERR_INNER; |
| | 313 | + } |
| | 314 | + |
| | 315 | + // Tasks with dynamic output shapes must not be cached, same as the AI |
| | 316 | + // Core path (CreatAiCoreKernelLauncher abandons the cache for them). |
| | 317 | + const bool includeOutShape = args->ContainsOpArgType(OP_OUTSHAPE_ARG); |
| | 318 | + status = RegisterKernelLauncher(opType, executor, args, launcher, includeOutShape); |
| | 319 | + if (status != ACLNN_SUCCESS) { |
| | 320 | + return status; |
| | 321 | + } |
| | 322 | + // Reuse the prepared state through explicit executor repeat, but execute |
| | 323 | + // the producer callback on every run. Direct runtime dispatches do not |
| | 324 | + // populate OpExecCache's task queue, so device-task replay must not bypass |
| | 325 | + // Launch(). Dynamic output shapes still prohibit repeat altogether. |
| | 326 | + executor->AbandonCache(includeOutShape); |
| | 327 | + return ACLNN_SUCCESS; |
| | 328 | +} |
| | 329 | + |
| | 330 | +} // namespace op |