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 absl::Status GenerateCode(const GenerationContext& ctx,
38 GeneratedCode* generated_code) const final {
39 const auto& attr =
40 absl::any_cast<const ElementwiseAttributes&>(ctx.op_attr);
41 auto adds = absl::get_if<Tensor<Linear, DataType::FLOAT32>>(&attr.param);
42 auto scalar = absl::get_if<float>(&attr.param);
43
44 const auto* hwc_tensor =
45 absl::get_if<Tensor<HWC, DataType::FLOAT32>>(&attr.param);
46 if (hwc_tensor) {
47 return absl::UnimplementedError(
48 "Add does not support HWC constant tensor");
49 }
50
51 if (!adds && !scalar) {
52 // check if it is a broadcast
53 if (ctx.input_shapes.size() == 2 &&
54 ctx.input_shapes[0] != ctx.input_shapes[1] &&
55 ctx.input_shapes[1][1] == 1 && ctx.input_shapes[1][2] == 1 &&
56 ctx.input_shapes[0][3] == ctx.input_shapes[1][3]) {
57 // TODO(b/147771327): investigate why input_data_1[gid.z] worked before
58 *generated_code = {
59 /*parameters=*/{},
60 /*objects=*/{},
61 /*shared_variables=*/{},
62 /*workload=*/uint3(),
63 /*workgroup=*/uint3(),
64 /*source_code=*/
65 "value_0 = $input_data_0[gid.x, gid.y, gid.z]$ + "
66 " $input_data_1[0, 0, gid.z]$;",
67 /*input=*/IOStructure::ONLY_DEFINITIONS,
68 /*output=*/IOStructure::AUTO,
69 };
70 return absl::OkStatus();
71 }
72
73 std::string code = "value_0 = value_0";
74 for (int index = 1; index < ctx.input_shapes.size(); ++index) {
75 if (ctx.input_shapes[index] != ctx.input_shapes[0]) {
76 return absl::InvalidArgumentError("Shapes are not equal");
77 }
78 absl::StrAppend(&code, " + value_", index);
79 }
80 absl::StrAppend(&code, ";");
81 *generated_code = {
82 /*parameters=*/{},
83 /*objects=*/{},
84 /*shared_variables=*/{},
85 /*workload=*/uint3(),
86 /*workgroup=*/uint3(),
87 /*source_code=*/std::move(code),
88 /*input=*/IOStructure::AUTO,
89 /*output=*/IOStructure::AUTO,
90 };
91 return absl::OkStatus();
92 }
93
94 if (scalar) {
95 *generated_code = {
96 /*parameters=*/{{"scalar", *scalar}},
97 /*objects=*/{},
98 /*shared_variables=*/{},
99 /*workload=*/uint3(),
100 /*workgroup=*/uint3(),
101 /*source_code=*/"value_0 += $scalar$;",
102 /*input=*/IOStructure::AUTO,
103 /*output=*/IOStructure::AUTO,
104 };
105 } else {
106 *generated_code = {
107 /*parameters=*/{},
108 /*objects=*/{{"add_buffer", MakeReadonlyObject(adds->data)}},
109 /*shared_variables=*/{},
110 // Declare workload explicitly because shader depends on gid.z.
111 /*workload=*/
112 uint3(ctx.input_shapes[0][2], ctx.input_shapes[0][1],
113 DivideRoundUp(ctx.input_shapes[0][3], 4)),
114 /*workgroup=*/uint3(),
115 /*source_code=*/"value_0 += $add_buffer[gid.z]$;",
116 /*input=*/IOStructure::AUTO,
117 /*output=*/IOStructure::AUTO,
118 };
119 }
120
121 return absl::OkStatus();
122 }
123 };
124
125 } // namespace
126
NewAddNodeShader()127 std::unique_ptr<NodeShader> NewAddNodeShader() {
128 return absl::make_unique<Add>();
129 }
130
131 } // namespace gl
132 } // namespace gpu
133 } // namespace tflite
134