File size: 20,062 Bytes
b413fd8
 
 
 
 
 
 
 
 
 
 
00282c2
b413fd8
 
 
 
c967683
b413fd8
c967683
b413fd8
00282c2
 
 
 
 
 
 
 
 
7ce90c2
 
 
b413fd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c967683
 
 
 
 
 
 
b413fd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00282c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ce90c2
 
 
 
56262e2
 
 
 
 
 
 
 
 
 
 
7ce90c2
00282c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13d819f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00282c2
 
13d819f
 
 
 
 
 
 
 
 
 
 
 
 
00282c2
 
 
b413fd8
 
 
a5023b8
b413fd8
 
 
 
a5023b8
 
 
 
 
 
 
 
 
 
b413fd8
 
 
13d819f
b413fd8
 
 
13d819f
b413fd8
 
 
 
13d819f
b413fd8
c967683
 
 
13d819f
c967683
b413fd8
13d819f
 
 
 
c967683
 
13d819f
b413fd8
 
13d819f
b413fd8
13d819f
 
b413fd8
 
 
 
 
 
c967683
 
 
b413fd8
 
 
 
 
c967683
 
 
b413fd8
 
 
a5023b8
 
 
 
 
 
 
 
 
 
 
 
 
 
b413fd8
 
8a93f94
cd80b88
 
 
 
8a93f94
 
 
cd80b88
8a93f94
 
 
 
 
 
 
 
00282c2
cd80b88
 
 
00282c2
 
 
 
 
8a93f94
 
b413fd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c967683
b413fd8
 
c967683
b413fd8
 
 
 
 
c967683
 
b413fd8
 
 
 
c967683
 
 
 
 
 
 
b413fd8
 
 
 
8a93f94
 
 
 
 
 
 
 
 
 
 
 
 
 
13d819f
 
 
 
b413fd8
 
 
 
 
8a93f94
b413fd8
8a93f94
 
 
b413fd8
13d819f
 
b413fd8
8a93f94
b413fd8
8a93f94
 
 
 
 
 
 
00282c2
 
 
 
 
 
 
 
 
b413fd8
 
 
 
 
 
 
 
00282c2
 
 
 
 
 
 
 
 
 
 
 
13d819f
 
00282c2
 
 
 
 
 
 
 
 
 
b413fd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
#!/usr/bin/env python3
"""Build the dashboard's public JSON payload from a W&B project."""
from __future__ import annotations

import argparse
import json
import math
import os
import sys
from collections import defaultdict
from datetime import datetime, timezone
from itertools import islice

_SCORE_PREFIX = "miner/"
_SCORE_SUFFIX = "/score"
_LEN_SUFFIX = "/completion_len"
_CORRECTNESS_SUFFIX = "/correctness_score"
_ORIG_SCORE = "original/score"
_ORIG_CORRECTNESS = "original/correctness_score"
_ORIG_LEN = "original/completion_len"
_PROGRESS_STAGES = {"qualification", "full_evaluation"}
_PROGRESS_STATUSES = {"running", "completed", "failed"}
_STAGE_STATUSES = {
    "pending", "waiting", "preparing", "evaluating", "completed", "skipped", "failed"
}
_MINER_STATUSES = {
    "pending", "waiting", "evaluating", "finished", "failed", "skipped",
    "not_selected", "rejected",
}
_BASELINE_STATUSES = {
    "pending", "waiting", "evaluating", "finished", "failed", "skipped",
}


def _miner_hotkey_from_score_key(key: str) -> str | None:
    if key.startswith(_SCORE_PREFIX) and key.endswith(_SCORE_SUFFIX):
        hk = key[len(_SCORE_PREFIX) : -len(_SCORE_SUFFIX)]
        return hk or None
    return None


def _as_float(value) -> float | None:
    try:
        f = float(value)
    except (TypeError, ValueError):
        return None
    return f if math.isfinite(f) else None


def _score_to_signed_correctness(value) -> float | None:
    score = _as_float(value)
    if score is None:
        return None
    return (2.0 * score) - 1.0


def extract_rankings(summary: dict) -> list[dict]:
    """Return the current miner ranking from a run summary."""
    out = []
    for key, value in summary.items():
        hk = _miner_hotkey_from_score_key(key)
        if hk is None:
            continue
        score = _as_float(value)
        if score is None:
            continue
        out.append({"hotkey": hk, "score": score})
    out.sort(key=lambda r: r["score"], reverse=True)
    return out


