Line data Source code
1 : /**
2 : * Copyright (c) 2025 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 "operator_kernel_register.h"
12 :
13 : #include "aicpusd_status.h"
14 : #include "aicpusd_model_err_process.h"
15 :
16 :
17 : namespace AicpuSchedule {
18 134 : OperatorKernelRegister &OperatorKernelRegister::Instance()
19 : {
20 134 : static OperatorKernelRegister instance;
21 134 : return instance;
22 : }
23 :
24 28 : int32_t OperatorKernelRegister::RunOperatorKernel(const AicpuTaskInfo &kernelTaskInfo, const RunContext &taskContext)
25 : {
26 28 : const auto kernelName = PtrToPtr<const void, const char_t>(ValueToPtr(kernelTaskInfo.kernelName));
27 28 : if (kernelName == nullptr) {
28 1 : aicpusd_err("The name of kernel is nullptr.");
29 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
30 : }
31 :
32 27 : std::shared_ptr<OperatorKernel> kernel = GetOperatorKernel(kernelName);
33 27 : if (kernel == nullptr) {
34 17 : aicpusd_err("Cannot find aicpu operator kernel, kernelName=%s", kernelName);
35 17 : return AICPU_SCHEDULE_ERROR_NOT_FOUND_LOGICAL_TASK;
36 : }
37 :
38 10 : aicpusd_info("Begin to process kernelName[%s] modelId[%u] streamId[%u].", kernelName, taskContext.modelId,
39 : taskContext.streamId);
40 10 : const int32_t ret = kernel->Compute(kernelTaskInfo, taskContext);
41 10 : if (ret != AICPU_SCHEDULE_OK) {
42 1 : aicpusd_err("Operator kernel compute error, ret=%d, kernelName=%s", ret, kernelName);
43 : }
44 10 : aicpusd_info("End to process kernelName[%s] modelId[%u] streamId[%u] result[%d].",
45 : kernelName, taskContext.modelId, taskContext.streamId, ret);
46 :
47 10 : return ret;
48 27 : }
49 :
50 6 : int32_t OperatorKernelRegister::CheckOperatorKernelSupported(const std::string &kernelName)
51 : {
52 6 : std::shared_ptr<OperatorKernel> kernel = GetOperatorKernel(kernelName);
53 6 : if (kernel == nullptr) {
54 3 : aicpusd_warn("Finish to process, kernel[%s] is not supported.", kernelName.c_str());
55 3 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
56 : }
57 :
58 3 : aicpusd_info("Finish to process, kernel[%s] supported", kernelName.c_str());
59 3 : return AICPU_SCHEDULE_OK;
60 6 : }
61 :
62 33 : std::shared_ptr<OperatorKernel> OperatorKernelRegister::GetOperatorKernel(const std::string &opType)
63 : {
64 33 : std::map<std::string, std::shared_ptr<OperatorKernel>>::const_iterator iter = kernelInstMap_.find(opType);
65 33 : if (iter != kernelInstMap_.end()) {
66 13 : return iter->second;
67 : }
68 20 : aicpusd_warn("The kernel[%s] is not registered", opType.c_str());
69 20 : return std::shared_ptr<OperatorKernel>(nullptr);
70 : }
71 :
72 105 : OperatorKernelRegister::Registerar::Registerar(const std::string &type, const KernelCreatorFunc &func)
73 : {
74 105 : OperatorKernelRegister::Instance().Register(type, func);
75 105 : }
76 :
77 108 : void OperatorKernelRegister::Register(const std::string &type, const KernelCreatorFunc &func)
78 : {
79 108 : std::unique_lock<std::mutex> lock(kernelInstMapMutex_);
80 108 : std::map<std::string, std::shared_ptr<OperatorKernel>>::const_iterator iter = kernelInstMap_.find(type);
81 108 : if (iter != kernelInstMap_.end()) {
82 1 : aicpusd_run_warn("Register[%s] creator already exist.", type.c_str());
83 1 : return;
84 : }
85 :
86 107 : kernelInstMap_[type] = func();
87 107 : aicpusd_debug("Kernel[%s] register successfully.", type.c_str());
88 107 : return;
89 108 : }
90 :
91 : } // namespace AicpuSchedule
|