-
Notifications
You must be signed in to change notification settings - Fork 748
[ET-VK] Add 2D Reduction to Vulkan Backend #12860
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b9e001b
[ET-VK] Add 2D Reduction to Vulkan Backend
alexdean08 22c69b9
Update backends/vulkan/runtime/graph/ops/impl/Reduce.cpp
alexdean08 01502f6
Update 2D reduction to only be enabled for non-packed dims
alexdean08 df13204
Merge remote-tracking branch 'upstream/main' into main
alexdean08 f891c4b
Fix style in Reduction.cpp and op_registry.py
alexdean08 f20811a
Merge branch 'pytorch:main' into main
alexdean08 c2286ae
FIx missing VkMemoryLayout in op_registry.py
alexdean08 3ca809f
Fix style issues in op_registry.py
alexdean08 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,128 @@ | ||
| /* | ||
| * 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} | ||
| #define VEC4_T ${texel_load_type(DTYPE, STORAGE)} | ||
|
|
||
| ${define_active_storage_type(STORAGE)} | ||
|
|
||
| #extension GL_EXT_control_flow_attributes : require | ||
|
|
||
| layout(std430) buffer; | ||
|
|
||
| ${layout_declare_tensor(B, "w", "tout", DTYPE, STORAGE)} | ||
| ${layout_declare_tensor(B, "r", "tin", DTYPE, STORAGE)} | ||
|
|
||
| ${layout_declare_ubo(B, "ivec3", "tin_limits")} | ||
| ${layout_declare_ubo(B, "ivec4", "tin_sizes")} | ||
|
|
||
| layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in; | ||
|
|
||
| layout(constant_id = 3) const int packed_dim = 0; | ||
| layout(constant_id = 4) const int reduce_dim1 = 0; | ||
| layout(constant_id = 5) const int reduce_dim2 = 1; | ||
| layout(constant_id = 6) const int group_dim = 2; | ||
|
|
||
| // A more verbose name would be NWORKERS_PER_GROUP. This describes the number of | ||
| // threads that will co-operate to compute one reduction output. There may be | ||
| // multiple groups computing distinct reduction outputs within one work group. | ||
| #define NWORKERS 4 | ||
|
|
||
| // Sets an upper limit on the total size of a work group based on how many | ||
| // elements are allocated in the shared memory array below. Each thread in the | ||
| // work group will write into its assigned element in the shared array. | ||
| #define MAX_NTHREADS 16 | ||
|
|
||
|
|
||
| shared vec4 shared_vecs[MAX_NTHREADS]; | ||
|
|
||
| #include "indexing_utils.h" | ||
|
|
||
| int tid_to_smi(const ivec2 tid) { | ||
| return tid.x + tid.y * NWORKERS; | ||
| } | ||
|
|
||
| // Initializing the accumulator accepts the first value in the reduction row, | ||
| // since some reduction operations (i.e. amax, amin) prefer to initialize with | ||
| // a data point instead of a static value. | ||
| #define INIT_ACCUM(first_val) ${INIT_ACCUM} | ||
| #define UPDATE_ACCUM(accum, new_val) ${UPDATE_ACCUM} | ||
| // Useful for operators such as mean which want to perform a final calculation | ||
| // with the accumulator. | ||
| #define POSTPROCESS(accum) ${POSTPROCESS} | ||
|
|
||
| void reduce_2d_non_packed_dim(const ivec2 tid, ivec3 scan_pos) { | ||
| // shared memory index of this thread | ||
| const int smi = tid_to_smi(tid); | ||
|
|
||
| scan_pos[reduce_dim1] = 0; | ||
| scan_pos[reduce_dim2] = 0; | ||
| vec4 accum = INIT_ACCUM(load_texel(tin, scan_pos)); | ||
|
|
||
| // First dimension reduction | ||
| scan_pos[reduce_dim1] = tid.x; | ||
| for (int i = tid.x; i < tin_sizes[reduce_dim1]; | ||
| i += NWORKERS, scan_pos[reduce_dim1] += NWORKERS) { | ||
|
|
||
| // Second dimension reduction | ||
| scan_pos[reduce_dim2] = 0; | ||
| for (int j = 0; j < tin_sizes[reduce_dim2]; j++, scan_pos[reduce_dim2]++) { | ||
| accum = UPDATE_ACCUM(accum, load_texel(tin, scan_pos)); | ||
| } | ||
| } | ||
|
|
||
| // Write partial output to shared memory and synchronize | ||
| shared_vecs[smi] = accum; | ||
| barrier(); | ||
|
|
||
| // Main thread aggregates results | ||
| if (tid.x == 0) { | ||
| // Iterate over the partial outputs to obtain the overall output | ||
| int group_i = tid.y * NWORKERS; | ||
| accum = shared_vecs[group_i++]; | ||
| for (int i = 1; i < NWORKERS; i++, group_i++) { | ||
| accum = UPDATE_ACCUM(accum, shared_vecs[group_i]); | ||
| } | ||
|
|
||
| // Determine if there are any padding elements in the final texel of the | ||
| // packed dimension | ||
| const int nspill = mod4(tin_sizes[packed_dim]); | ||
| // Detect if this thread is working on the final texels of the packed | ||
| // dimension, which may have padding elements | ||
| const bool is_last_texel = | ||
| scan_pos[packed_dim] == (tin_limits[packed_dim] - 1); | ||
|
|
||
| // Explicitly set padding elements to 0 | ||
| if (is_last_texel && nspill > 0) { | ||
| [[unroll]] for (int i = nspill; i < 4; i++) { | ||
| accum[i] = 0; | ||
| } | ||
| } | ||
| scan_pos[reduce_dim1] = 0; | ||
| scan_pos[reduce_dim2] = 0; | ||
| write_texel(tout, scan_pos, POSTPROCESS(accum)); | ||
| } | ||
| } | ||
|
|
||
| void main() { | ||
| ivec3 scan_pos = ivec3(gl_GlobalInvocationID); | ||
| scan_pos[reduce_dim1] = 0; | ||
| scan_pos[reduce_dim2] = 0; | ||
|
|
||
| const ivec2 tid = ivec2( | ||
| gl_LocalInvocationID[reduce_dim1], | ||
| gl_LocalInvocationID[group_dim]); | ||
|
|
||
| if (any(greaterThanEqual(scan_pos, tin_limits))) { | ||
| return; | ||
| } | ||
|
|
||
| reduce_2d_non_packed_dim(tid, scan_pos); | ||
| } |
This file contains hidden or 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,29 @@ | ||
| # 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. | ||
|
|
||
| reduce2d: | ||
| parameter_names_with_default_values: | ||
| DTYPE: float | ||
| STORAGE: texture3d | ||
| INIT_ACCUM: VEC4_T(0) | ||
| UPDATE_ACCUM: accum + new_val | ||
| POSTPROCESS: accum | ||
| generate_variant_forall: | ||
| DTYPE: | ||
| - VALUE: half | ||
| - VALUE: float | ||
| shader_variants: | ||
| - NAME: sum2d | ||
| - NAME: mean2d | ||
| POSTPROCESS: (accum / (tin_sizes[reduce_dim1] * tin_sizes[reduce_dim2])) | ||
| - NAME: amax2d | ||
| INIT_ACCUM: first_val | ||
| UPDATE_ACCUM: max(accum, new_val) | ||
| POSTPROCESS: accum | ||
| - NAME: amin2d | ||
| INIT_ACCUM: first_val | ||
| UPDATE_ACCUM: min(accum, new_val) | ||
| POSTPROCESS: accum |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should also add a check that
graph.packed_dim_of(in)andgraph.packed_dim_of(out)is not one of the reduction dims.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just added above this comment. Let me know if you're aligned