Skip to content

[ET-VK] Introduce graph runtime shader library that enables dynamic shapes #2366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backends/vulkan/TARGETS
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ load(":targets.bzl", "define_common_targets")

oncall("executorch")

define_common_targets()
define_common_targets(is_fbcode = True)

runtime.python_library(
name = "vulkan_preprocess",
Expand Down
62 changes: 62 additions & 0 deletions backends/vulkan/runtime/graph/ops/glsl/all_shaders.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

binary_op:
parameter_names_with_default_values:
OPERATOR: X + A * Y
NDIM: 3
DTYPE: float
PACKING: CHANNELS_PACKED
generate_variant_forall:
DTYPE:
- VALUE: "half"
SUFFIX: "half"
- VALUE: "float"
SUFFIX: "float"
shader_variants:
- NAME: binary_add
- NAME: binary_sub
OPERATOR: X - Y
- NAME: binary_mul
OPERATOR: X * Y
- NAME: binary_div
OPERATOR: X / Y
- NAME: binary_pow
OPERATOR: pow(X, Y)
- NAME: binary_floor_divide
OPERATOR: floor(X / Y)

image_to_nchw:
parameter_names_with_default_values:
NDIM: 3
DTYPE: float
PACKING: CHANNELS_PACKED
generate_variant_forall:
DTYPE:
- VALUE: "half"
SUFFIX: "half"
- VALUE: "float"
SUFFIX: "float"
shader_variants:
- NAME: image3d_to_nchw_C_packed
- NAME: image2d_to_nchw_C_packed
NDIM: 2

nchw_to_image:
parameter_names_with_default_values:
NDIM: 3
DTYPE: float
PACKING: CHANNELS_PACKED
generate_variant_forall:
DTYPE:
- VALUE: "half"
SUFFIX: "half"
- VALUE: "float"
SUFFIX: "float"
shader_variants:
- NAME: nchw_to_image3d_C_packed
- NAME: nchw_to_image2d_C_packed
NDIM: 2
67 changes: 67 additions & 0 deletions backends/vulkan/runtime/graph/ops/glsl/binary_op.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

#version 450 core

#include "broadcasting_utils.h"
#include "indexing_utils.h"

#define PRECISION ${PRECISION}

#define OP(X, Y, A) ${OPERATOR}

layout(std430) buffer;

layout(set = 0, binding = 0, ${IMAGE_FORMAT[DTYPE]}) uniform PRECISION restrict writeonly ${IMAGE_T[NDIM][DTYPE]} image_out;
layout(set = 0, binding = 1) uniform PRECISION sampler3D image_in;
layout(set = 0, binding = 2) uniform PRECISION sampler3D image_other;

layout(set = 0, binding = 3) uniform PRECISION restrict OutSizes {
ivec4 data;
}
out_sizes;

layout(set = 0, binding = 4) uniform PRECISION restrict InSizes {
ivec4 data;
}
in_sizes;

layout(set = 0, binding = 5) uniform PRECISION restrict OtherSizes {
ivec4 data;
}
other_sizes;

layout(set = 0, binding = 6) uniform PRECISION restrict Alpha {
float data;
}
alpha;

layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in;

void main() {
const ivec3 pos = ivec3(gl_GlobalInvocationID);
const ivec4 coord = POS_TO_COORD_${PACKING}(pos, out_sizes.data);

if (any(greaterThanEqual(coord, out_sizes.data))) {
return;
}

ivec4 in_coord = out_coord_to_in_coord(coord, in_sizes.data);
vec4 in_texel = texelFetch(
image_in,
COORD_TO_POS_${PACKING}(in_coord, in_sizes.data),
0);

ivec4 other_coord = out_coord_to_in_coord(coord, other_sizes.data);
vec4 other_texel = texelFetch(
image_other,
COORD_TO_POS_${PACKING}(other_coord, other_sizes.data),
0);

imageStore(image_out, pos, OP(in_texel, other_texel, alpha.data));
}
17 changes: 17 additions & 0 deletions backends/vulkan/runtime/graph/ops/glsl/broadcasting_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

ivec4 out_coord_to_in_coord(const ivec4 out_coord, const ivec4 in_sizes) {
ivec4 in_coord = out_coord;
for (int i = 0; i < 4; ++i) {
if (in_sizes[i] == 1) {
in_coord[i] = 0;
}
}
return in_coord;
}
61 changes: 61 additions & 0 deletions backends/vulkan/runtime/graph/ops/glsl/image_to_nchw.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

#version 450 core

#define PRECISION ${PRECISION}

#include "indexing_utils.h"

layout(std430) buffer;

layout(set = 0, binding = 0) uniform PRECISION ${SAMPLER_T[NDIM][DTYPE]} image_in;
layout(set = 0, binding = 1) buffer PRECISION restrict writeonly Buffer {
${T[DTYPE]} data[];
}
buffer_out;

layout(set = 0, binding = 2) uniform PRECISION restrict GpuSizes {
ivec4 data;
}
gpu_sizes;

layout(set = 0, binding = 3) uniform PRECISION restrict CpuSizes {
ivec4 data;
}
cpu_sizes;

layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in;

void main() {
const ivec3 pos = ivec3(gl_GlobalInvocationID);
const ivec4 coord = POS_TO_COORD_${PACKING}(pos, gpu_sizes.data);

if (any(greaterThanEqual(coord, gpu_sizes.data))) {
return;
}

const ${VEC4_T[DTYPE]} intex = texelFetch(image_in, ${GET_POS[NDIM]("pos")}, 0);

const int base_index = COORD_TO_BUFFER_IDX(coord, cpu_sizes.data);
const ivec4 buf_indices =
base_index + ivec4(0, 1, 2, 3) * (gpu_sizes.data.x * gpu_sizes.data.y);

if (coord.z < cpu_sizes.data.z) {
buffer_out.data[buf_indices.x] = intex.x;
}
if (coord.z + 1 < cpu_sizes.data.z) {
buffer_out.data[buf_indices.y] = intex.y;
}
if (coord.z + 2 < cpu_sizes.data.z) {
buffer_out.data[buf_indices.z] = intex.z;
}
if (coord.z + 3 < cpu_sizes.data.z) {
buffer_out.data[buf_indices.w] = intex.w;
}
}
17 changes: 17 additions & 0 deletions backends/vulkan/runtime/graph/ops/glsl/indexing_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

#define POS_TO_COORD_CHANNELS_PACKED(pos, sizes) \
ivec4(pos.x, pos.y, (pos.z * 4) % sizes.z, (pos.z * 4) / sizes.z)

#define COORD_TO_POS_CHANNELS_PACKED(coord, sizes) \
ivec3(coord.x, coord.y, (coord.z + coord.w * sizes.z) / 4)

#define COORD_TO_BUFFER_IDX(coord, sizes) \
coord.x + coord.y* sizes.x + coord.z* sizes.y* sizes.x + \
coord.w* sizes.z* sizes.y* sizes.x;
61 changes: 61 additions & 0 deletions backends/vulkan/runtime/graph/ops/glsl/nchw_to_image.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

#version 450 core

#define PRECISION ${PRECISION}

#include "indexing_utils.h"

layout(std430) buffer;

layout(set = 0, binding = 0, ${IMAGE_FORMAT[DTYPE]}) uniform PRECISION restrict writeonly ${IMAGE_T[NDIM][DTYPE]} image_out;
layout(set = 0, binding = 1) buffer PRECISION restrict readonly Buffer {
${T[DTYPE]} data[];
}
buffer_in;

layout(set = 0, binding = 2) uniform PRECISION restrict GpuSizes {
ivec4 data;
}
gpu_sizes;

layout(set = 0, binding = 3) uniform PRECISION restrict CpuSizes {
ivec4 data;
}
cpu_sizes;

layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in;

void main() {
const ivec3 pos = ivec3(gl_GlobalInvocationID);
const ivec4 coord = POS_TO_COORD_${PACKING}(pos, gpu_sizes.data);

if (any(greaterThanEqual(coord, gpu_sizes.data))) {
return;
}

const int base_index = COORD_TO_BUFFER_IDX(coord, cpu_sizes.data);
const ivec4 buf_indices =
base_index + ivec4(0, 1, 2, 3) * (gpu_sizes.data.x * gpu_sizes.data.y);

${T[DTYPE]} val_x = buffer_in.data[buf_indices.x];
${T[DTYPE]} val_y = buffer_in.data[buf_indices.y];
${T[DTYPE]} val_z = buffer_in.data[buf_indices.z];
${T[DTYPE]} val_w = buffer_in.data[buf_indices.w];

${VEC4_T[DTYPE]} texel = ${VEC4_T[DTYPE]}(val_x, val_y, val_z, val_w);

if (coord.z + 3 >= cpu_sizes.data.z) {
ivec4 c_ind = ivec4(coord.z) + ivec4(0, 1, 2, 3);
vec4 valid_c = vec4(lessThan(c_ind, ivec4(cpu_sizes.data.z)));
texel = texel * valid_c;
}

imageStore(image_out, ${GET_POS[NDIM]("pos")}, texel);
}
Loading