File size: 7,965 Bytes
383a1cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
tests/test_systems_and_serving.py

Comprehensive test suite verifying Enterprise Systems & Serving Modules:
1. vLLM Continuous Batching and PagedAttention Adapter (src/vllm_adapter.py)
2. NVIDIA TensorRT-LLM Custom Plugin (src/trt_llm_plugin.py)
3. Multi-Architecture Hardware Roofline Analyzer (src/roofline_analyzer.py)
"""

import sys
from pathlib import Path

# Ensure repo root is on sys.path
REPO_ROOT = Path(__file__).resolve().parent.parent
if str(REPO_ROOT) not in sys.path:
    sys.path.insert(0, str(REPO_ROOT))

import pytest
import torch
import torch.nn as nn

from src.config import ModelConfig
from src.models import QTensorFormer
from src.vllm_adapter import VLLMContinuousBatchingAdapter, QTensorFormerPagedAttentionMetadata
from src.trt_llm_plugin import QTensorFormerTRTPlugin
from src.roofline_analyzer import EnterpriseRooflineAnalyzer, DecodingRooflineProfile, HardwareRooflineSpec


@pytest.fixture
def tiny_qtf_model():
    cfg = ModelConfig(
        d_model=64,
        n_heads=2,
        n_layers=2,
        ff_multiplier=2,
        max_seq_len=64,
        vocab_size=256,
        tt_rank=4,
        tt_min_rank=1,
        use_quantum=False,
    )
    model = QTensorFormer(cfg)
    model.eval()
    return model


# =========================================================================
# 1. vLLM Continuous Batching & PagedAttention Tests
# =========================================================================

def test_vllm_paged_attention_metadata_creation():
    """Validates creation and tensor properties of PagedAttention metadata."""
    metadata = QTensorFormerPagedAttentionMetadata.create_dummy_metadata(
        batch_size=4,
        max_seq_len=32,
        block_size=16,
        device="cpu",
    )
    assert metadata.block_tables.shape == (4, 2)
    assert metadata.slot_mapping.shape == (4 * 32,)
    assert metadata.context_lens.shape == (4,)
    assert metadata.active_ranks.shape == (4,)
    assert metadata.max_context_len == 32


def test_vllm_rank_scheduling_thermal_and_entropy(tiny_qtf_model):
    """Verifies dynamic rank scheduling across varied token entropies and thermal states."""
    adapter = VLLMContinuousBatchingAdapter(tiny_qtf_model)

    # High, medium, low entropy tokens
    entropies = torch.tensor([3.2, 1.8, 0.4])

    # Case 1: Normal thermal headroom (> 5.0 C margin)
    ranks_normal = adapter.schedule_batch_ranks(
        seq_entropies=entropies,
        hardware_thermal_margin_c=12.5,
        target_latency_budget_ms=25.0,
    )
    assert ranks_normal == [16, 8, 4]

    # Case 2: Severe thermal throttling (< 5.0 C margin)
    ranks_throttled = adapter.schedule_batch_ranks(
        seq_entropies=entropies,
        hardware_thermal_margin_c=3.2,
        target_latency_budget_ms=25.0,
    )
    assert ranks_throttled == [2, 2, 2]


def test_vllm_paged_forward_execution(tiny_qtf_model):
    """Verifies that execute_paged_forward produces correct logits shapes without regression."""
    adapter = VLLMContinuousBatchingAdapter(tiny_qtf_model)
    metadata = QTensorFormerPagedAttentionMetadata.create_dummy_metadata(
        batch_size=2,
        max_seq_len=16,
        block_size=8,
    )
    input_ids = torch.randint(0, 256, (2, 16))
    positions = torch.arange(16).unsqueeze(0).expand(2, -1)

    logits = adapter.execute_paged_forward(
        input_ids=input_ids,
        positions=positions,
        metadata=metadata,
        selected_ranks=[4, 8],
    )
    assert logits.shape == (2, 16, 256)
    assert not torch.isnan(logits).any()


# =========================================================================
# 2. NVIDIA TensorRT-LLM Custom Plugin Tests
# =========================================================================

def test_trt_llm_plugin_initialization_and_metadata():
    """Validates IPluginV2DynamicExt contracts and static characteristics."""
    plugin = QTensorFormerTRTPlugin(
        in_features=64,
        out_features=64,
        max_tt_rank=8,
        in_modes=[4, 4, 4],
        out_modes=[4, 4, 4],
        dtype=torch.float32,
    )
    assert plugin.get_plugin_type() == "QTensorFormerPlugin_TRT"
    assert plugin.get_plugin_version() == "2.1.0"
    # Zero-scratchpad in SMEM contract
    assert plugin.get_workspace_size(max_batch_size=8, max_seq_len=128) == 0


def test_trt_llm_plugin_smem_sizing():
    """Verifies that SMEM allocation stays well below the 228 KB Hopper limit."""
    plugin = QTensorFormerTRTPlugin(
        in_features=64,
        out_features=64,
        max_tt_rank=8,
        in_modes=[4, 4, 4],
        out_modes=[4, 4, 4],
    )
    smem_r1 = plugin.compute_smem_requirement_bytes(active_rank=1)
    smem_r8 = plugin.compute_smem_requirement_bytes(active_rank=8)
    assert smem_r1 > 0
    assert smem_r8 >= smem_r1
    assert smem_r8 <= 65536  # Within hardware SM limits


def test_trt_llm_plugin_dynamic_slicing_and_forward():
    """Verifies that forward pass runs correctly across active rank configurations."""
    plugin = QTensorFormerTRTPlugin(
        in_features=64,
        out_features=64,
        max_tt_rank=8,
        in_modes=[4, 4, 4],
        out_modes=[4, 4, 4],
        dtype=torch.float32,
    )

    x = torch.randn(2, 10, 64)

    # Test full rank
    out_full = plugin(x, active_rank=8)
    assert out_full.shape == (2, 10, 64)
    assert not torch.isnan(out_full).any()

    # Test sliced sub-rank
    out_sliced = plugin(x, active_rank=2)
    assert out_sliced.shape == (2, 10, 64)
    assert not torch.isnan(out_sliced).any()

    # Core slicing checks
    c1, c2, c3 = plugin.slice_cores(active_rank=2)
    assert c1.shape == (1, 4, 4, 2)
    assert c2.shape == (2, 4, 4, 2)
    assert c3.shape == (2, 4, 4, 1)


# =========================================================================
# 3. Enterprise Roofline Analyzer Tests
# =========================================================================

def test_roofline_analyzer_initialization_all_targets():
    """Verifies hardware specs for Hopper, Blackwell, Thor, Orin, and Raspberry Pi 5."""
    targets = [
        "blackwell_b200",
        "hopper_h100",
        "ampere_a100",
        "jetson_thor",
        "jetson_orin_nano",
        "raspberry_pi_5",
    ]
    for key in targets:
        analyzer = EnterpriseRooflineAnalyzer(key)
        assert analyzer.spec.peak_bandwidth_gb_s > 0
        assert analyzer.spec.peak_tensor_tflops > 0
        assert analyzer.spec.ridge_point > 0


def test_roofline_analyzer_invalid_key():
    """Ensures unrecognized target raises KeyError with informative message."""
    with pytest.raises(KeyError):
        EnterpriseRooflineAnalyzer("unknown_supercomputer")


def test_roofline_token_decoding_simulation():
    """Validates operational intensity and speedup calculations."""
    analyzer = EnterpriseRooflineAnalyzer("hopper_h100")
    profile = analyzer.analyze_token_decoding(
        batch_size=1,
        seq_len=512,
        active_rank=4,
        d_model=512,
        n_layers=8,
    )
    assert profile.hardware_name == "NVIDIA Hopper H100 (SXM5)"
    assert profile.speedup_ratio >= 1.0
    assert profile.dram_traffic_reduction_pct > 80.0
    assert profile.qtf_latency_ms_per_token > 0.0
    assert profile.dense_latency_ms_per_token > profile.qtf_latency_ms_per_token
    assert "DRAM Bandwidth Limited" in profile.dense_regime


def test_roofline_compare_all_and_markdown_table():
    """Validates comparison across all targets and confirms zero em dashes in output."""
    comparison = EnterpriseRooflineAnalyzer.compare_all_hardware(batch_size=1, seq_len=512, active_rank=4)
    assert len(comparison) == 6
    for key, p in comparison.items():
        assert p.speedup_ratio > 1.0

    table_md = EnterpriseRooflineAnalyzer.generate_roofline_summary_table()
    assert "| NVIDIA Hopper H100" in table_md
    assert "| NVIDIA Blackwell B200" in table_md
    assert "—" not in table_md, "Found forbidden em dash in roofline summary table"