pub struct SamplingKernel { /* private fields */ }Expand description
GPU sampling kernel — owns compiled pipelines for softmax, top-k, and categorical sampling.
Construct with SamplingKernel::new. All heavy GPU resources (pipelines,
bind-group layouts) are created once at construction time and reused across
calls.
Implementations§
Source§impl SamplingKernel
impl SamplingKernel
Sourcepub fn new(_context: ()) -> GpuResult<Self>
pub fn new(_context: ()) -> GpuResult<Self>
Stub constructor when the gpu feature is disabled.
Always returns Err(GpuError::NoAdapter).
Sourcepub fn softmax(&self, logits: &[f32], temperature: f32) -> GpuResult<Vec<f32>>
pub fn softmax(&self, logits: &[f32], temperature: f32) -> GpuResult<Vec<f32>>
Apply temperature scaling and compute softmax probabilities.
logits— raw logit vector (host slice), lengthn_vocab.temperature— sampling temperature.0.0→ argmax (degenerate distribution with 1.0 at the argmax, 0.0 elsewhere).
Returns a host Vec<f32> of normalised probabilities.
Sourcepub fn top_k(&self, probs: &[f32], k: usize) -> GpuResult<(Vec<f32>, Vec<u32>)>
pub fn top_k(&self, probs: &[f32], k: usize) -> GpuResult<(Vec<f32>, Vec<u32>)>
Extract top-k probability/index pairs.
probs— normalised probability distribution, host slice of lengthn_vocab.k— number of candidates to extract. Must satisfyk ≤ n_vocab.
Returns (topk_probs, topk_idxs) as host Vecs of length k.
Sourcepub fn sample(&self, probs: &[f32], idxs: &[u32], seed: u64) -> GpuResult<u32>
pub fn sample(&self, probs: &[f32], idxs: &[u32], seed: u64) -> GpuResult<u32>
Sample one token from a probability distribution.
probs— probability values (need not sum to 1.0; the shader walks the raw CDF, so partial sums work too as long as the uniform variate is within range).idxs— token IDs corresponding to each entry inprobs.seed— 64-bit seed for the LCG RNG.
Returns the sampled token ID as a u32.
Auto Trait Implementations§
impl Freeze for SamplingKernel
impl RefUnwindSafe for SamplingKernel
impl Send for SamplingKernel
impl Sync for SamplingKernel
impl Unpin for SamplingKernel
impl UnsafeUnpin for SamplingKernel
impl UnwindSafe for SamplingKernel
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more