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 #ifndef TENSORFLOW_LITE_DELEGATES_GPU_GL_COMPILER_FUSE_INPLACE_H_
17 #define TENSORFLOW_LITE_DELEGATES_GPU_GL_COMPILER_FUSE_INPLACE_H_
18 
19 #include <vector>
20 
21 #include "tensorflow/lite/delegates/gpu/common/model.h"
22 #include "tensorflow/lite/delegates/gpu/common/model_transformer.h"
23 
24 namespace tflite {
25 namespace gpu {
26 namespace gl {
27 
28 // Fuse two shaders where second shader is inline shader with the first.
29 // First shader should have a special symbol that defines a place where such
30 // fusion should be made and what variable needs to be changed.
31 // Second shader needs to operation with 'value_0' variable.
32 // Example:
33 //
34 //  First shader:
35 //   vec4 result = input_data_0.data[gid.x, gid.y, gid.z];
36 //   $inplace_update:result$
37 //   ...
38 //   output_data_0.data[1,2,3] = result;
39 //
40 //  Second shader:
41 //   value_0 = max(value_0, 0);
42 //
43 //  Fused shader:
44 //   vec4 result = input_data_0.data[gid.x, gid.y, gid.z];
45 //   result = max(result, 0);
46 //   ...
47 //   output_data_0.data[1,2,3] = result;
48 //
49 class FuseInplaceUpdate : public SequenceTransformation {
50  public:
ExpectedSequenceLength()51   int ExpectedSequenceLength() const final { return 2; }
52 
53   TransformResult ApplyToNodesSequence(const std::vector<Node*>& sequence,
54                                        GraphFloat32* graph) final;
55 };
56 
57 // Removes all %inplace_update:XXX% strings from the code.
58 class RemoveUnusedInplaceUpdates : public NodeTransformation {
59  public:
60   TransformResult ApplyToNode(Node* node, GraphFloat32* graph) final;
61 };
62 
63 }  // namespace gl
64 }  // namespace gpu
65 }  // namespace tflite
66 
67 #endif  // TENSORFLOW_LITE_DELEGATES_GPU_GL_COMPILER_FUSE_INPLACE_H_
68