Skip to content
Merged
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
70 changes: 14 additions & 56 deletions backends/webgpu/runtime/ops/boolean_op/BooleanOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,61 +84,22 @@ void dispatch_bool_op(
utils::make_uniform(device, &params, sizeof(BoolOpParams));
graph.add_uniform_buffer_bytes(sizeof(BoolOpParams));

WGPUShaderSourceWGSL wgsl_desc = {};
wgsl_desc.chain.sType = WGPUSType_ShaderSourceWGSL;
wgsl_desc.code = {wgsl, WGPU_STRLEN};
WGPUShaderModuleDescriptor shader_desc = {};
shader_desc.nextInChain = &wgsl_desc.chain;
WGPUShaderModule shader = wgpuDeviceCreateShaderModule(device, &shader_desc);

WGPUBindGroupLayoutEntry entries[3] = {};
entries[0].buffer.type = WGPUBufferBindingType_ReadOnlyStorage;
entries[1].buffer.type = WGPUBufferBindingType_Storage;
entries[2].buffer.type = WGPUBufferBindingType_Uniform;
for (uint32_t i = 0; i < 3; i++) {
entries[i].binding = i;
entries[i].visibility = WGPUShaderStage_Compute;
}

WGPUBindGroupLayoutDescriptor bgl_desc = {};
bgl_desc.entryCount = 3;
bgl_desc.entries = entries;
WGPUBindGroupLayout bgl = wgpuDeviceCreateBindGroupLayout(device, &bgl_desc);

WGPUPipelineLayoutDescriptor pl_desc = {};
pl_desc.bindGroupLayoutCount = 1;
pl_desc.bindGroupLayouts = &bgl;
WGPUPipelineLayout pipeline_layout =
wgpuDeviceCreatePipelineLayout(device, &pl_desc);

WGPUComputePipelineDescriptor pipeline_desc = {};
pipeline_desc.layout = pipeline_layout;
pipeline_desc.compute.module = shader;
pipeline_desc.compute.entryPoint = {"main", WGPU_STRLEN};
pipeline_desc.compute.constantCount = 1;
pipeline_desc.compute.constants = &wg_size_constant;
WGPUComputePipeline pipeline =
wgpuDeviceCreateComputePipeline(device, &pipeline_desc);

WGPUBindGroupEntry bg_entries[3] = {};
bg_entries[0].binding = 0;
bg_entries[0].buffer = self_tensor.buffer;
bg_entries[0].size = self_bind_size;
bg_entries[1].binding = 1;
bg_entries[1].buffer = out_tensor.buffer;
bg_entries[1].size = out_bind_size;
bg_entries[2].binding = 2;
bg_entries[2].buffer = params_buf;
bg_entries[2].size = sizeof(BoolOpParams);

WGPUBindGroupDescriptor bg_desc = {};
bg_desc.layout = bgl;
bg_desc.entryCount = 3;
bg_desc.entries = bg_entries;
WGPUBindGroup bind_group = wgpuDeviceCreateBindGroup(device, &bg_desc);
utils::ComputePipelineBundle bundle = utils::make_compute_pipeline(
device,
wgsl,
{
{0,
WGPUBufferBindingType_ReadOnlyStorage,
self_tensor.buffer,
self_bind_size},
{1, WGPUBufferBindingType_Storage, out_tensor.buffer, out_bind_size},
{2, WGPUBufferBindingType_Uniform, params_buf, sizeof(BoolOpParams)},
},
&wg_size_constant,
1);

const size_t dispatch_idx =
graph.add_dispatch({pipeline, bind_group, workgroup_count});
graph.add_dispatch({bundle.pipeline, bundle.bind_group, workgroup_count});

WGPUBuffer p_buf = params_buf;
auto resize =
Expand All @@ -158,9 +119,6 @@ void dispatch_bool_op(
};
graph.add_tensor_resize_hook(self_id, resize);

wgpuShaderModuleRelease(shader);
wgpuBindGroupLayoutRelease(bgl);
wgpuPipelineLayoutRelease(pipeline_layout);
graph.own_uniform_buffer(params_buf);
}

