Skip to content

armgit / sie

Public Imported from GitHub
0

Open-source inference server and production cluster for all the models your agent needs.

12.5 KB
Raw Blame
from __future__ import annotations

from datetime import UTC, datetime, timedelta

import pytest
from sie_placement.planner import PlannerParameters, plan
from sie_placement.report import ComeAliveBasis
from sie_placement.schema import (
    BootEvidence,
    BootOutcome,
    Cell,
    ContainerShape,
    Demand,
    DemandHour,
    DemandSeries,
    Envelope,
    IdentityServiceLevel,
    MachineProfilePrice,
    Operation,
    Percentiles,
    Placement,
    Plan,
    PlanIdentity,
    PlanLane,
    PriceList,
    Receipt,
    RequestShape,
    ServiceLevel,
    ServiceLevels,
    SourceRef,
    Throughput,
    Warmth,
)

START = datetime(2026, 11, 2, tzinfo=UTC)
MONTH = 730.0
SCALE = MONTH / 24
L4 = Cell(machine_profile="l4", gpu_product="NVIDIA-L4")
A100 = Cell(machine_profile="a100-80gb", gpu_product="NVIDIA-A100-SXM4-80GB")
SHAPE = ContainerShape(gpu_count=1, cpu_cores=4, memory_mib=16384)
WIDE = ContainerShape(gpu_count=1, cpu_cores=8, memory_mib=32768)
PRICES = PriceList(
    currency="USD",
    machine_profiles=(
        MachineProfilePrice(
            name="l4", gpu_product="NVIDIA-L4", accelerator_per_hour=1.0, core_per_hour=0.1, memory_gib_per_hour=0.0
        ),
        MachineProfilePrice(
            name="a100-80gb",
            gpu_product="NVIDIA-A100-SXM4-80GB",
            accelerator_per_hour=3.0,
            core_per_hour=0.1,
            memory_gib_per_hour=0.0,
        ),
    ),
    placements=(Placement(name="test", region_multiplier=1),),
)
PARAMETERS = PlannerParameters(placement="test", max_containers=4, fallback_floor=2, cooldown_s=900)
L4_HOURLY = 1.0 + 4 * 0.1


def receipt(identity, cell=L4, *, shape=SHAPE, p99=100.0, concurrency=8, rps=10.0, beside=()):
    return Receipt(
        identity=identity,
        cell=cell,
        container_shape=shape,
        concurrency=concurrency,
        request_shape=RequestShape(operation=Operation.ENCODE, items_per_request=1),
        throughput=Throughput(requests_per_second=rps),
        latency=Percentiles(p99_ms=p99),
        co_residents=tuple(beside),
        source=SourceRef(
            uri=f"receipts/{identity}/{cell.machine_profile}/{shape.memory_mib}/{concurrency}/{'+'.join(beside)}"
        ),
    )


def boot(identity, seconds, warmth=Warmth.COLD_WITH_BOUND, cell=L4, *, shape=SHAPE, borrowed=False):
    return BootEvidence(
        identity=identity,
        cell=cell,
        container_shape=shape,
        warmth=warmth,
        boot_seconds=seconds,
        outcome=BootOutcome.SUCCEEDED,
        borrowed=borrowed,
        source=SourceRef(uri=f"boots/{identity}/{cell.machine_profile}/{warmth.value}"),
    )


def busy(*identities, hours=6, requests=10):
    return Demand(
        start=START,
        end=START + timedelta(hours=24),
        series=tuple(
            DemandSeries(
                identity=identity,
                operation=Operation.ENCODE,
                hours=tuple(
                    DemandHour(start=START + timedelta(hours=hour), requests=requests) for hour in range(hours)
                ),
            )
            for identity in identities
        ),
    )


def planned(bounds, receipts, boots, demand=None, current=None):
    service_levels = ServiceLevels(
        identities=tuple(
            IdentityServiceLevel(identity=identity, level=ServiceLevel(come_alive_p95_s=bound, warm_p99_ms=250))
            for identity, bound in bounds.items()
        )
    )
    return plan(service_levels, receipts, boots, PRICES, demand or busy(*bounds), current, PARAMETERS)


def run(bounds, receipts, boots, demand=None):
    return planned(bounds, receipts, boots, demand).report


def shared_lane(*identities):
    return Plan(
        placement="test",
        lanes=(
            PlanLane(
                name="shared",
                queue_pool="shared",
                bundle="default",
                cell=L4,
                container_shape=SHAPE,
                concurrency=8,
                max_containers=4,
                envelope=Envelope(floor_min=1, floor_max=1),
            ),
        ),
        identities=tuple(
            PlanIdentity(identity=identity, lane="shared", warmth=Warmth.PINNED) for identity in identities
        ),
    )


def cold_cost(hours=6):
    return L4_HOURLY * hours * 2 * SCALE


