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