Skip to content
Merged
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
1 change: 1 addition & 0 deletions changelog.d/v2-cache-comparison-baseline-plan.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Current-law US national baselines of comparison-path reports in the v2 report app (shadow comparisons with production jobs, and the operator-only `POST /internal/stage12/reports`) load their region-group segments from the precomputed baselines again, for the years and SPM selection the precompute stores. Since the precomputed baselines were planned as simulation runs store them, the comparison-path coordinator still gave its baseline the report's plan, so every segment computed a cache key the precompute never stores and calculated its baseline. It now gives the baseline the baseline's stored plan whenever that plan holds every column the report reads from it, as a report run's coordinator does; a report with `include_cliffs: true` and every reform keep the report's plan. A comparison report whose reform turns a labour-supply response on, and whose baseline's own policy turns none on, now splits its US national baseline into 20 region-group segments instead of calculating it in one container.
11 changes: 8 additions & 3 deletions docs/engineering/skills/observability.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,16 @@ operations:
| `stage12_country_model_load` | Loading the country model and its Python modules |
| `stage12_output_configuration` | Creating planning simulations and configuring requested output groups |
| `stage12_output_variable_resolution` | Resolving entity variables and constructing the output schema |
| `stage12_child_input_planning` | Attaching the output plan to one baseline or reform child input |
| `stage12_child_input_planning` | Choosing one baseline or reform child's output plan and attaching it to the child input |

`stage12_child_input_planning` runs once for each simulation and includes a
`simulation_role` attribute. The other planning operations include the
`country` attribute.
`simulation_role` attribute. Where a coordinator resolves a simulation's
stored plan (for each child of a report run, and for every comparison-path
report's baseline, including one that then keeps the report's plan), that
resolution records its own `stage12_country_model_load`,
`stage12_output_configuration` and `stage12_output_variable_resolution`
operations inside it. The other planning operations include the `country`
attribute.

### Annual job stage durations

Expand Down
47 changes: 32 additions & 15 deletions docs/migration/stage-12-modal-worker-foundation.md
Original file line number Diff line number Diff line change
Expand Up @@ -426,11 +426,21 @@ reads is read with them as zeros. A run that succeeded before this rule, under
a report's plan, is reused when it holds what the new report reads from it,
and refused (`reused_simulation_output_incompatible`) otherwise. The
precomputed US baselines are planned the same way, so a report run's
current-law baseline finds its precomputed files; a comparison-path call (a
current-law baseline finds its precomputed files. A comparison-path call (a
shadow comparison with a production job, or the operator-only
`POST /internal/stage12/reports`) still plans its baseline with its report's
plan, so its US national baseline no longer finds them and calculates all 20
region groups.
`POST /internal/stage12/reports`) gives its baseline the baseline's stored plan
too whenever that plan holds what the report reads from the baseline
(`side_read_plan`), so its current-law US national baseline finds them as
well; it is aggregated as a report run's side is, with any response it doesn't
store read as zeros. Its reform, and the baseline of a report reading columns
the stored plan lacks, such as the cliff columns of `include_cliffs: true`,
keep the report's plan. A US national baseline is split into region groups
from its own plan, so a comparison report whose reform turns a labour-supply
response on, and whose baseline's own policy turns none on, now splits its
baseline into 20 segments and calculates its reform in one container. A
current-law baseline's segments are then found in the precomputed files when
the report's year and SPM selection are ones the precompute stores; any other
such baseline calculates all 20 region groups.

**Shared simulation runs.** Identical requests share simulation runs, so a
report run may name a run another report run's worker is calculating (C1 · 24,
Expand Down Expand Up @@ -543,13 +553,18 @@ Planning never loads a dataset or executes a simulation. The former
`variables=("*",)` internal marker is not an output expansion mechanism and is
no longer used.

The coordinator includes the exact same output plan in the baseline and reform
child inputs. Each child adds the plan's additional variables before calling
The comparison-path coordinator gives the reform child the report's output
plan, and the baseline child either the same plan or, when it holds every
column the report reads from the baseline, the baseline's stored plan (see
"What a simulation run stores"); a report run's coordinator gives each child
its stored plan. Each child adds its plan's additional variables before calling
`Simulation.ensure()`, then verifies that every planned entity and column is
present before writing its Parquet artifact. The artifact descriptor records a
digest of the plan. After both calls complete, the coordinator checks both plan
digests and independently validates both loaded Parquet schemas before it runs
aggregate calculations. Extra columns are permitted; missing planned columns
digest of the plan. After both calls complete, the coordinator checks each
artifact's plan digest against the plan its child was given, and validates
both loaded Parquet schemas against the report's plan, with any labour-supply
response a side doesn't store read as zeros, before it runs aggregate
calculations. Extra columns are permitted; missing planned columns
fail the temporary Stage 12 report without affecting the production result.

