@@ -97,78 +97,26 @@ void adamw_step_impl(WebGPUGraph& graph, const std::vector<int>& args) {
9797 utils::make_uniform (device, ¶ms, sizeof (params));
9898 graph.add_uniform_buffer_bytes (sizeof (params));
9999
100- WGPUShaderSourceWGSL wgsl_desc = {};
101- wgsl_desc.chain .sType = WGPUSType_ShaderSourceWGSL;
102- wgsl_desc.code = {kAdamwStepWGSL , WGPU_STRLEN };
103- WGPUShaderModuleDescriptor shader_desc = {};
104- shader_desc.nextInChain = &wgsl_desc.chain ;
105- WGPUShaderModule shader = wgpuDeviceCreateShaderModule (device, &shader_desc);
106-
107- WGPUBindGroupLayoutEntry entries[5 ] = {};
108- for (uint32_t i = 0 ; i <= 2 ; i++) {
109- entries[i].binding = i;
110- entries[i].visibility = WGPUShaderStage_Compute;
111- entries[i].buffer .type = WGPUBufferBindingType_Storage;
112- }
113- entries[3 ].binding = 3 ;
114- entries[3 ].visibility = WGPUShaderStage_Compute;
115- entries[3 ].buffer .type = WGPUBufferBindingType_ReadOnlyStorage;
116- entries[4 ].binding = 4 ;
117- entries[4 ].visibility = WGPUShaderStage_Compute;
118- entries[4 ].buffer .type = WGPUBufferBindingType_Uniform;
119-
120- WGPUBindGroupLayoutDescriptor bgl_desc = {};
121- bgl_desc.entryCount = 5 ;
122- bgl_desc.entries = entries;
123- WGPUBindGroupLayout bgl = wgpuDeviceCreateBindGroupLayout (device, &bgl_desc);
124-
125- WGPUPipelineLayoutDescriptor pl_desc = {};
126- pl_desc.bindGroupLayoutCount = 1 ;
127- pl_desc.bindGroupLayouts = &bgl;
128- WGPUPipelineLayout pipeline_layout =
129- wgpuDeviceCreatePipelineLayout (device, &pl_desc);
130-
131100 WGPUConstantEntry wg_size_constant = {};
132101 wg_size_constant.key = {" wg_size" , WGPU_STRLEN };
133102 wg_size_constant.value = static_cast <double >(wg_size);
134103
135- WGPUComputePipelineDescriptor pipeline_desc = {};
136- pipeline_desc.layout = pipeline_layout;
137- pipeline_desc.compute .module = shader;
138- pipeline_desc.compute .entryPoint = {" main" , WGPU_STRLEN };
139- pipeline_desc.compute .constantCount = 1 ;
140- pipeline_desc.compute .constants = &wg_size_constant;
141- WGPUComputePipeline pipeline =
142- wgpuDeviceCreateComputePipeline (device, &pipeline_desc);
143-
144- WGPUBindGroupEntry bg_entries[5 ] = {};
145- bg_entries[0 ].binding = 0 ;
146- bg_entries[0 ].buffer = param.buffer ;
147- bg_entries[0 ].size = param.nbytes ;
148- bg_entries[1 ].binding = 1 ;
149- bg_entries[1 ].buffer = m.buffer ;
150- bg_entries[1 ].size = m.nbytes ;
151- bg_entries[2 ].binding = 2 ;
152- bg_entries[2 ].buffer = v.buffer ;
153- bg_entries[2 ].size = v.nbytes ;
154- bg_entries[3 ].binding = 3 ;
155- bg_entries[3 ].buffer = grad.buffer ;
156- bg_entries[3 ].size = grad.nbytes ;
157- bg_entries[4 ].binding = 4 ;
158- bg_entries[4 ].buffer = uniform_buffer;
159- bg_entries[4 ].size = sizeof (params);
160-
161- WGPUBindGroupDescriptor bg_desc = {};
162- bg_desc.layout = bgl;
163- bg_desc.entryCount = 5 ;
164- bg_desc.entries = bg_entries;
165- WGPUBindGroup bind_group = wgpuDeviceCreateBindGroup (device, &bg_desc);
166-
167- graph.add_dispatch ({pipeline, bind_group, workgroup_count, " adamw_step" });
168-
169- wgpuShaderModuleRelease (shader);
170- wgpuBindGroupLayoutRelease (bgl);
171- wgpuPipelineLayoutRelease (pipeline_layout);
104+ utils::ComputePipelineBundle bundle = utils::make_compute_pipeline (
105+ device,
106+ kAdamwStepWGSL ,
107+ {
108+ {0 , WGPUBufferBindingType_Storage, param.buffer , param.nbytes },
109+ {1 , WGPUBufferBindingType_Storage, m.buffer , m.nbytes },
110+ {2 , WGPUBufferBindingType_Storage, v.buffer , v.nbytes },
111+ {3 , WGPUBufferBindingType_ReadOnlyStorage, grad.buffer , grad.nbytes },
112+ {4 , WGPUBufferBindingType_Uniform, uniform_buffer, sizeof (params)},
113+ },
114+ &wg_size_constant,
115+ 1 );
116+
117+ graph.add_dispatch (
118+ {bundle.pipeline , bundle.bind_group , workgroup_count, " adamw_step" });
119+
172120 graph.own_uniform_buffer (uniform_buffer);
173121}
174122
0 commit comments