Skip to content

Check that L0/refit saved weights match their base and the flags their fit read #1636

Open

Description

@MaxGhenis

export_us_l0_refit_h5 takes a base H5 and an NPZ of saved L0/refit weights from the operator and trusts that a fit on that base produced the weights. Line numbers refer to commit 59e1483 of #1453.

Nothing ties the weights to their base beyond its household count

load_l0_refit_npz (packages/microcosm-build/src/microcosm/build/us_runtime/l0_refit_export.py:289-354) checks the weight vector's length against the base's household count (:311). When the NPZ's metadata carries them, it also checks the candidate count, the selected count and the weight entity (:328-348). The export's summary records the sha256 of the base and of the NPZ (:691, :693), and nothing compares either with the files the fit read.

For example, an operator rebuilds a base after a stage change that keeps every household but changes some inputs or the row order, then exports weights from a fit on the old base. The lengths match, so the export puts the old fit's weights on the new base's rows. By default the input-mass gate lets each column's weighted total drift by up to 50%, so such an export can pass every gate.

Main's load_l0_refit_npz makes the same checks, so this gap predates #1453. Since #1453 the export reads the local-jurisdiction rungs from the base it loads and holds the kept rows to them, so a wrong base now brings the wrong rungs too.

A base without the flags gets flags its fit never read

Since #1453, the export derives the local-jurisdiction flags on the whole loaded base before it applies the weights (l0_refit_export.py:641; packages/microcosm-build/src/microcosm/build/us_local_jurisdictions.py:654-685). A base that predates #1453 carries none, and a fit that reads it unchanged sees False for every flag.

For example, every household of a pre-#1453 base from the block-ladder line sits on a block, so the export derives all four flags and writes in_wilmington for Wilmington households. Under policyengine-us 2.2.1, a Wilmington itemizer with $100,000 of wages and $20,000 of cash gifts to charity then owes $10,965.82 of 2026 federal income tax. That figure trails the $11,240.82 the fit computed by $275, because Wilmington's earned income tax enters SALT. The geography-ladder gate's flag checks pass, since the flags agree with the geography, and the input-mass gate does not fail a column only the export carries (packages/microcosm-build/tests/engine_free/us/test_us_l0_refit_export.py:1474). Only the summary's carried_flags: [] records that the base held no flag.

This gap comes with #1453. Main's export writes no flag, so the engine default matches what such a fit read. With #1453, the export writes the flags a pre-flag base's geography decides, where main leaves False on every resident. The release builder does not share the gap: it derives the flags before it materializes its targets (tools/build_us_fiscal_refresh_release.py:13878, :13985), so its own fit reads them.

Change

  • Each writer of an L0/refit NPZ records in metadata_json the sha256 of the base H5 it fit on, and the export refuses a base with another sha256. An NPZ without that field needs an explicit opt-in, which the summary records. The builder's warm start already refuses a different support or row order by comparing stored initial weights (tools/build_us_fiscal_refresh_release.py:9124), and the export can add the same check.
  • The writer also records the flag receipt of the frame its fit read: its rungs and the flags it carried or derived. The export refuses to write a flag that receipt lacks. Under an opt-in it writes the flag and lists it in the summary as one the fit never read. A fit that derived the flags first, as the builder does, records them, so its weights export as they do now.

Tests

In packages/microcosm-build/tests/engine_free/us/test_us_l0_refit_export.py, the export refuses weights whose recorded base sha256 differs from the loaded base's, and an NPZ with no recorded base unless the operator sets the opt-in. On the pre-#1453 Wilmington base, it refuses to write in_wilmington when the receipt lacks the flag, and under the opt-in it writes the flag and names it in the summary. It writes the flag as it does now when the receipt shows that the fit derived it.

This work starts after #1453 merges.

🤖 Generated with Claude Code

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions