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 : #ifndef AICPUSD_MODEL_EXECUTE_H
12 : #define AICPUSD_MODEL_EXECUTE_H
13 :
14 : #include <atomic>
15 : #include <cstdint>
16 : #include <unordered_map>
17 : #include <vector>
18 : #include <unordered_set>
19 : #include <thread>
20 : #include <list>
21 : #include <mutex>
22 : #include "aicpusd_status.h"
23 : #include "aicpusd_common.h"
24 : #include "aicpusd_info.h"
25 : #include "aicpu_task_struct.h"
26 : #include "aicpusd_util.h"
27 : #include "aicpusd_model.h"
28 :
29 : namespace AicpuSchedule {
30 : struct AicpuModelConfig {
31 : int32_t version;
32 : uint32_t geModelId;
33 : uint32_t runtimeModelId;
34 : int32_t abnormalBreak;
35 : int32_t abnormalEnqueue;
36 : int32_t outputMsgQueue;
37 : int32_t inputMsgQueue;
38 : int8_t rsv[36];
39 : };
40 :
41 : struct AicpuModelShapeConfig {
42 : int32_t version;
43 : uint32_t geModelId;
44 : uint32_t runtimeModelId;
45 : uint32_t tensortlvLen;
46 : uint64_t tlvDataAddr;
47 : int8_t rsv[40];
48 : };
49 :
50 : // tlv struct
51 : struct TlvHead {
52 : uint32_t type;
53 : uint32_t len;
54 : uint8_t data[0];
55 : };
56 :
57 : class AicpuModelManager {
58 : public:
59 : static AicpuModelManager& GetInstance();
60 :
61 : int32_t ModelLoad(const AicpuModelInfo* const modelInfo, const ModelCfgInfo* const cfgInfo = nullptr);
62 :
63 : AicpuModel* GetModel(const uint32_t modelId);
64 :
65 : AicpuModel* GetModelByStreamId(const uint32_t streamId);
66 :
67 : AicpuModel* GetModelByQueueId(const uint32_t queueId);
68 :
69 : std::vector<AicpuModel*> GetModelsByTableId(const uint32_t tableId);
70 :
71 : StatusCode ParseModelConfigTensorDesc(const AicpuModelShapeConfig& cfg);
72 :
73 : StatusCode GetModelConfigShape(const uint32_t modelId, std::vector<ModelConfigTensorDesc>& tensorDescArr);
74 :
75 : void ModelConfigClear(const uint32_t modelId);
76 :
77 : /**
78 : * @brief call it when process exit.
79 : * @return AICPU_SCHEDULE_OK:success, other failed.
80 : */
81 : int32_t Exit();
82 :
83 : AicpuModelStatus GetModelStatus(const uint32_t modelId) const;
84 :
85 : StatusCode TransModelInfo(
86 : const void* const ptr, AicpuModelInfo& aicpuModelInfo, std::vector<AicpuTaskInfo>& aicpuTaskInfos,
87 : std::vector<StreamInfo>& streamInfos, std::vector<QueInfo>& queInfos,
88 : std::vector<ModelCfgInfo>* const modelcfgs = nullptr);
89 : /**
90 : * @ingroup ProcessExtInfoCfgMsg
91 : * @brief it is used to process the config of ge model id or other extension info.
92 : * @param [in] cfgInfo : the config info of extension info.
93 : */
94 : StatusCode ProcessExtInfoCfgMsg(const aicpu::AicpuExtendInfo& cfgInfo);
95 :
96 : /**
97 : * @ingroup ProcessModelConfigMsg
98 : * @brief it is used to process the config of ge model.
99 : * @param [in] cfg : the config of model.
100 : */
101 : StatusCode ProcessModelConfigMsg(const AicpuModelConfig& cfg);
102 :
103 : /**
104 : * @ingroup ProcessModelShapeConfigMsg
105 : * @brief it is used to process the shape config of ge model.
106 : * @param [in] cfg : the shape config of model.
107 : */
108 : StatusCode ProcessModelShapeConfigMsg(const AicpuModelShapeConfig& cfg);
109 :
110 5125 : ~AicpuModelManager() = default;
111 :
112 : // not allow copy constructor and assignment operators
113 : AicpuModelManager(const AicpuModelManager&) = delete;
114 :
115 : AicpuModelManager& operator=(const AicpuModelManager&) = delete;
116 :
117 : AicpuModelManager(AicpuModelManager&&) = delete;
118 :
119 : AicpuModelManager&& operator=(AicpuModelManager&&) = delete;
120 :
121 : uint32_t GetExtModelId(const uint32_t modelId);
122 :
123 : bool AbnormalBreak(const uint32_t modelId);
124 :
125 : bool AbnormalEnqueue(const uint32_t modelId);
126 :
127 : bool AbnormalEnabled(const uint32_t modelId);
128 :
129 : StatusCode ProcessModelPriorityMsg(const AicpuPriInfo& cfg, const bool isProcessMode);
130 :
131 : StatusCode SetPidPriority(const AicpuPriInfo& cfg, const std::vector<uint32_t>& deviceVec);
132 :
133 : StatusCode SetEventPriority(const AicpuPriInfo& cfg, const std::vector<uint32_t>& deviceVec);
134 :
135 : StatusCode GetModelMsgQueues(const uint32_t modelId, const bool isInput, int32_t& queueId) const;
136 :
137 : static bool IsUsed();
138 :
139 : private:
140 : AicpuModelManager();
141 : StatusCode CheckModelConfigShape(const uint32_t type, const uint32_t tlvLen, int32_t& unparseLen) const;
142 : StatusCode CheckModelConfigDtype(const TlvHead tlvHeadAddr, int32_t& unparseLen) const;
143 :
144 : AicpuModel allModel_[MAX_MODEL_COUNT];
145 : uint32_t extModelIds_[MAX_MODEL_COUNT]{};
146 : int32_t abnormalBreaks_[MAX_MODEL_COUNT]{};
147 : int32_t abnormalEnqueues_[MAX_MODEL_COUNT]{};
148 : int32_t abnormalFlags_[MAX_MODEL_COUNT]{};
149 : std::unordered_map<uint32_t, std::vector<ModelConfigTensorDesc>> tensorDescMap_;
150 : std::unordered_map<uint32_t, std::pair<int32_t, int32_t>> msgQMap_;
151 : int32_t curPidPri_ = INVALID_ESCAPE_PRI_VALUE;
152 : int32_t curEventPri_ = INVALID_ESCAPE_PRI_VALUE;
153 : std::mutex mutexForSetPidPri_;
154 : std::mutex mutexForSetEvnPri_;
155 : static bool isUsed_;
156 : };
157 : } // namespace AicpuSchedule
158 : #endif // MAIN_AICPUSD_MODEL_EXECUTE_H
|