tflite-micro/tensorflow/lite/micro/micro_context.cc
RJ Ascani c3cfd344e5
Make MicroContext and MicroGraph abstract (#2202)
MicroContext and MicroGraph serve as the primary API surface between the interpreter and the kernels. For interpreter-less execution, we need to be able to substitute different MicroContext and MicroGraph implementations. This PR makes MicroContext and MicroGraph abstract and moves their existing implementations to MicroInterpreterContext and MicroInterpreterGraph, respectively. This will allow us to substitute an inference-only implementation of each for codegen.

FakeMicroContext and MockMicroGraph were also updated to implement the base classes.

BUG=b/295174086
2023-09-11 21:17:51 +00:00

77 lines
2.4 KiB
C++

/* Copyright 2023 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/micro_context.h"
#include <cstdarg>
#include <cstddef>
#include "tensorflow/lite/micro/micro_common.h"
#include "tensorflow/lite/micro/micro_log.h"
namespace tflite {
namespace {
int GetTensorIndex(int index, int max_size, const int* tensor_indices) {
if (index >= 0 && index < max_size) {
const int tensor_index = tensor_indices[index];
if (tensor_index != kTfLiteOptionalTensor) {
return tensor_index;
}
}
return -1;
}
} // namespace
TfLiteTensor* MicroContext::AllocateTempInputTensor(const TfLiteNode* node,
int index) {
const int tensor_index =
GetTensorIndex(index, node->inputs->size, node->inputs->data);
if (tensor_index < 0) {
return nullptr;
}
return AllocateTempTfLiteTensor(tensor_index);
}
TfLiteTensor* MicroContext::AllocateTempOutputTensor(const TfLiteNode* node,
int index) {
const int tensor_index =
GetTensorIndex(index, node->outputs->size, node->outputs->data);
if (tensor_index < 0) {
return nullptr;
}
return AllocateTempTfLiteTensor(tensor_index);
}
TfLiteTensor* MicroContext::AllocateTempIntermediateTensor(
const TfLiteNode* node, int index) {
const int tensor_index = GetTensorIndex(index, node->intermediates->size,
node->intermediates->data);
if (tensor_index < 0) {
return nullptr;
}
return AllocateTempTfLiteTensor(tensor_index);
}
void MicroContextReportOpError(struct TfLiteContext* context,
const char* format, ...) {
va_list args;
va_start(args, format);
VMicroPrintf(format, args);
va_end(args);
}
} // namespace tflite