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/reshape.h"
17
18 #include <algorithm>
19 #include <any>
20 #include <cstdint>
21 #include <cstring>
22 #include <memory>
23 #include <string>
24 #include <utility>
25 #include <vector>
26
27 #include "absl/memory/memory.h"
28 #include "tensorflow/lite/delegates/gpu/common/status.h"
29 #include "tensorflow/lite/delegates/gpu/common/types.h"
30
31 namespace tflite {
32 namespace gpu {
33 namespace gl {
34 namespace {
35
36 class Reshape : public NodeShader {
37 public:
GenerateCode(const GenerationContext & ctx,GeneratedCode * generated_code) const38 absl::Status GenerateCode(const GenerationContext& ctx,
39 GeneratedCode* generated_code) const final {
40 if (ctx.input_shapes[0][1] * ctx.input_shapes[0][2] *
41 ctx.input_shapes[0][3] !=
42 ctx.output_shapes[0][1] * ctx.output_shapes[0][2] *
43 ctx.output_shapes[0][3]) {
44 return absl::InvalidArgumentError(
45 "Number of elements in input & output tensors don't match.");
46 }
47 const auto& attr = std::any_cast<const ReshapeAttributes&>(ctx.op_attr);
48 if (attr.new_shape.h != ctx.output_shapes[0][1] ||
49 attr.new_shape.w != ctx.output_shapes[0][2] ||
50 attr.new_shape.c != ctx.output_shapes[0][3]) {
51 return absl::InvalidArgumentError(
52 "Dimensions for output does not match new_shape attribute");
53 }
54
55 std::string code = R"(
56 int input_ch_w = $input_channels$ * $input_data_0_w$;
57 int output_ch_w = $output_channels$ * $output_data_0_w$;
58 for (int i = 0; i < 4; ++i) {
59 int dst_channel = gid.z * 4 + i;
60 if (dst_channel >= $output_channels$) {
61 continue;
62 }
63 int p = dst_channel + $output_channels$ * gid.x + output_ch_w * gid.y;
64 int src_y = p / input_ch_w;
65 int src_x = (p % input_ch_w) / $input_channels$;
66 int src_z = (p % input_ch_w) % $input_channels$;
67 int src_layer = src_z / 4;
68 int src_channel = src_z % 4;
69 value_0[i] = $input_data_0[src_x, src_y, src_layer]$[src_channel];
70 }
71 )";
72 *generated_code = {
73 /*parameters=*/{
74 {"input_data_0_w", static_cast<int>(ctx.input_shapes[0][2])},
75 {"input_channels", static_cast<int>(ctx.input_shapes[0][3])},
76 {"output_data_0_w", static_cast<int>(ctx.output_shapes[0][2])},
77 {"output_channels", static_cast<int>(ctx.output_shapes[0][3])},
78 },
79 /*objects=*/{},
80 /*shared_variables=*/{},
81 /*workload=*/uint3(),
82 /*workgroup=*/uint3(),
83 /*source_code=*/std::move(code),
84 /*input=*/IOStructure::ONLY_DEFINITIONS,
85 /*output=*/IOStructure::AUTO,
86 };
87 return absl::OkStatus();
88 }
89 };
90
91 } // namespace
92
NewReshapeNodeShader()93 std::unique_ptr<NodeShader> NewReshapeNodeShader() {
94 return std::make_unique<Reshape>();
95 }
96
97 } // namespace gl
98 } // namespace gpu
99 } // namespace tflite
100