Skip to content

Run one warm-up calculation in the v2 US workers' snapshot hook - #1147

Draft
anth-volk wants to merge 8 commits into
v2-restore-telemetryfrom
claude/v2-snapshot-warm-up-d59487
Draft

anth-volk wants to merge 8 commits into
v2-restore-telemetryfrom
claude/v2-snapshot-warm-up-d59487

Conversation

@anth-volk

Copy link
Copy Markdown
Contributor

Fixes #1146

Stacked on #1140 (base branch v2-restore-telemetry), which edits the same snapshot hook and brings v2_app.py to its 800-line limit. Merge #1140 first.

What changes

SimulationSegmentWorkerUS and SimulationWorkerUS in the v2 app now run one warm-up calculation in their @modal.enter(snap=True) hook, after the model preload, before Modal takes the memory snapshot. A process's first calculation for a year builds the year's parameter values and SPM threshold amounts, which later calculations reuse; containers restored from the snapshot start with that done.

  • What it calculates: one segment, as a segmented national run's child receives it: the reform side of the first region group the v2 cache precomputes (state/hi, state/ia, state/wi), for the first precompute year (2026), with the Child Tax Credit base amount (gov.irs.credits.ctc.amount.base[0].amount) raised to $2,500. Its input is the precompute's own planned reform side (stage12_cache.precompute_planning.scoped_planned_simulation), given that policy and planned again with resolve_simulation_output_plan. It goes through stage12_runtime.calculate_segment, as the fan-out sends a segment to its worker.
  • Why the reform side: a v2 segment calculates one simulation. A current-law baseline planned as the precompute plans it loads the precomputed file instead of calculating, which would build no parameter values. A reform segment always calculates and never loads or computes the precomputed current-law baseline, so the warm-up does the same work before and after Give comparison-path US baselines their stored plan so they load the precomputed baselines #1138's cache fix. (Its own calculation still reads current-law values through its baseline branch; that is what builds the module-level parameter values a later call reuses.)
  • Where: new module src/modal/v2_snapshot_warm_up.py. It shares no code with the run_simulation app's warm-up (src/modal/snapshot_warm_up.py), since that app is to be removed while this one stays.
  • UK: SimulationWorkerUK's hook calls the warm-up, which returns at once. The v2 UK image holds no prebuilt UK year file, so a UK warm-up would build its dataset (about 97 s in production) before the snapshot. It waits for the UK files in the v2 cache.

Before the snapshot

Requirement How
Runs only where Modal takes a snapshot Only when MODAL_ENABLE_SNAP_RESTORE is 1; Modal runs the hook in every container that isn't restored.
No connection Only when both US model modules were preloaded (another import could fetch the data release manifest) and the image's prebuilt folder holds the group's year file in a form .py reuses, looked up exactly as the calculation looks it up. Otherwise it logs a warning and skips.
No Modal Secret The v2 calculation (calculate_simulation_frames) sets up Google Cloud credentials from the stage12-evaluation-gcp-credentials Secret before every calculation. New gcp_credentials.skip_gcp_credential_setup() sets a context variable that makes setup_gcp_credentials read and write nothing; the warm-up calculates inside it. Calls after the warm-up set up credentials as before.
No telemetry, no call number No observability is configured; the calculation gets no runtime, so no span or operation record. Nothing touches ContainerCalls, so the first call is still number 1.
Nothing left for a request Afterwards it removes the reform it added to .py's in-process simulation cache, the output file .py saved beside the year file, and the memory-threshold warnings .py's cache marked as given. It then runs gc.collect(), after the exception is handled when the calculation failed, so the calculation's reference cycles are freed before the snapshot.
Never fatal Any failure or skip logs a warning; the container starts as it would without the warm-up. The executor modules it uses are imported when it runs.
Apart from the run_simulation app The module imports nothing from the run_simulation app's modules; a test blocks them and checks that the v2 app still imports and warms up.

Snapshot-taking time (estimated, not measured)

Containers Modal takes a snapshot from start later by the warm-up: three output plannings with the preloaded US model, a 2.6 s year-file load and one reform segment's calculation on 8 CPUs. Production v2 segments on fresh containers (25–27 Nov, before snapshots) calculated in a median 31.9 s for baselines (all computed, since the cache missed) and 109.7 s for reforms, whose policies were users' own. So the estimate is about 35–110 s. The run_simulation app's warm-up added 72–97 s. #1138 doesn't change it: the warm-up never loads or computes the precomputed baseline.

