|
12 | 12 | """ |
13 | 13 |
|
14 | 14 | import sys |
| 15 | +import time |
15 | 16 | import numpy as np |
16 | 17 | import pytest |
17 | 18 | from prototype.model_tools import ToyModel |
@@ -319,19 +320,28 @@ def test_throughput_consistency(self): |
319 | 320 | for bs in batch_sizes: |
320 | 321 | x = np.random.randn(bs, 8).astype(np.float32) |
321 | 322 |
|
322 | | - # Measure time for single-node |
323 | | - start = np.datetime64('now') |
324 | | - _ = single_node_forward(model, x[0]) # First element only |
325 | | - end = np.datetime64('now') |
| 323 | + # Warm up to avoid first-run JIT/cache effects |
| 324 | + for _ in range(3): |
| 325 | + _ = single_node_forward(model, x[0]) |
326 | 326 |
|
327 | | - latency = (end - start).astype(np.float64) * 1e9 # nanoseconds |
| 327 | + # Take best-of-N to reduce OS scheduling noise |
| 328 | + samples = [] |
| 329 | + for _ in range(10): |
| 330 | + start = time.perf_counter_ns() |
| 331 | + _ = single_node_forward(model, x[0]) # First element only |
| 332 | + end = time.perf_counter_ns() |
| 333 | + samples.append(end - start) |
328 | 334 |
|
329 | | - latencies.append(latency) |
| 335 | + best_latency = float(min(samples)) # nanoseconds |
| 336 | + latencies.append(best_latency) |
330 | 337 |
|
331 | 338 | # Latencies should be within 20% of each other (allowing for variance) |
332 | 339 | min_latency = min(latencies) |
333 | 340 | max_latency = max(latencies) |
334 | 341 |
|
| 342 | + if min_latency < 100: |
| 343 | + pytest.skip("Timer resolution too low to measure latency reliably") |
| 344 | + |
335 | 345 | assert (max_latency - min_latency) / min_latency < 0.2, \ |
336 | 346 | f"Latency variance too high: {min_latency} vs {max_latency}" |
337 | 347 |
|
|
0 commit comments