xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/delegates/gpu/gl/kernels/slice.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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/slice.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 #include "tensorflow/lite/delegates/gpu/gl/variable.h"
31 
32 namespace tflite {
33 namespace gpu {
34 namespace gl {
35 namespace {
36 
37 class Slice : public NodeShader {
38  public:
GenerateCode(const GenerationContext & ctx,GeneratedCode * generated_code) const39   absl::Status GenerateCode(const GenerationContext& ctx,
40                             GeneratedCode* generated_code) const final {
41     const auto& attr = std::any_cast<const SliceAttributes&>(ctx.op_attr);
42 
43     const int4 channels(attr.starts.c, attr.strides.c, attr.ends.c, 0);
44     const int4 heights(attr.starts.h, attr.strides.h, attr.ends.h, 0);
45     const int4 widths(attr.starts.w, attr.strides.w, attr.ends.w, 0);
46 
47     std::vector<Variable> parameters = {
48         {"channels", channels},
49         {"heights", heights},
50         {"widths", widths},
51         {"dst_size", static_cast<int>(ctx.output_shapes[0][3])},
52     };
53 
54     std::string code;
55     code += "      ivec2 offset;\n";
56     if (attr.strides.w > 0) {
57       code += "      offset.x = $widths.x$;\n";
58     } else {
59       if (attr.ends.w > 0) {
60         code += "      offset.x = $widths.z$;\n";
61       } else {
62         code += "      offset.x = $src_size.x$ + $widths.z$;\n";
63       }
64     }
65     if (attr.strides.h > 0) {
66       code += "      offset.y = $heights.x$;\n";
67     } else {
68       if (attr.ends.h > 0) {
69         code += "      offset.y = $heights.z$;\n";
70       } else {
71         code += "      offset.y = src_height + $heights.z$;\n";
72       }
73     }
74     code += "      ivec2 stride = ivec2($widths.y$, $heights.y$);\n";
75     code += "      ivec2 coord = offset + ivec2(gid.xy) * stride;\n";
76     code += "      bool outside = false;\n";
77     code += "      int step = gid.z * 4;\n";
78     code += "      int buffer_index = 0;\n";
79     code += "      int addr = 0;\n";
80     for (int i = 0; i < 4; i++) {
81       code += "      addr = step * $channels.y$;\n";
82       if (attr.strides.c > 0) {
83         code += "      addr += $channels.x$;\n";
84       } else {
85         if (attr.ends.c > 0) {
86           code += "      addr += $channels.z$;\n";
87         } else {
88           code += "      addr += src_channels + $channels.z$;\n";
89         }
90       }
91       code += "      if (step < $dst_size$) {\n        value_0[" +
92               std::to_string(i) +
93               "] = $input_data_0[coord.x, coord.y, addr / 4]$[addr % 4];\n     "
94               " }\n";
95       if (i != 3) {
96         code += "      step++;\n";
97       }
98     }
99 
100     *generated_code = {
101         /*parameters=*/std::move(parameters),
102         /*objects=*/{},
103         /*shared_variables=*/{},
104         /*workload=*/uint3(),
105         /*workgroup=*/uint3(),
106         /*source_code=*/std::move(code),
107         /*input=*/IOStructure::ONLY_DEFINITIONS,
108         /*output=*/IOStructure::AUTO,
109     };
110     return absl::OkStatus();
111   }
112 };
113 
114 }  // namespace
115 
NewSliceNodeShader()116 std::unique_ptr<NodeShader> NewSliceNodeShader() {
117   return std::make_unique<Slice>();
118 }
119 
120 }  // namespace gl
121 }  // namespace gpu
122 }  // namespace tflite
123