import json
import logging
import pytest
import torch
from tests.test_reshard_refit_fused_wire import _build, _RecordingTransport
SLOW_BYTES = 15_889_231_872
SLOW_WIRE_S = 10.213842
SLOW_GBPS = 12.4
FAST_BYTES = 16_637_117_592
FAST_WIRE_S = 0.631954
FLOOR_GBPS = 50.0
def _receiver(monkeypatch, floor):
if floor is None:
monkeypatch.delenv("MX_RESHARD_MIN_GBPS", raising=False)
else:
monkeypatch.setenv("MX_RESHARD_MIN_GBPS", str(floor))
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_floor(_Rig(), step, wire_bytes, stages)
def _record(caplog):
line = next(m for m in caplog.messages if "MX_REFIT_SLOW_THROUGHPUT" in m)
return json.loads(line.split("MX_REFIT_SLOW_THROUGHPUT ", 1)[1])
def test_the_incident_is_reported(monkeypatch, caplog):
mod = _receiver(monkeypatch, FLOOR_GBPS)
with caplog.at_level(logging.WARNING):
_check(mod, SLOW_BYTES, {"wire_fused_s": SLOW_WIRE_S})
payload = _record(caplog)
assert payload["schema"] == "refit-slow-throughput-v1"
assert payload["wire_bytes"] == SLOW_BYTES
assert payload["floor_gbps"] == FLOOR_GBPS
assert payload["implied_gbps"] == pytest.approx(SLOW_GBPS, abs=0.2)
def test_it_does_not_raise(monkeypatch, caplog):
mod = _receiver(monkeypatch, FLOOR_GBPS)
with caplog.at_level(logging.WARNING):
assert _check(mod, SLOW_BYTES, {"wire_fused_s": SLOW_WIRE_S}) is None
def test_the_healthy_run_is_silent(monkeypatch, caplog):
mod = _receiver(monkeypatch, FLOOR_GBPS)
with caplog.at_level(logging.WARNING):
_check(mod, FAST_BYTES, {"wire_fused_s": FAST_WIRE_S})
assert not [m for m in caplog.messages if "MX_REFIT_SLOW_THROUGHPUT" in m]
def test_the_shortfall_is_quantified(monkeypatch, caplog):
mod = _receiver(monkeypatch, FLOOR_GBPS)
with caplog.at_level(logging.WARNING):
_check(mod, SLOW_BYTES, {"wire_fused_s": SLOW_WIRE_S})
assert _record(caplog)["shortfall_x"] == pytest.approx(
FLOOR_GBPS / SLOW_GBPS, abs=0.1
)
def test_a_rate_exactly_at_the_floor_is_allowed(monkeypatch, caplog):
mod = _receiver(monkeypatch, FLOOR_GBPS)
exact_s = SLOW_BYTES * 8 / (FLOOR_GBPS * 1e9)
with caplog.at_level(logging.WARNING):
_check(mod, SLOW_BYTES, {"wire_fused_s": exact_s})
assert not [m for m in caplog.messages if "MX_REFIT_SLOW_THROUGHPUT" in m]
def test_disabled_by_default(monkeypatch, caplog):
mod = _receiver(monkeypatch, None)
assert mod._min_gbps() == 0
with caplog.at_level(logging.WARNING):
_check(mod, SLOW_BYTES, {"wire_fused_s": SLOW_WIRE_S})
assert not [m for m in caplog.messages if "MX_REFIT_SLOW_THROUGHPUT" in m]
@pytest.mark.parametrize("value", ["nan", "NaN", "inf", "-inf"])
def test_a_non_finite_floor_disables_rather_than_warning_on_every_refit(
monkeypatch, caplog, value
):
mod = _receiver(monkeypatch, value)
with caplog.at_level(logging.WARNING):
_check(mod, SLOW_BYTES, {"wire_fused_s": SLOW_WIRE_S})
assert not [m for m in caplog.messages if "MX_REFIT_SLOW_THROUGHPUT" in m]
@pytest.mark.parametrize("value", ["-50", "-0.5"])
def test_a_negative_floor_disables_but_says_so(monkeypatch, caplog, value):
mod = _receiver(monkeypatch, value)
with caplog.at_level(logging.WARNING):
assert mod._min_gbps() == 0
assert [m for m in caplog.messages if "is negative" in m], (
f"a negative floor must warn, got {caplog.messages}"
)
def test_an_unparseable_floor_disables_rather_than_crashes(monkeypatch):
mod = _receiver(monkeypatch, "not-a-number")
assert mod._min_gbps() == 0
assert _check(mod, SLOW_BYTES, {"wire_fused_s": SLOW_WIRE_S}) is None
def test_phased_mode_sums_its_three_wire_stages(monkeypatch, caplog):
mod = _receiver(monkeypatch, FLOOR_GBPS)
phased = {"wire_exact_s": 4.0, "wire_full_s": 4.0, "wire_convert_s": 2.213842}
with caplog.at_level(logging.WARNING):
_check(mod, SLOW_BYTES, phased)
assert _record(caplog)["wire_s"] == pytest.approx(SLOW_WIRE_S, abs=1e-5)
def test_a_missing_or_zero_wire_time_is_not_a_zero_rate(monkeypatch, caplog):
mod = _receiver(monkeypatch, FLOOR_GBPS)
with caplog.at_level(logging.WARNING):
assert _check(mod, SLOW_BYTES, {}) is None
assert _check(mod, SLOW_BYTES, {"wire_fused_s": 0.0}) is None
assert _check(mod, 0, {"wire_fused_s": 0.5}) is None
assert not [m for m in caplog.messages if "MX_REFIT_SLOW_THROUGHPUT" in m]
def test_the_report_names_the_rank_that_saw_it(monkeypatch, caplog):
mod = _receiver(monkeypatch, FLOOR_GBPS)
with caplog.at_level(logging.WARNING):
_check(mod, SLOW_BYTES, {"wire_fused_s": SLOW_WIRE_S})
assert _record(caplog)["rank"] == 3
def _refit(monkeypatch, floor, stage_record):
monkeypatch.setenv("MX_RESHARD_FUSED_WIRE", "1")
monkeypatch.setenv("MX_RESHARD_MIN_GBPS", str(floor))
monkeypatch.delenv("MX_RESHARD_MAX_GBPS", raising=False)
monkeypatch.setenv("MX_REFIT_STAGE_RECORD", stage_record)
monkeypatch.setattr(torch.cuda, "synchronize", lambda *a, **k: None)
return _build(_RecordingTransport())
def test_a_breached_floor_still_installs(monkeypatch, caplog):
harness, keepalive = _refit(monkeypatch, 1e9, "1")
with caplog.at_level(logging.WARNING):
harness.update_weights(step=1)
assert harness._install_order == ["install"], (
"a slow refit is still a correct refit and must reach live parameters"
)
assert [m for m in caplog.messages if "MX_REFIT_SLOW_THROUGHPUT" in m]
assert keepalive is not None
def test_the_floor_does_not_depend_on_the_stage_record(monkeypatch, caplog):
harness, keepalive = _refit(monkeypatch, 1e9, "0")
with caplog.at_level(logging.WARNING):
harness.update_weights(step=1)
assert not [m for m in caplog.messages if "MX_REFIT_STAGE " in m], (
"precondition: the stage record is off for this run"
)
assert [m for m in caplog.messages if "MX_REFIT_SLOW_THROUGHPUT" in m], (
"the floor must report independently of the stage record"
)
assert keepalive is not None
def test_a_healthy_refit_through_the_real_path_is_silent(monkeypatch, caplog):
harness, keepalive = _refit(monkeypatch, 0.000001, "1")
with caplog.at_level(logging.WARNING):
harness.update_weights(step=1)
assert harness._install_order == ["install"]
assert not [m for m in caplog.messages if "MX_REFIT_SLOW_THROUGHPUT" in m]
assert keepalive is not None
def test_floor_and_ceiling_can_both_be_configured(monkeypatch, caplog):
monkeypatch.setenv("MX_RESHARD_MIN_GBPS", str(FLOOR_GBPS))
monkeypatch.setenv("MX_RESHARD_MAX_GBPS", "400")
from modelexpress.refit.reshard import receiver
with caplog.at_level(logging.WARNING):
receiver.ReshardReceiver._check_throughput_floor(
_Rig(), 1, FAST_BYTES, {"wire_fused_s": FAST_WIRE_S}
)
receiver.ReshardReceiver._check_throughput_ceiling(
_Rig(), 1, FAST_BYTES, {"wire_fused_s": FAST_WIRE_S}
)
assert not [
m
for m in caplog.messages
if "MX_REFIT_SLOW_THROUGHPUT" in m or "MX_REFIT_IMPOSSIBLE_THROUGHPUT" in m
]