1 /* Copyright 2021 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/tile.h"
17
18 #include <memory>
19 #include <string>
20 #include <utility>
21
22 #include "absl/memory/memory.h"
23 #include "absl/types/any.h"
24 #include "tensorflow/lite/delegates/gpu/common/operations.h"
25 #include "tensorflow/lite/delegates/gpu/common/status.h"
26 #include "tensorflow/lite/delegates/gpu/gl/node_shader.h"
27
28 namespace tflite {
29 namespace gpu {
30 namespace gl {
31 namespace {
32
33 class Tile : public NodeShader {
34 public:
GenerateCode(const GenerationContext & ctx,GeneratedCode * generated_code) const35 absl::Status GenerateCode(const GenerationContext& ctx,
36 GeneratedCode* generated_code) const final {
37 std::string code = R"(
38 for (int i = 0; i < 4; ++i) {
39 int dst_c = 4 * gid.z + i;
40 int src_x = gid.x % $input_data_w$;
41 int src_y = gid.y % $input_data_h$;
42 int src_c = dst_c % $input_data_c$;
43 value_0[i] = $input_data_0[src_x, src_y, src_c / 4]$[src_c % 4];
44 }
45 )";
46
47 *generated_code = {
48 /*parameters=*/{
49 {"input_data_h", static_cast<int>(ctx.input_shapes[0][1])},
50 {"input_data_w", static_cast<int>(ctx.input_shapes[0][2])},
51 {"input_data_c", static_cast<int>(ctx.input_shapes[0][3])},
52 },
53 /*objects=*/{},
54 /*shared_variables=*/{},
55 /*workload=*/uint3(),
56 /*workgroup=*/uint3(),
57 /*source_code=*/std::move(code),
58 /*input=*/IOStructure::ONLY_DEFINITIONS,
59 /*output=*/IOStructure::AUTO,
60 };
61 return absl::OkStatus();
62 }
63 };
64 } // namespace
65
NewTileNodeShader()66 std::unique_ptr<NodeShader> NewTileNodeShader() {
67 return std::make_unique<Tile>();
68 }
69
70 } // namespace gl
71 } // namespace gpu
72 } // namespace tflite
73