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_KERNELS_KERNEL_UTIL_H_
17 #define TENSORFLOW_LITE_MICRO_KERNELS_KERNEL_UTIL_H_
18 
19 #include <cstdint>
20 
21 #include "tensorflow/lite/c/builtin_op_data.h"
22 #include "tensorflow/lite/c/common.h"
23 #include "tensorflow/lite/kernels/internal/compatibility.h"
24 #include "tensorflow/lite/kernels/internal/types.h"
25 
26 namespace tflite {
27 namespace micro {
28 
29 // Returns a mutable tensor for a given input index. is_variable must be checked
30 // during prepare when the full TfLiteTensor is available.
GetMutableEvalInput(const TfLiteContext * context,const TfLiteNode * node,int index)31 inline TfLiteEvalTensor* GetMutableEvalInput(const TfLiteContext* context,
32                                              const TfLiteNode* node,
33                                              int index) {
34   TFLITE_DCHECK(context != nullptr);
35   TFLITE_DCHECK(node != nullptr);
36   return context->GetEvalTensor(context, node->inputs->data[index]);
37 }
38 
39 // Returns the TfLiteEvalTensor struct for a given input index in a node.
GetEvalInput(const TfLiteContext * context,const TfLiteNode * node,int index)40 inline const TfLiteEvalTensor* GetEvalInput(const TfLiteContext* context,
41                                             const TfLiteNode* node, int index) {
42   return GetMutableEvalInput(context, node, index);
43 }
44 
45 // Returns the TfLiteEvalTensor struct for a given output index in a node.
GetEvalOutput(const TfLiteContext * context,const TfLiteNode * node,int index)46 inline TfLiteEvalTensor* GetEvalOutput(const TfLiteContext* context,
47                                        const TfLiteNode* node, int index) {
48   TFLITE_DCHECK(context != nullptr);
49   TFLITE_DCHECK(node != nullptr);
50   return context->GetEvalTensor(context, node->outputs->data[index]);
51 }
52 
53 // Returns data for a TfLiteEvalTensor struct.
54 template <typename T>
GetTensorData(TfLiteEvalTensor * tensor)55 T* GetTensorData(TfLiteEvalTensor* tensor) {
56   return tensor != nullptr ? reinterpret_cast<T*>(tensor->data.raw) : nullptr;
57 }
58 
59 // Returns const data for a TfLiteEvalTensor struct.
60 template <typename T>
GetTensorData(const TfLiteEvalTensor * tensor)61 const T* GetTensorData(const TfLiteEvalTensor* tensor) {
62   TFLITE_DCHECK(tensor != nullptr);
63   return reinterpret_cast<const T*>(tensor->data.raw);
64 }
65 
66 // Returns the shape of a TfLiteEvalTensor struct.
67 const RuntimeShape GetTensorShape(const TfLiteEvalTensor* tensor);
68 
69 // Return true if the given tensors have the same shape.
70 bool HaveSameShapes(const TfLiteEvalTensor* input1,
71                     const TfLiteEvalTensor* input2);
72 
73 PaddingType RuntimePaddingType(TfLitePadding padding);
74 
75 }  // namespace micro
76 }  // namespace tflite
77 
78 #endif  // TENSORFLOW_LITE_MICRO_KERNELS_KERNEL_UTIL_H_
79