|
| 1 | +/* Copyright 2023 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_ARRAY_H_ |
| 16 | +#define TENSORFLOW_LITE_ARRAY_H_ |
| 17 | + |
| 18 | +#include <cstring> |
| 19 | +#include <initializer_list> |
| 20 | +#include <memory> |
| 21 | +#include <type_traits> |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +#include "tensorflow/lite/core/c/common.h" |
| 25 | + |
| 26 | +namespace tflite { |
| 27 | + |
| 28 | +/// TfLite*Array helpers |
| 29 | + |
| 30 | +namespace array_internal { |
| 31 | + |
| 32 | +// Function object used as a deleter for unique_ptr holding TFLite*Array |
| 33 | +// objects. |
| 34 | +struct TfLiteArrayDeleter { |
| 35 | + void operator()(TfLiteIntArray* a); |
| 36 | + void operator()(TfLiteFloatArray* a); |
| 37 | +}; |
| 38 | + |
| 39 | +// Maps T to the corresponding TfLiteArray type. |
| 40 | +template <class T> |
| 41 | +struct TfLiteArrayInfo; |
| 42 | + |
| 43 | +template <> |
| 44 | +struct TfLiteArrayInfo<int> { |
| 45 | + using Type = TfLiteIntArray; |
| 46 | +}; |
| 47 | + |
| 48 | +template <> |
| 49 | +struct TfLiteArrayInfo<float> { |
| 50 | + using Type = TfLiteFloatArray; |
| 51 | +}; |
| 52 | + |
| 53 | +} // namespace array_internal |
| 54 | + |
| 55 | +template <class T> |
| 56 | +using TfLiteArrayUniquePtr = |
| 57 | + std::unique_ptr<typename array_internal::TfLiteArrayInfo<T>::Type, |
| 58 | + array_internal::TfLiteArrayDeleter>; |
| 59 | + |
| 60 | +// `unique_ptr` wrapper for `TfLiteIntArray`s. |
| 61 | +using IntArrayUniquePtr = TfLiteArrayUniquePtr<int>; |
| 62 | + |
| 63 | +// `unique_ptr` wrapper for `TfLiteFloatArray`s. |
| 64 | +using FloatArrayUniquePtr = TfLiteArrayUniquePtr<float>; |
| 65 | + |
| 66 | +// Allocates a TfLiteArray of given size using malloc. |
| 67 | +// |
| 68 | +// This builds an int array by default as this is the overwhelming part of the |
| 69 | +// use cases. |
| 70 | +template <class T = int> |
| 71 | +TfLiteArrayUniquePtr<T> BuildTfLiteArray(int size); |
| 72 | + |
| 73 | +// Allocates a TfLiteIntArray of given size using malloc. |
| 74 | +template <> |
| 75 | +inline IntArrayUniquePtr BuildTfLiteArray<int>(const int size) { |
| 76 | + return IntArrayUniquePtr(TfLiteIntArrayCreate(size)); |
| 77 | +} |
| 78 | + |
| 79 | +// Allocates a TfLiteFloatArray of given size using malloc. |
| 80 | +template <> |
| 81 | +inline FloatArrayUniquePtr BuildTfLiteArray<float>(const int size) { |
| 82 | + return FloatArrayUniquePtr(TfLiteFloatArrayCreate(size)); |
| 83 | +} |
| 84 | + |
| 85 | +// Allocates a TFLiteArray of given size and initializes it. |
| 86 | +// |
| 87 | +// `values` is expected to holds `size` elements. |
| 88 | +template <class T> |
| 89 | +TfLiteArrayUniquePtr<T> BuildTfLiteArray(const int size, |
| 90 | + const T* const values) { |
| 91 | + auto array = BuildTfLiteArray<T>(size); |
| 92 | + if (array) { |
| 93 | + memcpy(array->data, values, size * sizeof(T)); |
| 94 | + } |
| 95 | + return array; |
| 96 | +} |
| 97 | + |
| 98 | +// Allocates a TFLiteArray and initializes it with the given values. |
| 99 | +template <class T> |
| 100 | +TfLiteArrayUniquePtr<T> BuildTfLiteArray(const std::vector<T>& values) { |
| 101 | + return BuildTfLiteArray(static_cast<int>(values.size()), values.data()); |
| 102 | +} |
| 103 | + |
| 104 | +// Allocates a TFLiteArray and initializes it with the given values. |
| 105 | +template <class T> |
| 106 | +TfLiteArrayUniquePtr<T> BuildTfLiteArray( |
| 107 | + const std::initializer_list<T>& values) { |
| 108 | + return BuildTfLiteArray(static_cast<int>(values.size()), values.begin()); |
| 109 | +} |
| 110 | + |
| 111 | +// Allocates a TFLiteArray and initializes it with the given array. |
| 112 | +inline IntArrayUniquePtr BuildTfLiteArray(const TfLiteIntArray& other) { |
| 113 | + return BuildTfLiteArray(other.size, other.data); |
| 114 | +} |
| 115 | + |
| 116 | +// Allocates a TFLiteArray and initializes it with the given array. |
| 117 | +inline FloatArrayUniquePtr BuildTfLiteArray(const TfLiteFloatArray& other) { |
| 118 | + return BuildTfLiteArray(other.size, other.data); |
| 119 | +} |
| 120 | + |
| 121 | +} // namespace tflite |
| 122 | + |
| 123 | +#endif // TENSORFLOW_LITE_ARRAY_H_ |
0 commit comments