Repository navigation
feat: A4b search-level seed (SeedSequence fan-out, conditional identifier walk, legacy table) - #1686
Merged
Merged
Conversation
…al identifier walk NonLinearSearch accepts seed; stream 0 seeds the initializer (start_points), FitContext spawns it so ctx.rng.spawn(k) gives the backend streams 1..k, and grid-search children get an independent child sequence via an unhashed spawn key. The identifier walk appends the seed when not None for a search whose __identifier_fields__ do not already name it. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…global random module Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…t 42) and seeded emcee/dynesty backends - Nautilus, NSS, MultiStart: their seed argument is the base seed (passed to super). - BlackJAXNUTS, SMC: seed defaults to None (backend_seed 42, unhashed); a user-supplied seed is hashed. - Emcee's RandomState and dynesty's rstate come from backend stream 1 when seeded. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…SON, old output) and witnesses Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
… wins DynestyStatic/DynestyDynamic passed both **self.search_kwargs and **self._rstate_kwargs(), so a subclass that already puts `rstate` in `search_kwargs` (autofit_workspace_test analytic_autofit.SeededDynestyStatic) raised `TypeError: multiple values for keyword argument 'rstate'` at sampler construction. `_sampler_kwargs()` now resolves it once for the static and dynamic, pooled and unpooled call sites: an explicit `rstate` in `search_kwargs` wins, else the search-level seed's backend stream 1, else dynesty's own default. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…dentifier main ran BlackJAXNUTS/SMC with an unhashed default seed=42 and recorded `seed: 42` in every default search.json and pickle. PR 2 hydrated that 42 as a user seed and hashed it, so SearchOutput.id, the database scrape and output reconstruction recomputed a new identifier (NUTS 2a6f5217... -> b120de22..., SMC 3baa494f... -> 933c180e...). - seed.legacy_default_seed: a NUTS/SMC seed of 42 (constructor, from_dict or an unpickled main search via __setstate__) is the unhashed default None; backend_seed is still 42, so sampling is bit-identical. An explicit seed=42 therefore shares the default identity (class docstrings, seed.py table, docs/design/run_ctx.md Revision 3). - SearchOutput.id prefers the identifier the output persisted (md5 of its `.identifier` description) over a recompute, so a future hashing change cannot re-key historical outputs; outputs without the file recompute. - test_seed.py: the old-JSON expectation now pins main's identifier; new pickle, aggregator (hard-coded main identifiers) and persisted-id tests. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…nd seeds Grid-search children carried spawn keys (0,), (1,) that only reached the SeedSequence helpers, so the raw-seed backends (Nautilus Sampler(seed=), the JAX PRNGKey of NSS, BlackJAXNUTS and SMC) and MultiStart's _seed_for broad-start and resurrection streams gave every child the same randomness. - seed.child_seed: with a spawn key, a non-negative 31-bit seed from SeedSequence(seed, spawn_key=key); without one (a standalone search) or with no seed, the raw seed unchanged, so existing seeded runs are bit-identical. - Nautilus and NSS gain backend_seed (used at every backend call site); NUTS/SMC backend_seed derives from the default 42 or the seed. - MultiStart._seed_for adds the spawn key; standalone paths unchanged. - The spawn key stays unhashed and out of search.json. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…erpolator tests The Initializer now draws from initializer_rng(search) (stream 0 of SeedSequence(search.seed)), which is OS entropy for these unseeded searches, so the autouse fixture's stdlib-random seeding no longer controlled the start points and test_single_variable was a coin flip again (failed on unittest 3.13). Patch seed.seed_sequence to substitute SEED for a missing search seed; dynesty's unseeded fallback get_random_generator patch remains live. Test-only change. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Collaborator
Author
|
Companion PR: PyAutoLabs/autofit_workspace_test#111 |
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
Search-extensibility phase A4b (PR 2 of 3, stacked on #1685; base
feature/search-ext-a4-decompose). This adds one search-levelseed(decision D4), fanned out withnumpy.random.SeedSequence. The seed enters the identifier through a conditional in the identifier walk, never through the base__identifier_fields__. Issue #1684 is closed by PR 3.What changed, by sub-step
Base seed + fan-out + identifier (
2954b09a0).NonLinearSearch.__init__(..., seed=None).autofit/non_linear/search/seed.pyholds the rules:start_pointspassesinitializer_rng(search));FitContext.rngspawns stream 0 on construction, so the backend'sctx.rng.spawn(k)gets streams 1..k;_fitbackends usebackend_seed_sequences/backend_random_state/backend_generator;_seed_spawn_key = (index,), which gives independent child streams and is neither serialized nor hashed.autofit/mapper/identifier.pyappendsseedafter the__identifier_fields__values only whentype(value)._identifier_seedexists,"seed"is not already a field, and the seed is not None.docs/design/run_ctx.mdgains Revision 3.Initializer off global
random(f4f23aa8c).AbstractInitializer.samples_from_model(..., rng=None). Every draw (Initializer,InitializerParamBounds,InitializerParamStartPointsfallbacks) uses the passednumpy.random.Generator, or a fresh unseeded one, throughinitializer.rng.Legacy table + seeded backends (
2b3536f62):"seed"stays a static field; theirseedkwarg is passed to the base (alias)seed=Nonedefault →backend_seed == 42, unhashed; a user-supplied seed (incl. 42) is hashedEmcee's
RandomStateand dynesty'srstatecome from backend stream 1 when a seed is given (unseeded runs keep each backend's own default). Nautilus (Sampler(seed=)), NSS/NUTS/SMC (PRNGKey) and MultiStart (SeedSequence([seed, stream])) keep taking the seed directly, so existing seeded runs of those reproduce exactly.Tests (
27e77474c):test_seed.pywithseed_golden.json, which holds main 8437a3b's values plus the new explicit-seed identifiers.Invariants
hash_lists for all 15 are byte-identical to main (test_default_and_explicit_none_identifiers_unchanged,test_default_hash_list_unchanged;Nautilus().hash_listincluded). The only changes are the seed table's own, listed under "Accepted re-keys"search.json, constructor argumentsconstructor_argumentspass). The serialized argument set gains one key,seed(null unless passed), for every search, becausedictablewalks the base__init__;test_dict.py's fixture records it. Old JSON withoutseedloadstest_old_incomplete_output_resolves_the_same_folder)samples_info["class_path"]Accepted re-keys (for the human reviewer)
seed=42). These now hash the seed, so existing explicit-seed output folders re-key, as accepted in the epic report §8.6.seed: 42insearch.json. Loading it passes 42 explicitly, so the loaded object hashes it. Output folders are not recomputed fromsearch.json(identifiers are on disk), so nothing re-keys on disk; the test pins this behaviour.seedargument (it seeds_broad_starts/ resurrection). Applying the table's "hashed only when not None" rule therefore re-keys existing explicit-seed MultiStart folders. Their sampling is bit-identical:_seed_foris untouched. This is within the seed table's rule but was not anticipated by the report. Please confirm it on review. The alternative is one line: keep MultiStart's seed unhashed by overriding_identifier_seedto return None.Witness
test_same_seed_same_initial_points, throughstart_points), and different seeds give different points. The initializer never touches the globalrandommodule (test_initializer_never_uses_the_global_random_module).Emcee(seed=1)fits on this machine write byte-identicalsamples.csv(test_two_seeded_emcee_fits_write_identical_samples). The control is two unseeded fits, which differ.Nautilus().hash_listis unchanged.ctx.rng.spawn(1)equals backend stream 1, grid children get distinct streams with an unchanged identifier and no serialized spawn key, and the dynestyrstateis reproducible.Tests
unittest-nojaxemulation: 3392 passed, 176 skipped, 2 xfailed. NSS seed cases skip without blackjax._generate_searches.py --check: up to date.Decisions taken
None. The signature default isNone, the backend usesbackend_seed = 42, andsearch.seedreadsNoneby default (main read42). A newsearch.jsonrecordsseed: nullfor a default NUTS/SMC, so it reloads unhashed. An explicitseed=None(which crashedPRNGKeyon main) now means the default.seed=Nonethe initializer uses a fresh OS-entropy generator. A user who seeded the globalrandommodule to reproduce start points must now passseed=.rngargument added in commit 2. The branch head is consistent; the intermediate commit is not separately runnable.Companion changes required
autofit_workspace_test/scripts/graphical/analytic_autofit.py. This change is recommended. It is no longer required, because finding 2's precedence fix keeps the existing helper working (verified:analytic_gaussian.pypasses 41/41 unchanged).class SeededDynestyStatic(af.DynestyStatic)(lines 206–219). It seeds dynesty by injectingrstateintosearch_kwargs.run_joint_fit(line 231), replacerstate(backend stream 1) and the initializer's live points:randomandnumpy.randomare seeded too, for the initializer's live-point draws." with "The search-levelseedseeds the initializer's live points and dynesty'srstate."scripts/graphical/analytic_gaussian.pylines 50–51, replace "(analytic_autofit.SeededDynestyStatic, dynestyrstateplusrandom/numpy.randomfor the initializer, seed 0)" with "(af.DynestyStatic(seed=0): the search-level seed seeds dynesty'srstateand the initializer)".analytic_gaussian.pypasses PARITY: PASS (41/41), 23/23 cells, exit 0. The joint-fit column values move slightly becauserstatenow comes from backend stream 1, notdefault_rng(0).Adversary review 07 (Codex gpt-6-astra) — enacted
Each finding was reproduced first as a failing test on the pre-fix head
27e77474c(20 failures across the three findings), then fixed. One commit per finding:5cc906af2(finding 2),71b925b3f(finding 1),175d91a58(finding 3).search.jsonand pickles recordseed: 42, which PR 2 hydrated as a user seed and hashed. As a result,SearchOutput.id, the database scrape and reconstruction re-keyed historical outputs.origin/main(8437a3b) checkout, main gives NUTS2a6f5217539f82fab8ea42eda9bf3441and SMC3baa494f2ca7b37a8c4b85884cadd1f7. Loading that JSON or pickle under PR 2 gaveb120de22…/933c180e…, and the aggregator output id moved fromf804af64…tocc9851e4….seed.legacy_default_seedreads a NUTS/SMC 42 (from the constructor,from_dict, or__setstate__for main pickles) as the unhashed defaultNone.backend_seedstays 42, so sampling is bit-identical. An explicitseed=42on NUTS/SMC therefore shares the default identity, as documented in both class docstrings, theseed.pytable anddocs/design/run_ctx.mdRevision 3. Every other explicit NUTS/SMC seed is still hashed (the accepted re-key). (b) Aggregator:SearchOutput.id, which is also the scrape's key, now prefers the identifier the output persisted: the md5 of its.identifierdescription, which is whatIdentifier.__str__hashes. It recomputes only when the file is absent.test_old_search_json_loads(formerly expected the re-key; it now pins main's identifier).test_nuts_smc_default_is_an_unhashed_42(seed=42 ≡ default).test_old_nuts_smc_pickle_keeps_the_main_identifier.test_old_nuts_smc_output_keeps_the_main_identifier_in_the_aggregator, which hard-codes the two main identifiers above and checksSearchOutput.idwith and without.identifier.test_search_output_id_prefers_the_persisted_identifierand…_recomputes_without_a_persisted_identifier.**search_kwargsand**_rstate_kwargs(), so a subclass that already setsrstateraisedTypeError: multiple values for keyword argument 'rstate'.search_internal_fromsampler construction for static and dynamic, pooled and unpooled.AbstractDynesty._sampler_kwargs()resolvesrstateonce for all four call sites. Precedence: an explicitrstateinsearch_kwargswins, then the search-level seed's backend stream 1, then dynesty's own default.test_dynesty_sampler_kwargs_resolve_rstate_once,test_dynesty_subclass_with_rstate_constructs_its_sampler[static/dynamic × pooled/unpooled]. End to end,autofit_workspace_test/scripts/graphical/analytic_gaussian.py(which usesanalytic_autofit.SeededDynestyStatic) passes PARITY: PASS (41/41) on this head, both unchanged and with the companion edit below applied in a scratch copy._seed_forand resurrection streams, so children shared backend randomness.seed=9hadbackend_seed == 9in both, and MultiStart's broad-start draws were identical.seed.child_seed: with a spawn key, the backend gets a non-negative 31-bit seed fromSeedSequence(seed, spawn_key=key). With no key (a standalone search), the raw seed passes through unchanged. Nautilus and NSS gainbackend_seed, which all their backend call sites use. NUTS/SMCbackend_seedderives from the seed, or from 42 by default.MultiStart._seed_foradds the spawn key. The spawn key stays unhashed and out ofsearch.json, so no identifier changes.test_grid_children_get_distinct_raw_backend_seeds[Nautilus, NSS, BlackJAXNUTS, SMC], which checks distinct child seeds, standalonebackend_seed == 9, unchanged identifiers and no serialized key.test_multi_start_grid_children_get_distinct_streams[×4], where the standalone streams equal the historicalSeedSequence([9, stream])and unseeded runs still give0/1.test_raw_seed_backend_uses_backend_seed.Superseded statements above:
seed=42no longer re-keys either; it is the default.None. That includes default NUTS/SMC/NSS (42) and unseeded MultiStart (streams 0/1). Grid cells of those default searches therefore no longer replay identical backend randomness, as they did on main. Their identifiers and folders are unchanged. Standalone runs are bit-identical.Gates on this head (
175d91a58):unittest-nojaxemulation: 3411 passed, 178 skipped, 2 xfailed._generate_searches.py --check: up to date.hash_listandto_dictdump: identical to the previous PR 2 head. Against main it differs only by the declaredseedadditions and the NUTS/SMCseed: 42 → null.Follow-up (
114adeebd, test-only): CI determinism regression onunittest (3.13).test_autofit/interpolator/test_covariance.py::test_single_variablefailed on175d91a58(27.51 == 25.0 ± 2). The cause is not a flake. The module's autouse fixtureseed_search_randomnessmade the one-call-capped Dynesty search deterministic by seeding stdlibrandom, which the old Initializer drew from. This PR moved the Initializer toinitializer_rng(search), which is stream 0 ofSeedSequence(search.seed)and therefore OS entropy for these unseeded searches. The test went back to its measured ~3.6 % miss rate. The fix is in the fixture only: it patchesseed.seed_sequenceto substitute the moduleSEEDwhen the search has no seed. Dynesty's existing patch stays live, because an unseeded_sampler_kwargs()passes norstate, so dynesty still falls back todynesty.dynesty.get_random_generator. The fixture docstring now describes the new generators. Library behaviour is unchanged. The recovered value is bit-identical across processes (24.997808837505918×3).test_covariance.pypassed 20/20 in a loop on both114adeebdand the rebased PR 3 head4b86167b4, andtest_autofit/interpolator+test_seed.pypassed (157). The full serial suite on4b86167b4gave 3776 passed, 1 skipped.🤖 Generated with Claude Code