def test_a_zero_bound_lever_saves_nothing_under_its_bound_and_is_priced_at_the_bound_it_permits():
    report = run({"org/warm": 0}, [receipt("org/warm")], [boot("org/warm", 95.2, borrowed=True)])
    (lever,) = report.come_alive_levers
    assert (lever.identity, lever.cell, lever.container_shape, lever.warmth) == (
        "org/warm",
        L4,
        SHAPE,
        Warmth.COLD_WITH_BOUND,
    )
    assert (lever.bound_s, lever.come_alive_s, lever.permitted_bound_s) == (0, 95.2, 96)
    assert lever.come_alive_basis is ComeAliveBasis.BORROWED_BOOT_EVIDENCE
    assert lever.saving_under_bound == 0
    assert lever.monthly_cost == pytest.approx(cold_cost())
    assert lever.saving_at_permitted_bound == pytest.approx(L4_HOURLY * MONTH - cold_cost())


def test_a_finite_bound_lever_saves_the_same_under_its_bound_and_at_the_bound_it_permits():
    report = run({"org/slow": 60}, [receipt("org/slow")], [boot("org/slow", 240.0)])
    (lever,) = report.come_alive_levers
    assert (lever.bound_s, lever.permitted_bound_s) == (60, 240)
    assert lever.saving_under_bound == pytest.approx(L4_HOURLY * MONTH - cold_cost())
    assert lever.saving_at_permitted_bound == lever.saving_under_bound


@pytest.mark.parametrize("seconds", [0.0, 0.4])
def test_a_sub_second_restore_permits_a_one_second_bound(seconds):
    report = run({"org/warm": 0}, [receipt("org/warm")], [boot("org/warm", seconds, Warmth.SNAPSHOT_COLD)])
    assert report.come_alive_levers[0].permitted_bound_s == 1


def test_levers_rank_by_the_saving_under_the_bound_then_at_the_permitted_bound():
    bounds = {"org/always-warm": 0, "org/small-saving": 60, "org/big-saving": 60, "org/zero-quiet": 0}
    receipts = [receipt(identity) for identity in bounds]
    boots = [boot(identity, 120.0) for identity in bounds]
    demand = Demand(
        start=START,
        end=START + timedelta(hours=24),
        series=(
            *busy("org/small-saving", hours=8).series,
            *busy("org/big-saving", hours=2).series,
            *busy("org/always-warm", hours=1).series,
            *busy("org/zero-quiet", hours=4).series,
        ),
    )
    report = run(bounds, receipts, boots, demand)
    ranked = [(lever.identity, lever.saving_under_bound > 0) for lever in report.come_alive_levers]
    assert ranked == [
        ("org/big-saving", True),
        ("org/small-saving", True),
        ("org/always-warm", False),
        ("org/zero-quiet", False),
    ]
    savings = [lever.saving_at_permitted_bound for lever in report.come_alive_levers]
    assert savings[2] > savings[3]


def test_a_cell_that_misses_its_warm_bound_offers_no_lever():
    report = run(
        {"org/model": 60}, [receipt("org/model", p99=400.0), receipt("org/model", A100)], [boot("org/model", 240.0)]
    )
    assert report.come_alive_levers == ()
    assert report.identity("org/model").chosen.cell == A100


def test_a_cold_posture_that_is_not_cheaper_offers_no_lever():
    report = run({"org/model": 60}, [receipt("org/model")], [boot("org/model", 240.0)], busy("org/model", hours=24))
    assert report.identity("org/model").chosen.warmth is Warmth.PINNED
    assert report.come_alive_levers == ()


def test_an_undeclared_bound_offers_no_lever():
    report = run({"org/model": None}, [receipt("org/model")], [boot("org/model", 240.0)])
    assert report.identity("org/model").chosen.warmth is Warmth.COLD_WITH_BOUND
    assert report.come_alive_levers == ()


def test_a_measured_come_alive_under_the_bound_is_a_choice_and_not_a_lever():
    report = run({"org/model": 60}, [receipt("org/model")], [boot("org/model", 30.0)])
    assert report.identity("org/model").chosen.warmth is Warmth.COLD_WITH_BOUND
    assert report.come_alive_levers == ()


def test_each_cell_shape_and_warmth_is_one_lever_at_its_cheapest_receipt():
    receipts = [
        receipt("org/model", concurrency=8),
        receipt("org/model", concurrency=16),
        receipt("org/model", shape=WIDE),
    ]
    boots = [
        boot("org/model", 240.0),
        boot("org/model", 30.0 + 60.0, Warmth.SNAPSHOT_COLD),
        boot("org/model", 300.0, shape=WIDE),
    ]
    report = run({"org/model": 60}, receipts, boots)
    keys = [(lever.container_shape, lever.warmth) for lever in report.come_alive_levers]
    assert sorted(keys, key=str) == sorted(
        [(SHAPE, Warmth.COLD_WITH_BOUND), (SHAPE, Warmth.SNAPSHOT_COLD), (WIDE, Warmth.COLD_WITH_BOUND)], key=str
    )
    assert len(keys) == len(set(keys))
    by_key = {(lever.container_shape, lever.warmth): lever for lever in report.come_alive_levers}
    assert by_key[(SHAPE, Warmth.COLD_WITH_BOUND)].monthly_cost == pytest.approx(cold_cost())
    assert by_key[(WIDE, Warmth.COLD_WITH_BOUND)].monthly_cost == pytest.approx((1.0 + 8 * 0.1) * 6 * 2 * SCALE)
    assert by_key[(SHAPE, Warmth.SNAPSHOT_COLD)].permitted_bound_s == 90


