846d0930b659931673ec8bb96880240b9687db3f
[platform/core/ml/nnfw.git] / runtime / onert / core / src / exec / ExecTime.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_EXEC_TIME_H__
18 #define __ONERT_EXEC_EXEC_TIME_H__
19
20 #include "backend/Backend.h"
21 #include "backend/IConfig.h"
22 #include "JSONExecTime.h"
23 #include <memory>
24 #include <limits>
25 #include <map>
26 #include <unordered_map>
27 #include <vector>
28
29 namespace onert
30 {
31 namespace exec
32 {
33 class ExecTime
34 {
35 public:
36   explicit ExecTime(const std::vector<const backend::Backend *> &backends)
37       : _json(backends, _measurements)
38   {
39   }
40
41 public:
42   /**
43    * @brief Get exec time of an operation with input size
44    *        or linearly interpolated value based on size if there is no record for given size
45    *
46    * @param[in] backend id of a backend
47    * @param[in] operation name of an operation
48    * @param[in] quant if input type quantized
49    * @param[in] op_size sum of operation's flattened sizes of inputs and outputs
50    * @return execution time for given input sizes
51    *         -1 if there are no records for given parameters (backend, op, quantization).
52    */
53   int64_t getOperationExecTime(const backend::Backend *backend, const std::string &operation,
54                                bool quant, uint32_t op_size) const;
55   /**
56    * @brief Update exec time of the operation on a backend with given input size or
57    *        add new entity if there is no one.
58    *
59    * @param[in] backend id of a backend
60    * @param[in] operation name of an operation
61    * @param[in] quant if input type quantized
62    * @param[in] op_size sum of operation's flattened sizes of inputs and outputs
63    * @param[in] time real measured value
64    */
65   void updateOperationExecTime(const backend::Backend *backend, const std::string &operation,
66                                bool quant, uint32_t op_size, int64_t time);
67   /**
68    * @brief Get the permute time from one backend to another
69    *
70    * @param[in] from_backend
71    * @param[in] to_backend
72    * @param[in] quant if input type quantized
73    * @param[in] op_size sum of operation's flattened sizes of inputs and outputs
74    * @return permutation time for operation size
75    */
76   int64_t getPermuteTime(const backend::Backend *from_backend, const backend::Backend *to_backend,
77                          bool quant, uint32_t op_size) const;
78   /**
79    * @brief Update permute time from one backend to another
80    *
81    * @param[in] from_backend
82    * @param[in] to_backend
83    * @param[in] quant if input type quantized
84    * @param[in] time measured permutation time
85    * @param[in] op_size sum of operation's flattened sizes of inputs and outputs
86    */
87   void updatePermuteTime(const backend::Backend *from_backend, const backend::Backend *to_backend,
88                          bool quant, uint32_t op_size, int64_t time);
89   /**
90    * @brief Get the max value of int32_t in int64_t
91    * @return max value
92    */
93   static int64_t getMax() { return _MAX; }
94   /**
95    * @brief Update metrics file with new data.
96    */
97   void uploadOperationsExecTime() const { _json.uploadOperationsExecTime(); }
98   static const int64_t NOT_FOUND = -1;
99
100 private:
101   /// @brief Measurement data, which is shared with serializer
102   MeasurementData _measurements;
103   // int64_t::max may cause integer overflow
104   static const int64_t _MAX = std::numeric_limits<int32_t>::max();
105   /// @brief Serializer
106   JSON _json;
107 };
108
109 } // namespace exec
110 } // namespace onert
111
112 #endif // __ONERT_EXEC_EXEC_TIME_H__