def _summary_dict(run) -> dict:
    summary = getattr(run.summary, "_json_dict", run.summary)
    return dict(summary)


def extract_progress(summary: dict) -> dict | None:
    raw = summary.get("progress/snapshot")
    if isinstance(raw, str):
        try:
            raw = json.loads(raw)
        except json.JSONDecodeError:
            return None
    if not isinstance(raw, dict):
        return None

    epoch = _as_float(raw.get("epoch"))
    status = raw.get("status")
    if epoch is None or status not in _PROGRESS_STATUSES:
        return None

    stages = {}
    raw_stages = raw.get("stages")
    if isinstance(raw_stages, dict):
        for stage_name in _PROGRESS_STAGES:
            raw_stage = raw_stages.get(stage_name)
            if not isinstance(raw_stage, dict):
                continue
            stage_status = raw_stage.get("status")
            if stage_status not in _STAGE_STATUSES:
                stage_status = "pending"
            miners = {}
            raw_miners = raw_stage.get("miners")
            if isinstance(raw_miners, dict):
                for hotkey, miner_status in list(raw_miners.items())[:512]:
                    if (
                        isinstance(hotkey, str)
                        and hotkey
                        and miner_status in _MINER_STATUSES
                    ):
                        miners[hotkey] = miner_status
            stage = {"status": stage_status, "miners": miners}
            baseline_status = raw_stage.get("baseline")
            if baseline_status in _BASELINE_STATUSES:
                stage["baseline"] = baseline_status
            scores = {}
            raw_scores = raw_stage.get("scores")
            if isinstance(raw_scores, dict):
                for hotkey, score in list(raw_scores.items())[:512]:
                    if not isinstance(hotkey, str) or not hotkey:
                        continue
                    value = _as_float(score)
                    if value is not None:
                        scores[hotkey] = value
            if scores:
                stage["scores"] = scores
            stages[stage_name] = stage

    current_stage = raw.get("current_stage")
    if current_stage not in _PROGRESS_STAGES:
        current_stage = None
    updated_at = raw.get("updated_at")
    return {
        "schema_version": 1,
        "epoch": int(epoch) if epoch == int(epoch) else epoch,
        "status": status,
        "current_stage": current_stage,
        "updated_at": updated_at[:64] if isinstance(updated_at, str) else None,
        "stages": stages,
    }


def _parse_iso(value) -> datetime | None:
    if not isinstance(value, str):
        return None
    try:
        return datetime.fromisoformat(value)
    except ValueError:
        return None


def _snapshot_for_epoch(raw, epoch: float) -> dict | None:
    if isinstance(raw, str):
        try:
            raw = json.loads(raw)
        except json.JSONDecodeError:
            return None
    if not isinstance(raw, dict):
        return None
    row_epoch = _as_float(raw.get("epoch"))
    if row_epoch is None or row_epoch != epoch:
        return None
    return raw


def extract_stage_timings(run, epoch) -> dict[str, dict]:
    """Replay a run's progress history to time how long each stage's baseline
    evaluation actually took for the given epoch.

    The live progress snapshot (summary) only ever holds the *latest* stage
    state, so a fast baseline/miner pass can flip from "evaluating" to
    "finished" between two dashboard polls without ever being observed live.
    Scanning history lets the dashboard show concrete proof ("base done in
    4.2s") instead of silently losing that transition.
    """
    epoch_value = _as_float(epoch)
    if epoch_value is None:
        return {}

    active_stage: dict[str, datetime] = {}
    active_baseline: dict[str, datetime] = {}
    timings: dict[str, dict] = {}

    try:
        rows = run.scan_history()
    except Exception:
        return {}

    for row in rows:
        snapshot = _snapshot_for_epoch(row.get("progress/snapshot"), epoch_value)
        if snapshot is None:
            continue
        ts = _parse_iso(snapshot.get("updated_at"))
        raw_stages = snapshot.get("stages")
        if not isinstance(raw_stages, dict):
            continue
        for stage_name in _PROGRESS_STAGES:
            stage = raw_stages.get(stage_name)
            if not isinstance(stage, dict):
                continue
            status = stage.get("status")
            if status == "evaluating" and stage_name not in active_stage:
                active_stage[stage_name] = ts
            elif (
                status in {"completed", "skipped", "failed"}
                and stage_name in active_stage
                and "duration_seconds" not in timings.get(stage_name, {})
            ):
                start = active_stage[stage_name]
                if start is not None and ts is not None:
                    timings.setdefault(stage_name, {})["duration_seconds"] = (
                        ts - start
                    ).total_seconds()

            baseline = stage.get("baseline")
            if baseline == "evaluating" and stage_name not in active_baseline:
                active_baseline[stage_name] = ts
            elif (
                baseline in {"finished", "skipped", "failed"}
                and stage_name in active_baseline
                and "baseline_seconds" not in timings.get(stage_name, {})
            ):
                start = active_baseline[stage_name]
                if start is not None and ts is not None:
                    timings.setdefault(stage_name, {})["baseline_seconds"] = (
                        ts - start
                    ).total_seconds()

    return timings


