Spaces:
Running

File size: 5,468 Bytes
a97a0bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Agentic Kernel Benchmark -- Proves the core throughput claim.

Run:  python kernel_benchmark.py
"""
import time, random, sys, io, os
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')

from kernel_interface import AgenticKernel, PrecisionKernel, ThermalKernel, NetworkKernel

LAYER_DISTS = [
    {"embedding": 0.15, "attention": 0.25, "ffn": 0.40, "layernorm": 0.002, "output": 0.198},
    {"embedding": 0.08, "attention": 0.18, "ffn": 0.60, "layernorm": 0.002, "output": 0.138},
    {"embedding": 0.12, "attention": 0.30, "ffn": 0.38, "layernorm": 0.002, "output": 0.198},
]
PARAM_SIZES = [3_800_000_000, 7_000_000_000, 13_000_000_000, 46_000_000_000, 70_000_000_000]
PRECISIONS = ["FP32", "BF16", "FP16", "FP8"]
SAFE_COMBOS = [
    {"embedding": "FP32", "attention": "BF16", "ffn": "FP8", "layernorm": "BF16", "output": "FP32"},
    {"embedding": "FP32", "attention": "BF16", "ffn": "BF16", "layernorm": "BF16", "output": "FP32"},
    {"embedding": "BF16", "attention": "BF16", "ffn": "FP8", "layernorm": "BF16", "output": "FP32"},
    {"embedding": "FP32", "attention": "FP16", "ffn": "FP8", "layernorm": "BF16", "output": "FP32"},
    {"embedding": "FP32", "attention": "BF16", "ffn": "FP8", "layernorm": "FP16", "output": "BF16"},
]

def random_action(rng):
    if rng.random() < 0.7:
        return {"precision_strategy": rng.choice(SAFE_COMBOS)}
    layers = ["embedding", "attention", "ffn", "layernorm", "output"]
    return {"precision_strategy": {l: rng.choice(PRECISIONS) for l in layers}}

def random_state(rng):
    return {
        "model": {"total_params": rng.choice(PARAM_SIZES), "layer_distribution": rng.choice(LAYER_DISTS)},
        "cluster": {"total_gpus": rng.choice([4, 8, 16, 32]), "total_memory_gb": rng.choice([320, 640, 1280, 2560]), "gpu_memory_gb": 80.0},
        "num_models": rng.choice([2, 3, 4]),
    }

def fmt(n):
    return f"{n:,.0f}"

def run_benchmark(n_evals=10_000):
    rng = random.Random(42)
    kernel = AgenticKernel()

    print("=" * 65)
    print("  AGENTIC KERNEL BENCHMARK")
    print("=" * 65)
    print(f"  Modules  : {', '.join(kernel.get_module_names())}")
    print(f"  Weights  : {kernel.get_weights()}")
    print(f"  Evals    : {fmt(n_evals)}")
    print("-" * 65)

    # Warmup (JIT, cache priming)
    for _ in range(500):
        kernel.evaluate(random_state(rng), random_action(rng))
    kernel.reset_stats()

    # Timed run
    start = time.perf_counter()
    for _ in range(n_evals):
        kernel.evaluate(random_state(rng), random_action(rng))
    wall = time.perf_counter() - start

    stats = kernel.get_throughput_stats()

    print(f"\n  RESULTS ({n_evals} evaluations)")
    print(f"  {'-' * 50}")
    print(f"  Wall-clock time     : {wall*1000:.1f} ms")
    print(f"  Throughput          : {fmt(stats['throughput_evals_per_sec'])} evals/sec")
    print(f"  Mean latency        : {stats['mean_latency_us']:.1f} us")
    print(f"  Median latency      : {stats['median_latency_us']:.1f} us")
    print(f"  P99 latency         : {stats['p99_latency_us']:.1f} us")
    print(f"  Min latency         : {stats['min_latency_us']:.1f} us")
    print(f"  Max latency         : {stats['max_latency_us']:.1f} us")

    # Per-module benchmarks
    print(f"\n  PER-MODULE BREAKDOWN")
    print(f"  {'-' * 50}")
    for ModClass in [PrecisionKernel, ThermalKernel, NetworkKernel]:
        mod = ModClass()
        mk = AgenticKernel(modules=[mod], weights={mod.name: 1.0})
        for _ in range(200):
            mk.evaluate(random_state(rng), random_action(rng))
        mk.reset_stats()
        for _ in range(n_evals):
            mk.evaluate(random_state(rng), random_action(rng))
        ms = mk.get_throughput_stats()
        print(f"  {mod.name:12s} : {fmt(ms['throughput_evals_per_sec']):>10s} evals/sec  (mean {ms['mean_latency_us']:.1f} us)")

    # Batch mode
    print(f"\n  BATCH MODE")
    print(f"  {'-' * 50}")
    batch_kernel = AgenticKernel()
    batch_kernel.reset_stats()
    batch = [{"state": random_state(rng), "action": random_action(rng)} for _ in range(n_evals)]
    t0 = time.perf_counter()
    results = batch_kernel.batch_evaluate(batch)
    batch_wall = time.perf_counter() - t0
    bs = batch_kernel.get_throughput_stats()
    print(f"  Batch throughput    : {fmt(bs['throughput_evals_per_sec'])} evals/sec")
    print(f"  Batch wall-clock    : {batch_wall*1000:.1f} ms")

    # Comparison table
    tput = stats['throughput_evals_per_sec']
    lat_us = stats['mean_latency_us']
    print(f"\n  COMPARISON vs SANDBOX ALTERNATIVES")
    print(f"  {'-' * 60}")
    print(f"  {'Method':<22s} {'Cold Start':>12s} {'Throughput':>16s} {'GPU Starvation':>16s}")
    print(f"  {'-' * 60}")
    print(f"  {'Docker sandbox':<22s} {'100-300ms':>12s} {'~10 evals/sec':>16s} {'Severe':>16s}")
    print(f"  {'MicroVM (Firecracker)':<22s} {'~125ms':>12s} {'~8 evals/sec':>16s} {'Severe':>16s}")
    print(f"  {'Agentic Kernel':<22s} {'0ms':>12s} {fmt(tput)+' evals/sec':>16s} {'None':>16s}")
    print(f"  {'-' * 60}")
    speedup = tput / 10.0
    print(f"  Speedup vs Docker   : {speedup:,.0f}x")
    print(f"  Mean eval latency   : {lat_us:.1f} us ({lat_us/1000:.4f} ms)")

    print("\n" + "=" * 65)
    print(f"  >> {fmt(tput)} evals/sec -- Sandbox Scaling Problem: SOLVED")
    print("=" * 65)

    return stats

if __name__ == "__main__":
    n = int(sys.argv[1]) if len(sys.argv) > 1 else 10_000
    run_benchmark(n)