Repository navigation
Frame: share unchanged columns and check them before writing - #1683
Merged
Merged
Conversation
…rame._adopt with_weights, place and select now hand their result's tables to Frame._adopt instead of the copying constructor. Under pandas copy-on-write the result shares every unchanged column's memory with its source, in DataFrame objects of its own, so a pandas write to either frame never reaches the other; with copy-on-write off, _adopt still deep-copies. place keeps its column insert, on a shallow copy, and ignores pandas' fragmentation warning around it. The checkpoint loader takes its decoded tables without a copy, so reading a checkpoint holds one copy of the data instead of two. calibrate gains return_frame (default True). With return_frame=False the result carries frame=None, and the calibrated weights still pass the checks with_weights makes, now in Frame._checked_weight_change, which both paths call. The memory-sharing test in test_us_post_export_scoring.py now asserts that calibration leaves its input frame unchanged and unreferenced, and that the calibrated frame shares the input's column memory. The release tool's comment on del target_frame and the release build rule document describe why the tables are freed only once both references are gone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Pin a SHA-256 of each checkpoint's dataset contents (names, dtypes, shapes, bytes) for every operation, recorded with the copying code at 8afea44, so a change to an operation's output, not just to the table layout, fails the test. Add a source frame with named column axes. - Drop the loader test's writability check, which could not fail. - Assert that each result holds its own strata object. - The post-export memory test now follows a column to the array that owns its memory: the calibrated frame keeps it alive, a result built with return_frame=False does not. - refit_l0_selection refuses a selection without a frame with a clear ValueError instead of an AttributeError; L0RefitResult.frame is typed Frame | None. - _checked_weight_change and calibrate's docstrings now say that the frame constructor's table checks are not re-run without a frame. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- test_place_shares_every_table_it_does_not_change also checks the table the column moves out of; drop a strata assertion that held whatever the operations did. - CalibrationResult docs and the release tool's comment on del target_frame describe which frames share the target tables and that a frameless result carries no frame. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The sharing tests turn copy-on-write on under pandas 2.x, as the _adopt tests do; packages still allow pandas >= 2.3. - load_frame_checkpoint's docstring says that a pandas fixed-format HDF write of a loaded table can group its columns into different datasets than a write of the same table built in memory, with the same values. - _checked_weight_change's docstring states the skipped length check correctly; the release build rule document names the frames that share the target tables. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The copy-on-write-off test drops the partial-selection case, which copies rows whatever _adopt does, and also checks the strata. The test frame's strata are object dtype so that memory comparisons of them are meaningful; with_weights' sharing test checks them too. - select's docstring and comment say the selected tables escape a second copy only under pandas copy-on-write. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
calibrate's result frame now shares the calibrated frame's tables instead of copying them, so us_annual_static_aging's module docstring, its comment on releasing the calibration result, and docs/us-local-annual-projection.md no longer count a calibration copy. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Rebasing onto the Frame._adopt branch's latest commit, which rewrote the same import block, dropped `import warnings`, so Frame.place raised NameError. Restore it and re-record the calibrate and simulate parity pins, whose implementation hash covers bundle.py's source. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The calibrate.adam@1 and simulate kernels hash the source of frame/bundle.py, and calibrate's also that of calibrate/solve.py, both of which this branch changes. Re-recorded on top of the Frame._adopt branch's pins with tools/graph_parity_repin.py on arm64/darwin/py3.14: only the implementation hash and node key move; the tool checks that each kernel's direct output bytes did not. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
calibrate's return_frame option changes solve.py, whose bytes the seed protocol hashes. Regenerated with tools/repin_us_spec_engine_contracts.py and tools/spec_engine_coverage.py in the locked US environment. The US bundle itself does not change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Frame operations now record a SHA-256 digest of each column when they start sharing it. The digest belongs to the array's memory, so a column is hashed once however many frames share it, and it is dropped when the memory is freed. Checkpoint and output writers compare the digests first and raise SharedColumnChangedError naming each changed table and column. A column replaced through pandas moves to new memory and is not compared. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Compare recorded columns before the Frame constructor and the SPM stage make deep copies, so a changed column cannot pass through them unseen. Hash category values and sparse positions, and leave out values hidden by a nullable column's mask, which pandas itself overwrites. Stop recording tables passed to replace_tables, which callers may keep editing, and correct what the error and docs claim about in-place pandas writes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Compare shared columns at the start of uk_release_export_frame, whose copies would otherwise hide a change. Hash block-format sparse positions, and tighten the tests of nullable masks and of the constructor's error reporting. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 19, 2026
Brings in 4ee5584 from claude/frame-adopt-replace-tables-b93d80. Both sides changed bundle.py, so the calibrate and simulate parity pins were regenerated with tools/graph_parity_repin.py under Python 3.14 rather than taken from either side. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Merged
5 tasks done
anth-volk
changed the base branch from
claude/frame-adopt-replace-tables-b93d80
to
main
November 19, 2026 19:23
Three files conflicted. test_us_fiscal_refresh_builder.py keeps both sides' new tests. The US spec_sha256 pin in test_us_multispine_pool_tool.py and docs/evidence/spec-engine/us-f0-coverage.json were regenerated with tools/repin_us_spec_engine_contracts.py and tools/spec_engine_coverage.py, because main's us/gates.json row and this branch's solve.py change both move them. The calibrate and simulate parity pins regenerate unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…indings reach microcosm.frame.bundle imports the shared-column check, so the module count in test_the_batterys_binding_identity_adds_only_the_leaf_and_its_imports grows by one. engine-free (3.13) and (3.14) failed on this. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
#1682's in-place QRF leaf array in microcosm.fit.qrf and this branch's calibrate return_frame option in solve both move the US seed-protocol digests, so neither side's values were kept. Regenerated with tools/repin_us_spec_engine_contracts.py and tools/spec_engine_coverage.py in the US engine environment; both --check runs pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…K target kernels microcosm.frame.bundle now imports microcosm.frame.shared_columns, so the walk from the UK target kernels' hashed modules meets that edge. engine-free (3.13) failed on test_every_import_a_target_kernel_reaches_is_hashed_or_recorded. The check changes no column, so no target value comes from it; the edge is recorded beside bundle's other imports, as #931 did for receipt_counts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…oc write describe_uk_export shares the person columns with the frame uk_dataset_year_frame returns (with_weights now shares unchanged columns). Once that frame is freed, the test's .loc write lands in the recorded memory and the shared-column check refuses it before the descriptor comparison runs, so test_export_refuses_population_changed_after_descriptor failed on engine-free (3.14) and fails locally on 3.13 and 3.14. Frame tables are read-only; the test now builds the changed population as a new frame and still expects the descriptor refusal. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…k (red) microcosm.frame.bundle and microcosm.frame.materialize now import microcosm.frame.shared_columns, and engine-free (3.13) and (3.14) failed on test_every_import_a_compile_kernel_reaches_is_hashed_or_recorded. The record expects takeup.compare, takeup.gap and transport.create to hash the module beside the bundle, as they hash its schema and weights, and records the edges for targets.compile and transport.quantile_map@1 and @2, which leave the bundle's imports unhashed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… kernels target_kernels._MATRIX_MODULES (targets.problem, takeup.compare, takeup.gap) and transport.create's hash list name microcosm.frame.bundle with its schema and weights. The bundle now calls the shared-column check when it builds and copies frames, so these lists hash microcosm.frame.shared_columns too. Their hashes already change in this PR because bundle.py changed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The only conflict is uk_release_export_frame's docstring: main describes the household totals, this branch the shared-column check made before the tables are copied. Both sentences are kept. The US spec-engine pin checks pass after the merge. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
#1667's one-target-at-a-time ACS QRF transfer and this branch's calibrate return_frame option in solve both move the US seed-protocol digests, so neither side's values were kept. Both comments are kept, and the values were regenerated with tools/repin_us_spec_engine_contracts.py and tools/spec_engine_coverage.py in the US engine environment; both --check runs pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
juaristi22
reviewed
Nov 24, 2026
Collaborator
There was a problem hiding this comment.
Reviewed at 62341fbd2; updated for 9027c2f99, which makes Frame.adopt public and so settles the last #1666 follow-up. The sharing and the column check do what the description says. On invented data, raw writes reach the source frame and check_shared_columns reports each one: np.add.at on a NumPy column, and .values writes on a nullable integer (with and without missing values), a category and a python-backed string (.values or to_numpy()). One thing before merge, and two notes.
Before merge
- Let CI finish on the new head.
Notes
- The check now stops the build if a stage writes into a frame table in place after its sharing partner is freed. A grep of
uk_runtime/finds no such write. - The check also misses a changed column that a later step uses and then drops before the next check. On invented data, a raw write into a shared
x, theny = 2 * xwithxdropped, passes the check whileycarries the changed value. Checking a frame's recorded columns whenever it is shared again would catch that one step later, at extra hashing cost.
Checked
- At
62341fbd2, all 831 tests on the touched surface pass locally (macOS arm64, Python 3.14), none skipped: the frame, checkpoint,solve, transport and UK graph, export and compile tests, the raw-write scan and the parity pins.
The review of #1683 asked for Frame._adopt to be documented as private or made public, since load_frame_checkpoint in microcosm-build calls it. It becomes public as Frame.adopt. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
load_frame_checkpoint in microcosm-build builds its frame through it, so the method is public API rather than a private one called from another package. Its docstring now says that adopt records no columns for the shared-column check: the frame operations record the columns they share before calling it. Every reference, including both changelog fragments, moves to the new name. bundle.py's bytes change, so the calibrate and simulate parity kernels are re-pinned with tools/graph_parity_repin.py under Python 3.14. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s (red) The #1683 review found that a raw write into a shared column passes every later check once a step drops the column: a new column derived from it carries the changed value, and no checked frame still holds the evidence. These tests expect replace_tables, and with_columns through it, to compare each recorded column the new frame no longer holds before building it, for every raw write the check covers, and to hash only those columns. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A raw write into a shared column passed every later check once a step dropped the column, while a column derived from it carried the changed value on (#1683 review). check_columns_leaving compares each recorded column of a replaced table whose arrays the replacement no longer stores, and replace_tables, which with_columns ends in, calls it before sharing anything. Only leaving columns are hashed. The shared_columns docstring now lists the remaining case: a column that leaves every checked frame outside these two operations. bundle.py changes, so the calibrate and simulate parity kernels are re-pinned under Python 3.14. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
#1684's joblib.cpu_count() default in microcosm.fit.qrf and this branch's calibrate return_frame option in solve both move the US seed-protocol digests, so neither side's values were kept. Both comments are kept, and the values were regenerated with tools/repin_us_spec_engine_contracts.py and tools/spec_engine_coverage.py in the US engine environment; both --check runs pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
anth-volk
added a commit
that referenced
this pull request
Nov 24, 2026
Resolves the one conflict #1683 brought: the import block at the top of test_us_post_export_scoring.py keeps main's weakref and Iterator imports and this branch's DroppedConstraintMatrix import. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
anth-volk
added a commit
that referenced
this pull request
Nov 24, 2026
main's table operations through adopt (#1683) and build-tool thread settings (#1696) moved the same seed-protocol and spec digests as this branch. They were regenerated from the merged source with tools/repin_us_spec_engine_contracts.py, tools/generate_us_bundle_from_constants.py and tools/spec_engine_coverage.py. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
juaristi22
added a commit
that referenced
this pull request
Nov 25, 2026
…C new-claimant draw and the PIP split on the clone-first spine (#931) Main took 22 pull requests since bcb5cba. Among them are #1687 (policyengine-uk 2.179.2, the UC new-claimant draw and the re-pin to Chronicle 15040ea5), #1663 (the PIP split and the LCWRA imputation), #1399 (CenTax childcare parent targets), and the frame and QRF changes #1666, #1683, #1667, #1682 and #1684. Thirty files conflicted. The spec. sources.yaml is the truth, and source_stages.json is derived from it by stage name, so the two agree by value; only the stages' source-manifest pins in the release-input coverage manifest move beyond a plain three-way merge, from the merged stage file. Notes both sides extended keep both texts. Main's child-tab pay (CHILD.CHEARNS) reaches frs_hmrc_spine_leaves, whose block this branch never changed, and main's uc_new_claimant_draw runs last, as on main: the roster has 64 stages. The new-claimant draw follows this branch's engine-stage rule: its kernel binds the modules every engine stage binds and the FRS release's year rules, it joins the counting stages, and it binds frs_spine for two constants only. Main's LCWRA imputation receipt counts people in distinct source households among geographic clone 0's rows, as the rest of that receipt does here; main's tests of it pass unchanged. The release export keeps this branch's line parameter and area-code rule and adds main's UK totals of the share keys' components. Main's dataset-year frame is rebuilt with the ISA income contract and the savings income projection, so the two export kernels now hash the engine adapter; the measures and national problem kernels hash the shared-column check main's frames run before each write (#1683). target_fit_reviewed_exclusions.json is main's (640f252 retired a stale national row), and the battery tests' clock moves to main's approval of 23 November. The gate battery's policy, gates manifest, fingerprint and release-cut pins, the spec-engine seed map and protocol, and the UK loader and US spec pins are computed on the merged tree; the US F0 coverage report is regenerated (--check passes). The measure-simulation names number 146: main's PIP and RTI checks and this branch's two land diagnostics. The feed. Main re-pinned to Chronicle 15040ea5. On the merged tree the vendored facts, the national and local references and membership (with the two UC seat deferrals), and both compile parity receipts are rebuilt from the 15040ea5 artifact under main's labels. The rebuild gives the three-way merge's own bytes everywhere but in two of this branch's files: the Scottish Band D facts now name the 15040ea5 feed, their 32 rows unchanged, and the local references list main's CenTax childcare parents source, with 27,155 active references and 2,944 signed deferrals as before. The H2 fixture is regenerated on policyengine-uk 2.179.2. Checks on the merged tree: the feed-gated tests on the 15040ea5 artifact (the high-value homes, their gates and region control, the target references, the local rows, the RFDR layer, the stock cells, the Chronicle feed and the shared pin identity: 251) pass, and so do the touched, fixture, pin and spec tests (2,136). The H2 clone-first tests cannot build their engine blocks until the engine-population declaration is reviewed on 2.179.2, which the next commit does; the US pool tool's tests need policyengine-us, which this environment does not install. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
juaristi22
added a commit
that referenced
this pull request
Nov 25, 2026
…rnels (#931) This branch's derived transport binding rule found two kernels that compute through microcosm.frame.shared_columns without hashing it: export.readback@1 and transport.input_closure@1. Both compute through frame.materialize, which main's #1683 made check a frame's shared columns before it writes. Each now hashes the module, as main's population and target kernels do, and the evidence kernels' pinned module lists name it. The two kernels re-key; no spec pin moves (the US spec pin, the UK loader pin and the US F0 coverage report are as before). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
juaristi22
added a commit
that referenced
this pull request
Nov 27, 2026
…rnels (#931) This branch's derived transport binding rule found two kernels that compute through microcosm.frame.shared_columns without hashing it: export.readback@1 and transport.input_closure@1. Both compute through frame.materialize, which main's #1683 made check a frame's shared columns before it writes. Each now hashes the module, as main's population and target kernels do, and the evidence kernels' pinned module lists name it. The two kernels re-key; no spec pin moves (the US spec pin, the UK loader pin and the US F0 coverage report are as before). 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.
Follows #1666 (
Frame.adopt, which this PR makes public,replace_tablesandwith_columns), which is merged; the diff shows only this PR's work.Summary
Frame.with_weights,Frame.place,Frame.selectandload_frame_checkpointno longer deep-copy every table. They build their result throughFrame.adopt.Frameoperations share the memory of unchanged columns with their source, and the loader keeps the columns it decoded without a second copy. Each frame still holds its own DataFrame objects, so a write made through pandas does not reach another frame. With copy-on-write off,Frame.adoptdeep-copies as before.Frame.adoptis public (it wasFrame._adopt), becauseload_frame_checkpointin microcosm-build calls it. It validates like the constructor and, under copy-on-write, stores the given DataFrames without copying them; the caller must not write to them afterwards. It records no columns for the shared-column check: the frame operations record the columns they share before calling it.SharedColumnChangedError, naming each changed table and column, before a checkpoint or output is written.calibrate(..., return_frame=False)returns the weights and diagnostics withframe=None, after the same weight checks. The default is unchanged. The release tool does not use the option yet.The expected memory effect is that reading a checkpoint holds one frame instead of two. That follows from removing the copy. It has not been measured on a real build.
How sharing works
with_weights: shallow-copies every table and passes them toFrame.adopt. Its entity, type, weight-kind and mass-policy checks moved unchanged intoFrame._checked_weight_change.place: inserts the column into a shallow copy of the destination table and drops it from a shallow copy of the owner table. The insert stays a column assignment becausepd.concat(axis=1)changes the dtype of the column labels.PerformanceWarningis suppressed for that one insert.select: passes the tables it already built toFrame.adopt.load_frame_checkpoint: passes its decoded tables toFrame.adopt.calibrate: withreturn_frame=False, calls_checked_weight_changeand skips building the frame.refit_l0_selectionraisesValueErrorif a selection that needs a frame has none.How the column check works
The code is in
microcosm/frame/shared_columns.py.Frame.check_shared_columnshashes the recorded arrays again. It runs inwrite_frame_checkpoint,write_nullable_us_h5, the target-frame checkpoint writer, the legacy US writer,put_frame_table, and before these deep copies: theFrameconstructor,engine_tables,uk_release_export_frame, the SPM role stage and the ACS local structure extraction.replace_tables, andwith_columnsthrough it, first compare each recorded column of a replaced table that the replacement no longer stores, because it was dropped or replaced. Without this, a raw write into a shared column passed every later check once a step dropped the column, while a column derived from it carried the changed value on (found in review). Only the leaving columns are hashed.replace_tablesorwith_columnsis compared once, and passes unless a raw write changed it. Values hidden by a nullable column's mask are left out of the digest, because pandas overwrites them itself in operations such ascumsum.Cost, from synthetic tables on an arm64 Mac, not a real build: SHA-256 ran at about 2.4 GB/s on one thread. Hashing needs no extra memory for NumPy numeric columns. In a review measurement on a 2.4 GB, 1,500-column frame,
engine_tablestook 3.15 s, against 0.58 s for its copies alone. Modal's CPUs were not measured.Pins
calibrateandsimulatekernel pins are regenerated because they hashbundle.pyandsolve.py.solve.py. This used the documented sequence intools/repin_us_spec_engine_contracts.py. The US bundle does not change.Behaviour to review
.loc,fillna(inplace=True)andupdatewrite into it directly, and the check reports them. Frame tables are documented as read-only, and a scan of production code found no such write, but a future one would stop the build.table.copy()or aselectthat drops rows, carries the changed values into new memory.replace_tablesorwith_columnsare recorded only once a later operation shares them. Neither method has a production caller.replace_tablesorwith_columns, for example when a newFrameis built from tables without it, or values derived from it are written without a frame. A column derived from it carries the change on unreported._dataand_maskof nullable arrays, read-only, because pandas has no public access to them when values are missing. Both are recorded as reviewed intest_shared_buffer_writes.py.tools/build_us_multispine_pool.pyfeeds loaded frames to such a writer, on resume. H5 comparisons should go by variable name.warnings.catch_warningschanges process-wide filters for the duration of theplaceinsert. None of theplacecall sites runs in threads.PerformanceWarning.graph_evidence.result_summary_datareadsresult.frame, so it fails on areturn_frame=Falseresult. No caller passes one.Test plan
Run by pytest node ID. The full suite was not run locally.
return_frameand column-check tests, the checkpoint bit-equivalence tests, the post-export scoring tests, the raw-write scan and the spec-engine digest tests.tools/repin_us_spec_engine_contracts.py --checkandtools/spec_engine_coverage.py --checkpass.test_monolith_and_staged_builds_are_bit_exact_at_every_boundarywas not run. It needs US input files that are not available locally.🤖 Generated with Claude Code