The plan distinguishes calculated variables from columns copied directly from
Expand Down Expand Up @@ -650,9 +665,10 @@ Stage 12.

## US segmentation and baseline cache

Segmentation applies by default when both simulation inputs describe a US
national society-wide calculation and the shared output plan does not require
cliff analysis or labor-supply responses. `segmented=false` selects the
Segmentation applies by default to each simulation that describes a US
national society-wide calculation and whose own output plan does not require
cliff analysis or labor-supply responses, so a report may split one side and
calculate the other in one container. `segmented=false` selects the
single-container calculation explicitly. UK calculations, subnational US
calculations, a report's cliff analysis, and labor-supply-response
calculations also use the single-container path. A simulation run's own cliff
Expand All @@ -661,9 +677,10 @@ is (`should_segment_cliff_calculation`), and its cliff gap and share are taken
from the merged rows. `segmented=true` is accepted, but it does not override
those numerical eligibility conditions. A US national simulation run
started on its own (role `standalone`) is segmented by the same rule. It never
uses the precomputed baseline files, which serve only a report run's
current-law baseline, planned as its simulation run stores its output, so it
calculates all 20 region groups.
uses the precomputed baseline files, which serve only a report's
current-law baseline planned as its simulation run stores its output (a
report run's, or a comparison-path report's that reads no column the stored
plan lacks), so it calculates all 20 region groups.

