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