Documentation
¶
Index ¶
- Constants
- type Model
- func (m *Model) ForGeneration() decode.IncrementalModelFn
- func (m *Model) ForTraining() decode.IterativeModelFn
- func (m *Model) FromContext(ctx *context.Context) *Model
- func (m *Model) WithBias(use bool) *Model
- func (m *Model) WithDType(dtype dtypes.DType) *Model
- func (m *Model) WithDropout(rate float64) *Model
- func (m *Model) WithFFNDim(dim int) *Model
- func (m *Model) WithLayerNorm(use bool) *Model
- func (m *Model) WithMaxPosEmbed(maxLen int) *Model
- func (m *Model) WithPositionalEmbedding(encoder pos.Encoder) *Model
- func (m *Model) WithRoPE(baseFreq float64) *Model
Constants ¶
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 NewFromContext ¶
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 ¶
FromContext configures the model with optional hyperparameters from the context. This allows fine-tuning an existing model configuration.
func (*Model) WithDropout ¶
WithDropout sets dropout rate.
func (*Model) WithFFNDim ¶
WithFFNDim sets FFN dimension.
func (*Model) WithLayerNorm ¶
WithLayerNorm toggles layer normalization.
func (*Model) WithMaxPosEmbed ¶
WithMaxPosEmbed sets max positional embedding length.
func (*Model) WithPositionalEmbedding ¶
WithPositionalEmbedding sets the positional encoder.