1 /* Copyright 2017 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_KERNELS_INTERNAL_TENSOR_CTYPES_H_ 16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_TENSOR_CTYPES_H_ 17 18 #include "tensorflow/lite/c/c_api_internal.h" 19 #include "tensorflow/lite/kernels/internal/types.h" 20 21 namespace tflite { 22 23 template <typename T> 24 inline T* GetTensorData(TfLiteTensor* tensor); 25 26 template <> GetTensorData(TfLiteTensor * tensor)27inline float* GetTensorData(TfLiteTensor* tensor) { 28 return tensor != nullptr ? tensor->data.f : nullptr; 29 } 30 31 template <> GetTensorData(TfLiteTensor * tensor)32inline uint8_t* GetTensorData(TfLiteTensor* tensor) { 33 return tensor != nullptr ? tensor->data.uint8 : nullptr; 34 } 35 36 template <> GetTensorData(TfLiteTensor * tensor)37inline int16_t* GetTensorData(TfLiteTensor* tensor) { 38 return tensor != nullptr ? tensor->data.i16 : nullptr; 39 } 40 41 template <> GetTensorData(TfLiteTensor * tensor)42inline int32_t* GetTensorData(TfLiteTensor* tensor) { 43 return tensor != nullptr ? tensor->data.i32 : nullptr; 44 } 45 46 template <> GetTensorData(TfLiteTensor * tensor)47inline int64_t* GetTensorData(TfLiteTensor* tensor) { 48 return tensor != nullptr ? tensor->data.i64 : nullptr; 49 } 50 51 template <> GetTensorData(TfLiteTensor * tensor)52inline bool* GetTensorData(TfLiteTensor* tensor) { 53 return tensor != nullptr ? tensor->data.b : nullptr; 54 } 55 56 template <> GetTensorData(TfLiteTensor * tensor)57inline int8_t* GetTensorData(TfLiteTensor* tensor) { 58 return tensor != nullptr ? tensor->data.int8 : nullptr; 59 } 60 61 template <typename T> 62 inline const T* GetTensorData(const TfLiteTensor* tensor); 63 64 template <> GetTensorData(const TfLiteTensor * tensor)65inline const float* GetTensorData(const TfLiteTensor* tensor) { 66 return tensor != nullptr ? tensor->data.f : nullptr; 67 } 68 69 template <> GetTensorData(const TfLiteTensor * tensor)70inline const uint8_t* GetTensorData(const TfLiteTensor* tensor) { 71 return tensor != nullptr ? tensor->data.uint8 : nullptr; 72 } 73 74 template <> GetTensorData(const TfLiteTensor * tensor)75inline const int8_t* GetTensorData(const TfLiteTensor* tensor) { 76 return tensor != nullptr ? tensor->data.int8 : nullptr; 77 } 78 79 template <> GetTensorData(const TfLiteTensor * tensor)80inline const int16_t* GetTensorData(const TfLiteTensor* tensor) { 81 return tensor != nullptr ? tensor->data.i16 : nullptr; 82 } 83 84 template <> GetTensorData(const TfLiteTensor * tensor)85inline const int32_t* GetTensorData(const TfLiteTensor* tensor) { 86 return tensor != nullptr ? tensor->data.i32 : nullptr; 87 } 88 89 template <> GetTensorData(const TfLiteTensor * tensor)90inline const int64_t* GetTensorData(const TfLiteTensor* tensor) { 91 return tensor != nullptr ? tensor->data.i64 : nullptr; 92 } 93 94 template <> GetTensorData(const TfLiteTensor * tensor)95inline const bool* GetTensorData(const TfLiteTensor* tensor) { 96 return tensor != nullptr ? tensor->data.b : nullptr; 97 } 98 GetTensorShape(const TfLiteTensor * tensor)99inline RuntimeShape GetTensorShape(const TfLiteTensor* tensor) { 100 if (tensor == nullptr) { 101 return RuntimeShape(); 102 } 103 104 TfLiteIntArray* dims = tensor->dims; 105 const int dims_size = dims->size; 106 const int32_t* dims_data = dims->data; 107 return RuntimeShape(dims_size, dims_data); 108 } 109 110 } // namespace tflite 111 112 #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_TENSOR_CTYPES_H_ 113