File size: 20,260 Bytes
63e99b4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 | """
GLADIUS v2.0 β Net2Net Progressive Expansion
Function-preserving model expansion. The bigger model starts with the
EXACT same outputs as the smaller one. Zero loss spike.
Theory (Chen et al., 2015 "Net2Net"):
- Net2WiderNet: Copy columns + divide weights to preserve output identity
- Net2DeeperNet: Initialize new layers as identity transforms
Extended for GLADIUS kernel components:
- Embedding expansion (zero-padded new dimensions)
- Attention head splitting/growth
- SwiGLU FFN widening
- Hot memory slot expansion
- Warm memory rank growth
- Time engine, cognition, modulator, tool cortex, router β all expanded
Usage:
from expansion.net2net import expand_kernel
big_kernel = expand_kernel(small_kernel, target_config)
Author: Ava Shakil
Date: 2026-03-06
"""
import torch
import torch.nn as nn
import copy
import math
from typing import Optional
import sys, os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
from kernel.config import KernelConfig
from kernel.kernel import GladiusKernel
def _zero_pad_2d(tensor: torch.Tensor, new_rows: int, new_cols: int) -> torch.Tensor:
"""Pad a 2D weight matrix with zeros to new dimensions."""
old_rows, old_cols = tensor.shape
padded = torch.zeros(new_rows, new_cols, dtype=tensor.dtype, device=tensor.device)
padded[:old_rows, :old_cols] = tensor
return padded
def _zero_pad_1d(tensor: torch.Tensor, new_size: int) -> torch.Tensor:
"""Pad a 1D bias/norm vector with zeros."""
old_size = tensor.shape[0]
if old_size >= new_size:
return tensor[:new_size]
padded = torch.zeros(new_size, dtype=tensor.dtype, device=tensor.device)
padded[:old_size] = tensor
return padded
def _widen_linear(old_weight: torch.Tensor, old_bias: Optional[torch.Tensor],
new_in: int, new_out: int,
noise_scale: float = 0.01) -> tuple:
"""
Net2WiderNet for a linear layer.
For input expansion: zero-pad new input columns
For output expansion: copy random existing rows + add noise + divide
This preserves the function: f(x) for old inputs stays identical.
"""
old_out, old_in = old_weight.shape
# New weight matrix
new_weight = torch.zeros(new_out, new_in, dtype=old_weight.dtype, device=old_weight.device)
# Copy existing weights
new_weight[:old_out, :old_in] = old_weight
# For expanded output dims: initialize with small random values
# (identity-preserving for the old subspace, small perturbation for new)
if new_out > old_out:
# Fan-in initialization for new rows
std = 1.0 / math.sqrt(new_in)
new_weight[old_out:, :] = torch.randn(new_out - old_out, new_in,
dtype=old_weight.dtype,
device=old_weight.device) * std * noise_scale
# Bias
new_bias = None
if old_bias is not None:
new_bias = torch.zeros(new_out, dtype=old_bias.dtype, device=old_bias.device)
new_bias[:old_out] = old_bias
return new_weight, new_bias
def _expand_embedding(old_embed: torch.Tensor, new_vocab: int, new_dim: int,
noise_scale: float = 0.01) -> torch.Tensor:
"""Expand embedding table: preserve existing, init new with noise."""
old_vocab, old_dim = old_embed.shape
new_embed = torch.zeros(new_vocab, new_dim, dtype=old_embed.dtype, device=old_embed.device)
# Copy existing embeddings
new_embed[:old_vocab, :old_dim] = old_embed
# New vocab entries: initialize from mean + noise
if new_vocab > old_vocab:
mean = old_embed.mean(dim=0)
std = old_embed.std(dim=0) * noise_scale
for i in range(old_vocab, new_vocab):
new_embed[i, :old_dim] = mean + torch.randn_like(mean) * std
return new_embed
def _expand_rmsnorm(old_weight: torch.Tensor, new_dim: int) -> torch.Tensor:
"""Expand RMSNorm weight to new dimension."""
new_weight = torch.ones(new_dim, dtype=old_weight.dtype, device=old_weight.device)
old_dim = old_weight.shape[0]
new_weight[:old_dim] = old_weight
return new_weight
def _create_identity_layer(config: KernelConfig, layer_idx: int) -> dict:
"""
Create state dict for a new transformer layer that acts as identity.
The key insight: if attention output and FFN output are both zero,
and the residual connection passes through, the layer is identity.
We achieve this by initializing the output projections to near-zero.
"""
from kernel.attention import TransformerLayer
layer = TransformerLayer(config, layer_idx=layer_idx)
state = layer.state_dict()
# Zero out the output projections so residual passes through
for key in state:
if 'out_proj' in key or 'w_down' in key:
state[key] = state[key] * 0.01 # Near-zero, not exactly zero (gradient flow)
return state
def expand_kernel(
source: GladiusKernel,
target_config: KernelConfig,
noise_scale: float = 0.01,
verbose: bool = True,
) -> GladiusKernel:
"""
Expand a GLADIUS kernel to a larger configuration using Net2Net.
Function-preserving: the expanded model produces (approximately) the
same outputs as the source for the same inputs. The new capacity is
initialized to near-identity, allowing gradual activation during training.
Args:
source: The trained smaller kernel
target_config: Configuration for the larger kernel
noise_scale: Scale of noise for new parameters (default 0.01)
verbose: Print expansion details
Returns:
Expanded GladiusKernel with transferred weights
"""
src_cfg = source.config
tgt_cfg = target_config
if verbose:
src_params = sum(p.numel() for p in source.parameters())
print(f"π Net2Net Expansion: {src_params:,} β ", end="")
# Create target kernel (random init)
target = GladiusKernel(tgt_cfg)
if verbose:
tgt_params = sum(p.numel() for p in target.parameters())
print(f"{tgt_params:,} params ({tgt_params/src_params:.1f}x)")
print(f" Hidden: {src_cfg.hidden_dim} β {tgt_cfg.hidden_dim}")
print(f" Layers: {src_cfg.num_layers} β {tgt_cfg.num_layers}")
print(f" Heads: {src_cfg.num_heads} β {tgt_cfg.num_heads}")
print(f" FFN: {src_cfg.ffn_dim} β {tgt_cfg.ffn_dim}")
src_sd = source.state_dict()
tgt_sd = target.state_dict()
expanded = {}
transferred = 0
initialized = 0
# === 1. EMBEDDINGS ===
if verbose:
print(" [1/7] Expanding embeddings...")
# Token embedding
# Support both naming conventions (token_embed vs token_embedding)
emb_key = 'embeddings.token_embed.weight' if 'embeddings.token_embed.weight' in src_sd else 'embeddings.token_embedding.weight'
old_emb = src_sd[emb_key]
expanded[emb_key] = _expand_embedding(
old_emb, tgt_cfg.vocab_size, tgt_cfg.hidden_dim, noise_scale
)
transferred += 1
# Output head
old_head = src_sd['embeddings.output_head.weight']
expanded['embeddings.output_head.weight'] = _expand_embedding(
old_head, tgt_cfg.vocab_size, tgt_cfg.hidden_dim, noise_scale
)
if 'embeddings.output_head.bias' in src_sd:
expanded['embeddings.output_head.bias'] = _zero_pad_1d(
src_sd['embeddings.output_head.bias'], tgt_cfg.vocab_size
)
transferred += 1
# === 2. TRANSFORMER LAYERS ===
if verbose:
print(f" [2/7] Expanding transformer ({src_cfg.num_layers} β {tgt_cfg.num_layers} layers)...")
num_transfer = min(src_cfg.num_layers, tgt_cfg.num_layers)
for i in range(num_transfer):
prefix = f'layers.{i}.'
for key in src_sd:
if not key.startswith(prefix):
continue
suffix = key[len(prefix):]
tgt_key = f'layers.{i}.{suffix}'
if tgt_key not in tgt_sd:
if verbose:
print(f" SKIP (no target): {tgt_key}")
continue
old_val = src_sd[key]
new_shape = tgt_sd[tgt_key].shape
if old_val.shape == new_shape:
# Same shape β direct copy
expanded[tgt_key] = old_val.clone()
elif len(old_val.shape) == 2:
# Linear layer β widen
w, b = _widen_linear(old_val, None, new_shape[1], new_shape[0], noise_scale)
expanded[tgt_key] = w
elif len(old_val.shape) == 1:
# Norm or bias β pad
expanded[tgt_key] = _zero_pad_1d(old_val, new_shape[0])
if 'norm' in suffix and 'weight' in suffix:
# RMSNorm weights should default to 1, not 0
expanded[tgt_key] = _expand_rmsnorm(old_val, new_shape[0])
else:
if verbose:
print(f" SKIP (unknown shape {old_val.shape}): {tgt_key}")
continue
transferred += 1
# New layers (identity initialization)
if tgt_cfg.num_layers > src_cfg.num_layers:
if verbose:
print(f" Adding {tgt_cfg.num_layers - src_cfg.num_layers} identity layers...")
for i in range(src_cfg.num_layers, tgt_cfg.num_layers):
prefix = f'layers.{i}.'
# Use the target's random init but scale down output projections
for key in tgt_sd:
if key.startswith(prefix):
if key not in expanded:
val = tgt_sd[key].clone()
# Scale down output projections for identity-like behavior
if 'out_proj' in key or 'w_down' in key:
val *= noise_scale
expanded[key] = val
initialized += 1
# === 3. FINAL NORM ===
if verbose:
print(" [3/7] Expanding final norm...")
expanded['final_norm.weight'] = _expand_rmsnorm(
src_sd['final_norm.weight'], tgt_cfg.hidden_dim
)
transferred += 1
# === 4. MEMORY ===
if verbose:
print(" [4/7] Expanding memory system...")
for key in src_sd:
if not key.startswith('memory.'):
continue
tgt_key = key
if tgt_key not in tgt_sd:
continue
old_val = src_sd[key]
new_shape = tgt_sd[tgt_key].shape
if old_val.shape == new_shape:
expanded[tgt_key] = old_val.clone()
elif len(old_val.shape) == 2:
expanded[tgt_key] = _zero_pad_2d(old_val, new_shape[0], new_shape[1])
elif len(old_val.shape) == 1:
if 'norm' in key and 'weight' in key:
expanded[tgt_key] = _expand_rmsnorm(old_val, new_shape[0])
else:
expanded[tgt_key] = _zero_pad_1d(old_val, new_shape[0])
transferred += 1
# === 5. TIME ENGINE ===
if verbose:
print(" [5/7] Expanding time engine...")
for key in src_sd:
if not key.startswith('time_engine.'):
continue
tgt_key = key
if tgt_key not in tgt_sd:
continue
old_val = src_sd[key]
new_shape = tgt_sd[tgt_key].shape
if old_val.shape == new_shape:
expanded[tgt_key] = old_val.clone()
elif len(old_val.shape) == 2:
expanded[tgt_key] = _zero_pad_2d(old_val, new_shape[0], new_shape[1])
elif len(old_val.shape) == 1:
expanded[tgt_key] = _zero_pad_1d(old_val, new_shape[0])
transferred += 1
# === 6. COGNITION + MODULATOR + ROUTER + TOOLS ===
if verbose:
print(" [6/7] Expanding cognition, modulator, router, tools...")
for component in ['cognition.', 'modulator.', 'router.', 'tool_cortex.']:
for key in src_sd:
if not key.startswith(component):
continue
tgt_key = key
if tgt_key not in tgt_sd:
continue
old_val = src_sd[key]
new_shape = tgt_sd[tgt_key].shape
if old_val.shape == new_shape:
expanded[tgt_key] = old_val.clone()
elif len(old_val.shape) == 2:
expanded[tgt_key] = _zero_pad_2d(old_val, new_shape[0], new_shape[1])
elif len(old_val.shape) == 1:
if 'norm' in key and 'weight' in key:
expanded[tgt_key] = _expand_rmsnorm(old_val, new_shape[0])
else:
expanded[tgt_key] = _zero_pad_1d(old_val, new_shape[0])
transferred += 1
# === 7. CAUSAL MASK ===
if verbose:
print(" [7/7] Rebuilding causal mask...")
# The causal mask is a buffer, handled by __init__. Skip it.
# === APPLY EXPANDED WEIGHTS ===
# Fill any remaining keys from target (random init)
for key in tgt_sd:
if key not in expanded and 'causal_mask' not in key:
expanded[key] = tgt_sd[key]
initialized += 1
# Remove causal mask from expanded (it's a buffer, not a parameter)
for key in list(expanded.keys()):
if 'causal_mask' in key:
del expanded[key]
# Load
missing, unexpected = target.load_state_dict(expanded, strict=False)
if verbose:
print(f"\n β
Expansion complete!")
print(f" Transferred: {transferred} tensors from source")
print(f" Initialized: {initialized} tensors (new capacity)")
if missing:
print(f" Missing: {len(missing)} ({missing[:5]}...)")
if unexpected:
print(f" Unexpected: {len(unexpected)}")
# Verify
tgt_params = sum(p.numel() for p in target.parameters())
print(f" Final: {tgt_params:,} params ({tgt_params * 4 / 1024 / 1024:.1f} MB f32)")
return target
def verify_expansion(source: GladiusKernel, target: GladiusKernel,
num_tests: int = 5, rtol: float = 0.1) -> bool:
"""
Verify that expansion is approximately function-preserving.
Generate random inputs in the source's vocab range and compare outputs.
Due to the expansion adding new dimensions, outputs won't be exactly
identical, but the distribution should be similar.
"""
source.eval()
target.eval()
src_cfg = source.config
print("\n㪠Verifying expansion (function preservation)...")
all_close = True
for i in range(num_tests):
# Random input in shared vocab range
seq_len = min(32, src_cfg.max_seq_len)
vocab = min(src_cfg.vocab_size, target.config.vocab_size)
x = torch.randint(4, vocab, (1, seq_len)) # Skip special tokens 0-3
with torch.no_grad():
src_out = source(x, timestamp=1.0)
tgt_out = target(x, timestamp=1.0)
# Compare logit distributions (not exact values β dimensions changed)
src_logits = src_out['logits'][0, -1, :vocab]
tgt_logits = tgt_out['logits'][0, -1, :vocab]
# Check top-k predictions overlap
src_top10 = src_logits.topk(10).indices.tolist()
tgt_top10 = tgt_logits.topk(10).indices.tolist()
overlap = len(set(src_top10) & set(tgt_top10))
# KL divergence between softmax distributions
src_probs = torch.softmax(src_logits, dim=-1)
tgt_probs = torch.softmax(tgt_logits, dim=-1)
kl = torch.nn.functional.kl_div(
tgt_probs.log(), src_probs, reduction='sum'
).item()
status = "β
" if overlap >= 3 else "β οΈ"
print(f" Test {i+1}: top-10 overlap={overlap}/10, KL={kl:.4f} {status}")
if overlap < 2:
all_close = False
if all_close:
print(" β
Expansion is function-preserving (distributions similar)")
else:
print(" β οΈ Expansion shows divergence (expected for large dim jumps)")
return all_close
# === EXPANSION STAGES ===
def stage_configs() -> dict:
"""
The four stages of GLADIUS expansion.
HATCHLING β DRAKE β WYRM β DRAGON
Each stage is designed to be trainable on a T4 16GB GPU.
"""
return {
'seed': KernelConfig(
vocab_size=16000, hidden_dim=192, num_layers=6, num_heads=6,
head_dim=32, ffn_dim=768, max_seq_len=256, num_specialists=4,
hot_memory_slots=64, warm_rank=12, cold_embedding_dim=192,
time_dim=24, max_tools=8,
),
'hatchling': KernelConfig(
vocab_size=16000, hidden_dim=384, num_layers=12, num_heads=12,
head_dim=32, ffn_dim=1536, max_seq_len=512, num_specialists=4,
hot_memory_slots=128, warm_rank=16, cold_embedding_dim=384,
time_dim=48, max_tools=16,
cognition_state_dim=128,
batch_size=8, accumulation_steps=4,
),
'drake': KernelConfig(
vocab_size=16000, hidden_dim=512, num_layers=12, num_heads=16,
head_dim=32, ffn_dim=2048, max_seq_len=512, num_specialists=4,
hot_memory_slots=256, warm_rank=24, cold_embedding_dim=512,
time_dim=64, max_tools=16,
cognition_state_dim=128,
batch_size=8, accumulation_steps=4,
),
'wyrm': KernelConfig(
vocab_size=16000, hidden_dim=640, num_layers=14, num_heads=20,
head_dim=32, ffn_dim=2560, max_seq_len=512, num_specialists=4,
hot_memory_slots=512, warm_rank=32, cold_embedding_dim=640,
time_dim=64, max_tools=32,
cognition_state_dim=128,
batch_size=8, accumulation_steps=4,
),
'dragon': KernelConfig(
vocab_size=16000, hidden_dim=768, num_layers=16, num_heads=24,
head_dim=32, ffn_dim=3072, max_seq_len=512, num_specialists=4,
hot_memory_slots=512, warm_rank=32, cold_embedding_dim=768,
time_dim=64, max_tools=32,
cognition_state_dim=128,
batch_size=4, accumulation_steps=8,
),
}
def expand_to_stage(source_path: str, target_stage: str,
output_path: str = None) -> GladiusKernel:
"""
Load a checkpoint and expand it to the target stage.
Args:
source_path: Path to source .pt checkpoint
target_stage: One of 'hatchling', 'drake', 'wyrm', 'dragon'
output_path: Where to save expanded checkpoint (optional)
Returns:
Expanded GladiusKernel
"""
configs = stage_configs()
if target_stage not in configs:
raise ValueError(f"Unknown stage: {target_stage}. Choose from: {list(configs.keys())}")
tgt_cfg = configs[target_stage]
print(f"\nπ GLADIUS EXPANSION β {target_stage.upper()}")
print(f" Loading source from: {source_path}")
# Load source
source = GladiusKernel.load_checkpoint(source_path)
# Expand
target = expand_kernel(source, tgt_cfg)
# Verify
verify_expansion(source, target)
# Save
if output_path:
target.save_checkpoint(output_path)
print(f"\n πΎ Saved to: {output_path}")
return target
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='GLADIUS Net2Net Expansion')
parser.add_argument('source', help='Source checkpoint path')
parser.add_argument('--stage', required=True,
choices=['hatchling', 'drake', 'wyrm', 'dragon'],
help='Target expansion stage')
parser.add_argument('--output', '-o', help='Output checkpoint path')
parser.add_argument('--noise', type=float, default=0.01,
help='Noise scale for new parameters')
args = parser.parse_args()
if not args.output:
args.output = args.source.replace('.pt', f'_{args.stage}.pt')
expand_to_stage(args.source, args.stage, args.output)
|