Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog.d/us-build-thread-environment.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
`MICROCOSM_THREADS` is the one thread setting of the US release, base, ACS multispine staging and ACS local-release commands. When it is set to a positive whole number, each command copies it, before numpy loads, into every unset or blank OpenMP, BLAS, numexpr and joblib thread variable (`OMP_NUM_THREADS`, `OPENBLAS_NUM_THREADS`, `MKL_NUM_THREADS`, `BLIS_NUM_THREADS`, `VECLIB_MAXIMUM_THREADS`, `NUMEXPR_NUM_THREADS` and `LOKY_MAX_CPU_COUNT`, and `NUMEXPR_MAX_THREADS` above 64). Variables already set keep their values. A value that is not a positive whole number stops the command, and a Modal plan that sets one is refused when it is loaded. Commands that import the release tool, such as the exact-k ladder, certification and scoring tools, read the setting at that import. Without it, the base, ACS staging and ACS local-release commands set nothing and each library chooses its own count as before (until the ACS local-release command imports the release tool, as before), and the release tool keeps its default of `min(os.cpu_count(), 16)`, which still leaves a blank variable as it is.

On Linux the OpenBLAS thread count can change output bytes, so the base command now records `MICROCOSM_THREADS` and each loaded BLAS library's thread count (`blas_threads_in_use`, from threadpoolctl) in `thread_environment`, and a resume with different values is refused, including one on a host where OpenBLAS would use a different count.
169 changes: 166 additions & 3 deletions docs/us-modal-stage-runbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,161 @@ that release's current build.
Modal, 3 November 2026: the telemetry token"); it did not cover a
container reaching the collector.

## Thread variables

`MICROCOSM_THREADS` is the one thread setting of the US build commands: the
release command (`tools/build_us_fiscal_refresh_release.py`), the base command
(`tools/build_us_puf_support_base.py`), the ACS multispine staging command
(`tools/build_us_acs_multispine_base.py`, and
`tools/_legacy/build_us_acs_multispine_base.py` when that file is run directly)
and the ACS local-release command (`tools/build_us_acs_local_release.py`). Set it
to a positive whole number to set the thread count of every numerical library
these commands use. Before numpy loads, `tools/us_build_thread_environment.py`
copies it into each of these variables that is unset or blank:

- `OMP_NUM_THREADS`: OpenMP, which scikit-learn's QRF sign classifier and torch
use. OpenBLAS and numexpr read it when their own variable is unset.
- `OPENBLAS_NUM_THREADS`: OpenBLAS, which numpy and scipy bundle on Linux. It
reads this before its older name, `GOTO_NUM_THREADS`.
- `MKL_NUM_THREADS`: Intel MKL, which torch's Linux x86-64 builds include. torch
also takes its own thread count from it, ahead of `OMP_NUM_THREADS`.
- `BLIS_NUM_THREADS`: BLIS, which nothing installed here uses.
- `VECLIB_MAXIMUM_THREADS`: Apple's Accelerate, which numpy uses on macOS.
- `NUMEXPR_NUM_THREADS`: numexpr, which pandas uses for large element-wise
arithmetic.
- `LOKY_MAX_CPU_COUNT`: the limit on `joblib.cpu_count()`, which sizes the QRF's
tree-building jobs and prediction workers while `POPULACE_FIT_N_JOBS` and
`POPULACE_FIT_PREDICT_WORKERS` are unset.

numexpr refuses a count above `NUMEXPR_MAX_THREADS`, 64 by default, and then runs
one thread, so a count above 64 also fills `NUMEXPR_MAX_THREADS`. Some libraries
use fewer threads than they are given: OpenBLAS no more than the CPUs it may run
on, and `joblib.cpu_count()` no more than CPU affinity and a CPU quota allow.

A variable that already has a value keeps it, so one library's count can still be
set directly. A setting that is not a positive whole number, such as `0`, `+4` or
`four`, stops the command before numpy loads, and a Modal plan that sets one is
refused when it is loaded. The base, ACS staging and ACS local-release commands
read the setting only when started as commands, not when imported.

The release tool's step runs on import too, as it always has, so other commands
that import it read the setting at that import. `build_us_exact_k_ladder_release.py`,
`certify_us_national_build.py`, `score_us_fiscal_targets.py` and
`score_us_release_head_to_head.py` import it before numpy loads. Code that
imports it later, such as the release-gate preflight
(`microcosm.build.us_runtime.release_gate_preflight`), applies it only to the
libraries it loads afterwards and to its child processes, and a setting that is
not a positive whole number stops it at that import.

**Without the setting.** The base, ACS staging and ACS local-release commands set
nothing, and each library chooses its own count, as before:

- OpenBLAS and OpenMP use the CPUs the process may run on (its CPU affinity).
They do not follow a CPU quota.
- scikit-learn uses the smaller of OpenMP's count and `joblib.cpu_count()`, which
is the smallest of CPU affinity, a CPU quota and `LOKY_MAX_CPU_COUNT`. When none
of those is below the logical CPU count, it uses the physical cores instead.
- torch uses the physical cores (on Apple chips, the performance cores).
- numexpr uses the online CPUs, up to 16.
- `joblib.cpu_count()`, and with it the QRF's pools, follows CPU affinity, a CPU
quota and `LOKY_MAX_CPU_COUNT`.

