model

package
v0.15.3 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2026 License: MIT Imports: 28 Imported by: 9

Documentation

Index

Constants

View Source
const (
	TOKEN_TYPE_NORMAL = iota + 1
	TOKEN_TYPE_UNKNOWN
	TOKEN_TYPE_CONTROL
	TOKEN_TYPE_USER_DEFINED
	TOKEN_TYPE_UNUSED
	TOKEN_TYPE_BYTE
)

Variables

View Source
var (
	ErrNoVisionModel        = errors.New("this model is missing data required for image input")
	ErrUnsupportedModel     = errors.New("model not supported")
	ErrUnsupportedTokenizer = errors.New("tokenizer not supported")
)

Functions

func Forward

func Forward(ctx ml.Context, m Model, batch input.Batch) (ml.Tensor, error)

func Register

func Register(name string, f func(fs.Config) (Model, error))

Register registers a model constructor for the given architecture

Types

type Base

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

Base implements the common fields and methods for all models

func (*Base) Backend

func (m *Base) Backend() ml.Backend

Backend returns the underlying backend that will run the model

func (*Base) Config

func (m *Base) Config() config

type BytePairEncoding

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

func NewBytePairEncoding

func NewBytePairEncoding(vocab *Vocabulary, pretokenizers ...string) BytePairEncoding

func (BytePairEncoding) Decode

func (bpe BytePairEncoding) Decode(ids []int32) (string, error)

func (BytePairEncoding) Encode

func (bpe BytePairEncoding) Encode(s string, addSpecial bool) ([]int32, error)

func (BytePairEncoding) Is

func (bpe BytePairEncoding) Is(id int32, special Special) bool

func (BytePairEncoding) Vocabulary added in v0.6.7

func (bpe BytePairEncoding) Vocabulary() *Vocabulary

type Model

type Model interface {
	Forward(ml.Context, input.Batch) (ml.Tensor, error)

	Backend() ml.Backend
	Config() config
}

Model implements a specific model architecture, defining the forward pass and any model-specific configuration

func New

func New(modelPath string, params ml.BackendParams) (Model, error)

New initializes a new model instance with the provided configuration based on the metadata in the model file

type MultimodalProcessor added in v0.6.0

type MultimodalProcessor interface {
	// EncodeMultimodal processes a single input (such as an image) and
	// generates an output (typically an embedding) that can be used by the model.
	//
	// The return value is one or more tensors, each with optional model-specific
	// opaque metadata. Typically, the tensors might be views into an embedding
	// with each view representing a chunk of data that can be processed independently
	// in different batches.
	//
	// The result may be cached by the runner.
	EncodeMultimodal(ml.Context, []byte) ([]input.Multimodal, error)

	// PostTokenize is called after tokenization to allow the model to edit the
	// input stream to correctly arrange multimodal elements.
	//
	// The input is a slice of tokens with the results of EncodeMultimodal interleaved
	// in the order that the user provided them. Each element of the slice will be
	// either a single token or single multimodal object.
	//
	// The model must ensure that inputs are stored according to how they will be
	// processed and stored in the cache. For example, Llava-style models should insert
	// placeholder tokens equal to the feature size of the corresponding image with
	// the image itself attached to and split across these tokens. When Forward is called
	// a partial subset of these tokens may be submitted according to the batch size.
	//
	// This function is also responsible for updating MultimodalHash for any Multimodal
	// that is modified to ensure that there is a unique hash value that accurately
	// represents the contents.
	PostTokenize([]*input.Input) ([]*input.Input, error)
}

MultimodalProcessor must be implemented by multimodal models.

type SentencePiece added in v0.12.0

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

func NewSentencePiece added in v0.12.0

func NewSentencePiece(vocab *Vocabulary) SentencePiece

func (SentencePiece) Decode added in v0.12.0

func (spm SentencePiece) Decode(ids []int32) (string, error)

func (SentencePiece) Encode added in v0.12.0

func (spm SentencePiece) Encode(s string, addSpecial bool) ([]int32, error)

func (SentencePiece) Is added in v0.12.0

func (spm SentencePiece) Is(id int32, special Special) bool

func (SentencePiece) Vocabulary added in v0.12.0

func (spm SentencePiece) Vocabulary() *Vocabulary

type Special

type Special int32
const (
	SpecialBOS Special = iota
	SpecialEOS
)

type Tag

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

type TextProcessor

type TextProcessor interface {
	Encode(s string, addSpecial bool) ([]int32, error)
	Decode([]int32) (string, error)
	Is(int32, Special) bool
	Vocabulary() *Vocabulary
}

func NewTextProcessor added in v0.6.0

func NewTextProcessor(s string) (TextProcessor, error)

type Validator added in v0.15.0

type Validator interface {
	Validate() error
}

Validator is an optional interface that models can implement to perform validation after tensors have been loaded. If validation fails, model loading will fail with the returned error.

type Vocabulary

type Vocabulary struct {
	Values []string
	Types  []int32
	Scores []float32
	Merges []string

	BOS, EOS       []int32
	AddBOS, AddEOS bool
	// contains filtered or unexported fields
}

func (*Vocabulary) Decode

func (v *Vocabulary) Decode(id int32) string

func (*Vocabulary) Encode

func (v *Vocabulary) Encode(s string) int32

func (*Vocabulary) Is

func (v *Vocabulary) Is(id int32, special Special) bool

func (*Vocabulary) Merge

func (v *Vocabulary) Merge(left, right string) int

func (*Vocabulary) SpecialVocabulary

func (v *Vocabulary) SpecialVocabulary() []string

type WordPiece added in v0.12.0

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

func NewWordPiece added in v0.12.0

func NewWordPiece(vocab *Vocabulary, lowercase bool) WordPiece

func (WordPiece) Decode added in v0.12.0

func (wpm WordPiece) Decode(ids []int32) (string, error)

Decode implements TextProcessor.

func (WordPiece) Encode added in v0.12.0

func (wpm WordPiece) Encode(s string, addSpecial bool) ([]int32, error)

Encode implements TextProcessor.

func (WordPiece) Is added in v0.12.0

func (wpm WordPiece) Is(id int32, special Special) bool

Is implements TextProcessor.

func (WordPiece) Vocabulary added in v0.12.0

func (wpm WordPiece) Vocabulary() *Vocabulary

Vocabulary implements TextProcessor.

Jump to

Keyboard shortcuts

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