Documentation
¶
Index ¶
- Constants
- Variables
- func ActiveModelName() string
- func BuildKVTestIDs(tok *Tokenizer) []int
- func CompareKVPrefill(model *Model, cfg *Config, tok *Tokenizer, cacheSize int) float32
- func EnsureModelFiles(u ui.UI)
- func FormatChat(user string) string
- func FormatSystemPrefix() string
- func FormatUserTurn(user string) string
- func GraphDisplayName() string
- func Qwen(modelArg string, u ui.UI)
- func RunChat(u ui.UI)
- func RunKVCacheTest(u ui.UI) error
- func SetModel(name string) error
- type Attention
- type Block
- type ChatSession
- type Config
- type KVForwardCompare
- type MLP
- type Model
- func (m *Model) CaptureGraph(tokens []int, g *graph.Collector)
- func (m *Model) Forward(tokens [][]int) *tensor.Tensor
- func (m *Model) ForwardNext(cache *llm.KVCache, token int, predict bool, tr neural.Trace) (*tensor.Tensor, neural.Trace)
- func (m *Model) ForwardPrefill(cache *llm.KVCache, ids []int, predict bool, tr neural.Trace) (*tensor.Tensor, neural.Trace)
- func (m *Model) Load(weightsDir string, u ui.UI) error
- type ModelSpec
- type RMSNorm
- type RoPECache
- type Tokenizer
- type TurnProfile
Constants ¶
const KVLogitDriftMax = float32(0.05)
KVLogitDriftMax is max allowed |full-inc| logit diff on the same device.
Variables ¶
var ( Model05B = ModelSpec{ Name: "Qwen2.5-0.5B-Instruct", URL: "https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct/resolve/main", } Model15B = ModelSpec{ Name: "Qwen2.5-1.5B-Instruct", URL: "https://huggingface.co/Qwen/Qwen2.5-1.5B-Instruct/resolve/main", } Model3B = ModelSpec{ Name: "Qwen2.5-3B-Instruct", URL: "https://huggingface.co/Qwen/Qwen2.5-3B-Instruct/resolve/main", Shards: []string{ "model-00001-of-00002.safetensors", "model-00002-of-00002.safetensors", }, } Model7B = ModelSpec{ Name: "Qwen2.5-7B-Instruct", URL: "https://huggingface.co/Qwen/Qwen2.5-7B-Instruct/resolve/main", Shards: []string{ "model-00001-of-00004.safetensors", "model-00002-of-00004.safetensors", "model-00003-of-00004.safetensors", "model-00004-of-00004.safetensors", }, } )
var DisableKVCache = false
DisableKVCache forces full-sequence forward each step.
var ProfileEnabled = false
ProfileEnabled prints prefill/decode timing after each reply (--profile).
Functions ¶
func ActiveModelName ¶
func ActiveModelName() string
func BuildKVTestIDs ¶ added in v1.3.0
BuildKVTestIDs builds a short multi-turn chat prefix for KV cache checks.
func CompareKVPrefill ¶ added in v1.3.0
CompareKVPrefill checks batched ForwardPrefill vs token-by-token ForwardNext.
func EnsureModelFiles ¶
EnsureModelFiles downloads config, weights, and tokenizer if missing.
func FormatChat ¶
FormatChat is an alias for a single-turn instruct prompt.
func FormatSystemPrefix ¶ added in v1.3.0
func FormatSystemPrefix() string
FormatSystemPrefix is the default system block at conversation start.
func FormatUserTurn ¶
FormatUserTurn builds one user message plus assistant header for generation.
func GraphDisplayName ¶ added in v1.3.0
func GraphDisplayName() string
GraphDisplayName is the short label shown in the graph UI.
func RunKVCacheTest ¶ added in v1.3.0
RunKVCacheTest loads the active model and compares KV incremental vs full forward.
Types ¶
type Attention ¶
type Attention struct {
HiddenSize int
NumHeads int
NumKVHeads int
HeadDim int
KVRepeat int
Scale float32
RoPE *RoPECache
QOut int
KOut int
VOut int
QKVProj *neural.Linear // D -> q+k+v (fused q/k/v)
OProj *neural.Linear // H*hd -> D
}
Attention implements multi-head self-attention with GQA and RoPE.
func NewAttention ¶
func NewAttention(cfg *Config, rope *RoPECache, rep ...tensor.AllocReporter) *Attention
type ChatSession ¶
type ChatSession struct {
Model *Model
Tokenizer *Tokenizer
Graph *graph.Collector
Cache *llm.KVCache
Tokens []int
RNG *rand.Rand
ContextSize int
MaxGen int
Temperature float32
TopK int
// contains filtered or unexported fields
}
ChatSession holds loaded model state for multi-turn chat.
func NewChatSession ¶
func NewChatSession(u ui.UI) (*ChatSession, error)
NewChatSession loads model, tokenizer, and prepares KV cache.
func (*ChatSession) Generate ¶
func (s *ChatSession) Generate(u ui.UI, logits *tensor.Tensor, prof *TurnProfile) string
Generate streams assistant tokens to u until stop or max length.
func (*ChatSession) Prefill ¶
func (s *ChatSession) Prefill(ids []int) *tensor.Tensor
Prefill runs a user turn through the cache and returns logits for the first reply token.
func (*ChatSession) Reply ¶
func (s *ChatSession) Reply(user string, u ui.UI)
Reply encodes a user turn, prefills it, and generates the assistant response.
func (*ChatSession) Reset ¶
func (s *ChatSession) Reset()
Reset clears conversation history and KV cache.
type Config ¶
type Config struct {
VocabSize int `json:"vocab_size"`
HiddenSize int `json:"hidden_size"`
NumHiddenLayers int `json:"num_hidden_layers"`
NumAttentionHeads int `json:"num_attention_heads"`
NumKeyValueHeads int `json:"num_key_value_heads"`
IntermediateSize int `json:"intermediate_size"`
MaxPositionEmbedding int `json:"max_position_embeddings"`
RopeTheta float64 `json:"rope_theta"`
RMSNormEps float32 `json:"rms_norm_eps"`
TieWordEmbeddings bool `json:"tie_word_embeddings"`
BosTokenID int `json:"bos_token_id"`
EosTokenID int `json:"eos_token_id"`
}
Config holds Qwen2 model hyperparameters from config.json.
func LoadConfig ¶
type KVForwardCompare ¶ added in v1.3.0
type KVForwardCompare struct {
TokenCount int
MaxDiff float32
WorstIndex int
FullArgmax int
IncArgmax int
}
KVForwardCompare summarizes incremental KV decode vs one-shot full forward.
func CompareKVForward ¶ added in v1.3.0
func CompareKVForward(model *Model, cfg *Config, ids []int, cacheSize int) KVForwardCompare
CompareKVForward checks each position: incremental ForwardNext vs full Forward logits.
type Model ¶
type Model struct {
Config *Config
RoPE *RoPECache
Embed *neural.Embeddings
Blocks []*Block
Norm *RMSNorm
LMHead *tensor.Tensor // [D, V] tied to embed when configured
}
Model is Qwen2 causal LM.
func (*Model) CaptureGraph ¶ added in v1.3.0
CaptureGraph records one full forward pass for tokens, then freezes g.
func (*Model) ForwardNext ¶
func (m *Model) ForwardNext(cache *llm.KVCache, token int, predict bool, tr neural.Trace) (*tensor.Tensor, neural.Trace)
ForwardNext runs one token with KV cache; returns logits [B,1,V] when predict=true.
type ModelSpec ¶
type ModelSpec struct {
Name string
URL string
Shards []string // empty = single model.safetensors
}
ModelSpec describes a HuggingFace Qwen2.5 instruct checkpoint.
type RMSNorm ¶
RMSNorm scales by root-mean-square without mean centering.
func NewRMSNorm ¶
func NewRMSNorm(d int, eps float32, rep ...tensor.AllocReporter) *RMSNorm
type RoPECache ¶
type RoPECache struct {
HeadDim int
Cos *tensor.Tensor // [maxPos, headDim]
Sin *tensor.Tensor // [maxPos, headDim]
}
RoPECache holds precomputed cos/sin tables for rotary embeddings.
type Tokenizer ¶
type Tokenizer struct {
Vocab map[string]int
InvVocab []string
Merges map[string]int // "a b" -> rank
ByteEnc map[byte]rune
ByteDec map[rune]byte
Special map[string]int
Regex *regexp.Regexp
}
Tokenizer encodes/decodes Qwen2 BPE text.
func NewTokenizer ¶
func (*Tokenizer) ChatEosIDs ¶ added in v1.3.0
ChatEosIDs returns end-of-sequence ids for soft stop heuristics (not im_start).
func (*Tokenizer) ChatStopIDs ¶ added in v1.3.0
ChatStopIDs returns token ids that end assistant generation.