def progress_for_run(run, summary: dict) -> dict | None:
    progress = extract_progress(summary)
    if progress is None:
        return None
    if progress["status"] == "running" and getattr(run, "state", None) in {
        "crashed",
        "failed",
        "killed",
    }:
        progress["status"] = "failed"
    timings = extract_stage_timings(run, progress["epoch"])
    for stage_name, timing in timings.items():
        stage = progress["stages"].get(stage_name)
        if isinstance(stage, dict):
            stage.update(timing)
    return progress


def extract_history(run) -> dict:
    """Return aligned per-epoch miner and base-model series for one run."""
    rows_by_epoch: dict[float, dict] = {}
    stage_scores_by_epoch: dict[float, dict[str, dict[str, float]]] = {}
    miner_keys: set[str] = set()
    saw_orig_score = saw_orig_len = False

    for row in run.scan_history():
        progress = extract_progress(row)
        if progress is not None:
            progress_epoch = _as_float(progress.get("epoch"))
            if progress_epoch is not None:
                epoch_scores = stage_scores_by_epoch.setdefault(progress_epoch, {})
                for stage_name in _PROGRESS_STAGES:
                    scores = progress.get("stages", {}).get(stage_name, {}).get("scores")
                    if scores:
                        epoch_scores.setdefault(stage_name, {}).update(scores)

        epoch = _as_float(row.get("epoch"))
        if epoch is None:
            continue
        row_metrics = {}
        for key, value in row.items():
            hk = _miner_hotkey_from_score_key(key)
            if hk is not None:
                row_metrics[("miner", hk, "score")] = _as_float(value)
                miner_keys.add(hk)
            elif key.startswith(_SCORE_PREFIX) and key.endswith(_LEN_SUFFIX):
                hk2 = key[len(_SCORE_PREFIX) : -len(_LEN_SUFFIX)]
                if hk2:
                    row_metrics[("miner", hk2, "completion_len")] = _as_float(value)
                    miner_keys.add(hk2)
            elif key.startswith(_SCORE_PREFIX) and key.endswith(_CORRECTNESS_SUFFIX):
                hk2 = key[len(_SCORE_PREFIX) : -len(_CORRECTNESS_SUFFIX)]
                if hk2:
                    row_metrics[("miner", hk2, "correctness_score")] = _as_float(value)
                    miner_keys.add(hk2)
            elif key == _ORIG_SCORE:
                row_metrics[("orig", "score")] = _as_float(value)
                row_metrics.setdefault(
                    ("orig", "correctness_score"), _score_to_signed_correctness(value)
                )
                saw_orig_score = True
            elif key == _ORIG_CORRECTNESS:
                row_metrics[("orig", "correctness_score")] = _as_float(value)
                saw_orig_score = True
            elif key == _ORIG_LEN:
                row_metrics[("orig", "completion_len")] = _as_float(value)
                saw_orig_len = True
        if any(value is not None for value in row_metrics.values()):
            rows_by_epoch.setdefault(epoch, {}).update(row_metrics)

    epochs = sorted(rows_by_epoch)
    miners: dict[str, dict] = {}
    for hk in miner_keys:
        miners[hk] = {
            "score": [rows_by_epoch[e].get(("miner", hk, "score")) for e in epochs],
            "correctness_score": [
                rows_by_epoch[e].get(("miner", hk, "correctness_score")) for e in epochs
            ],
            "completion_len": [rows_by_epoch[e].get(("miner", hk, "completion_len")) for e in epochs],
        }
    original = {}
    if saw_orig_score:
        original["score"] = [rows_by_epoch[e].get(("orig", "score")) for e in epochs]
        original["correctness_score"] = [
            rows_by_epoch[e].get(("orig", "correctness_score")) for e in epochs
        ]
    if saw_orig_len:
        original["completion_len"] = [rows_by_epoch[e].get(("orig", "completion_len")) for e in epochs]

    stage_scores = [
        {
            "epoch": int(epoch) if epoch == int(epoch) else epoch,
            **stage_scores_by_epoch[epoch],
        }
        for epoch in sorted(stage_scores_by_epoch)
        if stage_scores_by_epoch[epoch]
    ]
    return {
        "epochs": [int(e) if e == int(e) else e for e in epochs],
        "miners": miners,
        "original": original,
        "stage_scores": stage_scores,
    }


