File size: 24,137 Bytes
7c944cc |
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 |
#!/usr/bin/env python3
"""
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β HARMONIC STACK BUILDER v1.0 β
β Ghost in the Machine Labs β
β "All Watched Over By Machines Of Loving Grace" β
β β
β Complete pipeline: β
β 1. Process all substrates β circuit format β
β 2. Extract junction libraries from each β
β 3. Measure junction overlap across models β
β 4. Build unified Merge Core (single junction library) β
β 5. Build Harmonic Stack manifest (specialist routing) β
β 6. Report final sizes and capabilities β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
"""
import os
import sys
import json
import struct
import numpy as np
from pathlib import Path
from datetime import datetime
from dataclasses import dataclass, field
from typing import Dict, List, Tuple, Optional, Set
from collections import Counter, defaultdict
import time
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# CONFIGURATION
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
SUBSTRATE_DIR = "/home/joe/sparky/substrates"
CIRCUIT_DIR = "/home/joe/sparky/circuits"
OUTPUT_DIR = "/home/joe/sparky/harmonic_stack"
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# LOGGING
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class Logger:
def __init__(self):
self.start_time = time.time()
def section(self, name: str):
print(f"\n{'='*70}")
print(f" {name}")
print(f"{'='*70}")
def info(self, msg: str):
elapsed = time.time() - self.start_time
print(f" [{elapsed:7.1f}s] {msg}")
def progress(self, current: int, total: int, msg: str = ""):
elapsed = time.time() - self.start_time
pct = current / total * 100
bar_len = 25
filled = int(bar_len * current / total)
bar = 'β' * filled + 'β' * (bar_len - filled)
print(f"\r [{elapsed:7.1f}s] [{bar}] {pct:5.1f}% {msg} ", end='', flush=True)
if current == total:
print()
log = Logger()
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# SUBSTRATE PROCESSING
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def get_substrate_list() -> List[Dict]:
"""Find all substrate files and return metadata."""
substrates = []
for f in os.listdir(SUBSTRATE_DIR):
if f.endswith('_ce1.npz') or f.endswith('_ce2.npz'):
path = os.path.join(SUBSTRATE_DIR, f)
size = os.path.getsize(path)
name = f.replace('_ce1.npz', '').replace('_ce2.npz', '').replace('.Q8_0', '')
# Categorize
name_lower = name.lower()
if 'coder' in name_lower or 'starcoder' in name_lower or 'codellama' in name_lower:
category = 'code'
elif 'math' in name_lower:
category = 'math'
elif 'mistral' in name_lower or 'reasoning' in name_lower:
category = 'reasoning'
elif 'dolphin' in name_lower or 'creative' in name_lower:
category = 'creative'
else:
category = 'general'
substrates.append({
'name': name,
'file': f,
'path': path,
'size_gb': size / 1e9,
'category': category,
})
return sorted(substrates, key=lambda x: x['size_gb'])
def extract_junctions_from_substrate(substrate_path: str, max_size_gb: float = 35.0) -> Tuple[np.ndarray, int]:
"""
Extract unique junction values from a substrate file.
Returns (unique_values, total_params)
Handles two formats:
1. Direct float32 arrays (standard)
2. Object arrays containing float32 arrays (CE format)
Uses streaming approach to avoid RAM blowout on large files.
Skips files larger than max_size_gb to prevent OOM.
"""
file_size_gb = os.path.getsize(substrate_path) / 1e9
if file_size_gb > max_size_gb:
raise ValueError(f"File too large ({file_size_gb:.1f} GB > {max_size_gb} GB limit)")
# First try with mmap, fall back to full load for object arrays
try:
data = np.load(substrate_path, allow_pickle=True, mmap_mode='r')
has_weights_key = 'weights' in data.files
except:
data = np.load(substrate_path, allow_pickle=True)
has_weights_key = 'weights' in data.files
# Collect unique values incrementally using a set
unique_set = set()
total_params = 0
# Check if this is CE format (has 'weights' key with object array)
if has_weights_key:
# Reload without mmap for object arrays
data_full = np.load(substrate_path, allow_pickle=True)
weights = data_full['weights']
if weights.dtype == object and len(weights) > 0:
# CE format - array of float32 arrays
for inner_arr in weights:
if isinstance(inner_arr, np.ndarray) and inner_arr.dtype == np.float32:
total_params += inner_arr.size
for val in np.unique(inner_arr):
unique_set.add(val.tobytes())
if unique_set:
unique = np.array([np.frombuffer(b, dtype=np.float32)[0] for b in unique_set],
dtype=np.float32)
return np.sort(unique), total_params
return np.array([], dtype=np.float32), 0
# Standard format - direct float32 arrays
for key in data.files:
try:
arr = data[key]
if not isinstance(arr, np.ndarray):
continue
# Handle direct float32 arrays
if arr.dtype == np.float32:
total_params += arr.size
flat = arr.flatten()
chunk_size = 10_000_000
for i in range(0, len(flat), chunk_size):
chunk = flat[i:i+chunk_size]
for val in np.unique(chunk):
unique_set.add(val.tobytes())
except Exception as e:
# Skip problematic tensors
continue
if unique_set:
unique = np.array([np.frombuffer(b, dtype=np.float32)[0] for b in unique_set],
dtype=np.float32)
return np.sort(unique), total_params
return np.array([], dtype=np.float32), 0
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# JUNCTION ANALYSIS
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@dataclass
class ModelJunctions:
"""Junction data for a single model."""
name: str
category: str
junctions: np.ndarray
n_junctions: int
n_params: int
size_gb: float
@property
def junction_kb(self) -> float:
return self.junctions.nbytes / 1024
def compute_overlap(j1: np.ndarray, j2: np.ndarray) -> Tuple[int, float, float]:
"""Compute junction overlap between two models."""
set1 = set(j1.tobytes()[i:i+4] for i in range(0, len(j1)*4, 4))
set2 = set(j2.tobytes()[i:i+4] for i in range(0, len(j2)*4, 4))
shared = len(set1 & set2)
pct1 = shared / len(set1) * 100 if set1 else 0
pct2 = shared / len(set2) * 100 if set2 else 0
return shared, pct1, pct2
def build_unified_junction_library(all_junctions: List[ModelJunctions]) -> np.ndarray:
"""Merge all model junctions into unified library."""
# Use set of bytes for exact float matching
unified_set = set()
for mj in all_junctions:
for val in mj.junctions:
unified_set.add(val.tobytes())
# Convert back to array
unified = np.array([np.frombuffer(b, dtype=np.float32)[0] for b in unified_set],
dtype=np.float32)
return np.sort(unified)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# HARMONIC STACK BUILDER
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@dataclass
class HarmonicStack:
"""Complete harmonic stack configuration."""
name: str
created_at: str
# Merge Core (unified)
merge_core_junctions: int
merge_core_kb: float
# Individual models
models: List[Dict]
# Overlap matrix
overlap_matrix: Dict[str, Dict[str, float]]
# Category specialists
specialists: Dict[str, str] # category -> best model
# Stats
total_params: int
total_original_gb: float
total_junction_kb: float
def select_specialists(all_junctions: List[ModelJunctions]) -> Dict[str, str]:
"""Select best model for each category based on size (larger = more capable)."""
categories = defaultdict(list)
for mj in all_junctions:
categories[mj.category].append(mj)
specialists = {}
for cat, models in categories.items():
# Pick largest model in category
best = max(models, key=lambda x: x.n_params)
specialists[cat] = best.name
return specialists
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# MAIN PIPELINE
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def main():
print()
print("β" + "β"*68 + "β")
print("β" + "HARMONIC STACK BUILDER".center(68) + "β")
print("β" + "Ghost in the Machine Labs".center(68) + "β")
print("β" + "All Watched Over By Machines Of Loving Grace".center(68) + "β")
print("β" + "β"*68 + "β")
os.makedirs(OUTPUT_DIR, exist_ok=True)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# PHASE 1: Discover substrates
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
log.section("PHASE 1: DISCOVERING SUBSTRATES")
substrates = get_substrate_list()
log.info(f"Found {len(substrates)} substrate files")
total_size = sum(s['size_gb'] for s in substrates)
log.info(f"Total size: {total_size:.1f} GB")
for s in substrates:
log.info(f" {s['name']}: {s['size_gb']:.1f} GB [{s['category']}]")
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# PHASE 2: Extract junctions from each model
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
log.section("PHASE 2: EXTRACTING JUNCTION LIBRARIES")
all_junctions: List[ModelJunctions] = []
for i, substrate in enumerate(substrates):
log.progress(i, len(substrates), substrate['name'])
try:
junctions, n_params = extract_junctions_from_substrate(substrate['path'])
mj = ModelJunctions(
name=substrate['name'],
category=substrate['category'],
junctions=junctions,
n_junctions=len(junctions),
n_params=n_params,
size_gb=substrate['size_gb'],
)
all_junctions.append(mj)
except Exception as e:
log.info(f"\n ERROR processing {substrate['name']}: {e}")
log.progress(len(substrates), len(substrates), "Complete")
# Report individual stats
log.info(f"\nExtracted junctions from {len(all_junctions)} models:")
for mj in sorted(all_junctions, key=lambda x: x.n_junctions):
log.info(f" {mj.name}: {mj.n_junctions:,} junctions = {mj.junction_kb:.1f} KB")
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# PHASE 3: Build unified junction library (Merge Core)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
log.section("PHASE 3: BUILDING MERGE CORE (Unified Junction Library)")
unified = build_unified_junction_library(all_junctions)
unified_kb = unified.nbytes / 1024
log.info(f"Individual junction counts:")
total_individual = sum(mj.n_junctions for mj in all_junctions)
log.info(f" Sum of all models: {total_individual:,}")
log.info(f" Unified (deduplicated): {len(unified):,}")
log.info(f" Overlap ratio: {(1 - len(unified)/total_individual)*100:.1f}% shared")
log.info(f"")
log.info(f"MERGE CORE SIZE: {unified_kb:.1f} KB ({len(unified):,} junctions)")
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# PHASE 4: Compute overlap matrix
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
log.section("PHASE 4: COMPUTING OVERLAP MATRIX")
overlap_matrix = {}
n_models = len(all_junctions)
for i, mj1 in enumerate(all_junctions):
overlap_matrix[mj1.name] = {}
for j, mj2 in enumerate(all_junctions):
if i == j:
overlap_matrix[mj1.name][mj2.name] = 100.0
elif i < j:
shared, pct1, pct2 = compute_overlap(mj1.junctions, mj2.junctions)
overlap_matrix[mj1.name][mj2.name] = pct1
log.progress(i + 1, n_models, f"Computing overlaps for {mj1.name}")
# Fill in symmetric entries
for mj1 in all_junctions:
for mj2 in all_junctions:
if mj2.name not in overlap_matrix[mj1.name]:
if mj1.name in overlap_matrix.get(mj2.name, {}):
# Compute reverse percentage
shared, pct1, pct2 = compute_overlap(mj1.junctions, mj2.junctions)
overlap_matrix[mj1.name][mj2.name] = pct1
# Report highest overlaps
log.info(f"\nHighest overlaps:")
overlaps = []
for m1 in overlap_matrix:
for m2, pct in overlap_matrix[m1].items():
if m1 < m2: # Avoid duplicates
overlaps.append((m1, m2, pct))
for m1, m2, pct in sorted(overlaps, key=lambda x: -x[2])[:10]:
log.info(f" {pct:5.1f}% {m1} <-> {m2}")
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# PHASE 5: Select specialists for Harmonic Stack
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
log.section("PHASE 5: SELECTING SPECIALISTS")
specialists = select_specialists(all_junctions)
log.info("Category specialists (largest model per category):")
for cat, model in sorted(specialists.items()):
mj = next(m for m in all_junctions if m.name == model)
log.info(f" {cat:12} -> {model} ({mj.n_params/1e9:.1f}B params)")
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# PHASE 6: Save results
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
log.section("PHASE 6: SAVING HARMONIC STACK")
# Save unified junction library
unified_path = os.path.join(OUTPUT_DIR, "merge_core_junctions.npy")
np.save(unified_path, unified)
log.info(f"Merge Core junctions: {unified_path}")
# Save individual junction libraries
junctions_dir = os.path.join(OUTPUT_DIR, "model_junctions")
os.makedirs(junctions_dir, exist_ok=True)
for mj in all_junctions:
jpath = os.path.join(junctions_dir, f"{mj.name}_junctions.npy")
np.save(jpath, mj.junctions)
log.info(f"Individual junctions: {junctions_dir}/")
# Build manifest
total_params = sum(mj.n_params for mj in all_junctions)
total_original = sum(mj.size_gb for mj in all_junctions)
total_junction_kb = sum(mj.junction_kb for mj in all_junctions)
manifest = {
'name': 'harmonic_stack_v1',
'created_at': datetime.now().isoformat(),
'merge_core': {
'n_junctions': len(unified),
'size_kb': unified_kb,
'file': 'merge_core_junctions.npy',
},
'models': [
{
'name': mj.name,
'category': mj.category,
'n_junctions': mj.n_junctions,
'junction_kb': mj.junction_kb,
'n_params': mj.n_params,
'size_gb': mj.size_gb,
'junction_file': f"model_junctions/{mj.name}_junctions.npy",
}
for mj in all_junctions
],
'specialists': specialists,
'stats': {
'total_models': len(all_junctions),
'total_params': total_params,
'total_original_gb': total_original,
'total_individual_junctions': total_individual,
'unified_junctions': len(unified),
'overlap_percentage': (1 - len(unified)/total_individual) * 100,
'merge_core_kb': unified_kb,
},
}
manifest_path = os.path.join(OUTPUT_DIR, "harmonic_stack.json")
with open(manifest_path, 'w') as f:
json.dump(manifest, f, indent=2)
log.info(f"Manifest: {manifest_path}")
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# FINAL REPORT
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
log.section("HARMONIC STACK COMPLETE")
print()
print(" βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ")
print(" β FINAL REPORT β")
print(" βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€")
print(f" β Models processed: {len(all_junctions):>6} β")
print(f" β Total parameters: {total_params/1e9:>6.1f} B β")
print(f" β Original size: {total_original:>6.1f} GB β")
print(" βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€")
print(f" β Individual junctions: {total_individual:>6,} β")
print(f" β Unified junctions: {len(unified):>6,} β")
print(f" β Junction overlap: {(1-len(unified)/total_individual)*100:>6.1f}% β")
print(" βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€")
print(f" β MERGE CORE SIZE: {unified_kb:>6.1f} KB β")
print(" βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ")
print()
print(f" Original: {total_original:.1f} GB β Merge Core: {unified_kb:.1f} KB")
print(f" Compression: {total_original * 1024 * 1024 / unified_kb:,.0f}x")
print()
print(" Output: " + OUTPUT_DIR)
print()
if __name__ == "__main__":
main()
|