Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions docs/us-modal-stage-runbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -923,9 +923,14 @@ against 1.6 hours locally. At the preemption rate this run saw, a 5- to
7-hour preemptible run finishes uninterrupted 1 to 3% of the time, so
non-preemptible is the practical placement for it. The Modal path works,
but it does not yet make this stage cheaper or faster. The run did show that the stage runs as one long single-core
loop over 80 chunks of households. If the chunks are independent, which
this runbook has not checked in `materialize_chunked`, fanning them out
across containers would let preemption lose one chunk instead of the run.
loop over 80 chunks of households. Two tests compare the target matrix
`materialize_chunked` returns at several `--hh-chunk` and `--batch` values
with one chunk in one batch, bit for bit: a fake-engine test in
`packages/microcosm-build/tests/engine_free/us/test_us_acs_local_sparse_cd.py`
and a real-engine test on six households in
`packages/microcosm-build/tests/engine_workflow/us/test_us_acs_local_materialize_chunks.py`.
The chunks are independent in those tests, so fanning them out across
containers would let preemption lose one chunk instead of the run.

## Route A's base stage

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@
from microcosm.calibrate import TargetSpec
from microcosm.calibrate.matrix import build_constraint_matrix
from microcosm.calibrate.target import TargetSet
from microcosm.frame import US_SCHEMA, Frame, SharedColumnChangedError
from microcosm.frame import (
US_SCHEMA,
Frame,
SharedColumnChangedError,
WeightKind,
Weights,
)
from test_support.paths import paths_for

_TEST_PATHS = paths_for("microcosm-build")
Expand Down Expand Up @@ -177,10 +183,24 @@ def _identity(digests: dict, *, households: int) -> dict:
}


def _install_fake_materialization(module, fixtures, release, monkeypatch):
"""The fake engine, with the projection and null fill as no-ops.

Returns the fake engine's ledger of the engines it builds.
"""

ledger = fixtures._install_fake_engine(
release, monkeypatch, reform_specs=fixtures._REFORMS
)
monkeypatch.setattr(
module, "project_input_only", lambda frame, **options: (frame, {})
)
monkeypatch.setattr(module, "fill_reviewed_nulls", lambda *args, **options: None)
return ledger


def _materialize(module, fixtures, release, monkeypatch, tmp_path, specs, hh_chunk):
fixtures._install_fake_engine(release, monkeypatch, reform_specs=fixtures._REFORMS)
monkeypatch.setattr(module, "project_input_only", lambda frame, **kw: (frame, {}))
monkeypatch.setattr(module, "fill_reviewed_nulls", lambda *args, **kw: None)
_install_fake_materialization(module, fixtures, release, monkeypatch)
return module.materialize_chunked(
fixtures._nested_frame(),
specs,
Expand Down Expand Up @@ -546,6 +566,327 @@ def corrupting_later_chunks(self, row, low, carrier, positions, *, name):
)


# ---------------------------------------------------------------------------
# Chunk independence: every chunk and batch size gives the one-chunk matrix
# ---------------------------------------------------------------------------

#: Copies of the nested fixture's five households in :func:`_copied_frame`.
_COPIES = 3
#: An id ``k`` of the fixture becomes ``_ID_STRIDE * k + copy``.
_ID_STRIDE = 4
_COPIED_HOUSEHOLDS = 5 * _COPIES
#: Copy 1 of the fixture's Maryland household 4 moves to New York district
#: 3601, which then has the most households of any district.
_MOVED_HOUSEHOLD_ID = _ID_STRIDE * 4 + 1
#: Declared rows that hold no value in the reference: no household lives in
#: Texas, and no Maryland tax unit of the fixture files jointly with wages.
_EMPTY_ROWS = {"cd_4801_agi", "cd_2401_joint_wages"}
#: A target whose engine variable the fake engine does not define, so it does
#: not compile and ``materialize_chunked`` drops its row (``sparse_rows``).
_UNCOMPILED_TARGET = "unmodelled_benefit_total"


def _copied_frame(fixtures) -> Frame:
"""Fifteen households: three copies of the nested fixture, ids interleaved.

- Every id ``k`` of the fixture becomes ``4k + copy``. The fake engine
computes each value from its record's own id, so the copies' values
differ.
- Group tables stay sorted by id, as ``Frame`` requires, so household rows
cycle through the copies, and a chunk boundary can fall inside a state
or a district.
- The person rows are in a fixed shuffled order, so a household's people
are not adjacent rows.
- One copied Maryland household moves to New York district 3601. 3601 then
has the most households, so chunk 0's direct check of one district row
per carrier picks 3601 rows in a one-chunk run (except for the
``cd_0601_wages`` carrier, which has only a 0601 row), but 0601 rows
for every carrier in a chunk 0 of at most ten households.
"""