Expand Down
116 changes: 27 additions & 89 deletions backends/webgpu/runtime/ops/div/BinaryOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,94 +78,35 @@ void div_impl(WebGPUGraph& graph, const std::vector<int>& args) {
utils::make_uniform(device, &in2_meta, sizeof(TensorMeta));
graph.add_uniform_buffer_bytes(3 * sizeof(TensorMeta));

WGPUShaderSourceWGSL wgsl_desc = {};
wgsl_desc.chain.sType = WGPUSType_ShaderSourceWGSL;
wgsl_desc.code = {kBinaryDivWGSL, WGPU_STRLEN};

WGPUShaderModuleDescriptor shader_desc = {};
shader_desc.nextInChain = &wgsl_desc.chain;
WGPUShaderModule shader = wgpuDeviceCreateShaderModule(device, &shader_desc);

WGPUBindGroupLayoutEntry entries[6] = {};

entries[0].binding = 0;
entries[0].visibility = WGPUShaderStage_Compute;
entries[0].buffer.type = WGPUBufferBindingType_ReadOnlyStorage;

entries[1].binding = 1;
entries[1].visibility = WGPUShaderStage_Compute;
entries[1].buffer.type = WGPUBufferBindingType_ReadOnlyStorage;

entries[2].binding = 2;
entries[2].visibility = WGPUShaderStage_Compute;
entries[2].buffer.type = WGPUBufferBindingType_Storage;

entries[3].binding = 3;
entries[3].visibility = WGPUShaderStage_Compute;
entries[3].buffer.type = WGPUBufferBindingType_Uniform;

entries[4].binding = 4;
entries[4].visibility = WGPUShaderStage_Compute;
entries[4].buffer.type = WGPUBufferBindingType_Uniform;

entries[5].binding = 5;
entries[5].visibility = WGPUShaderStage_Compute;
entries[5].buffer.type = WGPUBufferBindingType_Uniform;

WGPUBindGroupLayoutDescriptor bgl_desc = {};
bgl_desc.entryCount = 6;
bgl_desc.entries = entries;
WGPUBindGroupLayout bgl = wgpuDeviceCreateBindGroupLayout(device, &bgl_desc);

WGPUPipelineLayoutDescriptor pl_desc = {};
pl_desc.bindGroupLayoutCount = 1;
pl_desc.bindGroupLayouts = &bgl;
WGPUPipelineLayout pipeline_layout =
wgpuDeviceCreatePipelineLayout(device, &pl_desc);

WGPUComputePipelineDescriptor pipeline_desc = {};
pipeline_desc.layout = pipeline_layout;
pipeline_desc.compute.module = shader;
pipeline_desc.compute.entryPoint = {"main", WGPU_STRLEN};
pipeline_desc.compute.constantCount = 1;
pipeline_desc.compute.constants = &wg_size_constant;
WGPUComputePipeline pipeline =
wgpuDeviceCreateComputePipeline(device, &pipeline_desc);

WGPUBindGroupEntry bg_entries[6] = {};

bg_entries[0].binding = 0;
bg_entries[0].buffer = in1_tensor.buffer;
bg_entries[0].size = in1_tensor.nbytes;

bg_entries[1].binding = 1;
bg_entries[1].buffer = in2_tensor.buffer;
bg_entries[1].size = in2_tensor.nbytes;

bg_entries[2].binding = 2;
bg_entries[2].buffer = out_tensor.buffer;
bg_entries[2].size = out_tensor.nbytes;

bg_entries[3].binding = 3;
bg_entries[3].buffer = out_meta_buf;
bg_entries[3].size = sizeof(TensorMeta);

bg_entries[4].binding = 4;
bg_entries[4].buffer = in1_meta_buf;
bg_entries[4].size = sizeof(TensorMeta);

bg_entries[5].binding = 5;
bg_entries[5].buffer = in2_meta_buf;
bg_entries[5].size = sizeof(TensorMeta);

WGPUBindGroupDescriptor bg_desc = {};
bg_desc.layout = bgl;
bg_desc.entryCount = 6;
bg_desc.entries = bg_entries;
WGPUBindGroup bind_group = wgpuDeviceCreateBindGroup(device, &bg_desc);
utils::ComputePipelineBundle bundle = utils::make_compute_pipeline(
device,
kBinaryDivWGSL,
{
{0,
WGPUBufferBindingType_ReadOnlyStorage,
in1_tensor.buffer,
in1_tensor.nbytes},
{1,
WGPUBufferBindingType_ReadOnlyStorage,
in2_tensor.buffer,
in2_tensor.nbytes},
{2,
WGPUBufferBindingType_Storage,
out_tensor.buffer,
out_tensor.nbytes},
{3, WGPUBufferBindingType_Uniform, out_meta_buf, sizeof(TensorMeta)},
{4, WGPUBufferBindingType_Uniform, in1_meta_buf, sizeof(TensorMeta)},
{5, WGPUBufferBindingType_Uniform, in2_meta_buf, sizeof(TensorMeta)},
},
&wg_size_constant,
1);

const size_t dispatch_idx = graph.add_dispatch(
{pipeline, bind_group, workgroup_count.x, "div", workgroup_count.y});
{bundle.pipeline,
bundle.bind_group,
workgroup_count.x,
"div",
workgroup_count.y});

