summaryrefslogtreecommitdiff
path: root/runtimes/neurun/core/include/backend/ExecTime.h
blob: 4eaf49fab5483ac8cc6715518cf05998bcba9374 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __NEURUN_BACKEND_EXEC_TIME_H__
#define __NEURUN_BACKEND_EXEC_TIME_H__

#include "backend/Backend.h"
#include "backend/IConfig.h"
#include "JSONExecTime.h"
#include <memory>
#include <limits>
#include <map>
#include <unordered_map>
#include <vector>

namespace neurun
{
namespace backend
{
class ExecTime
{
public:
  explicit ExecTime(const std::vector<const Backend *> &backends) : _json(backends, _measurements)
  {
  }

public:
  /**
   * @brief Get exec time of an operation with input size
   *        or linearly interpolated value based on size if there is no record for given size
   *
   * @param[in] backend id of a backend
   * @param[in] operation name of an operation
   * @param[in] quant if input type quantized
   * @param[in] op_size sum of operation's flattened sizes of inputs and outputs
   * @return execution time for given input sizes
   *         -1 if there are no records for given parameters (backend, op, quantization).
   */
  int64_t getOperationExecTime(const Backend *backend, const std::string &operation, bool quant,
                               uint32_t op_size) const;
  /**
   * @brief Update exec time of the operation on a backend with given input size or
   *        add new entity if there is no one.
   *
   * @param[in] backend id of a backend
   * @param[in] operation name of an operation
   * @param[in] quant if input type quantized
   * @param[in] op_size sum of operation's flattened sizes of inputs and outputs
   * @param[in] time real measured value
   */
  void updateOperationExecTime(const Backend *backend, const std::string &operation, bool quant,
                               uint32_t op_size, int64_t time);
  /**
   * @brief Get the permute time from one backend to another
   *
   * @param[in] from_backend
   * @param[in] to_backend
   * @param[in] quant if input type quantized
   * @param[in] op_size sum of operation's flattened sizes of inputs and outputs
   * @return permutation time for operation size
   */
  int64_t getPermuteTime(const Backend *from_backend, const Backend *to_backend, bool quant,
                         uint32_t op_size) const;
  /**
   * @brief Update permute time from one backend to another
   *
   * @param[in] from_backend
   * @param[in] to_backend
   * @param[in] quant if input type quantized
   * @param[in] time measured permutation time
   * @param[in] op_size sum of operation's flattened sizes of inputs and outputs
   */
  void updatePermuteTime(const Backend *from_backend, const Backend *to_backend, bool quant,
                         uint32_t op_size, int64_t time);
  /**
   * @brief Get the max value of int32_t in int64_t
   * @return max value
   */
  static int64_t getMax() { return _MAX; }
  /**
   * @brief Update metrics file with new data.
   */
  void uploadOperationsExecTime() const { _json.uploadOperationsExecTime(); }
  static const int64_t NOT_FOUND = -1;

private:
  /// @brief Measurement data, which is shared with serializer
  MeasurementData _measurements;
  // int64_t::max may cause integer overflow
  static const int64_t _MAX = std::numeric_limits<int32_t>::max();
  /// @brief Serializer
  JSON _json;
};

} // namespace backend
} // namespace neurun

#endif // __NEURUN_BACKEND_EXEC_TIME_H__