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 : #ifndef AICPUSD_DUMP_TASK_H
11 : #define AICPUSD_DUMP_TASK_H
12 :
13 : #include <cmath>
14 : #include <map>
15 : #include <memory>
16 : #include <mutex>
17 : #include <set>
18 : #include <sstream>
19 : #include <string>
20 : #include <vector>
21 : #include "Eigen/Dense"
22 : #include "aicpusd_status.h"
23 : #include "dump_data.pb.h"
24 : #include "op_mapping_info.pb.h"
25 : #include "dump/adump_device_pub.h"
26 : #include "aicpusd_common.h"
27 : #include "type_def.h"
28 : #include "datadump_kfc_interface.h"
29 : #include "aicpusd_sqe_adapter.h"
30 :
31 : #define DATADUMP_MAKE_SHARED(exec_expr0, exec_expr1) \
32 : try { \
33 : exec_expr0; \
34 : } catch (const std::bad_alloc &err) { \
35 : aicpusd_err("bad alloc for object, reason is [%s]", err.what()); \
36 : exec_expr1; \
37 : } catch (const std::exception &err) { \
38 : aicpusd_err("make shared failed for object failed, reason is [%s]", err.what()); \
39 : exec_expr1; \
40 : } catch (...) { \
41 : aicpusd_err("make shared failed. reason is [%s]", strerror(errno)); \
42 : exec_expr1; \
43 : }
44 :
45 : #ifdef __cplusplus
46 : extern "C" {
47 : #endif
48 : __attribute__((weak)) bool AdumpStatsOpInitStatus();
49 : #ifdef __cplusplus
50 : }
51 : #endif
52 :
53 : namespace AicpuSchedule {
54 : constexpr uint32_t INVALID_VAL = 65535U;
55 : constexpr uint8_t STARS_DATADUMP_LOAD_INFO = 8;
56 : // datadump for kfc
57 : using AicpuKfcDumpFuncPtr = uint32_t(*)(void *);
58 :
59 : using DumpMode = ::aicpu::dump::DumpData;
60 :
61 119 : static inline void ReplaceStringElem(std::string &str)
62 : {
63 119 : (void)for_each(str.begin(), str.end(),
64 1342 : [](char_t &ch) {
65 1342 : if ((ch == ' ') ||
66 1236 : (ch == '.') ||
67 1183 : (ch == '/') ||
68 1342 : (ch == '\\')) { ch = '_'; }
69 1342 : });
70 119 : }
71 : struct MappingInfoOptionalParam {
72 200 : MappingInfoOptionalParam() : hasModelName(false),
73 200 : hasModelId(false),
74 200 : modelId(0U),
75 200 : hasStepId(false),
76 200 : stepIdAddr(nullptr),
77 200 : hasIterationsPerLoop(false),
78 200 : iterationsPerLoopAddr(nullptr),
79 200 : hasLoopCond(false),
80 200 : loopCondAddr(nullptr),
81 200 : hasDumpSwitch(false),
82 200 : dumpSwitchAddr(nullptr) {}
83 :
84 : bool hasModelName;
85 : std::string modelName;
86 : bool hasModelId;
87 : uint32_t modelId;
88 : bool hasStepId;
89 : uint64_t *stepIdAddr;
90 : bool hasIterationsPerLoop;
91 : uint64_t *iterationsPerLoopAddr;
92 : bool hasLoopCond;
93 : uint64_t *loopCondAddr;
94 : bool hasDumpSwitch;
95 : uint64_t *dumpSwitchAddr;
96 : };
97 :
98 : struct IntervalStep {
99 : uint64_t start;
100 : uint64_t end;
101 : };
102 :
103 : struct DumpStep {
104 : std::set<uint64_t> singleStep;
105 : std::vector<IntervalStep> intervalStep;
106 : std::string DebugString() const;
107 : };
108 :
109 : struct TaskInfo {
110 131 : TaskInfo() : streamId_(0U),
111 131 : taskId_(0U),
112 131 : contextId_(INVALID_VAL),
113 131 : threadId_(INVALID_VAL) {};
114 :
115 153 : TaskInfo(const uint32_t streamId,
116 : const uint32_t taskId,
117 : const uint32_t contextId = INVALID_VAL,
118 153 : const uint32_t threadId = INVALID_VAL) : streamId_(streamId), taskId_(taskId),
119 153 : contextId_(contextId),
120 153 : threadId_(threadId) {};
121 :
122 : uint32_t streamId_;
123 : uint32_t taskId_;
124 : uint32_t contextId_ = INVALID_VAL;
125 : uint32_t threadId_ = INVALID_VAL;
126 : friend bool operator < (const TaskInfo &item1, const TaskInfo &item2);
127 : };
128 :
129 : struct TaskInfoExt {
130 21 : TaskInfoExt() : streamId_(INVALID_VAL),
131 21 : taskId_(INVALID_VAL),
132 21 : contextId_(INVALID_VAL),
133 21 : threadId_(INVALID_VAL),
134 21 : indexId_(INVALID_VAL) {};
135 :
136 50 : TaskInfoExt(const uint32_t streamId,
137 : const uint32_t taskId,
138 : const uint32_t contextId = INVALID_VAL,
139 : const uint32_t threadId = INVALID_VAL,
140 50 : const uint32_t indexId = INVALID_VAL) : streamId_(streamId), taskId_(taskId),
141 50 : contextId_(contextId),
142 50 : threadId_(threadId),
143 50 : indexId_(indexId) {};
144 :
145 : uint32_t streamId_;
146 : uint32_t taskId_;
147 : uint32_t contextId_;
148 : uint32_t threadId_;
149 : uint32_t indexId_;
150 : };
151 :
152 : struct DumpFileName {
153 67 : DumpFileName(const uint32_t streamId,
154 : const uint32_t taskId,
155 : const uint32_t contextId = INVALID_VAL,
156 67 : const uint32_t threadId = INVALID_VAL) :
157 67 : streamId_(streamId), taskId_(taskId), contextId_(contextId), threadId_(threadId) {};
158 : uint32_t streamId_;
159 : uint32_t taskId_;
160 : uint32_t contextId_;
161 : uint32_t threadId_;
162 : };
163 :
164 466 : inline bool operator < (const TaskInfo &item1, const TaskInfo &item2)
165 : {
166 466 : if (item1.streamId_ != item2.streamId_) {
167 197 : return item1.streamId_ < item2.streamId_;
168 : }
169 269 : if (item1.taskId_ != item2.taskId_) {
170 89 : return item1.taskId_ < item2.taskId_;
171 : }
172 180 : if (item1.contextId_ != item2.contextId_) {
173 12 : return item1.contextId_ < item2.contextId_;
174 : }
175 168 : return item1.threadId_ < item2.threadId_;
176 : }
177 :
178 : class OpDumpTask {
179 : public:
180 : explicit OpDumpTask(const int32_t hostPid, const uint32_t deviceId);
181 131 : ~OpDumpTask() = default;
182 :
183 : /**
184 : * Preprocess op mapping info.
185 : * @param task task info from op mapping info
186 : * @param basePath base dump path
187 : * @param param optional param
188 : * @param dumpStep step need dump
189 : * @param skipAddressConversion indicates whether to skip address conversion based on addrtype
190 : * @return whather preprocess success
191 : */
192 : StatusCode PreProcessOpMappingInfo(const aicpu::dump::Task &task,
193 : const std::string &basePath,
194 : const MappingInfoOptionalParam ¶m,
195 : const DumpStep &dumpStep,
196 : const DumpMode dumpMode,
197 : const bool skipAddressConversion = false);
198 : StatusCode UpdatePreProcessFftsPlusInputAndOutput(const aicpu::dump::Context &item);
199 : StatusCode PreProcessUdfOpMappingInfo(uint8_t *dumpInfo, uint64_t length);
200 : /**
201 : * Deal with dump info event.
202 : * @return whather dump success
203 : */
204 : StatusCode DumpOpInfo(const TaskInfoExt &dumpTaskInfo, const DumpFileName &dumpFileName);
205 : StatusCode DumpOpInfo(const uint32_t streamId = INVALID_VAL, const uint32_t taskId = INVALID_VAL);
206 :
207 : /**
208 : * Get model id of this task.
209 : * @param modelId model id
210 : * @return whather get model id success
211 : */
212 : bool GetModelId(uint32_t &modelId) const;
213 :
214 : /**
215 : * Check this task is end graph task or not.
216 : * @return whather is end graph task
217 : */
218 : bool IsEndGraph() const;
219 :
220 : /**
221 : * Update dump number.
222 : * @return void
223 : */
224 : void UpdateDumpNum();
225 :
226 : /**
227 : * Get op name.
228 : * @return op name
229 : */
230 : std::string GetOpName() const;
231 :
232 : /**
233 : * Clear baseDumpData_.
234 : * @return void
235 : */
236 : void ClearBaseDumpData();
237 : bool IsSupportKfcDump();
238 5 : const std::string &GetDumpPath()
239 : {
240 5 : return dumpPath_;
241 : }
242 5 : const uint32_t GetDeviceId()
243 : {
244 5 : return deviceId_;
245 : }
246 5 : const int32_t GetHostPid()
247 : {
248 5 : return hostPid_;
249 : }
250 93 : const std::string &GetOpName()
251 : {
252 93 : return opName_;
253 : }
254 : void GetKfcDumpInfo(std::shared_ptr<KfcDumpInfo> dumpInfo);
255 : StatusCode Dump(const std::string &path,
256 : char_t * const data,
257 : const uint64_t len,
258 : IDE_SESSION &ideSession,
259 : const bool isLastSlice) const;
260 : private:
261 : /**
262 : * Get dump number
263 : * @param dumpNum task dump number
264 : * @return whather get dump param success
265 : */
266 : StatusCode GetDumpNumber(uint64_t &dumpNum);
267 :
268 : /**
269 : * This step need dump or not
270 : * @param step task dump number
271 : * @return whather need dump
272 : */
273 : bool NeedDump(const uint64_t step) const;
274 :
275 : StatusCode PreProcessOutput(const aicpu::dump::Task &task,
276 : ::toolkit::dumpdata::DumpData &dumpData);
277 :
278 : StatusCode PreProcessInput(const aicpu::dump::Task &task,
279 : ::toolkit::dumpdata::DumpData &dumpData);
280 :
281 : StatusCode PreProcessOpBuffer(const aicpu::dump::Task &task,
282 : ::toolkit::dumpdata::DumpData &dumpData);
283 : StatusCode PreProcessWorkspace(const aicpu::dump::Task &task,
284 : ::toolkit::dumpdata::DumpData &dumpData);
285 : StatusCode ProcessInputDump(const ::toolkit::dumpdata::DumpData &dumpData,
286 : const std::string &path,
287 : IDE_SESSION &ideSession);
288 : StatusCode ProcessOutputDump(const ::toolkit::dumpdata::DumpData &dumpData,
289 : const std::string &path,
290 : IDE_SESSION &ideSession);
291 : StatusCode ProcessOpBufferDump(const ::toolkit::dumpdata::DumpData &dumpData,
292 : const std::string &path,
293 : IDE_SESSION &ideSession);
294 : StatusCode ProcessOpWorkspaceDump(const ::toolkit::dumpdata::DumpData &dumpData,
295 : const std::string &path,
296 : IDE_SESSION &ideSession);
297 :
298 : std::string DumpPath(const uint64_t nowTime, const uint64_t dumpNumber,
299 : const DumpFileName &dumpFileName,
300 : const bool debugFlag = false);
301 :
302 : StatusCode DoDumpTensor(const std::string &dumpFilePath);
303 :
304 : StatusCode ProcessngNoTiliInput();
305 :
306 : StatusCode ProcessngNoTiliOutput();
307 :
308 : void UpdateDumpData();
309 : void UpdateUdfDumpDataTotalSize();
310 : /**
311 : * Get Input DataAddr
312 : * @param i index
313 : * @return void
314 : */
315 : void GetInputDataAddr(uint64_t &dataAddr, const int32_t i);
316 :
317 : /**
318 : * Get Output DataAddr
319 : * @param i index
320 : * @return void
321 : */
322 : void GetOutputDataAddr(uint64_t &dataAddr, const int32_t i);
323 :
324 : StatusCode ProcessDumpOpInfo(const TaskInfoExt &dumpTaskInfo, const std::string &dumpFilePath);
325 : StatusCode ProcessDumpTensor(const std::string &dumpFilePath);
326 : StatusCode ProcessDumpStats(const std::string &dumpFilePath);
327 : StatusCode ProcessDumpStatistic(const TaskInfoExt &dumpTaskInfo, const std::string &dumpFilePath);
328 : StatusCode DoDumpStats(const std::string &dumpFilePath, const std::string &content);
329 : std::string GenerateDataStatsInfo(uint64_t dataAddr, uint64_t dataSize,
330 : ::toolkit::dumpdata::OutputDataType dataType) const;
331 : std::string GenerateDataDimInfo(::toolkit::dumpdata::Shape dataShape) const;
332 : std::string GetDataFormatStr(::toolkit::dumpdata::OutputFormat dataFormat) const;
333 : std::string GetDataTypeStr(::toolkit::dumpdata::OutputDataType dataType) const;
334 : StatusCode ProcessKfcDumpStats(KfcDumpTask &taskInfo, const std::string &dumpFilePath);
335 : bool CheckAndGetKfcDumpStatsAPI();
336 : std::string ShapeDebugString(std::vector<uint64_t> shapeInfo) const;
337 :
338 : std::mutex dumpMtx_;
339 : ::toolkit::dumpdata::DumpData baseDumpData_;
340 : std::string baseDumpPath_;
341 : std::string dumpPath_;
342 : std::string opName_;
343 : std::string opType_;
344 : MappingInfoOptionalParam optionalParam_;
345 : uint64_t taskDumpNum_;
346 : TaskInfo taskInfo_;
347 : ::aicpu::dump::Task::TaskType taskType_;
348 : DumpStep dumpStep_;
349 : std::vector<uint64_t> inputsSize_;
350 : std::vector<uint64_t> outputSize_;
351 : std::vector<uint64_t> inputsOffset_;
352 : std::vector<uint64_t> outputOffset_;
353 : bool endGraph_;
354 : std::vector<uint64_t> inputsBaseAddr_;
355 : std::vector<int32_t> inputsDataType_;
356 : std::vector<int32_t> inputsFormat_;
357 : std::vector<std::vector<uint64_t>> inputsShape_;
358 : std::vector<std::vector<uint64_t>> inputsOriginShape_;
359 : std::vector<uint64_t> outputsBaseAddr_;
360 : std::vector<int32_t> outputsDataType_;
361 : std::vector<int32_t> outputsFormat_;
362 : std::vector<std::vector<uint64_t>> outputsShape_;
363 : std::vector<std::vector<uint64_t>> outputsOriginShape_;
364 : std::vector<int32_t> inputsAddrType_;
365 : std::vector<int32_t> outputsAddrType_;
366 : std::vector<uint64_t> opBufferAddr_;
367 : std::vector<uint64_t> opWorkspaceAddr_;
368 : std::vector<uint64_t> opWorkspaceSize_;
369 : uint64_t inputTotalSize_;
370 : uint64_t outputTotalSize_;
371 : uint64_t opBufferTotalSize_;
372 : uint64_t opWorkspaceTotalSize_;
373 :
374 : std::unique_ptr<char_t[]> buff_;
375 : uint64_t buffSize_;
376 : uint64_t offset_;
377 : bool skipAddressConversion_;
378 : int32_t hostPid_;
379 : uint32_t deviceId_;
380 : DumpMode dumpMode_;
381 : AicpuKfcDumpFuncPtr kfcDumpFunc_ = nullptr;
382 : }; // class OpDumpTask
383 :
384 : class OpDumpTaskManager {
385 : public:
386 : static OpDumpTaskManager &GetInstance();
387 7 : OpDumpTaskManager() = default;
388 7 : ~OpDumpTaskManager() = default;
389 :
390 : /**
391 : * Load op mapping info
392 : * @param infoAddr info address pointer
393 : * @param len info length
394 : * @return whather load success
395 : */
396 : int32_t LoadOpMappingInfo(const char_t * const infoAddr, const uint32_t len, AicpuSqeAdapter &aicpuSqeAdapter);
397 :
398 : int32_t LoadOpMappingInfo(const char_t * const infoAddr, const uint32_t len);
399 :
400 : /**
401 : * Deal with dump info event for know shape.
402 : * @param dumpTaskInfo Dump Task Info
403 : * @param streamId Stream id
404 : * @param taskId Task id
405 : * @return whather dump success
406 : */
407 : int32_t DumpOpInfo(TaskInfoExt &dumpTaskInfo,
408 : const DumpFileName &dumpFileName);
409 : int32_t DumpOpInfo(const uint32_t streamId, const uint32_t taskId,
410 : const uint32_t streamId1 = INVALID_VAL, const uint32_t taskId1 = INVALID_VAL);
411 : int32_t DumpOpInfo(TaskInfoExt &dumpTaskInfo,
412 : const uint32_t streamId, const uint32_t taskId,
413 : const uint32_t contextId, const uint32_t threadId);
414 : /**
415 : * Deal with dump info event for unknow shape.
416 : * @param opMappingInfoAddr op mapping info addr
417 : * @param opMappingInfoLen op mapping info length
418 : * @return whather dump success
419 : */
420 : int32_t DumpOpInfoForUnknowShape(const uint64_t opMappingInfoAddr, const uint64_t opMappingInfoLen) const;
421 :
422 : /**
423 : * clear all resource od data dump for ctrl cpu and minirc
424 : * @return void
425 : */
426 : void ClearResource();
427 :
428 : int32_t DoDump(const aicpu::dump::OpMappingInfo &opMappingInfo, const MappingInfoOptionalParam &optionalParam) const;
429 : void MakeDumpOpInfoforKfc(const KfcDumpTask &taskinfo, std::shared_ptr<OpDumpTask> dumpTask);
430 : int32_t GetDumpOpTaskDataforKfc(const KfcDumpTask &taskKey, KfcDumpInfo **dumpInfo);
431 : int32_t DumpOpTaskDataforKfc(const KfcDumpTask &taskKey, void *dumpData, uint32_t length) const;
432 : bool IsCustDumpTask(const uint32_t streamId, const uint32_t taskId);
433 : int32_t SetCustDumpTaskFlag(const uint32_t streamId, const uint32_t taskId, const bool flag);
434 : void ClearKfcDumpTaskInfo(const KfcDumpTask &kfcTaskinfo);
435 : int32_t DoDumpBySwitchBitmap(const aicpu::dump::OpMappingInfo &opMappingInfo, const MappingInfoOptionalParam &optionalParam, const uint64_t switchBitMap) const;
436 :
437 : private:
438 : OpDumpTaskManager(const OpDumpTaskManager &) = delete;
439 : OpDumpTaskManager &operator=(const OpDumpTaskManager &) = delete;
440 : OpDumpTaskManager(OpDumpTaskManager&&) = delete;
441 : OpDumpTaskManager& operator=(OpDumpTaskManager&&) = delete;
442 :
443 : /**
444 : * Get optional param from op mapping info proto
445 : * @param opMappingInfo op mapping info
446 : * @param optionalParam optional param
447 : * @return void
448 : */
449 : void GetOptionalParam(const aicpu::dump::OpMappingInfo &opMappingInfo,
450 : MappingInfoOptionalParam &optionalParam) const;
451 :
452 : /**
453 : * Update all task dump number of according model id
454 : * @param modelId model id
455 : * @return void
456 : */
457 : void UpdateDumpNumByModelId(const uint32_t modelId);
458 :
459 : /**
460 : * Porcess end graph task if it exist in opDumptasks
461 : * @param opDumptasks tasks
462 : * @return void
463 : */
464 : void ProcessEndGraph(const std::vector<std::shared_ptr<OpDumpTask>> &opDumptasks);
465 :
466 : /**
467 : * Parse dump step from string, like 0|1-20
468 : * @param str dump step string
469 : * @param dumpStep dump step of parse result
470 : * @return whather parse success
471 : */
472 : bool GetDumpStepFromString(const std::string &str, DumpStep &dumpStep) const;
473 :
474 : /**
475 : * Parse dump step from step string
476 : * @param step step string
477 : * @param tmpDumpStep dump step of parse result
478 : * @return whather parse success
479 : */
480 : bool MatchAndInsert(const std::string &step, DumpStep &tmpDumpStep) const;
481 :
482 : /**
483 : * load mapping info
484 : * @param opMappingInfo op mapping info proto
485 : * @return whather load success
486 : */
487 : int32_t Load(const aicpu::dump::OpMappingInfo &opMappingInfo, AicpuSqeAdapter &aicpuSqeAdapter);
488 :
489 : /**
490 : * unload mapping info
491 : * @param opMappingInfo op mapping info proto
492 : * @return whather unload success
493 : */
494 : int32_t Unload(const aicpu::dump::OpMappingInfo &opMappingInfo, AicpuSqeAdapter &aicpuSqeAdapter);
495 :
496 : /**
497 : * clear baseDumpData
498 : * @param TaskInfo taskInfo
499 : * @return void
500 : */
501 : void UnloadClearTaskInfo(const TaskInfo &dumpTaskInfo);
502 : /**
503 : * create OpDumpTask
504 : * @param opDumpTaskPtr hostPid deviceId
505 : * @return create success
506 : */
507 : int32_t CreateOpDumpTask(std::shared_ptr<OpDumpTask> &opDumpTaskPtr,
508 : const int32_t hostPid, const uint32_t deviceId) const;
509 : /**
510 : * create KfcDumpInfo
511 : * @param kfcDumpInfoPtr
512 : * @return create success
513 : */
514 : int32_t CreateKfcDumpInfo(std::shared_ptr<KfcDumpInfo> &kfcDumpInfoPtr) const;
515 : bool EnsureDeviceOpened(const uint32_t deviceId) const;
516 : int32_t GetAndClearOverflowStatus(const uint32_t deviceId, const uint32_t streamId, const uint32_t opType, uint32_t *status) const;
517 : private:
518 : std::multimap<TaskInfo, std::shared_ptr<OpDumpTask>> dumpTaskMap_;
519 : std::mutex dumpTaskMapMtx_;
520 : std::mutex kfcDumpTaskMapMtx_;
521 : std::map<uint32_t, std::set<TaskInfo>> modelIdToTask_;
522 : std::map<KfcDumpTask, std::shared_ptr<OpDumpTask>> kfcDumpTaskMap_;
523 : std::map<KfcDumpTask, std::shared_ptr<KfcDumpInfo>> kfcDumpInfoMap_;
524 : std::map<TaskInfo, bool> custDumpTaskMap_;
525 : };
526 :
527 : template <typename T>
528 : class DataStats {
529 : public:
530 119 : DataStats(uint64_t dataAddr, uint64_t dataSize) : maxValue_(0),
531 119 : minValue_(0),
532 119 : avgValue_(0.0),
533 119 : count_(0UL),
534 119 : nanCount_(0UL),
535 119 : negInfCount_(0UL),
536 119 : posInfCount_(0UL),
537 119 : data_(nullptr),
538 119 : dataSize_(dataSize)
539 : {
540 119 : data_ = PtrToPtr<void, T>(ValueToPtr(dataAddr));
541 119 : count_ = dataSize_ / sizeof(T);
542 119 : };
543 :
544 : virtual ~DataStats() = 0;
545 :
546 116 : virtual inline std::string GetDataStatsStr()
547 : {
548 116 : this->Stats();
549 116 : std::ostringstream oss;
550 326 : oss << maxValue_ << "," << minValue_ << "," << avgValue_ << "," << count_ << "," << nanCount_ << ","
551 116 : << negInfCount_ << "," << posInfCount_;
552 232 : return oss.str();
553 116 : };
554 :
555 : protected:
556 0 : virtual inline bool IsNan(T ele) const
557 : {
558 : (void)ele;
559 0 : return true;
560 : }
561 :
562 0 : virtual inline bool IsInf(T ele) const
563 : {
564 : (void)ele;
565 0 : return true;
566 : }
567 :
568 5230 : virtual inline void UpdateAvgValue(T ele, uint64_t i)
569 : {
570 5230 : avgValue_ += (static_cast<double>(ele) - avgValue_) / static_cast<double>(i + 1);
571 5230 : }
572 :
573 119 : virtual inline void AdjustAvgValue()
574 : {
575 119 : if ((negInfCount_ > 0UL) && (posInfCount_ == 0UL)) {
576 0 : avgValue_ = -INFINITY;
577 119 : } else if ((negInfCount_ == 0UL) && (posInfCount_ > 0UL)) {
578 1 : avgValue_ = INFINITY;
579 : }
580 119 : return;
581 : }
582 :
583 : void Stats();
584 :
585 : T maxValue_;
586 : T minValue_;
587 : double avgValue_;
588 : uint64_t count_;
589 : uint64_t nanCount_;
590 : uint64_t negInfCount_;
591 : uint64_t posInfCount_;
592 :
593 : private:
594 119 : inline void ResetStats()
595 : {
596 119 : avgValue_ = static_cast<double>(0.0);
597 119 : nanCount_ = 0UL;
598 119 : negInfCount_ = 0UL;
599 119 : posInfCount_ = 0UL;
600 119 : return;
601 : }
602 :
603 : DataStats(DataStats const&) = delete;
604 : DataStats& operator=(DataStats const&) = delete;
605 : DataStats(DataStats&&) = delete;
606 : DataStats& operator=(DataStats&&) = delete;
607 :
608 : T *data_;
609 : uint64_t dataSize_;
610 : };
611 :
612 : template <typename T>
613 : class NormalDataStats : public DataStats<T> {
614 : public:
615 14 : NormalDataStats(uint64_t dataAddr, uint64_t dataSize) : DataStats<T>(dataAddr, dataSize) {}
616 14 : ~NormalDataStats() = default;
617 : protected:
618 140 : inline bool IsNan(T ele) const
619 : {
620 140 : return std::isnan(ele);
621 : }
622 :
623 140 : inline bool IsInf(T ele) const
624 : {
625 140 : return std::isinf(ele);
626 : }
627 : private:
628 : NormalDataStats(NormalDataStats const&) = delete;
629 : NormalDataStats& operator=(NormalDataStats const&) = delete;
630 : NormalDataStats(NormalDataStats&&) = delete;
631 : NormalDataStats& operator=(NormalDataStats&&) = delete;
632 : };
633 :
634 : class Uint8DataStats : public NormalDataStats<uint8_t> {
635 : public:
636 2 : Uint8DataStats(uint64_t dataAddr, uint64_t dataSize) : NormalDataStats<uint8_t>(dataAddr, dataSize) {}
637 2 : ~Uint8DataStats() = default;
638 :
639 2 : inline std::string GetDataStatsStr()
640 : {
641 2 : Stats();
642 2 : std::ostringstream oss;
643 2 : oss << static_cast<uint32_t>(maxValue_) << "," << static_cast<uint32_t>(minValue_) << "," << avgValue_ << ","
644 2 : << count_ << "," << nanCount_ << "," << negInfCount_ << "," << posInfCount_;
645 4 : return oss.str();
646 2 : };
647 : private:
648 : Uint8DataStats(Uint8DataStats const&) = delete;
649 : Uint8DataStats& operator=(Uint8DataStats const&) = delete;
650 : Uint8DataStats(Uint8DataStats&&) = delete;
651 : Uint8DataStats& operator=(Uint8DataStats&&) = delete;
652 : };
653 :
654 : class Int8DataStats : public NormalDataStats<int8_t> {
655 : public:
656 1 : Int8DataStats(uint64_t dataAddr, uint64_t dataSize) : NormalDataStats<int8_t>(dataAddr, dataSize) {}
657 1 : ~Int8DataStats() = default;
658 :
659 1 : inline std::string GetDataStatsStr()
660 : {
661 1 : Stats();
662 1 : std::ostringstream oss;
663 1 : oss << static_cast<int32_t>(maxValue_) << "," << static_cast<int32_t>(minValue_) << "," << avgValue_ << ","
664 1 : << count_ << "," << nanCount_ << "," << negInfCount_ << "," << posInfCount_;
665 2 : return oss.str();
666 1 : };
667 : private:
668 : Int8DataStats(Int8DataStats const&) = delete;
669 : Int8DataStats& operator=(Int8DataStats const&) = delete;
670 : Int8DataStats(Int8DataStats&&) = delete;
671 : Int8DataStats& operator=(Int8DataStats&&) = delete;
672 : };
673 :
674 : class EigenDataStats : public DataStats<Eigen::half> {
675 : public:
676 105 : EigenDataStats(uint64_t dataAddr, uint64_t dataSize) : DataStats<Eigen::half>(dataAddr, dataSize) {}
677 105 : ~EigenDataStats() = default;
678 :
679 : protected:
680 5090 : inline bool IsNan(Eigen::half ele) const
681 : {
682 5090 : return Eigen::numext::isnan(ele);
683 : }
684 :
685 5090 : inline bool IsInf(Eigen::half ele) const
686 : {
687 5090 : return Eigen::numext::isinf(ele);
688 : }
689 :
690 : private:
691 : EigenDataStats(EigenDataStats const&) = delete;
692 : EigenDataStats& operator=(EigenDataStats const&) = delete;
693 : EigenDataStats(EigenDataStats&&) = delete;
694 : EigenDataStats& operator=(EigenDataStats&&) = delete;
695 : };
696 :
697 : class DumpSessionManager {
698 : public:
699 : static DumpSessionManager &GetInstance();
700 4 : DumpSessionManager() = default;
701 4 : ~DumpSessionManager() = default;
702 :
703 : IDE_SESSION GetSession(int32_t hostPid, uint32_t deviceId);
704 : IDE_SESSION ReacquireSession(int32_t hostPid, uint32_t deviceId);
705 : void CloseAllSessions();
706 : IDE_SESSION CreateIdeDumpSession(int32_t hostPid, uint32_t deviceId) const;
707 :
708 : private:
709 : std::unordered_map<uint64_t, IDE_SESSION> sessionsMap_;
710 : std::mutex mutex_;
711 : };
712 : } // namespace aicpu
713 :
714 : #endif
|