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_CORE_API_FLATBUFFER_CONVERSIONS_H_
16 #define TENSORFLOW_LITE_CORE_API_FLATBUFFER_CONVERSIONS_H_
17 
18 // These functions transform codes and data structures that are defined in the
19 // flatbuffer serialization format into in-memory values that are used by the
20 // runtime API and interpreter.
21 
22 #include "tensorflow/lite/c/c_api_internal.h"
23 #include "tensorflow/lite/core/api/error_reporter.h"
24 #include "tensorflow/lite/core/api/op_resolver.h"
25 #include "tensorflow/lite/schema/schema_generated.h"
26 
27 namespace tflite {
28 
29 // Interface class for builtin data allocations.
30 class BuiltinDataAllocator {
31  public:
32   virtual void* Allocate(size_t size) = 0;
33   virtual void Deallocate(void* data) = 0;
34 
35   // Allocate a structure, but make sure it is a POD structure that doesn't
36   // require constructors to run. The reason we do this, is that Interpreter's C
37   // extension part will take ownership so destructors  will not be run during
38   // deallocation.
39   template <typename T>
AllocatePOD()40   T* AllocatePOD() {
41     static_assert(std::is_pod<T>::value, "Builtin data structure must be POD.");
42     return static_cast<T*>(this->Allocate(sizeof(T)));
43   }
44 
~BuiltinDataAllocator()45   virtual ~BuiltinDataAllocator() {}
46 };
47 
48 // Parse the appropriate data out of the op.
49 //
50 // This handles builtin data explicitly as there are flatbuffer schemas.
51 // If it returns kTfLiteOk, it passes the data out with `builtin_data`. The
52 // calling function has to pass in an allocator object, and this allocator
53 // will be called to reserve space for the output data. If the calling
54 // function's allocator reserves memory on the heap, then it's the calling
55 // function's responsibility to free it.
56 // If it returns kTfLiteError, `builtin_data` will be `nullptr`.
57 TfLiteStatus ParseOpData(const Operator* op, BuiltinOperator op_type,
58                          ErrorReporter* error_reporter,
59                          BuiltinDataAllocator* allocator, void** builtin_data);
60 
61 // Converts the tensor data type used in the flat buffer to the representation
62 // used by the runtime.
63 TfLiteStatus ConvertTensorType(TensorType tensor_type, TfLiteType* type,
64                                ErrorReporter* error_reporter);
65 
66 }  // namespace tflite
67 
68 #endif  // TENSORFLOW_LITE_CORE_API_FLATBUFFER_CONVERSIONS_H_
69