The gain isn't measured for v2. The run_simulation app's warm-up made a restored container's first calculation 11–16 s faster on Modal testing, with identical results.

v2_app.py length

#1140 leaves v2_app.py at 800 lines, and this change adds two (an import and the call). The workers' CPU, memory, time and container limits move to src/modal/v2_worker_resources.py, so v2_app.py is 795 lines. All six registered option sets (three classes, three plain functions) are unchanged, checked with a script comparing them with the old literal values.

Environment variables

Variable Provider Read
MODAL_ENABLE_SNAP_RESTORE Modal's runtime (Modal 1.6.4 reads it in _container_entrypoint.py and _runtime/user_code_imports.py) In the snapshot hook, before the snapshot. Unverified on Modal: no run has seen it as 1 inside the hook yet. #1140's container_start attribute will show it.
POLICYENGINE_DATA_FOLDER Image environment: build_v2_image in src/modal/v2_images.py sets it to STAGE12_DATA_DIR Before the snapshot (prebuilt-folder lookup, bundle receipt path) and per call; the same value in every container of the image.
POLICYENGINE_BUNDLE_RECEIPT None in the v2 images (only src/modal/app.py sets it), so the receipt is read from the data folder Before the snapshot and per call.
GOOGLE_APPLICATION_CREDENTIALS_JSON, GCP_CREDENTIALS_JSON, GOOGLE_CREDENTIALS, SERVICE_ACCOUNT_JSON, GOOGLE_APPLICATION_CREDENTIALS Modal Secret stage12-evaluation-gcp-credentials, attached through worker_secrets in v2_app.py Not by the warm-up. Per call after the restore, as before.

The preload before the warm-up already reads HUGGING_FACE_TOKEN (Modal Secret pe-uk-private-hf-read-token) for .py's data release manifest fetch, as since #1142.

Tests

