Skip to content

Request 1 CPU and 10 GiB for the segment workers - #1126

Merged
anth-volk merged 7 commits into
mainfrom
claude/segment-worker-resources-574022
Nov 26, 2026
Merged

anth-volk merged 7 commits into
mainfrom
claude/segment-worker-resources-574022

Conversation

@anth-volk

@anth-volk anth-volk commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1125

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/1125.changed.md)

Summary

The segment children of a segmented national run now request cpu=1.0, memory=10240 (1 CPU, 10 GiB) instead of cpu=8.0, memory=32768, in both places that size them:

  • the SimulationSegmentWorker class, which has memory snapshots and is what segmented_national.py spawns;
  • the plain run_simulation_segment function, kept without snapshots for callers still running the code from before the classes.

SimulationWorker and the plain run_simulation keep 8 CPUs and 32 GiB. The diff touches only those resource arguments, their test and a changelog fragment; it doesn't change the snapshot hooks or the telemetry code.

The precompute app's description said its baseline containers are sized like the segment workers. They keep 8 CPUs and 32 GiB, so the description now states that size, and the test test_compute_baseline_matches_segment_worker_shape is renamed test_compute_baseline_requests_8_cpus_and_32_gib.

Why 1 CPU

A child calculates one region group (a set of states) in one process, which uses one CPU. Measured on Modal testing, 20 groups for 2026, 2 repetitions per request size, without memory snapshots:

Request Mean CPUs busy Peak memory per container Median calculation
8 CPUs, 32 GiB 0.95 3.0 GB 84 s, 71 s
1 CPU, 6 GiB 0.94 3.0 GB 82 s, 76 s

Measured with memory snapshots, and why 10 GiB

