@@ -37,6 +37,50 @@ static_assert(
3737 sizeof (EmbeddingParams) == 32 ,
3838 " EmbeddingParams must be 32 bytes" );
3939
40+ // Resize hook body: recompute counts/dispatch; out = indices dims +
41+ // [embed_dim].
42+ void resize_embedding_q4gsw (
43+ WebGPUGraph& g,
44+ int indices_id,
45+ int out_id,
46+ uint32_t embed_dim,
47+ uint32_t blocks_per_row,
48+ uint32_t gs_u,
49+ uint32_t groups_per_row,
50+ uint32_t bytes_per_row,
51+ uint32_t wg_size,
52+ size_t dispatch_idx,
53+ WGPUBuffer params_buf) {
54+ const auto & id = g.cur_dims (indices_id);
55+ const uint64_t ni = utils::numel_of (id);
56+ if (ni == 0 ) {
57+ throw std::runtime_error (" WebGPU embedding_q4gsw: zero indices" );
58+ }
59+ const uint64_t total_blocks = ni * blocks_per_row;
60+ if (total_blocks > UINT32_MAX ) {
61+ throw std::runtime_error (
62+ " WebGPU embedding_q4gsw: total_blocks exceeds uint32" );
63+ }
64+ std::vector<int64_t > od = id;
65+ od.push_back (static_cast <int64_t >(embed_dim));
66+ g.set_cur_dims (out_id, od);
67+ EmbeddingParams p = {};
68+ p.embed_dim = embed_dim;
69+ p.blocks_per_row = blocks_per_row;
70+ p.num_indices = static_cast <uint32_t >(ni);
71+ p.group_size = gs_u;
72+ p.groups_per_row = groups_per_row;
73+ p.bytes_per_row = bytes_per_row;
74+ p.total_blocks = static_cast <uint32_t >(total_blocks);
75+ wgpuQueueWriteBuffer (g.queue (), params_buf, 0 , &p, sizeof (p));
76+ g.dispatch_at (dispatch_idx).workgroup_count_x =
77+ utils::compute_1d_workgroup_count (
78+ g.device (),
79+ static_cast <uint32_t >(total_blocks),
80+ wg_size,
81+ " embedding_q4gsw(resize)" );
82+ }
83+
4084// arg order mirrors Vulkan EmbeddingQ4gsw.cpp.
4185void embedding_q4gsw_impl (WebGPUGraph& graph, const std::vector<int >& args) {
4286 const int weight_id = args.at (0 );
@@ -241,34 +285,18 @@ void embedding_q4gsw_impl(WebGPUGraph& graph, const std::vector<int>& args) {
241285 wg_size,
242286 dispatch_idx,
243287 params_buf](WebGPUGraph& g) {
244- const auto & id = g.cur_dims (indices_id);
245- const uint64_t ni = utils::numel_of (id);
246- if (ni == 0 ) {
247- throw std::runtime_error (" WebGPU embedding_q4gsw: zero indices" );
248- }
249- const uint64_t total_blocks = ni * blocks_per_row;
250- if (total_blocks > UINT32_MAX ) {
251- throw std::runtime_error (
252- " WebGPU embedding_q4gsw: total_blocks exceeds uint32" );
253- }
254- std::vector<int64_t > od = id;
255- od.push_back (static_cast <int64_t >(embed_dim));
256- g.set_cur_dims (out_id, od);
257- EmbeddingParams p = {};
258- p.embed_dim = embed_dim;
259- p.blocks_per_row = blocks_per_row;
260- p.num_indices = static_cast <uint32_t >(ni);
261- p.group_size = gs_u;
262- p.groups_per_row = groups_per_row;
263- p.bytes_per_row = bytes_per_row;
264- p.total_blocks = static_cast <uint32_t >(total_blocks);
265- wgpuQueueWriteBuffer (g.queue (), params_buf, 0 , &p, sizeof (p));
266- g.dispatch_at (dispatch_idx).workgroup_count_x =
267- utils::compute_1d_workgroup_count (
268- g.device (),
269- static_cast <uint32_t >(total_blocks),
270- wg_size,
271- " embedding_q4gsw(resize)" );
288+ resize_embedding_q4gsw (
289+ g,
290+ indices_id,
291+ out_id,
292+ embed_dim,
293+ blocks_per_row,
294+ gs_u,
295+ groups_per_row,
296+ bytes_per_row,
297+ wg_size,
298+ dispatch_idx,
299+ params_buf);
272300 });
273301
274302 wgpuShaderModuleRelease (shader);
0 commit comments