def rankings_from_history(history: dict) -> list[dict]:
    epochs = history.get("epochs", [])
    if not epochs:
        return []
    latest_index = len(epochs) - 1
    rankings = []
    for hotkey, series in history.get("miners", {}).items():
        scores = series.get("score", [])
        score = scores[latest_index] if latest_index < len(scores) else None
        if score is not None:
            rankings.append({"hotkey": hotkey, "score": score})
    rankings.sort(key=lambda row: row["score"], reverse=True)
    return rankings


def validator_payload(run, hotkey: str) -> dict:
    history = extract_history(run)
    summary = _summary_dict(run)
    rankings = rankings_from_history(history) or (
        extract_rankings(summary) if not history.get("epochs") else []
    )
    payload = {"hotkey": hotkey, "rankings": rankings, "history": history}
    progress = progress_for_run(run, summary)
    if progress is not None:
        payload["progress"] = progress
    return payload


def build_aggregate(validators: list[dict]) -> dict:
    """Average miner scores across validators that ranked each miner."""
    score_sums: dict[str, float] = defaultdict(float)
    score_counts: dict[str, int] = defaultdict(int)
    for v in validators:
        for r in v["rankings"]:
            score_sums[r["hotkey"]] += r["score"]
            score_counts[r["hotkey"]] += 1

    rankings = [
        {"hotkey": hk, "score": score_sums[hk] / score_counts[hk], "validators": score_counts[hk]}
        for hk in score_sums
    ]
    rankings.sort(key=lambda r: r["score"], reverse=True)

    history = {"epochs": [], "miners": {}, "original": {}}
    if rankings:
        top = rankings[0]["hotkey"]
        history = _mean_history(validators, top)
    return {"rankings": rankings, "history": history}


def _mean_history(validators: list[dict], top_hotkey: str) -> dict:
    """Average per-epoch history for the leading miner and base model."""
    epochs = sorted({e for v in validators for e in v["history"].get("epochs", [])})

    def mean_series(getter):
        out = []
        for e in epochs:
            vals = []
            for v in validators:
                hist = v["history"]
                if e not in hist.get("epochs", []):
                    continue
                pos = hist["epochs"].index(e)
                val = getter(hist, pos)
                if val is not None and val == val:
                    vals.append(val)
            out.append(sum(vals) / len(vals) if vals else None)
        return out

    miner_score = mean_series(lambda h, p: (h["miners"].get(top_hotkey, {}).get("score") or [None] * (p + 1))[p] if h["miners"].get(top_hotkey) else None)
    miner_correctness = mean_series(lambda h, p: (h["miners"].get(top_hotkey, {}).get("correctness_score") or [None] * (p + 1))[p] if h["miners"].get(top_hotkey) else None)
    miner_len = mean_series(lambda h, p: (h["miners"].get(top_hotkey, {}).get("completion_len") or [None] * (p + 1))[p] if h["miners"].get(top_hotkey) else None)
    orig_score = mean_series(lambda h, p: (h["original"].get("score") or [None] * (p + 1))[p] if h["original"].get("score") else None)
    orig_correctness = mean_series(lambda h, p: (h["original"].get("correctness_score") or [None] * (p + 1))[p] if h["original"].get("correctness_score") else None)
    orig_len = mean_series(lambda h, p: (h["original"].get("completion_len") or [None] * (p + 1))[p] if h["original"].get("completion_len") else None)

    original = {}
    if any(x is not None for x in orig_score):
        original["score"] = orig_score
    if any(x is not None for x in orig_correctness):
        original["correctness_score"] = orig_correctness
    if any(x is not None for x in orig_len):
        original["completion_len"] = orig_len
    return {
        "epochs": epochs,
        "miners": {
            top_hotkey: {
                "score": miner_score,
                "correctness_score": miner_correctness,
                "completion_len": miner_len,
            }
        },
        "original": original,
    }


