1 /* Copyright 2020 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 "tensorflow/lite/delegates/gpu/gl/compiler/fuse_auto_input.h"
17 
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20 #include "absl/types/any.h"
21 #include "tensorflow/lite/delegates/gpu/gl/compiler/compiled_node.h"
22 
23 namespace tflite {
24 namespace gpu {
25 namespace gl {
26 namespace {
27 
TEST(FuseAutoInputTest,SkipsDiamond)28 TEST(FuseAutoInputTest, SkipsDiamond) {
29   //     v0
30   //   /    \
31   // n1      n2
32   // v1      v2
33   //   \    /
34   //     n3
35   //     v3
36   GraphFloat32 graph;
37   auto* v0 = graph.NewValue();
38   auto* v1 = graph.NewValue();
39   auto* v2 = graph.NewValue();
40   auto* v3 = graph.NewValue();
41   auto* n1 = graph.NewNode();
42   CompiledNodeAttributes a1;
43   a1.code.output = IOStructure::AUTO;
44   n1->operation.attributes = std::move(a1);
45   ASSERT_OK(graph.AddConsumer(n1->id, v0->id));
46   ASSERT_OK(graph.SetProducer(n1->id, v1->id));
47   auto* n2 = graph.NewNode();
48   CompiledNodeAttributes a2;
49   a2.code.output = IOStructure::AUTO;
50   n2->operation.attributes = std::move(a2);
51   ASSERT_OK(graph.AddConsumer(n2->id, v0->id));
52   ASSERT_OK(graph.SetProducer(n2->id, v2->id));
53   auto* n3 = graph.NewNode();
54   CompiledNodeAttributes a3;
55   a3.code.input = IOStructure::AUTO;
56   n3->operation.attributes = std::move(a3);
57   ASSERT_OK(graph.AddConsumer(n3->id, v1->id));
58   ASSERT_OK(graph.AddConsumer(n3->id, v2->id));
59   ASSERT_OK(graph.SetProducer(n3->id, v3->id));
60 
61   FuseAutoInput fuse_auto_input;
62   EXPECT_EQ(fuse_auto_input.ApplyToNode(n3, &graph).status,
63             TransformStatus::SKIPPED);
64 }
65 
TEST(FuseAutoInputTest,SkipsTriangle)66 TEST(FuseAutoInputTest, SkipsTriangle) {
67   // v0
68   // |  \
69   // |   n1
70   // |   v1
71   // |  /
72   // n2
73   // v2
74   GraphFloat32 graph;
75   auto* v0 = graph.NewValue();
76   auto* v1 = graph.NewValue();
77   auto* v2 = graph.NewValue();
78   auto* n1 = graph.NewNode();
79   CompiledNodeAttributes a1;
80   a1.code.output = IOStructure::AUTO;
81   n1->operation.attributes = std::move(a1);
82   ASSERT_OK(graph.AddConsumer(n1->id, v0->id));
83   ASSERT_OK(graph.SetProducer(n1->id, v1->id));
84   auto* n2 = graph.NewNode();
85   CompiledNodeAttributes a2;
86   a2.code.input = IOStructure::AUTO;
87   n2->operation.attributes = std::move(a2);
88   ASSERT_OK(graph.AddConsumer(n2->id, v0->id));
89   ASSERT_OK(graph.AddConsumer(n2->id, v1->id));
90   ASSERT_OK(graph.SetProducer(n2->id, v2->id));
91 
92   FuseAutoInput fuse_auto_input;
93   EXPECT_EQ(fuse_auto_input.ApplyToNode(n2, &graph).status,
94             TransformStatus::SKIPPED);
95 }
96 
97 }  // namespace
98 }  // namespace gl
99 }  // namespace gpu
100 }  // namespace tflite
101