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_control.h"
12 : #include "aicpu_sched/common/aicpu_task_struct.h"
13 : #include "aicpusd_monitor.h"
14 : #include "aicpusd_msg_send.h"
15 : #include "aicpusd_model_err_process.h"
16 : #include "hwts_kernel_common.h"
17 :
18 : namespace AicpuSchedule {
19 : namespace {
20 : const std::string KERNEL_END_GRAPH = "endGraph";
21 : const std::string RECORD_NOTIFY = "recordNotify";
22 : const std::string ACTIVE_ENTRY_STREAM = "activeEntryStream";
23 : const std::string MODEL_STOP = "AICPUModelStop";
24 : const std::string MODEL_CLEAR_RESTART = "AICPUModelClearInputAndRestart";
25 : } // namespace
26 :
27 3 : int32_t EndGraphTsKernel::Compute(const aicpu::HwtsTsKernel &tsKernelInfo)
28 : {
29 3 : aicpusd_info("Begin to process ts kernel end graph");
30 : const auto modelIdPtr =
31 3 : PtrToPtr<void, uint32_t>(ValueToPtr(tsKernelInfo.kernelBase.cceKernel.paramBase));
32 3 : if (modelIdPtr == nullptr) {
33 1 : aicpusd_err("ModelEndGraph taskInfo paramBase is null, kernelType[%u].",
34 : tsKernelInfo.kernelType);
35 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
36 : }
37 :
38 2 : const uint32_t modelId = *modelIdPtr;
39 2 : AicpuMonitor::GetInstance().SetModelEndTime(modelId);
40 :
41 2 : return HwTsKernelCommon::ProcessEndGraph(modelId);
42 : }
43 :
44 3 : int32_t RecordNotifyTsKernel::Compute(const aicpu::HwtsTsKernel &tsKernelInfo)
45 : {
46 : const auto info =
47 3 : PtrToPtr<void, TsAicpuNotify>(ValueToPtr(tsKernelInfo.kernelBase.cceKernel.paramBase));
48 3 : if (info == nullptr) {
49 1 : aicpusd_err("ModelRecord taskInfo paramBase is null, kernelType[%u].",
50 : tsKernelInfo.kernelType);
51 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
52 : }
53 :
54 2 : aicpusd_info("Begin to process ts notify[%u] event", info->notify_id);
55 2 : bool hasWait = false;
56 2 : uint32_t waitStreamId = INVALID_NUMBER;
57 : // if has wait, set hasWait true and set waitStreamId, or else save notify come.
58 2 : EventWaitManager::NotifyWaitManager().Event(static_cast<size_t>(info->notify_id), hasWait, waitStreamId);
59 2 : if (!hasWait) {
60 1 : aicpusd_info("End to process ts notify[%u] event, but no stream is waiting", info->notify_id);
61 1 : return AICPU_SCHEDULE_OK;
62 : }
63 :
64 1 : uint32_t modelId = 0U;
65 1 : auto ret = ModelStreamManager::GetInstance().GetStreamModelId(waitStreamId, modelId);
66 1 : if (ret != AICPU_SCHEDULE_OK) {
67 0 : aicpusd_err("ModelRecord[%u] need active stream[%u] but find modelId failed.",
68 : info->notify_id, waitStreamId);
69 0 : return ret;
70 : }
71 :
72 : // use event to avoid pending ts response
73 1 : AICPUSubEventInfo aicpuSubEventInfo = {};
74 1 : aicpuSubEventInfo.modelId = modelId;
75 1 : aicpuSubEventInfo.para.streamInfo.streamId = waitStreamId;
76 1 : ret = AicpuMsgSend::SendAICPUSubEvent(PtrToPtr<AICPUSubEventInfo, const char_t>(&aicpuSubEventInfo),
77 : static_cast<uint32_t>(sizeof(AICPUSubEventInfo)),
78 : AICPU_SUB_EVENT_RECOVERY_STREAM,
79 : CP_DEFAULT_GROUP_ID,
80 : false);
81 1 : if (ret != AICPU_SCHEDULE_OK) {
82 0 : aicpusd_err("Failed to process ts notify[%u] event, ret[%d].", info->notify_id, ret);
83 0 : return ret;
84 : }
85 :
86 1 : aicpusd_info("End to process ts notify[%u] event, ret[%d]", info->notify_id, ret);
87 1 : return AICPU_SCHEDULE_OK;
88 : }
89 :
90 2 : int32_t ActiveEntryStreamTsKernel::Compute(const aicpu::HwtsTsKernel &tsKernelInfo)
91 : {
92 : const auto streamIdPtr =
93 2 : PtrToPtr<void, uint32_t>(ValueToPtr(tsKernelInfo.kernelBase.cceKernel.paramBase));
94 2 : if (streamIdPtr == nullptr) {
95 1 : aicpusd_err("ModelActiveEntryStream taskInfo paramBase is null, kernelType[%u].",
96 : tsKernelInfo.kernelType);
97 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
98 : }
99 :
100 1 : aicpusd_info("Begin to process ts active stream[%u] event.", *streamIdPtr);
101 1 : uint32_t modelId = 0U;
102 1 : auto ret = ModelStreamManager::GetInstance().GetStreamModelId(*streamIdPtr, modelId);
103 1 : if (ret != AICPU_SCHEDULE_OK) {
104 0 : aicpusd_err("ActiveEntryStream need active stream[%u] but find modelId failed.", *streamIdPtr);
105 0 : return ret;
106 : }
107 :
108 1 : AICPUSubEventInfo aicpuSubEventInfo = {};
109 1 : aicpuSubEventInfo.modelId = modelId;
110 1 : aicpuSubEventInfo.para.streamInfo.streamId = *streamIdPtr;
111 1 : ret = AicpuMsgSend::SendAICPUSubEvent(PtrToPtr<AICPUSubEventInfo, const char_t>(&aicpuSubEventInfo),
112 : static_cast<uint32_t>(sizeof(AICPUSubEventInfo)),
113 : AICPU_SUB_EVENT_ACTIVE_STREAM,
114 : CP_DEFAULT_GROUP_ID,
115 : false);
116 1 : if (ret != AICPU_SCHEDULE_OK) {
117 0 : aicpusd_err("Failed to process ts active stream[%u] event, ret[%d].", *streamIdPtr, ret);
118 0 : return ret;
119 : }
120 1 : aicpusd_info("End to process ts active stream[%u] event, ret[%d].", *streamIdPtr, ret);
121 1 : return AICPU_SCHEDULE_OK;
122 : }
123 :
124 3 : int32_t ModelStopTsKernel::Compute(const aicpu::HwtsTsKernel &tsKernelInfo)
125 : {
126 3 : aicpusd_run_info("Begin to process ts kernel ModelStop event.");
127 3 : const aicpu::HwtsCceKernel &kernel = tsKernelInfo.kernelBase.cceKernel;
128 3 : const auto cfg = PtrToPtr<void, ReDeployConfig>(ValueToPtr(kernel.paramBase));
129 3 : const uint32_t modelIdNum = cfg->modelIdNum;
130 3 : const uint32_t *const modelIds = PtrToPtr<void, uint32_t>(ValueToPtr(cfg->modelIdsAddr));
131 3 : if ((modelIdNum != 0U) && (modelIds == nullptr)) {
132 1 : aicpusd_err("TsKernelModelStop modelIds is null");
133 1 : return AICPU_SCHEDULE_FAIL;
134 : }
135 :
136 3 : for (uint32_t i = 0U; i < modelIdNum; ++i) {
137 2 : const int32_t ret = AicpuScheduleInterface::GetInstance().Stop(modelIds[i]);
138 2 : if (ret != AICPU_SCHEDULE_OK) {
139 1 : aicpusd_err("Stop model[%u] failed, ret[%d].", modelIds[i], ret);
140 1 : return AICPU_SCHEDULE_FAIL;
141 : }
142 1 : aicpusd_info("Stop model[%u] success", modelIds[i]);
143 : }
144 :
145 1 : aicpusd_run_info("Finish to process ts kernel ModelStop event, [%u] model had been processed.", modelIdNum);
146 1 : return AICPU_SCHEDULE_OK;
147 : }
148 :
149 4 : int32_t ModelClearAndRestartTsKernel::Compute(const aicpu::HwtsTsKernel &tsKernelInfo)
150 : {
151 4 : aicpusd_run_info("Begin to process ts kernel ModelClearInputAndRestart event.");
152 4 : const aicpu::HwtsCceKernel &kernel = tsKernelInfo.kernelBase.cceKernel;
153 4 : const auto cfg = PtrToPtr<void, ReDeployConfig>(ValueToPtr(kernel.paramBase));
154 4 : const uint32_t modelIdNum = cfg->modelIdNum;
155 4 : const uint32_t *const modelIds = PtrToPtr<void, uint32_t>(ValueToPtr(cfg->modelIdsAddr));
156 4 : if ((modelIdNum != 0U) && (modelIds == nullptr)) {
157 1 : aicpusd_err("TsKernelModelClearInputAndRestart modelIds is null");
158 1 : return AICPU_SCHEDULE_FAIL;
159 : }
160 :
161 4 : for (uint32_t i = 0U; i < modelIdNum; ++i) {
162 3 : int32_t ret = AicpuScheduleInterface::GetInstance().ClearInput(modelIds[i]);
163 3 : if (ret != AICPU_SCHEDULE_OK) {
164 1 : aicpusd_err("ClearInput model[%u] failed, ret[%d].", modelIds[i], ret);
165 1 : return AICPU_SCHEDULE_FAIL;
166 : }
167 2 : aicpusd_info("ClearInput model[%u] success", modelIds[i]);
168 :
169 2 : ret = AicpuScheduleInterface::GetInstance().Restart(modelIds[i]);
170 2 : if (ret != AICPU_SCHEDULE_OK) {
171 1 : aicpusd_err("Restart model[%u] failed, ret[%d].", modelIds[i], ret);
172 1 : return AICPU_SCHEDULE_FAIL;
173 : }
174 1 : aicpusd_info("Restart model[%u] success", modelIds[i]);
175 : }
176 :
177 1 : aicpusd_run_info("Finish to process ts kernel ModelClearInputAndRestart event, [%u] model had been processed.",
178 : modelIdNum);
179 1 : return AICPU_SCHEDULE_OK;
180 : }
181 :
182 : REGISTER_HWTS_KERNEL(KERNEL_END_GRAPH, EndGraphTsKernel);
183 : REGISTER_HWTS_KERNEL(RECORD_NOTIFY, RecordNotifyTsKernel);
184 : REGISTER_HWTS_KERNEL(ACTIVE_ENTRY_STREAM, ActiveEntryStreamTsKernel);
185 : REGISTER_HWTS_KERNEL(MODEL_STOP, ModelStopTsKernel);
186 : REGISTER_HWTS_KERNEL(MODEL_CLEAR_RESTART, ModelClearAndRestartTsKernel);
187 : } // namespace AicpuSchedule
|