Spaces:
Running

File size: 10,291 Bytes
a97a0bc
 
 
 
 
 
 
0a7b128
a97a0bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a7b128
 
 
a97a0bc
 
0a7b128
 
a97a0bc
 
 
 
 
0a7b128
 
 
a97a0bc
0a7b128
a97a0bc
 
 
 
0a7b128
a97a0bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a7b128
a97a0bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a7b128
 
 
 
 
 
 
 
 
a97a0bc
 
 
0a7b128
a97a0bc
 
0a7b128
a97a0bc
 
0a7b128
 
a97a0bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a7b128
a97a0bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a7b128
a97a0bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a7b128
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
"""
Agentic Kernel — Clean Interface for Physics-Based RL Evaluation.

Wraps physics grading into a composable, pluggable abstraction with
batch evaluation and built-in performance instrumentation.
"""
from __future__ import annotations
from collections import deque
import time, statistics
from typing import Any, Dict, List, Optional

from environment.physics_model import (
    BYTES_PER_PARAM, STABILITY_SCORE, THROUGHPUT_MULTIPLIER, ACCURACY_PENALTY,
    POWER_DRAW_MULTIPLIER, H100_COST_PER_HOUR_USD, GPU_TDP_WATTS,
    MEMORY_UTILIZATION_DANGER, MEMORY_UTILIZATION_WARNING, POWER_UTILIZATION_DANGER,
    compute_training_cost, compute_hardware_safety, score_precision_layer,
    compute_network_topology,
)

def _clamp(s: float) -> float:
    try: v = float(s)
    except: v = 0.01
    return max(0.01, min(0.99, v))


class KernelModule:
    """Base class for pluggable kernel evaluation modules."""
    name: str = "base"
    def evaluate(self, state: dict, action: dict) -> dict:
        raise NotImplementedError


