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