1 /* Copyright 2022 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 16 #ifndef TENSORFLOW_LITE_EXPERIMENTAL_TELEMETRY_PROFILER_H_ 17 #define TENSORFLOW_LITE_EXPERIMENTAL_TELEMETRY_PROFILER_H_ 18 19 #include <cstdint> 20 21 #include "tensorflow/lite/core/api/profiler.h" 22 #include "tensorflow/lite/experimental/telemetry/telemetry_settings.h" 23 #include "tensorflow/lite/experimental/telemetry/telemetry_status.h" 24 25 namespace tflite { 26 27 // Telemetry profiler interface. 28 // When installed, the telemetry profilers accepts profiler events exported from 29 // TFLite runtime profiler instrumentation points, interprets the events 30 // based on the event type and forward to corresponding `Report` function. 31 // The implementation of the `Report` functions are responsible for dumping the 32 // profiling events to the data sink. 33 class TelemetryProfiler : public Profiler { 34 public: 35 // General Telemetry events. 36 37 // Reports a telemetry event with status. 38 // `event_name` indicates the name of the event (e.g. "Invoke") and should not 39 // be nullptr. 40 // `status` shows 1) the source of the event, interpreter or which delegate, 41 // 2) the return status of the event. 42 virtual void ReportTelemetryEvent(const char* event_name, 43 TelemetryStatusCode status) = 0; 44 45 // Reports an op telemetry event with status. 46 // Same as `ReportTelemetryEvent`, with additional args `op_idx` and 47 // `subgraph_idx`. 48 virtual void ReportTelemetryOpEvent(const char* event_name, int64_t op_idx, 49 int64_t subgraph_idx, 50 TelemetryStatusCode status) = 0; 51 52 // Telemetry ReportSettings events. 53 54 // Reports the model and interpreter settings. 55 // `setting_name` indicates the name of the setting and should not be nullptr. 56 // `settings`'s lifespan is not guaranteed outside the scope of 57 // `ReportSettings` call. 58 virtual void ReportSettings(const char* setting_name, 59 const TelemetrySettings& settings) = 0; 60 61 // Performance measurement events. 62 63 // Signals the beginning of an operator invocation. 64 // `op_name` is the name of the operator and should not be nullptr. 65 // Op invoke event are triggered with OPERATOR_INVOKE_EVENT type for TfLite 66 // ops and delegate kernels, and DELEGATE_OPERATOR_INVOKE_EVENT for delegate 67 // ops within a delegate kernels, if the instrumentation is in place. 68 // Returns event handle which can be passed to `EndOpInvokeEvent` later. 69 virtual uint32_t ReportBeginOpInvokeEvent(const char* op_name, int64_t op_idx, 70 int64_t subgraph_idx) = 0; 71 72 // Signals the end to the event specified by `event_handle`. 73 virtual void ReportEndOpInvokeEvent(uint32_t event_handle) = 0; 74 75 // For op / delegate op with built-in performance measurements, they 76 // are able to report the elapsed time directly. 77 virtual void ReportOpInvokeEvent(const char* op_name, uint64_t elapsed_time, 78 int64_t op_idx, int64_t subgraph_idx) = 0; 79 80 private: 81 // Methods inherited from TfLite Profiler. 82 // TelemetryProfiler will dispatch the event signals to appropriate `Report` 83 // functinos defined above based on the event type. 84 // Subclasses should not override those following methods. 85 void AddEvent(const char* tag, EventType event_type, uint64_t metric, 86 int64_t event_metadata1, int64_t event_metadata2) final; 87 void AddEventWithData(const char* tag, EventType event_type, 88 const void* data) final; 89 uint32_t BeginEvent(const char* tag, EventType event_type, 90 int64_t event_metadata1, int64_t event_metadata2) final; 91 void EndEvent(uint32_t event_handle) final; 92 }; 93 94 } // namespace tflite 95 96 #endif // TENSORFLOW_LITE_EXPERIMENTAL_TELEMETRY_PROFILER_H_ 97