class PrecisionKernel(KernelModule):
    """Mixed-precision evaluation using NVIDIA TE physics constants."""
    name = "precision"

    def evaluate(self, state: dict, action: dict) -> dict:
        model = state.get("model", {})
        params = model.get("total_params", 7_000_000_000)
        layer_dist = model.get("layer_distribution", {
            "embedding": 0.15, "attention": 0.25, "ffn": 0.40,
            "layernorm": 0.002, "output": 0.198,
        })
        cluster = state.get("cluster", {})
        total_mem = cluster.get("total_memory_gb", 640)
        n_models = state.get("num_models", 1)
        strategy = action.get("precision_strategy", {})
        
        # Load active hardware profile name
        hw_profile = cluster.get("hardware_profile", state.get("hardware_profile", "nvidia_h100"))

        vals = list(strategy.values())
        if vals and all(v in ("FP8", "INT8") for v in vals):
            return {"score": 0.01, "feedback": "IRD: All-FP8/INT8 crash", "details": {"ird_violation": True}}
        if vals and all(v == "FP32" for v in vals):
            return {"score": 0.01, "feedback": "IRD: All-FP32 waste", "details": {"ird_violation": True}}
        if not strategy:
            return {"score": 0.01, "feedback": "IRD: Empty strategy", "details": {"ird_violation": True}}

        metrics = compute_training_cost(total_params=params, precision_strategy=strategy, layer_distribution=layer_dist, hardware_profile=hw_profile)
        if metrics.get("unsupported_precisions"):
            return {"score": 0.01, "feedback": f"UNSUPPORTED precision on {hw_profile}", "details": {"unsupported": metrics["unsupported_precisions"], "ird_violation": False}}
        n_gpus = max(1, cluster.get("total_gpus", 8) // max(n_models, 1))
        hw = compute_hardware_safety(total_params=params, precision_strategy=strategy, layer_distribution=layer_dist, num_gpus=n_gpus, gpu_memory_gb=cluster.get("gpu_memory_gb", 80.0), hardware_profile=hw_profile)

        layer_scores = {}
        for lt in layer_dist:
            p = strategy.get(lt, "FP32")
            ls, lf = score_precision_layer(lt, p, hardware_profile=hw_profile)
            layer_scores[lt] = {"precision": p, "score": ls}

        avg = sum(v["score"] for v in layer_scores.values()) / max(len(layer_scores), 1)
        fair = total_mem / max(n_models, 1)
        mp = 0.15 if metrics["memory_gb"] > fair * 1.5 else (0.05 if metrics["memory_gb"] > fair else 0.0)
        sp = 0.0 if metrics["estimated_stable"] else 0.3
        hp = 0.0 if hw["overall_safe"] else 0.1
        score = _clamp(avg - mp - sp - hp)

        return {
            "score": score,
            "feedback": f"precision={score:.3f} mem={metrics['memory_gb']}GB speed={metrics['speedup_vs_fp32']}x",
            "details": {"layer_scores": layer_scores, "metrics": metrics, "hw_safety": hw, "ird_violation": False},
        }



class ThermalKernel(KernelModule):
    """Thermal-aware workload placement evaluation (NVIDIA H100 SXM5 specs)."""
    name = "thermal"
    AMBIENT_C = 25.0
    THERMAL_R = 0.083  # C/W
    THROTTLE_C = 83.0
    SHUTDOWN_C = 92.0
    SUSTAINED = 1.15

    def evaluate(self, state: dict, action: dict) -> dict:
        model = state.get("model", {})
        params = model.get("total_params", 7_000_000_000)
        layer_dist = model.get("layer_distribution", {"embedding": 0.15, "attention": 0.25, "ffn": 0.40, "layernorm": 0.002, "output": 0.198})
        cluster = state.get("cluster", {})
        n_models = state.get("num_models", 1)
        n_gpus = max(1, cluster.get("total_gpus", 8) // max(n_models, 1))
        strategy = action.get("precision_strategy", {})
        if not strategy:
            return {"score": 0.01, "feedback": "No strategy for thermal eval", "details": {}}

        # Load active hardware profile
        hw_profile = cluster.get("hardware_profile", state.get("hardware_profile", "nvidia_h100"))
        from environment.physics_model import HARDWARE_PROFILES
        profile = HARDWARE_PROFILES.get(hw_profile, HARDWARE_PROFILES["nvidia_h100"])
        power_draw_map = profile["power_draw_multipliers"]
        tdp_watts = profile["tdp_watts"]
        bytes_map = profile["bytes_per_param"]
        gpu_mem_limit = cluster.get("gpu_memory_gb", profile["gpu_memory_gb"])

        wp, tw = 0.0, 0.0
        for lt, frac in layer_dist.items():
            p = strategy.get(lt, "FP32")
            wp += power_draw_map.get(p, 1.0) * frac
            tw += frac
        avg_pr = wp / max(tw, 0.001)
        watts = avg_pr * tdp_watts * self.SUSTAINED
        temp = self.AMBIENT_C + watts * self.THERMAL_R

        mem_bytes = sum(params * layer_dist.get(lt, 0) * bytes_map.get(strategy.get(lt, "FP32"), 4) for lt in layer_dist)
        mem_util = (mem_bytes / 1e9) / max(n_gpus, 1) / gpu_mem_limit
        temp *= 1.0 + mem_util * 0.05

        if temp >= self.SHUTDOWN_C:
            risk, score = "SHUTDOWN", 0.01
        elif temp >= self.THROTTLE_C:
            risk = "THROTTLING"
            score = max(0.10, 0.50 - (temp - self.THROTTLE_C) / (self.SHUTDOWN_C - self.THROTTLE_C) * 0.40)
        elif temp >= self.THROTTLE_C - 10:
            risk, score = "HIGH", 0.65
        elif temp >= 60:
            risk, score = "MODERATE", 0.80
        else:
            risk, score = "OPTIMAL", 0.95

        return {
            "score": _clamp(score),
            "feedback": f"thermal={temp:.1f}C {watts:.0f}W risk={risk}",

            "details": {"temp_c": round(temp, 1), "watts": round(watts, 0), "power_ratio": round(avg_pr, 3), "risk": risk, "mem_util": round(mem_util, 3)},
        }


class NetworkKernel(KernelModule):
    """Network topology and communication overhead evaluation."""
    name = "network"
    def evaluate(self, state: dict, action: dict) -> dict:
        topo = action.get("topology", "NVLink_v4")
        tp = action.get("tensor_parallel_size", 1)
        pp = action.get("pipeline_parallel_size", 1)
        hidden = state.get("model", {}).get("hidden_size", 4096)
        batch = action.get("batch_size", 4)
        r = compute_network_topology(topo, tp, pp, hidden, batch)
        score = _clamp(r["effective_throughput_pct"] / 100.0)
        return {"score": score, "feedback": f"network={r['effective_throughput_pct']:.1f}% risk={r['bottleneck_risk']}", "details": r}


class AgenticKernel:
    """The Agentic Kernel: zero-overhead, pure-math RL evaluation engine.

    Composes pluggable KernelModules (precision, thermal, network) to produce
    deterministic reward signals at 10,000+ evals/sec on a single CPU core.
    """
    DEFAULT_WEIGHTS = {"precision": 0.60, "thermal": 0.25, "network": 0.15}

    def __init__(self, modules=None, weights=None):
        if modules is None:
            modules = [PrecisionKernel(), ThermalKernel(), NetworkKernel()]
        self.modules = {m.name: m for m in modules}
        self.weights = weights or dict(self.DEFAULT_WEIGHTS)
        active = {k: v for k, v in self.weights.items() if k in self.modules}
        t = sum(active.values())
        if t > 0:
            self.weights = {k: v / t for k, v in active.items()}
        self._eval_count = 0
        self._total_ns = 0
        self._latencies = deque(maxlen=10_000)

    def evaluate(self, state: dict, action: dict) -> dict:
        start = time.perf_counter_ns()
        breakdown = {}
        ws = 0.0
        for name, mod in self.modules.items():
            r = mod.evaluate(state, action)
            breakdown[name] = r
            ws += r["score"] * self.weights.get(name, 0.0)
        score = _clamp(ws)
        ns = time.perf_counter_ns() - start
        us = ns / 1000.0
        self._eval_count += 1
        self._total_ns += ns
        self._latencies.append(us)
        parts = [f"{n}:{r['score']:.3f}" for n, r in breakdown.items()]
        return {"score": score, "feedback": f"kernel={score:.3f} [{', '.join(parts)}] ({us:.1f}us)", "breakdown": breakdown, "latency_us": round(us, 1)}

    def batch_evaluate(self, rollouts: list) -> list:
        return [self.evaluate(r["state"], r["action"]) for r in rollouts]

    def get_throughput_stats(self) -> dict:
        if not self._eval_count:
            return {"total_evaluations": 0, "throughput_evals_per_sec": 0.0, "mean_latency_us": 0.0, "median_latency_us": 0.0, "p99_latency_us": 0.0}
        s = sorted(self._latencies)
        total_s = self._total_ns / 1e9
        p99i = int(len(s) * 0.99)
        return {
            "total_evaluations": self._eval_count,
            "total_time_ms": round(self._total_ns / 1e6, 2),
            "throughput_evals_per_sec": round(self._eval_count / max(total_s, 1e-9), 0),
            "mean_latency_us": round(statistics.mean(self._latencies), 1),
            "median_latency_us": round(statistics.median(self._latencies), 1),
            "p99_latency_us": round(s[min(p99i, len(s)-1)], 1),
            "min_latency_us": round(s[0], 1),
            "max_latency_us": round(s[-1], 1),
        }

    def get_module_names(self) -> list:
        return list(self.modules.keys())

    def get_weights(self) -> dict:
        return dict(self.weights)

    def reset_stats(self):
        self._eval_count = 0
        self._total_ns = 0
        self._latencies = deque(maxlen=10_000)