Spaces:
Running

File size: 5,520 Bytes
0a7b128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""
Test script for Libratio Fleet Multi-Vendor Hardware Profile Architecture.
Verifies H100, AMD MI300X, and Google TPU v5p dynamic physics calculations.
"""
from environment.fleet_env import FleetEnvironment
from environment.physics_model import HARDWARE_PROFILES, compute_training_cost, score_precision_layer
import json
import sys
import io


def test_hardware_registry():
    print("--- Testing Hardware Profile Registry ---")
    assert "nvidia_h100" in HARDWARE_PROFILES
    assert "amd_mi300x" in HARDWARE_PROFILES
    assert "google_tpu_v5p" in HARDWARE_PROFILES

    h100 = HARDWARE_PROFILES["nvidia_h100"]
    amd = HARDWARE_PROFILES["amd_mi300x"]
    tpu = HARDWARE_PROFILES["google_tpu_v5p"]

    print(f"[OK] H100 GPU: max memory = {h100['gpu_memory_gb']} GB")
    print(f"[OK] AMD GPU: max memory = {amd['gpu_memory_gb']} GB")
    print(f"[OK] TPU chip: max memory = {tpu['gpu_memory_gb']} GB")

    # Verify memory differences
    assert h100["gpu_memory_gb"] == 80.0
    assert amd["gpu_memory_gb"] == 192.0
    assert tpu["gpu_memory_gb"] == 95.0


def test_tpu_unsupported_fp8():
    print("\n--- Testing Format Rejections (TPU v5p vs NVIDIA H100) ---")
    
    # 1. H100 supports FP8
    score_h100, feedback_h100 = score_precision_layer("ffn", "FP8", hardware_profile="nvidia_h100")
    print(f"H100 FP8 FFN Score: {score_h100:.3f} | {feedback_h100[:60]}...")
    assert score_h100 > 0.90

    # 2. TPU v5p does not support FP8
    score_tpu, feedback_tpu = score_precision_layer("ffn", "FP8", hardware_profile="google_tpu_v5p")
    print(f"TPU FP8 FFN Score: {score_tpu:.3f} | {feedback_tpu}")
    assert score_tpu == 0.01
    assert "UNSUPPORTED" in feedback_tpu

    # 3. TPU v5p supports INT8 instead
    score_tpu_int8, feedback_tpu_int8 = score_precision_layer("ffn", "INT8", hardware_profile="google_tpu_v5p")
    print(f"TPU INT8 FFN Score: {score_tpu_int8:.3f} | {feedback_tpu_int8}")
    assert score_tpu_int8 > 0.90


def test_amd_mi300x_capacity():
    print("\n--- Testing AMD MI300X VRAM Capacity Cost Calculation ---")
    
    layer_dist = {"embedding": 0.15, "attention": 0.25, "ffn": 0.40, "layernorm": 0.002, "output": 0.198}
    strategy = {"embedding": "FP32", "attention": "BF16", "ffn": "FP8", "layernorm": "BF16", "output": "FP32"}
    
    # AMD MI300X VRAM allocation should yield identical param memory but run on a larger budget
    metrics_amd = compute_training_cost(
        total_params=70_000_000_000, # 70B model
        precision_strategy=strategy,
        layer_distribution=layer_dist,
        hardware_profile="amd_mi300x"
    )
    print(f"AMD 70B model memory footprint: {metrics_amd['memory_gb']} GB")
    assert metrics_amd["memory_gb"] > 100.0


def test_environment_scenarios():
    print("\n--- Testing Environment Integration ---")
    env = FleetEnvironment()

    # 1. Reset AMD Scenario
    print("Testing AMD scenario reset:")
    obs = env.reset("fleet_precision", scenario_id="amd_mi300x_fleet")
    print(f"  Observed Profile: {obs['cluster']['hardware_profile']} ({obs['cluster']['vendor']})")
    print(f"  Observed Available Precisions: {obs['available_precisions']}")
    assert obs["cluster"]["hardware_profile"] == "amd_mi300x"
    assert "FP8" in obs["available_precisions"]

    # Step AMD Scenario
    res = env.step({
        "precision_strategy": {
            "embedding": "FP32", "attention": "BF16", "ffn": "FP8",
            "layernorm": "BF16", "output": "FP32"
        },
        "reasoning": "Standard AMD mixed precision"
    })
    print(f"  AMD Step Score: {res['reward']['score']:.3f} | {res['reward']['feedback'][:100]}...")
    assert res["reward"]["score"] > 0.80

    # 2. Reset Google TPU Scenario
    print("Testing TPU scenario reset:")
    obs_tpu = env.reset("fleet_precision", scenario_id="google_tpu_fleet")
    print(f"  Observed Profile: {obs_tpu['cluster']['hardware_profile']} ({obs_tpu['cluster']['vendor']})")
    print(f"  Observed Available Precisions: {obs_tpu['available_precisions']}")
    assert obs_tpu["cluster"]["hardware_profile"] == "google_tpu_v5p"
    assert "FP8" not in obs_tpu["available_precisions"]
    assert "INT8" in obs_tpu["available_precisions"]

    # Step TPU Scenario with correct INT8
    res_tpu = env.step({
        "precision_strategy": {
            "embedding": "FP32", "attention": "BF16", "ffn": "INT8",
            "layernorm": "BF16", "output": "FP32"
        },
        "reasoning": "TPU INT8 FFN layout"
    })
    print(f"  TPU Step Score (INT8): {res_tpu['reward']['score']:.3f} | {res_tpu['reward']['feedback'][:100]}...")
    assert res_tpu["reward"]["score"] > 0.80

    # Step TPU Scenario with incorrect FP8 (should trigger penalty/rejection)
    env.reset("fleet_precision", scenario_id="google_tpu_fleet")
    res_tpu_fail = env.step({
        "precision_strategy": {
            "embedding": "FP32", "attention": "BF16", "ffn": "FP8",
            "layernorm": "BF16", "output": "FP32"
        },
        "reasoning": "Unoptimized FP8 on TPU"
    })
    print(f"  TPU Step Score (FP8): {res_tpu_fail['reward']['score']:.3f} | {res_tpu_fail['reward']['feedback'][:100]}...")
    assert res_tpu_fail["reward"]["score"] == 0.01


if __name__ == "__main__":
    if hasattr(sys.stdout, "reconfigure"):
        sys.stdout.reconfigure(encoding="utf-8", errors="replace")
    test_hardware_registry()
    test_tpu_unsupported_fp8()
    test_amd_mi300x_capacity()
    test_environment_scenarios()
    print("\n=== ALL HARDWARE PROFILE TESTS PASSED SUCCESSFULLY ===")