// Dynamic shapes: rebuild all 3 broadcast TensorMeta UBOs + dispatch.
WGPUBuffer o_buf = out_meta_buf, a_buf = in1_meta_buf, b_buf = in2_meta_buf;
Expand Down Expand Up @@ -206,9 +147,6 @@ void div_impl(WebGPUGraph& graph, const std::vector<int>& args) {
graph.add_tensor_resize_hook(in1_id, div_resize);
graph.add_tensor_resize_hook(in2_id, div_resize);

wgpuShaderModuleRelease(shader);
wgpuBindGroupLayoutRelease(bgl);
wgpuPipelineLayoutRelease(pipeline_layout);
// Graph owns them so a resize hook can rewrite them; freed in the dtor.
graph.own_uniform_buffer(out_meta_buf);
graph.own_uniform_buffer(in1_meta_buf);
Expand Down
86 changes: 20 additions & 66 deletions backends/webgpu/runtime/ops/expand_copy/ExpandCopy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,72 +60,26 @@ void expand_copy_impl(WebGPUGraph& graph, const std::vector<int>& args) {
utils::make_uniform(device, &in_meta, sizeof(TensorMeta));
graph.add_uniform_buffer_bytes(2 * sizeof(TensorMeta));

WGPUShaderSourceWGSL wgsl_desc = {};
wgsl_desc.chain.sType = WGPUSType_ShaderSourceWGSL;
wgsl_desc.code = {kExpandCopyWGSL, WGPU_STRLEN};
WGPUShaderModuleDescriptor shader_desc = {};
shader_desc.nextInChain = &wgsl_desc.chain;
WGPUShaderModule shader = wgpuDeviceCreateShaderModule(device, &shader_desc);

WGPUBindGroupLayoutEntry entries[4] = {};
entries[0].binding = 0;
entries[0].visibility = WGPUShaderStage_Compute;
entries[0].buffer.type = WGPUBufferBindingType_ReadOnlyStorage;
entries[1].binding = 1;
entries[1].visibility = WGPUShaderStage_Compute;
entries[1].buffer.type = WGPUBufferBindingType_Storage;
entries[2].binding = 2;
entries[2].visibility = WGPUShaderStage_Compute;
entries[2].buffer.type = WGPUBufferBindingType_Uniform;
entries[3].binding = 3;
entries[3].visibility = WGPUShaderStage_Compute;
entries[3].buffer.type = WGPUBufferBindingType_Uniform;

WGPUBindGroupLayoutDescriptor bgl_desc = {};
bgl_desc.entryCount = 4;
bgl_desc.entries = entries;
WGPUBindGroupLayout bgl = wgpuDeviceCreateBindGroupLayout(device, &bgl_desc);

WGPUPipelineLayoutDescriptor pl_desc = {};
pl_desc.bindGroupLayoutCount = 1;
pl_desc.bindGroupLayouts = &bgl;
WGPUPipelineLayout pipeline_layout =
wgpuDeviceCreatePipelineLayout(device, &pl_desc);

WGPUComputePipelineDescriptor pipeline_desc = {};
pipeline_desc.layout = pipeline_layout;
pipeline_desc.compute.module = shader;
pipeline_desc.compute.entryPoint = {"main", WGPU_STRLEN};
pipeline_desc.compute.constantCount = 1;
pipeline_desc.compute.constants = &wg_size_constant;
WGPUComputePipeline pipeline =
wgpuDeviceCreateComputePipeline(device, &pipeline_desc);

WGPUBindGroupEntry bg_entries[4] = {};
bg_entries[0].binding = 0;
bg_entries[0].buffer = in_tensor.buffer;
bg_entries[0].size = in_tensor.nbytes;
bg_entries[1].binding = 1;
bg_entries[1].buffer = out_tensor.buffer;
bg_entries[1].size = out_tensor.nbytes;
bg_entries[2].binding = 2;
bg_entries[2].buffer = out_meta_buf;
bg_entries[2].size = sizeof(TensorMeta);
bg_entries[3].binding = 3;
bg_entries[3].buffer = in_meta_buf;
bg_entries[3].size = sizeof(TensorMeta);

WGPUBindGroupDescriptor bg_desc = {};
bg_desc.layout = bgl;
bg_desc.entryCount = 4;
bg_desc.entries = bg_entries;
WGPUBindGroup bind_group = wgpuDeviceCreateBindGroup(device, &bg_desc);

graph.add_dispatch({pipeline, bind_group, workgroup_count});

wgpuShaderModuleRelease(shader);
wgpuBindGroupLayoutRelease(bgl);
wgpuPipelineLayoutRelease(pipeline_layout);
utils::ComputePipelineBundle bundle = utils::make_compute_pipeline(
device,
kExpandCopyWGSL,
{
{0,
WGPUBufferBindingType_ReadOnlyStorage,
in_tensor.buffer,
in_tensor.nbytes},
{1,
WGPUBufferBindingType_Storage,
out_tensor.buffer,
out_tensor.nbytes},
{2, WGPUBufferBindingType_Uniform, out_meta_buf, sizeof(TensorMeta)},
{3, WGPUBufferBindingType_Uniform, in_meta_buf, sizeof(TensorMeta)},
},
&wg_size_constant,
1);

graph.add_dispatch({bundle.pipeline, bundle.bind_group, workgroup_count});

wgpuBufferRelease(out_meta_buf);
wgpuBufferRelease(in_meta_buf);
}
Expand Down
72 changes: 20 additions & 52 deletions backends/webgpu/runtime/ops/fill/Fill.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,57 +67,28 @@ void add_fill(
utils::make_uniform(device, &params, sizeof(FillParams));
graph.add_uniform_buffer_bytes(sizeof(FillParams));

WGPUShaderSourceWGSL wgsl_desc = {};
wgsl_desc.chain.sType = WGPUSType_ShaderSourceWGSL;
wgsl_desc.code = {kFillWGSL, WGPU_STRLEN};
WGPUShaderModuleDescriptor shader_desc = {};
shader_desc.nextInChain = &wgsl_desc.chain;
WGPUShaderModule shader = wgpuDeviceCreateShaderModule(device, &shader_desc);

WGPUBindGroupLayoutEntry entries[2] = {};
entries[0].binding = 0;
entries[0].visibility = WGPUShaderStage_Compute;
entries[0].buffer.type = WGPUBufferBindingType_Storage;
entries[1].binding = 1;
entries[1].visibility = WGPUShaderStage_Compute;
entries[1].buffer.type = WGPUBufferBindingType_Uniform;

WGPUBindGroupLayoutDescriptor bgl_desc = {};
bgl_desc.entryCount = 2;
bgl_desc.entries = entries;
WGPUBindGroupLayout bgl = wgpuDeviceCreateBindGroupLayout(device, &bgl_desc);

WGPUPipelineLayoutDescriptor pl_desc = {};
pl_desc.bindGroupLayoutCount = 1;
pl_desc.bindGroupLayouts = &bgl;
WGPUPipelineLayout pipeline_layout =
wgpuDeviceCreatePipelineLayout(device, &pl_desc);

WGPUComputePipelineDescriptor pipeline_desc = {};
pipeline_desc.layout = pipeline_layout;
pipeline_desc.compute.module = shader;
pipeline_desc.compute.entryPoint = {"main", WGPU_STRLEN};
pipeline_desc.compute.constantCount = 1;
pipeline_desc.compute.constants = &wg_size_constant;
WGPUComputePipeline pipeline =
wgpuDeviceCreateComputePipeline(device, &pipeline_desc);

WGPUBindGroupEntry bg_entries[2] = {};
bg_entries[0].binding = 0;
bg_entries[0].buffer = out_tensor.buffer;
bg_entries[0].size = out_tensor.nbytes;
bg_entries[1].binding = 1;
bg_entries[1].buffer = uniform_buffer;
bg_entries[1].size = sizeof(FillParams);

WGPUBindGroupDescriptor bg_desc = {};
bg_desc.layout = bgl;
bg_desc.entryCount = 2;
bg_desc.entries = bg_entries;
WGPUBindGroup bind_group = wgpuDeviceCreateBindGroup(device, &bg_desc);
utils::ComputePipelineBundle bundle = utils::make_compute_pipeline(
device,
kFillWGSL,
{
{0,
WGPUBufferBindingType_Storage,
out_tensor.buffer,
out_tensor.nbytes},
{1,
WGPUBufferBindingType_Uniform,
uniform_buffer,
sizeof(FillParams)},
},
&wg_size_constant,
1);

const size_t dispatch_idx = graph.add_dispatch(
{pipeline, bind_group, workgroup_count.x, "", workgroup_count.y});
{bundle.pipeline,
bundle.bind_group,
workgroup_count.x,
"",
workgroup_count.y});

// Dynamic shapes: recompute num_elements/dispatch from the live output dims.
WGPUBuffer params_buf = uniform_buffer;
Expand All @@ -136,9 +107,6 @@ void add_fill(
g.dispatch_at(dispatch_idx).workgroup_count_y = wgc.y;
});

wgpuShaderModuleRelease(shader);
wgpuBindGroupLayoutRelease(bgl);
wgpuPipelineLayoutRelease(pipeline_layout);
graph.own_uniform_buffer(uniform_buffer);
}

Expand Down
Loading
Loading