Skip to content

Request 1 CPU and 12 GiB for the API v2 segment worker - #1163

Merged
anth-volk merged 3 commits into
mainfrom
claude/v2-segment-worker-resources-5e568e
Nov 30, 2026
Merged

anth-volk merged 3 commits into
mainfrom
claude/v2-segment-worker-resources-5e568e

Conversation

@anth-volk

Copy link
Copy Markdown
Contributor

Fixes #1162

Same-Repository Draft PR Check

  • This PR is a draft.
  • This PR is from a branch in PolicyEngine/policyengine-sim-api, not a personal fork.

Changelog

  • This PR adds a changelog fragment, changelog.d/<name>.<type>.md, whose type is breaking, added, changed, fixed or removed. (changelog.d/v2-segment-worker-resources.changed.md)

Summary

The API v2 simulation app's US segment worker now requests cpu=1.0, memory=12288 (1 CPU, 12 GiB) instead of cpu=8.0, memory=32768. Both places that run a segment take SEGMENT_WORKER_RESOURCES from src/modal/v2_worker_resources.py, so both change:

  • the SimulationSegmentWorkerUS class, which has memory snapshots and is what segmented US simulations and their cliff calculations spawn;
  • the plain run_single_simulation_segment_us function, kept without snapshots for callers still on older code.

SimulationWorkerUS and SimulationWorkerUK keep 8 CPUs and 32 GiB. The diff is the two values with a comment, their test and a changelog fragment.

At list price, 1 CPU and 12 GiB cost $0.0000397 per container-second, against $0.000176 for 8 CPUs and 32 GiB: about 23 %.

Measured in production (read-only)

Modal's per-container statistics (modal function stats --container) for every production segment container from 25 to 30 Nov: 1,480 calls in 947 containers, all with 8 CPUs and 32 GiB. Each call's year comes from Cloud Logging. Every production segment in that period ran in a shadow comparison of a run_simulation app job, and those jobs' records name the year.

CPU (cores, per-container percentiles: median over containers [highest]):

Containers n Container's p50 Container's p90 Container's p99
Restored from a snapshot, 2026 407 1.02 [1.29] 1.30 [1.48] 1.35 [1.88]
Before the snapshots, years built into the image 306 0.92 [1.14] 1.15 [1.48] 1.33 [1.52]
Before the snapshots, a year not built in 226 1.03 [1.11] 1.28 [1.39] 1.35 [1.55]

A segment keeps about one core busy, and about 1.3 cores in its busiest tenth. Modal's Resources guide says cpu= is a guaranteed minimum: by default a container may use up to 16 more cores when its host has them free, and Modal bills the larger of the request and the use.

Memory (GiB, per-container p99: median over containers [90th percentile; highest]):

Containers n Memory p99
Restored from a snapshot, 2026 407 2.63 [2.84; 5.01]
Before the snapshots, years built into the image 306 2.48 [2.65; 6.36]
Before the snapshots, a year not built in 226 4.68 [5.95; 8.61]
  • The image holds prebuilt year files and stored baselines for 2025–2027 only. A budget window run on 27 Nov calculated 2027–2035, and its containers served 3–5 calls for 3–4 different years each. The highest reached 8.61 GiB (years 2027, 2029, 2030 and 2035).
  • Memory grows as a container serves more calls: restored 2026 containers reached 2.53 GiB at the median for one call, 3.25 GiB for six, and 5.01 GiB in the one container that served 21.
  • Modal gives each container's p50, p90 and p99, not its maximum, so a true peak may be a little higher.

Cliff calculations, measured locally

A US national simulation's cliff calculation is split into region groups like its run (cliff_results._calculation, should_segment_cliff_calculation), so it runs on this worker too. None of the production calls above was one. On a Mac, one region group's segment (state/ar+state/nc+state/nd) planned as a cliff calculation (resolve_cliff_output_plan) was compared with the same segment planned as a report run's reform child, each in a fresh process with the US models preloaded as the snapshot hook loads them (macOS physical footprint):

