1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations 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
33namespace 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.
39constexpr 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.
45constexpr char kFlexCustomCodePrefix[] = "Flex";
46
47// Checks whether the prefix of the custom name indicates the operation is an
48// Flex operation.
49bool IsFlexOp(const char* custom_name);
50
51// Converts a `std::vector` to a `TfLiteIntArray`. The caller takes ownership
52// of the returned pointer.
53TfLiteIntArray* 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 'ndims' elements.
58TfLiteIntArray* ConvertArrayToTfLiteIntArray(const int ndims, 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.
62bool EqualArrayAndTfLiteIntArray(const TfLiteIntArray* a, const int b_size,
63 const int* b);
64
65size_t CombineHashes(std::initializer_list<size_t> hashes);
66
67struct TfLiteIntArrayDeleter {
68 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.
75std::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.
80TfLiteStatus 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..
87TfLiteRegistration CreateUnresolvedCustomOp(const char* custom_op_name);
88
89// Checks whether the provided op is an unresolved custom op.
90bool IsUnresolvedCustomOp(const TfLiteRegistration& registration);
91
92// Returns a descriptive name with the given op TfLiteRegistration.
93std::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.
97constexpr char kValidationSubgraphNamePrefix[] = "VALIDATION:";
98
99// Checks whether the prefix of the subgraph name indicates the subgraph is a
100// validation subgraph.
101bool IsValidationSubgraph(const char* name);
102
103// Multiply two sizes and return true if overflow occurred;
104// This is based off tensorflow/overflow.h but is simpler as we already
105// have unsigned numbers. It is also generalized to work where sizeof(size_t)
106// is not 8.
107TfLiteStatus MultiplyAndCheckOverflow(size_t a, size_t b, size_t* product);
108
109// Returns whether the TfLiteTensor is a resource or variant tensor.
110inline bool IsResourceOrVariant(const TfLiteTensor* tensor) {
111 return tensor->type == kTfLiteResource || tensor->type == kTfLiteVariant;
112}
113
114} // namespace tflite
115
116#endif // TENSORFLOW_LITE_UTIL_H_
117