• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 // This file provides general C++ utility functions in TFLite.
17 // For example: Converting between `TfLiteIntArray`, `std::vector` and
18 // Flatbuffer vectors. These functions can't live in `context.h` since it's pure
19 // C.
20 
21 #ifndef TENSORFLOW_LITE_UTIL_H_
22 #define TENSORFLOW_LITE_UTIL_H_
23 
24 #include <vector>
25 #include "tensorflow/lite/c/c_api_internal.h"
26 
27 namespace tflite {
28 
29 // The prefix of Flex op custom code.
30 // This will be matched agains the `custom_code` field in `OperatorCode`
31 // Flatbuffer Table.
32 // WARNING: This is an experimental API and subject to change.
33 constexpr char kFlexCustomCodePrefix[] = "Flex";
34 
35 // Checks whether the prefix of the custom name indicates the operation is an
36 // Flex operation.
37 bool IsFlexOp(const char* custom_name);
38 
39 // Converts a `std::vector` to a `TfLiteIntArray`. The caller takes ownership
40 // of the returned pointer.
41 TfLiteIntArray* ConvertVectorToTfLiteIntArray(const std::vector<int>& input);
42 
43 // Converts an array (of the given size) to a `TfLiteIntArray`. The caller
44 // takes ownership of the returned pointer, and must make sure 'dims' has at
45 // least 'rank' elemnts.
46 TfLiteIntArray* ConvertArrayToTfLiteIntArray(const int rank, const int* dims);
47 
48 // Checks whether a `TfLiteIntArray` and an int array have matching elements.
49 // The caller must guarantee that 'b' has at least 'b_size' elements.
50 bool EqualArrayAndTfLiteIntArray(const TfLiteIntArray* a, const int b_size,
51                                  const int* b);
52 
53 size_t CombineHashes(std::initializer_list<size_t> hashes);
54 
55 struct TfLiteIntArrayDeleter {
operatorTfLiteIntArrayDeleter56   void operator()(TfLiteIntArray* a) {
57     if (a) TfLiteIntArrayFree(a);
58   }
59 };
60 
61 }  // namespace tflite
62 
63 #endif  // TENSORFLOW_LITE_UTIL_H_
64