The ACS local-release command later imports the release tool, which then fills
the variables still unset with the release default, as before. The release tool
fills them with `min(os.cpu_count(), 16)`, as it did before for all of them but
`BLIS_NUM_THREADS`, which has no effect here. As before, it keeps a blank value;
only `MICROCOSM_THREADS` replaces a blank. Its calibration's exact weights depend
on torch's thread count, so that default stays.

**On Linux the BLAS thread count can change output bytes.** numpy's Linux wheels
use OpenBLAS, which splits a large dot product between its threads, so the order
of the additions, and with it the last bits of the sum, depends on the count.
Setting only `OMP_NUM_THREADS` has the same effect, because OpenBLAS follows it.
In container probes on 18 November 2026, whose results are not kept in the
repository (x86-64 under emulation and arm64, 10 CPUs visible, numpy 2.4.6),
`np.dot` and `@` on vectors of 20,000 to 500,000 rows gave different results at
1, 2, 4, 8 and 10 threads, and `_project_weighted_sum_to_bounds` on 150,000 rows
wrote different bytes at 1, 2 and 4 threads than at 10. These were small
synthetic checks, not full builds. The same checks on macOS, where numpy uses
Accelerate, showed no difference. The affected operations reach, for example:

- the ESI premium stage (`esi_premiums.py`), which scales a person column by a
weighted total;
- the PUF aggregate-record and source-AGI projections
(`_project_weighted_sum_to_bounds`), which write the projected amounts;
- cross-fitted calibration of the QRF sign classifier (`microcosm.fit.qrf`),
which uses `w @ ...` on row-length vectors.

So a Linux base build's output can depend on the OpenBLAS thread count. Without
the setting that count follows the host, so two hosts with different CPU counts
can write different bytes, as before. A build made with `MICROCOSM_THREADS=N` can
match one made without it only where OpenBLAS would otherwise use N threads.

**Other effects of the setting.** Because it sets `OMP_NUM_THREADS`, scikit-learn
no longer limits itself to physical cores, a CPU quota or `LOKY_MAX_CPU_COUNT`:
the sign classifier uses N threads. Its probabilities were byte-identical at 1, 2
and 4 OpenMP threads in the tests, which does not prove it for every input. torch
uses N threads too; review found no torch calculation in the base or ACS staging
commands. The QRF's pools use `joblib.cpu_count()`, at most N, and their imputed
values do not depend on those counts.

**When a change takes effect.**

- *Base command.* Its run configuration (`stage_run_context.json`) records, under
`thread_environment`, `MICROCOSM_THREADS`, `OMP_NUM_THREADS`,
`OPENBLAS_NUM_THREADS`, `MKL_NUM_THREADS`, `BLIS_NUM_THREADS`,
`VECLIB_MAXIMUM_THREADS`, `NUMEXPR_NUM_THREADS`, `POPULACE_FIT_N_JOBS`,
`POPULACE_FIT_PREDICT_WORKERS` and `PYTHONHASHSEED`. It also records
`blas_threads_in_use`: each loaded BLAS library with the thread count it uses
once numpy has loaded, as `threadpoolctl` reads it from the library. That count
does not come from the environment. threadpoolctl does not report Accelerate,
so where numpy and scipy use Accelerate, as on Apple-silicon Macs, the list is
empty.
- *A resume of the base command* compares all of these except
`POPULACE_FIT_N_JOBS` and `POPULACE_FIT_PREDICT_WORKERS`, and refuses a
difference, so a new thread setting needs a new checkpoint directory. Values
are compared as written: `007` and `7`, or a blank and an unset variable,
differ. Because
`blas_threads_in_use` is compared, a resume is also refused on a host where
OpenBLAS would use a different count. Before, with the variables unset on both
hosts, the configurations matched.
- *`POPULACE_FIT_N_JOBS`, `POPULACE_FIT_PREDICT_WORKERS` and
`LOKY_MAX_CPU_COUNT`:* the first two are recorded and not compared, and a new
value applies from the next outer stage of a resumed run. `LOKY_MAX_CPU_COUNT`
is not recorded. The QRF counts these set change no imputed value. While
`OMP_NUM_THREADS` is unset, `LOKY_MAX_CPU_COUNT` also limits the sign
classifier's OpenMP count (see "Without the setting").
- *The helper's own file* is not in the digest of the tool's code
(`builder_code_identity`), but the values it sets are recorded and compared,
except `LOKY_MAX_CPU_COUNT` and `NUMEXPR_MAX_THREADS`. Those two set only
thread counts: numexpr's maximum, the QRF pools' size, which no imputed value
depends on, and, while `OMP_NUM_THREADS` is unset, the sign classifier's
OpenMP count (see above).
- *Base runs started before this change:* they cannot resume after it. The run
configuration holds a digest that covers the tool's own file
(`builder_code_identity`), and the file changed.
- *ACS staging and ACS local-release commands:* the staging command keeps no run
configuration, so the values apply to the process that was started. The
local-release command's stage settings check does not record thread variables.

