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 "tensorflow/lite/delegates/gpu/gl/compiler/fuse_inline.h"
17 
18 #include <algorithm>
19 #include <iterator>
20 #include <string>
21 #include <vector>
22 
23 #include "absl/strings/str_cat.h"
24 #include "absl/strings/string_view.h"
25 #include "absl/types/any.h"
26 #include "tensorflow/lite/delegates/gpu/common/status.h"
27 #include "tensorflow/lite/delegates/gpu/common/types.h"
28 #include "tensorflow/lite/delegates/gpu/gl/compiler/compiled_node.h"
29 #include "tensorflow/lite/delegates/gpu/gl/compiler/shader_code.h"
30 #include "tensorflow/lite/delegates/gpu/gl/node_shader.h"
31 
32 namespace tflite {
33 namespace gpu {
34 namespace gl {
35 
ApplyToNodesSequence(const std::vector<Node * > & sequence,GraphFloat32 * graph)36 TransformResult FuseAutoOutputWithInline::ApplyToNodesSequence(
37     const std::vector<Node*>& sequence, GraphFloat32* graph) {
38   Node* node1 = sequence.front();
39   Node* node2 = sequence.back();
40   auto& attr1 =
41       absl::any_cast<CompiledNodeAttributes&>(node1->operation.attributes);
42   auto& attr2 =
43       absl::any_cast<CompiledNodeAttributes&>(node2->operation.attributes);
44 
45   if (attr1.code.output != IOStructure::AUTO ||
46       graph->FindInputs(node2->id).size() != 1 ||
47       graph->FindOutputs(node2->id).size() != 1 ||
48       attr2.code.output != IOStructure::AUTO ||
49       attr2.code.input != IOStructure::AUTO ||
50       (attr1.code.workload != attr2.code.workload &&
51        uint3() != attr2.code.workload) ||
52       graph->FindOutputs(node1->id).size() !=
53           graph->FindInputs(node2->id).size()) {
54     return {TransformStatus::SKIPPED, ""};
55   }
56 
57   // Check if the code was not fused yet, and wrap source code into {}.
58   if (node1->operation.type.find('+') == std::string::npos) {
59     attr1.code.source_code =
60         absl::StrCat("\n{\n", attr1.code.source_code, "\n}\n");
61   }
62   if (!MergeCode(&attr2, &attr1).ok()) {
63     return {TransformStatus::INVALID, "Unable to merge two nodes"};
64   }
65   absl::StrAppend(&attr1.code.source_code, "{\n", attr2.code.source_code,
66                   "\n}");
67   node1->operation.type += "+" + node2->operation.type;
68 
69   if (!RemoveFollowingNode(graph, node2, node1).ok()) {
70     return {TransformStatus::INVALID,
71             "Unable to remove node " + std::to_string(node2->id)};
72   }
73   return {TransformStatus::APPLIED, ""};
74 }
75 
76 }  // namespace gl
77 }  // namespace gpu
78 }  // namespace tflite
79