Skip to content

Raise at construction for ChainFactor n_steps < 1 - #36

Open
whtetigr2 wants to merge 2 commits into
extropic-ai:mainfrom
whtetigr2:fix/f4-chainfactor-nsteps
Open

whtetigr2 wants to merge 2 commits into
extropic-ai:mainfrom
whtetigr2:fix/f4-chainfactor-nsteps

Conversation

@whtetigr2

Copy link
Copy Markdown
Contributor

ChainFactor(base, n_steps=0).sample(...) returns an all-zeros array shaped
like the base factor's output spec. No step of the base factor ever ran; the
returned value is the scan's initial carry placeholder, presented to the
caller as a sample from the factor.

Minimal reproduction (identity DeterministicFactor on a scalar int port,
chained over its feedback port, input 5):

  • n_steps=1 → returns 5
  • n_steps=3 → returns 5
  • n_steps=0 → returns 0 — a value the factor never produced.

The placeholder matches the output spec in shape and dtype, so it is
indistinguishable from a legitimate sample by any downstream check. For
0/1-encoded discrete factors the all-zeros value is even a legal
configuration. The trigger is realistic: the in-repo Gibbs example wires a
user sweep-count hyperparameter straight into n_steps, so a computed
count that hits 0 (e.g. "skip burn-in", total - warmup) silently yields
fabricated samples. The fabrication appears on every sample path
(sample, sample(return_aux=True), sample_with_references) — the aux
traces honestly show 0 steps; only the primary return lies.

Root cause: AbstractChainFactor._run builds the scan's initial "main"
carry as zeros of the base output spec; with zero steps, jax.lax.scan
returns that carry untouched and sample returns it as the result.
ChainFactor.__init__ validates nothing. (Negative n_steps currently
fails at sample time with an inscrutable MLIRError from inside
jax.random.split, with no mention of n_steps.)

Both circuit classes in the same library already guard the analogous
parameter: DiscretePCircuit and HybridPCircuit raise ValueError for
reps < 1 ("reps must be at least 1"). The chain factor's step count is
the same kind of parameter with no equivalent guard.

Fix

Validate in AbstractChainFactor.__init__, mirroring the circuits' check:

if n_steps < 1:
    raise ValueError("n_steps must be at least 1.")

This closes the silent hole (0) at construction on every sample path, and
turns the negative case into a clear construction-time ValueError
instead of a sample-time MLIRError. Nothing legitimate is rejected: no
test, example, or notebook in the repo constructs with n_steps < 1, and
n_steps=1 behavior is bit-identical to before.

Tests

New tests/test_f4_chainfactor_check.py (6 tests): n_steps=0 and
n_steps=-1 raise ValueError at construction (string and callable
porting forms); n_steps ∈ {1, 3, 1000} on the identity chain return the
input unchanged.

Verified against torx v0.0.3 (2142045): the new tests pass, and the full
suite (tests/, excluding the hardware-timing test_performance.py)
passes with this fix and an unrelated HybridPCircuit fix applied:
230 passed + 147 subtests, no regressions. Code formatted with the repo's
pre-commit hooks (black / isort / flake8 all pass).

Comment thread torx/composite_factors.py
*,
slice_info: bool = False,
) -> None:
if n_steps < 1:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is fine, don't need all the tests for a simple check (that users should realistically never miss, but because the check is cheap it is fine)

@whtetigr2

Copy link
Copy Markdown
Contributor Author

Trimmed — down to the two cases that pin distinct behavior: n_steps=0 raises at construction, and n_steps=1 still samples. The negative and alternate-porting-fn cases hit the same guard branch, and the 3/1000 cases exercise the same valid path, so they're out. Fix itself untouched.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants