A hybrid language model architecture in Rust that combines a global n-gram probabilistic base with an episodic memory system via Contextual RAG. The goal is strong local generalization from the n-gram layer and deterministic recall of extended contexts from the retrieval layer, with Sticky Gating to resist stylistic drift within a conversation thread.
1. BPE tokenization – Extracts a compact vocabulary from the corpus using Byte Pair Encoding, enabling semantic encoding without an oversized dictionary.
2. Sparse N-gram with backoff – An order-4 sparse n-gram model trained with backoff. Handles syntax and unseen contexts.
3. Episodic indexing – The corpus is segmented into episodes, each with its own probabilistic mapping tracking contextual state histories up to RETR_MAX_CTX = 64 tokens.
Prevents abrupt context switching by giving the active episode a score bonus:
Score_current = L_current + STICKY_BONUS
A competing episode must beat the incumbent by a decisive margin to take over:
Score_new > Score_current + SWITCH_MARGIN
Token probabilities blend the global n-gram and the active episode, weighted by match depth:
alpha = alpha_max * min(1.0, L_match / RETR_MAX_CTX)
P_final(x) = (1 - alpha) * P_global(x) + alpha * P_episode(x)
P_T(x) = P_final(x)^(1/T)p = 0.90.src/main.rs)| Parameter | Default | Description |
|---|---|---|
RETR_MAX_CTX |
64 | Max exact-match search depth |
RETR_ALPHA_MAX |
0.92 | Max episode authority over global grammar |
STICKY_BONUS |
3.0 | Score advantage for the current episode |
SWITCH_MARGIN |
2.5 | Required delta for an episode to take over |
MIN_MATCH_TO_STICK |
6 | Minimum match depth for sticky protection |
TEMPERATURE |
0.90 | Sampling temperature |
TOP_P |
0.90 | Nucleus truncation |
REPEAT_PENALTY |
0.25 | Repetition suppression (0.25 = 75% reduced chance) |
RECENT_PENALTY |
0.55 | Recent loop suppression |
cargo build --release --bin erlm
# target/release/erlm
</a>
MIT.