**On Modal.** A plan may set `MICROCOSM_THREADS`, because its environment
allowlist accepts `MICROCOSM_*` names, and it may set `OMP_NUM_THREADS`,
`MKL_NUM_THREADS`, `OPENBLAS_NUM_THREADS`, `NUMEXPR_NUM_THREADS` and
`BLIS_NUM_THREADS` directly. It may not set `VECLIB_MAXIMUM_THREADS`,
`LOKY_MAX_CPU_COUNT`, `GOTO_NUM_THREADS` or `NUMEXPR_MAX_THREADS`. Modal set
`OMP_NUM_THREADS`, `OPENBLAS_NUM_THREADS`, `MKL_NUM_THREADS` and
`BLIS_NUM_THREADS` to the class's CPU request in the check class (2) and in a
`base` run at `4b57d15a2` (4;
`docs/evidence/us-release-reimputation-1191/receipts/build_environment.base_0fb05b67.release_417d23ae.json`).
Because a value already set is kept, a plan's `MICROCOSM_THREADS` does not
replace those four. It fills only `VECLIB_MAXIMUM_THREADS`, `NUMEXPR_NUM_THREADS`
and `LOKY_MAX_CPU_COUNT`, and `NUMEXPR_MAX_THREADS` above 64. To change the BLAS and OpenMP counts on Modal, a plan
sets those variables directly, as before; torch follows `MKL_NUM_THREADS` before
`OMP_NUM_THREADS`, so changing only the latter leaves torch at Modal's value. No
record shows what Modal sets in the `acs-staging` class. The tools at `4b57d15a2`
and `74f5e6123`, the commits pinned by the historical base plan and the ACS
staging plan, predate the setting and ignore it.

## Resources and cost

The heavy and light classes are sized from the #974 measured peaks
Expand Down Expand Up @@ -1164,7 +1319,9 @@ digests, settings, its code identity and the thread variables it sees) and
refuses a resume whose config differs. Code newer than the pinned
`4b57d15a2` that contains the QRF thread-count change records
`POPULACE_FIT_N_JOBS` and `POPULACE_FIT_PREDICT_WORKERS` without comparing
them. Every path in the config is the same in
them, and code that reads `MICROCOSM_THREADS` also records it and the BLAS
thread counts in use, and compares them (see "Thread variables"). Every path in
the config is the same in
every attempt of the run, and so are the image, the plan's environment and
the class, so a relaunch of the same plan should resume. If Modal gave the
new container different compared thread variables, the tool would refuse,
Expand Down Expand Up @@ -1325,8 +1482,14 @@ python3 tools/modal_us_stage_plan.py compare-lineage "$RECEIPT" \
Two things are reported and never refused: the interpreter string
(`python`) and every other `thread_environment` variable whose value
differs (`thread_environment_differences`). The tool reads those variables
from its environment, Modal sets some of them itself (see "Resources"), and
whether they change the numbers has not been shown. The report says
from its environment, and Modal sets some of them itself (see "Resources").
Code that records `blas_threads_in_use` reads that one from the loaded BLAS
libraries instead. These differences are reported and not refused, although a
different BLAS or OpenMP count can change output bytes on Linux (see "Thread
variables"). `blas_threads_in_use` is an empty list for a run where numpy and scipy use
Accelerate, as on Apple-silicon Macs, and is absent from a reference made with code that does
not record it, such as `4b57d15a2`. Only the checkpoint digests compared above
show that outputs agree. The report says
`compared_outputs_match`, with `stages_compared` 2 of `stages_total` 24 and
the 22 stages it did not compare (`stages_not_compared`).

Expand Down
4 changes: 4 additions & 0 deletions packages/microcosm-build/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ dependencies = [
# emitter release needs no change here. The socket protocol carries its
# own version, and every emitter release accepts the versions in use.
"microcosm-emitter-interface>=0.16.0,<1",
# The US base command records each loaded BLAS library's thread count in
# its run configuration. scikit-learn already depends on it; 3.6 reads
# the scipy-openblas library that numpy 2 wheels bundle.
"threadpoolctl>=3.6",
]

[project.optional-dependencies]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
PRIMARY_QRF_WORKER_MODULE = "microcosm.build.us_runtime.puf_qrf_worker"
PRIMARY_QRF_INTERPRETER_PLACEHOLDER = "{python_interpreter}"
APPROVED_UV_LOCK_SHA256 = (
"b9bac3fc8ebda9e5cd5b97fb5ca62c79c7ce704f8ed3c275e42300aaaf267e3f"
"cc2ece9d97931ede6009491aeb311b728ebea7cb03cf6e2752b06fac412b02eb"
)
LEGACY_CAMPAIGN_UV_LOCK_SHA256 = (
"27f47e385cfa35e2644a37410d1804b361ad9aee123577551c8421547bda65ee"
Expand Down
Loading
Loading