|
| 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 T ${buffer_scalar_type(DTYPE)} |
| 14 | +#define VEC4_T ${buffer_gvec_type(DTYPE, 4)} |
| 15 | + |
| 16 | +#define TILE_ROWS ${TILE_ROWS} |
| 17 | + |
| 18 | +#define NGROUPS 8 |
| 19 | +#define NWORKERS 8 |
| 20 | + |
| 21 | +${define_required_extensions(DTYPE)} |
| 22 | + |
| 23 | +$if WEIGHT_STORAGE == "buffer": |
| 24 | + ${define_required_extensions("int8")} |
| 25 | + |
| 26 | +#extension GL_EXT_control_flow_attributes : require |
| 27 | + |
| 28 | +layout(std430) buffer; |
| 29 | + |
| 30 | +${layout_declare_tensor(B, "w", "t_out", DTYPE, OUT_STORAGE, is_scalar_array=False)} |
| 31 | +${layout_declare_tensor(B, "r", "t_in", DTYPE, IN_STORAGE, is_scalar_array=False)} |
| 32 | +${layout_declare_tensor(B, "r", "t_weight", "int8", WEIGHT_STORAGE, is_scalar_array=False)} |
| 33 | +${layout_declare_tensor(B, "r", "t_scales", DTYPE, SCALES_STORAGE, is_scalar_array=False)} |
| 34 | + |
| 35 | +layout(push_constant) uniform restrict Block { |
| 36 | + ivec4 out_sizes; |
| 37 | + ivec4 in_sizes; |
| 38 | + ivec4 weight_sizes; |
| 39 | +}; |
| 40 | + |
| 41 | +layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in; |
| 42 | + |
| 43 | +shared VEC4_T partial_c[NGROUPS][NWORKERS][TILE_ROWS]; |
| 44 | + |
| 45 | +void main() { |
| 46 | + const uint out_row = gl_GlobalInvocationID.y * TILE_ROWS; |
| 47 | + const uint out_col = gl_GlobalInvocationID.x << 2; |
| 48 | + |
| 49 | + const int gid = int(gl_LocalInvocationID.x); // group id |
| 50 | + const int wid = int(gl_LocalInvocationID.z); // worker id |
| 51 | + |
| 52 | + if (out_col >= out_sizes.x || out_row >= out_sizes.y) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + VEC4_T a[TILE_ROWS]; |
| 57 | + VEC4_T b[4]; |
| 58 | + VEC4_T local_c[TILE_ROWS]; |
| 59 | + |
| 60 | + $if SCALES_STORAGE == "buffer": |
| 61 | + const VEC4_T scales = VEC4_T(t_scales[out_col >> 2]); |
| 62 | + $else: |
| 63 | + const VEC4_T scales = VEC4_T(texelFetch(t_scales, ivec3(out_col >> 2, 0, 0), 0)); |
| 64 | + |
| 65 | + [[unroll]] for (int i = 0; i < TILE_ROWS; ++i) { |
| 66 | + partial_c[gid][wid][i] = VEC4_T(0.0); |
| 67 | + } |
| 68 | + |
| 69 | + for (int pos = 4 * wid; pos < in_sizes.x; pos += (4 * NWORKERS)) { |
| 70 | + // Preload t_weight |
| 71 | + [[unroll]] for (int i = 0; i < 4; i++) { |
| 72 | + $if WEIGHT_STORAGE == "buffer": |
| 73 | + b[i] = t_weight[((pos + i) * weight_sizes.x + out_col) >> 2]; |
| 74 | + $else: |
| 75 | + b[i] = VEC4_T(texelFetch(t_weight, ivec2(out_col >> 2, pos + i), 0)); |
| 76 | + } |
| 77 | + // Preload t_in |
| 78 | + for (int i = 0; i < TILE_ROWS; i++) { |
| 79 | + $if IN_STORAGE == "buffer": |
| 80 | + a[i] = t_in[((out_row + i) * in_sizes.x + ((pos)) >> 2)]; |
| 81 | + $else: |
| 82 | + a[i] = VEC4_T(texelFetch(t_in, ivec3(pos >> 2, out_row + i, 0), 0)); |
| 83 | + } |
| 84 | + |
| 85 | + // Compute t_out...? |
| 86 | + [[unroll]] for (int i = 0; i < TILE_ROWS; ++i) { |
| 87 | + local_c[i] += a[i].x * b[0] |
| 88 | + + a[i].y * b[1] |
| 89 | + + a[i].z * b[2] |
| 90 | + + a[i].w * b[3]; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + [[unroll]] for (int i = 0; i < TILE_ROWS; ++i) { |
| 95 | + partial_c[gid][wid][i] = local_c[i]; |
| 96 | + } |
| 97 | + |
| 98 | + memoryBarrierShared(); |
| 99 | + barrier(); |
| 100 | + |
| 101 | + if (wid != 0) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + VEC4_T c[TILE_ROWS]; |
| 106 | + |
| 107 | + for (int row = 0; row < TILE_ROWS; ++row) { |
| 108 | + c[row] = VEC4_T(0.0); |
| 109 | + [[unroll]] for (int worker = 0; worker < NWORKERS; ++worker) { |
| 110 | + c[row] += partial_c[gid][worker][row]; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + [[unroll]] for (int i = 0; i < TILE_ROWS; ++i) { |
| 115 | + $if OUT_STORAGE == "buffer": |
| 116 | + if (out_row + i < out_sizes.y) { |
| 117 | + t_out[((out_row + i) * out_sizes.x + out_col) >> 2] = c[i] * scales; |
| 118 | + } |
| 119 | + $else: |
| 120 | + imageStore(t_out, ivec3(out_col >> 2, out_row + i, 0), c[i] * scales); |
| 121 | + } |
| 122 | +} |
0 commit comments