1 /* Copyright 2019 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 module provides helper functions for testing the interaction between 17 // control flow ops and subgraphs. 18 // For convenience, we mostly only use `kTfLiteInt32` in this module. 19 20 #ifndef TENSORFLOW_LITE_KERNELS_SUBGRAPH_TEST_UTIL_H_ 21 #define TENSORFLOW_LITE_KERNELS_SUBGRAPH_TEST_UTIL_H_ 22 23 #include <stdint.h> 24 25 #include <memory> 26 #include <vector> 27 28 #include <gtest/gtest.h> 29 #include "tensorflow/lite/core/subgraph.h" 30 #include "tensorflow/lite/interpreter.h" 31 32 namespace tflite { 33 namespace subgraph_test_util { 34 35 class SubgraphBuilder { 36 public: 37 ~SubgraphBuilder(); 38 39 // Build a subgraph with a single Add op. 40 // 2 inputs. 1 output. 41 void BuildAddSubgraph(Subgraph* subgraph); 42 43 // Build a subgraph with a single Mul op. 44 // 2 inputs. 1 output. 45 void BuildMulSubgraph(Subgraph* subgraph); 46 47 // Build a subgraph with a single Pad op. 48 // 2 inputs. 1 output. 49 void BuildPadSubgraph(Subgraph* subgraph); 50 51 // Build a subgraph with a single If op. 52 // 3 inputs: 53 // The 1st input is condition with boolean type. 54 // The 2nd and 3rd inputs are feed input the branch subgraphs. 55 // 1 output. 56 void BuildIfSubgraph(Subgraph* subgraph); 57 58 // Build a subgraph with a single Less op. 59 // The subgraph is used as the condition subgraph for testing `While` op. 60 // 2 inputs: 61 // The 1st input is a counter with `kTfLiteInt32` type. 62 // The 2nd input is ignored in this subgraph. 63 // 1 output with `kTfLiteBool` type. 64 // Equivalent to (input < rhs). 65 void BuildLessEqualCondSubgraph(Subgraph* subgraph, int rhs); 66 67 // An accumulate loop body subgraph. Used to produce triangle number 68 // sequence. 2 inputs and 2 outputs 69 // Equivalent to (counter, value) -> (counter + 1, counter + 1 + value) 70 void BuildAccumulateLoopBodySubgraph(Subgraph* subgraph); 71 72 // A pad loop body subgraph. When used in a loop it will repeatively enlarge 73 // the 74 // tensor. 75 // 2 inputs and 2 outputs. 76 // Equivalent to (counter, value) -> (counter + 1, tf.pad(value, padding)) 77 // Note the padding is created as a constant tensor. 78 void BuildPadLoopBodySubgraph(Subgraph* subgraph, 79 const std::vector<int> padding); 80 81 // Build a subgraph with a single While op. 82 // 2 inputs, 2 outputs. 83 void BuildWhileSubgraph(Subgraph* subgraph); 84 85 // Build a subgraph that assigns a random value to a variable. 86 // No input/output. 87 void BuildAssignRandomValueToVariableSubgraph(Subgraph* graph); 88 89 // Build a subgraph with CallOnce op and ReadVariable op. 90 // No input and 1 output. 91 void BuildCallOnceAndReadVariableSubgraph(Subgraph* graph); 92 93 // Build a subgraph with CallOnce op, ReadVariable op and Add op. 94 // No input and 1 output. 95 void BuildCallOnceAndReadVariablePlusOneSubgraph(Subgraph* graph); 96 97 // Build a subgraph with a single Less op. 98 // The subgraph is used as the condition subgraph for testing `While` op. 99 // 3 inputs: 100 // The 1st and 2nd inputs are string tensors, which will be ignored. 101 // The 3rd input is an integner value as a counter in this subgraph. 102 // 1 output with `kTfLiteBool` type. 103 // Equivalent to (int_val < rhs). 104 void BuildLessEqualCondSubgraphWithDynamicTensor(Subgraph* subgraph, int rhs); 105 106 // Build a subgraph with a single While op, which has 3 inputs and 3 outputs. 107 // This subgraph is used for creating/invoking dynamic allocated tensors based 108 // on string tensors. 109 // Equivalent to (str1, str2, int_val) -> 110 // (str1, Fill(str1, int_val + 1), int_val + 1). 111 void BuildBodySubgraphWithDynamicTensor(Subgraph* subgraph); 112 113 // Build a subgraph with a single While op, that contains 3 inputs and 3 114 // outputs (str1, str2, int_val). 115 void BuildWhileSubgraphWithDynamicTensor(Subgraph* subgraph); 116 117 private: 118 void CreateConstantInt32Tensor(Subgraph* subgraph, int tensor_index, 119 const std::vector<int>& shape, 120 const std::vector<int>& data); 121 std::vector<void*> buffers_; 122 }; 123 124 class ControlFlowOpTest : public ::testing::Test { 125 public: ControlFlowOpTest()126 ControlFlowOpTest() 127 : interpreter_(new Interpreter), builder_(new SubgraphBuilder) {} 128 ~ControlFlowOpTest()129 ~ControlFlowOpTest() override { 130 interpreter_.reset(); 131 builder_.reset(); 132 } 133 134 protected: 135 std::unique_ptr<Interpreter> interpreter_; 136 std::unique_ptr<SubgraphBuilder> builder_; 137 }; 138 139 // Fill a `TfLiteTensor` with a 32-bits integer vector. 140 // Preconditions: 141 // * The tensor must have `kTfLiteInt32` type. 142 // * The tensor must be allocated. 143 // * The element count of the tensor must be equal to the length or 144 // the vector. 145 void FillIntTensor(TfLiteTensor* tensor, const std::vector<int32_t>& data); 146 147 // Fill a `TfLiteTensor` with a string value. 148 // Preconditions: 149 // * The tensor must have `kTfLitString` type. 150 void FillScalarStringTensor(TfLiteTensor* tensor, const std::string& data); 151 152 // Check if the scalar string data of a tensor is as expected. 153 void CheckScalarStringTensor(const TfLiteTensor* tensor, 154 const std::string& data); 155 156 // Check if the shape and string data of a tensor is as expected. 157 void CheckStringTensor(const TfLiteTensor* tensor, 158 const std::vector<int>& shape, 159 const std::vector<std::string>& data); 160 161 // Check if the shape and int32 data of a tensor is as expected. 162 void CheckIntTensor(const TfLiteTensor* tensor, const std::vector<int>& shape, 163 const std::vector<int32_t>& data); 164 // Check if the shape and bool data of a tensor is as expected. 165 void CheckBoolTensor(const TfLiteTensor* tensor, const std::vector<int>& shape, 166 const std::vector<bool>& data); 167 168 } // namespace subgraph_test_util 169 } // namespace tflite 170 171 #endif // TENSORFLOW_LITE_KERNELS_SUBGRAPH_TEST_UTIL_H_ 172