• 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_RECORDING_MICRO_INTERPRETER_H_
17 #define TENSORFLOW_LITE_MICRO_RECORDING_MICRO_INTERPRETER_H_
18 
19 #include "tensorflow/lite/micro/micro_interpreter.h"
20 #include "tensorflow/lite/micro/recording_micro_allocator.h"
21 
22 namespace tflite {
23 
24 // Utility subclass that enables internal recordings of the MicroInterpreter.
25 // This class should be used to audit and analyze memory arena usage for a given
26 // model and interpreter.
27 //
28 // After construction and the first Invoke() or AllocateTensors() call - the
29 // memory usage is recorded and available through the GetMicroAllocator()
30 // function. See RecordingMicroAlloctor for more details on what is currently
31 // recorded from arena allocations.
32 //
33 // It is recommended for users to increase the tensor arena size by at least 1kb
34 // to ensure enough additional memory is available for internal recordings.
35 class RecordingMicroInterpreter : public MicroInterpreter {
36  public:
RecordingMicroInterpreter(const Model * model,const MicroOpResolver & op_resolver,uint8_t * tensor_arena,size_t tensor_arena_size,ErrorReporter * error_reporter)37   RecordingMicroInterpreter(const Model* model,
38                             const MicroOpResolver& op_resolver,
39                             uint8_t* tensor_arena, size_t tensor_arena_size,
40                             ErrorReporter* error_reporter)
41       : MicroInterpreter(model, op_resolver,
42                          RecordingMicroAllocator::Create(
43                              tensor_arena, tensor_arena_size, error_reporter),
44                          error_reporter),
45         recording_micro_allocator_(
46             static_cast<const RecordingMicroAllocator&>(allocator())) {}
47 
RecordingMicroInterpreter(const Model * model,const MicroOpResolver & op_resolver,RecordingMicroAllocator * allocator,ErrorReporter * error_reporter)48   RecordingMicroInterpreter(const Model* model,
49                             const MicroOpResolver& op_resolver,
50                             RecordingMicroAllocator* allocator,
51                             ErrorReporter* error_reporter)
52       : MicroInterpreter(model, op_resolver, allocator, error_reporter),
53         recording_micro_allocator_(*allocator) {}
54 
GetMicroAllocator()55   const RecordingMicroAllocator& GetMicroAllocator() const {
56     return recording_micro_allocator_;
57   }
58 
59  private:
60   const RecordingMicroAllocator& recording_micro_allocator_;
61 };
62 
63 }  // namespace tflite
64 
65 #endif  // TENSORFLOW_LITE_MICRO_RECORDING_MICRO_INTERPRETER_H_
66