def wandb_path(project: str, entity: str | None) -> str:
    project = project.strip()
    entity = entity.strip() if entity else None
    if "/" in project:
        project_entity, project_name = (part.strip() for part in project.rsplit("/", 1))
        if not project_entity or not project_name:
            raise ValueError("W&B project must be 'project' or 'entity/project'")
        project = project_name
        entity = entity or project_entity
    if not project:
        raise ValueError("W&B project cannot be empty")
    return f"{entity}/{project}" if entity else project


def _is_running_run(run) -> bool:
    return getattr(run, "state", None) == "running"


def build(project: str, entity: str | None, api=None) -> dict:
    if api is None:
        import wandb

        api = wandb.Api()
    path = wandb_path(project, entity)

    order: list[str] = []
    fallbacks: dict[str, dict] = {}
    selected: dict[str, dict] = {}
    for run in api.runs(path, order="-created_at"):
        if not _is_running_run(run):
            continue
        hotkey = dict(run.config).get("validator_hotkey")
        if not isinstance(hotkey, str) or not hotkey or hotkey in selected:
            continue
        payload = validator_payload(run, hotkey)
        if hotkey not in fallbacks:
            order.append(hotkey)
            fallbacks[hotkey] = payload
        if payload["rankings"]:
            selected[hotkey] = payload

    validators = []
    for hotkey in order:
        latest = fallbacks[hotkey]
        chosen = dict(selected.get(hotkey, latest))
        if "progress" in latest:
            chosen["progress"] = latest["progress"]
        else:
            chosen.pop("progress", None)
        validators.append(chosen)

    return {
        "generated_at": datetime.now(timezone.utc).isoformat(),
        "validators": validators,
        "aggregate": build_aggregate(validators),
    }


def build_progress(
    project: str, entity: str | None, api=None, run_limit: int = 500
) -> dict[str, dict]:
    if api is None:
        import wandb

        api = wandb.Api()
    path = wandb_path(project, entity)
    progress_by_validator = {}
    seen = set()
    runs = api.runs(path, order="-created_at")
    for run in islice(runs, max(1, run_limit)):
        if not _is_running_run(run):
            continue
        hotkey = dict(run.config).get("validator_hotkey")
        if not isinstance(hotkey, str) or not hotkey or hotkey in seen:
            continue
        seen.add(hotkey)
        progress = progress_for_run(run, _summary_dict(run))
        if progress is not None:
            progress_by_validator[hotkey] = progress
    return progress_by_validator


def main(argv: list[str] | None = None) -> int:
    parser = argparse.ArgumentParser(description="Generate the static dashboard's data.json from wandb.")
    parser.add_argument("--wandb-project", required=True, help="Shared wandb project every validator logs into")
    parser.add_argument("--wandb-entity", default=None, help="Wandb entity/team (default: the API key's default entity)")
    parser.add_argument("--out", default="data.json", help="Output path for the generated JSON")
    args = parser.parse_args(argv)

    key = os.environ.get("WANDB_KEY") or os.environ.get("WANDB_API_KEY")
    if not key:
        print("error: set WANDB_KEY (or WANDB_API_KEY) in the environment", file=sys.stderr)
        return 2
    os.environ["WANDB_API_KEY"] = key

    data = build(args.wandb_project, args.wandb_entity)
    with open(args.out, "w", encoding="utf-8") as fh:
        json.dump(data, fh, indent=2)
        fh.write("\n")
    print(f"wrote {args.out}: {len(data['validators'])} validators, "
          f"{len(data['aggregate']['rankings'])} ranked miners")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())