Skip to content

Commit 5cdc6cd

Browse files
committed
Update on "[ET-VK] Introduce graph runtime shader library that enables dynamic shapes"
## 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. Differential Revision: [D54754545](https://our.internmc.facebook.com/intern/diff/D54754545/) [ghstack-poisoned]
2 parents 5bb0389 + bafd1e9 commit 5cdc6cd

File tree

5 files changed

+6
-7
lines changed

5 files changed

+6
-7
lines changed

backends/vulkan/targets.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def vulkan_spv_shader_lib(name, spv_filegroups, is_fbcode = False):
3737
srcs = [
3838
":{}[{}.cpp]".format(genrule_name, name),
3939
],
40+
define_static_target = False,
4041
# Static initialization is used to register shaders to the global shader registry,
4142
# therefore link_whole must be True to make sure unused symbols are not discarded.
4243
# @lint-ignore BUCKLINT: Avoid `link_whole=True`

examples/models/llama2/runner/targets.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def _get_operator_lib(aten = False):
66
elif runtime.is_oss:
77
return ["//executorch/kernels/portable:generated_lib"]
88
else:
9-
return ["//executorch/kernels/portable:generated_lib", "//executorch/examples/models/llama2/custom_ops:custom_ops", "//executorch/examples/models/llama2/ops:generated_lib"]
9+
return ["//executorch/configurations:optimized_native_cpu_ops", "//executorch/examples/models/llama2/custom_ops:custom_ops", "//executorch/examples/models/llama2/ops:generated_lib"]
1010

1111
def define_common_targets():
1212
for aten in (True, False):

kernels/portable/test/targets.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def define_common_targets():
88
"""
99
define_supported_features_lib()
1010

11-
op_test(name = "op_allclose_test", aten_compatible = False)
11+
op_test(name = "op_allclose_test")
1212
op_test(name = "op_div_test")
1313
op_test(name = "op_gelu_test")
1414
op_test(name = "op_mul_test")

kernels/test/targets.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")
22
load("@fbsource//xplat/executorch/kernels/test:util.bzl", "codegen_function_header_wrapper", "generated_op_test", "op_test")
33

4-
def _common_op_test(name, kernels, aten_compatible = True):
4+
def _common_op_test(name, kernels):
55
"""
66
Defines test targets in format of <kernel>_op_<op-name>_test
77
For ATen kernel testing, let's use portable functions.yaml for tested ops.
88
"""
99
for kernel in kernels:
1010
deps = [":function_header_wrapper_{}".format(kernel)]
11-
op_test(name, aten_compatible = aten_compatible, kernel_name = kernel, use_kernel_prefix = True, deps = deps)
11+
op_test(name, kernel_name = kernel, use_kernel_prefix = True, deps = deps)
1212

1313
def make_example_generated_op_test_target():
1414
"""

kernels/test/util.bzl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
load("@fbsource//tools/build_defs:fbsource_utils.bzl", "is_xplat")
22
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")
33

4-
def op_test(name, deps = [], aten_compatible = True, kernel_name = "portable", use_kernel_prefix = False):
4+
def op_test(name, deps = [], kernel_name = "portable", use_kernel_prefix = False):
55
"""Defines a cxx_test() for an "op_*_test.cpp" file.
66
77
Args:
@@ -11,8 +11,6 @@ def op_test(name, deps = [], aten_compatible = True, kernel_name = "portable", u
1111
under //kernels/<kernel>/...; e.g., "op_add_test" will depend on
1212
"//kernels/portable/cpu:op_add".
1313
deps: Optional extra deps to add to the cxx_test().
14-
aten_compatible: If True, the operator under test is ATen-compatible
15-
(i.e., appears in `functions.yaml`).
1614
kernel_name: The name string as in //executorch/kernels/<kernel_name>.
1715
use_kernel_prefix: If True, the target name is
1816
<kernel>_op_<operator-group-name>_test. Used by common kernel testing.

0 commit comments

Comments
 (0)