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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
CogNet1B: Non-Transformer Language Model with Cognitive Routing
|
| 3 |
+
================================================================
|
| 4 |
+
Replaces self-attention with O(n) cognitive routing and
|
| 5 |
+
hierarchical memory, enabling linear-time sequence processing.
|
| 6 |
+
|
| 7 |
+
Key architectural innovations:
|
| 8 |
+
- CognitiveChannel: Depthwise separable conv + SwiGLU FFN (O(n) per channel)
|
| 9 |
+
- CoherenceRouter: O(n) routing via learned coherence scoring
|
| 10 |
+
- SharedHierarchicalMemory: 3-tier key-value memory (Working/Episodic/Semantic)
|
| 11 |
+
- AdaptiveComputationBlock: Variable-depth processing per token
|
| 12 |
+
- CompositionalReasoner: Hyperdimensional computing for role-filler binding
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
import math
|
| 16 |
+
import torch
|
| 17 |
+
import torch.nn as nn
|
| 18 |
+
import torch.nn.functional as F
|
| 19 |
+
from typing import Dict, Optional, Tuple
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# βββ Token Encoder βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 23 |
+
|
| 24 |
+
class TokenEncoder(nn.Module):
|
| 25 |
+
"""Token embedding + learned positional encoding."""
|
| 26 |
+
|
| 27 |
+
def __init__(self, vocab_size: int, hidden_dim: int, max_seq_len: int, dropout: float = 0.1):
|
| 28 |
+
super().__init__()
|
| 29 |
+
self.token_emb = nn.Embedding(vocab_size, hidden_dim)
|
| 30 |
+
self.pos_emb = nn.Embedding(max_seq_len, hidden_dim)
|
| 31 |
+
self.dropout = nn.Dropout(dropout)
|
| 32 |
+
self.norm = nn.LayerNorm(hidden_dim)
|
| 33 |
+
self._init_weights()
|
| 34 |
+
|
| 35 |
+
def _init_weights(self):
|
| 36 |
+
nn.init.normal_(self.token_emb.weight, mean=0.0, std=0.02)
|
| 37 |
+
nn.init.normal_(self.pos_emb.weight, mean=0.0, std=0.02)
|
| 38 |
+
|
| 39 |
+
def forward(self, input_ids: torch.Tensor) -> torch.Tensor:
|
| 40 |
+
B, T = input_ids.shape
|
| 41 |
+
positions = torch.arange(T, device=input_ids.device).unsqueeze(0).expand(B, -1)
|
| 42 |
+
x = self.token_emb(input_ids) + self.pos_emb(positions)
|
| 43 |
+
return self.dropout(self.norm(x))
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# βββ Cognitive Channel βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 47 |
+
|
| 48 |
+
class CognitiveChannel(nn.Module):
|
| 49 |
+
"""Depthwise separable convolution + SwiGLU FFN β O(n) per channel."""
|
| 50 |
+
|
| 51 |
+
def __init__(self, channel_dim: int, ff_dim: int, dropout: float = 0.1):
|
| 52 |
+
super().__init__()
|
| 53 |
+
# Depthwise separable conv
|
| 54 |
+
self.dw_conv = nn.Conv1d(
|
| 55 |
+
channel_dim, channel_dim, kernel_size=3, padding=1,
|
| 56 |
+
groups=channel_dim
|
| 57 |
+
)
|
| 58 |
+
self.pw_conv = nn.Conv1d(channel_dim, channel_dim, kernel_size=1)
|
| 59 |
+
self.conv_norm = nn.LayerNorm(channel_dim)
|
| 60 |
+
self.conv_dropout = nn.Dropout(dropout)
|
| 61 |
+
|
| 62 |
+
# SwiGLU FFN
|
| 63 |
+
self.ff_gate = nn.Linear(channel_dim, ff_dim, bias=False)
|
| 64 |
+
self.ff_up = nn.Linear(channel_dim, ff_dim, bias=False)
|
| 65 |
+
self.ff_down = nn.Linear(ff_dim, channel_dim, bias=False)
|
| 66 |
+
self.ff_norm = nn.LayerNorm(channel_dim)
|
| 67 |
+
self.ff_dropout = nn.Dropout(dropout)
|
| 68 |
+
|
| 69 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 70 |
+
# x: (B, T, D)
|
| 71 |
+
residual = x
|
| 72 |
+
# Conv path
|
| 73 |
+
h = x.transpose(1, 2) # (B, D, T)
|
| 74 |
+
h = self.dw_conv(h)
|
| 75 |
+
h = self.pw_conv(h)
|
| 76 |
+
h = h.transpose(1, 2) # (B, T, D)
|
| 77 |
+
h = self.conv_norm(h)
|
| 78 |
+
x = residual + self.conv_dropout(h)
|
| 79 |
+
|
| 80 |
+
# FFN path (SwiGLU)
|
| 81 |
+
residual = x
|
| 82 |
+
gate = F.silu(self.ff_gate(x))
|
| 83 |
+
up = self.ff_up(x)
|
| 84 |
+
h = gate * up
|
| 85 |
+
h = self.ff_down(h)
|
| 86 |
+
h = self.ff_norm(h)
|
| 87 |
+
x = residual + self.ff_dropout(h)
|
| 88 |
+
return x
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
# βββ Coherence Router ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 92 |
+
|
| 93 |
+
class CoherenceRouter(nn.Module):
|
| 94 |
+
"""O(n) routing: compute which channel handles each token."""
|
| 95 |
+
|
| 96 |
+
def __init__(self, hidden_dim: int, num_channels: int, routing_iters: int = 1):
|
| 97 |
+
super().__init__()
|
| 98 |
+
self.num_channels = num_channels
|
| 99 |
+
self.routing_iters = routing_iters
|
| 100 |
+
self.query = nn.Linear(hidden_dim, num_channels, bias=False)
|
| 101 |
+
self.key = nn.Linear(hidden_dim, num_channels, bias=False)
|
| 102 |
+
self.refine = nn.Linear(num_channels, num_channels, bias=False)
|
| 103 |
+
|
| 104 |
+
def forward(self, x: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 105 |
+
"""
|
| 106 |
+
Args:
|
| 107 |
+
x: (B, T, D)
|
| 108 |
+
Returns:
|
| 109 |
+
routing_weights: (B, T, num_channels) β soft assignment
|
| 110 |
+
channel_masks: (B, T, num_channels) β hard top-k for efficiency
|
| 111 |
+
"""
|
| 112 |
+
B, T, D = x.shape
|
| 113 |
+
q = self.query(x) # (B, T, C)
|
| 114 |
+
k = self.key(x) # (B, T, C)
|
| 115 |
+
|
| 116 |
+
# O(n) coherence: dot-product of each token's query with mean key
|
| 117 |
+
mean_key = k.mean(dim=1, keepdim=True) # (B, 1, C)
|
| 118 |
+
scores = q * mean_key # (B, T, C)
|
| 119 |
+
scores = scores + self.refine(scores) * 0.1 # one refinement step
|
| 120 |
+
routing_weights = F.softmax(scores, dim=-1) # (B, T, C)
|
| 121 |
+
|
| 122 |
+
# Hard routing: top-2 channels per token
|
| 123 |
+
_, top_idx = routing_weights.topk(2, dim=-1)
|
| 124 |
+
channel_masks = torch.zeros_like(routing_weights)
|
| 125 |
+
channel_masks.scatter_(-1, top_idx, 1.0)
|
| 126 |
+
|
| 127 |
+
return routing_weights, channel_masks
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
# βββ Shared Hierarchical Memory ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 131 |
+
|
| 132 |
+
class SharedHierarchicalMemory(nn.Module):
|
| 133 |
+
"""3-tier memory: Working β Episodic β Semantic with key-value attention."""
|
| 134 |
+
|
| 135 |
+
def __init__(self, hidden_dim: int, key_dim: int,
|
| 136 |
+
working_slots: int, episodic_slots: int, semantic_slots: int,
|
| 137 |
+
dropout: float = 0.1):
|
| 138 |
+
super().__init__()
|
| 139 |
+
self.key_dim = key_dim
|
| 140 |
+
self.working_slots = working_slots
|
| 141 |
+
self.episodic_slots = episodic_slots
|
| 142 |
+
self.semantic_slots = semantic_slots
|
| 143 |
+
|
| 144 |
+
# Key / value projections
|
| 145 |
+
self.q_proj = nn.Linear(hidden_dim, key_dim, bias=False)
|
| 146 |
+
self.k_proj = nn.Linear(hidden_dim, key_dim, bias=False)
|
| 147 |
+
self.v_proj = nn.Linear(hidden_dim, hidden_dim, bias=False)
|
| 148 |
+
self.out_proj = nn.Linear(hidden_dim, hidden_dim, bias=False)
|
| 149 |
+
|
| 150 |
+
# Learnable memory slots
|
| 151 |
+
self.working_keys = nn.Parameter(torch.randn(working_slots, key_dim) * 0.02)
|
| 152 |
+
self.working_vals = nn.Parameter(torch.randn(working_slots, hidden_dim) * 0.02)
|
| 153 |
+
self.episodic_keys = nn.Parameter(torch.randn(episodic_slots, key_dim) * 0.02)
|
| 154 |
+
self.episodic_vals = nn.Parameter(torch.randn(episodic_slots, hidden_dim) * 0.02)
|
| 155 |
+
self.semantic_keys = nn.Parameter(torch.randn(semantic_slots, key_dim) * 0.02)
|
| 156 |
+
self.semantic_vals = nn.Parameter(torch.randn(semantic_slots, hidden_dim) * 0.02)
|
| 157 |
+
|
| 158 |
+
# Gating between tiers
|
| 159 |
+
self.tier_gate = nn.Linear(hidden_dim * 3, 3, bias=False)
|
| 160 |
+
self.norm = nn.LayerNorm(hidden_dim)
|
| 161 |
+
self.dropout = nn.Dropout(dropout)
|
| 162 |
+
|
| 163 |
+
def _read_tier(self, queries: torch.Tensor, keys: torch.Tensor,
|
| 164 |
+
values: torch.Tensor) -> torch.Tensor:
|
| 165 |
+
"""
|
| 166 |
+
Read from one memory tier.
|
| 167 |
+
queries: (B, T, key_dim)
|
| 168 |
+
keys: (S, key_dim)
|
| 169 |
+
values: (S, hidden_dim)
|
| 170 |
+
Returns: (B, T, hidden_dim)
|
| 171 |
+
"""
|
| 172 |
+
B = queries.shape[0]
|
| 173 |
+
# BUG FIX: expand keys/vales to batch dim without transposing last two dims
|
| 174 |
+
keys_expanded = keys.unsqueeze(0).expand(B, -1, -1) # (B, S, key_dim)
|
| 175 |
+
values_expanded = values.unsqueeze(0).expand(B, -1, -1) # (B, S, hidden_dim)
|
| 176 |
+
|
| 177 |
+
# Scaled dot-product attention (O(n*S) but S is small)
|
| 178 |
+
scale = math.sqrt(self.key_dim)
|
| 179 |
+
# (B, T, key_dim) @ (B, key_dim, S) β (B, T, S)
|
| 180 |
+
attn = torch.bmm(queries, keys_expanded.transpose(1, 2)) / scale
|
| 181 |
+
attn = F.softmax(attn, dim=-1)
|
| 182 |
+
# (B, T, S) @ (B, S, hidden_dim) β (B, T, hidden_dim)
|
| 183 |
+
out = torch.bmm(attn, values_expanded)
|
| 184 |
+
return out
|
| 185 |
+
|
| 186 |
+
def forward(self, x: torch.Tensor) -> Tuple[torch.Tensor, Dict[str, torch.Tensor]]:
|
| 187 |
+
B, T, D = x.shape
|
| 188 |
+
queries = self.q_proj(x) # (B, T, key_dim)
|
| 189 |
+
|
| 190 |
+
# Read from each tier
|
| 191 |
+
w_out = self._read_tier(queries, self.working_keys, self.working_vals)
|
| 192 |
+
e_out = self._read_tier(queries, self.episodic_keys, self.episodic_vals)
|
| 193 |
+
s_out = self._read_tier(queries, self.semantic_keys, self.semantic_vals)
|
| 194 |
+
|
| 195 |
+
# Gated combination
|
| 196 |
+
gate_input = torch.cat([w_out, e_out, s_out], dim=-1) # (B, T, D*3)
|
| 197 |
+
gates = F.softmax(self.tier_gate(gate_input), dim=-1) # (B, T, 3)
|
| 198 |
+
combined = (gates[..., 0:1] * w_out +
|
| 199 |
+
gates[..., 1:2] * e_out +
|
| 200 |
+
gates[..., 2:3] * s_out)
|
| 201 |
+
|
| 202 |
+
# Project and residual
|
| 203 |
+
out = self.out_proj(self.v_proj(x) + combined)
|
| 204 |
+
out = self.norm(out)
|
| 205 |
+
x = x + self.dropout(out)
|
| 206 |
+
|
| 207 |
+
stats = {
|
| 208 |
+
'mem_w_gate': gates[..., 0].mean(),
|
| 209 |
+
'mem_e_gate': gates[..., 1].mean(),
|
| 210 |
+
'mem_s_gate': gates[..., 2].mean(),
|
| 211 |
+
}
|
| 212 |
+
return x, stats
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
# βββ Gated FFN (SwiGLU) βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 216 |
+
|
| 217 |
+
class GatedFFN(nn.Module):
|
| 218 |
+
"""SwiGLU feed-forward network."""
|
| 219 |
+
|
| 220 |
+
def __init__(self, hidden_dim: int, ff_dim: int, dropout: float = 0.1):
|
| 221 |
+
super().__init__()
|
| 222 |
+
self.gate_proj = nn.Linear(hidden_dim, ff_dim, bias=False)
|
| 223 |
+
self.up_proj = nn.Linear(hidden_dim, ff_dim, bias=False)
|
| 224 |
+
self.down_proj = nn.Linear(ff_dim, hidden_dim, bias=False)
|
| 225 |
+
self.norm = nn.LayerNorm(hidden_dim)
|
| 226 |
+
self.dropout = nn.Dropout(dropout)
|
| 227 |
+
|
| 228 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 229 |
+
residual = x
|
| 230 |
+
gate = F.silu(self.gate_proj(x))
|
| 231 |
+
up = self.up_proj(x)
|
| 232 |
+
h = gate * up
|
| 233 |
+
h = self.down_proj(h)
|
| 234 |
+
h = self.norm(h)
|
| 235 |
+
return residual + self.dropout(h)
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
# βββ Adaptive Computation Block ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 239 |
+
|
| 240 |
+
class AdaptiveComputationBlock(nn.Module):
|
| 241 |
+
"""Variable-depth processing: each token may take 1..max_adaptive_steps."""
|
| 242 |
+
|
| 243 |
+
def __init__(self, hidden_dim: int, ff_dim: int, max_adaptive_steps: int,
|
| 244 |
+
dropout: float = 0.1):
|
| 245 |
+
super().__init__()
|
| 246 |
+
self.max_steps = max_adaptive_steps
|
| 247 |
+
self.layers = nn.ModuleList([
|
| 248 |
+
GatedFFN(hidden_dim, ff_dim, dropout) for _ in range(max_adaptive_steps)
|
| 249 |
+
])
|
| 250 |
+
self.halt_prob = nn.Linear(hidden_dim, 1, bias=False)
|
| 251 |
+
self.norm = nn.LayerNorm(hidden_dim)
|
| 252 |
+
|
| 253 |
+
def forward(self, x: torch.Tensor) -> Tuple[torch.Tensor, Dict[str, torch.Tensor]]:
|
| 254 |
+
B, T, D = x.shape
|
| 255 |
+
output = torch.zeros_like(x)
|
| 256 |
+
total_weight = torch.zeros(B, T, 1, device=x.device)
|
| 257 |
+
|
| 258 |
+
stats = {'avg_steps': torch.tensor(0.0, device=x.device)}
|
| 259 |
+
|
| 260 |
+
for step_idx in range(self.max_steps):
|
| 261 |
+
x = self.layers[step_idx](x)
|
| 262 |
+
|
| 263 |
+
# Halting probability
|
| 264 |
+
p = torch.sigmoid(self.halt_prob(x)) # (B, T, 1)
|
| 265 |
+
|
| 266 |
+
# BUG FIX: clamp to avoid going over 1.0
|
| 267 |
+
remaining = 1.0 - total_weight
|
| 268 |
+
# Compute max allowed p (leave at least 0.01 for remaining steps)
|
| 269 |
+
steps_left = self.max_steps - step_idx
|
| 270 |
+
min_remaining = 0.01 * max(steps_left - 1, 0)
|
| 271 |
+
max_val = torch.clamp(remaining - min_remaining, min=0.01)
|
| 272 |
+
p = torch.clamp(p, min=torch.tensor(0.01, device=x.device), max=max_val)
|
| 273 |
+
|
| 274 |
+
# On last step, use all remaining weight
|
| 275 |
+
if step_idx == self.max_steps - 1:
|
| 276 |
+
p = torch.clamp(remaining, min=0.01)
|
| 277 |
+
|
| 278 |
+
output = output + p * x
|
| 279 |
+
total_weight = total_weight + p
|
| 280 |
+
|
| 281 |
+
output = self.norm(output)
|
| 282 |
+
|
| 283 |
+
avg_steps = torch.tensor(float(self.max_steps), device=x.device)
|
| 284 |
+
stats['avg_steps'] = avg_steps
|
| 285 |
+
return output, stats
|
| 286 |
+
|
| 287 |
+
|
| 288 |
+
# βββ Compositional Reasoner βββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 289 |
+
|
| 290 |
+
class CompositionalReasoner(nn.Module):
|
| 291 |
+
"""Hyperdimensional computing for role-filler binding."""
|
| 292 |
+
|
| 293 |
+
def __init__(self, hidden_dim: int, key_dim: int, dropout: float = 0.1):
|
| 294 |
+
super().__init__()
|
| 295 |
+
self.role_proj = nn.Linear(hidden_dim, key_dim, bias=False)
|
| 296 |
+
self.filler_proj = nn.Linear(hidden_dim, key_dim, bias=False)
|
| 297 |
+
self.unbind_proj = nn.Linear(key_dim, hidden_dim, bias=False)
|
| 298 |
+
self.norm = nn.LayerNorm(hidden_dim)
|
| 299 |
+
self.dropout = nn.Dropout(dropout)
|
| 300 |
+
|
| 301 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 302 |
+
residual = x
|
| 303 |
+
roles = self.role_proj(x) # (B, T, K)
|
| 304 |
+
fillers = self.filler_proj(x) # (B, T, K)
|
| 305 |
+
|
| 306 |
+
# Circular convolution as binding operation (element-wise multiply in frequency domain)
|
| 307 |
+
bound = roles * fillers # (B, T, K) β simplified HDC binding
|
| 308 |
+
|
| 309 |
+
# Shift-based unbinding for positional awareness
|
| 310 |
+
bound_shifted = torch.roll(bound, shifts=1, dims=1)
|
| 311 |
+
composed = bound + bound_shifted
|
| 312 |
+
|
| 313 |
+
out = self.unbind_proj(composed)
|
| 314 |
+
out = self.norm(out)
|
| 315 |
+
return residual + self.dropout(out)
|
| 316 |
+
|
| 317 |
+
|
| 318 |
+
# βββ Cognitive Router ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 319 |
+
|
| 320 |
+
class CognitiveRouter(nn.Module):
|
| 321 |
+
"""Routes tokens to channels based on coherence scores."""
|
| 322 |
+
|
| 323 |
+
def __init__(self, hidden_dim: int, num_channels: int, channel_dim: int,
|
| 324 |
+
routing_iters: int = 1):
|
| 325 |
+
super().__init__()
|
| 326 |
+
self.num_channels = num_channels
|
| 327 |
+
self.channel_dim = channel_dim
|
| 328 |
+
self.coherence_router = CoherenceRouter(hidden_dim, num_channels, routing_iters)
|
| 329 |
+
|
| 330 |
+
# Per-channel projections
|
| 331 |
+
self.to_channels = nn.Linear(hidden_dim, num_channels * channel_dim, bias=False)
|
| 332 |
+
self.from_channels = nn.Linear(num_channels * channel_dim, hidden_dim, bias=False)
|
| 333 |
+
|
| 334 |
+
# Channel processing
|
| 335 |
+
self.channels = nn.ModuleList([
|
| 336 |
+
CognitiveChannel(channel_dim, channel_dim * 4) for _ in range(num_channels)
|
| 337 |
+
])
|
| 338 |
+
|
| 339 |
+
self.norm = nn.LayerNorm(hidden_dim)
|
| 340 |
+
|
| 341 |
+
def forward(self, x: torch.Tensor) -> Tuple[torch.Tensor, Dict[str, torch.Tensor]]:
|
| 342 |
+
B, T, D = x.shape
|
| 343 |
+
|
| 344 |
+
# Route
|
| 345 |
+
routing_weights, channel_masks = self.coherence_router(x) # (B, T, C)
|
| 346 |
+
|
| 347 |
+
# Project to channel space
|
| 348 |
+
channel_input = self.to_channels(x) # (B, T, C*CD)
|
| 349 |
+
channel_input = channel_input.view(B, T, self.num_channels, self.channel_dim)
|
| 350 |
+
|
| 351 |
+
# Process each channel
|
| 352 |
+
channel_outputs = []
|
| 353 |
+
for c in range(self.num_channels):
|
| 354 |
+
# Weighted input for this channel
|
| 355 |
+
w = routing_weights[:, :, c:c+1].unsqueeze(-1) # (B, T, 1, 1)
|
| 356 |
+
ch_in = (channel_input[:, :, c, :] * routing_weights[:, :, c:c+1]) # (B, T, CD)
|
| 357 |
+
ch_out = self.channels[c](ch_in) # (B, T, CD)
|
| 358 |
+
channel_outputs.append(ch_out)
|
| 359 |
+
|
| 360 |
+
# Combine channels
|
| 361 |
+
combined = torch.cat(channel_outputs, dim=-1) # (B, T, C*CD)
|
| 362 |
+
out = self.from_channels(combined)
|
| 363 |
+
out = self.norm(out)
|
| 364 |
+
x = x + out
|
| 365 |
+
|
| 366 |
+
stats = {
|
| 367 |
+
'routing_entropy': -(routing_weights * (routing_weights + 1e-8).log()).sum(-1).mean(),
|
| 368 |
+
}
|
| 369 |
+
return x, stats
|
| 370 |
+
|
| 371 |
+
|
| 372 |
+
# βββ CogNet Block ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 373 |
+
|
| 374 |
+
class CogNetBlock(nn.Module):
|
| 375 |
+
"""Router + Memory Read + FFN with residual connections."""
|
| 376 |
+
|
| 377 |
+
def __init__(self, hidden_dim: int, num_channels: int, channel_dim: int,
|
| 378 |
+
ff_dim: int, key_dim: int, routing_iters: int,
|
| 379 |
+
max_adaptive_steps: int,
|
| 380 |
+
working_slots: int, episodic_slots: int, semantic_slots: int,
|
| 381 |
+
dropout: float = 0.1):
|
| 382 |
+
super().__init__()
|
| 383 |
+
self.router = CognitiveRouter(hidden_dim, num_channels, channel_dim, routing_iters)
|
| 384 |
+
self.memory = SharedHierarchicalMemory(
|
| 385 |
+
hidden_dim, key_dim, working_slots, episodic_slots, semantic_slots, dropout
|
| 386 |
+
)
|
| 387 |
+
self.adaptive_ffn = AdaptiveComputationBlock(
|
| 388 |
+
hidden_dim, ff_dim, max_adaptive_steps, dropout
|
| 389 |
+
)
|
| 390 |
+
self.composer = CompositionalReasoner(hidden_dim, key_dim, dropout)
|
| 391 |
+
self.norm = nn.LayerNorm(hidden_dim)
|
| 392 |
+
|
| 393 |
+
def forward(self, x: torch.Tensor) -> Tuple[torch.Tensor, Dict[str, torch.Tensor]]:
|
| 394 |
+
stats = {}
|
| 395 |
+
|
| 396 |
+
x, r_stats = self.router(x)
|
| 397 |
+
stats.update(r_stats)
|
| 398 |
+
|
| 399 |
+
x, m_stats = self.memory(x)
|
| 400 |
+
stats.update(m_stats)
|
| 401 |
+
|
| 402 |
+
x, a_stats = self.adaptive_ffn(x)
|
| 403 |
+
stats.update(a_stats)
|
| 404 |
+
|
| 405 |
+
x = self.composer(x)
|
| 406 |
+
x = self.norm(x)
|
| 407 |
+
|
| 408 |
+
return x, stats
|
| 409 |
+
|
| 410 |
+
|
| 411 |
+
# βββ CogNet1B ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 412 |
+
|
| 413 |
+
class CogNet1B(nn.Module):
|
| 414 |
+
"""Non-transformer language model with cognitive routing."""
|
| 415 |
+
|
| 416 |
+
def __init__(
|
| 417 |
+
self,
|
| 418 |
+
vocab_size: int = 256,
|
| 419 |
+
hidden_dim: int = 2048,
|
| 420 |
+
num_blocks: int = 13,
|
| 421 |
+
num_channels: int = 8,
|
| 422 |
+
channel_dim: int = 256,
|
| 423 |
+
ff_dim: int = 4096,
|
| 424 |
+
routing_iters: int = 1,
|
| 425 |
+
max_adaptive_steps: int = 2,
|
| 426 |
+
max_seq_len: int = 2048,
|
| 427 |
+
working_slots: int = 64,
|
| 428 |
+
episodic_slots: int = 128,
|
| 429 |
+
semantic_slots: int = 256,
|
| 430 |
+
key_dim: int = 256,
|
| 431 |
+
dropout: float = 0.1,
|
| 432 |
+
):
|
| 433 |
+
super().__init__()
|
| 434 |
+
self.vocab_size = vocab_size
|
| 435 |
+
self.hidden_dim = hidden_dim
|
| 436 |
+
self.num_blocks = num_blocks
|
| 437 |
+
self.num_channels = num_channels
|
| 438 |
+
self.channel_dim = channel_dim
|
| 439 |
+
self.ff_dim = ff_dim
|
| 440 |
+
self.max_seq_len = max_seq_len
|
| 441 |
+
|
| 442 |
+
# Encoder
|
| 443 |
+
self.encoder = TokenEncoder(vocab_size, hidden_dim, max_seq_len, dropout)
|
| 444 |
+
|
| 445 |
+
# Blocks
|
| 446 |
+
self.blocks = nn.ModuleList([
|
| 447 |
+
CogNetBlock(
|
| 448 |
+
hidden_dim, num_channels, channel_dim, ff_dim,
|
| 449 |
+
key_dim, routing_iters, max_adaptive_steps,
|
| 450 |
+
working_slots, episodic_slots, semantic_slots, dropout
|
| 451 |
+
)
|
| 452 |
+
for _ in range(num_blocks)
|
| 453 |
+
])
|
| 454 |
+
|
| 455 |
+
# Final norm
|
| 456 |
+
self.final_norm = nn.LayerNorm(hidden_dim)
|
| 457 |
+
|
| 458 |
+
# Output head (weight-tied with token embedding)
|
| 459 |
+
self.output_proj = nn.Linear(hidden_dim, vocab_size, bias=False)
|
| 460 |
+
self.output_proj.weight = self.encoder.token_emb.weight
|
| 461 |
+
|
| 462 |
+
# Initialize
|
| 463 |
+
self.apply(self._init_weights)
|
| 464 |
+
|
| 465 |
+
def _init_weights(self, module):
|
| 466 |
+
if isinstance(module, nn.Linear):
|
| 467 |
+
torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
|
| 468 |
+
if module.bias is not None:
|
| 469 |
+
torch.nn.init.zeros_(module.bias)
|
| 470 |
+
elif isinstance(module, nn.LayerNorm):
|
| 471 |
+
torch.nn.init.ones_(module.weight)
|
| 472 |
+
torch.nn.init.zeros_(module.bias)
|
| 473 |
+
|
| 474 |
+
def forward(self, input_ids: torch.Tensor,
|
| 475 |
+
return_stats: bool = False) -> Dict[str, torch.Tensor]:
|
| 476 |
+
"""
|
| 477 |
+
Args:
|
| 478 |
+
input_ids: (B, T) integer token ids
|
| 479 |
+
return_stats: whether to collect intermediate statistics
|
| 480 |
+
Returns:
|
| 481 |
+
dict with 'logits' (B, T, vocab_size) and optional 'stats'
|
| 482 |
+
"""
|
| 483 |
+
x = self.encoder(input_ids)
|
| 484 |
+
|
| 485 |
+
all_stats = {} if return_stats else None
|
| 486 |
+
|
| 487 |
+
for i, block in enumerate(self.blocks):
|
| 488 |
+
x, block_stats = block(x)
|
| 489 |
+
if return_stats:
|
| 490 |
+
for k, v in block_stats.items():
|
| 491 |
+
key = f'block{i}_{k}'
|
| 492 |
+
# BUG FIX: clamp NaN/Inf in stats
|
| 493 |
+
if isinstance(v, torch.Tensor):
|
| 494 |
+
v = v.detach().float()
|
| 495 |
+
if torch.isnan(v) or torch.isinf(v):
|
| 496 |
+
v = torch.tensor(0.0)
|
| 497 |
+
all_stats[key] = v
|
| 498 |
+
|
| 499 |
+
x = self.final_norm(x)
|
| 500 |
+
logits = self.output_proj(x)
|
| 501 |
+
|
| 502 |
+
result = {'logits': logits}
|
| 503 |
+
if return_stats:
|
| 504 |
+
result['stats'] = all_stats
|
| 505 |
+
return result
|
| 506 |
+
|
| 507 |
+
@torch.no_grad()
|
| 508 |
+
def generate(self, input_ids: torch.Tensor, max_new_tokens: int = 50,
|
| 509 |
+
temperature: float = 1.0, top_k: int = 0,
|
| 510 |
+
) -> torch.Tensor:
|
| 511 |
+
"""Autoregressive generation."""
|
| 512 |
+
self.eval()
|
| 513 |
+
for _ in range(max_new_tokens):
|
| 514 |
+
# Crop to max_seq_len
|
| 515 |
+
idx = input_ids[:, -self.max_seq_len:]
|
| 516 |
+
result = self(idx)
|
| 517 |
+
logits = result['logits'][:, -1, :] / max(temperature, 1e-8)
|
| 518 |
+
|
| 519 |
+
# Top-k filtering
|
| 520 |
+
if top_k > 0:
|
| 521 |
+
v, _ = torch.topk(logits, min(top_k, logits.size(-1)))
|
| 522 |
+
logits[logits < v[:, [-1]]] = float('-inf')
|
| 523 |
+
|
| 524 |
+
probs = F.softmax(logits, dim=-1)
|
| 525 |
+
next_token = torch.multinomial(probs, num_samples=1)
|
| 526 |
+
input_ids = torch.cat([input_ids, next_token], dim=1)
|
| 527 |
+
|
| 528 |
+
return input_ids
|
| 529 |
+
|
| 530 |
+
def count_parameters(self) -> Dict[str, int]:
|
| 531 |
+
total = sum(p.numel() for p in self.parameters())
|
| 532 |
+
trainable = sum(p.numel() for p in self.parameters() if p.requires_grad)
|
| 533 |
+
return {'total': total, 'trainable': trainable}
|
| 534 |
+
|
| 535 |
+
def get_complexity_analysis(self) -> Dict[str, str]:
|
| 536 |
+
return {
|
| 537 |
+
'architecture': 'CogNet (Non-Transformer)',
|
| 538 |
+
'routing': f'O(n) coherence routing x {self.num_channels} channels',
|
| 539 |
+
'memory': '3-tier hierarchical (Working/Episodic/Semantic)',
|
| 540 |
+
'attention': 'None (replaced by cognitive routing + memory)',
|
| 541 |
+
'ffn': 'SwiGLU with adaptive computation',
|
| 542 |
+
'composition': 'Hyperdimensional role-filler binding',
|
| 543 |
+
'sequence_complexity': 'O(n) per layer (vs O(n^2) for transformers)',
|
| 544 |
+
'params': f'{self.count_parameters()["total"]:,}',
|
| 545 |
+
}
|
| 546 |
+
|
| 547 |
+
|
| 548 |
+
# βββ Factory Functions βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 549 |
+
|
| 550 |
+
def create_cognet_1b_small(vocab_size: int = 256, max_seq_len: int = 2048,
|
| 551 |
+
dropout: float = 0.1) -> CogNet1B:
|
| 552 |
+
"""Create ~87M parameter model."""
|
| 553 |
+
return CogNet1B(
|
| 554 |
+
vocab_size=vocab_size,
|
| 555 |
+
hidden_dim=1024,
|
| 556 |
+
num_blocks=8,
|
| 557 |
+
num_channels=8,
|
| 558 |
+
channel_dim=128,
|
| 559 |
+
ff_dim=2048,
|
| 560 |
+
routing_iters=1,
|
| 561 |
+
max_adaptive_steps=2,
|
| 562 |
+
max_seq_len=max_seq_len,
|
| 563 |
+
working_slots=32,
|
| 564 |
+
episodic_slots=64,
|
| 565 |
+
semantic_slots=128,
|
| 566 |
+
key_dim=256,
|
| 567 |
+
dropout=dropout,
|
| 568 |
+
)
|
| 569 |
+
|
| 570 |
+
|
| 571 |
+
def create_cognet_1b(vocab_size: int = 256, max_seq_len: int = 2048,
|
| 572 |
+
dropout: float = 0.1) -> CogNet1B:
|
| 573 |
+
"""Create ~1B parameter model."""
|
| 574 |
+
return CogNet1B(
|
| 575 |
+
vocab_size=vocab_size,
|
| 576 |
+
hidden_dim=2048,
|
| 577 |
+
num_blocks=13,
|
| 578 |
+
num_channels=8,
|
| 579 |
+
channel_dim=256,
|
| 580 |
+
ff_dim=4096,
|
| 581 |
+
routing_iters=1,
|
| 582 |
+
max_adaptive_steps=2,
|
| 583 |
+
max_seq_len=max_seq_len,
|
| 584 |
+
working_slots=64,
|
| 585 |
+
episodic_slots=128,
|
| 586 |
+
semantic_slots=256,
|
| 587 |
+
key_dim=256,
|
| 588 |
+
dropout=dropout,
|
| 589 |
+
)
|
| 590 |
+
|
| 591 |
+
|
| 592 |
+
# βββ Self-Test βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 593 |
+
|
| 594 |
+
if __name__ == '__main__':
|
| 595 |
+
print("=" * 60)
|
| 596 |
+
print("CogNet1B Self-Test")
|
| 597 |
+
print("=" * 60)
|
| 598 |
+
|
| 599 |
+
# Small model for quick test
|
| 600 |
+
model = CogNet1B(
|
| 601 |
+
vocab_size=128,
|
| 602 |
+
hidden_dim=128,
|
| 603 |
+
num_blocks=2,
|
| 604 |
+
num_channels=4,
|
| 605 |
+
channel_dim=32,
|
| 606 |
+
ff_dim=256,
|
| 607 |
+
routing_iters=1,
|
| 608 |
+
max_adaptive_steps=2,
|
| 609 |
+
max_seq_len=64,
|
| 610 |
+
working_slots=8,
|
| 611 |
+
episodic_slots=16,
|
| 612 |
+
semantic_slots=32,
|
| 613 |
+
key_dim=64,
|
| 614 |
+
dropout=0.1,
|
| 615 |
+
)
|
| 616 |
+
|
| 617 |
+
params = model.count_parameters()
|
| 618 |
+
print(f"\nParameters: {params['total']:,} total, {params['trainable']:,} trainable")
|
| 619 |
+
|
| 620 |
+
# Forward pass
|
| 621 |
+
x = torch.randint(0, 128, (2, 16))
|
| 622 |
+
result = model(x, return_stats=True)
|
| 623 |
+
logits = result['logits']
|
| 624 |
+
print(f"Input shape: {x.shape}")
|
| 625 |
+
print(f"Output logits shape: {logits.shape}")
|
| 626 |
+
print(f"Stats keys: {len(result.get('stats', {}))}")
|
| 627 |
+
|
| 628 |
+
# Backward pass
|
| 629 |
+
loss = logits.sum()
|
| 630 |
+
loss.backward()
|
| 631 |
+
print("Backward pass OK")
|
| 632 |
+
|
| 633 |
+
# Generate test
|
| 634 |
+
gen = model.generate(x[:, :4], max_new_tokens=8, temperature=0.8, top_k=10)
|
| 635 |
+
print(f"Generated shape: {gen.shape}")
|
| 636 |
+
|
| 637 |
+
# Complexity analysis
|
| 638 |
+
analysis = model.get_complexity_analysis()
|
| 639 |
+
for k, v in analysis.items():
|
| 640 |
+
print(f" {k}: {v}")
|
| 641 |
+
|
| 642 |
+
print("\nβ All self-tests passed!")
|
| 643 |
+
|
| 644 |
+
# Test factory functions
|
| 645 |
+
small = create_cognet_1b_small(vocab_size=128, max_seq_len=64)
|
| 646 |
+
print(f"\nSmall model params: {small.count_parameters()['total']:,}")
|