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/kernels/add.h"
17
18 #include <algorithm>
19 #include <cstdint>
20 #include <cstring>
21 #include <string>
22 #include <vector>
23
24 #include "absl/memory/memory.h"
25 #include "absl/strings/str_cat.h"
26 #include "tensorflow/lite/delegates/gpu/common/data_type.h"
27 #include "tensorflow/lite/delegates/gpu/common/status.h"
28 #include "tensorflow/lite/delegates/gpu/common/types.h"
29
30 namespace tflite {
31 namespace gpu {
32 namespace gl {
33 namespace {
34
35 class Add : public NodeShader {
36 public:
GenerateCode(const GenerationContext & ctx,GeneratedCode * generated_code) const37 Status GenerateCode(const GenerationContext& ctx,
38 GeneratedCode* generated_code) const final {
39 auto attr = absl::any_cast<AddAttributes>(ctx.node->operation.attributes);
40 auto adds = absl::get_if<Tensor<Linear, DataType::FLOAT32>>(&attr.param);
41 auto scalar = absl::get_if<float>(&attr.param);
42 auto inputs = ctx.graph->FindInputs(ctx.node->id);
43
44 if (!adds && !scalar) {
45 // check if it is a broadcast
46 if (inputs.size() == 2 &&
47 inputs[0]->tensor.shape != inputs[1]->tensor.shape &&
48 inputs[1]->tensor.shape.h == 1 && inputs[1]->tensor.shape.w == 1 &&
49 inputs[0]->tensor.shape.c == inputs[1]->tensor.shape.c) {
50 // TODO(b/147771327): investigate why input_data_1[gid.z] worked before
51 *generated_code = {
52 /*parameters=*/{},
53 /*objects=*/{},
54 /*shared_variables=*/{},
55 /*workload=*/uint3(),
56 /*workgroup=*/uint3(),
57 /*source_code=*/
58 "value_0 = $input_data_0[gid.x, gid.y, gid.z]$ + "
59 " $input_data_1[0, 0, gid.z]$;",
60 /*input=*/IOStructure::ONLY_DEFINITIONS,
61 /*output=*/IOStructure::AUTO,
62 };
63 return OkStatus();
64 }
65
66 std::string code = "value_0 = value_0";
67 for (int index = 1; index < inputs.size(); ++index) {
68 if (inputs[index]->tensor.shape != inputs[0]->tensor.shape) {
69 return InvalidArgumentError("Shapes are not equal");
70 }
71 absl::StrAppend(&code, " + value_", index);
72 }
73 absl::StrAppend(&code, ";");
74 *generated_code = {
75 /*parameters=*/{},
76 /*objects=*/{},
77 /*shared_variables=*/{},
78 /*workload=*/uint3(),
79 /*workgroup=*/uint3(),
80 /*source_code=*/std::move(code),
81 /*input=*/IOStructure::AUTO,
82 /*output=*/IOStructure::AUTO,
83 };
84 return OkStatus();
85 }
86
87 if (scalar) {
88 *generated_code = {
89 /*parameters=*/{{"scalar", *scalar}},
90 /*objects=*/{},
91 /*shared_variables=*/{},
92 /*workload=*/uint3(),
93 /*workgroup=*/uint3(),
94 /*source_code=*/"value_0 += $scalar$;",
95 /*input=*/IOStructure::AUTO,
96 /*output=*/IOStructure::AUTO,
97 };
98 } else {
99 auto shape = inputs[0]->tensor.shape;
100 *generated_code = {
101 /*parameters=*/{},
102 /*objects=*/{{"add_buffer", MakeReadonlyObject(adds->data)}},
103 /*shared_variables=*/{},
104 // Declare workload explicitly because shader depends on gid.z.
105 /*workload=*/
106 uint3(shape.w, shape.h, IntegralDivideRoundUp(shape.c, 4)),
107 /*workgroup=*/uint3(),
108 /*source_code=*/"value_0 += $add_buffer[gid.z]$;",
109 /*input=*/IOStructure::AUTO,
110 /*output=*/IOStructure::AUTO,
111 };
112 }
113
114 return OkStatus();
115 }
116 };
117
118 } // namespace
119
NewAddNodeShader()120 std::unique_ptr<NodeShader> NewAddNodeShader() {
121 return absl::make_unique<Add>();
122 }
123
124 } // namespace gl
125 } // namespace gpu
126 } // namespace tflite
127