|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#version 450 core |
| 10 | + |
| 11 | +#define PRECISION ${PRECISION} |
| 12 | + |
| 13 | +#define VEC4_T ${texel_type(DTYPE)} |
| 14 | +#define T ${buffer_scalar_type(DTYPE)} |
| 15 | + |
| 16 | +#define USING_TEXTURE3D |
| 17 | + |
| 18 | +layout(std430) buffer; |
| 19 | + |
| 20 | +#include "indexing_utils.h" |
| 21 | + |
| 22 | +${layout_declare_tensor(B, "w", "t_out", DTYPE, "texture3d")} |
| 23 | + |
| 24 | +$for i in range(NUM_INPUTS): |
| 25 | + ${layout_declare_tensor(B, "r", "t_in" + str(i + 1), DTYPE, "texture3d")} |
| 26 | + |
| 27 | +${layout_declare_ubo(B, "int", "concat_dim")} |
| 28 | + |
| 29 | +$in_metadata = "" |
| 30 | +$for i in range(NUM_INPUTS): |
| 31 | + $in_metadata += "ivec4 in" + str(i + 1) + "_sizes;\n" |
| 32 | + |
| 33 | +layout(push_constant) uniform restrict Block { |
| 34 | + ivec4 out_sizes; |
| 35 | + ${in_metadata} |
| 36 | +}; |
| 37 | + |
| 38 | +${layout_declare_spec_const(C, "int", "out_layout", "DEFAULT_LAYOUT")} |
| 39 | +const lowp ivec4 out_axis_map = unhash_axis_map(out_layout); |
| 40 | +const lowp int out_packed_dim = unhash_packed_dim(out_layout); |
| 41 | + |
| 42 | +$for i in range(NUM_INPUTS): |
| 43 | + ${layout_declare_spec_const(C, "int", "in" + str(i+1) + "_layout", "DEFAULT_LAYOUT")} |
| 44 | + const lowp ivec4 in${i+1}_axis_map = unhash_axis_map(in${i+1}_layout); |
| 45 | + const lowp int in${i+1}_packed_dim = unhash_packed_dim(in${i+1}_layout); |
| 46 | + |
| 47 | +layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in; |
| 48 | + |
| 49 | +// Check if we can use the fast path (no texel merging required) |
| 50 | +bool can_use_fast_path() { |
| 51 | + // Fast path is possible when: |
| 52 | + // 1. The concat dimension is not the packed dimension, or |
| 53 | + // 2. The concat dimension is the packed dimension but both input tensors have dimensions |
| 54 | + // that are multiples of 4 along the packed dimension |
| 55 | + if (concat_dim != out_packed_dim) { |
| 56 | + return true; |
| 57 | + } |
| 58 | + |
| 59 | + // Check if all input tensors have dimensions that are multiples of 4 along the packed dimension |
| 60 | + bool all_concat_dim_size_multiple_of_4 = true; |
| 61 | + $for i in range(NUM_INPUTS): |
| 62 | + all_concat_dim_size_multiple_of_4 = |
| 63 | + all_concat_dim_size_multiple_of_4 && |
| 64 | + (in${i+1}_sizes[concat_dim] % 4 == 0); |
| 65 | + |
| 66 | + return all_concat_dim_size_multiple_of_4; |
| 67 | +} |
| 68 | + |
| 69 | +void main() { |
| 70 | + const ivec3 lpos = ivec3(gl_GlobalInvocationID); |
| 71 | + ivec4 out_tidx = lpos_to_tidx(lpos, out_sizes, out_axis_map.w, out_packed_dim); |
| 72 | + |
| 73 | + if (any(greaterThanEqual(out_tidx, out_sizes))) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + if (can_use_fast_path()) { |
| 78 | + // Fast path: No texel merging required |
| 79 | + ivec4 in_tidx = out_tidx; |
| 80 | + |
| 81 | + $for i in range(NUM_INPUTS): |
| 82 | + // For each input tensor, check if the tensor index is within bounds. If |
| 83 | + // so, read the texel from the input tensor and write it to the output |
| 84 | + if (in_tidx[concat_dim] < in${i+1}_sizes[concat_dim]) { |
| 85 | + const ivec3 in_pos = tidx_to_pos(in_tidx, in${i+1}_sizes, in${i+1}_axis_map, in${i+1}_packed_dim); |
| 86 | + const VEC4_T in_texel = load_texel(t_in${i+1}, in_pos); |
| 87 | + write_texel_lpos(t_out, lpos, in_texel, out_axis_map); |
| 88 | + return; |
| 89 | + } |
| 90 | + // Otherwise, adjust the index along the concat dimension and try the next |
| 91 | + // input tensor. |
| 92 | + else { |
| 93 | + in_tidx[concat_dim] -= in${i+1}_sizes[concat_dim]; |
| 94 | + } |
| 95 | + } |
| 96 | + else { |
| 97 | + // Slow path: Texel merging required |
| 98 | + VEC4_T out_texel = VEC4_T(0); |
| 99 | + |
| 100 | + // Process each element in the output texel individually |
| 101 | + for (int texel_i = 0; texel_i < 4; ++texel_i) { |
| 102 | + ivec4 curr_out_tidx = out_tidx; |
| 103 | + curr_out_tidx[out_packed_dim] += texel_i; |
| 104 | + |
| 105 | + // Skip if we're out of bounds |
| 106 | + if (curr_out_tidx[out_packed_dim] >= out_sizes[out_packed_dim]) { |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + ivec4 in_tidx = curr_out_tidx; |
| 111 | + $for i in range(NUM_INPUTS): |
| 112 | + // For each input tensor, check if the tensor index is within bounds. If |
| 113 | + // so, read the corresponding texel element from the input tensor and |
| 114 | + // write it to the output texel. |
| 115 | + if (in_tidx[concat_dim] < in${i+1}_sizes[concat_dim]) { |
| 116 | + const ivec4 in_posi = tidx_to_posi(in_tidx, in${i+1}_sizes, in${i+1}_axis_map, in${i+1}_packed_dim); |
| 117 | + out_texel[texel_i] = load_texel(t_in${i+1}, in_posi.xyz)[in_posi.w]; |
| 118 | + continue; |
| 119 | + } |
| 120 | + // Otherwise, adjust the index along the concat dimension and try the |
| 121 | + // next input tensor. |
| 122 | + else { |
| 123 | + in_tidx[concat_dim] -= in${i+1}_sizes[concat_dim]; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + write_texel_lpos(t_out, lpos, out_texel, out_axis_map); |
| 128 | + } |
| 129 | +} |
0 commit comments