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(const void * const ptr,
86 : AicpuModelInfo &aicpuModelInfo,
87 : std::vector<AicpuTaskInfo> &aicpuTaskInfos,
88 : std::vector<StreamInfo> &streamInfos,
89 : std::vector<QueInfo> &queInfos,
90 : std::vector<ModelCfgInfo> *const modelcfgs = nullptr);
91 : /**
92 : * @ingroup ProcessExtInfoCfgMsg
93 : * @brief it is used to process the config of ge model id or other extension info.
94 : * @param [in] cfgInfo : the config info of extension info.
95 : */
96 : StatusCode ProcessExtInfoCfgMsg(const aicpu::AicpuExtendInfo &cfgInfo);
97 :
98 : /**
99 : * @ingroup ProcessModelConfigMsg
100 : * @brief it is used to process the config of ge model.
101 : * @param [in] cfg : the config of model.
102 : */
103 : StatusCode ProcessModelConfigMsg(const AicpuModelConfig &cfg);
104 :
105 : /**
106 : * @ingroup ProcessModelShapeConfigMsg
107 : * @brief it is used to process the shape config of ge model.
108 : * @param [in] cfg : the shape config of model.
109 : */
110 : StatusCode ProcessModelShapeConfigMsg(const AicpuModelShapeConfig &cfg);
111 :
112 5125 : ~AicpuModelManager() = default;
113 :
114 : // not allow copy constructor and assignment operators
115 : AicpuModelManager(const AicpuModelManager &) = delete;
116 :
117 : AicpuModelManager &operator=(const AicpuModelManager &) = delete;
118 :
119 : AicpuModelManager(AicpuModelManager &&) = delete;
120 :
121 : AicpuModelManager &&operator=(AicpuModelManager &&) = delete;
122 :
123 : uint32_t GetExtModelId(const uint32_t modelId);
124 :
125 : bool AbnormalBreak(const uint32_t modelId);
126 :
127 : bool AbnormalEnqueue(const uint32_t modelId);
128 :
129 : bool AbnormalEnabled(const uint32_t modelId);
130 :
131 : StatusCode ProcessModelPriorityMsg(const AicpuPriInfo &cfg, const bool isProcessMode);
132 :
133 : StatusCode SetPidPriority(const AicpuPriInfo &cfg, const std::vector<uint32_t> &deviceVec);
134 :
135 : StatusCode SetEventPriority(const AicpuPriInfo &cfg, const std::vector<uint32_t> &deviceVec);
136 :
137 : StatusCode GetModelMsgQueues(const uint32_t modelId, const bool isInput, int32_t &queueId) const;
138 :
139 : static bool IsUsed();
140 :
141 : private:
142 : AicpuModelManager();
143 : StatusCode CheckModelConfigShape(const uint32_t type, const uint32_t tlvLen, int32_t &unparseLen) const;
144 : StatusCode CheckModelConfigDtype(const TlvHead tlvHeadAddr, int32_t &unparseLen) const;
145 :
146 : AicpuModel allModel_[MAX_MODEL_COUNT];
147 : uint32_t extModelIds_[MAX_MODEL_COUNT]{};
148 : int32_t abnormalBreaks_[MAX_MODEL_COUNT]{};
149 : int32_t abnormalEnqueues_[MAX_MODEL_COUNT]{};
150 : int32_t abnormalFlags_[MAX_MODEL_COUNT]{};
151 : std::unordered_map<uint32_t, std::vector<ModelConfigTensorDesc>> tensorDescMap_;
152 : std::unordered_map<uint32_t, std::pair<int32_t, int32_t>> msgQMap_;
153 : int32_t curPidPri_ = INVALID_ESCAPE_PRI_VALUE;
154 : int32_t curEventPri_ = INVALID_ESCAPE_PRI_VALUE;
155 : std::mutex mutexForSetPidPri_;
156 : std::mutex mutexForSetEvnPri_;
157 : static bool isUsed_;
158 : };
159 : }
160 : #endif // MAIN_AICPUSD_MODEL_EXECUTE_H
|