Initial commit: GKERN glyph kernel

This commit is contained in:
gyt
2026-07-09 13:28:07 -04:00
commit 0807c58eae
43 changed files with 6006 additions and 0 deletions
+47
View File
@@ -0,0 +1,47 @@
#!/usr/bin/env python3
"""COMPARISON REPORT"""
print("""
\033[1;36m============================================================\033[0m
\033[1;36m GLYPHOS vs TRANSFORMER - COMPARATIVE ANALYSIS \033[0m
\033[1;36m============================================================\033[0m
\033[1;33m1. WHAT EACH BENCHMARK MEASURES\033[0m
------------------------------------------------------------
\033[1mTRANSFORMER:\033[0m
✓ Full vocabulary embedding (10,000+ classes)
✓ Multi-head attention O(N²) for ALL token pairs
✓ Softmax normalization (exponential operations)
✓ Residual connections + Layer Normalization
✓ Language model output head
\033[1mGLYPHOS:\033[0m
✓ Sparse graph with 4 edges per node
✓ Simple weighted averaging of neighbors
✓ Binary similarity check (fixed threshold)
✓ Sigmoid activation (cheap approximation)
✓ No vocabulary, no language modeling
\033[91mKEY POINT: These solve fundamentally DIFFERENT problems!\033[0m
\033[1;33m2. OPERATIONAL COST COMPARISON\033[0m
------------------------------------------------------------
| Component | Transformer | GlyphOS |
|--------------------|------------------|------------------|
| Attention Ops | \033[91m~500M/token\033[0m | \033[92m~16K/node\033[0m |
| Memory Pattern | \033[91mRandom/Cache miss\033[0m|\033[92m Sequential/Clean\033[0m|
| Scaling Behavior | \033[91mO(N²)\033[0m | \033[92mO(edges) ≈ O(N)\033[0m |
| Training Required | \033[91mYes (weeks)\033[0m | \033[92mNo (static)\033[0m |
| Capability | \033[93mText generation\033[0m | \033[93mGraph relaxation\033[0m |
\033[1;33m3. APPLES-TO-APPLES COMPARISON NEEDS\033[0m
------------------------------------------------------------
□ Same task (e.g., text completion)
✓ Same sequence length
✓ Same hardware
□ Same evaluation metric (perplexity, BLEU, etc.)
□ Same parameter budget
\033[91mWithout these, performance claims are misleading.\033[0m
\033[36mRun actual benchmarks to see real timings.\033[0m
""")
+63
View File
@@ -0,0 +1,63 @@
#!/usr/bin/env python3
"""GLYPHOS SUBSTRATE BENCHMARK"""
try:
import numpy as np
except ImportError:
print("\033[91m[ERROR] numpy not installed. Run: pip install numpy\033[0m")
exit(1)
import time
def resonance(similarity):
return 1.0 / (1.0 + np.exp(-1.0 * (similarity - 4.0)))
class SubstrateGraph:
def __init__(self, node_count=4096, edges_per_node=4):
np.random.seed(42) # Reproducible
self.nodes = np.random.rand(node_count).astype(np.float32)
self.edges = [(i, (i * 7 + e * 13 + 3) % node_count)
for i in range(node_count) for e in range(edges_per_node)]
def converge(self, max_epochs=100, threshold=0.001):
for epoch in range(max_epochs):
new_nodes = self.nodes.copy()
max_delta = 0.0
for i in range(len(self.nodes)):
neighbors = [self.nodes[j] for _, j in self.edges if _ == i]
if neighbors:
sims = np.where(np.abs(self.nodes[i] - neighbors) < 0.5, 5.0, 0.0)
weights = np.array([resonance(s) for s in sims])
w_sum = np.sum(weights)
new_nodes[i] = np.sum(np.array(neighbors) * weights) / w_sum if w_sum > 0 else self.nodes[i]
delta = abs(new_nodes[i] - self.nodes[i])
max_delta = max(max_delta, delta)
self.nodes = new_nodes
if max_delta < threshold:
return epoch + 1, max_delta
return max_epochs, max_delta
def benchmark():
print("\033[35mGlyphOS Substrate Benchmark\033[0m")
# TTC test
print("\033[33mRunning convergence test (4096 nodes)...\033[0m")
start = time.perf_counter()
epochs, delta = SubstrateGraph(4096, 4).converge(100)
ttc = (time.perf_counter() - start) * 1000
print(f" Converged in {epochs} epochs, delta={delta:.4f}")
# NEPS test
print("\033[33mRunning throughput test...\033[0m")
start = time.perf_counter()
for _ in range(20):
SubstrateGraph(4096, 4).converge(5)
elapsed = time.perf_counter() - start
neps = (4096 * 20) / elapsed
print(f"\n\033[1m=== GLYPHOS BASELINE RESULTS ===\033[0m")
print(f"TTC (4096 nodes): {ttc:.2f} ms in {epochs} epochs")
print(f"NEPS: {neps:,.0f} node-epochs/sec")
print(f"\033[93mNote: Measures constraint graph relaxation, not AI inference\033[0m")
if __name__ == '__main__':
benchmark()
+70
View File
@@ -0,0 +1,70 @@
#!/usr/bin/env python3
"""REAL TRANSFORMER INFERENCE BENCHMARK"""
try:
import torch
import torch.nn as nn
except ImportError:
print("\033[91m[ERROR] torch not installed. Run: pip install torch\033[0m")
exit(1)
import time
import numpy as np
class SmallTransformer(nn.Module):
def __init__(self, d_model=256, n_heads=4, n_layers=2, max_seq=1024):
super().__init__()
self.d_model = d_model
self.embedding = nn.Embedding(10000, d_model)
self.pos_embed = nn.Parameter(torch.randn(1, max_seq, d_model) * 0.02)
attn = nn.MultiheadAttention(d_model, n_heads, dropout=0, batch_first=True)
self.attention = nn.ModuleList([attn] * n_layers)
self.ffn = nn.Sequential(
nn.Linear(d_model, d_model * 4),
nn.ReLU(),
nn.Linear(d_model * 4, d_model)
)
self.norm = nn.LayerNorm(d_model)
self.lm_head = nn.Linear(d_model, 10000)
def forward(self, x):
seq_len = x.size(1)
h = self.embedding(x) + self.pos_embed[:, :seq_len, :]
mask = torch.triu(torch.ones(seq_len, seq_len), diagonal=1).bool().to(x.device)
for attn_layer in self.attention:
attn, _ = attn_layer(h, h, h, attn_mask=mask)
h = h + attn
h = h + self.ffn(h)
h = self.norm(h)
return self.lm_head(h)
def benchmark():
device = 'cuda' if torch.cuda.is_available() else 'cpu'
print(f"\033[36mDevice:\033[0m {device}")
if device == 'cuda':
print(f"\033[36mGPU:\033[0m {torch.cuda.get_device_name(0)}")
config = {'d_model': 256, 'n_heads': 4, 'n_layers': 2, 'max_seq': 1024}
model = SmallTransformer(**config).to(device).eval()
input_ids = torch.randint(0, 10000, (1, 256), device=device)
times = []
print("\033[33mRunning 5 inference cycles...\033[0m")
for i in range(5):
if device == 'cuda': torch.cuda.synchronize()
start = time.perf_counter()
with torch.inference_mode():
_ = model(input_ids)
if device == 'cuda': torch.cuda.synchronize()
times.append((time.perf_counter() - start) * 1000)
print(f" Cycle {i+1}: {times[-1]:.2f} ms")
print(f"\n\033[1m=== TRANSFORMER BASELINE RESULTS ===\033[0m")
print(f"TTFT (256 tokens): {np.mean(times):.2f} ± {np.std(times):.2f} ms")
print(f"Parameters: {sum(p.numel() for p in model.parameters()):,}")
print(f"Model: {config['n_layers']}-layer, {config['d_model']}d")
if __name__ == '__main__':
benchmark()