| 34 | class Slice : public NodeShader { |
| 35 | public: |
| 36 | Status GenerateCode(const GenerationContext& ctx, |
| 37 | GeneratedCode* generated_code) const final { |
| 38 | auto output = ctx.graph->FindOutputs(ctx.node->id)[0]; |
| 39 | |
| 40 | auto attr = |
| 41 | absl::any_cast<const SliceAttributes&>(ctx.node->operation.attributes); |
| 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", output->tensor.shape.c}, |
| 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 " |
nothing calls this directly
no test coverage detected