Last updated: 2026-09-07
Sampling
Compute Argmax
Wrap the GPU argmax reduction used for greedy token sampling.
This helper owns the compute pipeline for the two-phase argmax shader and records the reduction dispatches that pick the next token entirely on GPU.
1 exports shown
struct
ArgmaxDispatch
pub const ArgmaxDispatch = struct GPU-accelerated two-phase argmax reduction for greedy token sampling.
Methods
5method
ArgmaxDispatch.init
pub fn init( instance: *const Instance, shader_dir: []const u8, allocator: std.mem.Allocator, ) !ArgmaxDispatch Create the argmax compute pipeline and descriptor pool on the given Vulkan instance.
method
ArgmaxDispatch.record
pub fn record( self: *const ArgmaxDispatch, cmd: *CommandBuffer, descriptor_set: vk.c.VkDescriptorSet, n_logits: u32, phase0_workgroups: u32, ) !void Record the two-phase argmax reduction into a command buffer.
Phase 0 dispatches `phase0_workgroups` workgroups that each reduce a slice of the logit vector and write partial (value, index) results; phase 1 dispatches a single workgroup that reduces those partials to the final winner. A compute barrier is inserted between the two phases.
method
ArgmaxDispatch.allocDescriptorSet
pub fn allocDescriptorSet(self: *const ArgmaxDispatch) !vk.c.VkDescriptorSet Allocate a descriptor set from the argmax descriptor pool.
method
ArgmaxDispatch.writeDescriptorSet
pub fn writeDescriptorSet( self: *const ArgmaxDispatch, descriptor_set: vk.c.VkDescriptorSet, logits_buf: vk.c.VkBuffer, logits_size: vk.c.VkDeviceSize, partials_buf: vk.c.VkBuffer, partials_size: vk.c.VkDeviceSize, result_buf: vk.c.VkBuffer, result_size: vk.c.VkDeviceSize, ) void Bind the logits, partials, and result buffers to a descriptor set via `vkUpdateDescriptorSets`.
The three buffers map to shader bindings 0, 1, and 2 respectively.
method
ArgmaxDispatch.deinit
pub fn deinit(self: *ArgmaxDispatch) void Destroy the pipeline and descriptor pool.