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 "aicpusd_model_execute.h"
12 : #include <algorithm>
13 : #include <stack>
14 : #include <cstring>
15 : #include "aicpusd_resource_manager.h"
16 : #include "aicpusd_drv_manager.h"
17 : #include "aicpusd_monitor.h"
18 : #include "aicpusd_profiler.h"
19 : #include "aicpusd_info.h"
20 : #include "aicpu_event_struct.h"
21 : #include "type_def.h"
22 : #include "profiling_adp.h"
23 : #include "aicpusd_event_process.h"
24 : #include "aicpusd_context.h"
25 :
26 : namespace {
27 : constexpr uint32_t TLV_WITH_SHAPE = 1000;
28 : constexpr uint32_t TLV_WITH_DTYPE = 1001;
29 : }
30 : namespace AicpuSchedule {
31 : bool AicpuModelManager::isUsed_(false);
32 551 : AicpuModelManager &AicpuModelManager::GetInstance()
33 : {
34 551 : static AicpuModelManager instance;
35 551 : isUsed_ = true;
36 551 : return instance;
37 : }
38 :
39 13 : int32_t AicpuModelManager::ModelLoad(const AicpuModelInfo * const modelInfo, const ModelCfgInfo * const cfgInfo)
40 : {
41 13 : if (modelInfo == nullptr) {
42 1 : aicpusd_err("ModelLoad failed, as param is null.");
43 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
44 : }
45 12 : const auto modelId = modelInfo->moduleID;
46 12 : if (modelId >= MAX_MODEL_COUNT) {
47 1 : aicpusd_err("ModelLoad failed, as modelId[%u] is invalid.", modelId);
48 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
49 : }
50 :
51 11 : return allModel_[modelId].ModelLoad(modelInfo, cfgInfo);
52 : }
53 :
54 57 : AicpuModel *AicpuModelManager::GetModel(const uint32_t modelId)
55 : {
56 57 : if (modelId >= MAX_MODEL_COUNT) {
57 1 : aicpusd_err("Get model failed, as modelId[%u] is invalid.", modelId);
58 1 : return nullptr;
59 : }
60 56 : auto &model = allModel_[modelId];
61 56 : if (!model.IsValid()) {
62 25 : aicpusd_err("Get model failed, as model[%u] is invalid.", modelId);
63 25 : return nullptr;
64 : }
65 31 : return &model;
66 : }
67 :
68 1 : AicpuModel *AicpuModelManager::GetModelByStreamId(const uint32_t streamId)
69 : {
70 1 : uint32_t modelId = 0U;
71 1 : const int32_t ret = ModelStreamManager::GetInstance().GetStreamModelId(streamId, modelId);
72 1 : if (ret != AICPU_SCHEDULE_OK) {
73 1 : aicpusd_err("can't find model by stream[%u].", streamId);
74 1 : return nullptr;
75 : }
76 0 : return GetModel(modelId);
77 : }
78 :
79 3 : AicpuModel *AicpuModelManager::GetModelByQueueId(const uint32_t queueId)
80 : {
81 3075 : for (size_t i = 0U; i < static_cast<size_t>(MAX_MODEL_COUNT); ++i) {
82 3072 : if (allModel_[i].IsValid() && (allModel_[i].HasQueue(queueId))) {
83 0 : return &allModel_[i];
84 : }
85 : }
86 3 : return nullptr;
87 : }
88 :
89 1 : std::vector<AicpuModel*> AicpuModelManager::GetModelsByTableId(const uint32_t tableId)
90 : {
91 1 : std::vector<AicpuModel*> models;
92 1025 : for (size_t i = 0U; i < static_cast<size_t>(MAX_MODEL_COUNT); ++i) {
93 1024 : if (allModel_[i].IsValid() && (allModel_[i].GetTableTryLock() == static_cast<int64_t>(tableId))) {
94 1 : models.emplace_back(&allModel_[i]);
95 : }
96 : }
97 1 : return models;
98 0 : }
99 :
100 21 : int32_t AicpuModelManager::Exit()
101 : {
102 21 : int32_t ret = AICPU_SCHEDULE_OK;
103 21 : int32_t tmpRet = AICPU_SCHEDULE_OK;
104 21525 : for (auto &model : allModel_) {
105 21504 : tmpRet = model.Exit();
106 21504 : if ((tmpRet != AICPU_SCHEDULE_OK) && (ret == AICPU_SCHEDULE_OK)) {
107 0 : ret = AICPU_SCHEDULE_ERROR_MODEL_EXIT_ERR;
108 : }
109 : }
110 21 : tensorDescMap_.clear();
111 21 : msgQMap_.clear();
112 21 : return ret;
113 : }
114 :
115 9 : void AicpuModelManager::ModelConfigClear(const uint32_t modelId)
116 : {
117 9 : const auto iter = tensorDescMap_.find(modelId);
118 9 : if (iter == tensorDescMap_.end()) {
119 9 : aicpusd_warn("modelId[%u] does not exist.", modelId);
120 9 : return;
121 : }
122 0 : (void)tensorDescMap_.erase(iter);
123 :
124 0 : const auto msqQIter = msgQMap_.find(modelId);
125 0 : if (msqQIter != msgQMap_.end()) {
126 0 : msgQMap_.erase(modelId);
127 : }
128 : }
129 :
130 9 : AicpuModelStatus AicpuModelManager::GetModelStatus(const uint32_t modelId) const
131 : {
132 9 : if (modelId >= MAX_MODEL_COUNT) {
133 1 : aicpusd_err("Get model status failed, as modelId[%u] is invalid.", modelId);
134 1 : return AicpuModelStatus::MODEL_STATUS_ERROR;
135 : }
136 8 : return allModel_[modelId].GetModelStatus();
137 : }
138 :
139 6 : StatusCode AicpuModelManager::TransModelInfo(const void * const ptr,
140 : AicpuModelInfo &aicpuModelInfo,
141 : std::vector<AicpuTaskInfo> &aicpuTaskInfos,
142 : std::vector<StreamInfo> &streamInfos,
143 : std::vector<QueInfo> &queInfos,
144 : std::vector<ModelCfgInfo> *const modelcfgs)
145 : {
146 6 : const ModelInfo * const curModelInfo = PtrToPtr<const void, const ModelInfo>(ptr);
147 6 : aicpuModelInfo.moduleID = curModelInfo->modelId;
148 6 : aicpuModelInfo.tsId = 0U;
149 : // transform stream and task info
150 6 : const ModelStreamInfo * const streams = curModelInfo->streams;
151 6 : if (streams == nullptr) {
152 1 : aicpusd_err("Streams of model info is nullptr.");
153 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
154 : }
155 9 : for (size_t streamIndex = 0UL; streamIndex < curModelInfo->aicpuStreamNum; streamIndex++) {
156 5 : const auto &stream = streams[streamIndex];
157 5 : const StreamInfo stmInfo = {stream.streamId, stream.streamFlag};
158 5 : streamInfos.push_back(stmInfo);
159 5 : const uint16_t taskNum = stream.taskNum;
160 5 : const ModelTaskInfo * const tasks = stream.tasks;
161 5 : if ((taskNum > 0) && (tasks == nullptr)) {
162 1 : aicpusd_err("Tasks of stream info is nullptr.");
163 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
164 : }
165 7 : for (size_t taskIndex = 0UL; taskIndex < taskNum; taskIndex++) {
166 3 : const auto &taskInfo = tasks[taskIndex];
167 3 : const AicpuTaskInfo curAicpuTaskInfo = {
168 3 : taskInfo.taskId, stream.streamId,
169 : static_cast<uint32_t>(AicpuKernelType::CCE_KERNEL),
170 3 : taskInfo.kernelName, 0U, taskInfo.paraBase, 0U
171 3 : };
172 3 : aicpuTaskInfos.push_back(curAicpuTaskInfo);
173 : }
174 : }
175 4 : if (aicpuTaskInfos.empty() || streamInfos.empty()) {
176 1 : aicpusd_err("one of tasks[%zu], streams[%zu] is zero.", aicpuTaskInfos.size(), streamInfos.size());
177 1 : return AICPU_SCHEDULE_ERROR_TRANS_MODELINFO_FAILED;
178 : }
179 :
180 : // transform queue info
181 3 : const ModelQueueInfo * const queues = curModelInfo->queues;
182 3 : if (queues != nullptr) {
183 4 : for (size_t queueIndex = 0UL; queueIndex < curModelInfo->queueNum; queueIndex++) {
184 2 : const ModelQueueInfo &queueInfo = queues[queueIndex];
185 2 : const QueInfo curModelQueue = {queueInfo.queueId, queueInfo.flag};
186 2 : queInfos.push_back(curModelQueue);
187 : }
188 : }
189 :
190 : // transform model cfg
191 3 : if ((curModelInfo->cfgInfoPtr != 0U) && (modelcfgs != nullptr)) {
192 0 : (void)modelcfgs->emplace_back(*(PtrToPtr<void, ModelCfgInfo>(ValueToPtr(curModelInfo->cfgInfoPtr))));
193 : }
194 :
195 3 : aicpuModelInfo.streamInfoNum = static_cast<uint16_t>(streamInfos.size());
196 3 : aicpuModelInfo.streamInfoPtr = PtrToValue(&streamInfos[0UL]);
197 3 : aicpuModelInfo.aicpuTaskNum = static_cast<uint16_t>(aicpuTaskInfos.size());
198 3 : aicpuModelInfo.aicpuTaskPtr = PtrToValue(&aicpuTaskInfos[0UL]);
199 3 : aicpuModelInfo.queueSize = static_cast<uint16_t>(queInfos.size());
200 3 : aicpuModelInfo.queueInfoPtr = queInfos.size() > 0U ? PtrToValue(&queInfos[0UL]) : 0U;
201 3 : abnormalBreaks_[aicpuModelInfo.moduleID] = curModelInfo->abnormalBreak;
202 3 : abnormalEnqueues_[aicpuModelInfo.moduleID] = curModelInfo->abnormalEnqueue;
203 3 : abnormalFlags_[aicpuModelInfo.moduleID] = curModelInfo->abnormalEnable;
204 3 : const auto ret = ProcessModelPriorityMsg(curModelInfo->aicpuPriInfo, false);
205 3 : if (ret != AICPU_SCHEDULE_OK) {
206 0 : aicpusd_err("set priority error ret[%d]", ret);
207 0 : return AICPU_SCHEDULE_ERROR_SET_PRIORITY_FAILED;
208 : }
209 3 : return AICPU_SCHEDULE_OK;
210 : }
211 :
212 10 : uint32_t AicpuModelManager::GetExtModelId(const uint32_t modelId)
213 : {
214 10 : if (modelId >= MAX_MODEL_COUNT) {
215 0 : return INVALID_NUMBER;
216 : }
217 10 : return extModelIds_[modelId];
218 : }
219 :
220 10 : bool AicpuModelManager::AbnormalBreak(const uint32_t modelId)
221 : {
222 10 : if (modelId >= MAX_MODEL_COUNT) {
223 0 : return false;
224 : }
225 10 : return abnormalBreaks_[modelId] != 0;
226 : }
227 :
228 10 : bool AicpuModelManager::AbnormalEnqueue(const uint32_t modelId)
229 : {
230 10 : if (modelId >= MAX_MODEL_COUNT) {
231 0 : return false;
232 : }
233 10 : return abnormalEnqueues_[modelId] != 0;
234 : }
235 :
236 10 : bool AicpuModelManager::AbnormalEnabled(const uint32_t modelId)
237 : {
238 10 : if (modelId >= MAX_MODEL_COUNT) {
239 0 : return false;
240 : }
241 10 : return abnormalFlags_[modelId] != 0;
242 : }
243 :
244 5125 : AicpuModelManager::AicpuModelManager()
245 : {
246 5125 : for (uint32_t &extModelId : extModelIds_) {
247 5120 : extModelId = INVALID_NUMBER;
248 : }
249 5 : }
250 :
251 3 : StatusCode AicpuModelManager::ProcessExtInfoCfgMsg(const aicpu::AicpuExtendInfo &cfgInfo)
252 : {
253 3 : const auto msgType = static_cast<aicpu::AicpuExtInfoMsgType>(cfgInfo.msgType);
254 3 : aicpusd_info("Begin to ProcessExtInfoCfgMsg, msgType[%u], version[%u], model_id[%u], extend_model_id[%u].",
255 : msgType, cfgInfo.version, cfgInfo.modelIdMap.modelId, cfgInfo.modelIdMap.extendModelId);
256 3 : if (msgType != aicpu::AicpuExtInfoMsgType::EXT_MODEL_ID_MSG_TYPE) {
257 1 : aicpusd_err("Invalid msgType of ProcessExtInfoCfgMsg, msgType[%d].", msgType);
258 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
259 : }
260 2 : if (cfgInfo.modelIdMap.modelId >= MAX_MODEL_COUNT) {
261 1 : aicpusd_err("Invalid model id of ProcessExtInfoCfgMsg, model id[%d].", cfgInfo.modelIdMap.modelId);
262 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
263 : }
264 1 : extModelIds_[cfgInfo.modelIdMap.modelId] = cfgInfo.modelIdMap.extendModelId;
265 1 : return AICPU_SCHEDULE_OK;
266 : }
267 :
268 11 : StatusCode AicpuModelManager::CheckModelConfigShape(
269 : const uint32_t type, const uint32_t tlvLen, int32_t &unparseLen) const
270 : {
271 11 : if (type != TLV_WITH_SHAPE) {
272 1 : aicpusd_err("it is should be shape type, but not. type[%u]", type);
273 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
274 : }
275 10 : const int64_t shapeSize = static_cast<int64_t>(tlvLen) / static_cast<int64_t>(sizeof(int64_t));
276 10 : if ((tlvLen % sizeof(int64_t) != 0) || (shapeSize > MAX_DIM_SIZE)) {
277 0 : aicpusd_err("shape info is invalid, please check. tlv len[%u], shape size[%u]", tlvLen, shapeSize);
278 0 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
279 : }
280 :
281 10 : if (unparseLen < (static_cast<int32_t>(sizeof(TlvHead)) + static_cast<int32_t>(tlvLen))) {
282 0 : aicpusd_err("aicpu model config tensor length is error, please check.");
283 0 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
284 : }
285 10 : unparseLen = unparseLen - (static_cast<int32_t>(sizeof(TlvHead)) + static_cast<int32_t>(tlvLen));
286 10 : return AICPU_SCHEDULE_OK;
287 : }
288 :
289 10 : StatusCode AicpuModelManager::CheckModelConfigDtype(const TlvHead tlvHeadAddr, int32_t &unparseLen) const
290 : {
291 10 : if (unparseLen < static_cast<int32_t>(sizeof(TlvHead))) {
292 0 : aicpusd_err("aicpu model config tensor length is error, please check.");
293 0 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
294 : }
295 :
296 10 : const uint32_t type = tlvHeadAddr.type;
297 10 : const uint32_t len = tlvHeadAddr.len;
298 10 : if (type != TLV_WITH_DTYPE) {
299 1 : aicpusd_err("it is should be dtype type, but not. type[%u]", type);
300 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
301 : }
302 : // check unparse length
303 9 : if (unparseLen < (static_cast<int32_t>(sizeof(TlvHead)) + static_cast<int32_t>(len))) {
304 1 : aicpusd_err("aicpumodel config tensor length is error, please check.");
305 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
306 : }
307 8 : unparseLen = unparseLen - (static_cast<int32_t>(sizeof(TlvHead)) + static_cast<int32_t>(len));
308 8 : return AICPU_SCHEDULE_OK;
309 : }
310 :
311 9 : StatusCode AicpuModelManager::ParseModelConfigTensorDesc(const AicpuModelShapeConfig &cfg)
312 : {
313 : // parse tlv, get ModelConfigTensorDesc
314 9 : int32_t totalLen = static_cast<int32_t>(cfg.tensortlvLen);
315 9 : TlvHead *tlvHeadAddr = PtrToPtr<void, TlvHead>(ValueToPtr(cfg.tlvDataAddr));
316 9 : if (tlvHeadAddr == nullptr) {
317 0 : aicpusd_err("tlv data addr is invalid");
318 0 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
319 : }
320 9 : std::vector<ModelConfigTensorDesc> tensorInfo;
321 :
322 17 : while (totalLen > 0) {
323 : ModelConfigTensorDesc tensorDesc;
324 11 : if (totalLen < static_cast<int32_t>(sizeof(TlvHead))) {
325 0 : aicpusd_err("aicpu model config tensor length is error, please check.");
326 3 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
327 : }
328 11 : const uint32_t type = tlvHeadAddr->type;
329 11 : const uint32_t len = tlvHeadAddr->len;
330 11 : uint8_t *dataAddr = PtrToPtr<TlvHead, uint8_t>(tlvHeadAddr) + sizeof(TlvHead);
331 11 : tensorDesc.shape[0] = static_cast<int64_t>(len) / static_cast<int64_t>(sizeof(int64_t));
332 11 : StatusCode ret = CheckModelConfigShape(type, len, totalLen);
333 11 : if (ret != AICPU_SCHEDULE_OK) {
334 1 : aicpusd_err("CheckModelConfigShape failed");
335 1 : return ret;
336 : }
337 26 : for (int64_t j = 0; j < tensorDesc.shape[0]; j++) {
338 16 : int64_t *shapeValue = PtrToPtr<uint8_t, int64_t>(dataAddr + static_cast<int64_t>(j * sizeof(int64_t)));
339 16 : tensorDesc.shape[j + 1] = *shapeValue;
340 : }
341 :
342 10 : tlvHeadAddr = PtrToPtr<uint8_t, TlvHead>(dataAddr + len);
343 10 : ret = CheckModelConfigDtype(*tlvHeadAddr, totalLen);
344 10 : if (ret != AICPU_SCHEDULE_OK) {
345 2 : aicpusd_err("check model config dtype failed");
346 2 : return ret;
347 : }
348 :
349 8 : dataAddr = PtrToPtr<TlvHead, uint8_t>(tlvHeadAddr) + sizeof(TlvHead);
350 8 : uint32_t *dtypeVal = PtrToPtr<uint8_t, uint32_t>(dataAddr);
351 8 : tensorDesc.dtype = static_cast<int64_t>(*dtypeVal);
352 8 : tlvHeadAddr = PtrToPtr<uint8_t, TlvHead>(dataAddr + tlvHeadAddr->len);
353 8 : tensorInfo.push_back(tensorDesc);
354 : }
355 :
356 6 : tensorDescMap_[cfg.runtimeModelId] = tensorInfo;
357 6 : aicpusd_info("tensorInfo size = %zu, cfg.runtimeModelId = %u", tensorInfo.size(), cfg.runtimeModelId);
358 6 : return AICPU_SCHEDULE_OK;
359 9 : }
360 :
361 5 : StatusCode AicpuModelManager::GetModelConfigShape(
362 : const uint32_t modelId, std::vector<ModelConfigTensorDesc> &tensorDescArr)
363 : {
364 5 : if (tensorDescMap_.find(modelId) == tensorDescMap_.end()) {
365 1 : aicpusd_warn("not find modelId[%u] aicpu model shape config", modelId);
366 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
367 : }
368 4 : tensorDescArr = tensorDescMap_[modelId];
369 4 : return AICPU_SCHEDULE_OK;
370 : }
371 :
372 5 : StatusCode AicpuModelManager::ProcessModelConfigMsg(const AicpuModelConfig &cfg)
373 : {
374 5 : aicpusd_info("Begin to process model config, version[%d], model_id[%u], extend_model_id[%u], "
375 : "abnormal_break[%d], abnormal_enqueue[%d], inputMsgQueue[%d], outputMsgQueue[%d].",
376 : cfg.version, cfg.runtimeModelId, cfg.geModelId, cfg.abnormalBreak, cfg.abnormalEnqueue,
377 : cfg.inputMsgQueue, cfg.outputMsgQueue);
378 5 : if (cfg.runtimeModelId >= MAX_MODEL_COUNT) {
379 0 : aicpusd_err("Invalid model id[%u] of model config, must < [%u].", cfg.runtimeModelId, MAX_MODEL_COUNT);
380 0 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
381 : }
382 5 : extModelIds_[cfg.runtimeModelId] = cfg.geModelId;
383 5 : abnormalBreaks_[cfg.runtimeModelId] = cfg.abnormalBreak;
384 5 : abnormalEnqueues_[cfg.runtimeModelId] = cfg.abnormalEnqueue;
385 5 : abnormalFlags_[cfg.runtimeModelId] = true;
386 :
387 5 : if ((cfg.inputMsgQueue <= 0) && (cfg.outputMsgQueue <= 0)) {
388 1 : return AICPU_SCHEDULE_OK;
389 : }
390 :
391 4 : if (cfg.inputMsgQueue >= 0) {
392 4 : QueueSetInputPara inPutParam = {};
393 4 : QueueSetInput inPut = {};
394 4 : inPut.queSetWorkMode.qid = cfg.inputMsgQueue;
395 4 : inPut.queSetWorkMode.workMode = QUEUE_MODE_PULL;
396 4 : inPutParam.inBuff = static_cast<void *>(&inPut);
397 4 : inPutParam.inLen = static_cast<uint32_t>(sizeof(QueueSetInput));
398 4 : const auto ret = halQueueSet(0U, QUEUE_SET_WORK_MODE, &inPutParam);
399 4 : if (ret != DRV_ERROR_NONE) {
400 1 : aicpusd_err("Fail to set work mode for queue[%d], ret is %d.", cfg.inputMsgQueue,
401 : static_cast<int32_t>(ret));
402 2 : return AICPU_SCHEDULE_ERROR_FROM_DRV;
403 : }
404 : const auto subscribeRet =
405 3 : AicpuDrvManager::GetInstance().SubscribeQueueNotEmptyEvent(static_cast<uint32_t>(cfg.inputMsgQueue));
406 3 : if (subscribeRet != AICPU_SCHEDULE_OK) {
407 1 : aicpusd_err("Fail to subscribe queue[%d] not empty event.", cfg.inputMsgQueue);
408 1 : return subscribeRet;
409 : }
410 2 : aicpusd_info("SubscribeQueueNotEmptyEvent for queue[%d]", cfg.inputMsgQueue);
411 : }
412 :
413 2 : if (cfg.outputMsgQueue >= 0) {
414 : const auto subscribeRet =
415 2 : AicpuDrvManager::GetInstance().SubscribeQueueNotFullEvent(static_cast<uint32_t>(cfg.outputMsgQueue));
416 2 : if (subscribeRet != AICPU_SCHEDULE_OK) {
417 1 : aicpusd_err("Fail to subscribe queue[%d] not full event.", cfg.outputMsgQueue);
418 1 : return subscribeRet;
419 : }
420 1 : aicpusd_info("SubscribeQueueNotFullEvent for queue[%d]", cfg.outputMsgQueue);
421 : }
422 1 : msgQMap_[cfg.runtimeModelId] = std::make_pair(cfg.inputMsgQueue, cfg.outputMsgQueue);
423 1 : return AICPU_SCHEDULE_OK;
424 : }
425 :
426 9 : StatusCode AicpuModelManager::ProcessModelShapeConfigMsg(const AicpuModelShapeConfig &cfg)
427 : {
428 9 : aicpusd_info("Begin to process model config, version[%d], model_id[%u], extend_model_id[%u], "
429 : "tensor info length[%u]",
430 : cfg.version, cfg.runtimeModelId, cfg.geModelId, cfg.tensortlvLen);
431 9 : if (cfg.runtimeModelId >= MAX_MODEL_COUNT) {
432 0 : aicpusd_err("Invalid model id[%u] of model config, must < [%u].", cfg.runtimeModelId, MAX_MODEL_COUNT);
433 0 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
434 : }
435 :
436 9 : if (cfg.tensortlvLen != 0) {
437 9 : const StatusCode ret = ParseModelConfigTensorDesc(cfg);
438 9 : if (ret != AICPU_SCHEDULE_OK) {
439 3 : aicpusd_err("Save Model Config TensorDesc failed");
440 3 : return ret;
441 : }
442 : }
443 6 : return AICPU_SCHEDULE_OK;
444 : }
445 :
446 6 : StatusCode AicpuModelManager::SetPidPriority(const AicpuPriInfo &cfg, const std::vector<uint32_t> &deviceVec)
447 : {
448 6 : if ((cfg.pidPriority == INVALID_ESCAPE_PRI_VALUE) ||
449 5 : ((cfg.pidPriority > curPidPri_) && (curPidPri_ != INVALID_ESCAPE_PRI_VALUE))) {
450 3 : aicpusd_info("[AicpuModelEschedPriority] the new pid priority[%d] is lower than the current[%d]",
451 : cfg.pidPriority, curPidPri_);
452 3 : return AICPU_SCHEDULE_OK;
453 : }
454 :
455 3 : const std::lock_guard<std::mutex> lockForSetPidPri(mutexForSetPidPri_);
456 3 : aicpusd_info("[AicpuModelEschedPriority] Begin to set pid priority vect size[%u], pidPriority[%d], input[%d]",
457 : deviceVec.size(), curPidPri_, cfg.pidPriority);
458 6 : for (size_t i = 0U; i < deviceVec.size(); i++) {
459 3 : if (cfg.pidPriority < curPidPri_ || curPidPri_ == INVALID_ESCAPE_PRI_VALUE) {
460 2 : const auto ret = halEschedSetPidPriority(deviceVec[i], static_cast<SCHEDULE_PRIORITY>(cfg.pidPriority));
461 2 : if (ret != DRV_ERROR_NONE) {
462 0 : aicpusd_err("[AicpuModelEschedPriority]Failed to set priority [%d], deviceid[%u], result[%d].",
463 : cfg.pidPriority, deviceVec[i], ret);
464 0 : return AICPU_SCHEDULE_ERROR_DRV_ERR;
465 : }
466 2 : aicpusd_info("[AicpuModelEschedPriority] set pid priority success Index[%u], deviceid[%u]",
467 : i, deviceVec[i]);
468 2 : curPidPri_ = cfg.pidPriority;
469 : }
470 : }
471 3 : aicpusd_info("[AicpuModelEschedPriority] end to set pid priority pidPriority[%d]", curPidPri_);
472 3 : return AICPU_SCHEDULE_OK;
473 3 : }
474 :
475 6 : StatusCode AicpuModelManager::SetEventPriority(const AicpuPriInfo &cfg, const std::vector<uint32_t> &deviceVec)
476 : {
477 6 : if ((cfg.eventPriority == INVALID_ESCAPE_PRI_VALUE) ||
478 5 : ((cfg.eventPriority > curEventPri_) && (curEventPri_ != INVALID_ESCAPE_PRI_VALUE))) {
479 2 : aicpusd_info("[AicpuModelEschedPriority] the new event priority[%d] is lower than the current[%d]",
480 : cfg.eventPriority, curEventPri_);
481 2 : return AICPU_SCHEDULE_OK;
482 : }
483 :
484 4 : const std::lock_guard<std::mutex> lockForSetEvnPri(mutexForSetEvnPri_);
485 4 : aicpusd_info("[AicpuModelEschedPriority] Begin to set event priority vecSize[%u], eventPri[%d], input[%d]",
486 : deviceVec.size(), curEventPri_, cfg.eventPriority);
487 8 : for (size_t i = 0U; i < deviceVec.size(); i++) {
488 4 : auto ret = halEschedSetEventPriority(deviceVec[i], EVENT_QUEUE_EMPTY_TO_NOT_EMPTY,
489 4 : static_cast<SCHEDULE_PRIORITY>(cfg.eventPriority));
490 4 : if (ret != DRV_ERROR_NONE) {
491 0 : aicpusd_err("[AicpuModelEschedPriority] failed set EVENT_QUEUE_EMPTY_TO_NOT_EMPTY ret[%d].", ret);
492 0 : return AICPU_SCHEDULE_ERROR_DRV_ERR;
493 : }
494 4 : aicpusd_info("[AicpuModelEschedPriority] set EVENT_QUEUE_EMPTY_TO_NOT_EMPTY priority success Index[%u],"
495 : "deviceid[%u]", i, deviceVec[i]);
496 4 : ret = halEschedSetEventPriority(deviceVec[i], EVENT_QUEUE_FULL_TO_NOT_FULL,
497 4 : static_cast<SCHEDULE_PRIORITY>(cfg.eventPriority));
498 4 : if (ret != DRV_ERROR_NONE) {
499 0 : aicpusd_err("[AicpuModelEschedPriority] failed set EVENT_QUEUE_FULL_TO_NOT_FULL ret[%d].", ret);
500 0 : return AICPU_SCHEDULE_ERROR_DRV_ERR;
501 : }
502 4 : aicpusd_info("[AicpuModelEschedPriority] set EVENT_QUEUE_FULL_TO_NOT_FULL priority success Index[%u],"
503 : "deviceid[%u]", i, deviceVec[i]);
504 : }
505 4 : curEventPri_ = cfg.eventPriority;
506 4 : aicpusd_info("[AicpuModelEschedPriority] end to set event priority eventPri[%d]", curEventPri_);
507 4 : return AICPU_SCHEDULE_OK;
508 4 : }
509 :
510 11 : StatusCode AicpuModelManager::ProcessModelPriorityMsg(const AicpuPriInfo &cfg, const bool isProcessMode)
511 : {
512 11 : aicpusd_info("[AicpuModelEschedPriority] Begin to process AicpuModelEschedPriority");
513 11 : if (cfg.checkHead != PRIORITY_MSG_CHECKCODE) {
514 : // checkcode error in process mode is msg error return fail
515 5 : if (isProcessMode) {
516 2 : aicpusd_err("[AicpuModelEschedPriority] the msg checkcode is error [%x]", cfg.checkHead);
517 2 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
518 : } else {
519 3 : return AICPU_SCHEDULE_OK;
520 : }
521 : }
522 : // get device list to set priority
523 6 : const std::vector<uint32_t> deviceVec = AicpuDrvManager::GetInstance().GetDeviceList();
524 6 : if (deviceVec.empty()) {
525 0 : aicpusd_err("[AicpuModelEschedPriority] the device vector is empty");
526 0 : return AICPU_SCHEDULE_ERROR_DRV_ERR;
527 : }
528 : // set pid priority
529 6 : auto ret = SetPidPriority(cfg, deviceVec);
530 6 : if (ret != AICPU_SCHEDULE_OK) {
531 0 : aicpusd_err("[AicpuModelEschedPriority] set pid priority error ret[%d]", ret);
532 0 : return AICPU_SCHEDULE_ERROR_DRV_ERR;
533 : }
534 : // set event priority
535 6 : ret = SetEventPriority(cfg, deviceVec);
536 6 : if (ret != AICPU_SCHEDULE_OK) {
537 0 : aicpusd_err("[AicpuModelEschedPriority] set event priority error ret[%d]", ret);
538 0 : return AICPU_SCHEDULE_ERROR_DRV_ERR;
539 : }
540 6 : return AICPU_SCHEDULE_OK;
541 6 : }
542 26 : StatusCode AicpuModelManager::GetModelMsgQueues(const uint32_t modelId, const bool isInput, int32_t &queueId) const
543 : {
544 26 : const auto iter = msgQMap_.find(modelId);
545 26 : if (iter == msgQMap_.end()) {
546 20 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
547 : }
548 6 : queueId = isInput ? iter->second.first : iter->second.second;
549 6 : if (queueId < 0) {
550 2 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
551 : }
552 4 : return AICPU_SCHEDULE_OK;
553 : }
554 :
555 24 : bool AicpuModelManager::IsUsed()
556 : {
557 24 : return AicpuModelManager::isUsed_;
558 : }
559 : }
|