transformer

package
v0.27.1 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2026 License: Apache-2.0 Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	ParamVocabSize    = "transformer_vocab_size"
	ParamEmbedDim     = "transformer_embed_dim"
	ParamNumLayers    = "transformer_num_layers"
	ParamNumHeads     = "transformer_num_heads"
	ParamHeadDim      = "transformer_head_dim"
	ParamFFNDim       = "transformer_ffn_dim"
	ParamMaxPosEmbed  = "transformer_max_pos_embed"
	ParamDType        = "transformer_dtype"
	ParamDropout      = "transformer_dropout"
	ParamUseLayerNorm = "transformer_use_layer_norm"
	ParamUseBias      = "transformer_use_bias"

	// Legacy RoPE parameters, used to configure PosEmbed if set.
	ParamUseRoPE      = "transformer_use_rope"
	ParamRoPEBaseFreq = "transformer_rope_base_freq"
)

Hyperparameter keys for context configuration

Variables

This section is empty.

Functions

This section is empty.

Types

type Model

type Model struct {
	VocabSize    int          // Vocabulary size
	EmbedDim     int          // Embedding dimension
	NumLayers    int          // Transformer layers
	NumHeads     int          // Attention heads per layer
	HeadDim      int          // Head dimension
	FFNDim       int          // Feed-forward hidden dimension
	MaxPosEmbed  int          // Max positional embedding length
	DType        dtypes.DType // Data type
	Dropout      float64      // Dropout rate (0.0 = none)
	UseLayerNorm bool         // Use layer normalization
	UseBias      bool         // Use bias in dense layers

	PosEmbed pos.Encoder // Positional encoder (e.g. RoPE). If nil, standard absolute positional embeddings are used.
}

Model holds configuration for building a cached transformer model. Model configures a cached transformer model.

func New

func New(vocabSize, embedDim, numLayers, numHeads, headDim int) *Model

New creates a default transformer configuration.

func NewFromContext

func NewFromContext(ctx *context.Context) *Model

NewFromContext creates a transformer model configured from context hyperparameters. It reads parameters with the following keys (with defaults):

  • transformer_vocab_size (required, no default)
  • transformer_embed_dim (required, no default)
  • transformer_num_layers (required, no default)
  • transformer_num_heads (required, no default)
  • transformer_head_dim (required, no default)
  • transformer_ffn_dim (default: embed_dim * 4)
  • transformer_max_pos_embed (default: 512)
  • transformer_dtype (default: "float32")
  • transformer_dropout (default: 0.0)
  • transformer_use_rope (default: false)
  • transformer_rope_base_freq (default: 10000.0)
  • transformer_use_layer_norm (default: true)
  • transformer_use_bias (default: true)

Example usage:

ctx.SetParams(map[string]any{
    "transformer_vocab_size": 50257,
    "transformer_embed_dim": 768,
    "transformer_num_layers": 12,
    "transformer_num_heads": 12,
    "transformer_head_dim": 64,
})
model := transformer.NewFromContext(ctx)

func (*Model) ForGeneration

func (m *Model) ForGeneration() decode.IncrementalModelFn

ForGeneration returns a model function for generation (incremental forward with KV cache).

func (*Model) ForTraining

func (m *Model) ForTraining() decode.IterativeModelFn

ForTraining returns a model function for training (full-sequence forward, no KV cache).

func (*Model) FromContext

func (m *Model) FromContext(ctx *context.Context) *Model

FromContext configures the model with optional hyperparameters from the context. This allows fine-tuning an existing model configuration.

func (*Model) WithBias

func (m *Model) WithBias(use bool) *Model

WithBias toggles dense bias.

func (*Model) WithDType

func (m *Model) WithDType(dtype dtypes.DType) *Model

WithDType sets data type.

func (*Model) WithDropout

func (m *Model) WithDropout(rate float64) *Model

WithDropout sets dropout rate.

func (*Model) WithFFNDim

func (m *Model) WithFFNDim(dim int) *Model

WithFFNDim sets FFN dimension.

func (*Model) WithLayerNorm

func (m *Model) WithLayerNorm(use bool) *Model

WithLayerNorm toggles layer normalization.

func (*Model) WithMaxPosEmbed

func (m *Model) WithMaxPosEmbed(maxLen int) *Model

WithMaxPosEmbed sets max positional embedding length.

func (*Model) WithPositionalEmbedding

func (m *Model) WithPositionalEmbedding(encoder pos.Encoder) *Model

WithPositionalEmbedding sets the positional encoder.

func (*Model) WithRoPE

func (m *Model) WithRoPE(baseFreq float64) *Model

WithRoPE enables RoPE with base frequency.

Jump to

Keyboard shortcuts

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