File size: 12,816 Bytes
efd23fa | 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 | """
Condensate Layer 2: Predictor Tests
Tests prediction accuracy on known access patterns.
The key question: can we predict what's coming before it's requested?
Run: python3 test_predictor.py
"""
import numpy as np
import time
import os
import sys
sys.path.insert(0, os.path.dirname(__file__))
from membrane import Membrane
from graph_builder import GraphBuilder
from predictor import Predictor
def generate_and_learn(name, state, access_fn, train_iters,
causal_window_ns=3_000_000):
"""Helper: run a workload, build graph, learn predictor.
Returns (predictor, graph) after training.
"""
Membrane.clear()
wrapped = Membrane.wrap(state, name)
for _ in range(train_iters):
access_fn(wrapped)
train_log = Membrane.get_log()
graph = GraphBuilder(causal_window_ns=causal_window_ns)
graph.build(train_log)
predictor = Predictor()
predictor.learn(graph)
return predictor, graph, train_log
def test_sequential_prediction():
"""Test 1: Sequential layer access β can we predict the next layer?
Pattern: layer_0 β layer_1 β layer_2 β ... β layer_7
If we see layer_3, we should predict layer_4.
"""
print("\n--- Test 1: Sequential Layer Prediction ---")
state = {f"layer_{i}": {"w": np.random.randn(64, 64).astype(np.float32)}
for i in range(8)}
def access_fn(wrapped):
for i in range(8):
layer = wrapped[f"layer_{i}"]
_ = layer["w"]
time.sleep(0.0005)
# Train on 20 passes
predictor, graph, train_log = generate_and_learn(
"seq", state, access_fn, train_iters=20
)
predictor.print_model()
# Test on 10 new passes
Membrane.clear()
wrapped = Membrane.wrap(
{k: dict(v) if isinstance(v, dict) else v for k, v in state.items()},
"seq"
)
for _ in range(10):
access_fn(wrapped)
test_log = Membrane.get_log()
result = predictor.print_score(test_log, verbose=True)
assert result["accuracy"] > 50, f"Sequential prediction should be >50%, got {result['accuracy']}%"
print(f" Accuracy: {result['accuracy']}% β sequential prediction works!")
print(" PASS")
def test_causal_chain_prediction():
"""Test 2: Known causal chains β AβBβC with consistent timing.
The predictor should learn the chain and predict B when A fires,
and C when B fires. Multi-hop: seeing A should also predict C.
"""
print("\n--- Test 2: Causal Chain Prediction ---")
state = {f"r{i}": np.random.randn(32).astype(np.float32)
for i in range(8)}
def access_fn(wrapped):
# Chain: r0 β r2 β r5 β r7 (always, ~1ms apart)
_ = wrapped["r0"]
time.sleep(0.001)
_ = wrapped["r2"]
time.sleep(0.001)
_ = wrapped["r5"]
time.sleep(0.001)
_ = wrapped["r7"]
time.sleep(0.005)
# Noise
if np.random.random() > 0.7:
_ = wrapped[f"r{np.random.choice([1, 3, 4, 6])}"]
time.sleep(0.005)
predictor, graph, train_log = generate_and_learn(
"chain", state, access_fn, train_iters=50
)
predictor.print_model()
# Test: when r0 fires, do we predict r2?
preds = predictor.predict("chain.r0")
pred_paths = [p.path for p in preds]
print(f" When r0 fires, predictions: {[p.path.split('.')[-1] for p in preds[:5]]}")
r2_predicted = "chain.r2" in pred_paths
print(f" r2 predicted after r0: {r2_predicted}")
# Test: when r2 fires, do we predict r5?
preds_r2 = predictor.predict("chain.r2")
pred_paths_r2 = [p.path for p in preds_r2]
r5_predicted = "chain.r5" in pred_paths_r2
print(f" r5 predicted after r2: {r5_predicted}")
# Score on fresh data
Membrane.clear()
wrapped = Membrane.wrap(
{k: v.copy() if hasattr(v, 'copy') else v for k, v in state.items()},
"chain"
)
for _ in range(20):
access_fn(wrapped)
result = predictor.print_score(Membrane.get_log(), verbose=True)
assert r2_predicted, "Should predict r2 after r0"
print(" PASS")
def test_cluster_prediction():
"""Test 3: Cluster co-activation β if one member fires, predict all.
When item_0 fires, we should predict item_1 and item_2 (same cluster).
"""
print("\n--- Test 3: Cluster Co-Activation Prediction ---")
state = {f"item_{i}": np.random.randn(16).astype(np.float32)
for i in range(10)}
def access_fn(wrapped):
# Cluster A: always together
_ = wrapped["item_0"]
_ = wrapped["item_1"]
_ = wrapped["item_2"]
time.sleep(0.008)
# Cluster B: always together
_ = wrapped["item_5"]
_ = wrapped["item_6"]
_ = wrapped["item_7"]
time.sleep(0.008)
# Random singletons
_ = wrapped[f"item_{np.random.choice([3, 4, 8, 9])}"]
time.sleep(0.008)
predictor, graph, train_log = generate_and_learn(
"clust", state, access_fn, train_iters=40,
causal_window_ns=3_000_000
)
predictor.print_model()
# Test: when item_0 fires, predict item_1 and item_2
preds = predictor.predict("clust.item_0")
pred_paths = {p.path for p in preds}
print(f" When item_0 fires: {[p.path.split('.')[-1] for p in preds[:5]]}")
item_1_predicted = "clust.item_1" in pred_paths
item_2_predicted = "clust.item_2" in pred_paths
print(f" item_1 predicted: {item_1_predicted}")
print(f" item_2 predicted: {item_2_predicted}")
# Score on fresh data
Membrane.clear()
wrapped = Membrane.wrap(
{k: v.copy() for k, v in state.items()}, "clust"
)
for _ in range(15):
access_fn(wrapped)
result = predictor.print_score(Membrane.get_log(), verbose=True)
assert item_1_predicted and item_2_predicted, "Should predict cluster members"
print(" PASS")
def test_inference_simulation():
"""Test 4: Realistic inference β train on requests, predict on new ones.
This is the demo workload. If prediction accuracy is high here,
Condensate has legs.
"""
print("\n--- Test 4: AI Inference Prediction (The Real Test) ---")
state = {
"config": {"temp": 0.7, "max_tok": 512},
}
for i in range(6):
state[f"layer_{i}"] = {
"q": np.random.randn(64, 64).astype(np.float32),
"k": np.random.randn(64, 64).astype(np.float32),
"v": np.random.randn(64, 64).astype(np.float32),
"ffn": np.random.randn(64, 256).astype(np.float32),
}
for i in range(6):
state[f"kv_{i}"] = {
"keys": np.zeros((128, 64), dtype=np.float32),
"vals": np.zeros((128, 64), dtype=np.float32),
}
def access_fn(wrapped):
# Config once
_ = wrapped["config"]["temp"]
# 5 tokens of autoregressive generation
for tok in range(5):
for layer_idx in range(6):
layer = wrapped[f"layer_{layer_idx}"]
_ = layer["q"]
_ = layer["k"]
_ = layer["v"]
kv = wrapped[f"kv_{layer_idx}"]
_ = kv["keys"]
_ = kv["vals"]
_ = layer["ffn"]
time.sleep(0.0001)
# TRAIN on 10 requests
print(" Training on 10 requests...")
predictor, graph, train_log = generate_and_learn(
"inf", state, access_fn, train_iters=10,
causal_window_ns=2_000_000
)
predictor.print_model()
# TEST on 5 new requests
print(" Testing on 5 new requests...")
Membrane.clear()
# Rebuild state for test
test_state = {}
test_state["config"] = {"temp": 0.7, "max_tok": 512}
for i in range(6):
test_state[f"layer_{i}"] = {
"q": np.random.randn(64, 64).astype(np.float32),
"k": np.random.randn(64, 64).astype(np.float32),
"v": np.random.randn(64, 64).astype(np.float32),
"ffn": np.random.randn(64, 256).astype(np.float32),
}
for i in range(6):
test_state[f"kv_{i}"] = {
"keys": np.zeros((128, 64), dtype=np.float32),
"vals": np.zeros((128, 64), dtype=np.float32),
}
wrapped = Membrane.wrap(test_state, "inf")
for _ in range(5):
access_fn(wrapped)
test_log = Membrane.get_log()
result = predictor.print_score(test_log, verbose=True)
# The moment of truth
accuracy = result["accuracy"]
print(f"\n *** INFERENCE PREDICTION ACCURACY: {accuracy}% ***")
if accuracy >= 80:
print(" EXCELLENT β Condensate can predict inference access patterns!")
print(" This means: pre-staging works. RAM condensation is viable.")
elif accuracy >= 60:
print(" GOOD β Significant prediction capability. Worth pursuing.")
elif accuracy >= 40:
print(" MODERATE β Some structure learned. Needs better substrate.")
else:
print(" LOW β Pattern too noisy or model too simple. Investigate.")
print(" PASS")
def test_prediction_vs_no_prediction():
"""Test 5: Quantify the value β compare predicted vs unpredicted accesses.
Simulates what would happen with and without prediction:
- Without: every cold access = full latency (cache miss)
- With: predicted accesses = pre-staged (cache hit)
Reports the theoretical speedup.
"""
print("\n--- Test 5: Prediction Value (Theoretical Speedup) ---")
state = {}
for i in range(16):
state[f"block_{i}"] = np.random.randn(128, 128).astype(np.float32)
hot_blocks = {0, 1, 2, 3} # always in RAM
cold_blocks = set(range(4, 16)) # would need paging
def access_fn(wrapped):
# Hot blocks every iteration
for i in hot_blocks:
_ = wrapped[f"block_{i}"]
time.sleep(0.001)
# Cold blocks: predictable pattern
# Phase A: blocks 4,5,6 together
_ = wrapped["block_4"]
_ = wrapped["block_5"]
_ = wrapped["block_6"]
time.sleep(0.005)
# Phase B: blocks 10,11,12 together
_ = wrapped["block_10"]
_ = wrapped["block_11"]
_ = wrapped["block_12"]
time.sleep(0.005)
# Random cold access (unpredictable)
_ = wrapped[f"block_{np.random.choice([7, 8, 9, 13, 14, 15])}"]
time.sleep(0.005)
# Train
predictor, graph, train_log = generate_and_learn(
"value", state, access_fn, train_iters=30,
causal_window_ns=3_000_000
)
# Test
Membrane.clear()
wrapped = Membrane.wrap(
{k: v.copy() for k, v in state.items()}, "value"
)
for _ in range(10):
access_fn(wrapped)
result = predictor.score(Membrane.get_log())
# Simulate latency impact
hit_rate = result["accuracy"] / 100.0
cold_access_count = result["predictions_made"]
# Latency model (simplified):
# Cache hit (predicted & pre-staged): ~100ns (RAM-HOT)
# Cache miss (unpredicted cold): ~100ΞΌs (disk page-in)
# That's a 1000x difference
hit_latency_ns = 100
miss_latency_ns = 100_000
with_prediction = (cold_access_count * hit_rate * hit_latency_ns +
cold_access_count * (1 - hit_rate) * miss_latency_ns)
without_prediction = cold_access_count * miss_latency_ns
speedup = without_prediction / with_prediction if with_prediction > 0 else 1.0
print(f"\n Cold accesses in test: {cold_access_count}")
print(f" Prediction hit rate: {result['accuracy']}%")
print(f"")
print(f" Without Condensate:")
print(f" Every cold access = {miss_latency_ns/1000:.0f}ΞΌs (page from disk)")
print(f" Total latency: {without_prediction/1e6:.1f}ms")
print(f"")
print(f" With Condensate:")
print(f" Predicted hits: {hit_latency_ns}ns (pre-staged in RAM)")
print(f" Unpredicted misses: {miss_latency_ns/1000:.0f}ΞΌs (still cold)")
print(f" Total latency: {with_prediction/1e6:.1f}ms")
print(f"")
print(f" *** THEORETICAL SPEEDUP: {speedup:.1f}x ***")
if speedup > 5:
print(f" Significant β prediction eliminates most cold-access latency")
elif speedup > 2:
print(f" Meaningful β prediction cuts cold-access latency substantially")
else:
print(f" Marginal β need better prediction or different access patterns")
print(" PASS")
if __name__ == "__main__":
print("=" * 60)
print(" CONDENSATE β Layer 2 Predictor Tests")
print("=" * 60)
test_sequential_prediction()
test_causal_chain_prediction()
test_cluster_prediction()
test_inference_simulation()
test_prediction_vs_no_prediction()
print("\n" + "=" * 60)
print(" ALL TESTS PASSED")
print("=" * 60)
|