Case Plain Cliff
2026, year file built in the call 4.28 GiB 4.30 GiB
2028, year file built in the call 4.28 GiB 4.30 GiB
2026, year file present 2.79 GiB 3.15 GiB

So a cliff calculation adds up to 0.36 GiB (13 %). Adding that and 0.3 GiB for the snapshot hook's warm-up calculation to the 8.6 GiB above gives about 9.3–10.1 GiB, and 12 GiB leaves 1.9–2.7 GiB above that.

Not measured

A 1-CPU container's speed for the API v2 segment. The run_simulation app's segment worker runs the same calculation, and on Modal testing its 1-CPU restored containers calculated at 0.98–1.03 times the 8-CPU medians (#1126). The production check below covers it here.

Checked in production after deploy

With the read-only queries used for the before-and-after setup times of the API v2 workers (Cloud Logging operation records, Cloud Trace, Modal's container records and its "memory snapshot" log lines), compare restored segment containers after this deploys with the restored 8-CPU figures from 27–30 Nov:

  • enqueue to models loaded: 2.4 s median (n = 203);
  • reform segment calculation (stage12_calculation): 103.3 s median (n = 219), compared under the same reform and year where possible.

Each median may be at most 10 % slower. A deploy takes new snapshots, so containers that take a snapshot are reported separately; #1147's warm-up calculation also deploys with or before this change and makes a restored container's first calculation faster, so the comparison says which deploys each side includes. Also read modal function stats for the class: memory p99 should stay below 12 GiB, and no call should end as "Evicted (over memory request)".

Environment variables

The change reads and declares none. Both changed workers keep secrets=worker_secrets: Modal secrets stage12-evaluation-gcp-credentials, pe-uk-private-hf-read-token and stage12-evaluation-runtime. The tests assert them.

Testing

  • Red: tests/test_v2_worker_classes.py::test_the_v2_simulation_workers_are_classes_restored_from_memory_snapshots and ::test_plain_functions_keep_the_workers_for_callers_on_older_code fail at 3f297a10 (8.0 and 32768 against 1.0 and 12288).
  • Green: both pass after 3aa626bf.
  • ruff check and ruff format --check on the two changed Python files; the changelog fragment check (.github/scripts/check-changelog-fragment.sh); scripts/run_quality_guards.py (all guards pass, file length included).
  • Only the changed tests were run, not the full suite.

🤖 Generated with Claude Code

anth-volk and others added 3 commits December 1, 2026 02:17
Production statistics for 1,480 segment calls show one core busy, with
bursts to about 1.3, and up to 8.6 GiB in a container that calculated
several years without a prebuilt year file and baseline. The class and
the plain function share the options, so both tests fail until the
request changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SimulationSegmentWorkerUS and the plain run_single_simulation_segment_us
take SEGMENT_WORKER_RESOURCES, which asked for 8 CPUs and 32 GiB. In
production a segment keeps about one core busy, and Modal lets a
container use free cores beyond its request.

Containers that calculated several years without a prebuilt year file
and baseline reached 8.6 GiB (per-container p99; Modal reports no
maximum). Adding 0.3 GiB for the snapshot hook's warm-up calculation,
and 0.36 GiB (13 %) measured locally for a cliff calculation's segment,
which runs on this worker too, gives about 9.3-10.1 GiB. 12 GiB leaves
1.9-2.7 GiB above that.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@anth-volk
anth-volk marked this pull request as ready for review November 30, 2026 23:51
@anth-volk
anth-volk merged commit 464a0fe into main Nov 30, 2026
28 checks passed
@anth-volk
anth-volk deleted the claude/v2-segment-worker-resources-5e568e branch November 30, 2026 23:51
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.

Request the measured CPU and memory for the API v2 segment worker

1 participant