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_active_entry_stream.h"
12 :
13 : #include "aicpusd_status.h"
14 : #include "aicpusd_monitor.h"
15 : #include "aicpusd_profiler.h"
16 : #include "aicpusd_msg_send.h"
17 : #include "aicpusd_drv_manager.h"
18 : #include "aicpusd_model_execute.h"
19 : #include "aicpusd_model_statistic.h"
20 : #include "aicpusd_resource_manager.h"
21 : #include "operator_kernel_common.h"
22 :
23 :
24 : namespace AicpuSchedule {
25 : namespace {
26 : const std::string KERNEL_ACTIVE_ENTRY_STREAM = "activeEntryStream";
27 : } // namespace
28 :
29 8 : int32_t OperatorKernelActiveEntryStream::Compute(const AicpuTaskInfo &kernelTaskInfo, const RunContext &taskContext)
30 : {
31 8 : const auto streamIdPtr = PtrToPtr<void, uint32_t>(ValueToPtr(static_cast<uintptr_t>(kernelTaskInfo.paraBase)));
32 8 : if (streamIdPtr == nullptr) {
33 1 : aicpusd_err("ModelActiveEntryStream kernelTaskInfo paramBase is null, modelId[%u], streamId[%u], taskId[%u]",
34 : taskContext.modelId, taskContext.streamId, kernelTaskInfo.taskID);
35 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
36 : }
37 7 : return DoCompute(*streamIdPtr, taskContext);
38 : }
39 :
40 12 : int32_t OperatorKernelActiveEntryStream::DoCompute(const uint32_t streamId, const RunContext &taskContext) const
41 : {
42 12 : aicpusd_info("Begin to active ModeId[%u] streamId[%u].", taskContext.modelId, streamId);
43 12 : uint32_t streamFlag = 0U;
44 12 : auto ret = ModelStreamManager::GetInstance().GetStreamFlag(streamId, streamFlag);
45 12 : if (ret != AICPU_SCHEDULE_OK) {
46 1 : aicpusd_err("Model active stream[%u] is not found, modelId[%u], streamId[%u]", streamId, taskContext.modelId,
47 : taskContext.streamId);
48 1 : return ret;
49 : }
50 :
51 11 : const auto model = AicpuModelManager::GetInstance().GetModel(taskContext.modelId);
52 11 : if (model == nullptr) {
53 1 : aicpusd_err("Model active entry stream failed by no model found, modelId=%u, streamId=%u",
54 : taskContext.modelId, streamId);
55 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
56 : }
57 :
58 19 : if (((model->GetModelRetCode() != 0) && model->AbnormalEnabled()) ||
59 9 : model->GetNullDataFlag()) {
60 2 : aicpusd_run_info("Model no need active stream[%u]. modelId[%u], streamId[%u], modelRetCode=%d, nullFlag=%d",
61 : streamId, taskContext.modelId, taskContext.streamId, model->GetModelRetCode(),
62 : static_cast<int32_t>(model->GetNullDataFlag()));
63 2 : return SubmitEndGraph(taskContext.modelId);
64 : }
65 :
66 8 : AicpuSdModelStatistic::GetInstance().MarNNModelStartTime(taskContext.modelId);
67 :
68 8 : if ((streamFlag & AICPU_STREAM_INDEX) != 0U) {
69 1 : if (taskContext.executeInline) {
70 1 : aicpusd_info("ModelId[%u] switch to RunContext streamId from [%u] to [%u].", taskContext.modelId,
71 : taskContext.streamId, streamId);
72 1 : uint32_t *contextStreamId = const_cast<uint32_t *>(&taskContext.streamId);
73 1 : *contextStreamId = streamId;
74 1 : return AICPU_SCHEDULE_OK;
75 : } else {
76 0 : aicpusd_info("Other thread execute stream[%u].", streamId);
77 0 : AICPUSubEventInfo subEventInfo = {};
78 0 : subEventInfo.modelId = taskContext.modelId;
79 0 : subEventInfo.para.streamInfo.streamId = streamId;
80 0 : ret = OperatorKernelCommon::SendAICPUSubEvent(PtrToPtr<AICPUSubEventInfo, char_t>(&subEventInfo),
81 : static_cast<uint32_t>(sizeof(AICPUSubEventInfo)), AICPU_SUB_EVENT_ACTIVE_STREAM);
82 0 : return ret;
83 : }
84 : } else {
85 : // call drv interface to write register.
86 7 : const uint32_t pid = static_cast<uint32_t>(AicpuDrvManager::GetInstance().GetHostPid());
87 7 : const uint32_t tsId = taskContext.modelTsId;
88 7 : g_aicpuProfiler.SetTsStreamId(streamId);
89 7 : AicpuSqeAdapter aicpuSqeAdapter(FeatureCtrl::GetTsMsgVersion());
90 7 : const uint32_t deviceId = AicpuDrvManager::GetInstance().GetDeviceId();
91 7 : aicpusd_info("Begin to active drv ts stream, tsId[%u], tsStreamId[%u], modelId[%u], streamId[%u], pid[%u],"
92 : "devId[%u].", tsId, streamId, taskContext.modelId, taskContext.streamId, pid, deviceId);
93 : AicpuSqeAdapter::ActiveStreamInfo activateInfo(static_cast<uint16_t>(streamId), static_cast<uint8_t>(tsId),
94 7 : g_aicpuProfiler.GetKernelTrack().procEventStart, deviceId,
95 14 : taskContext.modelId);
96 7 : aicpuSqeAdapter.AicpuActiveStreamSetMsg(activateInfo);
97 7 : model->IncreaseActiveStreamNum();
98 7 : }
99 7 : AicpuMonitor::GetInstance().SetModelStartTime(taskContext.modelId);
100 7 : g_aicpuProfiler.SetActiveStream();
101 7 : return AICPU_SCHEDULE_OK;
102 : }
103 :
104 2 : int32_t OperatorKernelActiveEntryStream::SubmitEndGraph(const uint32_t modelId) const
105 : {
106 2 : aicpusd_info("Begin to submit EndGraph. modelId[%u].", modelId);
107 2 : AICPUSubEventInfo subEventInfo = {};
108 2 : subEventInfo.modelId = modelId;
109 2 : subEventInfo.para.endGraphInfo.result = 0;
110 2 : const int32_t ret = OperatorKernelCommon::SendAICPUSubEvent(PtrToPtr<AICPUSubEventInfo, char_t>(&subEventInfo),
111 : static_cast<uint32_t>(sizeof(AICPUSubEventInfo)), AICPU_SUB_EVENT_END_GRAPH);
112 2 : return ret;
113 : }
114 :
115 6 : REGISTER_OPERATOR_KERNEL(KERNEL_ACTIVE_ENTRY_STREAM, OperatorKernelActiveEntryStream);
116 : } // namespace AicpuSchedule
|