base = fixtures._nested_frame()
tables = {}
for entity in base.entities:
copies = []
for copy_number in range(_COPIES):
table = base.table(entity).copy()
id_columns = [
column
for column in table.columns
if column == f"{entity}_id"
or (entity == "person" and column.startswith("person_"))
]
table[id_columns] = _ID_STRIDE * table[id_columns] + copy_number
copies.append(table)
combined = pd.concat(copies, ignore_index=True)
if entity == "person":
order = np.random.default_rng(0).permutation(len(combined))
combined = combined.iloc[order]
else:
combined = combined.sort_values(f"{entity}_id")
tables[entity] = combined.reset_index(drop=True)
household = tables["household"]
moved = household["household_id"] == _MOVED_HOUSEHOLD_ID
assert int(moved.sum()) == 1
household.loc[moved, "state_fips"] = 36
household.loc[moved, "congressional_district_geoid"] = "3601"
weights = household["household_id"].to_numpy(dtype=np.float64)
assert len(weights) == _COPIED_HOUSEHOLDS
return Frame(tables, US_SCHEMA, {"household": Weights(weights, WeightKind.DESIGN)})


#: Fake-engine variables whose values stay whole: statuses, flags, counts, ages.
_WHOLE_VALUED_VARIABLES = frozenset(
{"filing_status", "tax_unit_itemizes", "eitc_child_count", "age"}
)


def _with_fractional_fake_values(monkeypatch) -> None:
"""Divide the fake engine's amounts by 7, so most are not whole numbers.

The fake engine's own values are whole, and float32 holds those exactly;
sevenths make the float64-to-float32 conversion round. Call it once per
test: a second call would divide by 7 again.
"""

from test_support.microcosm_build import (
us_batched_target_materialization as fake_engine,
)

whole_value = fake_engine._record_value

def fractional_value(variable: str, record_id: int):
value = whole_value(variable, record_id)
if variable in _WHOLE_VALUED_VARIABLES:
return value
return value / 7.0

monkeypatch.setattr(fake_engine, "_record_value", fractional_value)


def _materialize_layout(
module,
fixtures,
release,
monkeypatch,
tmp_path,
*,
hh_chunk,
batch,
):
"""``materialize_chunked`` on :func:`_copied_frame`, with its engine ledger."""

ledger = _install_fake_materialization(module, fixtures, release, monkeypatch)
specs = (
*_parented_surface_specs(fixtures),
fixtures._variable(_UNCOMPILED_TARGET, base_variable="unmodelled_benefit"),
)
materialized = module.materialize_chunked(
_copied_frame(fixtures),
specs,
hh_chunk=hh_chunk,
batch=batch,
summary_path=tmp_path / "summary.json",
)
return materialized, ledger


def _engine_runs(household_ids, chunk_sizes, batch) -> list[tuple]:
"""Each engine the fake engine should build, in order: its pass and its
household ids.

Each chunk runs the base pass, then the one JCT reform family, each over
consecutive batches of at most ``batch`` of the chunk's households.
"""

runs = []
low = 0
for chunk in chunk_sizes:
chunk_ids = tuple(int(value) for value in household_ids[low : low + chunk])
batches = [chunk_ids[start : start + batch] for start in range(0, chunk, batch)]
runs += [("base", ids) for ids in batches]
runs += [("reform", ids) for ids in batches]
low += chunk
return runs


def _ledger_runs(ledger) -> list[tuple]:
"""The engines the fake engine built: their pass and household ids."""

return [
("base" if run["reform"] is None else "reform", run["households"])
for run in ledger.constructions
]


def _compiled_names(materialized) -> list[str]:
return [spec.name for spec in materialized.compiled_specs]


def _directly_checked_districts(materialized) -> set[str]:
"""Districts of the rows chunk 0 materialized directly, one per carrier."""

return {
entry["target"].split("_")[1] for entry in materialized.carrier_check["checked"]
}


#: (hh_chunk, batch, chunk sizes) over the fifteen households.
_CHUNK_LAYOUTS = {
"one-chunk-several-batches": (15, 4, (15,)),
"chunk-larger-than-the-frame": (24, 6, (15,)),
"three-equal-chunks": (5, 5, (5, 5, 5)),
"five-equal-chunks-one-household-batches": (3, 1, (3,) * 5),
"uneven-last-chunk": (4, 3, (4, 4, 4, 3)),
"last-chunk-of-one-household": (7, 2, (7, 7, 1)),
"uneven-batches-in-uneven-chunks": (6, 4, (6, 6, 3)),
"one-household-per-chunk": (1, 1, (1,) * 15),
}


