1 /* Copyright 2018 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 #ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICRO_MICRO_INTERPRETER_H_
16 #define TENSORFLOW_LITE_EXPERIMENTAL_MICRO_MICRO_INTERPRETER_H_
17 
18 #include "tensorflow/lite/c/c_api_internal.h"
19 #include "tensorflow/lite/core/api/error_reporter.h"
20 #include "tensorflow/lite/core/api/op_resolver.h"
21 #include "tensorflow/lite/experimental/micro/simple_tensor_allocator.h"
22 #include "tensorflow/lite/schema/schema_generated.h"
23 
24 namespace tflite {
25 
26 class MicroInterpreter {
27  public:
28   // The lifetime of the model, op resolver, allocator, and error reporter must
29   // be at least as long as that of the interpreter object, since the
30   // interpreter may need to access them at any time. This means that you should
31   // usually create them with the same scope as each other, for example having
32   // them all allocated on the stack as local variables through a top-level
33   // function.
34   // The interpreter doesn't do any deallocation of any of the pointed-to
35   // objects, ownership remains with the caller.
36   MicroInterpreter(const Model* model, const OpResolver& op_resolver,
37                    SimpleTensorAllocator* tensor_allocator,
38                    ErrorReporter* error_reporter);
39 
40   TfLiteStatus Invoke();
41 
tensors_size()42   size_t tensors_size() const { return context_.tensors_size; }
43   TfLiteTensor* tensor(int tensor_index);
44 
45   TfLiteTensor* input(int index);
inputs_size()46   size_t inputs_size() const { return subgraph_->inputs()->Length(); }
47 
48   TfLiteTensor* output(int index);
outputs_size()49   size_t outputs_size() const { return subgraph_->outputs()->Length(); }
50 
initialization_status()51   TfLiteStatus initialization_status() const { return initialization_status_; }
52 
error_reporter()53   ErrorReporter* error_reporter() { return error_reporter_; }
54 
55  private:
56   const Model* model_;
57   const OpResolver& op_resolver_;
58   SimpleTensorAllocator* tensor_allocator_;
59   ErrorReporter* error_reporter_;
60 
61   TfLiteStatus initialization_status_;
62   const flatbuffers::Vector<flatbuffers::Offset<Tensor>>* tensors_;
63   const flatbuffers::Vector<flatbuffers::Offset<Operator>>* operators_;
64   TfLiteContext context_;
65 
66   const SubGraph* subgraph_;
67 };
68 
69 }  // namespace tflite
70 
71 #endif  // TENSORFLOW_LITE_EXPERIMENTAL_MICRO_MICRO_INTERPRETER_H_
72