Comprehensive technical reference for the Losion open-source AI framework. This document covers every component in detail, with mathematical foundations, implementation specifics, and design rationale — written for both human researchers and AI agents.
Version: v2.0.0 — Alive Gradients & Production Ready
- Overview
- Architecture Diagram
- Gradient Flow Architecture (v1.9.0)
- Jalur 1: SSM Terpadu
- Jalur 2: Attention + Compression
- Jalur 3: Specialized Retrieval (MoE)
- Adaptive Router
- Evoformer Feedback (5 Levels)
- Dual Memory System
- Training Pipeline (4-Phase)
- Inference Pipeline
- Reasoning Engine
- Elastic Inference
- Output Pipeline
- Parameter Computation
- Design Decisions & Justification
- Comparison with Other Architectures
Losion is a generative AI architecture built on the Tri-Jalur Router paradigm — three complementary computational pathways whose contributions are dynamically weighted per-token by an adaptive router. The name Tri-Jalur (Indonesian: "Three Pathways") reflects the core insight that no single computational primitive — whether attention, state-space modeling, or retrieval — is optimal for all tokens in all contexts.
Version 2.0.0 fixes the last remaining dead gradient path: the AuxFreeMoE MTP loss.
Before: MTPMoEHead inside AuxFreeMoE computed mtp_loss during forward pass and stored it in auxiliary_losses["mtp_loss"]. However, LosionForCausalLMV2.forward() never extracted this loss from the routing info and added it to the model's total loss. This meant that 32.2% of model parameters (all MTPMoEHead.pred_heads tensors) received zero gradient — they were computed but never learned.
After: LosionForCausalLMV2.forward() now iterates over all layers' routing_info["retrieval_aux"] dicts, extracts "mtp_loss" tensors with requires_grad=True, averages them, and adds them to the total loss:
# v2.0.0: Propagate AuxFreeMoE MTP loss to total loss
for layer_info in routing_info_list:
ret_aux = layer_info.get("retrieval_aux")
if isinstance(ret_aux, dict) and "mtp_loss" in ret_aux:
mtp_l = ret_aux["mtp_loss"]
if mtp_l.requires_grad:
moe_mtp_loss += mtp_l
n_moe_mtp += 1
if n_moe_mtp > 0:
loss += moe_mtp_loss / n_moe_mtpThis ensures that every parameter in the model — including the MTPMoEHead prediction heads that provide complementary training signal for expert specialization — now receives training gradients.
Version 1.9.0 introduces two major architectural themes:
-
Complete Gradient Flow: Every component in the architecture — Evoformer feedback, Dual Memory, ThinkingToggle, and all pathway sub-layers — now maintains a fully differentiable path from output to parameters. This eliminates gradient dead-ends that existed in prior versions where in-place buffer updates and detached states could block training signal.
-
Vectorized Attention: Mamba-2 uses cumsum-based parallel scan (no Python loop), RWKV-7 uses parallel cumsum WKV, and Lightning Attention uses vectorized
pair_maskcomputation. These replace sequential Python loops with batched tensor operations for 3-10× training speedup.
Traditional transformer architectures (e.g., GPT) apply a single uniform mechanism — self-attention — to all tokens. This is suboptimal because:
- Sequential dependencies (syntax, temporal patterns) do not require O(n²) attention; they are better modeled by O(n) state-space models.
- Reasoning (logical inference, multi-step comparison) genuinely benefits from full attention but is computationally expensive.
- Factual knowledge is better served by explicit retrieval (MoE + engram) than by distributing facts across dense attention weights.
Losion resolves this by assigning each type of computation to a pathway optimized for it:
| Pathway | Name | Optimized For | Complexity | Key Innovation |
|---|---|---|---|---|
| 1 | SSM Terpadu | Long-range sequential dependencies | O(n) | Mamba-2 SSD + Mamba-3 + RWKV-7 + Routing Mamba + Liquid SSM + PoST Decay + FG2-GDN + Structured Sparse + DeltaNet interleaved |
| 2 | Attention + Compression | Reasoning with memory efficiency | O(n·d_latent) | MLA+KDA compression (8× savings), Lightning Attention, Gated Attention, MoBA, iRoPE |
| 3 | Specialized Retrieval | Factual & domain-specific knowledge | O(n·k) sparse | AuxFreeMoE + S'MoRE + Symbolic-MoE + ∞-MoE + Engram Memory |
- Hardware-Agnostic: Pure PyTorch — runs on NVIDIA (CUDA) and AMD (ROCm) without code changes.
torch.compile()provides graph optimization without custom kernels. - Aux-Loss-Free: Router uses bias-based routing (DeepSeek-V3 style) — no auxiliary loss needed for load balancing.
- Adaptive Computation: Router adjusts compute per-token based on complexity; thinking mode activates deeper processing via sigmoid soft-blending (fully differentiable).
- Memory-Efficient: MLA KV compression (8× reduction) + SSM linear recurrence + progressive KV compression + Dual Memory system.
- Scalable: From 1B (prototype) to 48B+ (production) with identical architecture, varying only
d_model,n_layers, and expert counts. - Inference-Scalable: MCTS, parallel thinking, and neuro-symbolic verification allow trading compute for quality at inference time.
- Complete Gradient Flow: All feedback loops (Evoformer, DualMemory, ThinkingToggle) maintain differentiable paths from loss to parameters.
Agent Context: Losion is configured via
LosionConfig(seelosion/config.py). Sub-configs:SSMConfig,AttentionConfig,RetrievalConfig,RouterConfig,AttnResConfig,EvoformerConfig,Child3WConfig,AnchoredDecoderConfig,DualMemoryConfig,OutputConfig,JEPAConfig,DAPOConfig,RLVRConfig,PrefetchConfig,TrainingConfig,HardwareConfig,QuantizationConfig. Entry point:LosionModelV2(seelosion/models/losion_model_v2.py).
Complete data flow through a single LosionLayer with all v1.9.0 components:
INPUT x [B, S, d_model]
│
┌────────────────┼────────────────┐
│ │ │
┌─────▼─────┐ ┌─────▼─────┐ ┌──────▼──────┐
│ SSM Norm │ │ Attn Norm │ │ Retr. Norm │
│ (RMSNorm) │ │ (RMSNorm) │ │ (RMSNorm) │
└─────┬─────┘ └─────┬─────┘ └──────┬──────┘
│ │ │
┌──────────▼──────────┐ │ │
│ JALUR 1: SSM │ │ │
│ Terpadu Layer │ │ │
│ ┌───────────────┐ │ │ │
│ │ Interleaving │ │ │ │
│ │ Scheduler │ │ │ │
│ │ (4:1:1 ratio) │ │ │ │
│ └───────┬───────┘ │ │ │
│ │ │ │ │
│ ┌───────▼───────┐ │ │ │
│ │ ┌─────────┐ │ │ │ │
│ │ │ Mamba-2 │ │ │ │ │
│ │ │ SSD │──┤ │ │ │
│ │ └─────────┘ │ │ │ │
│ │ ┌─────────┐ │ │ │ │
│ │ │ Mamba-3 │ │ │ │ │
│ │ │ (half │──┤ │ │ │
│ │ │ state) │ │ │ │ │
│ │ └─────────┘ │ │ │ │
│ │ ┌─────────┐ │ │ │ │
│ │ │ RWKV-7 │ │ │ │ │
│ │ │ WKV │──┤ │ │ │
│ │ └─────────┘ │ │ │ │
│ │ ┌─────────┐ │ │ │ │
│ │ │ Routing │ │ │ │ │
│ │ │ Mamba │──┤ │ │ │
│ │ └─────────┘ │ │ │ │
│ │ ┌─────────┐ │ │ │ │
│ │ │ Liquid │ │ │ │ │
│ │ │ SSM │──┤ │ │ │
│ │ └─────────┘ │ │ │ │
│ │ ┌─────────┐ │ │ │ │
│ │ │ PoST │ │ │ │ │
│ │ │ Decay │──┤ │ │ │
│ │ └─────────┘ │ │ │ │
│ │ ┌─────────┐ │ │ │ │
│ │ │ Str.Sparse──┤ │ │ │
│ │ │ SSM │ │ │ │ │
│ │ └─────────┘ │ │ │ │
│ │ ┌─────────┐ │ │ │ │
│ │ │ FG2-GDN │ │ │ │ │
│ │ │(Fine-Gr. │──┤ │ │ │
│ │ │ Gated ΔN)│ │ │ │ │
│ │ └─────────┘ │ │ │ │
│ │ ┌─────────┐ │ │ │ │
│ │ │ Gated │ │ │ │ │
│ │ │DeltaNet │──┤ │ │ │
│ │ └─────────┘ │ │ │ │
│ └───────┬───────┘ │ │ │
│ │ │ │ │
│ ssm_out [B,S,D] │ │ │
└──────────┬──────────┘ │ │
│ │ │
│ ┌───────────▼───────────┐ │
│ │ JALUR 2: Attention + │ │
│ │ Compression │ │
│ │ ┌─────────────────┐ │ │
│ │ │ MLA + KDA │ │ │
│ │ │ (KV Compress + │ │ │
│ │ │ Key-Dep Attn) │ │ │
│ │ └────────┬────────┘ │ │
│ │ │ │ │
│ │ ┌────────▼────────┐ │ │
│ │ │ Lightning Attn │ │ │
│ │ │ (vectorized │ │ │
│ │ │ linear+local) │ │ │
│ │ └────────┬────────┘ │ │
│ │ │ │ │
│ │ ┌────────▼────────┐ │ │
│ │ │ Gated Attention │ │ │
│ │ │ (Qwen sigmoid) │ │ │
│ │ └────────┬────────┘ │ │
│ │ │ │ │
│ │ ┌────────▼────────┐ │ │
│ │ │ MoBA │ │ │
│ │ │ (Block Attn) │ │ │
│ │ └────────┬────────┘ │ │
│ │ │ │ │
│ │ ┌────────▼────────┐ │ │
│ │ │ Child-3W │ │ │
│ │ │ (QKV MoE) │ │ │
│ │ └────────┬────────┘ │ │
│ │ │ │ │
│ │ ┌────────▼────────┐ │ │
│ │ │ AttnRes │ │ │
│ │ │ (Attn Residual)│ │ │
│ │ └────────┬────────┘ │ │
│ │ │ │ │
│ │ ┌────────▼────────┐ │ │
│ │ │ Shared Attention│ │ │
│ │ │ (Zamba2-style) │ │ │
│ │ └────────┬────────┘ │ │
│ │ │ │ │
│ │ ┌────────▼────────┐ │ │
│ │ │ Cross-Jalur │ │ │
│ │ │ Routing │ │ │
│ │ └────────┬────────┘ │ │
│ │ │ │ │
│ │ ┌────────▼────────┐ │ │
│ │ │ Context Ext. │ │ │
│ │ │ + iRoPE │ │ │
│ │ └────────┬────────┘ │ │
│ │ │ │ │
│ │ attn_out [B,S,D] │ │
│ └───────────┬───────────┘ │
│ │ │
│ │ ┌────────────▼────────────┐
│ │ │ JALUR 3: Specialized │
│ │ │ Retrieval │
│ │ │ ┌──────────────────┐ │
│ │ │ │ Engram Memory │ │
│ │ │ │ (Hash-based │ │
│ │ │ │ Fact Store) │ │
│ │ │ └────────┬─────────┘ │
│ │ │ │ │
│ │ │ ┌────────▼─────────┐ │
│ │ │ │ MoE Pool: │ │
│ │ │ │ AuxFreeMoE │ │
│ │ │ │ S'MoRE │ │
│ │ │ │ Symbolic-MoE │ │
│ │ │ │ ∞-MoE │ │
│ │ │ │ MoHGE │ │
│ │ │ │ Expert Choice │ │
│ │ │ │ Gradient Routed │ │
│ │ │ │ Matryoshka MoE │ │
│ │ │ │ Asymmetric MoE │ │
│ │ │ │ Heterogeneous │ │
│ │ │ └────────┬─────────┘ │
│ │ │ │ │
│ │ │ ┌────────▼─────────┐ │
│ │ │ │ Gated Fusion │ │
│ │ │ │ (Engram + MoE) │ │
│ │ │ └────────┬─────────┘ │
│ │ │ │ │
│ │ │ retrieval_out [B,S,D] │
│ │ └───────────┬─────────────┘
│ │ │
┌─────▼────────────────▼────────────────▼─────┐
│ MERGE + ROUTING │
│ │
│ ┌─────────────────────────────────────┐ │
│ │ Adaptive Router │ │
│ │ ┌──────────┐ ┌──────────────────┐ │ │
│ │ │ Bias │ │ Thinking │ │ │
│ │ │ Router │ │ Toggle │ │ │
│ │ └────┬─────┘ └────┬─────────────┘ │ │
│ │ │ │ │ │
│ │ ┌────┴─────┐ │ │ │
│ │ │Symbolic │ │ │ │
│ │ │ MoE │ │ │ │
│ │ └────┬─────┘ │ │ │
│ │ └──────┬──────┘ │ │
│ │ │ │ │
│ │ routing_weights [B, S, 3] │ │
│ │ [w_ssm, w_attn, w_retr] │ │
│ └──────────────┬────────────────────────┘ │
│ │ │
│ ┌──────────────▼────────────────────────┐ │
│ │ Evoformer Co-evolution (L5) │ │
│ │ RouterExpertCoevolve adjustment │ │
│ └──────────────┬────────────────────────┘ │
│ │ │
│ merged = w_ssm·ssm_out + │
│ w_attn·attn_out + │
│ w_retr·retrieval_out │
│ │
│ output = x + merged (residual) │
│ output = PostMergeNorm(output) │
└──────────────────────┬────────────────────────┘
│
OUTPUT [B, S, d_model]
│
┌─────────────────────┼───────────────────────┐
│ │ │
┌─────▼─────┐ ┌─────▼─────┐ ┌──────▼──────┐
│ Evoformer │ │ Dual │ │ Output │
│ Feedback │ │ Memory │ │ Pipeline │
│ (5 Levels) │ │ (Working + │ │ (L-MTP + │
│ LayerRecy │ │ Long-term)│ │ Speculative│
│ TokenRecy │ │ │ │ + Mirror + │
│ DecFeedbk │ │ │ │ Anchored │
│ PredRecy │ │ │ │ Diffusion) │
│ RouterCoE │ │ │ │ │
└───────────┘ └───────────┘ └──────┬──────┘
│
LOGITS [B, S, V]
Agent Context: The
LosionLayerclass inlosion/models/losion_model.pyandLosionModelV2inlosion/models/losion_model_v2.pyimplement this entire flow. Each layer instantiatesSSMTerpaduLayer,AttentionKompresiLayer,RetrievalTerpaduLayer, andAdaptiveRouter. TheLosionModelV2adds Evoformer feedback, AttnRes, Dual Memory, and enhanced output pipeline.
Version 1.9.0 introduces complete gradient flow through all pathways, eliminating dead gradient paths that existed in previous versions.
In v0.3–v1.8, several components used in-place buffer updates or detached tensors for stability during training, which inadvertently blocked gradient flow:
- Evoformer LayerRecycling: Revision signal was only applied to shallow layers; deep layers (including the final output) had no gradient path through revision parameters.
- Evoformer RouterExpertCoevolve: In-place
nn.Parameterupdates withtorch.no_grad()meantexpert_state_updateandstate_gateparameters received no gradient. - DualMemory:
LongTermMemory.consolidate()updated a buffer in-place; theoutput_projandstate_projparameters could receive no gradient from the buffer. - ThinkingToggle: The
depth_multiplierused a hard step function, preventing gradient flow to the complexity estimation network.
Before: Revision applied to shallow layers only; recycled[-1] (the output used by the model) had no revision gradient.
After: Deep layers also receive a residual revision signal:
# v1.9.0: Deep layers receive revision residual
for i, h in enumerate(hidden_states):
if i < mid:
revised.append(h + revision * (0.1 if i < mid // 2 else 0.2))
else:
revised.append(h + revision * 0.05) # NEW: deep layers tooThis ensures recycled[-1] carries gradient through the revision path to all layer_recycling parameters.
Before: In-place self.coevolve_state.data[...] = ... with torch.no_grad() blocked all gradient.
After: update_state() returns the differentiable update tensor. The forward pass accumulates a tiny contribution to preserve the gradient path:
# v1.9.0: Differentiable path through co-evolve
total_update = torch.zeros(1, device=routing_weights.device)
for idx, output in enumerate(pathway_outputs):
if idx < self.num_pathways:
update = self.update_state(idx, output) # Returns differentiable tensor
total_update = total_update + update.sum() * 0.001
adjusted = adjusted + total_update.unsqueeze(-1) * 0 # Preserve grad, zero contributionThe in-place state update still uses detached values for stability, but the returned tensor preserves the differentiable path through expert_state_update and state_gate.
Before: LongTermMemory.retrieve() projected the stale buffer through output_proj, but the buffer content was detached.
After: DualMemorySystem.read() establishes a direct differentiable path:
# v1.9.0: Direct differentiable path through LTM params
# Process current input x (not detached) through consolidation pipeline
x_pooled = x.mean(dim=(0, 1))
ltm_direct = self.long_term_memory.output_proj(
self.long_term_memory.state_proj(x_pooled)
)
return x + 0.05 * memory_context + 0.01 * ltm_directThis ensures key_proj, value_proj, query, state_proj, and output_proj all receive gradients during training.
Before: Hard step function — depth_multiplier = 1.0 + complexity if complexity > threshold else 1.0 — zero gradient when below threshold.
After: Smooth sigmoid blending makes depth_multiplier fully differentiable:
# v1.9.0: Fully differentiable depth_multiplier
depth_multiplier = 1.0 + complexity * sigmoid(W_blend · x) # Smooth blendThe sigmoid function provides non-zero gradient everywhere, allowing the complexity estimation network to be trained end-to-end.
In v1.9.0, entropy regularization is applied from ALL layers, not just layer 0. This prevents entropy collapse in deep layers:
where
Jalur 1 is the sequential processing backbone of Losion. Instead of using a single SSM variant, Losion combines nine innovations in one coherent SSMTerpaduLayer with a configurable interleaving pattern.
Mamba-2 SSD implements the State Space Duality principle from Gu & Dao (2024).
v1.9.0 Optimization: Uses cumsum-based parallel scan (no Python loop) for 3-10× training speedup:
# v1.9.0: Vectorized cumsum-based parallel scan
dA = torch.exp(dt.unsqueeze(-1) * A) # [B, S, d_state]
dB = dt.unsqueeze(-1) * B # [B, S, d_state]
# Cumulative product for state evolution
dA_cumprod = torch.cumprod(dA, dim=1) # Parallel scan via cumsum in log space
# ... vectorized output computationDiscrete SSM formulation:
Default chunk size: 256 tokens (balances parallelism and memory usage).
Agent Context: Implemented in
Mamba2SSDclass (lossion/core/ssm/mamba2.py). Key params:d_model,d_state(default 64),d_conv(default 4),expand(default 2),chunk_size(default 256).
Mamba-3 (arXiv:2603.15569) is an evolution of Mamba-2 with three key improvements from an inference-first perspective:
-
Reduced state dimension (
d_state=32vs Mamba-2's 64): Half the state size but with better utilization through optimized S4D initialization and dual token shift. -
Dual Token Shift: Two independent shift patterns (inspired by RWKV) — forward shift and backward shift — combined via learnable mixing coefficient:
alpha = torch.sigmoid(self.mix_alpha) mixed = alpha * shift_fwd_proj(x_fwd) + (1 - alpha) * shift_bwd_proj(x_bwd) return x + mixed
-
Inference-first dt discretization: Clamped exponential and softplus-stabilized input scaling prevent numerical instability on long sequences:
dt_clamped = dt.clamp(min=0.0, max=dt_clamp_max) dA = torch.exp((dt_clamped.unsqueeze(-1) * A).clamp(max=0.0)) # Always ≤ 1 dt_stabilized = F.softplus(dt_clamped) dB = dt_stabilized.unsqueeze(-1) * B
Agent Context:
Mamba3SSDclass (lossion/core/ssm/mamba3.py). Defaultd_state=32. IncludesDualTokenShiftmodule.
RWKV-7 implements WKV recurrence — an evolution of attention that replaces softmax scoring with an exponentially-weighted moving average.
v1.9.0 Optimization: Uses parallel cumsum WKV instead of sequential Python loop:
# v1.9.0: Vectorized WKV via parallel cumsum
decay = torch.exp(w) # [B, S, H] — data-dependent decay
# Cumulative product of decay for parallel scan
decay_cumprod = torch.cumprod(decay, dim=1)
# ... vectorized numerator/denominator computationKey advantages:
-
O(1) inference: Only stores
(wkv_state, sum_state) - Unbounded context: State accumulates without sequence length limit
-
Explicit forgetting: Decay factor
$a_t = \exp(w_t)$ allows selective "forgetting"
Agent Context:
RWKV7WKVclass (lossion/core/ssm/rwkv7.py). Theuparameter (position bonus) is learned per-head.
Routing Mamba from Microsoft Research (NeurIPS 2025) scales SSM parameters using sparse mixtures of linear projection experts. Instead of a single set of B, C, dt projections, Routing Mamba maintains multiple expert projection sets and routes each token to a sparse subset:
Load balancing: DeepSeek-V3 aux-loss-free approach — EMA tracking of expert load with periodic bias updates (non-gradient).
Shared parameters: A_log matrix and D skip connection are shared across all experts.
Agent Context:
RoutingMambaclass (lossion/core/ssm/routing_mamba.py). Config:routing_mamba_num_experts(default 4),routing_mamba_active_experts(default 2).
Liquid SSM extends Mamba-2 with input-adaptive time constants controlled by a ComplexityGate:
ComplexityGate estimates per-token, per-head complexity and maps it to one of three depth levels:
- Depth 1 (fast): Single SSD pass — minimal compute for easy tokens
- Depth 2 (standard): SSD + one additional sub-layer
- Depth 3 (deep): Full interleaving through all sub-layers
Liquid time-constant rule:
where d_inner dimensions.
Training: Soft blending of all depth outputs with depth probabilities (for gradient flow). Inference: Hard early exit for easy tokens.
Agent Context:
LiquidSSMTerpaduLayerclass (lossion/core/ssm/liquid_ssm.py). IncludesComplexityGateandLiquidSSD. Depth entropy loss:depth_entropy_weight(default 0.01).
PoST (Position-Dependent Decay Spectra) replaces the single learnable decay parameter per head with a spectrum of decay rates that vary by position:
where:
-
$\gamma(m) \in (0,1)$ : Learnable decay rate per mode (sigmoid oflog_gamma) -
$\text{mix}_t(m)$ : Position-dependent mixing weights (softmax of position embedding + MLP)
This allows the SSM to retain information long-term (slow decay modes) while focusing on local context (fast decay modes) — position-dependent.
Agent Context:
PoSTDecaySSMclass (lossion/core/ssm/post_decay.py). IncludesDecaySpectrumsubmodule. Defaultn_decay_modes=4.
Based on NeurIPS 2025 (poster 118046), this replaces the diagonal transition matrix with structured off-diagonal elements enabling FSA (Finite State Automata) state tracking.
Key insight: Diagonal SSMs can only track O(log S) FSA states. Structured sparse transitions can track O(S) states — provably optimal.
Three sparsity patterns:
- Block-diagonal: Dense B×B blocks, diagonal across blocks. Complexity O(S × B).
- Banded: Diagonal ± band_width. Complexity O(S × bandwidth).
- Butterfly: Recursive butterfly factorization. Complexity O(S × log₂(B)).
The transition is applied via first-order matrix exponential approximation:
Agent Context:
StructuredSparseSSMclass (lossion/core/ssm/structured_sparse.py). Config:n_groups(default 4),transition_type.
FG2-GDN enhances the standard GatedDeltaNet with per-head, per-position gating:
where
Learnable temperature per head controls selectivity: low temperature → sharp (selective), high temperature → smooth (uniform).
Agent Context:
FG2GDNclass (lossion/core/ssm/fg2_gdn.py). IncludesFineGrainedGate. Gate types: "sigmoid" or "softmax". Position bias optional.
The standard Gated DeltaNet implements in-context learning via the delta rule:
Unlike standard attention that only adds new information, DeltaNet can correct previously stored associations.
Agent Context:
GatedDeltaNetclass (lossion/core/ssm/delta_net.py). Alpha initialized near 1.0 viaalpha_offset.
The SSM sub-layers are interleaved with a default 4:1:1 ratio:
- 4 blocks Mamba-2 SSD: Primary parallel sequential processing
- 1 block RWKV-7 WKV: Dynamic state evolution with explicit forgetting
- 1 block Gated DeltaNet: In-context learning and state correction
Additional sub-layers (Mamba-3, Routing Mamba, Liquid SSM, PoST Decay, Structured Sparse, FG2-GDN) can be enabled via config and participate in the interleaving schedule.
Dynamic routing mode: When routing weights are provided, all sub-layers process the input simultaneously and outputs are blended:
Agent Context:
InterleavingSchedulerinlossion/core/ssm/ssm_layer.py. State tracked viaSSMState.
Jalur 2 is the reasoning engine of Losion, combining multiple attention mechanisms with MLA compression for memory efficiency.
MLA, adapted from DeepSeek-V3, compresses KV-cache representations into a lower-dimensional latent space (8× compression). KDA adds key-dependent attention bias for improved routing information.
KV Compression math:
Memory savings: For Losion-7B with n_heads=16, d_kv=128, mla_latent_dim=512:
- Full KV:
$2 \times 16 \times 128 = 4{,}096$ dimensions → MLA latent:$512$ → 8× compression
Agent Context:
MLAclass (lossion/core/attention/mla.py).KDAclass (lossion/core/attention/kda_mla.py).
Lightning Attention combines linear attention (for global context) with local windowed attention (for positional precision), both computed in a vectorized manner.
v1.9.0: Uses vectorized pair_mask computation instead of sequential masking:
# Vectorized pair_mask for causal + local window
pair_mask = causal_mask & distance_mask # Both pre-computed as [S, S] bool tensorsArchitecture:
- Global component: Linear attention with feature map (
elu,relu, orcos) - Local component: Standard softmax attention within sliding window
- Chunk-based parallel training: Processes sequence in chunks for memory efficiency
Agent Context:
LightningAttentionclass (lossion/core/attention/lightning_attention.py). Config:lightning_window_size(default 2048),lightning_chunk_size(default 4096).
Adapted from Qwen (NeurIPS 2025 Best Paper), Gated Attention applies a sigmoid gate to the attention output:
The sigmoid gate provides smooth, differentiable control over how much attention information flows to the output. Unlike ReLU or hard gates, sigmoid ensures non-zero gradient everywhere.
Agent Context:
GatedAttentionclass (lossion/core/attention/gated_attention.py). Enabled viause_gated_attentionconfig.
MoBA (Moonshot AI, NeurIPS 2025) partitions the sequence into blocks and routes each query to a top-K subset of blocks:
This reduces attention complexity from O(n²) to O(n × K × block_size) while preserving the ability to attend to relevant distant context.
Agent Context:
MoBAclass (lossion/core/attention/moba.py). Config:moba_block_size(default 512),moba_top_k_blocks(default 4).
Child-3W applies MoE routing at the QKV level: multiple child attention parameter sets with routing between them. Each child has its own Q, K, V projections, and a router selects which children to activate per token.
where
Agent Context:
Child3WConfiginlossion/config.py. Config:num_children(default 4),top_k_children(default 2).
AttnRes (MoonshotAI 2026, v0.9) replaces fixed-weight residual connections with learned attention-based aggregation across layers. Three modes:
- Full: All-layer attention residual
- Block: Group layers into blocks; within-block attention residual
- Hybrid: Full across blocks, block within
Also supports token compression via linear, gated, or SSM-based compression.
Agent Context:
AttnResConfiginlossion/config.py. Modes: "full", "block", "hybrid".
Shared Attention, adapted from Zamba2, shares attention parameters across groups of layers, with a small ratio of unique parameters per layer. This reduces parameter count while maintaining expressiveness.
Agent Context: Config:
shared_n_groups(default 1),shared_pattern("all_shared" or "interleaved"),shared_unique_ratio(default 0.25).
Cross-Jalur Routing allows information flow between the three pathways within the attention layer. A learnable blend parameter controls how much cross-pathway information is incorporated:
Agent Context:
CrossJalurRoutingclass (lossion/core/retrieval/cross_jalur_routing.py). Config:cross_jalur_blend_alpha(default 0.3),cross_jalur_graph_top_k(default 8).
Context Extension dynamically adjusts the effective context window based on input complexity.
iRoPE (Interleaved RoPE) alternates between RoPE and NoPE layers with a 3:1 ratio:
Layer: 0 1 2 3 4 5 6 7 8 ...
RoPE: ✓ ✓ ✓ ✗ ✓ ✓ ✓ ✗ ✓ ...
RoPE provides explicit positional information; NoPE allows learned attention biases for long contexts without positional extrapolation issues.
Agent Context:
InterleavedRoPEclass (lossion/core/attention/irope.py).ContextExtensionclass (lossion/core/attention/context_extension.py).
Jalur 3 handles factual knowledge and domain-specific knowledge through a layered architecture combining multiple MoE variants with Engram Memory.
AuxFreeMoE eliminates the quality-degrading auxiliary loss, replacing it with bias-based load balancing (DeepSeek-V3 style):
Mechanism:
- Standard routing:
logits = gate_proj(x) + bias - Top-K expert selection with renormalization
- Bias updated via EMA running statistics (non-gradient): overloaded experts → negative bias, underloaded → positive bias
- No auxiliary loss returned — only monitoring metrics
MTP Training Signal: AuxFreeMoE includes Multi-Token Prediction heads that predict future tokens (t+1, t+2, ..., t+n), providing a complementary training signal for expert specialization without quality degradation:
where
Agent Context:
AuxFreeMoEclass (lossion/core/retrieval/aux_free_moe.py). IncludesAuxFreeMoERouterandMTPMoEHead. Config:bias_update_rate(default 0.01).
S'MoRE (Meta, NeurIPS 2025) organizes experts in a sub-tree structure with shared residual connections:
smore_num_sub_trees(default 4): Number of shared sub-treessmore_sub_tree_depth(default 2): Depth of each sub-tree
This allows experts to share base knowledge through the sub-tree structure while specializing at the leaves.
Agent Context:
SMoREclass (lossion/core/retrieval/smore.py).
Symbolic-MoE routes tokens based on discrete skill labels rather than continuous routing weights. This provides interpretable, deterministic routing for known task types:
- Each expert is associated with a symbolic skill (e.g., "math", "code", "translation")
- Routing is determined by the task label, not learned affinity
- Complementary to continuous routing — used for known, well-defined tasks
Agent Context:
SymbolicMoEclass (lossion/core/retrieval/symbolic_moe.py). Also used in the Adaptive Router for skill-based pathway selection.
∞-MoE replaces the discrete expert pool with a continuous expert space using a codebook + hypernetwork:
- Codebook:
infinite_moe_codebook_size(default 256) codes of dimensioninfinite_moe_code_dim(default 32) - Hypernetwork: Generates expert weights from codes via
infinite_moe_hypernet_hidden(default 256) dimensional hidden layer - Low-rank residual: Optional low-rank residual for efficient expert generation
Agent Context:
InfiniteMoEclass (lossion/core/retrieval/infinite_moe.py). Config:use_infinite_moe,infinite_moe_code_dim,infinite_moe_hypernet_hidden.
MoHGE groups experts into heterogeneous groups with different architectures or capacities, allowing the model to balance computational efficiency with expressiveness.
Agent Context:
MoHGEclass (lossion/core/retrieval/mohge.py).
- Expert Choice (Google Research): Experts choose tokens — guaranteed load balance
- Gradient Routed MoE: Routing weights are conditioned on gradient information
- Matryoshka MoE: Variable-depth expert computation (elastic inference)
- Asymmetric MoE: MoE layers placed only at specific layer indices
- Heterogeneous MoE: Experts with varying dimensions
Engram Memory is a hash-based fact store that stores factual knowledge explicitly:
Advantages: O(1) retrieval, explicit storage, updateable without retraining.
Agent Context:
EngramMemoryclass (lossion/core/retrieval/engram.py). Config:num_buckets(default 1,000,000),engram_dim(default 256).
Engram and MoE outputs are combined via gated fusion:
Three fusion modes: "gated" (default), "additive", "concat".
The Adaptive Router combines three components: BiasRouter (computational allocation), ThinkingToggle (complexity detection), and Symbolic-MoE (skill-based routing).
The bias
Agent Context:
BiasRouterclass (lossion/core/router/bias_router.py). Config:num_pathways(default 3),top_k_pathways(default 2).
v1.9.0: Uses sigmoid soft-blending for depth_multiplier (fully differentiable):
This replaces the hard step function, providing non-zero gradient everywhere.
Effects of Thinking Mode:
| Aspect | Non-Thinking | Thinking |
|---|---|---|
| Routing weights | Jalur 1 dominant | Jalur 2+3 activated |
| Interleaving ratio | 5:1 (local:global) | 2:1 (local:global) |
| Pairformer | Inactive | Active |
| Depth multiplier | 1.0 | 1.0–2.0 (smooth blend) |
| Gradient flow | Via BiasRouter | Via BiasRouter + sigmoid |
Agent Context:
ThinkingToggleclass (lossion/core/router/thinking_toggle.py). Output:ThinkingAssessmentwithmode,complexity_score,dominant_task,confidence,depth_multiplier.
Symbolic-MoE provides skill-based discrete routing as a third signal in the router. For known task types (math, code, translation), it provides deterministic, interpretable routing that complements the learned BiasRouter and ThinkingToggle.
After BiasRouter, ThinkingToggle, and Symbolic-MoE produce their outputs, routing weights are adjusted via a learned network with residual connection:
adjuster_input = cat([routing_weights, complexity_score.unsqueeze(-1)], dim=-1)
adjustment = thinking_adjuster(adjuster_input) # [B, S, 3]
adjusted = routing_weights + 0.1 * adjustment # Residual with small scale
adjusted = softmax(adjusted, dim=-1)Agent Context: Full routing in
AdaptiveRouterclass (lossion/core/router/router.py). Pathway priors:[0.4, 0.3, 0.3].
Adapted from AlphaFold2's Evoformer (Nobel Prize 2024), generalized as a universal architectural principle for LLMs. Five levels of bidirectional feedback:
Deep layers revise shallow layer representations via cross-attention:
v1.9.0: Deep layers also receive revision residual (0.05 scale) so that recycled[-1] carries gradient through the revision path.
Later tokens revise earlier token representations through bidirectional attention (applied after initial causal pass):
This is NOT BERT-style — it's iterative revision AFTER the initial forward pass, preserving autoregressive reasoning.
Bidirectional feedback between the decoder and prediction modules:
The most revolutionary level: predicted token N can revise representations of tokens 1 through N-1:
Router and experts co-evolve during training. The co-evolution state captures the "negotiation" between router choices and expert specialization:
v1.9.0: update_state() returns differentiable tensor preserving gradient flow through expert_state_update and state_gate parameters, while the in-place buffer update uses detached values for stability.
Agent Context:
EvoformerManagerclass (lossion/core/feedback/evoformer.py). Config:EvoformerConfigwithn_recycling_steps(default 3), 5 toggle flags for each level.
Two-level memory system inspired by human memory:
- Direct access to recent token/layer outputs (ring buffer)
- High detail, limited capacity (
working_memory_size, default 512) - Entries are detached for persistence across forward passes
- Compressed, persistent hidden state from consolidation
- Selective, persistent, compressed (
long_term_memory_dim, default 256) - Three consolidation methods: "attention" (default), "gated", "mean"
v1.9.0: DualMemorySystem.read() establishes a direct differentiable path through LTM parameters:
# Direct differentiable path: x_pooled → state_proj → output_proj
ltm_direct = self.long_term_memory.output_proj(
self.long_term_memory.state_proj(x_pooled)
)
return x + 0.05 * memory_context + 0.01 * ltm_directThis ensures key_proj, value_proj, query, state_proj, and output_proj all receive gradients during training.
Agent Context:
DualMemorySystemclass (lossion/core/memory/dual_memory.py). IncludesWorkingMemory(ring buffer) andLongTermMemory(attention-gated consolidation). Config:DualMemoryConfig.
Losion uses a 4-phase training pipeline, progressing from individual component training to advanced RL:
Each pathway is trained independently:
- Jalur 1 (SSM): Standard language modeling with SSM-only forward pass
- Jalur 2 (Attention): Standard language modeling with attention-only forward pass
- Jalur 3 (MoE): Standard language modeling with MoE-only forward pass
All three pathways are trained jointly with the adaptive router:
- Router learns to allocate computation across pathways
- BiasRouter biases are updated via gradient (no aux loss)
- Entropy regularization from ALL layers (v1.9.0)
- LLM-JEPA: Predicts future latent states instead of next tokens
LLM-JEPA (v0.6):
where the predictor forecasts
Agent Context:
JEPAConfiginlossion/config.py.prediction_horizon(default 4),loss_type("vicreg", "cosine", "mse"),teacher_ema_decay(default 0.996).
DAPO or GRPO (auto-selected based on config):
DAPO (Decoupled Clip & Dynamic Sampling Policy Optimization, v0.8):
Four key improvements over GRPO:
- Decoupled Clip: Asymmetric clip ratios —
clip_ratio_low=0.2(looser lower bound) andclip_ratio_high=0.28(tighter upper bound to prevent reward hacking) - Dynamic Sampling: Filter prompts where all responses have the same reward (zero learning signal) — ~15-20% efficiency gain
- Token-Level Loss: Per-token policy gradient for finer credit assignment
- Overlong Filtering: Penalty for responses exceeding
max_response_length
RLVR (Reinforcement Learning with Verifiable Rewards, v0.8):
Uses objective, programmable reward functions instead of learned reward models:
- Math verification with configurable tolerance
- Code execution verification with timeout
- Format checking
- Curriculum difficulty scheduling: "easy" → "medium" → "hard"
Agent Context:
DAPOConfiginlossion/config.py.DAPOTrainerinlossion/training/dapo.py.RLVRConfiginlossion/config.py.RLVRTrainerinlossion/training/rlvr.py.
- Evolutionary search for architecture optimization
- Active learning for data efficiency
- Distillation from larger models
The LosionRecipe and LosionOrchestrator automatically select the appropriate RL method:
if config.dapo.enabled:
rl_trainer = DAPOTrainer(config.dapo, model, reward_fn)
elif config.rlvr.enabled:
rl_trainer = RLVRTrainer(config.rlvr, model, reward_fn)
else:
rl_trainer = GRPOTrainer(config.grpo, model, reward_fn)Agent Context:
LosionOrchestrator(lossion/training/losion_orchestrator.py) manages the 4-phase pipeline.LosionRecipe(lossion/training/losion_recipe.py) provides pre-configured training recipes.
"Speculating Experts" (arXiv:2603.19289) predicts which MoE experts will be needed in subsequent layers and prefetches them, overlapping expert loading latency with ongoing computation.
Architecture:
- LightweightPredictor: 2-layer MLP per layer (< 1% of single expert params) maps layer-L hidden states to layer-(L+1) expert predictions
- Finite MoE mode: Predicts discrete expert indices via top-k or temperature sampling
- ∞-MoE mode: Predicts continuous expert codes; nearby codes in the continuous space are prefetched
- Adaptive temperature: Dynamically adjusts prediction temperature based on recent accuracy (exploit when accurate, explore when not)
- Accuracy tracking: Rolling precision/recall/hit-rate/coverage metrics per layer
Prefetch pipeline:
- Receive hidden_states from layer L
- Feed into predictor[L] → predicted expert set for L+1
- Issue async prefetch (overlaps with expert compute at layer L)
- At layer L+1: check if needed experts are already loaded (hit → zero latency, miss → fallback)
Agent Context:
ExpertPrefetcherclass (lossion/inference/expert_prefetch.py). Config:PrefetchConfigwithpredictor_hidden_dim(default 128),prefetch_budget(default 4),adaptive_temperature.
QuantSpec combines quantization with speculative decoding for fast, memory-efficient inference.
Agent Context:
QuantSpecclass (lossion/inference/quantspec.py).
The paged KV cache manages memory efficiently with INT4 quantization:
- Paged allocation: KV cache is organized in fixed-size pages, avoiding contiguous memory requirements
- INT4 quantization: KV vectors are quantized to 4-bit, reducing memory by 8× compared to FP32
- MLA compatibility: Compressed latent vectors from MLA are further quantized
Agent Context:
kv_cache.py(lossion/inference/kv_cache.py).KVCacheclass with paged allocation.
Matryoshka Nested Transformer enables one weight set to produce multiple valid submodels of different sizes. At inference time, a size_selector network predicts the appropriate granularity factor per token:
- Simple tokens → smaller submodel (faster)
- Complex tokens → full model (higher quality)
- Default granularity factors: [0.25, 0.5, 0.75, 1.0]
Mix'n'Match: Different layers can use different granularity factors — e.g., early layers small (0.25), late layers full (1.0).
Agent Context:
MatryoshkaFFNclass (lossion/core/elastic/matryoshka.py). AlsoMatryoshkaMoEfor elastic expert computation.
Losion integrates three reasoning techniques for inference-time compute scaling:
AlphaZero-inspired tree search with UCB selection:
Adaptive compute budget: budget = base + (max - base) · complexity²
Agent Context:
MCTSReasonerclass (lossion/core/reasoning/mcts.py). Config:num_simulations(default 64),c_puct(default 1.5).
Explores multiple reasoning paths simultaneously:
Selection strategies: BEST_OF_N, MAJORITY_VOTE, WEIGHTED_MERGE, TOURNAMENT.
Agent Context:
ParallelThinkerclass (lossion/core/reasoning/parallel_thinking.py).
Combines neural generation with symbolic verification for formally correct reasoning:
- Symbolic Rule Engine: Learned rule embeddings with verification networks
- Error Localization: Per-token error probability
- Feedback Generation: Correction signal for failed verification
- Iterative Revision Loop: Up to
max_revision_iterations(default 3)
Agent Context:
NeuroSymbolicVerifierclass (lossion/core/reasoning/neuro_symbolic.py). Output:VerificationResultwith status (VERIFIED/FAILED/PARTIAL/UNSURE/NEEDS_REVISION).
Nested FFN structure with Matryoshka loss:
where
# Matryoshka forward (from losion/core/elastic/matryoshka.py)
if factor >= 1.0:
gate = F.silu(self.gate_proj(x))
up = self.up_proj(x)
output = self.down_proj(gate * up)
else:
d_ff_active = int(factor * self.d_ff)
gate_w = self.gate_proj.weight[:d_ff_active, :]
up_w = self.up_proj.weight[:d_ff_active, :]
down_w = self.down_proj.weight[:, :d_ff_active]
gate = F.silu(F.linear(x, gate_w))
up = F.linear(x, up_w)
output = F.linear(gate * up, down_w)Agent Context:
MatryoshkaFFNclass (lossion/core/elastic/matryoshka.py). AlsoAttnLoRAfor elastic attention adaptation.
L-MTP (v0.8, NeurIPS 2025) extends standard MTP with leap scheduling — predicting tokens at non-adjacent positions:
where
-
Geometric (default):
$\Delta_k = 2^k$ — leap 1, 2, 4, 8 tokens ahead -
Arithmetic:
$\Delta_k = k \cdot \text{step}$ — uniform spacing -
Adjacent:
$\Delta_k = k$ — standard MTP
Agent Context:
LeapMTPclass (lossion/core/output/leap_mtp.py). Config:leap_mtp_schedule,leap_mtp_num_leaps(default 4),leap_mtp_max_leap(default 8).
Standard speculative decoding with draft model:
# Draft model proposes tokens → Target model verifies
draft_tokens = draft_model.generate(x, num_tokens=draft_len)
probabilities = target_model(draft_tokens)
acceptance = check_acceptance(draft_tokens, probabilities)Agent Context:
SpeculativeDecoderclass (lossion/core/output/speculative_decoder.py).
Mirror speculative decoding uses the same model as both draft and target, with different routing strategies:
- Draft: SSM-dominant routing (fast, linear time)
- Target: Full Tri-Jalur routing (accurate)
Agent Context:
MirrorSpeculativeclass (lossion/core/output/mirror_speculative.py).
Continuous vector prediction + lightweight anchored diffusion refinement:
- Predict continuous vector (not softmax → token ID)
- Refine via 2-3 step anchored diffusion process
- Disambiguate via attention-based disambiguation heads
Uses Evoformer feedback loop (Level 3) for iterative refinement.
Agent Context:
AnchoredDiffusionDecoderclass (lossion/core/output/anchored_decoder.py). Config:AnchoredDecoderConfigwithn_refine_steps(default 3),disambiguation_heads(default 8).
For the standard Losion-7B configuration:
| Component | Parameters |
|---|---|
| Embedding | vocab_size × d_model = 32,000 × 4,096 = 131M |
| SSM Terpadu (per layer) | ~35M × n_layers = 35M × 32 = 1,120M |
| Attention + MLA (per layer) | ~25M × n_layers = 25M × 32 = 800M |
| MoE (per layer, 64 experts) | ~50M × n_layers/2 = 50M × 16 = 800M |
| Router (per layer) | ~0.5M × 32 = 16M |
| Output Pipeline | ~135M |
| Evoformer + DualMemory | ~50M |
| Total (7B) | ~3,052M (active per token) |
Due to MoE sparsity (93.75%), only ~3B parameters are active per token despite total model size of ~7B.
| Criterion | Uniform Attention | Tri-Jalur |
|---|---|---|
| Sequential tokens | O(n²) waste | O(n) SSM — optimal |
| Reasoning tokens | Full attention needed | Attention pathway — optimal |
| Factual recall | Distributed in weights | MoE + Engram — explicit |
| Compute per token | Constant (expensive) | Adaptive (cheap or expensive) |
| Memory | Full KV cache | MLA + SSM (8× savings) |
Auxiliary loss (Switch Transformer style) adds a separate loss term: $\mathcal{L} = \mathcal{L}\text{main} + \alpha \cdot \mathcal{L}\text{aux}$. This:
- Requires hyperparameter tuning (
$\alpha$ ) - Interferes with main loss optimization
- Doesn't always produce optimal load balancing
Bias-based routing (DeepSeek-V3 style) updates bias directly via gradient or EMA — no separate loss term, no quality degradation.
Standard residual connections are one-way: information flows forward only. Evoformer's bidirectional feedback allows:
- Deep layers to correct shallow layer errors (Level 1)
- Later tokens to inform earlier tokens (Level 2)
- Predictions to refine their own inputs (Levels 3-4)
- Router and experts to co-evolve (Level 5)
In prior versions, in-place buffer updates and detached tensors in Evoformer and DualMemory inadvertently blocked gradient flow, meaning some parameters never received training signal. v1.9.0 ensures:
- Every parameter has a differentiable path from loss to itself
- Feedback loop parameters (revision gates, co-evolve state) are trained effectively
- Memory system parameters (state_proj, output_proj) receive gradient signal
- ThinkingToggle can be trained end-to-end via sigmoid soft-blending
| Feature | GPT-4 | DeepSeek-V3 | Llama 4 | Gemma 3 | Losion v1.9.0 |
|---|---|---|---|---|---|
| Architecture | Dense Transformer | MoE + MLA | Dense + iRoPE | Dense + Local/Global | Tri-Jalur Router |
| Attention | Full | MLA (8× savings) | Interleaved RoPE | Local/Global | MLA + KDA + Lightning + MoBA + Gated + Child-3W |
| SSM | None | None | None | None | Mamba-2 + Mamba-3 + RWKV-7 + Routing + Liquid + PoST + Structured Sparse + FG2-GDN + DeltaNet |
| MoE | None | AuxFreeMoE | None | None | AuxFreeMoE + S'MoRE + Symbolic + ∞-MoE + MoHGE + Expert Choice + Gradient Routed + Matryoshka |
| Router | N/A | Bias-based | N/A | N/A | BiasRouter + ThinkingToggle + Symbolic-MoE |
| Feedback | None | None | None | None | Evoformer (5 levels) |
| Memory | KV cache | MLA KV cache | KV cache | KV cache | MLA + Dual Memory (Working + LTM) |
| RL Training | RLHF | GRPO | RLHF | RLHF | DAPO + GRPO + RLVR |
| Gradient Flow | Full | Full | Full | Full | Complete (v1.9.0 fix) |
| Elastic Inference | No | No | No | No | Matryoshka |
| Speculative Decoding | Yes | Yes | No | No | L-MTP + Mirror + Anchored Diffusion |
| Expert Prefetching | No | Yes | No | No | Yes (Speculating Experts) |
Losion v1.9.0 — Complete Gradient Flow & Vectorized Attention