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