gpt2

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GPT2

func GPT2(u ui.UI)

Types

type Block

type Block struct {
	D    int
	LN1  *neural.LayerNorm // layer norm
	Attn *llm.Attention    // self-attention
	LN2  *neural.LayerNorm // layer norm
	MLP  *MLP              // Multi-Layer Perceptron
}

Transformer block

func NewBlock

func NewBlock(d, heads int, eps float32, causal bool) *Block

func (*Block) Forward

func (block *Block) Forward(in *tensor.Tensor, tr neural.Trace) (*tensor.Tensor, neural.Trace)

Forward: x -> x + Attn(LN1(x)) -> x + MLP(LN2(x))

func (*Block) ForwardNext added in v1.1.1

func (block *Block) ForwardNext(cache *llm.KVCacheBlock, in *tensor.Tensor) *tensor.Tensor

type MLP

type MLP struct {
	*neural.Sequential
	CFC   *neural.Linear
	CProj *neural.Linear
}

GPT-2 feed-forward MLP f(x) = linear( GeLU( linear(x) ) ) x → Linear(D→4D) → GELU → Linear(4D→D) First linear == CFC Second linear = CProj

func NewMLP

func NewMLP(d int) *MLP

type Model

type Model struct {
	VocabSize     int
	EmbeddingSize int
	ContextSize   int
	Heads         int
	Layers        int
	Eps           float32
	DropP         float32

	WTE    *neural.Embeddings      // token embedding
	WPE    *llm.PositionEmbeddings // position embedding
	Blocks []*Block                // transformer blocks
	LNF    *neural.LayerNorm       // final layer norm
	LMHead *neural.Linear          // projection to vocab
}

func NewModel

func NewModel(p *ModelParams) *Model

func (*Model) Forward

func (m *Model) Forward(tokens [][]int, tr neural.Trace) (*tensor.Tensor, neural.Trace)

tokens [B][T] (T <= ContextSize) -> logits [B,T,V]

func (*Model) ForwardNext added in v1.1.1

func (m *Model) ForwardNext(cache *llm.KVCache, token int, predict bool) *tensor.Tensor

func (*Model) Load

func (m *Model) Load(weightsFile string, u ui.UI) error

type ModelParams

type ModelParams struct {
	VocabSize     int
	EmbeddingSize int
	Heads         int
	Layers        int
	ContextSize   int
	Eps           float32
}

type Tokenizer

type Tokenizer struct {
	// token string -> id
	Vocab map[string]int

	// id -> token string
	InvVocab []string
	// contains filtered or unexported fields
}

Tokenizer supports decoding token IDs using vocab.json. It implements the GPT-2 byte decoder so you get normal UTF-8 text out.

func NewTokenizer

func NewTokenizer(vocabFile string) (*Tokenizer, error)

func (*Tokenizer) DebugSample

func (t *Tokenizer) DebugSample(n int)

Optional helper: print a few tokens for debugging.

func (*Tokenizer) Decode

func (t *Tokenizer) Decode(ids []int) string

Decode converts token ids into text (UTF-8), by concatenating decoded tokens.

func (*Tokenizer) DecodeID

func (t *Tokenizer) DecodeID(id int) string

DecodeID returns the decoded string for a single token id. This decodes GPT-2 byte-encoded token string into UTF-8 text.

func (*Tokenizer) EncodeID

func (t *Tokenizer) EncodeID(token string) (int, bool)

EncodeID returns the token id for a token string. The input must be the exact GPT-2 token string (already byte-encoded).

Jump to

Keyboard shortcuts

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