Red-green: failing tests first, then the change that makes them pass: 0518dc4f then 744768e7; after review round 1, 33b4bdee then d2219d22; after round 2, 18fba767 then 036dbc47.

  • tests/test_v2_snapshot_warm_up.py (new, 18 tests): the hook order in all three classes; only the US workers calculate; nothing without the snapshot flag; the segment's group, year, role, policy and plan; skips without the year file or the preloaded models; the real calculate_segment and calculate_simulation_frames, run up to the dataset load, read no Secret, write no credentials file, open no socket connection and look up the same year file as the warm-up's check; no observability and call number 1; failures only warn and clean up; a failed calculation's reference cycles are freed; no memory warning stays marked; the v2 app imports and warms up with the run_simulation app's modules blocked; a later call finds nothing the warm-up left; the reform changes a parameter of the installed US model.
  • tests/test_gcp_credentials.py: two tests for the switch.
  • tests/test_snapshot_warm_up.py: its environment-read recorder moved to tests/environment_reads.py, shared by both warm-up test files.
  • tests/test_v2_container_start.py (Report container start, call number, app and .py version on each v2 worker call #1140's): its fixture replaces the warm-up, as tests/test_modal_container_start.py does for the run_simulation app.

Run by node ID: every new test, the changed test in test_snapshot_warm_up.py, and four of test_v2_container_start.py's tests that use the changed fixture. Ruff 0.15.22 format and check, Pyright for the executor, the quality guards and the changelog fragment check pass.

Review

Three adversarial review rounds by separate reviewers.

  • Round 1: the warm-up imported the run_simulation app's warm-up module, so removing that app would have stopped the v2 app importing; the changelog fragment was missing. Both fixed, the first with a failing test.
  • Round 2: only rarer cases: after a failed calculation, garbage collection ran while the exception still held the calculation's frames; .py's cache kept memory warnings marked as given. Both fixed with failing tests.
  • Round 3: no bug. The Secret-read tests now also cover the v2 runtime and database Secrets, and the docstring says precisely what a reform segment calculates.

Not covered

  • No Modal run: the snapshot-taking delay, the gain, the snapshot size and the restored containers' memory are estimates or unmeasured. A Modal testing run would measure them.
  • No test runs the real calculation past the dataset load, or the real planning, so the rest of the real path (simulation build, ensure(), SPM provenance, Parquet output) is checked by reading the code; round 3 also ran the real planning offline with connections refused.
  • The success message is logged at INFO, which no handler prints before the snapshot, as in the run_simulation app's warm-up; only skips and failures appear in Modal's logs. A later container start of about the warm-up's length shows that it ran.
  • Warnings Python's warnings module shows once per code location, if the calculation raises any, aren't shown again in restored containers. None was found.

🤖 Generated with Claude Code

anth-volk and others added 8 commits November 28, 2026 02:27
The v2 US workers' snapshot hook is to calculate one reform segment of the
first region group the v2 cache precomputes, for the first precompute year,
only in a container Modal takes a snapshot from. The tests check that it
reads no Secret and writes no credentials file (through the real v2
calculation up to its dataset load), configures no observability, leaves the
first call's number at 1, leaves nothing in .py's cache or the prebuilt
folder, skips without the prebuilt year file or the preloaded US models, and
only logs a warning when it fails. Two tests cover a switch that makes the
Google Cloud credential setup do nothing.

The environment-read recorder moves from the run_simulation warm-up's tests
into a support module both files use.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SimulationSegmentWorkerUS and SimulationWorkerUS now calculate one segment
before Modal takes their memory snapshot: the reform side of the first
region group the v2 cache precomputes (state/hi, state/ia, state/wi), for
the first precompute year (2026), under the CTC base amount raised to
$2,500, through stage12_runtime.calculate_segment as the fan-out sends it.
A reform segment always calculates and never reads or computes a baseline,
so the warm-up does the same work with either cache outcome.

It runs only when Modal snapshots the container (MODAL_ENABLE_SNAP_RESTORE
is 1), the US models were preloaded and the image's prebuilt folder holds
the group's year file in a form .py reuses. It calculates with no
observability runtime and inside the new skip_gcp_credential_setup, so the
v2 calculation's credential setup reads no Modal Secret and writes no
credentials file before the snapshot. It removes the simulation and output
file it leaves, with the run_simulation warm-up's cleanup, now public. Any
failure only logs a warning. The UK worker's hook calls it and it does
nothing there, since the UK image holds no prebuilt year file yet.

v2_app.py would pass its 800-line limit, so the workers' CPU, memory, time
and container limits move into src/modal/v2_worker_resources.py, with the
registered options unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review round 1 found that the v2 warm-up imports the run_simulation app's
warm-up module, so removing or renaming that module would stop the v2 app
from importing. The new test blocks the run_simulation app's modules and
expects the v2 app to import and warm up; it fails today.

The real-calculation test now also checks that the warm-up's year-file
check asks the region, dataset and prebuilt-folder lookups exactly what the
calculation then asks, and that nothing opens a socket connection. A new
test checks the v2 warm-up's own reform against the installed US model.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The v2 warm-up now defines its own snapshot flag name, reform and cleanup
instead of importing them from src/modal/snapshot_warm_up.py, which goes
away with the run_simulation app while the v2 app stays. The run_simulation
app's warm-up module is back as it was. The v2 cleanup removes only what a
reform segment leaves: new entries in .py's simulation cache and new output
files in the prebuilt folder.

Also adds the changelog fragment for #1146.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review round 2 found that the warm-up collects garbage while a failed
calculation's exception still holds its frames, so the calculation's
reference cycles survive into the snapshot. It also found that .py's cache
remembers a memory warning the warm-up's calculation triggered, so restored
containers would not warn when their own calls pass that threshold. Both
new tests fail today.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The warm-up now collects garbage after its try statement, once a failed
calculation's exception and traceback are gone, so the calculation's
reference cycles are freed before Modal takes the snapshot. Its cleanup
also removes the memory-threshold warnings .py's cache marked as given
during the warm-up, so restored containers warn when their own calls pass
8, 16 or 32 GB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review round 3 found no bug. Two of its points: the Secret-read tests now
also refuse a read of the v2 runtime Secret's variables and the Modal worker
role's database URL, and the warm-up's docstring now says that a reform
segment never loads or computes the precomputed current-law baseline,
while its own calculation still reads current-law values through its
baseline branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant