From 0155a2046697a76a7cf574974168bae3530ffab0 Mon Sep 17 00:00:00 2001 From: Anthony Volk <14987227+anth-volk@users.noreply.github.com> Date: Thu, 26 Nov 2026 00:08:01 +0400 Subject: [PATCH 1/7] Expect the segment workers to request 1 CPU and 6 GiB 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 --- .../tests/test_modal_bundle_image.py | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/projects/policyengine-simulation-executor/tests/test_modal_bundle_image.py b/projects/policyengine-simulation-executor/tests/test_modal_bundle_image.py index 1de495d65..0117a3f68 100644 --- a/projects/policyengine-simulation-executor/tests/test_modal_bundle_image.py +++ b/projects/policyengine-simulation-executor/tests/test_modal_bundle_image.py @@ -133,23 +133,26 @@ def test_simulation_workers_are_classes_restored_from_memory_snapshots(monkeypat function_options = dict(app.app.function_calls) worker_options = { "image": app.simulation_image, - "cpu": 8.0, - "memory": 32768, "timeout": 3600, "retries": 0, "secrets": [app.gcp_secret, app.hf_secret], "enable_memory_snapshot": True, } + simulation_worker_resources = {"cpu": 8.0, "memory": 32768, "max_containers": 100} + # A segment child calculates one household group in one process, so it + # keeps one CPU busy, and it peaks at about 3.4 GiB with both countries' + # models loaded. 1 CPU and 6 GiB fit it. + segment_worker_resources = {"cpu": 1.0, "memory": 6144, "max_containers": 300} # Two classes, so the segment children keep their own container pool: a # blocking segmented parent must never share a bounded pool with the # children it waits on. assert class_options == { - "SimulationWorker": {**worker_options, "max_containers": 100}, - "SimulationSegmentWorker": {**worker_options, "max_containers": 300}, + "SimulationWorker": {**worker_options, **simulation_worker_resources}, + "SimulationSegmentWorker": {**worker_options, **segment_worker_resources}, } - # Plain functions with the same names and limits stay, without a snapshot, - # for callers still running the code from before the classes: the gateway - # while a deploy is under way, and runs that started before it. + # Plain functions with the same names and resources stay, without a + # snapshot, for callers still running the code from before the classes: + # the gateway while a deploy is under way, and runs that started before it. function_form_options = { option: value for option, value in worker_options.items() @@ -157,11 +160,11 @@ def test_simulation_workers_are_classes_restored_from_memory_snapshots(monkeypat } assert function_options["run_simulation"] == { **function_form_options, - "max_containers": 100, + **simulation_worker_resources, } assert function_options["run_simulation_segment"] == { **function_form_options, - "max_containers": 300, + **segment_worker_resources, } # The budget-window parent stays a small plain function with no snapshot. assert "enable_memory_snapshot" not in function_options["run_budget_window_batch"] From b09618ad1990e4fb9dd47b97c5d18311097647aa Mon Sep 17 00:00:00 2001 From: Anthony Volk <14987227+anth-volk@users.noreply.github.com> Date: Thu, 26 Nov 2026 00:08:50 +0400 Subject: [PATCH 2/7] Request 1 CPU and 6 GiB for the segment workers 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 --- .../src/modal/app.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/projects/policyengine-simulation-executor/src/modal/app.py b/projects/policyengine-simulation-executor/src/modal/app.py index db6153ab3..51b9e0902 100644 --- a/projects/policyengine-simulation-executor/src/modal/app.py +++ b/projects/policyengine-simulation-executor/src/modal/app.py @@ -531,8 +531,11 @@ def run_simulation( @app.cls( image=simulation_image, - cpu=8.0, - memory=32768, + # A segment child calculates one household group in one process, so it + # keeps one CPU busy. Measured on Modal, it peaks at about 3.4 GiB with + # both countries' models loaded. The memory is a request, not a limit. + cpu=1.0, + memory=6144, timeout=3600, retries=0, max_containers=300, @@ -586,8 +589,9 @@ def run_simulation( @app.function( image=simulation_image, - cpu=8.0, - memory=32768, + # The same resources as SimulationSegmentWorker. + cpu=1.0, + memory=6144, timeout=3600, retries=0, max_containers=300, From 0bc264c5cbf246f7f14e4774aa864cf1abf14287 Mon Sep 17 00:00:00 2001 From: Anthony Volk <14987227+anth-volk@users.noreply.github.com> Date: Thu, 26 Nov 2026 00:09:28 +0400 Subject: [PATCH 3/7] Add the changelog fragment for the segment worker resources Co-Authored-By: Claude Opus 5.5 --- changelog.d/1125.changed.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/1125.changed.md diff --git a/changelog.d/1125.changed.md b/changelog.d/1125.changed.md new file mode 100644 index 000000000..44c7278d8 --- /dev/null +++ b/changelog.d/1125.changed.md @@ -0,0 +1 @@ +Segment children of segmented national runs (the `SimulationSegmentWorker` class and the plain `run_simulation_segment` function) now request 1 CPU and 6 GiB instead of 8 CPUs and 32 GiB. A child calculates one household group in one process, so it uses one CPU and about 3 to 3.4 GiB, and the 20 children of a segmented national run cost about a sixth as much at list price. From c56099d013328aeea8c1aa37813d0beb4a561bd6 Mon Sep 17 00:00:00 2001 From: Anthony Volk <14987227+anth-volk@users.noreply.github.com> Date: Thu, 26 Nov 2026 00:15:35 +0400 Subject: [PATCH 4/7] Call each segment child's unit a region group, as the code does Co-Authored-By: Claude Opus 5.5 --- changelog.d/1125.changed.md | 2 +- projects/policyengine-simulation-executor/src/modal/app.py | 2 +- .../tests/test_modal_bundle_image.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/changelog.d/1125.changed.md b/changelog.d/1125.changed.md index 44c7278d8..daa0ba7d4 100644 --- a/changelog.d/1125.changed.md +++ b/changelog.d/1125.changed.md @@ -1 +1 @@ -Segment children of segmented national runs (the `SimulationSegmentWorker` class and the plain `run_simulation_segment` function) now request 1 CPU and 6 GiB instead of 8 CPUs and 32 GiB. A child calculates one household group in one process, so it uses one CPU and about 3 to 3.4 GiB, and the 20 children of a segmented national run cost about a sixth as much at list price. +Segment children of segmented national runs (the `SimulationSegmentWorker` class and the plain `run_simulation_segment` function) now request 1 CPU and 6 GiB instead of 8 CPUs and 32 GiB. A child calculates one region group (a set of states) in one process, so it uses one CPU and about 3 to 3.4 GiB, and the 20 children of a segmented national run cost about a sixth as much at list price. diff --git a/projects/policyengine-simulation-executor/src/modal/app.py b/projects/policyengine-simulation-executor/src/modal/app.py index 51b9e0902..ece01b3a5 100644 --- a/projects/policyengine-simulation-executor/src/modal/app.py +++ b/projects/policyengine-simulation-executor/src/modal/app.py @@ -531,7 +531,7 @@ def run_simulation( @app.cls( image=simulation_image, - # A segment child calculates one household group in one process, so it + # A segment child calculates one region group in one process, so it # keeps one CPU busy. Measured on Modal, it peaks at about 3.4 GiB with # both countries' models loaded. The memory is a request, not a limit. cpu=1.0, diff --git a/projects/policyengine-simulation-executor/tests/test_modal_bundle_image.py b/projects/policyengine-simulation-executor/tests/test_modal_bundle_image.py index 0117a3f68..5358578b5 100644 --- a/projects/policyengine-simulation-executor/tests/test_modal_bundle_image.py +++ b/projects/policyengine-simulation-executor/tests/test_modal_bundle_image.py @@ -139,7 +139,7 @@ def test_simulation_workers_are_classes_restored_from_memory_snapshots(monkeypat "enable_memory_snapshot": True, } simulation_worker_resources = {"cpu": 8.0, "memory": 32768, "max_containers": 100} - # A segment child calculates one household group in one process, so it + # A segment child calculates one region group in one process, so it # keeps one CPU busy, and it peaks at about 3.4 GiB with both countries' # models loaded. 1 CPU and 6 GiB fit it. segment_worker_resources = {"cpu": 1.0, "memory": 6144, "max_containers": 300} From 78c582a04c34ec8f570f97522644b848ebab03f6 Mon Sep 17 00:00:00 2001 From: Anthony Volk <14987227+anth-volk@users.noreply.github.com> Date: Thu, 26 Nov 2026 00:20:27 +0400 Subject: [PATCH 5/7] State the precompute baseline containers' own size 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 --- .../src/modal/precompute_app.py | 2 +- .../tests/test_precompute_app.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/projects/policyengine-simulation-executor/src/modal/precompute_app.py b/projects/policyengine-simulation-executor/src/modal/precompute_app.py index 8b9c1c54c..b486631be 100644 --- a/projects/policyengine-simulation-executor/src/modal/precompute_app.py +++ b/projects/policyengine-simulation-executor/src/modal/precompute_app.py @@ -10,7 +10,7 @@ Wave 1 builds the single-year US datasets (one container per year), wave 2 computes the 20 per-cohort national baselines per year (one container per -cohort, sized like the production ``run_simulation_segment`` workers). +cohort, requesting 8 CPUs and 32 GiB). Alongside both waves, one container per year and dataset filters the regional dataset that state regions load into one year file per state. See the library module for the idempotency, ``--force``, and determinism-gate diff --git a/projects/policyengine-simulation-executor/tests/test_precompute_app.py b/projects/policyengine-simulation-executor/tests/test_precompute_app.py index 055ab06ee..3dc5874f6 100644 --- a/projects/policyengine-simulation-executor/tests/test_precompute_app.py +++ b/projects/policyengine-simulation-executor/tests/test_precompute_app.py @@ -74,7 +74,7 @@ def test_every_worker_function_carries_gcp_secret(precompute_module, name): ) -def test_compute_baseline_matches_segment_worker_shape(precompute_module): +def test_compute_baseline_requests_8_cpus_and_32_gib(precompute_module): kwargs = _function_kwargs(precompute_module, "compute_baseline") assert kwargs["cpu"] == 8.0 assert kwargs["memory"] == 32768 From 2559e29dffb25a0d3f27287b54f696c4805f8124 Mon Sep 17 00:00:00 2001 From: Anthony Volk <14987227+anth-volk@users.noreply.github.com> Date: Thu, 26 Nov 2026 18:16:22 +0400 Subject: [PATCH 6/7] Expect the segment workers to request 10 GiB 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 --- .../tests/test_modal_bundle_image.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/projects/policyengine-simulation-executor/tests/test_modal_bundle_image.py b/projects/policyengine-simulation-executor/tests/test_modal_bundle_image.py index 5358578b5..4f6f158be 100644 --- a/projects/policyengine-simulation-executor/tests/test_modal_bundle_image.py +++ b/projects/policyengine-simulation-executor/tests/test_modal_bundle_image.py @@ -140,9 +140,11 @@ def test_simulation_workers_are_classes_restored_from_memory_snapshots(monkeypat } simulation_worker_resources = {"cpu": 8.0, "memory": 32768, "max_containers": 100} # A segment child calculates one region group in one process, so it - # keeps one CPU busy, and it peaks at about 3.4 GiB with both countries' - # models loaded. 1 CPU and 6 GiB fit it. - segment_worker_resources = {"cpu": 1.0, "memory": 6144, "max_containers": 300} + # keeps one CPU busy. With both countries' models loaded it peaks at about + # 3.4 GiB for a year whose dataset and baseline are baked into the image, + # and at about 6.9 GiB for a year whose dataset and baseline it builds + # itself. 1 CPU and 10 GiB fit both. + segment_worker_resources = {"cpu": 1.0, "memory": 10240, "max_containers": 300} # Two classes, so the segment children keep their own container pool: a # blocking segmented parent must never share a bounded pool with the # children it waits on. From 3d0577e0a4cc7a0fca221514a140b3c7faf99081 Mon Sep 17 00:00:00 2001 From: Anthony Volk <14987227+anth-volk@users.noreply.github.com> Date: Thu, 26 Nov 2026 18:16:22 +0400 Subject: [PATCH 7/7] Request 10 GiB for the segment workers 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 --- changelog.d/1125.changed.md | 2 +- .../policyengine-simulation-executor/src/modal/app.py | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/changelog.d/1125.changed.md b/changelog.d/1125.changed.md index daa0ba7d4..c7e31c9d3 100644 --- a/changelog.d/1125.changed.md +++ b/changelog.d/1125.changed.md @@ -1 +1 @@ -Segment children of segmented national runs (the `SimulationSegmentWorker` class and the plain `run_simulation_segment` function) now request 1 CPU and 6 GiB instead of 8 CPUs and 32 GiB. A child calculates one region group (a set of states) in one process, so it uses one CPU and about 3 to 3.4 GiB, and the 20 children of a segmented national run cost about a sixth as much at list price. +Segment children of segmented national runs (the `SimulationSegmentWorker` class and the plain `run_simulation_segment` function) now request 1 CPU and 10 GiB instead of 8 CPUs and 32 GiB. A child calculates one region group (a set of states) in one process, so it uses one CPU. With both countries' models loaded it uses about 3.4 GiB for a precomputed year (2025–2027), and about 6.9 GiB for another year, whose dataset and current-law baseline it builds itself. At list price, the 20 children of a segmented national run cost about a fifth as much. diff --git a/projects/policyengine-simulation-executor/src/modal/app.py b/projects/policyengine-simulation-executor/src/modal/app.py index ece01b3a5..072f1a982 100644 --- a/projects/policyengine-simulation-executor/src/modal/app.py +++ b/projects/policyengine-simulation-executor/src/modal/app.py @@ -532,10 +532,13 @@ def run_simulation( @app.cls( image=simulation_image, # A segment child calculates one region group in one process, so it - # keeps one CPU busy. Measured on Modal, it peaks at about 3.4 GiB with - # both countries' models loaded. The memory is a request, not a limit. + # keeps one CPU busy. Measured on Modal with both countries' models + # loaded, it peaks at about 3.4 GiB for a year whose dataset and + # baseline are baked into the image, and at about 6.9 GiB for a year + # whose dataset and baseline it builds itself. The memory is a request, + # not a limit. cpu=1.0, - memory=6144, + memory=10240, timeout=3600, retries=0, max_containers=300, @@ -591,7 +594,7 @@ def run_simulation( image=simulation_image, # The same resources as SimulationSegmentWorker. cpu=1.0, - memory=6144, + memory=10240, timeout=3600, retries=0, max_containers=300,