@pytest.mark.parametrize("reverse", [False, True])
def test_a_lever_is_priced_at_the_cheapest_receipt_of_its_cell_and_shape(reverse):
    receipts = [receipt("org/model", rps=0.01), receipt("org/model", concurrency=16, rps=0.02)]
    report = run(
        {"org/model": 60},
        receipts[::-1] if reverse else receipts,
        [boot("org/model", 240.0)],
        busy("org/model", requests=1),
    )
    (lever,) = report.come_alive_levers
    assert lever.monthly_cost == pytest.approx(L4_HOURLY * 6 * (1 / 0.02 + 1 * 2 * 900) / 3600 * SCALE)
    assert lever.saving_under_bound == pytest.approx(L4_HOURLY * MONTH - lever.monthly_cost)


@pytest.mark.parametrize(("bound", "seconds", "permitted"), [(0, 95.2, 96), (60, 240.0, 240)])
def test_an_identity_staying_beside_a_neighbour_offers_no_lever_that_loosening_its_bound_would_not_pay(
    bound, seconds, permitted
):
    bounds = {"org/a": bound, "org/b": 0}
    receipts = [
        receipt("org/a"),
        receipt("org/b"),
        receipt("org/a", beside=["org/b"]),
        receipt("org/b", beside=["org/a"]),
    ]
    boots = [boot("org/a", seconds)]
    result = planned(bounds, receipts, boots, current=shared_lane("org/a", "org/b"))
    assert result.plan == shared_lane("org/a", "org/b")
    assert result.report.identity("org/a").chosen.monthly_cost == pytest.approx(L4_HOURLY * MONTH)
    assert result.report.identity("org/a").chosen.co_residents == ("org/b",)
    assert result.report.come_alive_levers == ()
    loosened = planned({**bounds, "org/a": permitted}, receipts, boots, current=result.plan)
    assert loosened.plan == result.plan
    assert loosened.report.identity("org/a").chosen.warmth is Warmth.PINNED
    assert loosened.report.monthly_cost == pytest.approx(result.report.monthly_cost)


@pytest.mark.parametrize(("bound", "seconds", "permitted"), [(0, 95.2, 96), (60, 240.0, 240)])
def test_an_identity_alone_on_its_current_lane_offers_the_lever_that_loosening_its_bound_pays(
    bound, seconds, permitted
):
    bounds = {"org/a": bound, "org/b": 0}
    receipts = [receipt("org/a"), receipt("org/b")]
    boots = [boot("org/a", seconds)]
    result = planned(bounds, receipts, boots, current=shared_lane("org/a"))
    assert result.report.identity("org/a").lane == "shared"
    (lever,) = result.report.come_alive_levers
    assert (lever.identity, lever.permitted_bound_s) == ("org/a", permitted)
    loosened = planned({**bounds, "org/a": permitted}, receipts, boots, current=result.plan)
    saving = result.report.monthly_cost - loosened.report.monthly_cost
    assert saving == pytest.approx(L4_HOURLY * MONTH - cold_cost())
    assert lever.saving_at_permitted_bound == pytest.approx(saving)
    assert lever.saving_under_bound == (0 if bound == 0 else pytest.approx(saving))


def test_a_lever_saves_under_its_bound_what_its_own_boot_unlocks_and_at_its_permitted_bound_all_that_bound_unlocks():
    receipts = [receipt("org/model"), receipt("org/model", shape=WIDE)]
    boots = [boot("org/model", 240.0), boot("org/model", 300.0, shape=WIDE)]
    report = run({"org/model": 60}, receipts, boots)
    by_shape = {lever.container_shape: lever for lever in report.come_alive_levers}
    assert by_shape[WIDE].monthly_cost == pytest.approx((1.0 + 8 * 0.1) * 6 * 2 * SCALE)
    assert by_shape[WIDE].saving_under_bound == pytest.approx(L4_HOURLY * MONTH - by_shape[WIDE].monthly_cost)
    assert by_shape[WIDE].saving_at_permitted_bound == pytest.approx(L4_HOURLY * MONTH - cold_cost())
    assert by_shape[SHAPE].saving_under_bound == pytest.approx(L4_HOURLY * MONTH - cold_cost())
    assert [lever.container_shape for lever in report.come_alive_levers] == [SHAPE, WIDE]