Repository navigation
Expand file tree
/
Copy pathagent_loop.py
More file actions
executable file
·909 lines (825 loc) · 36.3 KB
/
Copy pathagent_loop.py
File metadata and controls
executable file
·909 lines (825 loc) · 36.3 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
#!/usr/bin/env python3
"""Architecture 2.0: Autonomous Closed-Loop AI-Native Design Engine.
==================================================================
Demonstrates an autonomous AI agent operating in closed physical loops:
- Loop A: SCALE-Sim Systolic Array Search & Memory Wall Roofline
- Loop B: Yosys + SkyWater 130nm RTL Synthesis & Timing Closure
- Loop C: 2D RUDY Macro Placement, Wirelength & Routing Congestion
- Loop D: RISC-V Instruction Specialization & HW/SW Co-Design
"""
from __future__ import annotations
import argparse
from dataclasses import asdict, dataclass, field
import json
import os
from pathlib import Path
import shutil
import subprocess
import sys
import time
from typing import Any, Dict, List, Optional
try:
from rich import box
from rich.console import Console
from rich.panel import Panel
from rich.progress import BarColumn, Progress, TextColumn
from rich.syntax import Syntax
from rich.table import Table
from rich.text import Text
RICH_AVAILABLE = True
except ImportError:
RICH_AVAILABLE = False
ROOT = Path(__file__).resolve().parent
REPO_ROOT = ROOT.parent
if str(REPO_ROOT) not in sys.path:
sys.path.insert(0, str(REPO_ROOT))
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
LABS_DIR = ROOT
# Import our physical referees and model drivers
try:
from labs.referees import (
BaseReferee,
PhysicalReceipt,
MicroarchitecturalReferee,
RTLVerificationReferee,
FloorplanReferee,
CodesignReferee,
)
from labs.model_drivers import (
BaseModelDriver,
TurnProposal,
create_model_driver,
list_available_models,
)
except ImportError:
from referees import (
BaseReferee,
PhysicalReceipt,
MicroarchitecturalReferee,
RTLVerificationReferee,
FloorplanReferee,
CodesignReferee,
)
from model_drivers import (
BaseModelDriver,
TurnProposal,
create_model_driver,
list_available_models,
)
# Backward compatibility alias
PhysicalVerificationReferee = RTLVerificationReferee
@dataclass
class AgentTurn:
"""One complete turn in the closed-loop optimization trajectory."""
turn_number: int
paradigm_label: str
intent_summary: str
agent_hypothesis: str
proposed_action: str
code_diff_summary: str
receipt: PhysicalReceipt
agent_reflection: str
candidate_verilog: str = ""
candidate_data: Dict[str, Any] = field(default_factory=dict)
class SiliconDesignAgentLoop:
"""Orchestrates autonomous closed-loop architectural search against physical tools."""
def __init__(
self,
console: Optional[Console] = None,
model: str = "reference",
max_turns: int = 3,
pace: float = 0.0,
interactive: bool = False,
width: int = 88,
loop: str = "b",
):
self.console = console or Console(width=width)
self.model_name = model
self.max_turns = max_turns
self.pace = pace
self.interactive = interactive
self.width = width
self.loop = loop.lower().strip()
self.history: List[AgentTurn] = []
self.driver: BaseModelDriver = create_model_driver(model)
# Referees
self.referee_a = MicroarchitecturalReferee()
self.referee_b = RTLVerificationReferee(clock_period_ns=2.000)
self.referee_c = FloorplanReferee()
self.referee_d = CodesignReferee()
def _wait_step(
self, prompt: str = "Press [Enter] to execute next agent reasoning turn..."
) -> None:
"""Pacing delay or interactive user gate."""
if self.interactive:
try:
input(f"\n [Interactive Gate] {prompt} ")
except (EOFError, KeyboardInterrupt):
self.console.print(
"\n[yellow]Interactive step cancelled by user.[/yellow]"
)
elif self.pace > 0:
time.sleep(self.pace)
def run(self) -> List[AgentTurn]:
"""Dispatches closed-loop execution to the requested target micro-loop."""
if self.loop in ("a", "01", "microarchitectural-sweep"):
return self.run_microarchitectural_loop()
elif self.loop in ("c", "03", "physical-floorplan"):
return self.run_physical_floorplan_loop()
elif self.loop in ("d", "04", "hw-sw-codesign"):
return self.run_codesign_loop()
elif self.loop in ("all", "*"):
return self.run_all_loops()
else:
return self.run_hero_timing_loop()
# =========================================================================
# Loop A: Systolic Microarchitecture & Memory Wall (SCALE-Sim)
# =========================================================================
def run_microarchitectural_loop(self) -> List[AgentTurn]:
"""Runs the closed-loop Systolic Array Microarchitecture Search against SCALE-Sim."""
self.history = []
self.console.print()
self.console.print(
Panel(
f"[bold white on blue] ARCHITECTURE 2.0: AUTONOMOUS CLOSED-LOOP AGENT ENGINE [/bold white on blue]\n"
f"[bold cyan]Problem: Systolic Microarchitectural Search under the Off-Chip Memory Wall[/bold cyan]\n"
f"[bold white]Active Brain:[/bold white] [green]{self.driver.model_name}[/green] | "
f"[dim]Workload: Mobile XR GEMM | PE Budget <= 1024 | Interface BW <= 4 words/cyc | Target <= 50k cyc[/dim]",
box=box.ROUNDED,
border_style="bright_blue",
width=self.width,
)
)
last_receipt: Optional[PhysicalReceipt] = None
for turn_idx in range(1, self.max_turns + 1):
self._wait_step(f"Press [Enter] for Loop A Turn {turn_idx}...")
proposal: TurnProposal = self.driver.propose_turn(
turn_number=turn_idx,
spec={
"max_pe_budget": 1024,
"max_bandwidth": 4,
"target_cycles": 50000,
},
history=self.history,
last_receipt=last_receipt,
loop_domain="a",
)
self.console.print(
Panel(
f"[bold cyan]🤖 Agent Turn {turn_idx}: {proposal.paradigm_label}[/bold cyan]\n"
f"[bold white]Hypothesis:[/bold white] {proposal.hypothesis}\n"
f"[dim]Action Proposed:[/dim] [yellow]{proposal.proposed_action}[/yellow]",
box=box.ROUNDED,
border_style="cyan" if turn_idx != 3 else "green",
width=self.width,
)
)
# Evaluate with physical referee using real SCALE-Sim simulation
cand_dict = proposal.arch_params or {
"rows": 32,
"cols": 32,
"dataflow": "output_stationary",
}
receipt = self.referee_a.evaluate(
candidate=cand_dict,
iteration=turn_idx,
paradigm=proposal.paradigm_label,
headline=f"SCALE-Sim Cycle-Accurate Evaluation (Turn {turn_idx})",
)
last_receipt = receipt
turn = AgentTurn(
turn_number=turn_idx,
paradigm_label=proposal.paradigm_label,
intent_summary=f"Turn {turn_idx} systolic geometry search",
agent_hypothesis=proposal.hypothesis,
proposed_action=proposal.proposed_action,
code_diff_summary=proposal.code_diff_summary,
receipt=receipt,
agent_reflection=proposal.reflection,
candidate_data=cand_dict,
)
self.history.append(turn)
self._print_turn_card(turn)
if receipt.status == "PASS":
self.console.print(
Panel(
f"[bold white on green] CONVERGENCE ACHIEVED IN TURN {turn_idx} [/bold white on green]\n"
f"Weight Stationary redesign and on-chip filter buffer closed physical signoff ({receipt.achieved_value:,.0f} cycles < {receipt.limit_value:,.0f})!",
box=box.ROUNDED,
border_style="green",
width=self.width,
)
)
break
self._print_agent_trajectory_summary("a")
self._save_history("Loop A: Systolic Microarchitecture & Memory Wall")
return self.history
# =========================================================================
# Loop B: RTL Synthesis, SkyWater 130nm Timing Closure (Hero Loop)
# =========================================================================
def run_hero_timing_loop(self) -> List[AgentTurn]:
"""Runs the closed-loop Timing Closure Loop (Loop B: 500 MHz Accumulator in SKY130)."""
self.history = []
self.console.print()
self.console.print(
Panel(
f"[bold white on blue] ARCHITECTURE 2.0: AUTONOMOUS CLOSED-LOOP AGENT ENGINE [/bold white on blue]\n"
f"[bold cyan]Problem Specification: 500 MHz PE Accumulator Timing Closure in SKY130 130nm[/bold cyan]\n"
f"[bold white]Active Model Driver Brain:[/bold white] [green]{self.driver.model_name}[/green] | "
f"[dim]Target: T_clk = 2.000 ns (500 MHz) | Bitwidth: 32-bit | Area <= 400 Cells | Vector Equivalence: 100%[/dim]",
box=box.ROUNDED,
border_style="bright_blue",
width=self.width,
)
)
last_receipt: Optional[PhysicalReceipt] = None
for turn_idx in range(1, self.max_turns + 1):
self._wait_step(f"Press [Enter] for Loop B Turn {turn_idx}...")
proposal: TurnProposal = self.driver.propose_turn(
turn_number=turn_idx,
spec={"clock_period_ns": 2.000, "technology": "SKY130", "bitwidth": 32},
history=self.history,
last_receipt=last_receipt,
loop_domain="b",
)
border = (
"green" if turn_idx == 3 else ("yellow" if turn_idx == 2 else "cyan")
)
self.console.print(
Panel(
f"[bold]🤖 Agent Turn {turn_idx}: {proposal.paradigm_label}[/bold]\n"
f"[bold white]Hypothesis:[/bold white] {proposal.hypothesis}\n"
f"[dim]Proposed Action:[/dim] [yellow]{proposal.proposed_action}[/yellow]",
box=box.ROUNDED,
border_style=border,
width=self.width,
)
)
# Evaluate with physical referee (Yosys + SKY130 + iverilog)
verilog_src = proposal.verilog_code or ""
receipt = self.referee_b.evaluate_verilog_code(
candidate_verilog=verilog_src,
candidate_module_name="pe_accumulator_candidate",
iteration=turn_idx,
paradigm=proposal.paradigm_label,
headline=f"RTL Synthesis & Equivalence Verification (Turn {turn_idx})",
)
last_receipt = receipt
turn = AgentTurn(
turn_number=turn_idx,
paradigm_label=proposal.paradigm_label,
intent_summary=f"Turn {turn_idx} RTL timing closure",
agent_hypothesis=proposal.hypothesis,
proposed_action=proposal.proposed_action,
code_diff_summary=proposal.code_diff_summary,
receipt=receipt,
agent_reflection=proposal.reflection,
candidate_verilog=verilog_src,
)
self.history.append(turn)
self._print_turn_card(turn)
if receipt.status == "PASS" and receipt.slack >= 0.0:
self.console.print(
Panel(
f"[bold white on green] CONVERGENCE ACHIEVED IN TURN {turn_idx} [/bold white on green]\n"
f"Model [bold]{self.driver.model_name}[/bold] successfully closed timing and passed 1,000-vector bit-exact equivalence!",
box=box.ROUNDED,
border_style="green",
width=self.width,
)
)
break
self._print_agent_trajectory_summary("b")
self._save_history("Loop B: 500 MHz PE Accumulator Timing Closure in SKY130")
return self.history
# =========================================================================
# Loop C: Physical Macro Placement & 2D RUDY Routing Congestion
# =========================================================================
def run_physical_floorplan_loop(self) -> List[AgentTurn]:
"""Runs the closed-loop Physical Floorplan Macro Placement against 2D RUDY Routing."""
self.history = []
self.console.print()
self.console.print(
Panel(
f"[bold white on blue] ARCHITECTURE 2.0: AUTONOMOUS CLOSED-LOOP AGENT ENGINE [/bold white on blue]\n"
f"[bold cyan]Problem: Physical Macro Placement, Geometric DRC & 2D RUDY Routing Congestion[/bold cyan]\n"
f"[bold white]Active Brain:[/bold white] [green]{self.driver.model_name}[/green] | "
f"[dim]Die: 1000x1000 µm | Max HPWL <= 10,000 µm | Peak Congestion <= 85.0% | DRC == 0[/dim]",
box=box.ROUNDED,
border_style="bright_blue",
width=self.width,
)
)
last_receipt: Optional[PhysicalReceipt] = None
for turn_idx in range(1, self.max_turns + 1):
self._wait_step(f"Press [Enter] for Loop C Turn {turn_idx}...")
proposal: TurnProposal = self.driver.propose_turn(
turn_number=turn_idx,
spec={
"die_width_um": 1000.0,
"max_congestion": 85.0,
"max_hpwl": 10000.0,
},
history=self.history,
last_receipt=last_receipt,
loop_domain="c",
)
border = (
"green" if turn_idx == 3 else ("yellow" if turn_idx == 2 else "cyan")
)
self.console.print(
Panel(
f"[bold]🤖 Agent Turn {turn_idx}: {proposal.paradigm_label}[/bold]\n"
f"[bold white]Hypothesis:[/bold white] {proposal.hypothesis}\n"
f"[dim]Action Proposed:[/dim] [yellow]{proposal.proposed_action}[/yellow]",
box=box.ROUNDED,
border_style=border,
width=self.width,
)
)
cand_layout = proposal.floorplan_layout or {}
receipt = self.referee_c.evaluate(
candidate=cand_layout,
iteration=turn_idx,
paradigm=proposal.paradigm_label,
headline=f"2D RUDY Congestion & DRC Evaluation (Turn {turn_idx})",
)
last_receipt = receipt
turn = AgentTurn(
turn_number=turn_idx,
paradigm_label=proposal.paradigm_label,
intent_summary=f"Turn {turn_idx} floorplan placement",
agent_hypothesis=proposal.hypothesis,
proposed_action=proposal.proposed_action,
code_diff_summary=proposal.code_diff_summary,
receipt=receipt,
agent_reflection=proposal.reflection,
candidate_data=cand_layout,
)
self.history.append(turn)
self._print_turn_card(turn)
if receipt.status == "PASS":
self.console.print(
Panel(
f"[bold white on green] CONVERGENCE ACHIEVED IN TURN {turn_idx} [/bold white on green]\n"
f"Multi-objective floorplan achieved 0 DRC violations and {receipt.achieved_value:.1f}% peak routing congestion!",
box=box.ROUNDED,
border_style="green",
width=self.width,
)
)
break
self._print_agent_trajectory_summary("c")
self._save_history("Loop C: Physical Macro Placement & Routing Congestion")
return self.history
# =========================================================================
# Loop D: Hardware/Software Co-Design & Instruction Specialization
# =========================================================================
def run_codesign_loop(self) -> List[AgentTurn]:
"""Runs the closed-loop HW/SW Co-Design against RISC-V Profiling & Synthesis."""
self.history = []
self.console.print()
self.console.print(
Panel(
f"[bold white on blue] ARCHITECTURE 2.0: AUTONOMOUS CLOSED-LOOP AGENT ENGINE [/bold white on blue]\n"
f"[bold cyan]Problem: Hardware-Software Co-Design & Instruction Specialization (XR Filtering)[/bold cyan]\n"
f"[bold white]Active Brain:[/bold white] [green]{self.driver.model_name}[/green] | "
f"[dim]Kernel: 256x256 Image Filter | Cycle Budget <= 50,000 | Area Budget <= 15,000 GE[/dim]",
box=box.ROUNDED,
border_style="bright_blue",
width=self.width,
)
)
last_receipt: Optional[PhysicalReceipt] = None
for turn_idx in range(1, self.max_turns + 1):
self._wait_step(f"Press [Enter] for Loop D Turn {turn_idx}...")
proposal: TurnProposal = self.driver.propose_turn(
turn_number=turn_idx,
spec={"target_cycles": 50000, "max_area_ge": 15000},
history=self.history,
last_receipt=last_receipt,
loop_domain="d",
)
border = (
"green" if turn_idx == 3 else ("yellow" if turn_idx == 2 else "cyan")
)
self.console.print(
Panel(
f"[bold]🤖 Agent Turn {turn_idx}: {proposal.paradigm_label}[/bold]\n"
f"[bold white]Hypothesis:[/bold white] {proposal.hypothesis}\n"
f"[dim]Action Proposed:[/dim] [yellow]{proposal.proposed_action}[/yellow]",
box=box.ROUNDED,
border_style=border,
width=self.width,
)
)
cand_spec = proposal.codesign_spec or {}
receipt = self.referee_d.evaluate(
candidate=cand_spec,
iteration=turn_idx,
paradigm=proposal.paradigm_label,
headline=f"RISC-V Profiling & Area Evaluation (Turn {turn_idx})",
)
last_receipt = receipt
turn = AgentTurn(
turn_number=turn_idx,
paradigm_label=proposal.paradigm_label,
intent_summary=f"Turn {turn_idx} HW/SW co-design",
agent_hypothesis=proposal.hypothesis,
proposed_action=proposal.proposed_action,
code_diff_summary=proposal.code_diff_summary,
receipt=receipt,
agent_reflection=proposal.reflection,
candidate_data=cand_spec,
)
self.history.append(turn)
self._print_turn_card(turn)
if receipt.status == "PASS":
self.console.print(
Panel(
f"[bold white on green] CONVERGENCE ACHIEVED IN TURN {turn_idx} [/bold white on green]\n"
f"Co-designed SIMD-4 and post-increment addressing met frame budget ({receipt.achieved_value:,.0f} cycles < {receipt.limit_value:,.0f}) within silicon area limits!",
box=box.ROUNDED,
border_style="green",
width=self.width,
)
)
break
self._print_agent_trajectory_summary("d")
self._save_history(
"Loop D: Hardware/Software Co-Design & Instruction Specialization"
)
return self.history
def run_all_loops(self) -> List[AgentTurn]:
"""Executes the closed-loop agent optimizer across all 4 micro-loops sequentially."""
all_turns = []
for l in ("a", "b", "c", "d"):
self.loop = l
turns = self.run()
all_turns.extend(turns)
return all_turns
def _print_turn_card(self, turn: AgentTurn) -> None:
"""Renders an agent reasoning card with real tool receipts."""
is_pass = turn.receipt.status == "PASS"
border_color = (
"green" if is_pass else ("yellow" if turn.turn_number == 2 else "red")
)
status_badge = (
"[bold white on green] PASS: SIGNED OFF [/bold white on green]"
if is_pass
else "[bold white on red] FAIL: REJECTED [/bold white on red]"
)
t = Table(box=box.SIMPLE, show_header=False, width=self.width - 4)
t.add_column("Key", style="bold cyan", width=22)
t.add_column("Val", style="white")
t.add_row("Paradigm Stage", turn.paradigm_label)
t.add_row("Agent Hypothesis", turn.agent_hypothesis)
t.add_row("Proposed Action", turn.proposed_action)
t.add_row("Representation Change", f"[yellow]{turn.code_diff_summary}[/yellow]")
t.add_row("Tool Provenance", turn.receipt.tool_provenance)
t.add_row(
"Verification Gate",
f"[bold green]{turn.receipt.verification_status}[/bold green]"
if "PASS" in turn.receipt.verification_status
else f"[bold red]{turn.receipt.verification_status}[/bold red]",
)
# Domain-specific metric formatting
if turn.receipt.loop == "A":
t.add_row(
"Execution Latency",
f"[{border_color}]{turn.receipt.achieved_value:,.0f} {turn.receipt.unit} (Limit: {turn.receipt.limit_value:,.0f})[/{border_color}]",
)
if "dram_traffic" in turn.receipt.metadata:
t.add_row(
"Off-Chip DRAM Traffic",
f"{turn.receipt.metadata['dram_traffic']:,} words",
)
if "utilization_pct" in turn.receipt.metadata:
t.add_row(
"Array Utilization",
f"{turn.receipt.metadata['utilization_pct']:.2f}%",
)
elif turn.receipt.loop == "C":
t.add_row(
"Peak RUDY Congestion",
f"[{border_color}]{turn.receipt.achieved_value:.1f}% (Limit: <= {turn.receipt.limit_value:.1f}%)[/{border_color}]",
)
if "hpwl_um" in turn.receipt.metadata:
t.add_row(
"Total Wirelength (HPWL)",
f"{turn.receipt.metadata['hpwl_um']:,.1f} µm",
)
if "drc_violations" in turn.receipt.metadata:
drc_s = (
"green" if turn.receipt.metadata["drc_violations"] == 0 else "red"
)
t.add_row(
"DRC Violations",
f"[{drc_s}]{turn.receipt.metadata['drc_violations']} violations[/{drc_s}]",
)
elif turn.receipt.loop == "D":
t.add_row(
"Execution Latency",
f"[{border_color}]{turn.receipt.achieved_value:,.0f} {turn.receipt.unit} (Budget: {turn.receipt.limit_value:,.0f})[/{border_color}]",
)
if "area_ge" in turn.receipt.metadata:
t.add_row(
"Silicon Area",
f"{turn.receipt.metadata['area_ge']:,} GE (Budget: <= 15,000 GE)",
)
else: # Loop B
t.add_row(
"Timing Slack (WNS)",
f"[{border_color}]{turn.receipt.slack:+.3f} {turn.receipt.unit} ({turn.receipt.status})[/{border_color}]",
)
if turn.receipt.logic_depth > 0:
t.add_row(
"Critical Path Depth", f"{turn.receipt.logic_depth} logic stages"
)
diag_text = "\n".join(f" • {d}" for d in turn.receipt.diagnostics)
t.add_row("Tool Diagnostics", diag_text)
t.add_row(
"Agent Reflection", f"[italic dim]{turn.agent_reflection}[/italic dim]"
)
self.console.print(
Panel(
t,
title=f"[bold]Turn {turn.turn_number}: {turn.paradigm_label} | {status_badge}[/bold]",
border_style=border_color,
box=box.ROUNDED,
width=self.width,
)
)
self.console.print()
def _print_agent_trajectory_summary(self, loop_domain: str = "b") -> None:
"""Renders the convergence trajectory table customized to the target micro-loop."""
self.console.print(
Panel(
"[bold white on blue] CLOSED-LOOP OPTIMIZATION TRAJECTORY CONVERGENCE [/bold white on blue]\n"
"[bold cyan]Observing Why Single-Layer Optimizers Plateau and AI-Native Co-Adaptation Succeeds[/bold cyan]",
box=box.ROUNDED,
border_style="bright_blue",
width=self.width,
)
)
table = Table(
box=box.ROUNDED,
width=self.width,
header_style="bold cyan",
title=f"Agent Trajectory ({self.driver.model_name}) | Loop {loop_domain.upper()}",
show_lines=True,
)
loop_norm = loop_domain.lower().strip()
if loop_norm in ("a", "01", "microarchitectural-sweep"):
table.add_column("Turn", justify="center", style="bold white", width=6)
table.add_column("Paradigm", justify="left", style="bold white", width=20)
table.add_column("Geometry", justify="center", width=12)
table.add_column("Dataflow", justify="center", width=12)
table.add_column("Cycles", justify="center", width=14)
table.add_column("DRAM Words", justify="center", width=14)
table.add_column("Signoff", justify="center", width=12)
for t in self.history:
is_pass = t.receipt.status == "PASS"
st_col = "green" if is_pass else "red"
signoff = (
"[bold green]SIGNED OFF[/bold green]"
if is_pass
else "[red]FAIL[/red]"
)
geom = f"{t.candidate_data.get('rows', '?')}x{t.candidate_data.get('cols', '?')}"
df = "WS" if "weight" in t.candidate_data.get("dataflow", "") else "OS"
cyc = f"{t.receipt.achieved_value:,.0f}"
dram = f"{t.receipt.metadata.get('dram_traffic', 0):,}"
table.add_row(
str(t.turn_number),
t.paradigm_label[:18],
geom,
df,
f"[{st_col}]{cyc}[/{st_col}]",
dram,
signoff,
)
elif loop_norm in ("c", "03", "physical-floorplan"):
table.add_column("Turn", justify="center", style="bold white", width=6)
table.add_column("Paradigm", justify="left", style="bold white", width=22)
table.add_column("Policy", justify="left", width=16)
table.add_column("HPWL (µm)", justify="center", width=14)
table.add_column("Peak Congest", justify="center", width=14)
table.add_column("DRCs", justify="center", width=10)
table.add_column("Signoff", justify="center", width=12)
for t in self.history:
is_pass = t.receipt.status == "PASS"
st_col = "green" if is_pass else "red"
signoff = (
"[bold green]SIGNED OFF[/bold green]"
if is_pass
else "[red]FAIL[/red]"
)
hpwl = f"{t.receipt.metadata.get('hpwl_um', 0):,.1f}"
cong = f"{t.receipt.achieved_value:.1f}%"
drc = str(t.receipt.metadata.get("drc_violations", 0))
policy = (
"Direct Draft"
if t.turn_number == 1
else ("HPWL Min" if t.turn_number == 2 else "Pin/Channel")
)
table.add_row(
str(t.turn_number),
t.paradigm_label[:20],
policy,
hpwl,
f"[{st_col}]{cong}[/{st_col}]",
drc,
signoff,
)
elif loop_norm in ("d", "04", "hw-sw-codesign"):
table.add_column("Turn", justify="center", style="bold white", width=6)
table.add_column("Paradigm", justify="left", style="bold white", width=22)
table.add_column("Specialization", justify="left", width=18)
table.add_column("Latency", justify="center", width=14)
table.add_column("Area (GE)", justify="center", width=12)
table.add_column("Speedup", justify="center", width=10)
table.add_column("Signoff", justify="center", width=12)
for t in self.history:
is_pass = t.receipt.status == "PASS"
st_col = "green" if is_pass else "red"
signoff = (
"[bold green]SIGNED OFF[/bold green]"
if is_pass
else "[red]FAIL[/red]"
)
spec = (
"Scalar dot"
if t.turn_number == 1
else ("Unroll-8" if t.turn_number == 2 else "SIMD-4+PostInc")
)
cyc = f"{t.receipt.achieved_value:,.0f}"
area = f"{t.receipt.metadata.get('hardware_area_ge', t.receipt.metadata.get('area_ge', 0)):,}"
spd = (
"1.00x"
if t.turn_number == 1
else ("1.58x" if t.turn_number == 2 else "5.09x")
)
table.add_row(
str(t.turn_number),
t.paradigm_label[:20],
spec,
f"[{st_col}]{cyc}[/{st_col}]",
area,
spd,
signoff,
)
else: # Loop B
table.add_column("Turn", justify="center", style="bold white", width=6)
table.add_column("Paradigm", justify="left", style="bold white", width=22)
table.add_column("Logic Depth", justify="center", width=14)
table.add_column("WNS Slack", justify="center", width=16)
table.add_column("Equivalence", justify="center", width=16)
table.add_column("Physical Signoff", justify="center", width=16)
for t in self.history:
is_pass = t.receipt.status == "PASS"
slack_col = "green" if is_pass else "red"
signoff = (
"[bold green]SIGNED OFF[/bold green]"
if is_pass
else "[red]FAIL[/red]"
)
verif_badge = (
"[green]100% Bit-Exact[/green]"
if "PASS" in t.receipt.verification_status
else "[red]MISMATCH[/red]"
)
table.add_row(
str(t.turn_number),
t.paradigm_label[:20],
f"{t.receipt.logic_depth} stages",
f"[{slack_col}]{t.receipt.slack:+.3f} ns[/{slack_col}]",
verif_badge,
signoff,
)
self.console.print(table)
self.console.print(
Panel(
"[bold white]The AI-Native Systems Law Demonstrated:[/bold white]\n"
"• [bold red]Turn 1 (AI-Assisted)[/bold red]: Open-loop generation without physical feedback violates system invariants.\n"
"• [bold yellow]Turn 2 (AI-Driven)[/bold yellow]: Single-layer parameter tuning hits a fundamental physical plateau (memory wall, gate delay, or channel choke).\n"
"• [bold green]Turn 3 (AI-Native)[/bold green]: Cross-layer co-adaptation shifts the underlying representation (dataflow, arithmetic, pin geometry, or ISA semantics), closing multi-objective physical signoff with authentic verification proofs.",
box=box.ROUNDED,
border_style="green",
width=self.width,
)
)
self.console.print()
def _save_history(self, problem_title: str) -> None:
"""Saves trajectory history to disk as structured JSON."""
out_path = LABS_DIR / "agent_history.json"
data = {
"timestamp": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
"problem": problem_title,
"loop": self.loop,
"model_driver": self.driver.model_name,
"turns": [
{
"turn": t.turn_number,
"paradigm": t.paradigm_label,
"hypothesis": t.agent_hypothesis,
"action": t.proposed_action,
"verilog": t.candidate_verilog,
"candidate_data": t.candidate_data,
"receipt": t.receipt.to_dict()
if hasattr(t.receipt, "to_dict")
else asdict(t.receipt),
"reflection": t.agent_reflection,
}
for t in self.history
],
}
out_path.write_text(json.dumps(data, indent=2), encoding="utf-8")
def main() -> None:
parser = argparse.ArgumentParser(
description="Architecture 2.0: Autonomous Closed-Loop AI-Native Design Engine"
)
parser.add_argument(
"--model",
type=str,
default="reference",
help="Agent model brain: 'reference' (deterministic), 'gpt-4o', 'gemini-2.5-pro', or 'ollama/<model>'",
)
parser.add_argument(
"--list-models",
action="store_true",
help="List supported model drivers and API key availability",
)
parser.add_argument(
"--loop",
type=str,
default="b",
help="Target micro-loop: a (SCALE-Sim), b (Yosys RTL), c (2D RUDY floorplan), d (HW/SW codesign), or all (default: b)",
)
parser.add_argument(
"--hero",
action="store_true",
help="Run the Hero PE Accumulator Closed-Loop Agent Demonstration (Loop B)",
)
parser.add_argument(
"--step",
action="store_true",
help="Step through agent turns interactively with [Enter]",
)
parser.add_argument(
"--pace",
type=float,
default=0.0,
help="Pacing delay in seconds between agent reasoning turns (e.g. 1.5)",
)
parser.add_argument(
"--max-turns",
type=int,
default=3,
help="Maximum closed-loop turns (default: 3)",
)
parser.add_argument(
"--width",
type=int,
default=88,
help="Terminal column width (default: 88)",
)
args = parser.parse_args()
console = Console(width=args.width)
if args.list_models:
console.print(
Panel(
"[bold cyan]Architecture 2.0: Supported Closed-Loop Agent Model Backends[/bold cyan]",
box=box.ROUNDED,
width=args.width,
)
)
table = Table(box=box.ROUNDED, width=args.width)
table.add_column("Model ID", style="bold white", width=18)
table.add_column("Name", style="cyan", width=24)
table.add_column("Status", style="green", width=22)
table.add_column("Description", style="white")
for m in list_available_models():
st_color = "green" if "READY" in m["status"] else "yellow"
table.add_row(
m["id"],
m["name"],
f"[{st_color}]{m['status']}[/{st_color}]",
m["description"],
)
console.print(table)
return
target_loop = "b" if args.hero else args.loop
engine = SiliconDesignAgentLoop(
console=console,
model=args.model,
max_turns=args.max_turns,
pace=args.pace,
interactive=args.step,
width=args.width,
loop=target_loop,
)
engine.run()
if __name__ == "__main__":
main()