kernel_runner.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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_KERNELS_KERNEL_RUNNER_H_
  13. #define TENSORFLOW_LITE_MICRO_KERNELS_KERNEL_RUNNER_H_
  14. #include "tensorflow/lite/c/common.h"
  15. #include "tensorflow/lite/kernels/internal/compatibility.h"
  16. #include "tensorflow/lite/micro/fake_micro_context.h"
  17. #include "tensorflow/lite/micro/mock_micro_graph.h"
  18. #include "tensorflow/lite/micro/simple_memory_allocator.h"
  19. namespace tflite {
  20. namespace micro {
  21. // Helper class to perform a simulated kernel (i.e. TfLiteRegistration)
  22. // lifecycle (init, prepare, invoke). All internal allocations are handled by
  23. // this class. Simply pass in the registration, list of required tensors, inputs
  24. // array, outputs array, and any pre-builtin data. Calling Invoke() will
  25. // automatically walk the kernel and outputs will be ready on the TfLiteTensor
  26. // output provided during construction.
  27. class KernelRunner {
  28. public:
  29. KernelRunner(const TfLiteRegistration& registration, TfLiteTensor* tensors,
  30. int tensors_size, TfLiteIntArray* inputs,
  31. TfLiteIntArray* outputs, void* builtin_data);
  32. // Calls init and prepare on the kernel (i.e. TfLiteRegistration) struct. Any
  33. // exceptions will be DebugLog'd and returned as a status code.
  34. TfLiteStatus InitAndPrepare(const char* init_data = nullptr,
  35. size_t length = 0);
  36. // Calls init, prepare, and invoke on a given TfLiteRegistration pointer.
  37. // After successful invoke, results will be available in the output tensor as
  38. // passed into the constructor of this class.
  39. TfLiteStatus Invoke();
  40. // Returns a pointer to the internal MockMicroGraph which KernelRunner uses
  41. // to stub out MicroGraph methods and track invocations on each subgraph.
  42. MockMicroGraph* GetMockGraph() { return &mock_micro_graph_; }
  43. // Returns true if all temp buffer in tests are deallocated.
  44. // TODO(b/209453859): move this function to private after deallocation checks
  45. // are enabled for all kernel tests.
  46. bool ValidateTempBufferDeallocated();
  47. private:
  48. static constexpr int kKernelRunnerBufferSize_ = 10000;
  49. static uint8_t kKernelRunnerBuffer_[kKernelRunnerBufferSize_];
  50. TfLiteContext context_ = {};
  51. TfLiteNode node_ = {};
  52. const TfLiteRegistration& registration_;
  53. SimpleMemoryAllocator* allocator_;
  54. MockMicroGraph mock_micro_graph_;
  55. FakeMicroContext fake_micro_context_;
  56. };
  57. } // namespace micro
  58. } // namespace tflite
  59. #endif // TENSORFLOW_LITE_MICRO_KERNELS_KERNEL_RUNNER_H_