import json
import logging
import pytest
import torch
from tests.test_reshard_refit_fused_wire import _build, _RecordingTransport
OBSERVED_BYTES = 40_611_246_080
OBSERVED_WIRE_S = 0.839641
OBSERVED_IMPLIED_GBPS = 386.9 FOUR_ADAPTER_CEILING = 381.6
TWO_ADAPTER_CEILING = 190.8
def _receiver(monkeypatch, ceiling):
if ceiling is None:
monkeypatch.delenv("MX_RESHARD_MAX_GBPS", raising=False)
else:
monkeypatch.setenv("MX_RESHARD_MAX_GBPS", str(ceiling))
from modelexpress.refit.reshard import receiver
return receiver
class _Rig:
_global_rank = 3
def _check(mod, wire_bytes, stages, step=1):
return mod.ReshardReceiver._check_throughput_ceiling(
_Rig(), step, wire_bytes, stages
)
def test_the_impossible_run_is_rejected(monkeypatch):
mod = _receiver(monkeypatch, TWO_ADAPTER_CEILING)
with pytest.raises(RuntimeError) as excinfo:
_check(mod, OBSERVED_BYTES, {"wire_fused_s": OBSERVED_WIRE_S})
message = str(excinfo.value)
assert "386.9" in message
assert "without moving the payload" in message
assert "failed, not fast" in message
def test_it_would_have_been_caught_even_at_the_four_adapter_ceiling(monkeypatch):
mod = _receiver(monkeypatch, FOUR_ADAPTER_CEILING)
with pytest.raises(RuntimeError):
_check(mod, OBSERVED_BYTES, {"wire_fused_s": OBSERVED_WIRE_S})
def test_the_real_topology_b_run_passes(monkeypatch):
mod = _receiver(monkeypatch, FOUR_ADAPTER_CEILING)
assert _check(mod, OBSERVED_BYTES, {"wire_fused_s": 1.273}) is None
def test_a_rate_exactly_at_the_ceiling_is_allowed(monkeypatch):
mod = _receiver(monkeypatch, FOUR_ADAPTER_CEILING)
exact_s = OBSERVED_BYTES * 8 / (FOUR_ADAPTER_CEILING * 1e9)
assert _check(mod, OBSERVED_BYTES, {"wire_fused_s": exact_s}) is None
def test_disabled_by_default(monkeypatch):
mod = _receiver(monkeypatch, None)
assert mod._max_gbps() == 0
assert _check(mod, OBSERVED_BYTES, {"wire_fused_s": OBSERVED_WIRE_S}) is None
@pytest.mark.parametrize("value", ["nan", "NaN", "inf", "-inf"])
def test_a_non_finite_ceiling_disables_rather_than_failing_every_refit(
monkeypatch, value
):
mod = _receiver(monkeypatch, value)
assert _check(mod, OBSERVED_BYTES, {"wire_fused_s": OBSERVED_WIRE_S}) is None
def test_an_unparseable_ceiling_disables_rather_than_crashes(monkeypatch):
mod = _receiver(monkeypatch, "not-a-number")
assert mod._max_gbps() == 0
assert _check(mod, OBSERVED_BYTES, {"wire_fused_s": OBSERVED_WIRE_S}) is None
def test_phased_mode_sums_its_three_wire_stages(monkeypatch):
mod = _receiver(monkeypatch, FOUR_ADAPTER_CEILING)
phased = {"wire_exact_s": 0.5, "wire_full_s": 0.5, "wire_convert_s": 0.273}
assert _check(mod, OBSERVED_BYTES, phased) is None
too_fast = {"wire_exact_s": 0.3, "wire_full_s": 0.3, "wire_convert_s": 0.2}
with pytest.raises(RuntimeError):
_check(mod, OBSERVED_BYTES, too_fast)
def test_a_missing_or_zero_wire_time_is_not_an_infinite_rate(monkeypatch):
mod = _receiver(monkeypatch, FOUR_ADAPTER_CEILING)
assert _check(mod, OBSERVED_BYTES, {}) is None
assert _check(mod, OBSERVED_BYTES, {"wire_fused_s": 0.0}) is None
assert _check(mod, 0, {"wire_fused_s": 0.5}) is None
def test_the_failure_is_machine_readable_before_it_raises(monkeypatch, caplog):
mod = _receiver(monkeypatch, TWO_ADAPTER_CEILING)
with caplog.at_level(logging.WARNING), pytest.raises(RuntimeError):
_check(mod, OBSERVED_BYTES, {"wire_fused_s": OBSERVED_WIRE_S})
line = next(m for m in caplog.messages if "MX_REFIT_IMPOSSIBLE_THROUGHPUT" in m)
payload = json.loads(line.split("MX_REFIT_IMPOSSIBLE_THROUGHPUT ", 1)[1])
assert payload["schema"] == "refit-impossible-throughput-v1"
assert payload["wire_bytes"] == OBSERVED_BYTES
assert payload["ceiling_gbps"] == TWO_ADAPTER_CEILING
assert payload["implied_gbps"] == pytest.approx(OBSERVED_IMPLIED_GBPS, abs=0.2)
def test_the_failure_names_the_rank_that_saw_it(monkeypatch, caplog):
mod = _receiver(monkeypatch, TWO_ADAPTER_CEILING)
with caplog.at_level(logging.WARNING), pytest.raises(RuntimeError):
_check(mod, OBSERVED_BYTES, {"wire_fused_s": OBSERVED_WIRE_S})
line = next(m for m in caplog.messages if "MX_REFIT_IMPOSSIBLE_THROUGHPUT" in m)
assert json.loads(line.split("MX_REFIT_IMPOSSIBLE_THROUGHPUT ", 1)[1])["rank"] == 3
def _refit(monkeypatch, ceiling):
monkeypatch.setenv("MX_RESHARD_FUSED_WIRE", "1")
monkeypatch.setenv("MX_RESHARD_MAX_GBPS", str(ceiling))
monkeypatch.delenv("MX_REFIT_STAGE_RECORD", raising=False)
monkeypatch.setattr(torch.cuda, "synchronize", lambda *a, **k: None)
return _build(_RecordingTransport())
def test_a_breached_ceiling_blocks_the_install(monkeypatch):
harness, keepalive = _refit(monkeypatch, 0.000001)
with pytest.raises(RuntimeError, match="failed, not fast"):
harness.update_weights(step=1)
assert harness._install_order == [], (
"the ceiling breached, so nothing should have reached live parameters"
)
assert keepalive is not None
def test_a_rate_under_the_ceiling_still_installs(monkeypatch):
harness, keepalive = _refit(monkeypatch, 1e9)
harness.update_weights(step=1)
assert harness._install_order == ["install"]
assert keepalive is not None
def test_a_bounded_plan_still_reaches_the_guard(monkeypatch):
monkeypatch.setenv("MX_RESHARD_FUSED_WIRE", "0")
monkeypatch.setenv("MX_RESHARD_MAX_GBPS", "0.000001")
monkeypatch.delenv("MX_REFIT_STAGE_RECORD", raising=False)
monkeypatch.setattr(torch.cuda, "synchronize", lambda *a, **k: None)
harness, keepalive = _build(_RecordingTransport())
harness._plan.segments = []
assert harness._plan.exact_descriptor_count, "precondition: a bounded plan"
with pytest.raises(RuntimeError, match="failed, not fast"):
harness.update_weights(step=1)
assert harness._install_order == []
assert keepalive is not None