a64cb3133e114e9fcf4a671ee1d6e2fa0b7a6585
[platform/core/ml/nnfw.git] / runtime / onert / core / src / exec / JSONExecTime.h
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef __ONERT_EXEC_JSON_EXEC_TIME_H__
18 #define __ONERT_EXEC_JSON_EXEC_TIME_H__
19
20 #include <fstream>
21 #include <unordered_map>
22 #include <map>
23 #include <vector>
24 #include "backend/Backend.h"
25 #include "backend/IConfig.h"
26
27 namespace onert
28 {
29 namespace exec
30 {
31
32 /**
33  * @brief table, that contains execution time of an operation on some backend for different input
34  * sizes and transfer time from one backend to another for various input sizes (permutation time)
35  *
36  *               backend ->  op ->  quant->  size   --> time
37  * _measurements[Backend*]["string"][bool][uint32_t] = int64_t
38  */
39 using MeasurementData = std::unordered_map<
40     const backend::Backend *,
41     std::unordered_map<std::string, std::unordered_map<bool, std::map<uint32_t, int64_t>>>>;
42
43 class JSON
44 {
45 public:
46   explicit JSON(const std::vector<const backend::Backend *> &backends,
47                 MeasurementData &measurements)
48       : _measurement_file("exec_time.json"), _backends(), _measurements(measurements)
49   {
50     for (const auto b : backends)
51     {
52       _backends.emplace(b->config()->id(), b);
53     }
54     loadOperationsExecTime();
55   };
56   /**
57    * @brief Update _operations_exec_time_file with new data.
58    */
59   void uploadOperationsExecTime() const;
60
61 private:
62   ///@brief file containing measurements
63   std::string _measurement_file;
64   std::unordered_map<std::string, const backend::Backend *> _backends;
65   std::unordered_map<
66       const backend::Backend *,
67       std::unordered_map<std::string, std::unordered_map<bool, std::map<uint32_t, int64_t>>>>
68       &_measurements;
69   /**
70    * @brief Helper function for inserting data to OperationExecTimes
71    *
72    * @param backend String name of backend
73    * @param operation String name of operation
74    * @param quant if input type quantized
75    * @param stream File stream
76    */
77   void readOperation(const std::string &backend, const std::string &operation, bool quant,
78                      std::ifstream &stream);
79
80   /**
81    * @brief Helper function for writing OperationExecTimes to stream
82    *
83    * @param operation_info Map of operations execution information
84    * @param stream File stream
85    */
86   void printOperation(const std::map<uint32_t, int64_t> &operation_info,
87                       std::ofstream &stream) const;
88   /**
89    * @brief Parse and load operations_exec_time from _operations_exec_time_file.
90    */
91   void loadOperationsExecTime();
92 };
93
94 } // namespace exec
95 } // namespace onert
96
97 #endif // __ONERT_EXEC_JSON_EXEC_TIME_H__