Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions changelog.d/us-post-export-request-descriptors.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The US release tool's `_record_post_export_request_descriptors` returns every request each post-export stage makes in the engine-free post-export dry run as a picklable `PostExportRequest` (`microcosm.build.us_runtime.post_export_requests`), for later work that runs the requests in worker processes; the release itself still takes only the baseline plans and reform-pass counts. A descriptor holds the stage, the request's place in the order the serial scorer runs it, the variable, period and `map_to`, and for a reform request the description its reform is rebuilt from (the smoke probe, the reform-validation spec, or a cumulative OBBBA state's parameter changes) and which of the stage's reformed simulations asked for it. Reform validation, like the smoke check, now hands the dry run those descriptions instead of building reforms, so the dry run needs no policyengine-core, and it refuses a reform it cannot describe or pickle. It also refuses a key asked of a reformed simulation after the stage opened the next one, which the scorer refuses once it has finished that simulation, so such a stage fails the plan before the export rather than certification's scoring after it. The plan no longer builds reform-validation reform classes before the export, as it already did not for the smoke check. Nothing records the descriptors: the plan in `calibration_diagnostics.json`, each stage's output and each stage's reform-pass count stay the same, which a test pins on a fixture. `RECORDS_REQUESTS_ONLY` moves from `reform_coverage_smoke` to the new module. `tools/sweep_us_post_export_scoring.py` records through the descriptors and rebuilds each reform in the worker that scores it.
13 changes: 13 additions & 0 deletions docs/us-release-build-rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,19 @@ one Microsimulation over the whole file.
2025 state taxes after 2027 keys. It checks for the
`md.msde.ccs.payment.informal.rates` error and warns if that error is absent;
it does not require the upstream bug to persist.
- `_record_post_export_request_descriptors` runs the same dry run and returns
every request, baseline and reform, as a picklable `PostExportRequest`
(`microcosm.build.us_runtime.post_export_requests`), which a process started
with `spawn` can unpickle; the release itself still takes only the baseline
plans and reform-pass counts. A reform request holds
the description its stage builds the reform from: the smoke probe, the
reform-validation spec, or a cumulative OBBBA state's parameter changes. The
dry run builds no reform, and refuses a reform it cannot describe or pickle.
It also refuses a key asked of a reformed simulation after its stage opened
the next one: the scorer finishes a reformed simulation when the next opens
and refuses it further keys, so the plan fails before the export instead.
Nothing records the descriptors; the guard sweep below rebuilds each reform
from its descriptor in the worker that scores it.
- Each batch engine keeps policyengine-us's projection of the release year to
its last CPI-U year (2035), because the plans request keys in later years
(reform validation reaches past 2025). The build's own simulations hold
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
"""Picklable descriptions of the US release's post-export scoring requests.

The release tool (``tools/build_us_fiscal_refresh_release.py``) learns what
each post-export consumer will ask policyengine-us for by running the consumer
once against engine-free recorders (its "dry run"). The consumers call
``simulate(None)`` for the baseline simulation and ``simulate(reform)`` for a
reformed one, then ``.calculate(variable, period, map_to=...)`` on each. The
release takes only each consumer's baseline plan and reform-pass count from the
dry run; the tool's ``_record_post_export_request_descriptors`` returns every
request of the same dry run as a :class:`PostExportRequest`, which a process
started with the ``spawn`` method can unpickle and run.

A built reform cannot cross a process boundary: ``Reform.from_dict`` returns a
class defined inside a function, which pickle cannot name. So a consumer whose
``simulate`` carries :data:`RECORDS_REQUESTS_ONLY` hands it the description it
builds the reform from instead, and :func:`build_post_export_reform` rebuilds
the reform from that description with the consumer's own builder:

- a :class:`~microcosm.build.us_runtime.release_input_coverage.ReformCoverageProbe`
(the reform-coverage smoke check), rebuilt by
``reform_coverage_smoke._build_reform``;
- a :class:`~microcosm.build.us_runtime.reform_validation.ReformValidationSpec`
(a reform-validation row simulated on its own), rebuilt by its
``build_reform``;
- a :class:`ParameterChangesReform` (one of reform validation's cumulative
OBBBA states), rebuilt by ``reform_validation._build_parameter_reform``.

These types live here, in an importable package, so that a spawned process
can unpickle them however the release tool was started. The tests and the
guard sweep load the tool by file path, outside ``sys.modules``, where a type
defined in it cannot even be pickled. A release runs the tool as a script,
where a spawned process would unpickle such a type only by running the whole
tool again as its ``__mp_main__`` module (which a process started with
``spawn`` does to the parent's main script in any case).

Neither type can be hashed, because a reform description holds dictionaries;
key a request's results by its ``(consumer, position)``.
"""

from __future__ import annotations

import copy
import pickle
from collections.abc import Mapping
from dataclasses import dataclass
from typing import Any

from microcosm.build.us_runtime.reform_validation import ReformValidationSpec
from microcosm.build.us_runtime.release_input_coverage import ReformCoverageProbe

__all__ = [
"RECORDS_REQUESTS_ONLY",
"ParameterChangesReform",
"PostExportReform",
"PostExportRequest",
"build_post_export_reform",
"check_post_export_reform",
]

#: Set to ``True`` on a ``simulate`` that only records the requests it is
#: asked for and never applies a reform (the release tool's engine-free dry
#: run). A consumer then hands it the description of each reform (see the
#: module docstring) in place of the built reform, so recording builds no
#: reform, resolves no list edit and needs no engine.
RECORDS_REQUESTS_ONLY = "records_requests_only"


@dataclass(frozen=True)
class ParameterChangesReform:
"""A reform given as a ``Reform.from_dict`` payload for country ``us``.

Reform validation scores OBBBA's provisions cumulatively from a pre-OBBBA
state; each state is one such payload. An empty payload is the baseline,
which a consumer asks for as ``simulate(None)``, so it is refused here.

It keeps a deep copy of the payload, as a ``dict``: the request records
the reform as the consumer asked for it, and no later change to the
consumer's dictionaries (which its reform specs share) reaches it.
"""

parameter_changes: Mapping[str, Any]

# Not hashable: the payload is a dictionary.
__hash__ = None # type: ignore[assignment]

def __post_init__(self) -> None:
if not isinstance(self.parameter_changes, Mapping):
raise TypeError(
"ParameterChangesReform needs its parameter changes as a mapping "
f"of parameter paths, got {type(self.parameter_changes).__name__}."
)
if not self.parameter_changes:
raise ValueError(
"ParameterChangesReform needs at least one of its parameter "
"changes; the baseline is simulate(None)."
)
try:
copied = copy.deepcopy(dict(self.parameter_changes))
except Exception as error:
raise TypeError(
"ParameterChangesReform's parameter changes cannot be copied: "
f"{type(error).__name__}: {error}"
) from error
object.__setattr__(self, "parameter_changes", copied)


#: What a post-export request records as its reform: the description the
#: consumer builds the reform from.
type PostExportReform = (
ReformCoverageProbe | ReformValidationSpec | ParameterChangesReform
)


_POST_EXPORT_REFORM_TYPES = (
ReformCoverageProbe,
ReformValidationSpec,
ParameterChangesReform,
)


def check_post_export_reform(reform: object) -> None:
"""Refuse a reform that is not a description a worker can rebuild.

The description must be exactly one of the accepted types, not a
subclass, whose module a worker might not have, and must pickle, so a
failure shows in the release's pre-export plan rather than when a request
is sent to a worker.

Raises:
TypeError: Naming the reform's type and the types accepted, or why
the description cannot be pickled.
"""
if type(reform) not in _POST_EXPORT_REFORM_TYPES:
accepted = ", ".join(kind.__name__ for kind in _POST_EXPORT_REFORM_TYPES)
raise TypeError(
"Post-export scoring records each reform as the description a worker "
f"rebuilds it from ({accepted}); got a {type(reform).__module__}."
f"{type(reform).__qualname__}. A consumer whose simulate carries "
"RECORDS_REQUESTS_ONLY must hand it a description of one of those "
"types exactly, not a built reform or a subclass."
)
try:
pickle.dumps(reform)
except Exception as error:
raise TypeError(
f"Post-export scoring got a {type(reform).__qualname__} reform "
"description that cannot be pickled for a worker process: "
f"{type(error).__name__}: {error}"
) from error


def build_post_export_reform(reform: PostExportReform) -> Any:
"""Build the reform ``reform`` describes, with its consumer's own builder.

Each builder is looked up when it is called, so this builds exactly what
the consumer builds when it scores the same description.
"""
check_post_export_reform(reform)
if isinstance(reform, ReformCoverageProbe):
from microcosm.build.us_runtime import reform_coverage_smoke

return reform_coverage_smoke._build_reform(reform)
if isinstance(reform, ParameterChangesReform):
from microcosm.build.us_runtime import reform_validation

return reform_validation._build_parameter_reform(reform.parameter_changes)
return reform.build_reform()


def _require_name(name: str, value: object) -> None:
if not isinstance(value, str):
raise TypeError(f"PostExportRequest.{name} must be a string, got {value!r}.")
if not value:
raise ValueError(f"PostExportRequest.{name} must not be empty.")


def _require_integer(name: str, value: object, *, minimum: int | None = None) -> None:
if not isinstance(value, int) or isinstance(value, bool):
raise TypeError(
f"PostExportRequest.{name} must be a Python integer, got {value!r}."
)
if minimum is not None and value < minimum:
raise ValueError(
f"PostExportRequest.{name} must be {minimum} or more, got {value}."
)


@dataclass(frozen=True)
class PostExportRequest:
"""One request a post-export consumer makes of policyengine-us.

Attributes:
consumer: The consumer's name in the release's post-export plan, such
as ``"reform_validation"``.
position: The request's place, from 0, in the order the serial scorer
runs the consumer's requests: the baseline plan's keys (scored
together in one pass, in ascending period order and then request
order), then one reform pass per reform request, in the order the
consumer first asks each reformed simulation for each key.
variable: The variable to calculate.
period: The year to calculate it for.
map_to: The entity the values are mapped to, or ``None`` for the
variable's own entity, which the engine resolves when it
calculates the variable.
reform: ``None`` for the baseline simulation; otherwise the
description the reform is rebuilt from (:func:`build_post_export_reform`).
reformed_simulation: For a reform request, which of the consumer's
``simulate(reform)`` calls made the simulation asked, numbered from
0 in call order (a simulation never asked makes no request but
keeps its number); ``None`` for the baseline.
"""

consumer: str
position: int
variable: str
period: int
map_to: str | None = None
reform: PostExportReform | None = None
reformed_simulation: int | None = None

# Not hashable: most reform descriptions hold dictionaries.
__hash__ = None # type: ignore[assignment]

def __post_init__(self) -> None:
_require_name("consumer", self.consumer)
_require_name("variable", self.variable)
_require_integer("position", self.position, minimum=0)
_require_integer("period", self.period)
if self.map_to is not None:
_require_name("map_to", self.map_to)
if self.reform is None:
if self.reformed_simulation is not None:
raise ValueError(
"A baseline PostExportRequest names no reformed simulation, "
f"got {self.reformed_simulation!r}."
)
return
check_post_export_reform(self.reform)
if self.reformed_simulation is None:
raise ValueError(
"A reform PostExportRequest names the reformed simulation that "
"asked for it."
)
_require_integer("reformed_simulation", self.reformed_simulation, minimum=0)

@property
def key(self) -> tuple[str, int, str | None]:
"""(variable, period, map_to), the release tool's ``PostExportKey``."""
return (self.variable, self.period, self.map_to)

def build_reform(self) -> Any:
"""The reform the worker's engine applies; ``None`` for the baseline."""
if self.reform is None:
return None
return build_post_export_reform(self.reform)
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from typing import Any

from microcosm.build.gates import GateResult
from microcosm.build.us_runtime.post_export_requests import RECORDS_REQUESTS_ONLY
from microcosm.build.us_runtime.release_input_coverage import (
ListParameterEdit,
ReformCoverageProbe,
Expand All @@ -38,19 +39,12 @@
)

__all__ = [
"RECORDS_REQUESTS_ONLY",
"us_reform_coverage_smoke_gate",
]

# simulate(reform_or_None) -> object with .calculate(measure, period).sum()
SimulateFn = Callable[[Any], Any]

#: Set to ``True`` on a ``simulate`` that only records the keys it is asked
#: for and never applies a reform (the release tool's post-export plan dry
#: run). The gate then passes it each probe in place of the probe's built
#: reform, so recording resolves no list edit and needs no engine.
RECORDS_REQUESTS_ONLY = "records_requests_only"


def _weighted_total(simulation: Any, measure: str, period: int) -> float:
"""Weighted population total (policyengine-us MicroSeries .sum() is weighted)."""
Expand Down Expand Up @@ -152,9 +146,10 @@ def us_reform_coverage_smoke_gate(
simulate: ``simulate(None)`` builds the baseline; ``simulate(reform)``
builds the reformed simulation. Each result answers
``.calculate(measure, period).sum()`` as a weighted total. A
``simulate`` whose :data:`RECORDS_REQUESTS_ONLY` attribute is true
only records requests: it receives each probe in place of the
probe's reform, and no reform is built.
``simulate`` whose
:data:`~microcosm.build.us_runtime.post_export_requests.RECORDS_REQUESTS_ONLY`
attribute is true only records requests: it receives each probe
in place of the probe's reform, and no reform is built.
probes: Probes to run. Defaults to the shipped manifest's probe set.
period: Default period for probes without a probe-specific period.
name: Gate name for the manifest.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -746,14 +746,29 @@ def reform_validation_payload(
incremental effect given the lines above it — so the per-line effects sum to
the bill total, matching JCT (see ``stacked_obbba_effects``). The shape
matches the calibration-diagnostics dashboard's reform_validation reader.

A ``simulate`` whose
:data:`~microcosm.build.us_runtime.post_export_requests.RECORDS_REQUESTS_ONLY`
attribute is true only records requests (the release tool's post-export
dry run). It receives each reform's description in place of the built
reform: the spec for a row simulated on its own, and a
:class:`~microcosm.build.us_runtime.post_export_requests.ParameterChangesReform`
for each cumulative OBBBA state. No reform is built.
"""
# Imported here: post_export_requests imports ReformValidationSpec.
from microcosm.build.us_runtime.post_export_requests import (
RECORDS_REQUESTS_ONLY,
ParameterChangesReform,
)

estimates = in_sample_estimates or {}
targets = in_sample_targets or {}
baseline: Any = None
baseline_totals: dict[tuple[int, str], float] = {}
obbba_specs = tuple(spec for spec in specs if _is_obbba_spec(spec))
obbba_pre_baseline_changes = _merged_parameter_changes(obbba_specs)
obbba_stacked: dict[str, tuple[float, float, float]] | None = None
records_requests_only = getattr(simulate, RECORDS_REQUESTS_ONLY, False) is True

def baseline_total(measure: str, at_period: int) -> float:
nonlocal baseline
Expand All @@ -765,8 +780,11 @@ def baseline_total(measure: str, at_period: int) -> float:
return baseline_totals[key]

def simulation_for_parameter_changes(changes: dict[str, Any]) -> Any:
reform = None if not changes else _build_parameter_reform(changes)
return simulate(reform) # type: ignore[misc]
if not changes:
return simulate(None) # type: ignore[misc]
if records_requests_only:
return simulate(ParameterChangesReform(changes)) # type: ignore[misc]
return simulate(_build_parameter_reform(changes)) # type: ignore[misc]

def released_weighted_total(
transient_simulation: Any, measure: str, at_period: int
Expand Down Expand Up @@ -849,8 +867,9 @@ def simulated_effect(
obbba_stacked = stacked_obbba_effects()
return obbba_stacked.get(spec.id, (None, None, None))
base = baseline_total(spec.budget_measure, spec.period)
reform = spec if records_requests_only else spec.build_reform()
reform_total = released_weighted_total(
simulate(spec.build_reform()), spec.budget_measure, spec.period
simulate(reform), spec.budget_measure, spec.period
)
raw = reform_total - base
# A counterfactual revert measures the provision as baseline − reform.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from hypothesis import strategies as st

import microcosm.build.us_runtime.release_input_coverage as coverage_module
from microcosm.build.us_runtime.post_export_requests import RECORDS_REQUESTS_ONLY
from microcosm.build.us_runtime.release_input_coverage import (
US_RELEASE_INPUT_COVERAGE_SCHEMA_VERSION,
ListParameterEdit,
Expand Down Expand Up @@ -394,7 +395,7 @@ def simulate(reform):
return _Sim(4.16e10 if reform is not None else 4.0e10)

if flag:
setattr(simulate, smoke_module.RECORDS_REQUESTS_ONLY, True)
setattr(simulate, RECORDS_REQUESTS_ONLY, True)
probes = [_probe(), _list_edit_probe()]
result = us_reform_coverage_smoke_gate(
simulate=simulate, probes=probes, period=2024
Expand Down
Loading
Loading