profiler.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. #ifndef TENSORFLOW_LITE_CORE_API_PROFILER_H_
  13. #define TENSORFLOW_LITE_CORE_API_PROFILER_H_
  14. #include <cstdint>
  15. namespace tflite {
  16. // A simple utility for enabling profiled event tracing in TensorFlow Lite.
  17. class Profiler {
  18. public:
  19. // As certain Profiler instance might be only interested in certain event
  20. // types, we define each event type value to allow a Profiler to use
  21. // bitmasking bitwise operations to determine whether an event should be
  22. // recorded or not.
  23. enum class EventType {
  24. // Default event type, the metadata field has no special significance.
  25. DEFAULT = 1,
  26. // The event is an operator invocation and the event_metadata field is the
  27. // index of operator node.
  28. OPERATOR_INVOKE_EVENT = 2,
  29. // The event is an invocation for an internal operator of a TFLite delegate.
  30. // The event_metadata field is the index of operator node that's specific to
  31. // the delegate.
  32. DELEGATE_OPERATOR_INVOKE_EVENT = 4,
  33. // The event is a recording of runtime instrumentation such as the overall
  34. // TFLite runtime status, the TFLite delegate status (if a delegate
  35. // is applied), and the overall model inference latency etc.
  36. // Note, the delegate status and overall status are stored as separate
  37. // event_metadata fields. In particular, the delegate status is encoded
  38. // as DelegateStatus::full_status().
  39. GENERAL_RUNTIME_INSTRUMENTATION_EVENT = 8,
  40. };
  41. virtual ~Profiler() {}
  42. // Signals the beginning of an event and returns a handle to the profile
  43. // event. The `event_metadata1` and `event_metadata2` have different
  44. // interpretations based on the actual Profiler instance and the `event_type`.
  45. // For example, as for the 'SubgraphAwareProfiler' defined in
  46. // lite/core/subgraph.h, when the event_type is OPERATOR_INVOKE_EVENT,
  47. // `event_metadata1` represents the index of a TFLite node, and
  48. // `event_metadata2` represents the index of the subgraph that this event
  49. // comes from.
  50. virtual uint32_t BeginEvent(const char* tag, EventType event_type,
  51. int64_t event_metadata1,
  52. int64_t event_metadata2) = 0;
  53. // Similar w/ the above, but `event_metadata2` defaults to 0.
  54. uint32_t BeginEvent(const char* tag, EventType event_type,
  55. int64_t event_metadata) {
  56. return BeginEvent(tag, event_type, event_metadata, /*event_metadata2*/ 0);
  57. }
  58. // Signals an end to the specified profile event with 'event_metadata's, This
  59. // is useful when 'event_metadata's are not available when the event begins
  60. // or when one wants to overwrite the 'event_metadata's set at the beginning.
  61. virtual void EndEvent(uint32_t event_handle, int64_t event_metadata1,
  62. int64_t event_metadata2) {}
  63. // Signals an end to the specified profile event.
  64. virtual void EndEvent(uint32_t event_handle) = 0;
  65. // Appends an event of type 'event_type' with 'tag' and 'event_metadata'
  66. // which started at 'start' and ended at 'end'
  67. // Note:
  68. // In cases were ProfileSimmarizer and tensorflow::StatsCalculator are used
  69. // they assume the value is in "usec", if in any case subclasses
  70. // didn't put usec, then the values are not meaningful.
  71. // TODO karimnosseir: Revisit and make the function more clear.
  72. void AddEvent(const char* tag, EventType event_type, uint64_t start,
  73. uint64_t end, int64_t event_metadata) {
  74. AddEvent(tag, event_type, start, end, event_metadata,
  75. /*event_metadata2*/ 0);
  76. }
  77. virtual void AddEvent(const char* tag, EventType event_type, uint64_t start,
  78. uint64_t end, int64_t event_metadata1,
  79. int64_t event_metadata2) {}
  80. protected:
  81. friend class ScopedProfile;
  82. };
  83. // Adds a profile event to `profiler` that begins with the construction
  84. // of the object and ends when the object goes out of scope.
  85. // The lifetime of tag should be at least the lifetime of `profiler`.
  86. // `profiler` may be null, in which case nothing is profiled.
  87. class ScopedProfile {
  88. public:
  89. ScopedProfile(Profiler* profiler, const char* tag,
  90. Profiler::EventType event_type = Profiler::EventType::DEFAULT,
  91. int64_t event_metadata = 0)
  92. : profiler_(profiler), event_handle_(0) {
  93. if (profiler) {
  94. event_handle_ = profiler_->BeginEvent(tag, event_type, event_metadata);
  95. }
  96. }
  97. ~ScopedProfile() {
  98. if (profiler_) {
  99. profiler_->EndEvent(event_handle_);
  100. }
  101. }
  102. protected:
  103. Profiler* profiler_;
  104. uint32_t event_handle_;
  105. };
  106. class ScopedOperatorProfile : public ScopedProfile {
  107. public:
  108. ScopedOperatorProfile(Profiler* profiler, const char* tag, int node_index)
  109. : ScopedProfile(profiler, tag, Profiler::EventType::OPERATOR_INVOKE_EVENT,
  110. static_cast<uint32_t>(node_index)) {}
  111. };
  112. class ScopedDelegateOperatorProfile : public ScopedProfile {
  113. public:
  114. ScopedDelegateOperatorProfile(Profiler* profiler, const char* tag,
  115. int node_index)
  116. : ScopedProfile(profiler, tag,
  117. Profiler::EventType::DELEGATE_OPERATOR_INVOKE_EVENT,
  118. static_cast<uint32_t>(node_index)) {}
  119. };
  120. class ScopedRuntimeInstrumentationProfile : public ScopedProfile {
  121. public:
  122. ScopedRuntimeInstrumentationProfile(Profiler* profiler, const char* tag)
  123. : ScopedProfile(
  124. profiler, tag,
  125. Profiler::EventType::GENERAL_RUNTIME_INSTRUMENTATION_EVENT, -1) {}
  126. void set_runtime_status(int64_t delegate_status, int64_t interpreter_status) {
  127. if (profiler_) {
  128. delegate_status_ = delegate_status;
  129. interpreter_status_ = interpreter_status;
  130. }
  131. }
  132. ~ScopedRuntimeInstrumentationProfile() {
  133. if (profiler_) {
  134. profiler_->EndEvent(event_handle_, delegate_status_, interpreter_status_);
  135. }
  136. }
  137. private:
  138. int64_t delegate_status_;
  139. int64_t interpreter_status_;
  140. };
  141. } // namespace tflite
  142. #define TFLITE_VARNAME_UNIQ_IMPL(name, ctr) name##ctr
  143. #define TFLITE_VARNAME_UNIQ(name, ctr) TFLITE_VARNAME_UNIQ_IMPL(name, ctr)
  144. #define TFLITE_SCOPED_TAGGED_DEFAULT_PROFILE(profiler, tag) \
  145. tflite::ScopedProfile TFLITE_VARNAME_UNIQ(_profile_, __COUNTER__)( \
  146. (profiler), (tag))
  147. #define TFLITE_SCOPED_TAGGED_OPERATOR_PROFILE(profiler, tag, node_index) \
  148. tflite::ScopedOperatorProfile TFLITE_VARNAME_UNIQ(_profile_, __COUNTER__)( \
  149. (profiler), (tag), (node_index))
  150. #define TFLITE_SCOPED_DELEGATE_OPERATOR_PROFILE(profiler, tag, node_index) \
  151. tflite::ScopedDelegateOperatorProfile TFLITE_VARNAME_UNIQ( \
  152. _profile_, __COUNTER__)((profiler), (tag), (node_index))
  153. #define TFLITE_ADD_RUNTIME_INSTRUMENTATION_EVENT( \
  154. profiler, tag, delegate_status, interpreter_status) \
  155. do { \
  156. if (!profiler) { \
  157. const auto handle = profiler->BeginEvent( \
  158. tag, Profiler::EventType::GENERAL_RUNTIME_INSTRUMENTATION_EVENT, \
  159. delegate_status, interpreter_status); \
  160. profiler->EndEvent(handle); \
  161. } \
  162. } while (false);
  163. #endif // TENSORFLOW_LITE_CORE_API_PROFILER_H_