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_model_repeat.h"
12 :
13 : #include "aicpusd_status.h"
14 : #include "aicpusd_profiler.h"
15 : #include "aicpusd_model_execute.h"
16 : #include "aicpusd_resource_manager.h"
17 : #include "operator_kernel_common.h"
18 :
19 : namespace AicpuSchedule {
20 : namespace {
21 : const std::string KERNEL_MODEL_REPEAT = "modelRepeat";
22 : } // namespace
23 :
24 2 : int32_t OperatorKernelModelRepeat::Compute(const AicpuTaskInfo& kernelTaskInfo, const RunContext& taskContext)
25 : {
26 2 : const auto modelIdPtr = PtrToPtr<void, uint32_t>(ValueToPtr(static_cast<uintptr_t>(kernelTaskInfo.paraBase)));
27 2 : if (modelIdPtr == nullptr) {
28 1 : aicpusd_err(
29 : "ModelRepeat kernelTaskInfo paramBase is null, modelId[%u], streamId[%u], taskId[%u]", taskContext.modelId,
30 : taskContext.streamId, kernelTaskInfo.taskID);
31 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
32 : }
33 1 : if (*modelIdPtr != taskContext.modelId) {
34 1 : aicpusd_warn(
35 : "ModelRepeat kernelTaskInfo modelId[%u] is diff with context, modelId[%u], streamId[%u], taskId[%u]",
36 : *modelIdPtr, taskContext.modelId, taskContext.streamId, kernelTaskInfo.taskID);
37 : }
38 1 : ResetStaticNNModelOutputIndex(taskContext.modelId);
39 1 : const auto model = AicpuModelManager::GetInstance().GetModel(taskContext.modelId);
40 1 : if ((model != nullptr) && (model->GetModelRetCode() != 0) && (model->AbnormalNeedBreak())) {
41 0 : aicpusd_err(
42 : "Model execute failed, need to break. modelId=%u, modelRetCode=%d.", taskContext.modelId,
43 : model->GetModelRetCode());
44 0 : return AICPU_SCHEDULE_ERROR_TASK_EXECUTE_FAILED;
45 : }
46 1 : return OperatorKernelModelRepeat::SendModelRepeatEvent(*modelIdPtr);
47 : }
48 :
49 4 : uint32_t OperatorKernelModelRepeat::SendModelRepeatEvent(const uint32_t modelId)
50 : {
51 4 : uint32_t iterCount = 0U;
52 4 : uint32_t activeStreamNum = 0U;
53 4 : const auto model = AicpuModelManager::GetInstance().GetModel(modelId);
54 4 : if (model != nullptr) {
55 3 : iterCount = model->GetIteratorId();
56 3 : activeStreamNum = model->GetActiveStreamNum();
57 : }
58 4 : aicpusd_info(
59 : "Begin to execute ModelRepeat. modelId[%u], activeStreamNum[%u], iterCount[%u]", modelId, activeStreamNum,
60 : iterCount);
61 4 : AICPUSubEventInfo subEventInfo = {};
62 4 : subEventInfo.modelId = modelId;
63 4 : g_aicpuProfiler.SetRepeatStart();
64 4 : const int32_t ret = OperatorKernelCommon::SendAICPUSubEvent(
65 : PtrToPtr<AICPUSubEventInfo, char_t>(&subEventInfo), static_cast<uint32_t>(sizeof(AICPUSubEventInfo)),
66 : AICPU_SUB_EVENT_REPEAT_MODEL);
67 4 : g_aicpuProfiler.SetRepeatEnd();
68 :
69 4 : return ret;
70 : }
71 :
72 1 : void OperatorKernelModelRepeat::ResetStaticNNModelOutputIndex(const uint32_t modelId) const
73 : {
74 1 : const auto model = AicpuModelManager::GetInstance().GetModel(modelId);
75 1 : if (model == nullptr) {
76 1 : return;
77 : }
78 0 : model->ResetStaticNNModelOutputIndex();
79 : }
80 :
81 6 : REGISTER_OPERATOR_KERNEL(KERNEL_MODEL_REPEAT, OperatorKernelModelRepeat);
82 : } // namespace AicpuSchedule
|