@pytest.mark.parametrize(
("hh_chunk", "batch", "chunk_sizes"),
list(_CHUNK_LAYOUTS.values()),
ids=list(_CHUNK_LAYOUTS),
)
def test_every_chunk_and_batch_size_gives_the_one_chunk_target_matrix(
monkeypatch, tmp_path, hh_chunk, batch, chunk_sizes
) -> None:
"""The matrix ``materialize_chunked`` returns does not depend on
``--hh-chunk`` or ``--batch``.

The reference is one chunk in one engine batch. Each layout must give the
same compiled target names in order and the same CSR matrix bit for bit:
type, shape, and the dtype and bytes of ``data``, ``indices`` and
``indptr``, so also the same bytes from ``save_target_matrix``.

The surface holds district SOI rows split from geography-free carriers
under state parents, one district row with no state (the split that does
not mask by state), and a target that does not compile. Chunk 0 also
materializes one district row per carrier directly, and in a layout of
several chunks it picks other rows than the reference does.

This shows that the assembly does not depend on the chunk or batch size.
The fake engine computes each value from its own record, so it cannot
show whether policyengine-us formulas read other households;
``engine_workflow/us/test_us_acs_local_materialize_chunks.py`` runs the
real engine on a small frame.
"""

from test_support.microcosm_build.us_local_materialize_chunks import (
assert_same_surface,
)

module = _load_tool_module()
import build_us_fiscal_refresh_release as release

fixtures = _load_fixtures()
_with_fractional_fake_values(monkeypatch)
reference, reference_ledger = _materialize_layout(
module,
fixtures,
release,
monkeypatch,
tmp_path,
hh_chunk=_COPIED_HOUSEHOLDS,
batch=_COPIED_HOUSEHOLDS,
)
materialized, ledger = _materialize_layout(
module, fixtures, release, monkeypatch, tmp_path, hh_chunk=hh_chunk, batch=batch
)

# The reference ran one chunk, each engine over every household; the
# layout ran its chunks, each engine over one batch of a chunk.
household_ids = _copied_frame(fixtures).table("household")["household_id"]
assert [stat["households"] for stat in reference.chunk_stats] == [15]
assert _ledger_runs(reference_ledger) == _engine_runs(
household_ids, (15,), _COPIED_HOUSEHOLDS
)
assert [stat["households"] for stat in materialized.chunk_stats] == list(
chunk_sizes
)
assert _ledger_runs(ledger) == _engine_runs(household_ids, chunk_sizes, batch)
# Chunk 0's direct check covered other rows than the reference's when the
# layout has several chunks (0601 against 3601).
assert "3601" in _directly_checked_districts(reference)
if len(chunk_sizes) > 1:
assert _directly_checked_districts(materialized) == {"0601"}
assert materialized.carrier_check["all_equal"] is True
assert materialized.carrier_check["parent_blocks_checked"] == 3 * len(chunk_sizes)

# No part of the comparison is empty: every household holds a value, every
# declared row but the two of _EMPTY_ROWS holds one, the uncompiled target
# is dropped, and float32 rounding changed some values.
assert np.all(np.diff(reference.matrix.tocsc().indptr) > 0)
assert _UNCOMPILED_TARGET not in _compiled_names(reference)
empty = {
name
for row, name in enumerate(_compiled_names(reference))
if reference.matrix[[row]].nnz == 0
}
assert empty == _EMPTY_ROWS
assert np.any(reference.matrix.data % 1 != 0)

assert_same_surface(
reference, materialized, label=f"hh_chunk={hh_chunk}, batch={batch}"
)
save = module.cd_surface.save_target_matrix
assert save(tmp_path / "layout.npz", materialized.matrix) == save(
tmp_path / "reference.npz", reference.matrix
)


def test_the_chunk_comparison_reports_a_value_read_from_other_households(
monkeypatch, tmp_path
) -> None:
"""A control: a column that adds how many households its engine holds
differs between chunk sizes, and the comparison names the column, how
many households differ, by how much, and the first of them."""

from test_support.microcosm_build.us_local_materialize_chunks import (
assert_same_surface,
)

module = _load_tool_module()
import build_us_fiscal_refresh_release as release

fixtures = _load_fixtures()
own_columns = release._base_simulation_household_columns

def columns_reading_the_whole_batch(frame, target_specs, *, simulation, system):
columns = own_columns(frame, target_specs, simulation=simulation, system=system)
columns["tanf"] = columns["tanf"] + frame.n("household")
return columns

monkeypatch.setattr(
release, "_base_simulation_household_columns", columns_reading_the_whole_batch
)

def run(hh_chunk):
materialized, _ = _materialize_layout(
module,
fixtures,
release,
monkeypatch,
tmp_path,
hh_chunk=hh_chunk,
batch=hh_chunk,
)
return materialized

reference, chunked = run(_COPIED_HOUSEHOLDS), run(5)

with pytest.raises(
AssertionError,
match=(
r"three chunks: the target matrix differs from the reference run\.\n"
r"1 target\(s\) hold different values:\n tanf: 15 household\(s\), "
r"largest absolute difference 10\.0 \(household 0: "
),
):
assert_same_surface(reference, chunked, label="three chunks")


def test_a_state_cd_row_without_a_compiled_parent_is_refused() -> None:
"""Otherwise its per-chunk check would silently check nothing."""

Expand Down
Loading
Loading