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 <gtest/gtest.h> 24 #include "tensorflow/lite/core/subgraph.h" 25 #include "tensorflow/lite/interpreter.h" 26 27 namespace tflite { 28 namespace subgraph_test_util { 29 30 // TODO(ycling): This file should be renamed as 31 // `control_flow_test_util` to avoid confusion. I'll do it immediately 32 // in a separated change. 33 class SubgraphBuilder { 34 public: 35 ~SubgraphBuilder(); 36 37 // Build a subgraph with a single Add op. 38 // 2 inputs. 1 output. 39 void BuildAddSubgraph(Subgraph* subgraph); 40 41 // Build a subgraph with a single Mul op. 42 // 2 inputs. 1 output. 43 void BuildMulSubgraph(Subgraph* subgraph); 44 45 // Build a subgraph with a single Pad op. 46 // 2 inputs. 1 output. 47 void BuildPadSubgraph(Subgraph* subgraph); 48 49 // Build a subgraph with a single If op. 50 // 3 inputs: 51 // The 1st input is condition with boolean type. 52 // The 2nd and 3rd inputs are feed input the branch subgraphs. 53 // 1 output. 54 void BuildIfSubgraph(Subgraph* subgraph); 55 56 // Build a subgraph with a single Less op. 57 // The subgraph is used as the condition subgraph for testing `While` op. 58 // 2 inputs: 59 // The 1st input is a counter with `kTfLiteInt32` type. 60 // The 2nd input is ignored in this subgraph. 61 // 1 output with `kTfLiteBool` type. 62 // Equivalent to (input < rhs). 63 void BuildLessEqualCondSubgraph(Subgraph* subgraph, int rhs); 64 65 // An accumulate loop body subgraph. Used to produce triangle number 66 // seqeuence. 2 inputs and 2 outpus 67 // Equivalent to (counter, value) -> (counter + 1, counter + 1 + value) 68 void BuildAccumulateLoopBodySubgraph(Subgraph* subgraph); 69 70 // A pad loop body subgraph. When used in a loop it will repeatively enlarge 71 // the 72 // tensor. 73 // 2 inputs and 2 outputs. 74 // Equivalent to (counter, value) -> (counter + 1, tf.pad(value, padding)) 75 // Note the padding is created as a constant tensor. 76 void BuildPadLoopBodySubgraph(Subgraph* subgraph, 77 const std::vector<int> padding); 78 79 // Build a subgraph with a single While op. 80 // 2 inputs, 2 outputs. 81 void BuildWhileSubgraph(Subgraph* subgraph); 82 83 private: 84 void CreateConstantInt32Tensor(Subgraph* subgraph, int tensor_index, 85 const std::vector<int>& shape, 86 const std::vector<int>& data); 87 std::vector<void*> buffers_; 88 }; 89 90 class ControlFlowOpTest : public ::testing::Test { 91 public: ControlFlowOpTest()92 ControlFlowOpTest() 93 : interpreter_(new Interpreter), builder_(new SubgraphBuilder) {} 94 ~ControlFlowOpTest()95 ~ControlFlowOpTest() override { 96 interpreter_.reset(); 97 builder_.reset(); 98 } 99 100 protected: 101 std::unique_ptr<Interpreter> interpreter_; 102 std::unique_ptr<SubgraphBuilder> builder_; 103 }; 104 105 // Fill a `TfLiteTensor` with a 32-bits integer vector. 106 // Preconditions: 107 // * The tensor must have `kTfLiteInt32` type. 108 // * The tensor must be allocated. 109 // * The element count of the tensor must be equal to the length or 110 // the vector. 111 void FillIntTensor(TfLiteTensor* tensor, const std::vector<int32_t>& data); 112 113 // Check if the shape and int32 data of a tensor is as expected. 114 void CheckIntTensor(const TfLiteTensor* tensor, const std::vector<int>& shape, 115 const std::vector<int32_t>& data); 116 // Check if the shape and bool data of a tensor is as expected. 117 void CheckBoolTensor(const TfLiteTensor* tensor, const std::vector<int>& shape, 118 const std::vector<bool>& data); 119 120 } // namespace subgraph_test_util 121 } // namespace tflite 122 123 #endif // TENSORFLOW_LITE_KERNELS_SUBGRAPH_TEST_UTIL_H_ 124