sample

package
v0.21.2 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2026 License: MIT Imports: 2 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 id, shape [B]
	Logprob     *mlx.Array // sampled-token logprob, shape [B,1]; nil unless Logprobs
	TopTokens   *mlx.Array // top-K token ids, shape [B,K]; nil unless TopLogprobs > 0
	TopLogprobs *mlx.Array // top-K logprobs, shape [B,K]; nil unless TopLogprobs > 0
}

Result bundles the outputs of one decode step. The logprob tensors are populated only when the sampler is configured to report them.

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 {
	Options
	// contains filtered or unexported fields
}

func New

func New(opts Options) *Sampler

func (*Sampler) AppendToken

func (s *Sampler) AppendToken(token *mlx.Array)

func (*Sampler) Free

func (s *Sampler) Free()

func (*Sampler) ResetHistory

func (s *Sampler) ResetHistory(history []int32)

func (*Sampler) Sample

func (s *Sampler) Sample(logits *mlx.Array) Result

Sample runs the configured transform chain on the raw per-token logits and returns the sampled token id plus, when configured, the reported log-probability tensors for the selected token and the top-K tokens.

type Transform

type Transform func(*Sampler, *mlx.Array) *mlx.Array

Jump to

Keyboard shortcuts

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