diff --git a/changelog.d/1125.changed.md b/changelog.d/1125.changed.md new file mode 100644 index 000000000..c7e31c9d3 --- /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 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 db6153ab3..072f1a982 100644 --- a/projects/policyengine-simulation-executor/src/modal/app.py +++ b/projects/policyengine-simulation-executor/src/modal/app.py @@ -531,8 +531,14 @@ def run_simulation( @app.cls( image=simulation_image, - cpu=8.0, - memory=32768, + # A segment child calculates one region group in one process, so it + # 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=10240, timeout=3600, retries=0, max_containers=300, @@ -586,8 +592,9 @@ def run_simulation( @app.function( image=simulation_image, - cpu=8.0, - memory=32768, + # The same resources as SimulationSegmentWorker. + cpu=1.0, + memory=10240, timeout=3600, retries=0, max_containers=300, 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_modal_bundle_image.py b/projects/policyengine-simulation-executor/tests/test_modal_bundle_image.py index 1de495d65..4f6f158be 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,28 @@ 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 region group in one process, so it + # 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. 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 +162,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"] 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