Skip to content

Read memory and cores one way, and add --memory-budget and --max-cores to the US build commands - #1853

Merged
anth-volk merged 11 commits into
mainfrom
claude/1-8-1-host-resources-budget
Dec 3, 2026
Merged

anth-volk merged 11 commits into
mainfrom
claude/1-8-1-host-resources-budget

Conversation

@anth-volk

Copy link
Copy Markdown
Contributor

Fixes #1852

Tracker task 1.8 · 1 (project 15, "US build memory, CPU and run time"): the build reads the machine's memory and usable cores one way, on macOS, in Linux cgroups and in Modal stage containers, and the US build commands take --memory-budget (a share of total memory or an amount in GiB, default 0.75) and --max-cores.

What changes

One reading of the machine: microcosm.build.host_resources (the module #1775 added, now extended):

  • Total memory: physical memory (SC_PHYS_PAGES × SC_PAGE_SIZE, hw.memsize on macOS), or a Linux cgroup memory limit where lower (v2 memory.max, v1 memory.limit_in_bytes). Inside a Modal stage container: the requested memory_mib from MICROCOSM_MODAL_RESOURCES, because none of the repository's resource classes sets a memory limit and gVisor reports the host's memory unless the sandbox's cgroup has a smaller limit.
  • Usable cores: os.process_cpu_count(), or a cgroup CPU quota rounded up where lower. Inside a Modal container: the class's cpu_limit, or its cpu request when it sets none, rounded up.
  • Free memory: psutil's available memory, bounded by the room left under a cgroup limit (limit less memory in use, not counting inactive file pages) or a Modal request.
  • A MICROCOSM_MODAL_RESOURCES that is set but is not the runner's request stops sizing; telemetry still leaves it out, as before.

The options. The national release, local-area release, base and ACS staging commands:

  • copy --memory-budget and --max-cores into MICROCOSM_MEMORY_BUDGET and MICROCOSM_MAX_CORES before numpy loads, only when run as a command, so child processes and pool workers inherit them; an option that disagrees with its variable is refused;
  • declare both in their parsers and, right after parsing, refuse an option argparse read that never reached its variable (an abbreviation such as --max-core), then print the resolved budget to standard error.

--memory-budget is a bare number in (0, 1] (a share of total memory) or <number>GiB; a bare number above 1, and a budget above total memory, are refused.

What they change before 2.6 · 2's planner exists:

  • --max-cores N holds every thread variable (OpenMP, OpenBLAS, MKL, BLIS, vecLib, numexpr, LOKY_MAX_CPU_COUNT) at or below N (tools/us_build_thread_environment.py):
    • the count is the smaller of N and MICROCOSM_THREADS or the release default; a variable already above N is lowered (each level of an OMP_NUM_THREADS list); MICROCOSM_THREADS above N is refused;
    • N binds only below the machine's CPU count, which no library's own count exceeds. Only then does a command with no count of its own fill N (the ACS local-release command and the dry run of the release's checks (tools/dry_run_us_release_gates.py) fill the release default, held to N), does a blank variable become N, and is a value that is not a count refused. A cap that binds nothing changes nothing;
    • through LOKY_MAX_CPU_COUNT it also holds the QRF's joblib.cpu_count() sizing.
  • The post-export worker formula uses the budget in place of 0.75 × memory, and at most --max-cores cores (N − 1 workers at most, at least one; a count set in MICROCOSM_POST_EXPORT_WORKERS is used as given). Its record gains memory_budget and max_cores.
  • tools/dry_run_us_release_gates.py, which runs the release in its own process, copies the release argv's two options into their variables before loading it, sets the thread variables before numpy under a cap that binds, and exits 64 (no report) when a setting is refused.
  • Writing the resolved budget to the manifests and telemetry is task 1.8 · 4.

Readers moved to the module (same values where the figure existed before): telemetry's host.cpu_count and host.memory_bytes, modal_resources (moved from telemetry_identity), the UK tool's memory check, the bakeoff tool's memory wait, tools/measure_qrf_leaf_memory.py's CPU count, and the Route A driver's avail_ram_bytes, supervisor and sampler. The Route A tools and the thread setup load the module from its file (tools/load_host_resources.py) so they never import microcosm.build, which loads numpy and pandas (about 1 s and 155 MB here). route_a.sh exports both settings when route_a.env sets them.

Readers left as they are, and why:

  • microcosm.fit.qrf.default_predict_workers() keeps joblib.cpu_count(): microcosm-fit cannot import microcosm-build, joblib already applies the same affinity and quota rules, and --max-cores reaches it through LOKY_MAX_CPU_COUNT. Switching it would change QRF thread counts on Modal with no option given.
  • The Modal stage runner's _cpu_visibility() imports nothing from the workspace's packages by design, and its raw readings are the evidence of what gVisor reports.
  • worker_identity.py's schema-4 os.cpu_count() reproduces how old records resolved an unset worker count.
  • Per-process memory readings (resident memory, peaks) belong to tasks 1.8 · 2 and 1.8 · 3.

Output bytes (decision 1.1 · 1)

With neither option nor variable set, no thread variable, thread count or worker count changes:

  • set_build_thread_environment takes the old path exactly when MICROCOSM_MAX_CORES is unset; the release default is still min(os.cpu_count(), 16). The existing process-level tests (test_release_tool_keeps_its_default_without_the_thread_setting, test_a_started_command_sets_nothing_without_the_thread_setting, test_importing_the_release_tool_still_applies_its_default) are unchanged and run in CI.
  • The post-export formula's default budget is Fraction(3, 4) of total memory rounded down, equal to the old memory * 3 // 4 for every non-negative integer (tested on three sizes), so worker counts and caps are unchanged; the existing formula tests are unchanged and run in CI.
  • No data path reads the new module: it sizes threads and workers, and the post-export results are joined in batch order whatever the worker count (Score US post-export requests in the shared worker pool by default #1775's one-worker against many-worker test).
  • With --max-cores at or above the machine's CPU count, nothing changes either (tested: no variable filled, blank variables kept). Below the count a build would otherwise use, it can change output bytes on Linux (OpenBLAS splits sums between its threads); the option's help and the docs say so.
  • Review round 3 traced the no-option path end to end for the four commands and the dry run and found nothing the build writes or the libraries see that differs from 1c22fba08; what is new is the budget line on standard error and the two record keys.

Tests

Run locally with only the tests this PR adds or changes, each selected by node ID; CI runs the rest.

  • 5dc83ee3d: tests that fail before the change, with placeholder functions so they reach their assertions. 63 tests (155 cases) in test_host_resources.py, test_us_build_tool_thread_defaults.py, test_us_build_resource_options.py (new), test_route_a_supervisor_memory.py (new), test_us_post_export_pool.py, test_telemetry_identity.py, test_run_uk_cut.py, test_bakeoff_us_support_mix_tool.py and test_route_a_driver.py: 138 cases failed and 17 passed (the cases whose expected result is "nothing changes"). Failures were on assertions, except the four commands' start-of-run check (AttributeError: the commands ran into their work) and one parser case (argparse exit 2).
  • dae739de7: the implementation. 154 passed and 1 failed: the driver's free-memory test had a wrong expected text, and could not tell the old reading from the new; rewritten to check a figure only the new reading gives, it passes.
  • Review fixes, each a commit of tests that fail before the fix and then the fix: 5ca2857e7 / 02c8c6d7b (6 tests), 2b94e55d2 / f84885c2d (5), 2690935e0 / 028c5e143 (4). Each set failed before its fix and passes after.
  • Ruff check passes on every changed file; ruff format was applied to new and rewritten files only (bakeoff_us_support_mix.py and build_us_acs_multispine_base.py are unformatted on main and left so).

Review

Three rounds of adversarial review, each by a fresh reviewer in its own clone.

  • Round 1 (1 medium, 4 low):
    • medium, fixed: the dry run of the release's checks (tools/dry_run_us_release_gates.py) refused any release argv with --max-cores or --memory-budget (it runs the release in-process, so the options never reached their variables), which would stop Route A at its dry-run stage, which runs by default;
    • low, fixed: --max-cores could raise thread counts (a cap above the CPU count filled N; the ACS local-release start-up pre-empted the release default);
    • low, fixed (docs): the runbook said a Modal plan could pass the options as tool options (plans take only each tool's listed flags), and several texts said --max-cores held every worker pool;
    • low, left: a Modal plan's MICROCOSM_MAX_CORES / MICROCOSM_MEMORY_BUDGET are not checked when the plan loads, as MICROCOSM_THREADS is, so a bad value stops the stage only when the tool starts, after its inputs are staged. Checking them would change the Modal runner, which the task's decisions ruled out.
  • Round 2 (1 medium, 3 low, in round 1's fixes):
    • medium, fixed: under a cap that binds nothing the release filled blank OpenMP and MKL variables, which changes torch's thread count and so the calibration's exact weights;
    • low, fixed: the dry run's refused settings exited 2, which Route A reads as the dry run's AT-RISK verdict (at-risk checks only, no certain failure), leaving a stale report;
    • low, fixed: an OMP_NUM_THREADS list was refused under any cap;
    • low, fixed in the dry run and documented for the export probe: programs that load numpy before the release tool got the cap only for libraries loaded afterwards. The probe loads numpy at import and takes no options.
  • Round 3 (3 low; no-option path and round 2's fixes confirmed):
    • low, fixed: the dry run set thread variables early under a cap that binds nothing when MICROCOSM_THREADS was set;
    • low, fixed: under a binding cap a blank variable took the release default rather than N;
    • low, left (documented): the cap's threshold is os.cpu_count(), which Python 3.13 takes from PYTHON_CPU_COUNT; with PYTHON_CPU_COUNT below N the cap binds nothing although the libraries, which do not read it, may use more than N threads.

Bugs left, by priority: two low ones above (the Modal plan check, PYTHON_CPU_COUNT). Also by design: under a cap that binds, a command with no count of its own fills N, which can raise torch above its own physical-core count when N lies between that count and the CPU count; the stages that are not build commands (base preflight, probe sample, probe) load numpy before any thread variable is set.

Environment variables

No secrets, workflows, Docker images or Terraform change. Three plain environment variables are read; none is a secret.

  • MICROCOSM_MEMORY_BUDGET, MICROCOSM_MAX_CORES
    • Providers: the operator, through a build command's --memory-budget / --max-cores (copied into the variable before numpy loads), the shell, Route A's route_a.env, or a Modal plan's env.
    • Wiring: route_a.sh exports each when route_a.env sets it; supervise.py starts each stage with its own environment plus the stage config's env; the base tool's step processes get os.environ.copy(); pool workers inherit at spawn; the dry run copies them from the release argv; the Modal runner adds a plan's env to the tool's environment (tool_environment in tools/modal_us_stage_plan.py), whose name checks accept MICROCOSM_* names.
    • Unverified: not run on Modal or in a Route A run.
  • MICROCOSM_MODAL_RESOURCES
    • Provider: the Modal stage runner only, from the plan's resource class (tool_environment(resources=plan.resources)); a plan may not set it and the runner removes any value the container holds.
    • Unverified: what os.cpu_count() reports inside a Modal gVisor container, which decides whether a plan's MICROCOSM_MAX_CORES binds there (the Modal reading of cores and memory itself comes from this variable, not from the container). A stale value left in an operator's shell would reach Route A's stages, where a malformed one stops them at start.

What the merge waits for

  • Earlier tasks: none. 1.8 · 1 is the first task of substage 1.8, has no dependencies, and the substage depends on none.
  • Base: main; it builds on no other task's branch. Score US post-export requests in the shared worker pool by default #1775 (1.4 · 7), whose host_resources.py this extends, is already merged.
  • Overlap: tasks 1.8 · 2 and 1.8 · 3 change telemetry_identity.py and the release, local-area and base tools; whichever PR merges later resolves the overlap. Their sessions were told which functions this PR changes.
  • Not in this PR: writing the resolved budget and thread plan to the manifests and telemetry (1.8 · 4).

🤖 Generated with Claude Code

anth-volk and others added 10 commits December 2, 2026 01:20
…s, and the build's resource options

Tests for tracker task 1.8 · 1, with placeholders for the names they call:

- microcosm.build.host_resources: the machine's memory and CPU count, cgroup
  limits, a Modal stage container's requested memory and CPU, free memory
  bounded by a cgroup or Modal request, and --memory-budget / --max-cores
  (parsing, the budget, copying the options into MICROCOSM_MEMORY_BUDGET and
  MICROCOSM_MAX_CORES, and refusing an option that did not reach its variable);
- tools/load_host_resources.py, which loads the module from its file;
- the thread variables held at or below --max-cores, in every US build command;
- the post-export worker formula reading the budget and --max-cores;
- telemetry's host figures, the UK tool's memory check, the bakeoff tool's
  memory wait and the Route A driver, supervisor and sampler reading memory
  through host_resources.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…es in the US build commands

Tracker task 1.8 · 1. microcosm.build.host_resources is now the one reading
of the machine:

- total memory: physical memory, or a Linux cgroup memory limit where lower;
  inside a Modal stage container, the requested memory_mib from
  MICROCOSM_MODAL_RESOURCES (Modal sets no memory limit and gVisor reports
  the host's memory);
- usable cores: os.process_cpu_count(), or a cgroup CPU quota rounded up
  where lower; inside a Modal container, the class's CPU limit, or its
  request when it sets none, rounded up;
- free memory: psutil's figure, bounded by the room left under a cgroup
  limit or a Modal request;
- the operator's --memory-budget (a share of total memory from 0 to 1,
  default 0.75, or an amount such as 24GiB) and --max-cores.

The national release, local-area release, base and ACS staging commands copy
both options into MICROCOSM_MEMORY_BUDGET and MICROCOSM_MAX_CORES before numpy
loads, hold every thread variable at or below --max-cores, and check after
parsing that what argparse read reached the variables. The post-export worker
formula uses the budget in place of 0.75 x memory and at most --max-cores
cores. Without either option no thread or worker count changes.

Telemetry's host figures (same values), the UK tool's memory check, the
bakeoff tool's memory wait, the QRF leaf-memory measurement and the Route A
driver, supervisor and sampler read through the module; the Route A tools and
the thread-variable setup load it from its file (tools/load_host_resources.py)
so they do not load numpy. route_a.sh exports both settings from route_a.env.

The driver's free-memory test now checks a figure only the bounded reading
gives, in place of a text match whose expected text lacked a quote mark.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nd a cap that binds nothing

From review round 1 of tracker task 1.8 · 1:

- the release-gate dry run must copy the release argv's --max-cores and
  --memory-budget into their variables before the release tool loads, as
  the release command does, or it refuses an argv the release accepts;
- a --max-cores at or above the machine's CPU count must fill no thread
  variable, so it never raises a count above what a library would use; the
  ACS local-release command's start-up, under a cap that binds, uses the
  release default it later imports rather than N.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…l no thread variable for a cap that binds nothing

Fixes from review round 1 of tracker task 1.8 · 1:

- tools/dry_run_us_release_gates.py runs the release in its own process, so
  the release's own start-up never copied --max-cores and --memory-budget into
  their variables and its check refused them; the dry run now copies them
  before the release tool and numpy load, and refuses a bad value with its
  usage error.
- set_build_thread_environment fills a command's thread variables from
  --max-cores alone only when N is below the machine's CPU count, which no
  library's own count exceeds, so a cap at or above every count changes
  nothing. The ACS local-release command's start-up, under a cap that binds,
  starts from the release default it applies later (capped_default_count),
  held to N, instead of N.
- Docs, the option's help, route_a.env.example and the changelog say that a
  Modal plan sets the variables in its env (its options take only each
  tool's listed flags), and that --max-cores limits the post-export formula,
  not a worker count named in MICROCOSM_POST_EXPORT_WORKERS or the
  POPULACE_FIT_* variables. A budget's line says it came from the option or
  the variable.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…r a cap, and the dry run's refusals and early thread setup

From review round 2 of tracker task 1.8 · 1:

- a cap at or above the CPU count keeps a blank thread variable, as the
  release default does; one below fills it;
- each level of an OpenMP list is held to the cap; a value that is not a
  count is refused only when the cap binds;
- the release-gate dry run returns 64 (no report) and removes a stale report
  when the release's resource options are refused, instead of argparse's 2,
  which Route A takes as AT-RISK;
- under a cap that binds, the dry run sets the thread variables before it
  loads numpy, as the release command does; without one it sets none.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… refuse the dry run's bad settings with its no-report exit

Fixes from review round 2 of tracker task 1.8 · 1:

- set_build_thread_environment treats --max-cores as binding only below the
  machine's CPU count. Only a cap that binds fills a blank variable or
  refuses a value that is not a count; each level of an OMP_NUM_THREADS list
  is held to the cap. A cap at or above the CPU count no longer fills blank
  OpenMP or MKL variables, which left torch at its own count and so the
  calibration's exact weights unchanged.
- tools/dry_run_us_release_gates.py returns 64 (no report) and removes a stale
  report when the release's resource settings are refused, instead of
  argparse's 2, which Route A reads as AT-RISK. Under a cap that binds it
  sets the thread variables before numpy loads, as the release command does;
  without a cap it sets none, as before.
- Docs, help and changelog: a cap that binds nothing changes nothing; the
  formula plans at least one worker; the export probe loads numpy before the
  release tool, so the cap reaches only what it loads afterwards; the three
  readings outside host_resources are named.
- Two tests no longer depend on the test machine's CPU count or leave thread
  variables in the test process.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…cap, and the dry run under a cap that binds nothing

From review round 3 of tracker task 1.8 · 1:

- under a cap below the CPU count, a blank thread variable (the library's
  own count) is held to N, not set to the release default;
- max_cores_binds says whether MICROCOSM_MAX_CORES is below the CPU count;
- the release-gate dry run applies a thread setting early only under a cap
  that binds; with one that binds nothing it waits for the release tool to
  load, as without the cap.

The Route A driver's free-memory test now reports 1 KiB free, so it holds on
any machine with more than 1 MiB of memory.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… threads early only under a cap that binds

Fixes from review round 3 of tracker task 1.8 · 1:

- Under a cap below the CPU count, a blank thread variable, which leaves the
  library its own count, becomes N rather than the release default an unset
  variable takes; the thread setting still fills a blank with itself.
- max_cores_binds says whether MICROCOSM_MAX_CORES is below the CPU count.
  The release-gate dry run sets thread variables before numpy only when it
  is, so a thread setting under a cap that binds nothing is applied when the
  release tool loads, as without the cap.
- The dry run's docstring and help say exit 64 also covers refused
  --memory-budget / --max-cores settings. The runbook and route_a.env.example
  name the stages that load numpy before any thread variable is set, and the
  runbook says PYTHON_CPU_COUNT below N makes the cap bind nothing (Python
  3.13 applies it to os.cpu_count(); the libraries do not read it).
- The dry-run test starts each run from blank variables, as a fresh process
  would.

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

test_local_builder_parser_accepts_every_allowlisted_option compiles the ACS
staging builder's _parse_args into a namespace of its own; the parser now adds
--memory-budget and --max-cores through host_resources (standard library only),
so the namespace holds it. The test also checks that a plan's argv leaves both
unset: a Modal plan sets the two variables in its env.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Empty commit. The push of 699ebba produced no GitHub Actions run after 30
minutes, so this push sends a fresh pull-request event (Anthony's go-ahead,
2 December).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@anth-volk anth-volk closed this Dec 2, 2026
@anth-volk anth-volk reopened this Dec 2, 2026
… module

The stand-in script copies the ACS staging builder's _parse_args, which now
adds --memory-budget and --max-cores through host_resources; it loads the
module from its file (tools/load_host_resources.py), as the build commands
do, so the runner's parser check and the synthetic stage run can call it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@anth-volk
anth-volk marked this pull request as ready for review December 3, 2026 00:51
@anth-volk
anth-volk merged commit e9c3bca into main Dec 3, 2026
13 checks passed
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.

Read memory and cores one way, and add --memory-budget and --max-cores to the US build commands

1 participant