• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <stdint.h>
17 
18 #include <memory>
19 #include <vector>
20 
21 #include <gtest/gtest.h>
22 #include "tensorflow/lite/interpreter.h"
23 #include "tensorflow/lite/kernels/kernel_util.h"
24 #include "tensorflow/lite/kernels/subgraph_test_util.h"
25 
26 namespace tflite {
27 
28 using subgraph_test_util::CheckIntTensor;
29 using subgraph_test_util::ControlFlowOpTest;
30 using subgraph_test_util::FillIntTensor;
31 
32 namespace {
33 
34 // A simple test that performs `ADD` if condition is true, and `MUL` otherwise.
35 // The computation is: `cond ? a + b : a * b`.
36 class SimpleIfTest : public ControlFlowOpTest {
37  protected:
SetUp()38   void SetUp() override {
39     interpreter_->AddSubgraphs(2);
40     builder_->BuildAddSubgraph(interpreter_->subgraph(1));
41     builder_->BuildMulSubgraph(interpreter_->subgraph(2));
42     builder_->BuildIfSubgraph(&interpreter_->primary_subgraph());
43 
44     interpreter_->ResizeInputTensor(interpreter_->inputs()[0], {1});
45     interpreter_->ResizeInputTensor(interpreter_->inputs()[1], {2});
46     interpreter_->ResizeInputTensor(interpreter_->inputs()[2], {1, 2});
47     ASSERT_EQ(interpreter_->AllocateTensors(), kTfLiteOk);
48 
49     FillIntTensor(interpreter_->tensor(interpreter_->inputs()[1]), {5, 7});
50     FillIntTensor(interpreter_->tensor(interpreter_->inputs()[2]), {1, 2});
51   }
52 };
53 
TEST_F(SimpleIfTest,TestIfTrue)54 TEST_F(SimpleIfTest, TestIfTrue) {
55   interpreter_->typed_input_tensor<bool>(0)[0] = true;
56   ASSERT_EQ(interpreter_->Invoke(), kTfLiteOk);
57   TfLiteTensor* output = interpreter_->tensor(interpreter_->outputs()[0]);
58   CheckIntTensor(output, {1, 2}, {6, 9});
59 }
60 
TEST_F(SimpleIfTest,TestIfFalse)61 TEST_F(SimpleIfTest, TestIfFalse) {
62   interpreter_->typed_input_tensor<bool>(0)[0] = false;
63   ASSERT_EQ(interpreter_->Invoke(), kTfLiteOk);
64   TfLiteTensor* output = interpreter_->tensor(interpreter_->outputs()[0]);
65   CheckIntTensor(output, {1, 2}, {5, 14});
66 }
67 
68 // Test IF op using subgraphs with dynamically sized outputs.
69 // The computation is: `cond ? a + b : pad(a, b)`.
70 class DynamicSubgraphIfTest : public ControlFlowOpTest {
71  protected:
SetUp()72   void SetUp() override {
73     interpreter_->AddSubgraphs(2);
74     builder_->BuildAddSubgraph(interpreter_->subgraph(1));
75     builder_->BuildPadSubgraph(interpreter_->subgraph(2));
76     builder_->BuildIfSubgraph(&interpreter_->primary_subgraph());
77 
78     interpreter_->ResizeInputTensor(interpreter_->inputs()[0], {1});
79     interpreter_->ResizeInputTensor(interpreter_->inputs()[1], {2});
80     interpreter_->ResizeInputTensor(interpreter_->inputs()[2], {1, 2});
81     ASSERT_EQ(interpreter_->AllocateTensors(), kTfLiteOk);
82 
83     FillIntTensor(interpreter_->tensor(interpreter_->inputs()[1]), {5, 7});
84     FillIntTensor(interpreter_->tensor(interpreter_->inputs()[2]), {1, 2});
85   }
86 };
87 
TEST_F(DynamicSubgraphIfTest,TestIfTrue)88 TEST_F(DynamicSubgraphIfTest, TestIfTrue) {
89   interpreter_->typed_input_tensor<bool>(0)[0] = true;
90   ASSERT_EQ(interpreter_->Invoke(), kTfLiteOk);
91   TfLiteTensor* output = interpreter_->tensor(interpreter_->outputs()[0]);
92   // Even if the true branch has a static type output, the output of the
93   // if op is dynamic because the other branch has dynamic output.
94   EXPECT_TRUE(IsDynamicTensor(output));
95   CheckIntTensor(output, {1, 2}, {6, 9});
96 }
97 
TEST_F(DynamicSubgraphIfTest,TestIfFalse)98 TEST_F(DynamicSubgraphIfTest, TestIfFalse) {
99   interpreter_->typed_input_tensor<bool>(0)[0] = false;
100   ASSERT_EQ(interpreter_->Invoke(), kTfLiteOk);
101   TfLiteTensor* output = interpreter_->tensor(interpreter_->outputs()[0]);
102   // The false branch has dynamic output.
103   EXPECT_TRUE(IsDynamicTensor(output));
104   CheckIntTensor(output, {5}, {0, 5, 7, 0, 0});
105 }
106 
107 }  // namespace
108 }  // namespace tflite
109