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 "hwts_kernel_model_process.h"
12 :
13 : #include "aicpusd_interface_process.h"
14 : #include "hwts_kernel_common.h"
15 :
16 : namespace AicpuSchedule {
17 : namespace {
18 : const std::string CFG_EXT_INFO = "AicpuCfgExtInfo";
19 : const std::string MODEL_CONFIG = "AicpuModelConfig";
20 : const std::string SHAPE_CONFIG = "AicpuModelShapeConfig";
21 : const std::string ESCHED_PRIORITY = "AicpuModelEschedPriority";
22 : const std::string CHECK_SUPPORTED = "CheckKernelSupported";
23 : const std::string PROCESS_DATA_EXCEPTION = "ProcessDataException";
24 : } // namespace
25 :
26 4 : int32_t ConfigExtInfoTsKernel::Compute(const aicpu::HwtsTsKernel &tsKernelInfo)
27 : {
28 4 : aicpusd_info("Begin to process ts kernel CfgExtInfo event");
29 : const auto cfgMsg =
30 4 : PtrToPtr<void, aicpu::AicpuExtendInfo>(ValueToPtr(tsKernelInfo.kernelBase.cceKernel.paramBase));
31 4 : if (cfgMsg == nullptr) {
32 1 : aicpusd_err("param base for config extension message is null.");
33 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
34 : }
35 :
36 3 : return AicpuModelManager::GetInstance().ProcessExtInfoCfgMsg(*cfgMsg);
37 : }
38 :
39 2 : int32_t ModelConfigTsKernel::Compute(const aicpu::HwtsTsKernel &tsKernelInfo)
40 : {
41 2 : aicpusd_info("Begin to process ts kernel ModelConfig event");
42 2 : const aicpu::HwtsCceKernel &kernel = tsKernelInfo.kernelBase.cceKernel;
43 2 : constexpr uint64_t len = sizeof(aicpu::AicpuParamHead) + sizeof(AicpuModelConfig);
44 2 : constexpr uint64_t offset = sizeof(aicpu::AicpuParamHead);
45 2 : const auto baseAddr = PtrToPtr<void, char_t>(ValueToPtr(kernel.paramBase));
46 2 : const aicpu::AicpuParamHead * const paramHead = PtrToPtr<char_t, aicpu::AicpuParamHead>(baseAddr);
47 2 : if (paramHead == nullptr) {
48 1 : aicpusd_err("ParamHead for ModelConfig is nullptr");
49 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
50 : }
51 :
52 1 : if (paramHead->length != len) {
53 0 : aicpusd_err("ModelConfig param length[%u] should be [%lu]", paramHead->length, len);
54 0 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
55 : }
56 :
57 1 : const auto cfg = PtrToPtr<const char_t, const AicpuModelConfig>(PtrAdd<const char_t>(baseAddr, len, offset));
58 1 : return AicpuModelManager::GetInstance().ProcessModelConfigMsg(*cfg);
59 : }
60 :
61 10 : int32_t ShapeConfigTsKernel::Compute(const aicpu::HwtsTsKernel &tsKernelInfo)
62 : {
63 10 : aicpusd_info("Begin to process ts kernel ModelShapeConfig event");
64 10 : const aicpu::HwtsCceKernel &kernel = tsKernelInfo.kernelBase.cceKernel;
65 10 : constexpr uint64_t len = sizeof(aicpu::AicpuParamHead) + sizeof(AicpuModelShapeConfig);
66 10 : const auto baseAddr = PtrToPtr<void, char_t>(ValueToPtr(kernel.paramBase));
67 10 : const aicpu::AicpuParamHead * const paramHead = PtrToPtr<char_t, aicpu::AicpuParamHead>(baseAddr);
68 10 : if (paramHead == nullptr) {
69 0 : aicpusd_err("ParamHead for ModelShapeConfig is nullptr");
70 0 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
71 : }
72 :
73 10 : if (paramHead->length != len) {
74 1 : aicpusd_err("ModelShapeConfig param length[%u] should be [%lu]", paramHead->length, len);
75 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
76 : }
77 :
78 9 : constexpr uint64_t offset = sizeof(aicpu::AicpuParamHead);
79 : const auto cfg =
80 9 : PtrToPtr<const char_t, const AicpuModelShapeConfig>(PtrAdd<const char_t>(baseAddr, len, offset));
81 9 : return AicpuModelManager::GetInstance().ProcessModelShapeConfigMsg(*cfg);
82 : }
83 :
84 3 : int32_t EschedPriorityTsKernel::Compute(const aicpu::HwtsTsKernel &tsKernelInfo)
85 : {
86 3 : aicpusd_info("Begin to process ts kernel AicpuModelEschedPriority event.");
87 3 : const aicpu::HwtsCceKernel &kernel = tsKernelInfo.kernelBase.cceKernel;
88 3 : constexpr uint64_t len = sizeof(aicpu::AicpuParamHead) + sizeof(AicpuPriInfo);
89 3 : constexpr uint64_t offset = sizeof(aicpu::AicpuParamHead);
90 3 : const auto baseAddr = PtrToPtr<void, char_t>(ValueToPtr(kernel.paramBase));
91 3 : const aicpu::AicpuParamHead * const paramHead = PtrToPtr<char_t, aicpu::AicpuParamHead>(baseAddr);
92 3 : if (paramHead == nullptr) {
93 1 : aicpusd_err("ParamHead for AicpuModelEschedPriority is nullptr");
94 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
95 : }
96 :
97 2 : if (paramHead->length != len) {
98 1 : aicpusd_err("AicpuModelEschedPriority param length[%u] should be [%lu]", paramHead->length, len);
99 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
100 : }
101 1 : const auto cfg = PtrToPtr<const char_t, const AicpuPriInfo>(PtrAdd<const char_t>(baseAddr, len, offset));
102 1 : return AicpuModelManager::GetInstance().ProcessModelPriorityMsg(*cfg, true);
103 : }
104 :
105 3 : int32_t CheckSupportedTsKernel::Compute(const aicpu::HwtsTsKernel &tsKernelInfo)
106 : {
107 3 : aicpusd_run_info("Begin to process kernel CheckKernelSupported event.");
108 3 : const aicpu::HwtsCceKernel &kernel = tsKernelInfo.kernelBase.cceKernel;
109 3 : const auto supportedCfg = PtrToPtr<void, CheckKernelSupportedConfig>(ValueToPtr(kernel.paramBase));
110 3 : const uint32_t kernelNameLen = supportedCfg->kernelNameLen;
111 3 : const char *const kernelName = PtrToPtr<void, const char>(ValueToPtr(supportedCfg->kernelNameAddr));
112 3 : uint32_t *resultAddr = PtrToPtr<void, uint32_t>(ValueToPtr(supportedCfg->checkResultAddr));
113 3 : if ((kernelName == nullptr) || (kernelNameLen == 0) || (resultAddr == nullptr)) {
114 1 : aicpusd_err("CheckKernelSupportedConfig params error");
115 1 : return AICPU_SCHEDULE_FAIL;
116 : }
117 :
118 2 : std::string operaterKernelName(kernelName, static_cast<uint64_t>(kernelNameLen));
119 2 : aicpusd_info("Task kernel name[%s].", operaterKernelName.c_str());
120 2 : const int32_t retCode = AicpuScheduleInterface::GetInstance().CheckKernelSupported(operaterKernelName);
121 2 : *resultAddr = static_cast<uint32_t>(retCode);
122 2 : aicpusd_run_info("Finish to process kernel CheckKernelSupported event retCode[%d].", retCode);
123 2 : return AICPU_SCHEDULE_OK;
124 2 : }
125 :
126 1 : int32_t ProcessDataExceptionTsKernel::Compute(const aicpu::HwtsTsKernel &tsKernelInfo)
127 : {
128 1 : const aicpu::HwtsCceKernel &kernel = tsKernelInfo.kernelBase.cceKernel;
129 1 : const auto exceptionInfo = PtrToPtr<void, DataFlowExceptionNotify>(ValueToPtr(kernel.paramBase));
130 1 : return AicpuScheduleInterface::GetInstance().ProcessException(exceptionInfo);
131 : }
132 :
133 : REGISTER_HWTS_KERNEL(CFG_EXT_INFO, ConfigExtInfoTsKernel);
134 : REGISTER_HWTS_KERNEL(MODEL_CONFIG, ModelConfigTsKernel);
135 : REGISTER_HWTS_KERNEL(SHAPE_CONFIG, ShapeConfigTsKernel);
136 : REGISTER_HWTS_KERNEL(ESCHED_PRIORITY, EschedPriorityTsKernel);
137 : REGISTER_HWTS_KERNEL(CHECK_SUPPORTED, CheckSupportedTsKernel);
138 : REGISTER_HWTS_KERNEL(PROCESS_DATA_EXCEPTION, ProcessDataExceptionTsKernel);
139 : } // namespace AicpuSchedule
|