The Stage 12 coordinator still persists only two logical simulation records:
one baseline and one reform. Each eligible logical simulation call performs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ def validate_aligned_outputs(

Raises ``ValueError`` unless the roles, evaluation, bundle, output schema
and row identities line up, and each artifact names the plan its own
simulation was given: one plan for both on the comparison path, each
simulation was given: on the comparison path the report's plan, or the
baseline's stored plan when it holds what the report reads, and each
simulation's stored plan for a report run.
"""
if baseline.role.value != "baseline" or reform.role.value != "reform":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from .output_planning import (
plan_simulation_input,
resolve_report_output_plan,
resolve_simulation_output_plan,
validate_output_frames,
with_zero_labor_supply_responses,
)
Expand Down Expand Up @@ -86,12 +87,12 @@ class CoordinatorDependencies:

The defaults are the Stage 12 database store, the private artifact
bucket, Modal child invocation, the standard aggregate builder and output
plan resolution, no telemetry runtime, the real clock and sleep, the
standard child wait window, and the report run store, the Cloud Storage
reader of reused output files, the stored output plan of each
simulation (``resolve_simulation_output_plan``) and Modal's state of a
call whose probe failed (``modal_call_state``), which only a call that
names a report run uses.
plan resolution, the stored output plan of each simulation
(``resolve_simulation_output_plan``), no telemetry runtime, the real
clock and sleep, the standard child wait window, and the report run
store, the Cloud Storage reader of reused output files and Modal's state
of a call whose probe failed (``modal_call_state``), which only a call
that names a report run uses.
"""

store: ComparisonStore | None = None
Expand Down Expand Up @@ -174,7 +175,7 @@ def plan_simulations(
"""Plan the report's outputs, then each simulation's child input.

The child's plan is ``child_output_plan``'s: the report's own plan
unless a coordinator plans each simulation by itself.
unless a coordinator gives a simulation its stored plan.
"""
with self.stage_scope(
Stage.STAGE12_OUTPUT_PLANNING,
Expand Down Expand Up @@ -203,12 +204,24 @@ def plan_simulations(

def child_output_plan(
self,
simulation: SimulationExecutionInput, # noqa: ARG002 # a coordinator that plans each simulation by itself reads it
simulation: SimulationExecutionInput, # noqa: ARG002 # a coordinator that gives a simulation its stored plan reads it
output_plan: Stage12OutputPlan,
) -> Stage12OutputPlan:
"""Return the plan ``simulation``'s child calculates: the report's own."""
return output_plan

def stored_output_plan(
self, simulation: SimulationExecutionInput
) -> Stage12OutputPlan:
"""Return the plan every run of ``simulation`` stores, whatever report names it.

It is ``resolve_simulation_output_plan``'s, or the seam's when set.
"""
resolver = self.dependencies.stored_plan_resolver
if resolver is not None:
return resolver(simulation)
return resolve_simulation_output_plan(simulation, stage_scope=self.stage_scope)

def spawn_child(
self,
simulation: PlannedSimulationExecutionInput,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,26 @@ def side_read_plan(
)


def stored_plan_holds(
stored_plan: Stage12OutputPlan, read_plan: Stage12OutputPlan
) -> bool:
"""Whether a side stored under ``stored_plan`` holds what ``read_plan`` names.

That is every entity of ``read_plan`` and every column it materializes
for that entity, such as what a report reads from a side
(``side_read_plan``).
"""
stored_columns = {
entity.entity: set(entity.materialized_variables)
for entity in stored_plan.entities
}
return all(
entity.entity in stored_columns
and set(entity.materialized_variables) <= stored_columns[entity.entity]
for entity in read_plan.entities
)


def with_zero_labor_supply_responses(
frames: Mapping[str, pd.DataFrame],
read_plan: Stage12OutputPlan,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

The comparison path's coordinator creates an evaluation child record for
each simulation, starts or reattaches its worker, then aggregates both
outputs, stores the aggregate and completes its evaluation record. A
detailed-save report run is coordinated on the run tables instead
(``report_run_coordination``).
outputs, stores the aggregate and completes its evaluation record. Its
baseline calculates the baseline's stored plan when that plan holds what the
report reads from it, so a current-law US national baseline finds the
precomputed baselines; otherwise, and for the reform, a child calculates the
report's plan. A detailed-save report run is coordinated on the run tables
instead (``report_run_coordination``).
"""

from __future__ import annotations
Expand All @@ -23,6 +26,7 @@
ComparisonSimulationRecord,
PlannedSimulationExecutionInput,
SimulationArtifactDescriptor,
SimulationExecutionInput,
SimulationRole,
Stage12OutputPlan,
)
Expand Down Expand Up @@ -55,6 +59,7 @@
report_failure,
)
from .failures import Stage12ExecutionError, Stage12FailureDetail
from .output_planning import side_read_plan, stored_plan_holds
from .simulation import descriptor_from_record

if TYPE_CHECKING:
Expand Down Expand Up @@ -86,6 +91,28 @@ def run(self, parent: ComparisonReportRecord) -> dict[str, Any]:
wait_for_children(self, calls, descriptors)
return self._aggregate_and_complete(parent, output_plan, descriptors)

def child_output_plan(
self,
simulation: SimulationExecutionInput,
output_plan: Stage12OutputPlan,
) -> Stage12OutputPlan:
"""Return the baseline's stored plan if it holds what the report reads from it.

The precompute stores each current-law US national baseline under
that plan, so the baseline's segments find the precomputed files. A
report reading columns the stored plan lacks, such as the cliff
columns, keeps ``output_plan``, the report's own, for its baseline,
and the reform always keeps it. The aggregation reads a side stored
under its own plan, with any labour-supply response it doesn't store
as zeros, as a report run's does.
"""
if simulation.role is not SimulationRole.BASELINE:
return output_plan
stored_plan = self.stored_output_plan(simulation)
if stored_plan_holds(stored_plan, side_read_plan(output_plan, stored_plan)):
return stored_plan
return output_plan

def child_descriptor(
self, role: str, result: object
) -> SimulationArtifactDescriptor:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
Stage12FailureDetail,
Stage12InputError,
)
from .output_planning import resolve_simulation_output_plan, side_read_plan
from .output_planning import side_read_plan
from .parent_claim import validate_dispatch_context
from .report_run_access import report_run_store
from .report_run_recording import (
Expand Down Expand Up @@ -421,10 +421,7 @@ def child_output_plan(
Identical requests share simulation runs, so a run's file holds what
any report reads from that side; this report reads ``output_plan``.
"""
resolver = self.dependencies.stored_plan_resolver
if resolver is not None:
return resolver(simulation)
return resolve_simulation_output_plan(simulation, stage_scope=self.stage_scope)
return self.stored_output_plan(simulation)

def _calculated_as_the_baseline(
self, simulation: PlannedSimulationExecutionInput
Expand Down
Loading
Loading