sample

package
v0.23.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 3, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options added in v0.21.1

type Options struct {
	Temperature      float32
	TopP             float32
	MinP             float32
	TopK             int
	RepeatLastN      int
	RepeatPenalty    float32
	PresencePenalty  float32
	FrequencyPenalty float32

	// Logprobs causes Sample to populate Result.Logprob with the selected
	// token's log-probability. TopLogprobs (when > 0) adds top-K pairs.
	Logprobs    bool
	TopLogprobs int
}

type Result added in v0.21.1

type Result struct {
	Token       *mlx.Array // sampled token ids,       shape [B]
	Logprob     *mlx.Array // sampled-token logprobs,  shape [B,1];    nil unless any registered slot has Logprobs
	TopTokens   *mlx.Array // top-K token ids,         shape [B,maxK]; nil unless any registered slot has TopLogprobs>0
	TopLogprobs *mlx.Array // top-K logprobs,          shape [B,maxK]; same
}

Result bundles the outputs of one decode step. Logprob/TopTokens/ TopLogprobs are populated whenever any registered slot has Logprobs (respectively TopLogprobs>0). Consumers need to filter by their per-slot Options.

func (Result) Arrays added in v0.21.1

func (r Result) Arrays() []*mlx.Array

Arrays returns the tensor fields as a slice so callers can drive the mlx lifecycle verbs (Pin, Unpin, Eval, AsyncEval) over the whole group. Unset fields stay nil; the mlx helpers skip them.

type Sampler

type Sampler struct {
	// contains filtered or unexported fields
}

Sampler is a batched, slot-based sampler. Sequences are registered with Add and released with Remove. Each Sample call takes a subset of registered slots (in any order) with their [B,V] logits, samples one token per row, and appends it to that slot's ring-buffer history. Slots not named in a given call are untouched.

func New

func New(numCtx int) *Sampler

New constructs an empty sampler with no registered slots. numCtx is the runner's context window and must be positive.

func (*Sampler) Add added in v0.22.0

func (s *Sampler) Add(seqID int, opts Options, priorTokens []int32)

Add registers a sequence under seqID. The last RepeatLastN entries of priorTokens seed the ring buffer.

func (*Sampler) Free

func (s *Sampler) Free()

Free releases the pooled history tensor and resets the sampler to the New-equivalent state so it may be reused.

func (*Sampler) Remove added in v0.22.0

func (s *Sampler) Remove(seqID int)

Remove releases the slot. The pool tensor is rebuilt to drop the row.

func (*Sampler) Sample

func (s *Sampler) Sample(seqIDs []int, logits *mlx.Array) Result

Sample draws one token per row of logits ([B,V]); seqIDs[i] names the slot whose logits live at row i. Each sampled token is appended to its slot's ring. Slots not named in seqIDs are untouched.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL