Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce graph runtime shader library that enables dynamic shapes (#…
…2366) Summary: Pull Request resolved: #2366 ## Context pytorch/pytorch#121598 introduces the ability to support dynamic shapes through tensor metadata updates. The idea is fairly simple. Instead of shaders accepting a UBO with size data for all arguments: ``` layout(set = 0, binding = 2) uniform PRECISION restrict Block { ivec4 output_sizes; ivec4 other_sizes; float alpha; } ``` Shaders will accept separate UBOs for each piece of tensor metadata: ``` 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; ``` Each UBO will be owned and maintained by the corresponding `vTensor` instance. To support a graph input resize, every tensor in the graph only needs to update their metadata UBOs via the `tensor.virtual_resize(new_sizes)` call. Shader dispatches in subsequent command buffer submissions will then see the updated metadata and execute as if the tensor were the updated sizes. This changeset introduces a new shader library for the Vulkan graph runtime that enables dynamic shapes through this technique in favor of relying on the shader library from PyTorch Vulkan. ## Considerations Technically, the UBO update technique can be applied to the shaders from PyTorch Vulkan as well. If that's the case, why introduce a new shader library for the graph runtime? The primary motivation is code quality. First, having `vTensor` supply UBOs for their own metadata greatly reduces the need to have operator specifc ad-hoc `Params` structs to organize arguments to write into a `api::UniformParamsBuffer`. Constructing an `ExecuteNode` for binary operators is now ``` graph.execute_nodes().emplace_back(new ExecuteNode( graph, api::shader_registry().get_shader_info(kernel_name.str()), global_size, local_size, {{out, api::MemoryAccessType::WRITE}, {{arg1, arg2}, api::MemoryAccessType::READ}}, {t_out.gpu_sizes_ubo(), t_in1.gpu_sizes_ubo(), t_in2.gpu_sizes_ubo(), graph.create_params_buffer(alpha_val)})) ``` instead of ``` ArithmeticParams block{ get_size_as_ivec4(t_out), get_size_as_ivec4(t_in1), get_size_as_ivec4(t_in2), alpha_val, }; api::UniformParamsBuffer params(graph.context(), block); graph.execute_nodes().emplace_back(new ExecuteNode( graph, shader, global_size, local_size, {{out, api::MemoryAccessType::WRITE}, {{arg1, arg2}, api::MemoryAccessType::READ}}, std::move(params))); ``` Another consideration is that pytorch/pytorch#115948 which was landed fairly recently enables much more expressive shader templates through the use of Python code blocks in the GLSL template. This enables shader templates that can easily express variants for different data types, packing structures, etc. Introducing a new shader library provides the opportunity to rewrite the shaders in PyTorch Vulkan in a more generic and extensible way. ghstack-source-id: 218429132 exported-using-ghexport bypass-github-export-checks bypass-github-pytorch-ci-checks bypass-github-executorch-ci-checks Reviewed By: jorgep31415 Differential Revision: D54754545 fbshipit-source-id: 7e2074699b61f8358358775a8b790d34dcb99ee6
- Loading branch information
1 parent
341d2d9
commit 835279e
Showing
15 changed files
with
524 additions
and
276 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
backends/vulkan/runtime/graph/ops/glsl/broadcasting_utils.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.