File size: 13,093 Bytes
5d8ed85 | 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 | """
CogNet Inference Engine for Next.js API
=======================================
Loads trained CogNet model and CharTokenizer, supports:
- generate: text generation with temperature/top-k sampling
- analyze: logits analysis, entropy, top predictions
- inspect: model architecture details
- info: model info without loading weights
"""
import json
import math
import os
import sys
from typing import Any, Dict, List, Optional
import torch
import torch.nn.functional as F
# Import from same directory
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from cognet_1b import CogNet1B
# โโโ Model Config (matches training) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
MODEL_CONFIG = {
'vocab_size': 136,
'hidden_dim': 512,
'num_blocks': 6,
'num_channels': 6,
'channel_dim': 128,
'ff_dim': 1024,
'routing_iters': 1,
'max_adaptive_steps': 2,
'max_seq_len': 192,
'working_slots': 32,
'episodic_slots': 64,
'semantic_slots': 128,
'key_dim': 256,
'dropout': 0.1,
}
CKPT_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'checkpoints')
TOKENIZER_PATH = os.path.join(CKPT_DIR, 'tokenizer_v3.json')
BEST_MODEL_PATH = os.path.join(CKPT_DIR, 'cognet_best.pt')
LATEST_MODEL_PATH = os.path.join(CKPT_DIR, 'cognet_latest.pt')
# โโโ CharTokenizer (standalone, no import needed from train_pipeline) โโโโโโโ
class CharTokenizer:
"""Character-level tokenizer: printable ASCII + French accents + newline/tab."""
def __init__(self):
self.chars = sorted(set(
[chr(i) for i in range(32, 127)]
+ list('ร รขรครฉรจรชรซรฏรฎรดรนรปรผรฟรงลรฆรรรรรรรรรรรรรลธรลร')
+ list('รซรรฑยฟยซยป')
+ ['\t', '\n']
))
self.char_to_id = {c: i for i, c in enumerate(self.chars)}
self.id_to_char = {i: c for i, c in enumerate(self.chars)}
self.vocab_size = len(self.chars)
def encode(self, text: str) -> List[int]:
return [self.char_to_id.get(c, self.char_to_id.get(' ', 0)) for c in text]
def decode(self, ids: List[int]) -> str:
return ''.join(self.id_to_char.get(i, ' ') for i in ids)
def save(self, path: str):
with open(path, 'w', encoding='utf-8') as f:
json.dump({
'chars': self.chars,
'vocab_size': self.vocab_size,
}, f, ensure_ascii=False, indent=2)
@classmethod
def load(cls, path: str) -> 'CharTokenizer':
tok = cls.__new__(cls)
with open(path, 'r', encoding='utf-8') as f:
data = json.load(f)
tok.chars = data['chars']
tok.char_to_id = {c: i for i, c in enumerate(tok.chars)}
tok.id_to_char = {i: c for i, c in enumerate(tok.chars)}
tok.vocab_size = data['vocab_size']
return tok
# โโโ JSON Helpers โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def sanitize_for_json(obj: Any) -> Any:
"""Replace NaN/Inf with None for JSON serialization."""
if isinstance(obj, float):
if math.isnan(obj) or math.isinf(obj):
return None
return obj
if isinstance(obj, dict):
return {k: sanitize_for_json(v) for k, v in obj.items()}
if isinstance(obj, list):
return [sanitize_for_json(v) for v in obj]
return obj
# โโโ Model Cache โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
_model_cache: Dict[str, Any] = {
'model': None,
'tokenizer': None,
'device': None,
'loaded': False,
}
def load_model_and_tokenizer() -> tuple:
"""Load model and tokenizer with caching."""
if _model_cache['loaded']:
return _model_cache['model'], _model_cache['tokenizer'], _model_cache['device']
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Load tokenizer
if not os.path.exists(TOKENIZER_PATH):
raise FileNotFoundError(
f"Tokenizer not found at {TOKENIZER_PATH}. "
"Run train_pipeline.py first to create it."
)
tokenizer = CharTokenizer.load(TOKENIZER_PATH)
# Update vocab_size from tokenizer
config = dict(MODEL_CONFIG)
config['vocab_size'] = tokenizer.vocab_size
# Create model
model = CogNet1B(**config).to(device)
# Load weights (prefer best, then latest)
model_path = BEST_MODEL_PATH if os.path.exists(BEST_MODEL_PATH) else LATEST_MODEL_PATH
if model_path and os.path.exists(model_path):
ckpt = torch.load(model_path, map_location=device, weights_only=False)
model.load_state_dict(ckpt['model_state_dict'])
step = ckpt.get('metrics', {}).get('step', '?')
print(f"Loaded model from {model_path} (step={step})")
else:
print("WARNING: No trained weights found. Using random initialization.")
model.eval()
# Cache
_model_cache['model'] = model
_model_cache['tokenizer'] = tokenizer
_model_cache['device'] = device
_model_cache['loaded'] = True
return model, tokenizer, device
# โโโ Action Handlers โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def handle_generate(prompt: str, max_tokens: int = 100,
temperature: float = 0.8, top_k: int = 20) -> Dict:
"""Generate text from a prompt."""
model, tokenizer, device = load_model_and_tokenizer()
# Encode prompt
ids = tokenizer.encode(prompt)
if len(ids) == 0:
ids = [0]
input_ids = torch.tensor([ids], dtype=torch.long, device=device)
# Generate
with torch.no_grad():
output_ids = model.generate(
input_ids,
max_new_tokens=max_tokens,
temperature=temperature,
top_k=top_k,
)
# Decode
generated_ids = output_ids[0].tolist()
generated_text = tokenizer.decode(generated_ids)
new_text = tokenizer.decode(generated_ids[len(ids):])
# Token details
token_details = []
for i, tid in enumerate(generated_ids):
char = tokenizer.decode([tid])
token_details.append({
'id': tid,
'char': char,
'position': i,
})
return sanitize_for_json({
'action': 'generate',
'prompt': prompt,
'generated_text': generated_text,
'new_text': new_text,
'token_details': token_details,
'num_tokens': len(generated_ids),
'temperature': temperature,
'top_k': top_k,
})
def handle_analyze(prompt: str) -> Dict:
"""Analyze logits, entropy, and top predictions."""
model, tokenizer, device = load_model_and_tokenizer()
ids = tokenizer.encode(prompt)
if len(ids) == 0:
ids = [0]
input_ids = torch.tensor([ids], dtype=torch.long, device=device)
with torch.no_grad():
result = model(input_ids, return_stats=True)
logits = result['logits']
# Analyze last token's predictions
last_logits = logits[0, -1, :] # (vocab_size,)
probs = F.softmax(last_logits, dim=-1)
# Entropy
entropy = -(probs * (probs + 1e-10).log()).sum().item()
# Top 10 predictions
topk_vals, topk_ids = torch.topk(probs, min(10, probs.size(0)))
top_predictions = []
for prob, tid in zip(topk_vals.tolist(), topk_ids.tolist()):
top_predictions.append({
'token_id': tid,
'char': tokenizer.decode([tid]),
'probability': prob,
})
# Per-position entropy
all_probs = F.softmax(logits[0], dim=-1)
pos_entropy = (-(all_probs * (all_probs + 1e-10).log()).sum(dim=-1)).tolist()
# Stats
stats = result.get('stats', {})
stats_summary = {}
for k, v in stats.items():
if isinstance(v, torch.Tensor):
v = v.item()
if isinstance(v, float) and (math.isnan(v) or math.isinf(v)):
v = None
stats_summary[k] = v
return sanitize_for_json({
'action': 'analyze',
'prompt': prompt,
'prompt_length': len(ids),
'entropy': entropy,
'top_predictions': top_predictions,
'per_position_entropy': pos_entropy,
'model_stats': stats_summary,
})
def handle_inspect() -> Dict:
"""Return model architecture details."""
model, tokenizer, device = load_model_and_tokenizer()
params = model.count_parameters()
complexity = model.get_complexity_analysis()
# Layer details
layers = []
for i, block in enumerate(model.blocks):
layer_params = sum(p.numel() for p in block.parameters())
layers.append({
'block_index': i,
'parameters': layer_params,
'components': ['CognitiveRouter', 'SharedHierarchicalMemory',
'AdaptiveComputationBlock', 'CompositionalReasoner'],
})
return sanitize_for_json({
'action': 'inspect',
'architecture': 'CogNet (Non-Transformer)',
'total_parameters': params['total'],
'trainable_parameters': params['trainable'],
'config': {
'vocab_size': model.vocab_size,
'hidden_dim': model.hidden_dim,
'num_blocks': model.num_blocks,
'num_channels': model.num_channels,
'channel_dim': model.channel_dim,
'ff_dim': model.ff_dim,
'max_seq_len': model.max_seq_len,
'tokenizer_vocab_size': tokenizer.vocab_size,
},
'complexity_analysis': complexity,
'layers': layers,
'device': str(device),
})
def handle_info() -> Dict:
"""Return model info without loading weights."""
config = dict(MODEL_CONFIG)
# Check what's available
has_tokenizer = os.path.exists(TOKENIZER_PATH)
has_best = os.path.exists(BEST_MODEL_PATH)
has_latest = os.path.exists(LATEST_MODEL_PATH)
# Estimate param count without loading
model = CogNet1B(**config)
params = model.count_parameters()
# Check checkpoint info if available
checkpoint_info = {}
if has_best:
try:
ckpt = torch.load(BEST_MODEL_PATH, map_location='cpu', weights_only=False)
checkpoint_info['best'] = {
'step': ckpt.get('metrics', {}).get('step', None),
'val_loss': ckpt.get('metrics', {}).get('val_loss', None),
'val_ppl': ckpt.get('metrics', {}).get('val_ppl', None),
}
except Exception:
checkpoint_info['best'] = {'error': 'Could not read checkpoint'}
if has_latest:
try:
ckpt = torch.load(LATEST_MODEL_PATH, map_location='cpu', weights_only=False)
checkpoint_info['latest'] = {
'step': ckpt.get('metrics', {}).get('step', None),
}
except Exception:
checkpoint_info['latest'] = {'error': 'Could not read checkpoint'}
return sanitize_for_json({
'action': 'info',
'model_name': 'CogNet',
'architecture': 'Non-Transformer (Cognitive Routing)',
'estimated_parameters': params['total'],
'config': config,
'files': {
'tokenizer': has_tokenizer,
'best_checkpoint': has_best,
'latest_checkpoint': has_latest,
},
'checkpoint_info': checkpoint_info,
})
# โโโ CLI Entry Point โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def main():
import argparse
parser = argparse.ArgumentParser(description='CogNet Inference Engine')
parser.add_argument('action', choices=['generate', 'analyze', 'inspect', 'info'],
help='Action to perform')
parser.add_argument('--prompt', type=str, default='The ',
help='Prompt text (for generate/analyze)')
parser.add_argument('--max-tokens', type=int, default=100,
help='Max tokens to generate')
parser.add_argument('--temperature', type=float, default=0.8,
help='Sampling temperature')
parser.add_argument('--top-k', type=int, default=20,
help='Top-k sampling')
args = parser.parse_args()
if args.action == 'generate':
result = handle_generate(args.prompt, args.max_tokens,
args.temperature, args.top_k)
elif args.action == 'analyze':
result = handle_analyze(args.prompt)
elif args.action == 'inspect':
result = handle_inspect()
elif args.action == 'info':
result = handle_info()
print(json.dumps(result, indent=2, ensure_ascii=False))
if __name__ == '__main__':
main()
|