xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/kernels/shim/test_util.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2021 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_SHIM_TEST_UTILS_H_
16 #define TENSORFLOW_LITE_KERNELS_SHIM_TEST_UTILS_H_
17 
18 #include <initializer_list>
19 #include <string>
20 
21 #include "tensorflow/core/platform/logging.h"
22 #include "tensorflow/lite/c/common.h"
23 #include "tensorflow/lite/type_to_tflitetype.h"
24 
25 namespace tflite {
26 
27 // A wrapper around TfLiteTensor which frees it in its dtor.
28 class UniqueTfLiteTensor {
29  public:
UniqueTfLiteTensor(TfLiteTensor * tensor)30   explicit UniqueTfLiteTensor(TfLiteTensor* tensor) : tensor_(tensor) {}
31 
32   UniqueTfLiteTensor() = default;
33 
34   // Returns the underlying pointer
35 
36   TfLiteTensor* get();
37 
38   TfLiteTensor& operator*();
39 
40   TfLiteTensor* operator->();
41 
42   const TfLiteTensor* get() const;
43 
44   const TfLiteTensor& operator*() const;
45 
46   const TfLiteTensor* operator->() const;
47 
48   // Resets the underlying pointer
49   void reset(TfLiteTensor* tensor);
50 
51   // Deallocates the tensor as well
52   ~UniqueTfLiteTensor();
53 
54  private:
55   TfLiteTensor* tensor_ = nullptr;
56 };
57 
58 // Prints a debug string for the given tensor.
59 std::string TfliteTensorDebugString(const ::TfLiteTensor* tensor,
60                                     const std::size_t max_values = 30);
61 
62 // Calculate the total number of elements given the shape.
63 std::size_t NumTotalFromShape(const std::initializer_list<int>& shape);
64 
65 template <typename T>
ReallocDynamicTensor(const std::initializer_list<int> shape,TfLiteTensor * tensor)66 void ReallocDynamicTensor(const std::initializer_list<int> shape,
67                           TfLiteTensor* tensor) {
68   TfLiteTensorFree(tensor);
69   tensor->allocation_type = kTfLiteDynamic;
70   tensor->type = typeToTfLiteType<T>();
71   // Populate Shape
72   TfLiteIntArray* shape_arr = TfLiteIntArrayCreate(shape.size());
73   int i = 0;
74   const std::size_t num_total = NumTotalFromShape(shape);
75   for (const int dim : shape) shape_arr->data[i++] = dim;
76   tensor->dims = shape_arr;
77   if (tensor->type != kTfLiteString) {
78     TfLiteTensorRealloc(num_total * sizeof(T), tensor);
79   }
80 }
81 
82 // Populates a tensor with the given values
83 template <typename T>
PopulateTfLiteTensorValue(const std::initializer_list<T> values,TfLiteTensor * tensor)84 void PopulateTfLiteTensorValue(const std::initializer_list<T> values,
85                                TfLiteTensor* tensor) {
86   T* buffer = reinterpret_cast<T*>(tensor->data.raw);
87   int i = 0;
88   for (const auto v : values) {
89     buffer[i++] = v;
90   }
91 }
92 
93 template <>
94 void PopulateTfLiteTensorValue<std::string>(
95     const std::initializer_list<std::string> values, TfLiteTensor* tensor);
96 
97 template <typename T>
PopulateTfLiteTensor(const std::initializer_list<T> values,const std::initializer_list<int> shape,TfLiteTensor * tensor)98 void PopulateTfLiteTensor(const std::initializer_list<T> values,
99                           const std::initializer_list<int> shape,
100                           TfLiteTensor* tensor) {
101   const std::size_t num_total = NumTotalFromShape(shape);
102   CHECK_EQ(num_total, values.size());
103   // Populate Shape
104   ReallocDynamicTensor<T>(shape, tensor);
105   // Value allocation
106   PopulateTfLiteTensorValue<T>(values, tensor);
107 }
108 
109 }  // namespace tflite
110 
111 #endif  // TENSORFLOW_LITE_KERNELS_SHIM_TEST_UTILS_H_
112