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