-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit.py
More file actions
1189 lines (1082 loc) · 40.9 KB
/
commit.py
File metadata and controls
1189 lines (1082 loc) · 40.9 KB
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asyncio
import json
import sys
from pathlib import Path
from typing import Callable
import requests
import click
from loguru import logger
from targon_client import TargonClient, ContainerDeployConfig
from targon_utils import ensure_running_container
from generator import Generator, PromptItem
from judge import Judge
from renderer import Renderer
from targon.client.serverless import ServerlessResourceListItem
from models import State, Schedule
_GENERATOR_POD_NAME: str = "generator"
_GENERATOR_PORT: int = 10006
_GENERATOR_HEALTH_CHECK_PATH: str = "/health"
_GENERATOR_RESOURCE_NAME: str = "h200-large"
_RENDER_POD_NAME: str = "render"
_RENDER_PORT: int = 8000
_RENDER_HEALTH_CHECK_PATH: str = "/health"
_RENDER_IMAGE_URL: str = (
"europe-west3-docker.pkg.dev/gen-456515/active-competition/render-service-js:0.4.3"
)
_RENDER_RESOURCE_NAME: str = "cpu-small"
_JUDGE_POD_NAME: str = "judge"
_JUDGE_PORT: int = 8000
_JUDGE_HEALTH_CHECK_PATH: str = "/health"
_JUDGE_IMAGE_URL: str = "vllm/vllm-openai:v0.20.0"
_JUDGE_RESOURCE_NAME: str = "h200-small"
_JUDGE_ARGS: list[str] = [
"zai-org/GLM-4.6V-Flash",
"--revision",
"411bb4d77144a3f03accbf4b780f5acb8b7cde4e",
"--served-model-name",
"glm-4.6v-flash",
"--host",
"0.0.0.0",
"--port",
"8000",
"--trust-remote-code",
"--dtype",
"bfloat16",
"--gpu-memory-utilization",
"0.90",
"--max-model-len",
"32768",
"--max-num-seqs",
"32",
]
_GITHUB_URL: str = (
"https://raw.githubusercontent.com/404-Repo/404-active-competition/main"
)
_REQUIRED_LOCK_ALPHA: float = 100.0
@click.group()
@click.option(
"-v", "--verbose", count=True, help="Verbosity: -v INFO, -vv DEBUG, -vvv TRACE"
)
def cli(verbose: int) -> None:
levels = {0: "WARNING", 1: "INFO", 2: "DEBUG"}
logger.remove()
logger.add(sys.stderr, level=levels.get(verbose, "TRACE"))
def _fetch_state() -> State:
"""Download and parse state.json from GitHub."""
state_url = f"{_GITHUB_URL}/state.json"
try:
response = requests.get(state_url, timeout=10)
response.raise_for_status()
# response.json() already returns a dict; use model_validate for dict input
return State.model_validate(response.json())
except requests.RequestException as e:
logger.error(f"Failed to fetch state.json: {e}")
raise RuntimeError(f"Failed to fetch state.json from {state_url}: {str(e)}")
except json.JSONDecodeError as e:
logger.error(f"Failed to parse state.json: {e}")
raise RuntimeError(f"Failed to parse state.json: {str(e)}")
def _fetch_schedule(round_number: int) -> Schedule:
"""Download and parse schedule.json from GitHub for a specific round."""
schedule_url = f"{_GITHUB_URL}/rounds/{round_number}/schedule.json"
try:
response = requests.get(schedule_url, timeout=10)
response.raise_for_status()
return Schedule.model_validate(response.json())
except requests.RequestException as e:
logger.error(f"Failed to fetch schedule.json for round {round_number}: {e}")
raise RuntimeError(
f"Failed to fetch schedule.json from {schedule_url}: {str(e)}"
)
except json.JSONDecodeError as e:
logger.error(f"Failed to parse schedule.json for round {round_number}: {e}")
raise RuntimeError(f"Failed to parse schedule.json: {str(e)}")
def _decode_revealed_commitment(com: object, block: int) -> tuple[int, str]:
"""Decode a single revealed commitment, tolerant of both shapes the chain returns.
Works around a bittensor 10.x bug: ``get_all_revealed_commitments`` assumes every
commitment is a ``0x`` hex string and calls ``bytes.fromhex()``, but the runtime
returns an already-decoded ``str`` whenever the payload is valid UTF-8 (raising
"non-hexadecimal number found in fromhex()"). We handle both, then strip the SCALE
compact-length prefix the same way bittensor does.
"""
if isinstance(com, str) and com.startswith("0x"):
raw = bytes.fromhex(com[2:])
elif isinstance(com, str):
raw = com.encode("utf-8", errors="ignore")
else:
raw = bytes(com) # type: ignore[arg-type]
if not raw:
return block, ""
mode = raw[0] & 0b11
offset = 1 if mode == 0 else 2 if mode == 1 else 4
return block, raw[offset:].decode("utf-8", errors="ignore")
async def _get_all_revealed_commitments(subtensor, netuid: int) -> dict[str, tuple]:
"""Robust replacement for ``subtensor.get_all_revealed_commitments`` (see above)."""
query = await subtensor.query_map(
module="Commitments", name="RevealedCommitments", params=[netuid]
)
result: dict[str, tuple] = {}
async for hotkey, data in query:
result[hotkey] = tuple(_decode_revealed_commitment(p[0], p[1]) for p in data)
return result
async def _fetch_and_parse_commitments(
subtensor_endpoint: str,
netuid: int,
round_number: int,
schedule: Schedule,
current_round: int,
) -> dict[str, dict]:
"""Fetch commitments from subtensor and parse them for a specific round."""
import bittensor as bt # Bittensor import should be here because bittensor captures command line args for click otherwise
async with bt.AsyncSubtensor(subtensor_endpoint) as subtensor:
raw_commitments = await _get_all_revealed_commitments(subtensor, netuid)
return _parse_commitments(
raw_commitments, round_number, schedule, current_round
)
def _check_lock_status(
subtensor_endpoint: str,
coldkey_ss58: str,
netuid: int,
current_hotkey: str,
commitment_hotkeys: set[str],
per_hotkey_alpha: float,
) -> tuple[float, int, float]:
"""Return (locked_alpha, num_submitting_hotkeys, required_alpha) for a coldkey.
The lock requirement scales with the number of this coldkey's hotkeys that have a
submission this round (the hotkey being committed now always counts):
required = per_hotkey_alpha * num_submitting_hotkeys.
"""
import bittensor as bt # Bittensor import should be here because bittensor captures command line args for click otherwise
async def _query() -> "tuple[list[str], object | None]":
async with bt.AsyncSubtensor(subtensor_endpoint) as subtensor:
owned = await subtensor.get_owned_hotkeys(coldkey_ss58)
lock = await subtensor.get_coldkey_lock(
coldkey_ss58=coldkey_ss58, netuid=netuid
)
return owned, lock
owned_hotkeys, lock = asyncio.run(_query())
submitting = {hk for hk in commitment_hotkeys if hk in set(owned_hotkeys)}
submitting.add(
current_hotkey
) # the hotkey we are committing for belongs to this coldkey
num_submitting = len(submitting)
locked_alpha = float(lock["locked_mass"].tao) if lock is not None else 0.0
return locked_alpha, num_submitting, num_submitting * per_hotkey_alpha
def _warn_if_insufficient_lock(
subtensor_endpoint: str,
coldkey_ss58: str,
netuid: int,
current_hotkey: str,
commitment_hotkeys: set[str],
per_hotkey_alpha: float,
) -> None:
"""Warn the miner (non-blocking) if their coldkey lacks the required conviction lock."""
try:
locked_alpha, num_submitting, required_alpha = _check_lock_status(
subtensor_endpoint,
coldkey_ss58,
netuid,
current_hotkey,
commitment_hotkeys,
per_hotkey_alpha,
)
except Exception as e:
click.echo(
f"WARNING: Failed to check conviction lock for your coldkey: {str(e)}",
err=True,
)
return
if locked_alpha < required_alpha:
click.echo(
f"WARNING: Your coldkey has only {locked_alpha:.4f} ρ locked on netuid {netuid}, but "
f"{required_alpha:.0f} is required ({per_hotkey_alpha:.0f} ρ per submitting hotkey x {num_submitting}). "
f"Lock at least {required_alpha:.0f} ρ (conviction) with your coldkey or your submissions may not be scored.",
err=True,
)
@cli.command("commit-hash")
@click.option("--hash", "commit_hash", required=True, help="HF commit SHA")
@click.option("--netuid", default=17, show_default=True)
@click.option(
"--subtensor.endpoint", "subtensor_endpoint", default="finney", show_default=True
)
@click.option(
"--wallet.name",
"wallet_name",
required=True,
help="Name of the bittensor wallet to use",
)
@click.option(
"--wallet.hotkey", "wallet_hotkey", required=True, help="Hotkey name of the wallet"
)
@click.option(
"--wallet.path",
"wallet_path",
default=None,
help="Path to the wallet directory (default: ~/.bittensor)",
)
@click.option(
"--required-lock",
"required_lock_per_hotkey",
type=float,
default=_REQUIRED_LOCK_ALPHA,
show_default=True,
help="Required locked ρ (conviction) per submitting hotkey.",
)
def commit_hash_cmd(
commit_hash: str,
netuid: int,
subtensor_endpoint: str,
wallet_name: str,
wallet_hotkey: str,
wallet_path: str | None,
required_lock_per_hotkey: float,
) -> None:
"""Commit revision hash on-chain."""
import bittensor as bt # Bittensor import should be here because bittensor captures command line args for click otherwise
try:
state = _fetch_state()
except Exception as e:
click.echo(
json.dumps({"success": False, "error": f"Failed to fetch state: {str(e)}"})
)
raise SystemExit(1)
try:
schedule = _fetch_schedule(state.current_round)
except Exception as e:
click.echo(
json.dumps(
{"success": False, "error": f"Failed to fetch schedule: {str(e)}"}
)
)
raise SystemExit(1)
try:
current_block = asyncio.run(
bt.AsyncSubtensor(subtensor_endpoint).get_current_block()
)
if current_block < schedule.earliest_reveal_block:
click.echo(
json.dumps(
{
"success": False,
"error": f"Current block {current_block} is before the earliest reveal block {schedule.earliest_reveal_block}",
}
)
)
raise SystemExit(1)
except Exception as e:
click.echo(
json.dumps(
{"success": False, "error": f"Failed to fetch current block: {str(e)}"}
)
)
raise SystemExit(1)
round_to_commit = (
state.current_round
if current_block <= schedule.latest_reveal_block
else state.current_round + 1
)
try:
commitments = asyncio.run(
_fetch_and_parse_commitments(
subtensor_endpoint=subtensor_endpoint,
netuid=netuid,
round_number=round_to_commit,
schedule=schedule,
current_round=state.current_round,
)
)
wallet = bt.Wallet(name=wallet_name, hotkey=wallet_hotkey, path=wallet_path)
hotkey = wallet.hotkey.ss58_address
if hotkey not in commitments:
click.echo(
f"WARNING: You have not commited repo and cdn_url for round {round_to_commit}.",
err=True,
)
elif not commitments[hotkey]["repo"] or not commitments[hotkey]["cdn_url"]:
click.echo(
f"WARNING: You have not commited repo and cdn_url for round {round_to_commit}.",
err=True,
)
_warn_if_insufficient_lock(
subtensor_endpoint,
wallet.coldkeypub.ss58_address,
netuid,
hotkey,
set(commitments),
required_lock_per_hotkey,
)
except Exception as e:
click.echo(
f"WARNING: Failed to fetch information about your commitments in round {round_to_commit}: {str(e)}",
err=True,
)
_run_commit(
data={"commit": commit_hash},
netuid=netuid,
subtensor_endpoint=subtensor_endpoint,
wallet_name=wallet_name,
wallet_hotkey=wallet_hotkey,
wallet_path=wallet_path,
state=state,
current_round=round_to_commit,
)
@cli.command("commit-repo-cdn")
@click.option("--repo", required=True, help="HF repo id (e.g. user/repo)")
@click.option(
"--cdn-url",
required=True,
help="URL of the S3 compatible object storage that saves the generated PLY files",
)
@click.option("--netuid", default=17, show_default=True)
@click.option(
"--subtensor.endpoint", "subtensor_endpoint", default="finney", show_default=True
)
@click.option(
"--wallet.name",
"wallet_name",
required=True,
help="Name of the bittensor wallet to use",
)
@click.option(
"--wallet.hotkey", "wallet_hotkey", required=True, help="Hotkey name of the wallet"
)
@click.option(
"--wallet.path",
"wallet_path",
default=None,
help="Path to the wallet directory (default: ~/.bittensor)",
)
@click.option(
"--required-lock",
"required_lock_per_hotkey",
type=float,
default=_REQUIRED_LOCK_ALPHA,
show_default=True,
help="Required locked ρ (conviction) per submitting hotkey.",
)
def commit_repo_cdn_cmd(
repo: str,
cdn_url: str,
netuid: int,
subtensor_endpoint: str,
wallet_name: str,
wallet_hotkey: str,
wallet_path: str | None,
required_lock_per_hotkey: float,
) -> None:
"""Commit repo and CDN URL on-chain."""
import bittensor as bt # Bittensor import should be here because bittensor captures command line args for click otherwise
try:
state = _fetch_state()
except Exception as e:
click.echo(
json.dumps({"success": False, "error": f"Failed to fetch state: {str(e)}"})
)
raise SystemExit(1)
try:
schedule = _fetch_schedule(state.current_round)
except Exception as e:
click.echo(
json.dumps(
{"success": False, "error": f"Failed to fetch schedule: {str(e)}"}
)
)
raise SystemExit(1)
try:
current_block = asyncio.run(
bt.AsyncSubtensor(subtensor_endpoint).get_current_block()
)
if current_block < schedule.earliest_reveal_block:
click.echo(
json.dumps(
{
"success": False,
"error": f"Current block {current_block} is before the earliest reveal block {schedule.earliest_reveal_block}",
}
)
)
raise SystemExit(1)
except Exception as e:
click.echo(
json.dumps(
{"success": False, "error": f"Failed to fetch current block: {str(e)}"}
)
)
raise SystemExit(1)
round_to_commit = (
state.current_round
if current_block <= schedule.latest_reveal_block
else state.current_round + 1
)
try:
commitments = asyncio.run(
_fetch_and_parse_commitments(
subtensor_endpoint=subtensor_endpoint,
netuid=netuid,
round_number=round_to_commit,
schedule=schedule,
current_round=state.current_round,
)
)
wallet = bt.Wallet(name=wallet_name, hotkey=wallet_hotkey, path=wallet_path)
hotkey = wallet.hotkey.ss58_address
if hotkey not in commitments:
click.echo(
json.dumps(
{
"success": False,
"error": f"You have not committed hash for round {round_to_commit}. Please commit hash first.",
}
)
)
raise SystemExit(1)
elif not commitments[hotkey]["commit_hash"]:
click.echo(
json.dumps(
{
"success": False,
"error": f"You have not committed hash for round {round_to_commit}. Please commit hash first.",
}
)
)
raise SystemExit(1)
_warn_if_insufficient_lock(
subtensor_endpoint,
wallet.coldkeypub.ss58_address,
netuid,
hotkey,
set(commitments),
required_lock_per_hotkey,
)
except SystemExit:
raise
except Exception as e:
click.echo(
json.dumps(
{
"success": False,
"error": f"Failed to fetch information about your commitments in round {round_to_commit}: {str(e)}",
}
)
)
raise SystemExit(1)
_run_commit(
data={"repo": repo, "cdn_url": cdn_url},
netuid=netuid,
subtensor_endpoint=subtensor_endpoint,
wallet_name=wallet_name,
wallet_hotkey=wallet_hotkey,
wallet_path=wallet_path,
state=state,
current_round=round_to_commit,
)
def _run_commit(
*,
data: dict,
netuid: int,
subtensor_endpoint: str,
wallet_name: str,
wallet_hotkey: str,
wallet_path: str | None,
state: State,
current_round: int,
) -> None:
import bittensor as bt # Bittensor import should be here because bittensor captures --help command otherwise
wallet = bt.Wallet(name=wallet_name, hotkey=wallet_hotkey, path=wallet_path)
logger.info(f"Committing {data} with wallet {wallet_name}@{wallet_hotkey}")
async def _commit() -> None:
async with bt.AsyncSubtensor(subtensor_endpoint) as subtensor:
payload = json.dumps(data)
success, block = await subtensor.set_reveal_commitment(
wallet=wallet,
netuid=netuid,
data=payload,
blocks_until_reveal=2,
)
if success:
click.echo(f"Committed at block {block}")
else:
raise RuntimeError(f"Commitment failed at block {block}")
try:
asyncio.run(_commit())
data["round"] = current_round
click.echo(json.dumps({"success": True, **data}))
except Exception as e:
logger.error(f"Commit failed: {e}")
click.echo(json.dumps({"success": False, "error": str(e)}))
raise SystemExit(1)
def _render_commitments_table(results: list[dict], round_number: int) -> None:
"""Pretty-print revealed commitments as a colored rich table."""
from rich.console import Console
from rich.table import Table
from rich import box
def _short(addr: str | None, head: int = 6, tail: int = 4) -> str:
if not addr:
return "[dim]—[/]"
return f"{addr[:head]}…{addr[-tail:]}"
table = Table(
title=f"Revealed commitments — round {round_number}",
title_style="bold",
header_style="bold cyan",
box=box.SIMPLE_HEAVY,
expand=False,
)
table.add_column("Hotkey", no_wrap=True)
table.add_column("Hash", style="dim", no_wrap=True)
table.add_column("Repo", overflow="ellipsis", max_width=32)
table.add_column("CDN", overflow="ellipsis", max_width=36)
table.add_column("Coldkey", no_wrap=True)
table.add_column("Locked ρ", justify="right", no_wrap=True)
table.add_column("Req ρ", justify="right", no_wrap=True)
table.add_column("OK", justify="center", no_wrap=True)
n_ok = n_short = n_unknown = 0
for e in results:
sufficient = e.get("lock_sufficient")
if sufficient is True:
ok, locked_style = "[green]✓[/]", "green"
n_ok += 1
elif sufficient is False:
ok, locked_style = "[red]✗[/]", "red"
n_short += 1
else:
ok, locked_style = "[dim]?[/]", "dim"
n_unknown += 1
locked = e.get("locked_rho")
required = e.get("required_rho")
locked_str = (
f"[{locked_style}]{locked:.2f}[/]" if locked is not None else "[dim]—[/]"
)
required_str = f"{required:.0f}" if required is not None else "[dim]—[/]"
commit_hash = e.get("commit_hash") or ""
table.add_row(
_short(e.get("hotkey")),
commit_hash[:10] if commit_hash else "[dim]—[/]",
e.get("repo") or "[dim]—[/]",
e.get("cdn_url") or "[dim]—[/]",
_short(e.get("coldkey")),
locked_str,
required_str,
ok,
)
console = Console()
console.print(table)
console.print(
f"[bold]{len(results)}[/] commitments "
f"[green]{n_ok} sufficient[/] "
f"[red]{n_short} insufficient[/] "
f"[dim]{n_unknown} unknown[/]"
)
@cli.command("list-all")
@click.option("--netuid", default=17, show_default=True)
@click.option(
"--subtensor.endpoint", "subtensor_endpoint", default="finney", show_default=True
)
@click.option(
"--required-lock",
"required_lock_per_hotkey",
type=float,
default=_REQUIRED_LOCK_ALPHA,
show_default=True,
help="Required locked ρ (conviction) per submitting hotkey.",
)
@click.option(
"--table",
"as_table",
is_flag=True,
default=False,
help="Render a colored table instead of JSON lines.",
)
def list_all_cmd(
netuid: int,
subtensor_endpoint: str,
required_lock_per_hotkey: float,
as_table: bool,
) -> None:
"""List all revealed commitments with each coldkey's locked stake and sufficiency."""
# Ask user for round number interactively
round_number: int = click.prompt("Enter round number", type=int)
logger.info(f"Listing commitments for round {round_number}")
# Case of the next round while current round is in progress should be handled here too.
try:
state = _fetch_state()
current_round = state.current_round
if round_number > current_round + 1:
click.echo(
json.dumps(
{
"success": False,
"error": f"Round {round_number} is not yet revealed. Next round is {current_round + 1}.",
}
)
)
raise SystemExit(1)
except Exception as e:
logger.error(f"Failed to fetch state: {e}")
click.echo(
json.dumps({"success": False, "error": f"Failed to fetch state: {str(e)}"})
)
raise SystemExit(1)
# Fetch schedule for the round.
# If the round is the next round while current round is in progress, fetch the schedule for the current round.
try:
round_to_fetch = (
round_number if round_number <= current_round else current_round
)
schedule = _fetch_schedule(round_to_fetch)
except Exception as e:
logger.error(f"Failed to fetch schedule: {e}")
click.echo(
json.dumps(
{"success": False, "error": f"Failed to fetch schedule: {str(e)}"}
)
)
raise SystemExit(1)
async def _list(
round_number: int, schedule: Schedule, current_round: int
) -> list[dict]:
import bittensor as bt # Bittensor import should be here because bittensor captures command line args for click otherwise
from collections import Counter
async with bt.AsyncSubtensor(subtensor_endpoint) as subtensor:
commitments = await _get_all_revealed_commitments(subtensor, netuid)
commitments_dict = _parse_commitments(
commitments, round_number, schedule, current_round
)
# Resolve the coldkey owner of each submitting hotkey.
hotkeys = list(commitments_dict)
owner_results = await asyncio.gather(
*(subtensor.get_hotkey_owner(hk) for hk in hotkeys),
return_exceptions=True,
)
owners = {
hk: (ck if isinstance(ck, str) else None)
for hk, ck in zip(hotkeys, owner_results)
}
# The lock requirement scales with the number of a coldkey's submitting hotkeys.
coldkey_counts = Counter(ck for ck in owners.values() if ck)
unique_coldkeys = list(coldkey_counts)
lock_results = await asyncio.gather(
*(
subtensor.get_coldkey_lock(coldkey_ss58=ck, netuid=netuid)
for ck in unique_coldkeys
),
return_exceptions=True,
)
locked_by_coldkey = {
ck: (float(lock["locked_mass"].tao) if isinstance(lock, dict) else 0.0)
for ck, lock in zip(unique_coldkeys, lock_results)
}
for entry in commitments_dict.values():
ck = owners.get(entry["hotkey"])
entry["coldkey"] = ck
if ck is None:
# Could not resolve the owner (e.g. deregistered hotkey).
entry["locked_rho"] = None
entry["required_rho"] = None
entry["lock_sufficient"] = None
else:
locked_rho = locked_by_coldkey.get(ck, 0.0)
required_rho = coldkey_counts[ck] * required_lock_per_hotkey
entry["locked_rho"] = locked_rho
entry["required_rho"] = required_rho
entry["lock_sufficient"] = locked_rho >= required_rho
results_list = list(commitments_dict.values())
results_list.sort(key=lambda x: x["commit_block"])
return results_list
results = asyncio.run(_list(round_number, schedule, current_round))
if as_table:
_render_commitments_table(results, round_number)
else:
for entry in results:
click.echo(json.dumps(entry))
def _parse_commitments(
commitments: dict, round_number: int, schedule: Schedule, current_round: int
) -> dict[str, dict]:
"""Extract latest commit and repo for each hotkey, sorted by commit block."""
results: dict[str, dict] = {}
for hotkey, entries in commitments.items():
latest_commit: tuple[int, str] | None = None
latest_repo: tuple[int, str] | None = None
latest_cdn_url: tuple[int, str] | None = None
for block, data in entries:
if (
round_number == current_round + 1
and block <= schedule.latest_reveal_block
):
continue
if round_number <= current_round and (
block < schedule.earliest_reveal_block
or block > schedule.latest_reveal_block
):
continue
try:
parsed = json.loads(data)
except json.JSONDecodeError:
continue
if commit_hash := parsed.get("commit"):
if latest_commit is None or block > latest_commit[0]:
latest_commit = (block, commit_hash)
if repo := parsed.get("repo"):
if latest_repo is None or block > latest_repo[0]:
latest_repo = (block, repo)
if cdn_url := parsed.get("cdn_url"):
if latest_cdn_url is None or block > latest_cdn_url[0]:
latest_cdn_url = (block, cdn_url)
if latest_commit is None:
continue
results[hotkey] = {
"hotkey": hotkey,
"commit_hash": latest_commit[1],
"commit_block": latest_commit[0],
"repo": latest_repo[1] if latest_repo else None,
"repo_block": latest_repo[0] if latest_repo else None,
"cdn_url": latest_cdn_url[1] if latest_cdn_url else None,
"cdn_block": latest_cdn_url[0] if latest_cdn_url else None,
}
return results
@cli.command("start-generator")
@click.option("--image-url", required=True, help="URL of the generator image to start")
@click.option("--targon-api-key", required=True, help="Targon API key")
def start_generator_cmd(image_url: str, targon_api_key: str) -> None:
"""Start the generator container."""
click.echo(f"Starting generator: {image_url}", err=True)
try:
container_url = asyncio.run(
_create_container(
image_url=image_url,
container_name=_GENERATOR_POD_NAME,
targon_api_key=targon_api_key,
resource_name=_GENERATOR_RESOURCE_NAME,
port=_GENERATOR_PORT,
health_check_path=_GENERATOR_HEALTH_CHECK_PATH,
echo=lambda msg: click.echo(msg, err=True),
)
)
click.echo(json.dumps({"success": True, "container_url": container_url}))
except KeyboardInterrupt:
logger.warning("Generator start interrupted by user")
click.echo(json.dumps({"success": False, "error": "Interrupted by user"}))
raise SystemExit(130) # Standard exit code for SIGINT
except Exception as e:
logger.error(f"Generator start failed: {e}")
click.echo(json.dumps({"success": False, "error": str(e)}))
raise SystemExit(1)
@cli.command("start-renderer")
@click.option("--targon-api-key", required=True, help="Targon API key")
def start_renderer_cmd(targon_api_key: str) -> None:
"""Start the renderer container."""
click.echo(f"Starting renderer: {_RENDER_IMAGE_URL}", err=True)
try:
container_url = asyncio.run(
_create_container(
image_url=_RENDER_IMAGE_URL,
container_name=_RENDER_POD_NAME,
targon_api_key=targon_api_key,
resource_name=_RENDER_RESOURCE_NAME,
port=_RENDER_PORT,
health_check_path=_RENDER_HEALTH_CHECK_PATH,
echo=lambda msg: click.echo(msg, err=True),
)
)
click.echo(json.dumps({"success": True, "container_url": container_url}))
except KeyboardInterrupt:
logger.warning("Renderer start interrupted by user")
click.echo(json.dumps({"success": False, "error": "Interrupted by user"}))
raise SystemExit(130) # Standard exit code for SIGINT
except Exception as e:
logger.error(f"Renderer start failed: {e}")
click.echo(json.dumps({"success": False, "error": str(e)}))
raise SystemExit(1)
@cli.command("render")
@click.option(
"--data-dir",
required=True,
help="Path to the directory containing .js submission files to render",
)
@click.option("--endpoint", required=True, help="Renderer endpoint URL.")
@click.option(
"--output-dir",
default="results",
help="Path to the directory where the rendered images will be saved.",
)
def render_cmd(data_dir: str, endpoint: str, output_dir: str) -> None:
"""Render .js submission files using the renderer endpoint."""
click.echo(f"Rendering {data_dir} with endpoint {endpoint}", err=True)
try:
renderer = Renderer(
data_dir=data_dir,
endpoint=endpoint,
output_dir=output_dir,
)
asyncio.run(renderer.render())
click.echo(json.dumps({"success": True, "output_dir": output_dir}))
except KeyboardInterrupt:
logger.warning("Renderer interrupted by user")
click.echo(json.dumps({"success": False, "error": "Interrupted by user"}))
@cli.command("start-judge")
@click.option("--targon-api-key", required=True, help="Targon API key.")
def start_judge_cmd(targon_api_key: str) -> None:
"""Start the judge VLLM container."""
click.echo(f"Starting judge: {_JUDGE_IMAGE_URL}", err=True)
try:
container_url = asyncio.run(
_create_container(
image_url=_JUDGE_IMAGE_URL,
container_name=_JUDGE_POD_NAME,
targon_api_key=targon_api_key,
resource_name=_JUDGE_RESOURCE_NAME,
port=_JUDGE_PORT,
health_check_path=_JUDGE_HEALTH_CHECK_PATH,
echo=lambda msg: click.echo(msg, err=True),
args=_JUDGE_ARGS,
)
)
click.echo(json.dumps({"success": True, "container_url": container_url}))
except KeyboardInterrupt:
logger.warning("Judge start interrupted by user")
click.echo(json.dumps({"success": False, "error": "Interrupted by user"}))
raise SystemExit(130)
except Exception as e:
logger.error(f"Judge start failed: {e}")
click.echo(json.dumps({"success": False, "error": str(e)}))
raise SystemExit(1)
@cli.command("judge")
@click.option(
"--prompts-json",
required=True,
help="Path to prompts JSON with prompts[].stem and prompts[].image_url.",
)
@click.option(
"--image-dir-1",
required=True,
help="Directory containing first rendered image set, grouped by stem.",
)
@click.option(
"--image-dir-2",
required=True,
help="Directory containing second rendered image set, grouped by stem.",
)
@click.option(
"--endpoint",
required=True,
help="Judge endpoint URL. /v1 is appended when omitted.",
)
@click.option("--seed", required=True, help="Seed for deterministic VLM calls.")
@click.option(
"--output-dir",
default="judge-results",
show_default=True,
help="Folder for per-duel JSON and duels.json.",
)
@click.option(
"--concurrency",
type=int,
default=1,
show_default=True,
help="Maximum number of duels judged concurrently.",
)
def judge_cmd(
prompts_json: str,
image_dir_1: str,
image_dir_2: str,
endpoint: str,
seed: str,
output_dir: str,
concurrency: int,
) -> None:
"""Judge two rendered image sets with the multi-stage duel pipeline."""
click.echo(f"Judging {prompts_json} with endpoint {endpoint}", err=True)
try:
judge = Judge(
endpoint=endpoint,
seed=int(seed),
output_dir=Path(output_dir),
concurrency=concurrency,
echo=lambda msg: click.echo(msg, err=True),
)
asyncio.run(
judge.judge(Path(prompts_json), Path(image_dir_1), Path(image_dir_2))
)
click.echo(json.dumps({"success": True, "output_dir": output_dir}))
except KeyboardInterrupt:
logger.warning("Judge interrupted by user")
click.echo(json.dumps({"success": False, "error": "Interrupted by user"}))
raise SystemExit(130)
except Exception as e:
logger.error(f"Judge failed: {e}")
click.echo(json.dumps({"success": False, "error": str(e)}))
raise SystemExit(1)
@cli.command("stop-pods")
@click.option("--targon-api-key", required=True, help="Targon API key.")
def stop_pods_cmd(targon_api_key: str) -> None:
"""Stop the generator, render, and judge pods."""
click.echo("Stopping pods...", err=True)