micro_profiler.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* Copyright 2020 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_MICRO_MICRO_PROFILER_H_
  13. #define TENSORFLOW_LITE_MICRO_MICRO_PROFILER_H_
  14. #include "tensorflow/lite/core/api/error_reporter.h"
  15. #include "tensorflow/lite/core/api/profiler.h"
  16. #include "tensorflow/lite/micro/compatibility.h"
  17. namespace tflite {
  18. // MicroProfiler creates a common way to gain fine-grained insight into runtime
  19. // performance. Bottleck operators can be identified along with slow code
  20. // sections. This can be used in conjunction with running the relevant micro
  21. // benchmark to evaluate end-to-end performance.
  22. //
  23. // Usage example:
  24. // MicroProfiler profiler(error_reporter);
  25. // {
  26. // ScopedProfile scoped_profile(profiler, tag);
  27. // work_to_profile();
  28. // }
  29. //
  30. // This will call the following methods in order:
  31. // int event_handle = profiler->BeginEvent(op_name, EventType::DEFAULT, 0)
  32. // work_to_profile();
  33. // profiler->EndEvent(event_handle)
  34. class MicroProfiler : public tflite::Profiler {
  35. public:
  36. explicit MicroProfiler(tflite::ErrorReporter* reporter);
  37. ~MicroProfiler() override = default;
  38. // AddEvent is unused for Tf Micro.
  39. void AddEvent(const char* tag, EventType event_type, uint64_t start,
  40. uint64_t end, int64_t event_metadata1,
  41. int64_t event_metadata2) override{};
  42. // BeginEvent followed by code followed by EndEvent will profile the code
  43. // enclosed. Multiple concurrent events are unsupported, so the return value
  44. // is always 0. Event_metadata1 and event_metadata2 are unused. The tag
  45. // pointer must be valid until EndEvent is called.
  46. uint32_t BeginEvent(const char* tag, EventType event_type,
  47. int64_t event_metadata1,
  48. int64_t event_metadata2) override;
  49. // Event_handle is ignored since TF Micro does not support concurrent events.
  50. void EndEvent(uint32_t event_handle) override;
  51. private:
  52. tflite::ErrorReporter* reporter_;
  53. int32_t start_time_;
  54. const char* event_tag_;
  55. TF_LITE_REMOVE_VIRTUAL_DELETE
  56. };
  57. } // namespace tflite
  58. #endif // TENSORFLOW_LITE_MICRO_MICRO_PROFILER_H_