File size: 23,654 Bytes
5dc80b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Comprehensive Benchmarking Suite for HRM SRAM/DRAM Memory Tiering.

Measures and compares:
  - Per-module latency (L-level SRAM vs H-level DRAM)
  - End-to-end inference latency
  - Throughput (samples/sec)
  - Memory usage per tier (SRAM / DRAM)
  - Cross-tier transfer overhead
  - SRAM hit rate
  - Triton kernel-level profiling
  - GPU utilization / power draw (when available)
"""

import json
import time
import math
import os
from typing import Dict, List, Optional, Tuple
from dataclasses import dataclass, asdict

import torch
import torch.nn.functional as F

from models.memory_tier import MemoryTierManager
from models.hrm.hrm_tiered import HRM_Tiered, HRM_Tiered_Inner
from models.hrm.hrm_act_v1 import (
    HierarchicalReasoningModel_ACTV1,
    HierarchicalReasoningModel_ACTV1Config,
    HierarchicalReasoningModel_ACTV1InnerCarry,
)
from models.triton_kernels import triton_memory_latency_probe


@dataclass
class BenchmarkResult:
    """Results from a single benchmark run."""
    model_name: str
    batch_size: int
    seq_len: int
    hidden_size: int
    H_cycles: int
    L_cycles: int
    H_layers: int
    L_layers: int
    num_iterations: int
    warmup_iterations: int

    # Latency (μs)
    l_level_latency_mean_us: float
    l_level_latency_min_us: float
    l_level_latency_max_us: float
    l_level_latency_std_us: float

    h_level_latency_mean_us: float
    h_level_latency_min_us: float
    h_level_latency_max_us: float
    h_level_latency_std_us: float

    total_inference_latency_mean_ms: float
    total_inference_latency_min_ms: float
    total_inference_latency_max_ms: float
    total_inference_latency_std_ms: float

    # Throughput
    throughput_samples_per_sec: float

    # Memory (MB)
    sram_peak_mb: float
    dram_peak_mb: float
    total_gpu_memory_mb: float

    # Transfer metrics (μs)
    h_l_transfer_mean_us: float
    l_h_transfer_mean_us: float

    # SRAM metrics
    sram_hit_rate: float

    # Triton kernel metrics
    triton_sram_probe_latency_us: float
    triton_dram_probe_latency_us: float

    # GPU metrics
    gpu_utilization_pct: Optional[float]
    gpu_power_w: Optional[float]
    gpu_temperature_c: Optional[float]

    # Derived
    h_over_l_latency_ratio: float
    memory_efficiency: float  # useful_compute_time / total_time


def _std(values: List[float]) -> float:
    if len(values) < 2:
        return 0.0
    mean = sum(values) / len(values)
    var = sum((v - mean) ** 2 for v in values) / (len(values) - 1)
    return math.sqrt(var)


def _create_dummy_batch(
    batch_size: int,
    seq_len: int,
    vocab_size: int,
    device: torch.device,
) -> Dict[str, torch.Tensor]:
    """Create a synthetic batch for benchmarking."""
    return {
        "inputs": torch.randint(0, vocab_size, (batch_size, seq_len), device=device),
        "labels": torch.randint(0, vocab_size, (batch_size, seq_len), device=device),
        "puzzle_identifiers": torch.arange(batch_size, device=device),
    }


def _get_gpu_metrics() -> Dict[str, Optional[float]]:
    """Try to read GPU utilization, power, temperature via nvidia-smi."""
    metrics = {'utilization': None, 'power': None, 'temperature': None}
    try:
        import subprocess
        result = subprocess.run(
            ['nvidia-smi', '--query-gpu=utilization.gpu,power.draw,temperature.gpu',
             '--format=csv,noheader,nounits'],
            capture_output=True, text=True, timeout=5,
        )
        if result.returncode == 0:
            parts = result.stdout.strip().split(',')
            if len(parts) >= 3:
                metrics['utilization'] = float(parts[0].strip())
                metrics['power'] = float(parts[1].strip())
                metrics['temperature'] = float(parts[2].strip())
    except Exception:
        pass
    return metrics


def _run_triton_latency_probe(
    batch_size: int,
    hidden_size: int,
    device: torch.device,
    num_iters: int = 100,
) -> Tuple[float, float]:
    """Measure SRAM vs DRAM effective latency using Triton probe kernels.

    Returns (sram_latency_us, dram_latency_us).
    """
    if not torch.cuda.is_available():
        return 0.0, 0.0

    # Small tensor → fits in SRAM (L1/L2/registers)
    sram_data = torch.randn(batch_size, hidden_size, device=device, dtype=torch.float32)

    # Large tensor → forces DRAM access (much larger than L2)
    dram_size = max(hidden_size, 65536)  # Force spill to DRAM
    dram_data = torch.randn(batch_size * 64, dram_size, device=device, dtype=torch.float32)

    # Make a contiguous slice for DRAM probe
    dram_probe_data = dram_data[:batch_size, :hidden_size].contiguous()

    # Warmup
    triton_memory_latency_probe(sram_data, num_iters=10)
    triton_memory_latency_probe(dram_probe_data, num_iters=10)
    torch.cuda.synchronize()

    # SRAM probe
    start = torch.cuda.Event(enable_timing=True)
    end = torch.cuda.Event(enable_timing=True)
    start.record()
    triton_memory_latency_probe(sram_data, num_iters=num_iters)
    end.record()
    torch.cuda.synchronize()
    sram_us = start.elapsed_time(end) * 1000  # ms → μs

    # DRAM probe (access scattered to prevent caching)
    start2 = torch.cuda.Event(enable_timing=True)
    end2 = torch.cuda.Event(enable_timing=True)
    start2.record()
    triton_memory_latency_probe(dram_probe_data, num_iters=num_iters)
    end2.record()
    torch.cuda.synchronize()
    dram_us = start2.elapsed_time(end2) * 1000

    # Cleanup
    del sram_data, dram_data
    torch.cuda.empty_cache()

    return sram_us, dram_us


def benchmark_tiered_model(
    batch_size: int = 8,
    seq_len: int = 64,
    hidden_size: int = 512,
    num_heads: int = 8,
    H_cycles: int = 2,
    L_cycles: int = 2,
    H_layers: int = 4,
    L_layers: int = 4,
    halt_max_steps: int = 1,
    warmup: int = 5,
    iterations: int = 20,
    device: Optional[torch.device] = None,
) -> BenchmarkResult:
    """Benchmark the tiered HRM model."""

    if device is None:
        device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    vocab_size = 32
    config_dict = {
        'batch_size': batch_size,
        'seq_len': seq_len,
        'puzzle_emb_ndim': 0,
        'num_puzzle_identifiers': batch_size,
        'vocab_size': vocab_size,
        'H_cycles': H_cycles,
        'L_cycles': L_cycles,
        'H_layers': H_layers,
        'L_layers': L_layers,
        'hidden_size': hidden_size,
        'expansion': 4.0,
        'num_heads': num_heads,
        'pos_encodings': 'rope',
        'halt_max_steps': halt_max_steps,
        'halt_exploration_prob': 0.0,
    }

    # Memory manager
    mem_mgr = MemoryTierManager(device=device, enable_tracking=True)

    # Create model
    model = HRM_Tiered(config_dict, memory_manager=mem_mgr).to(device)
    model.eval()

    batch = _create_dummy_batch(batch_size, seq_len, vocab_size, device)

    # ---- Warmup ----
    with torch.no_grad():
        for _ in range(warmup):
            carry = model.initial_carry(batch)
            carry.inner_carry.z_H = carry.inner_carry.z_H.to(device)
            carry.inner_carry.z_L = carry.inner_carry.z_L.to(device)
            carry.steps = carry.steps.to(device)
            carry.halted = carry.halted.to(device)
            carry.current_data = {k: v.to(device) for k, v in carry.current_data.items()}
            model(carry, batch)

    model.reset_timing()
    mem_mgr.reset_stats()
    if torch.cuda.is_available():
        torch.cuda.reset_peak_memory_stats(device)
        torch.cuda.synchronize()

    # ---- Benchmark iterations ----
    total_latencies_ms = []
    with torch.no_grad():
        for _ in range(iterations):
            carry = model.initial_carry(batch)
            carry.inner_carry.z_H = carry.inner_carry.z_H.to(device)
            carry.inner_carry.z_L = carry.inner_carry.z_L.to(device)
            carry.steps = carry.steps.to(device)
            carry.halted = carry.halted.to(device)
            carry.current_data = {k: v.to(device) for k, v in carry.current_data.items()}

            if torch.cuda.is_available():
                start_event = torch.cuda.Event(enable_timing=True)
                end_event = torch.cuda.Event(enable_timing=True)
                start_event.record()

            t0 = time.perf_counter()
            model(carry, batch)

            if torch.cuda.is_available():
                end_event.record()
                torch.cuda.synchronize()
                total_latencies_ms.append(start_event.elapsed_time(end_event))
            else:
                total_latencies_ms.append((time.perf_counter() - t0) * 1000)

    # ---- Collect results ----
    timing = model.get_timing_stats()
    mem_stats = mem_mgr.get_stats()

    # GPU memory
    total_gpu_mb = 0.0
    if torch.cuda.is_available():
        total_gpu_mb = torch.cuda.max_memory_allocated(device) / (1024 * 1024)

    # GPU metrics
    gpu_metrics = _get_gpu_metrics()

    # Triton latency probe
    sram_probe_us, dram_probe_us = _run_triton_latency_probe(
        batch_size, hidden_size, device,
    )

    # Compute derived metrics
    l_mean = timing['L_forward_us']['mean_us']
    h_mean = timing['H_forward_us']['mean_us']
    ratio = h_mean / l_mean if l_mean > 0 else float('inf')

    total_mean_ms = sum(total_latencies_ms) / len(total_latencies_ms)
    throughput = batch_size / (total_mean_ms / 1000) if total_mean_ms > 0 else 0

    compute_time = timing['L_forward_us']['total_us'] + timing['H_forward_us']['total_us']
    transfer_time = timing['H_L_transfer_us']['total_us'] + timing['L_H_transfer_us']['total_us']
    efficiency = compute_time / (compute_time + transfer_time) if (compute_time + transfer_time) > 0 else 0

    # Build L-level stats
    l_values = [timing['L_forward_us']['min_us'], timing['L_forward_us']['max_us']]

    result = BenchmarkResult(
        model_name='HRM_Tiered',
        batch_size=batch_size,
        seq_len=seq_len,
        hidden_size=hidden_size,
        H_cycles=H_cycles,
        L_cycles=L_cycles,
        H_layers=H_layers,
        L_layers=L_layers,
        num_iterations=iterations,
        warmup_iterations=warmup,

        l_level_latency_mean_us=l_mean,
        l_level_latency_min_us=timing['L_forward_us']['min_us'],
        l_level_latency_max_us=timing['L_forward_us']['max_us'],
        l_level_latency_std_us=0.0,

        h_level_latency_mean_us=h_mean,
        h_level_latency_min_us=timing['H_forward_us']['min_us'],
        h_level_latency_max_us=timing['H_forward_us']['max_us'],
        h_level_latency_std_us=0.0,

        total_inference_latency_mean_ms=total_mean_ms,
        total_inference_latency_min_ms=min(total_latencies_ms),
        total_inference_latency_max_ms=max(total_latencies_ms),
        total_inference_latency_std_ms=_std(total_latencies_ms),

        throughput_samples_per_sec=throughput,

        sram_peak_mb=mem_stats['sram']['peak_mb'],
        dram_peak_mb=mem_stats['dram']['peak_mb'],
        total_gpu_memory_mb=total_gpu_mb,

        h_l_transfer_mean_us=timing['H_L_transfer_us']['mean_us'],
        l_h_transfer_mean_us=timing['L_H_transfer_us']['mean_us'],

        sram_hit_rate=mem_stats['sram']['hit_rate'],

        triton_sram_probe_latency_us=sram_probe_us,
        triton_dram_probe_latency_us=dram_probe_us,

        gpu_utilization_pct=gpu_metrics['utilization'],
        gpu_power_w=gpu_metrics['power'],
        gpu_temperature_c=gpu_metrics['temperature'],

        h_over_l_latency_ratio=ratio,
        memory_efficiency=efficiency,
    )

    # Cleanup
    del model, batch, carry
    if torch.cuda.is_available():
        torch.cuda.empty_cache()

    return result


def benchmark_baseline_model(
    batch_size: int = 8,
    seq_len: int = 64,
    hidden_size: int = 512,
    num_heads: int = 8,
    H_cycles: int = 2,
    L_cycles: int = 2,
    H_layers: int = 4,
    L_layers: int = 4,
    halt_max_steps: int = 1,
    warmup: int = 5,
    iterations: int = 20,
    device: Optional[torch.device] = None,
) -> BenchmarkResult:
    """Benchmark the original (non-tiered) HRM model."""

    if device is None:
        device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    vocab_size = 32
    config_dict = {
        'batch_size': batch_size,
        'seq_len': seq_len,
        'puzzle_emb_ndim': 0,
        'num_puzzle_identifiers': batch_size,
        'vocab_size': vocab_size,
        'H_cycles': H_cycles,
        'L_cycles': L_cycles,
        'H_layers': H_layers,
        'L_layers': L_layers,
        'hidden_size': hidden_size,
        'expansion': 4.0,
        'num_heads': num_heads,
        'pos_encodings': 'rope',
        'halt_max_steps': halt_max_steps,
        'halt_exploration_prob': 0.0,
    }

    model = HierarchicalReasoningModel_ACTV1(config_dict).to(device)
    model.eval()

    batch = _create_dummy_batch(batch_size, seq_len, vocab_size, device)

    # ---- Warmup ----
    with torch.no_grad():
        for _ in range(warmup):
            carry = model.initial_carry(batch)
            carry.inner_carry.z_H = carry.inner_carry.z_H.to(device)
            carry.inner_carry.z_L = carry.inner_carry.z_L.to(device)
            carry.steps = carry.steps.to(device)
            carry.halted = carry.halted.to(device)
            carry.current_data = {k: v.to(device) for k, v in carry.current_data.items()}
            model(carry, batch)

    if torch.cuda.is_available():
        torch.cuda.reset_peak_memory_stats(device)
        torch.cuda.synchronize()

    # ---- Benchmark ----
    total_latencies_ms = []
    with torch.no_grad():
        for _ in range(iterations):
            carry = model.initial_carry(batch)
            carry.inner_carry.z_H = carry.inner_carry.z_H.to(device)
            carry.inner_carry.z_L = carry.inner_carry.z_L.to(device)
            carry.steps = carry.steps.to(device)
            carry.halted = carry.halted.to(device)
            carry.current_data = {k: v.to(device) for k, v in carry.current_data.items()}

            if torch.cuda.is_available():
                start_event = torch.cuda.Event(enable_timing=True)
                end_event = torch.cuda.Event(enable_timing=True)
                start_event.record()

            t0 = time.perf_counter()
            model(carry, batch)

            if torch.cuda.is_available():
                end_event.record()
                torch.cuda.synchronize()
                total_latencies_ms.append(start_event.elapsed_time(end_event))
            else:
                total_latencies_ms.append((time.perf_counter() - t0) * 1000)

    total_gpu_mb = 0.0
    if torch.cuda.is_available():
        total_gpu_mb = torch.cuda.max_memory_allocated(device) / (1024 * 1024)

    total_mean_ms = sum(total_latencies_ms) / len(total_latencies_ms)
    throughput = batch_size / (total_mean_ms / 1000) if total_mean_ms > 0 else 0

    gpu_metrics = _get_gpu_metrics()

    result = BenchmarkResult(
        model_name='HRM_Baseline',
        batch_size=batch_size,
        seq_len=seq_len,
        hidden_size=hidden_size,
        H_cycles=H_cycles,
        L_cycles=L_cycles,
        H_layers=H_layers,
        L_layers=L_layers,
        num_iterations=iterations,
        warmup_iterations=warmup,

        l_level_latency_mean_us=0, l_level_latency_min_us=0,
        l_level_latency_max_us=0, l_level_latency_std_us=0,
        h_level_latency_mean_us=0, h_level_latency_min_us=0,
        h_level_latency_max_us=0, h_level_latency_std_us=0,

        total_inference_latency_mean_ms=total_mean_ms,
        total_inference_latency_min_ms=min(total_latencies_ms),
        total_inference_latency_max_ms=max(total_latencies_ms),
        total_inference_latency_std_ms=_std(total_latencies_ms),

        throughput_samples_per_sec=throughput,

        sram_peak_mb=0, dram_peak_mb=0,
        total_gpu_memory_mb=total_gpu_mb,

        h_l_transfer_mean_us=0, l_h_transfer_mean_us=0,
        sram_hit_rate=0,

        triton_sram_probe_latency_us=0, triton_dram_probe_latency_us=0,

        gpu_utilization_pct=gpu_metrics['utilization'],
        gpu_power_w=gpu_metrics['power'],
        gpu_temperature_c=gpu_metrics['temperature'],

        h_over_l_latency_ratio=0,
        memory_efficiency=1.0,
    )

    del model, batch, carry
    if torch.cuda.is_available():
        torch.cuda.empty_cache()

    return result


def compare_models(
    batch_sizes: List[int] = [1, 8, 32],
    seq_lens: List[int] = [64, 128],
    hidden_size: int = 512,
    warmup: int = 5,
    iterations: int = 20,
    device: Optional[torch.device] = None,
) -> List[Dict]:
    """Run comparative benchmark between tiered and baseline HRM."""
    results = []

    for bs in batch_sizes:
        for sl in seq_lens:
            print(f"\n{'='*60}")
            print(f"  Benchmarking: batch_size={bs}, seq_len={sl}")
            print(f"{'='*60}")

            print("  → Baseline model...")
            baseline = benchmark_baseline_model(
                batch_size=bs, seq_len=sl, hidden_size=hidden_size,
                warmup=warmup, iterations=iterations, device=device,
            )

            print("  → Tiered model...")
            tiered = benchmark_tiered_model(
                batch_size=bs, seq_len=sl, hidden_size=hidden_size,
                warmup=warmup, iterations=iterations, device=device,
            )

            comparison = {
                'batch_size': bs,
                'seq_len': sl,
                'baseline': asdict(baseline),
                'tiered': asdict(tiered),
                'speedup': baseline.total_inference_latency_mean_ms / tiered.total_inference_latency_mean_ms if tiered.total_inference_latency_mean_ms > 0 else 0,
                'memory_savings_mb': baseline.total_gpu_memory_mb - tiered.total_gpu_memory_mb,
                'throughput_improvement': tiered.throughput_samples_per_sec / baseline.throughput_samples_per_sec if baseline.throughput_samples_per_sec > 0 else 0,
            }
            results.append(comparison)

            # Print summary
            print(f"\n  Results:")
            print(f"    Baseline latency: {baseline.total_inference_latency_mean_ms:.2f} ms")
            print(f"    Tiered latency:   {tiered.total_inference_latency_mean_ms:.2f} ms")
            print(f"    Speedup:          {comparison['speedup']:.2f}x")
            print(f"    H/L ratio:        {tiered.h_over_l_latency_ratio:.2f}x")
            print(f"    SRAM probe:       {tiered.triton_sram_probe_latency_us:.1f} μs")
            print(f"    DRAM probe:       {tiered.triton_dram_probe_latency_us:.1f} μs")

    return results


def print_results_table(results: List[BenchmarkResult]):
    """Pretty-print benchmark results as a table."""
    header = (
        f"{'Model':<15} {'BS':>4} {'Seq':>5} "
        f"{'Latency(ms)':>12} {'Throughput':>12} "
        f"{'L_lat(μs)':>10} {'H_lat(μs)':>10} {'H/L':>6} "
        f"{'GPU_MB':>8} {'Efficiency':>10}"
    )
    print(f"\n{'='*len(header)}")
    print(header)
    print(f"{'='*len(header)}")

    for r in results:
        print(
            f"{r.model_name:<15} {r.batch_size:>4} {r.seq_len:>5} "
            f"{r.total_inference_latency_mean_ms:>12.2f} {r.throughput_samples_per_sec:>12.1f} "
            f"{r.l_level_latency_mean_us:>10.1f} {r.h_level_latency_mean_us:>10.1f} {r.h_over_l_latency_ratio:>6.2f} "
            f"{r.total_gpu_memory_mb:>8.1f} {r.memory_efficiency:>10.3f}"
        )
    print()


def generate_plots(results: List[Dict], output_dir: str = "benchmark_results"):
    """Generate comparison plots using matplotlib."""
    try:
        import matplotlib
        matplotlib.use('Agg')
        import matplotlib.pyplot as plt
        import numpy as np
    except ImportError:
        print("matplotlib not available — skipping plot generation.")
        return

    os.makedirs(output_dir, exist_ok=True)

    # ---- Plot 1: Latency comparison ----
    fig, axes = plt.subplots(1, 3, figsize=(18, 5))
    fig.suptitle('HRM SRAM/DRAM Tiering — Benchmark Results', fontsize=14, fontweight='bold')

    configs = [f"bs={r['batch_size']}\nseq={r['seq_len']}" for r in results]
    baseline_lat = [r['baseline']['total_inference_latency_mean_ms'] for r in results]
    tiered_lat = [r['tiered']['total_inference_latency_mean_ms'] for r in results]

    x = np.arange(len(configs))
    w = 0.35

    ax = axes[0]
    ax.bar(x - w/2, baseline_lat, w, label='Baseline', color='#e74c3c', alpha=0.8)
    ax.bar(x + w/2, tiered_lat, w, label='Tiered (SRAM/DRAM)', color='#2ecc71', alpha=0.8)
    ax.set_xlabel('Configuration')
    ax.set_ylabel('Latency (ms)')
    ax.set_title('Inference Latency')
    ax.set_xticks(x)
    ax.set_xticklabels(configs, fontsize=8)
    ax.legend()
    ax.grid(axis='y', alpha=0.3)

    # ---- Plot 2: Throughput ----
    ax = axes[1]
    baseline_tp = [r['baseline']['throughput_samples_per_sec'] for r in results]
    tiered_tp = [r['tiered']['throughput_samples_per_sec'] for r in results]
    ax.bar(x - w/2, baseline_tp, w, label='Baseline', color='#e74c3c', alpha=0.8)
    ax.bar(x + w/2, tiered_tp, w, label='Tiered', color='#2ecc71', alpha=0.8)
    ax.set_xlabel('Configuration')
    ax.set_ylabel('Samples/sec')
    ax.set_title('Throughput')
    ax.set_xticks(x)
    ax.set_xticklabels(configs, fontsize=8)
    ax.legend()
    ax.grid(axis='y', alpha=0.3)

    # ---- Plot 3: H vs L latency (tiered only) ----
    ax = axes[2]
    l_lat = [r['tiered']['l_level_latency_mean_us'] for r in results]
    h_lat = [r['tiered']['h_level_latency_mean_us'] for r in results]
    ax.bar(x - w/2, l_lat, w, label='L-level (SRAM)', color='#3498db', alpha=0.8)
    ax.bar(x + w/2, h_lat, w, label='H-level (DRAM)', color='#e67e22', alpha=0.8)
    ax.set_xlabel('Configuration')
    ax.set_ylabel('Latency (μs)')
    ax.set_title('Per-Module Latency')
    ax.set_xticks(x)
    ax.set_xticklabels(configs, fontsize=8)
    ax.legend()
    ax.grid(axis='y', alpha=0.3)

    plt.tight_layout()
    plot_path = os.path.join(output_dir, 'benchmark_comparison.png')
    plt.savefig(plot_path, dpi=150, bbox_inches='tight')
    plt.close()
    print(f"  Plot saved: {plot_path}")

    # ---- Plot 4: Memory breakdown ----
    fig, axes = plt.subplots(1, 2, figsize=(12, 5))
    fig.suptitle('Memory Analysis', fontsize=14, fontweight='bold')

    ax = axes[0]
    gpu_mem_baseline = [r['baseline']['total_gpu_memory_mb'] for r in results]
    gpu_mem_tiered = [r['tiered']['total_gpu_memory_mb'] for r in results]
    ax.bar(x - w/2, gpu_mem_baseline, w, label='Baseline', color='#e74c3c', alpha=0.8)
    ax.bar(x + w/2, gpu_mem_tiered, w, label='Tiered', color='#2ecc71', alpha=0.8)
    ax.set_xlabel('Configuration')
    ax.set_ylabel('GPU Memory (MB)')
    ax.set_title('Total GPU Memory')
    ax.set_xticks(x)
    ax.set_xticklabels(configs, fontsize=8)
    ax.legend()
    ax.grid(axis='y', alpha=0.3)

    ax = axes[1]
    triton_sram = [r['tiered']['triton_sram_probe_latency_us'] for r in results]
    triton_dram = [r['tiered']['triton_dram_probe_latency_us'] for r in results]
    ax.bar(x - w/2, triton_sram, w, label='SRAM Probe', color='#3498db', alpha=0.8)
    ax.bar(x + w/2, triton_dram, w, label='DRAM Probe', color='#e67e22', alpha=0.8)
    ax.set_xlabel('Configuration')
    ax.set_ylabel('Latency (μs)')
    ax.set_title('Triton Memory Probe Latency')
    ax.set_xticks(x)
    ax.set_xticklabels(configs, fontsize=8)
    ax.legend()
    ax.grid(axis='y', alpha=0.3)

    plt.tight_layout()
    plot_path = os.path.join(output_dir, 'memory_analysis.png')
    plt.savefig(plot_path, dpi=150, bbox_inches='tight')
    plt.close()
    print(f"  Plot saved: {plot_path}")