kernel_runner.cc 4.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/lite/micro/kernels/kernel_runner.h"

18
#include "tensorflow/lite/micro/arena_allocator/single_arena_buffer_allocator.h"
19
#include "tensorflow/lite/micro/micro_arena_constants.h"
20
#include "tensorflow/lite/micro/micro_log.h"
21
#include "tensorflow/lite/micro/test_helpers.h"
22 23 24 25 26 27 28 29

namespace tflite {
namespace micro {

// TODO(b/161841696): Consider moving away from global arena buffers:
constexpr int KernelRunner::kKernelRunnerBufferSize_;
uint8_t KernelRunner::kKernelRunnerBuffer_[];

30 31 32 33 34 35 36
void ClearBufferApi(TfLiteContext* context_) {
  context_->GetScratchBuffer = nullptr;
  context_->GetExternalContext = nullptr;
  context_->AllocatePersistentBuffer = nullptr;
  context_->RequestScratchBufferInArena = nullptr;
}

37
KernelRunner::KernelRunner(const TFLMRegistration& registration,
38 39
                           TfLiteTensor* tensors, int tensors_size,
                           TfLiteIntArray* inputs, TfLiteIntArray* outputs,
40
                           void* builtin_data, TfLiteIntArray* intermediates)
41
    : registration_(registration),
42
      allocator_(SingleArenaBufferAllocator::Create(kKernelRunnerBuffer_,
43
                                                    kKernelRunnerBufferSize_)),
44 45
      mock_micro_graph_(allocator_),
      fake_micro_context_(tensors, allocator_, &mock_micro_graph_) {
46
  // Prepare TfLiteContext:
47 48
  context_.impl_ = static_cast<void*>(&fake_micro_context_);
  context_.ReportError = MicroContextReportOpError;
49
  context_.recommended_num_threads = 1;
50 51
  context_.GetTensor = MicroContextGetTensor;
  context_.GetEvalTensor = MicroContextGetEvalTensor;
52
  tflite::micro::ClearBufferApi(&context_);
53 54
  context_.AllocatePersistentBuffer = MicroContextAllocatePersistentBuffer;

55
  context_.recommended_num_threads = 0;
56 57 58 59 60

  // Prepare TfLiteNode:
  node_.inputs = inputs;
  node_.outputs = outputs;
  node_.builtin_data = builtin_data;
61
  node_.intermediates = intermediates;
62 63
}

64 65 66 67
bool KernelRunner::ValidateTempBufferDeallocated() {
  return fake_micro_context_.IsAllTempTfLiteTensorDeallocated();
}

68 69 70
TfLiteStatus KernelRunner::InitAndPrepare(const char* init_data,
                                          size_t length) {
  if (registration_.init) {
71 72
    tflite::micro::ClearBufferApi(&context_);
    context_.AllocatePersistentBuffer = MicroContextAllocatePersistentBuffer;
73 74
    node_.user_data = registration_.init(&context_, init_data, length);
  }
75 76

  TF_LITE_ENSURE(&context_, ValidateTempBufferDeallocated());
77

78
  if (registration_.prepare) {
79 80 81 82 83
    tflite ::micro::ClearBufferApi(&context_);
    context_.AllocatePersistentBuffer = MicroContextAllocatePersistentBuffer;
    context_.RequestScratchBufferInArena =
        MicroContextRequestScratchBufferInArena;
    context_.GetExternalContext = MicroContextGetExternalContext;
84 85
    TF_LITE_ENSURE_STATUS(registration_.prepare(&context_, &node_));
  }
86

87
  TF_LITE_ENSURE(&context_, ValidateTempBufferDeallocated());
88

89 90 91 92
  return kTfLiteOk;
}

TfLiteStatus KernelRunner::Invoke() {
93 94 95
  tflite::micro::ClearBufferApi(&context_);
  context_.GetScratchBuffer = MicroContextGetScratchBuffer;

96
  if (registration_.invoke == nullptr) {
97
    MicroPrintf("TFLMRegistration missing invoke function pointer!");
98 99
    return kTfLiteError;
  }
100 101 102

  TF_LITE_ENSURE_STATUS(registration_.invoke(&context_, &node_));

103
  TF_LITE_ENSURE(&context_, ValidateTempBufferDeallocated());
104 105

  return kTfLiteOk;
106 107
}

108 109 110 111 112 113 114 115 116 117 118 119 120
TfLiteStatus KernelRunner::Reset() {
  tflite::micro::ClearBufferApi(&context_);
  context_.GetScratchBuffer = MicroContextGetScratchBuffer;

  if (registration_.reset == nullptr) {
    MicroPrintf("TFLMRegistration missing reset function pointer!");
    return kTfLiteError;
  }

  registration_.reset(&context_, node_.user_data);
  return kTfLiteOk;
}

S
Steven Toribio 已提交
121 122 123 124 125
TfLiteStatus KernelRunner::Free() {
  tflite::micro::ClearBufferApi(&context_);
  context_.GetScratchBuffer = MicroContextGetScratchBuffer;

  if (registration_.free == nullptr) {
126
    MicroPrintf("TFLMRegistration missing free function pointer!");
S
Steven Toribio 已提交
127 128 129 130 131 132
    return kTfLiteError;
  }

  registration_.free(&context_, node_.user_data);
  return kTfLiteOk;
}
133
}  // namespace micro
134
}  // namespace tflite