Skip to main content

SamplingKernel

Struct SamplingKernel 

Source
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

Source

pub fn new(_context: ()) -> GpuResult<Self>

Stub constructor when the gpu feature is disabled.

Always returns Err(GpuError::NoAdapter).

Source

pub fn softmax(&self, logits: &[f32], temperature: f32) -> GpuResult<Vec<f32>>

Apply temperature scaling and compute softmax probabilities.

  • logits — raw logit vector (host slice), length n_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.

Source

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 length n_vocab.
  • k — number of candidates to extract. Must satisfy k ≤ n_vocab.

Returns (topk_probs, topk_idxs) as host Vecs of length k.

Source

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 in probs.
  • seed — 64-bit seed for the LCG RNG.

Returns the sampled token ID as a u32.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.