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 <stddef.h> 25 26 #include <initializer_list> 27 #include <memory> 28 #include <string> 29 #include <vector> 30 31 #include "tensorflow/lite/c/common.h" 32 33 namespace tflite { 34 35 // Memory allocation parameter used by ArenaPlanner. 36 // Clients (such as delegates) might look at this to ensure interop between 37 // TFLite memory & hardware buffers. 38 // NOTE: This only holds for tensors allocated on the arena. 39 constexpr int kDefaultTensorAlignment = 64; 40 41 // The prefix of Flex op custom code. 42 // This will be matched agains the `custom_code` field in `OperatorCode` 43 // Flatbuffer Table. 44 // WARNING: This is an experimental API and subject to change. 45 constexpr char kFlexCustomCodePrefix[] = "Flex"; 46 47 // Checks whether the prefix of the custom name indicates the operation is an 48 // Flex operation. 49 bool IsFlexOp(const char* custom_name); 50 51 // Converts a `std::vector` to a `TfLiteIntArray`. The caller takes ownership 52 // of the returned pointer. 53 TfLiteIntArray* ConvertVectorToTfLiteIntArray(const std::vector<int>& input); 54 55 // Converts an array (of the given size) to a `TfLiteIntArray`. The caller 56 // takes ownership of the returned pointer, and must make sure 'dims' has at 57 // least 'rank' elements. 58 TfLiteIntArray* ConvertArrayToTfLiteIntArray(const int rank, const int* dims); 59 60 // Checks whether a `TfLiteIntArray` and an int array have matching elements. 61 // The caller must guarantee that 'b' has at least 'b_size' elements. 62 bool EqualArrayAndTfLiteIntArray(const TfLiteIntArray* a, const int b_size, 63 const int* b); 64 65 size_t CombineHashes(std::initializer_list<size_t> hashes); 66 67 struct TfLiteIntArrayDeleter { operatorTfLiteIntArrayDeleter68 void operator()(TfLiteIntArray* a) { 69 if (a) TfLiteIntArrayFree(a); 70 } 71 }; 72 73 // Helper for Building TfLiteIntArray that is wrapped in a unique_ptr, 74 // So that it is automatically freed when it goes out of the scope. 75 std::unique_ptr<TfLiteIntArray, TfLiteIntArrayDeleter> BuildTfLiteIntArray( 76 const std::vector<int>& data); 77 78 // Populates the size in bytes of a type into `bytes`. Returns kTfLiteOk for 79 // valid types, and kTfLiteError otherwise. 80 TfLiteStatus GetSizeOfType(TfLiteContext* context, const TfLiteType type, 81 size_t* bytes); 82 83 // Creates a stub TfLiteRegistration instance with the provided 84 // `custom_op_name`. The op will fail if invoked, and is useful as a 85 // placeholder to defer op resolution. 86 // Note that `custom_op_name` must remain valid for the returned op's lifetime.. 87 TfLiteRegistration CreateUnresolvedCustomOp(const char* custom_op_name); 88 89 // Checks whether the provided op is an unresolved custom op. 90 bool IsUnresolvedCustomOp(const TfLiteRegistration& registration); 91 92 // Returns a descriptive name with the given op TfLiteRegistration. 93 std::string GetOpNameByRegistration(const TfLiteRegistration& registration); 94 95 // The prefix of a validation subgraph name. 96 // WARNING: This is an experimental API and subject to change. 97 constexpr char kValidationSubgraphNamePrefix[] = "VALIDATION:"; 98 99 // Checks whether the prefix of the subgraph name indicates the subgraph is a 100 // validation subgraph. 101 bool IsValidationSubgraph(const char* name); 102 } // namespace tflite 103 104 #endif // TENSORFLOW_LITE_UTIL_H_ 105