Skip to content

fix(graphical): prior id 0 no longer collides with the FactorValue sentinel; cavity gradient added after the VJP (#1557) - #1558

Merged
Jammy2211 merged 1 commit into
mainfrom
feature/ep-prior-id-zero
Sep 2, 2026
Merged

Jammy2211 merged 1 commit into
mainfrom
feature/ep-prior-id-zero

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Summary

Closes #1557. Found by the analytic Gaussian benchmark (autofit_workspace_test#91 / PR #92): the first prior created in a process gets id 0, and the FactorValue sentinel — a class-as-Variable — also carries id 0 and hash 0. Prior.__eq__ compared ids without a type check, so GaussianPrior(id=0) == FactorValue was True and the Jacobian VJP seed dict was overwritten by that prior's cavity gradient. Every multi-variable factor gradient was wrong whenever a graph contained the process's first prior, which is why the hierarchical factor in an exactly Gaussian EP graph never updated (mu stuck at its starting mean field, 50.0000 ± 20.6 vs closed form 50.86 ± 4.11; 0 SUCCESS updates). One-variable factors were unaffected, which is why analysis factors converged and the defect stayed hidden behind loose recovery tests.

Two independent fixes: the sentinel gets an id no Variable can be assigned (-1) and equality is type-aware in both directions; and the free-variable cavity gradient is no longer merged into the VJP seed at all — it is added after the pull-back, so the gradient is right even if some future sentinel collides.

Referee after this PR (worktree run of autofit_workspace_test/scripts/graphical/analytic_gaussian.py): leg A mu mean 50.8596 (a = 0.000; was stuck at 50.0000), every x_i mean a = 0.000, hierarchical updates SUCCESS = 20 (was 0). The remaining leg A miss is the std (3.76 vs 4.11, −8.5 %), which is the Laplace-covariance defect tracked separately (Mind prompt ep_laplace_covariance_and_failed_update_projection.md, next in the wave); leg B / the prior-family sweep still collapse or bias for the same reason plus the truncation-loss defect (ep_message_support_and_transform_lost_in_projection.md). Tolerances untouched.

API Changes

None — internal changes only. Equality semantics tightened: a Prior no longer compares equal to a non-Prior Variable with the same integer id, and a plain Variable no longer equals a Prior (they draw from different id counters); prior-vs-prior id equality and all hashes are unchanged. AbstractJacobian.grad(values) still returns the same keys; it now seeds the VJP only from its cotangent variables (new cotangent_variables property — out_variables for the VJP, left_variables for the JVP, whose property names are mirrored) and adds input-keyed values after the pull-back.
See full details below.

Test Plan

  • test_autofit/graphical/functionality/test_gradient_id_zero.py (new, 4 tests, 1.4 s): sentinel id/eq/hash and VariableData not clobbered; Variable vs Prior same-id distinct; func_gradient == central finite differences to 1e-6 on both variables of the id-0 hierarchical repro; seeded 3-group hierarchical EP puts mu at the closed form with a SUCCESS update. All four fail on main.
  • pytest test_autofit -q: 2391 passed, 2 skipped.
  • Referee leg A mu mean a = 0.000, SUCCESS = 20 (tables on fix(graphical): prior id 0 collides with the FactorValue sentinel, corrupting multi-variable factor gradients (EP) #1557).
  • CI green (unittest 3.12 / 3.13, docs)
Full API Changes (for automation & release notes)

Added

  • autofit.mapper.variable.VariableMetaClass.SENTINEL_ID = -1 — id given to class-level sentinels (FactorValue) so no instance id can collide.
  • autofit.graphical.factor_graphs.jacobians.AbstractJacobian.cotangent_variables — the variables the VJP/JVP seed may carry (out_variables for VectorJacobianProduct, left_variables for JacobianVectorProduct).

Changed Behaviour

  • autofit.mapper.variable.Variable.__eq__ — isinstance(other, Variable) and type(self)._ids is type(other)._ids and self.id == other.id (was id-only via the default path).
  • autofit.mapper.prior.abstract.Prior.__eq__ — isinstance(other, Prior) and self.id == other.id (was self.id == other.id with no type check). Hashes unchanged.
  • AbstractJacobian.grad(values) — input-keyed entries of values are added to the result after the pull-back instead of being written into the seed dict.
  • autofit.graphical.mean_field.FactorApproximation.func_gradient — deterministic-variable cotangents go to fjac.grad(...), free-variable cavity gradient terms are added afterwards.

Migration

  • None required.

Generated by the PyAutoLabs agent workflow.

…ntinel; cavity gradient added after the VJP (#1557)

The first prior of a process took id 0, the same id Variable.__init__ gave
the FactorValue class-as-variable, and Prior.__eq__ compared ids with no
type check: the prior compared and hashed equal to the sentinel, so its
cavity gradient overwrote the VJP seed in AbstractJacobian.grad and every
multi-variable factor gradient was wrong (analytic Gaussian benchmark leg
A: the parent mean never left its starting mean field).

- FactorValue takes a negative sentinel id no prior or variable can equal.
- Prior.__eq__ is type-aware and Variable.__eq__ mirrors it (same id
  counter and same id), so the comparison is symmetric; hashes unchanged.
- AbstractJacobian.grad seeds the VJP only with the cotangents of the
  factor's outputs (cotangent_variables: out_variables for the VJP,
  left_variables for the JVP) and adds input-keyed entries afterwards;
  FactorApproximation.func_gradient splits the cavity gradient the same way.
- Regression test: prior id 0 vs FactorValue, gradient vs central finite
  differences, and a three-group hierarchical EP run that moves the parent.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01C49DeRHoKss4xBnz5F7gUm
@Jammy2211 Jammy2211 added the pending-release PR queued for the next release build label Sep 2, 2026
@Jammy2211
Jammy2211 merged commit 809b4fd into main Sep 2, 2026
4 checks passed
@Jammy2211
Jammy2211 deleted the feature/ep-prior-id-zero branch September 2, 2026 22:06
@Jammy2211 Jammy2211 removed the pending-release PR queued for the next release build label Sep 4, 2026
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.

fix(graphical): prior id 0 collides with the FactorValue sentinel, corrupting multi-variable factor gradients (EP)

1 participant