Repository navigation
Keep the US post-export scorer's household batches in files, not memory - #1751
Merged
Merged
Conversation
These tests fail on the current scorer, which keeps every household batch frame of the written H5 in memory until it closes: - the open scorer, and a scorer after a pass, reaches no Frame or DataFrame; - each batch is in its own file, read back each time it is scored, and equals the batch selected in memory byte for byte (negative zero, NaN, 32-bit integers, booleans and strings included); - a batch file whose bytes changed, or that is missing, is refused; - closing or dropping the scorer removes its batch files, a given batch directory that exists is refused and left as it was, and a scorer that cannot write its batches leaves no files; - certification gives the scorer a directory in the build's --out and placing the build removes one an earlier attempt left. Two tests pass now and must keep passing: the values scored from the batch files equal those of the in-memory batches, and (engine workflow) each batch read back from a fixture H5 written by the engine's writer equals that batch as the scorer selected it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The scorer loaded the written H5 once and kept every household batch frame in memory until it closed, through the whole of certification: about 0.39 GiB on the published 57,240-household national file and about 2.1 GiB (estimate) on a 352,932-household national export. It still loads the H5 once, bound to its sha256, selects the batches and checks the batching premises. It now pickles each batch to its own file, records the sha256 of the bytes written and frees the frames. _PostExportBatchFiles reads a batch back when it is indexed and refuses a file whose bytes no longer hash to the recorded digest, so only bytes this process wrote are unpickled. _score is unchanged: it iterates _batches() as before, and each batch is read just before its engine is built. The scorer makes its directory new (refusing one that exists) or uses a new temporary one, and removes it when it closes, is freed or the interpreter exits. Certification keeps the files in post_export_batches/ in the build's --out; placing a build for certification removes one a killed attempt left. Scored values are unchanged: unpickling gives back the same tables, dtypes, index, values, weights and strata. POST_EXPORT_SCORING_METHOD is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
These tests fail on the previous commit: - A scorer that was neither closed nor freed must not remove a newer scorer's batch files once its directory's parent was emptied and reused, as placing a build for certification does: each scorer makes its own directory inside a given parent (batch_parent), keeps what the parent already holds, and removes the parent only if it made it. - A pass frees each batch's frame before it reads the next batch's file. - A batch file that changed or cannot be read raises PostExportBatchFileError, and the SSI and Medicare IRMAA held-out reports raise it instead of recording the measurement as unavailable. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Each scorer makes its own uniquely named directory inside a parent (batch_parent; certification passes --out/post_export_batches), and its cleanup removes only that directory, and the parent once empty if it made it. Before, the cleanup removed whatever was at a fixed path, so a scorer freed late could remove a newer scorer's live files there. - _score reads batches by position and drops each frame once its engine is built. enumerate keeps its last item, so the previous batch's frame was still alive while the next file was read. - A batch file that changed or cannot be read raises PostExportBatchFileError, which the SSI and Medicare IRMAA held-out reports raise instead of recording the measurement as unavailable. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…iew round 2) Fails on the previous commit, which removes the whole post_export_batches directory: placing a build for certification must remove only the scorer directories an earlier attempt left there. Also tightens tests that passed for the wrong reason or compared too little: - The unwritable-batch test now keeps the exception, and so the half-made scorer, alive while it checks, so only the scorer's own cleanup can have removed the files (garbage collection ran its finalizer before). - A new test fills the disk on the second batch: neither the part-written file nor the one before it is left. - assert_same_batch also compares the schema, link tables, record of weight changes and metadata. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ound 2) Placing a build for certification removed the whole post_export_batches directory, so anything a person had put there went too. It now removes only the directories named post-export-batches-*, which only scorers make, and keeps the rest. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
anth-volk
marked this pull request as ready for review
November 26, 2026 19:01
This was referenced Nov 26, 2026
juaristi22
added a commit
that referenced
this pull request
Nov 27, 2026
…the e4d46c93 feed and the new CI caps (#931) Main took six pull requests since 4b78a9b: #1736 (LCFS spending drawn as a budget and a donor's mix, with a decile receipt beside ONS Family Spending, the UK feed re-pinned to Chronicle e4d46c93, and register renewals), #1779 (the CI caps from the slowest legs of 19 to 26 Nov), #1772 (a batch file's error skips the fallback), #1767 (New Zealand declares its receipt's take-up cap), #1751 (the US post-export H5 path) and #1723 (US QRF identity tests). The engine lock does not move. Eight files conflicted. - uk_runtime/lcfs_consumption.py: main's receipt imports and the clip's docstring, which now names the mix donor and keeps this branch's count of distinct source households over every clone. Main's new decile and range-step receipts count the frame's support in distinct source households (source_household_id), so on the clone-first spine a source household's clones count once. - uk_runtime/graph_kernels.py: the LCFS stage keeps this branch's full list under the derived binding rule and adds microcosm.fit.donors, which main's draw computes through. - uk_runtime/calibration_run.py: main added the LCFS decile gate to the spine gate scope, which this branch keeps in gate_scopes.py; the gate goes there. - .github/workflows/test.yml: main's caps and their basis, and this branch's note on the after-merge job's cap. - The gate contract pins, the coverage manifest and the H2 fixture are recomputed on the merged tree. The feed. Main moved the UK feed from Chronicle 15040ea5 to e4d46c93 and recorded that every 15040ea5 row is byte-identical there, the 487 new rows being ONS Family Spending. This branch's own Scottish Band D facts take the new pin stamps with their 32 rows unchanged, as main's re-pin did for its vendored resources; the local membership takes main's feed label and DESNZ keys beside this branch's two UC seat deferrals. The vendor tool's check against the e4d46c93 artifact did not run here, which lacks that artifact. Checks: with policyengine-uk hidden, as in CI's engine-free job, the tests main's six pull requests change, the LCFS, consumption, Band D, vendoring and feed tests, the UK graph's binding rules, the nesting, stage health, battery, contract, gate and coverage tests (2,245 pass; the vendoring test that rebuilds the vendored resources from a local copy of the pinned feed refuses this machine's 15040ea5 copy, as it should, and skips in CI, which has none); the US coverage report and pins (--check); CI's test plan verify; and on the engine, H2 parity, the UK graph, the consumption resources and the release input coverage (56 pass). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
US post-export scoring (
_HouseholdBatchedPostExportScorerintools/build_us_fiscal_refresh_release.py, run by certification) loaded the written H5 once and kept every household batch frame in memory until it closed. That memory was held for the whole of certification:Now the scorer writes each batch to its own file. The process that scores holds no frame of the written H5 between batches: only the H5's path and digest, and each batch file's path and digest.
Scored values are unchanged, and
POST_EXPORT_SCORING_METHODis unchanged.What changes
_assert_post_export_batching_premises. Then_PostExportBatchFilespickles each batch to its own file, records the SHA-256 of the bytes written, and the frames are freed._batches()returns that object. Indexing it reads one batch back. The file's bytes must hash to the recorded digest before they are unpickled, so only bytes this process wrote are loaded. A file that changed or cannot be read raisesPostExportBatchFileError._score: it reads each batch by position and drops the frame once the batch engine is built. The engine holds its own copy of the data. The loop no longer usesenumerate, becauseenumeratekeeps its last item and would keep the previous batch's frame alive while the next file is read.PostExportBatchFileError, so a refused batch file stops certification in every stage.post-export-batches-*) insidebatch_parent, or inside the system's temporary directory when none is given.weakref.finalizeremoves that directory, and the parent if the scorer made it and it is empty, when the scorer closes, is freed or the interpreter exits.batch_parent=<--out>/post_export_batches._place_build_for_certificationremovespost-export-batches-*directories left there by a killed attempt, and keeps anything else._assert_post_export_batching_premises;_concatenate_post_export_parts;US_POPULATION_AGGREGATE_VARIABLESandPOST_EXPORT_BASELINE_BRANCH_READERS;post_export_scoringblock.Results are unchanged
test_batches_read_from_their_files_equal_the_written_h5s_batches): each batch read back from its file equals that batch of a fixture H5 written by the engine's own writer, as the scorer selected it in memory before. That covers the schema, tables, dtypes, index, every numeric column byte for byte, object values and types, weights and weight kinds, strata, link tables, the record of weight changes, and metadata.Memory, disk and time
Measured on the published national file (57,240 households, 29 batches of 2,000):
Tests
Red-green:
e332ad11e,d2d87ab1candfc61fe40fadd the tests and fail on the commits before them.120a69537,755099456and65538ee0amake them pass.Run locally, each by node ID:
packages/microcosm-build/tests/engine_free/us/test_us_post_export_batch_files.py(one parametrized test has two cases);packages/microcosm-build/tests/engine_free/us/test_us_post_export_scoring.py::test_main_and_writers_never_build_a_whole_pool_simulation, changed: it now pinsbatch_parent=in certification's scorer call;packages/microcosm-build/tests/engine_workflow/us/test_us_post_export_scoring.py::test_batches_read_from_their_files_equal_the_written_h5s_batches.Other existing tests were not run locally; CI runs them. The second reviewer read them and predicted none fail. That includes:
HouseholdRowMap(self.scorer._batches(), ...);WrittenH5Scorer;scorer._batches().Review
Two rounds of adversarial review.
Known limits
--outat once remove each other's batch files. Running them at once was already unsupported, because placement also removes the other run's release-directory files.tools/probe_us_post_export.pyandtools/sweep_us_post_export_scoring.pykeep their batch files in the system's temporary directory. A process killed without running its exit handlers leaves them there: SIGKILL, or the sweep'sos._exitwhen a worker passes 7 GiB. Only certification removes an earlier attempt's files.--maximum-microsim-batch-size, or a file no larger than one batch): while opening, and while reading each pass, the process briefly holds the pickled bytes beside the frame. That is still below the whole-file engine that follows.HouseholdRowMap) reads every batch back at once, as many frames as the scorer held before.Overlap with #1722 and #1725
Both change this scorer.
git merge-treewith each branch, as of their current heads, merges without conflicts.#1725's
_baseline_forcallsself._batches()only to check that the scorer is open. It wraps the merged baseline pass inexcept Exceptionand then lets each consumer score its own plan. Whichever of the two PRs merges second should addexcept PostExportBatchFileError: raisebefore that handler. Without it, a refused batch file still stops certification, but one pass later and after a log message saying each consumer will score its own plan.Deployment and environment variables
No CI workflow, Dockerfile, Terraform or Modal code changes, and no environment variable is read or added.
🤖 Generated with Claude Code