A second measurement on Modal testing deployed four segment worker classes with memory snapshots: 8 or 1 CPU, with or without a warm-up calculation in the snapshot hook (a planned change, tracker task 1.5 · 8). Each class ran 8 containers that took snapshots, then 8 restored containers. 128 calculations, no failures.

  • Starting: containers that took the snapshot started a median 52.8 s after being queued on 1 CPU, against 57.7 s on 8 CPUs (123.2 s against 145.4 s with the warm-up). Restored containers started within 1–2 s of 8-CPU ones, sooner in one batch and later in the other.

  • Calculating: on the same machine type, restored 1-CPU containers ran 0.98–1.03 times the 8-CPU medians, with or without the warm-up. Results were identical across all four classes on each machine type.

  • Memory (peak container memory, highest of 8 containers; it doesn't depend on the CPU count):

    Calculation Without warm-up With warm-up
    2026, baseline baked into the image 3.44 GiB 3.63 GiB
    2028, baseline calculated in the call 6.92 GiB 7.19 GiB

    A year outside the precomputed ones (2025–2027) builds its dataset and current-law baseline in the call, which needs more than 6 GiB. 10 GiB leaves 3.1 GiB for that case, or 2.8 GiB with the warm-up. An integer memory= is a request, not a hard limit, so a call that needs more isn't stopped by a limit.

  • Cost: CPU and memory are each billed by the larger of the request and the use. At list price, 1 CPU and 10 GiB costs about a fifth as much per second as 8 CPUs and 32 GiB. The first measurement's 1-CPU container time for the 20 groups comes to $0.084–$0.088 at that rate, against $0.36–$0.46 with 8 CPUs and 32 GiB.

Checked in production after deploy

The production check compares the medians of restored segment children's time from enqueue to models loaded, and of their calculation time, with the same medians for restored 8-CPU segment children before this change. Each may be at most 5 % slower. Snapshots are tied to the CPU type, so the 1-CPU class takes new snapshots after deploy.

Merge order

This was to merge only after the restore check (tracker task 1.5 · 3) had recorded production times for restored segment children on 8 CPUs, so that its before-and-after numbers measure the snapshots alone. That's done: 17 restored segment children from one report took a median 8.1 s from enqueue to models loaded, and calculated in a median 86.5 s under that report's reform. Those are the "before" figures for this change's production check. Calculation time depends on the reform, so the check should compare a report with the same reform where it can. When to merge is the maintainer's call.

#1124, which also edits src/modal/app.py, merged before this branch's last push; the branch merges with main without conflicts. Task 1.5 · 8's warm-up would raise a restored container's peak memory by 0.2–0.3 GiB; the measurement above covers it.

Environment variables

The change reads and declares none. Both changed workers keep secrets=[gcp_secret, hf_secret] (Modal secrets gcp-credentials in environment main, and pe-uk-private-hf-read-token, which must hold HUGGING_FACE_TOKEN), and the test asserts them.

Testing

  • tests/test_modal_bundle_image.py::test_simulation_workers_are_classes_restored_from_memory_snapshots: failed before each resource change (1 CPU and 6 GiB, then 10 GiB), passes after it.
  • tests/test_precompute_app.py::test_compute_baseline_requests_8_cpus_and_32_gib (renamed) passes.
  • Ruff 0.15.22 format and lint on the changed files, Pyright on the executor package, scripts/run_quality_guards.py, and the changelog fragment check.
  • Separate reviewers found no bugs in the first resource change, the precompute wording change or the 10 GiB change; the last review's wording corrections are in the 10 GiB commits.

🤖 Generated with Claude Code

anth-volk and others added 5 commits November 26, 2026 00:08
A segment child calculates one household group in one process. Measured
on Modal, it keeps one CPU busy and peaks at about 3.4 GiB of resident
memory with both countries' models loaded, so the 8 CPUs and 32 GiB it
requests are mostly unused. The test now expects the SimulationSegmentWorker
class and the plain run_simulation_segment function to request 1 CPU and
6 GiB, and the national workers to keep 8 CPUs and 32 GiB. It fails until
the requests change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A segment child calculates one household group in one process. Measured
on Modal, it keeps 0.9-1.0 of a CPU busy, calculates as fast with 1 CPU
and 6 GiB as with 8 CPUs and 32 GiB, and peaks at 3.0 GB without memory
snapshots and at about 3.4 GiB in a restored container that holds both
countries' models. So the SimulationSegmentWorker class and the plain
run_simulation_segment function now request 1 CPU and 6 GiB. At list
price, the 20 children of a segmented national run cost about a sixth as
much. The memory value is a request, not a hard limit.

The national workers keep 8 CPUs and 32 GiB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The precompute app's description said its baseline containers are sized
like the production run_simulation_segment workers, and its test was named
for matching them. The segment workers now request 1 CPU and 6 GiB, while
the baseline containers keep 8 CPUs and 32 GiB, so the description states
that size and the test is named for it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
anth-volk and others added 2 commits November 26, 2026 18:16
Measured on Modal testing with memory snapshots and both countries'
models loaded, a segment child peaks at about 3.4 GiB for a year whose
dataset and current-law baseline are baked into the image (2026), and at
about 6.9 GiB for a year whose dataset and baseline it builds itself
(2028). 6 GiB covers only the first. The test now expects the
SimulationSegmentWorker class and the plain run_simulation_segment
function to request 1 CPU and 10 GiB. It fails until the request changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A measurement on Modal testing ran four segment worker classes with
memory snapshots: 8 or 1 CPU, with or without a warm-up calculation in
the snapshot hook. On the same machine type, restored 1-CPU containers
calculated within 5 % of 8-CPU ones, and started within 1-2 s of them.
For 2026, whose dataset and baseline are baked into the image, peak
memory was the same on both CPU counts: about 3.4 GiB, or 3.6 GiB with
the warm-up. A 2028 calculation, run only on 1 CPU, builds its year's
dataset and baseline itself and peaked at about 6.9 GiB, or 7.2 GiB
with the warm-up. So the SimulationSegmentWorker class and the plain
run_simulation_segment function now request 10 GiB instead of 6 GiB.
At list price, 1 CPU and 10 GiB cost about a fifth as much per second
as 8 CPUs and 32 GiB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@anth-volk anth-volk changed the title Request 1 CPU and 6 GiB for the segment workers Request 1 CPU and 10 GiB for the segment workers Nov 26, 2026
@anth-volk
anth-volk marked this pull request as ready for review November 26, 2026 14:49
@anth-volk
anth-volk merged commit 8ef36e5 into main Nov 26, 2026
32 checks passed
@anth-volk
anth-volk deleted the claude/segment-worker-resources-574022 branch November 26, 2026 14:50
anth-volk added a commit that referenced this pull request Nov 26, 2026
…anch

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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 1 CPU and 10 GiB for the segment workers instead of 8 CPUs and 32 GiB

1 participant