• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 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_MICRO_KERNELS_KERNEL_RUNNER_H_
17 #define TENSORFLOW_LITE_MICRO_KERNELS_KERNEL_RUNNER_H_
18 
19 #include "tensorflow/lite/c/common.h"
20 #include "tensorflow/lite/kernels/internal/compatibility.h"
21 #include "tensorflow/lite/micro/simple_memory_allocator.h"
22 
23 namespace tflite {
24 namespace micro {
25 
26 // Helper class to perform a simulated kernel (i.e. TfLiteRegistration)
27 // lifecycle (init, prepare, invoke). All internal allocations are handled by
28 // this class. Simply pass in the registration, list of required tensors, inputs
29 // array, outputs array, and any pre-builtin data. Calling Invoke() will
30 // automatically walk the kernel and outputs will be ready on the TfLiteTensor
31 // output provided during construction.
32 class KernelRunner {
33  public:
34   KernelRunner(const TfLiteRegistration& registration, TfLiteTensor* tensors,
35                int tensors_size, TfLiteIntArray* inputs,
36                TfLiteIntArray* outputs, void* builtin_data);
37 
38   // Calls init and prepare on the kernel (i.e. TfLiteRegistration) struct. Any
39   // exceptions will be DebugLog'd and returned as a status code.
40   TfLiteStatus InitAndPrepare(const char* init_data = nullptr,
41                               size_t length = 0);
42 
43   // Calls init, prepare, and invoke on a given TfLiteRegistration pointer.
44   // After successful invoke, results will be available in the output tensor as
45   // passed into the constructor of this class.
46   TfLiteStatus Invoke();
47 
48  protected:
49   static TfLiteTensor* GetTensor(const struct TfLiteContext* context,
50                                  int tensor_index);
51   static TfLiteEvalTensor* GetEvalTensor(const struct TfLiteContext* context,
52                                          int tensor_index);
53   static void* AllocatePersistentBuffer(TfLiteContext* context, size_t bytes);
54   static TfLiteStatus RequestScratchBufferInArena(TfLiteContext* context,
55                                                   size_t bytes,
56                                                   int* buffer_index);
57   static void* GetScratchBuffer(TfLiteContext* context, int buffer_index);
58   static void ReportOpError(struct TfLiteContext* context, const char* format,
59                             ...);
60 
61  private:
62   static constexpr int kNumScratchBuffers_ = 12;
63 
64   static constexpr int kKernelRunnerBufferSize_ = 10000;
65   static uint8_t kKernelRunnerBuffer_[kKernelRunnerBufferSize_];
66 
67   SimpleMemoryAllocator* allocator_ = nullptr;
68   const TfLiteRegistration& registration_;
69   TfLiteTensor* tensors_ = nullptr;
70 
71   TfLiteContext context_ = {};
72   TfLiteNode node_ = {};
73 
74   int scratch_buffer_count_ = 0;
75   uint8_t* scratch_buffers_[kNumScratchBuffers_];
76 };
77 
78 }  // namespace micro
79 }  // namespace tflite
80 
81 #endif  // TENSORFLOW_LITE_MICRO_KERNELS_KERNEL_RUNNER_H_
82