Skip to content

Commit c99fcd4

Browse files
committed
[ET-VK][runtime][2/3] Add typed dispatch size abstractions
Pull Request resolved: pytorch#22224 Introduce `LocalWorkGroup`, `LwgShape`, `GlobalWorkGrid`, and `DispatchGridIntent` in `runtime/vk_api/DispatchGrid`. Remove legacy `WorkgroupSize`. Model local shapes as power-of-two exponents, support fitting against global extents, and validate local dimensions against device limits. Track dispatch intent independently from optional required local workgroup geometry. Authored with Codex. ghstack-source-id: 421281484 @exported-using-ghexport Differential Revision: [D117706351](https://our.internmc.facebook.com/intern/diff/D117706351/)
1 parent 40dd047 commit c99fcd4

16 files changed

Lines changed: 689 additions & 81 deletions

‎backends/vulkan/runtime/api/Context.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void Context::cmd_reset_querypool() {
7474
void Context::report_shader_dispatch_start(
7575
const std::string& shader_name,
7676
const utils::uvec3& global_wg_size,
77-
const utils::WorkgroupSize& local_wg_size,
77+
const LocalWorkGroup& local_wg_size,
7878
const uint32_t dispatch_id) {
7979
if (querypool_) {
8080
querypool_.shader_profile_begin(
@@ -133,7 +133,7 @@ void Context::check_device_capabilities(const vkapi::ShaderInfo& shader) {
133133

134134
vkapi::DescriptorSet Context::get_descriptor_set(
135135
const vkapi::ShaderInfo& shader_descriptor,
136-
const utils::WorkgroupSize& local_workgroup_size,
136+
const LocalWorkGroup& local_workgroup_size,
137137
const vkapi::SpecVarList& additional_constants,
138138
const uint32_t push_constants_size) {
139139
VkDescriptorSetLayout shader_layout =
@@ -324,7 +324,7 @@ VkPipeline Context::get_shader_pipeline(
324324
VkPipelineLayout pipeline_layout =
325325
pipeline_layout_cache().retrieve(shader_layout, push_constants_size);
326326

327-
const utils::WorkgroupSize local_workgroup_size(4u, 4u, 1u);
327+
const LocalWorkGroup local_workgroup_size(4u, 4u, 1u);
328328
vkapi::SpecVarList spec_constants = {
329329
SV(local_workgroup_size[0u]),
330330
SV(local_workgroup_size[1u]),

‎backends/vulkan/runtime/api/Context.h‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <executorch/backends/vulkan/runtime/vk_api/Adapter.h>
1717
#include <executorch/backends/vulkan/runtime/vk_api/Command.h>
1818
#include <executorch/backends/vulkan/runtime/vk_api/Descriptor.h>
19+
#include <executorch/backends/vulkan/runtime/vk_api/DispatchGrid.h>
1920
#include <executorch/backends/vulkan/runtime/vk_api/Fence.h>
2021
#include <executorch/backends/vulkan/runtime/vk_api/QueryPool.h>
2122
#include <executorch/backends/vulkan/runtime/vk_api/Runtime.h>
@@ -151,7 +152,7 @@ class Context final {
151152
void report_shader_dispatch_start(
152153
const std::string& shader_name,
153154
const utils::uvec3& global_wg_size,
154-
const utils::WorkgroupSize& local_wg_size,
155+
const LocalWorkGroup& local_wg_size,
155156
const uint32_t dispatch_id = UINT32_MAX);
156157

157158
/*
@@ -190,13 +191,13 @@ class Context final {
190191

191192
vkapi::DescriptorSet get_descriptor_set(
192193
const vkapi::ShaderInfo&,
193-
const utils::WorkgroupSize&,
194+
const LocalWorkGroup&,
194195
const vkapi::SpecVarList&,
195196
const uint32_t push_constants_size);
196197

197198
inline vkapi::DescriptorSet get_descriptor_set(
198199
const vkapi::ShaderInfo& shader_descriptor,
199-
const utils::WorkgroupSize& local_work_group_size) {
200+
const LocalWorkGroup& local_work_group_size) {
200201
return get_descriptor_set(shader_descriptor, local_work_group_size, {}, 0u);
201202
}
202203

@@ -370,15 +371,15 @@ inline bool Context::submit_compute_job(
370371
report_shader_dispatch_start(
371372
shader.kernel_name,
372373
global_work_group,
373-
utils::WorkgroupSize(local_work_group_size),
374+
LocalWorkGroup(local_work_group_size),
374375
dispatch_id);
375376

376377
// Factor out template parameter independent code to minimize code bloat.
377378
// Note that push constants are not exposed yet via this API, therefore the
378379
// push constants size is assumed to be 0.
379380
vkapi::DescriptorSet descriptor_set = get_descriptor_set(
380381
shader,
381-
utils::WorkgroupSize(local_work_group_size),
382+
LocalWorkGroup(local_work_group_size),
382383
specialization_constants,
383384
0u);
384385

‎backends/vulkan/runtime/graph/ComputeGraph.cpp‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ void ComputeGraph::update_descriptor_counts(
845845

846846
void ComputeGraph::register_pipeline_to_create(
847847
const vkapi::ShaderInfo& shader_info,
848-
const utils::WorkgroupSize& local_workgroup_size,
848+
const LocalWorkGroup& local_workgroup_size,
849849
const vkapi::SpecVarList& spec_vars,
850850
const std::vector<PushConstantDataInfo>& push_constants) {
851851
VkDescriptorSetLayout shader_layout =
@@ -921,8 +921,10 @@ utils::uvec3 ComputeGraph::create_local_wg_size(
921921

922922
utils::uvec3 local_group_size = {
923923
8,
924-
std::max(1u, std::min(4u, global_wg_size_desc[1].second)),
925-
std::max(1u, std::min(2u, global_wg_size_desc[2].second))};
924+
global_wg_size_desc[1].second >= 4u ? 4u
925+
: global_wg_size_desc[1].second >= 2u ? 2u
926+
: 1u,
927+
global_wg_size_desc[2].second >= 2u ? 2u : 1u};
926928

927929
if (global_wg_size_desc[2u].second == 1) {
928930
if (global_wg_size_desc[1u].second == 1) {

‎backends/vulkan/runtime/graph/ComputeGraph.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include <executorch/backends/vulkan/runtime/graph/ops/ExecuteNode.h>
2727
#include <executorch/backends/vulkan/runtime/graph/ops/PrepackNode.h>
2828

29+
#include <executorch/backends/vulkan/runtime/vk_api/DispatchGrid.h>
30+
2931
#ifdef ET_EVENT_TRACER_ENABLED
3032
std::string& set_and_get_current_operator_json(const std::string& json);
3133
size_t get_current_operator_count(const bool increment = false);
@@ -1010,7 +1012,7 @@ class ComputeGraph final {
10101012

10111013
void register_pipeline_to_create(
10121014
const vkapi::ShaderInfo& shader_info,
1013-
const utils::WorkgroupSize& local_workgroup_size,
1015+
const LocalWorkGroup& local_workgroup_size,
10141016
const vkapi::SpecVarList& spec_vars,
10151017
const std::vector<PushConstantDataInfo>& push_constants);
10161018

‎backends/vulkan/runtime/graph/ops/BlitNode.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void BlitNode::encode(ComputeGraph* graph) {
4444
kernel_name += vkapi::to_string(graph->dtype_of(dst_));
4545

4646
context->report_shader_dispatch_start(
47-
kernel_name, utils::uvec3(), utils::WorkgroupSize(), node_id_);
47+
kernel_name, utils::uvec3(), LocalWorkGroup(), node_id_);
4848

4949
context->register_blit(
5050
pipeline_barrier,

‎backends/vulkan/runtime/graph/ops/DispatchNode.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
#include <executorch/backends/vulkan/runtime/graph/ops/ExecuteNode.h>
1717

18+
#include <executorch/backends/vulkan/runtime/vk_api/DispatchGrid.h>
19+
1820
namespace vkcompute {
1921

2022
class ComputeGraph;
@@ -49,7 +51,7 @@ class DispatchNode : public ExecuteNode {
4951
protected:
5052
vkapi::ShaderInfo shader_;
5153
utils::uvec3 global_workgroup_size_;
52-
utils::WorkgroupSize local_workgroup_size_;
54+
LocalWorkGroup local_workgroup_size_;
5355
const vkapi::ParamsBindList params_;
5456
const vkapi::SpecVarList spec_vars_;
5557
const std::vector<PushConstantDataInfo> push_constants_;

‎backends/vulkan/runtime/graph/ops/DynamicDispatchNode.cpp‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ DynamicDispatchNode::DynamicDispatchNode(
3939
pick_local_wg_fn_(pick_local_wg_fn) {
4040
global_workgroup_size_ =
4141
pick_global_wg_fn(&graph, shader_, args, resize_args);
42-
local_workgroup_size_ = utils::WorkgroupSize(pick_local_wg_fn(
42+
local_workgroup_size_ = LocalWorkGroup(pick_local_wg_fn(
4343
&graph, shader_, global_workgroup_size_, args, resize_args));
4444

4545
// Calculate dispatch grid similar to Context.cpp register_shader_dispatch
@@ -76,7 +76,7 @@ DynamicDispatchNode::DynamicDispatchNode(
7676
pick_local_wg_fn_(pick_local_wg_fn) {
7777
global_workgroup_size_ =
7878
pick_global_wg_fn(&graph, shader_, args, resize_args);
79-
local_workgroup_size_ = utils::WorkgroupSize(pick_local_wg_fn(
79+
local_workgroup_size_ = LocalWorkGroup(pick_local_wg_fn(
8080
&graph, shader_, global_workgroup_size_, args, resize_args));
8181
// Calculate the work group grid that will be dispatched
8282
wg_dispatch_grid_ = {
@@ -119,8 +119,7 @@ bool DynamicDispatchNode::trigger_resize(ComputeGraph* graph) {
119119
if (pick_local_wg_fn_) {
120120
utils::uvec3 new_local_wg_uvec3 = pick_local_wg_fn_(
121121
graph, shader_, global_workgroup_size_, args_, resize_args_);
122-
utils::WorkgroupSize new_local_wg =
123-
utils::WorkgroupSize(new_local_wg_uvec3);
122+
LocalWorkGroup new_local_wg = LocalWorkGroup(new_local_wg_uvec3);
124123
if (local_workgroup_size_ != new_local_wg) {
125124
local_workgroup_size_ = new_local_wg;
126125
dispatch_params_changed = true;

‎backends/vulkan/runtime/graph/ops/PrepackNode.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <executorch/backends/vulkan/runtime/graph/containers/PushConstantData.h>
1414
#include <executorch/backends/vulkan/runtime/graph/containers/Value.h>
1515

16+
#include <executorch/backends/vulkan/runtime/vk_api/DispatchGrid.h>
17+
1618
namespace vkcompute {
1719

1820
class ComputeGraph;
@@ -52,7 +54,7 @@ class PrepackNode final {
5254
uint32_t node_id_;
5355
const vkapi::ShaderInfo shader_;
5456
const utils::uvec3 global_workgroup_size_;
55-
const utils::WorkgroupSize local_workgroup_size_;
57+
const LocalWorkGroup local_workgroup_size_;
5658
const ValueRef tref_;
5759
const ValueRef packed_;
5860
const vkapi::ParamsBindList params_;

‎backends/vulkan/runtime/utils/VecUtils.h‎

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -501,59 +501,5 @@ inline int64_t multiply_integers(const C& container) {
501501
return multiply_integers(container.begin(), container.end());
502502
}
503503

504-
class WorkgroupSize final {
505-
uint32_t val;
506-
507-
public:
508-
explicit WorkgroupSize() : val(0) {}
509-
explicit WorkgroupSize(const uint32_t x, const uint32_t y, const uint32_t z) {
510-
// shift numbers by multiple of 11 bits, since each local workgroup axis can
511-
// be 1024 at most and which is 0x400. only z axis can't store 1024, because
512-
// it would overflow uint32_t storage.
513-
if (z == 1024) {
514-
throw std::runtime_error(
515-
"Workgroup size in z axis cannot be 1024 because it would overflow uint32_t storage");
516-
}
517-
val = x | (y << 11) | (z << 22);
518-
}
519-
520-
explicit WorkgroupSize(const uvec3& vec) {
521-
// shift numbers by multiple of 11 bits, since each local workgroup axis can
522-
// be 1024 at most and which is 0x400. only z axis can't store 1024, because
523-
// it would overflow uint32_t storage.
524-
if (vec[2u] == 1024) {
525-
throw std::runtime_error(
526-
"Workgroup size in z axis cannot be 1024 because it would overflow uint32_t storage");
527-
}
528-
val = vec[0u] | (vec[1u] << 11) | (vec[2u] << 22);
529-
}
530-
531-
explicit inline operator uvec3() const {
532-
return {
533-
val & 0x7ffu,
534-
(val >> 11) & 0x7ffu,
535-
(val >> 22),
536-
};
537-
}
538-
539-
explicit inline operator uint32_t() const {
540-
return val;
541-
}
542-
543-
inline constexpr uint32_t operator[](const int idx) const {
544-
return (val >> (11 * idx)) & 0x7ffu;
545-
}
546-
547-
// Equality operator
548-
bool operator==(const WorkgroupSize& other) const {
549-
return val == other.val;
550-
}
551-
552-
// Inequality operator (optional, for completeness)
553-
bool operator!=(const WorkgroupSize& other) const {
554-
return !(*this == other);
555-
}
556-
};
557-
558504
} // namespace utils
559505
} // namespace vkcompute

‎backends/vulkan/runtime/vk_api/Command.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ void CommandBuffer::end() {
8181
void CommandBuffer::bind_pipeline(
8282
VkPipeline pipeline,
8383
VkPipelineLayout pipeline_layout,
84-
const utils::WorkgroupSize local_workgroup_size) {
84+
const LocalWorkGroup& local_workgroup_size) {
8585
VK_CHECK_COND(
8686
state_ == CommandBuffer::State::RECORDING,
8787
"Vulkan CommandBuffer: called bind_pipeline() on a command buffer whose state "

0 commit comments

Comments
 (0)