Repository navigation
fix(graphical): prior id 0 no longer collides with the FactorValue sentinel; cavity gradient added after the VJP (#1557) - #1558
Merged
Conversation
…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
This was referenced Sep 2, 2026
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
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
FactorValuesentinel — a class-as-Variable— also carries id 0 and hash 0.Prior.__eq__compared ids without a type check, soGaussianPrior(id=0) == FactorValuewasTrueand 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 (mustuck 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
Variablecan 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 Amumean 50.8596 (a = 0.000; was stuck at 50.0000), everyx_imean 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 promptep_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
Priorno longer compares equal to a non-PriorVariablewith the same integer id, and a plainVariableno longer equals aPrior(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 (newcotangent_variablesproperty —out_variablesfor the VJP,left_variablesfor the JVP, whose property names are mirrored) and adds input-keyedvaluesafter 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 andVariableDatanot clobbered;VariablevsPriorsame-id distinct;func_gradient== central finite differences to 1e-6 on both variables of the id-0 hierarchical repro; seeded 3-group hierarchical EP putsmuat the closed form with a SUCCESS update. All four fail onmain.pytest test_autofit -q: 2391 passed, 2 skipped.mumean a = 0.000, SUCCESS = 20 (tables on fix(graphical): prior id 0 collides with the FactorValue sentinel, corrupting multi-variable factor gradients (EP) #1557).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_variablesforVectorJacobianProduct,left_variablesforJacobianVectorProduct).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(wasself.id == other.idwith no type check). Hashes unchanged.AbstractJacobian.grad(values)— input-keyed entries ofvaluesare 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 tofjac.grad(...), free-variable cavity gradient terms are added afterwards.Migration
Generated by the PyAutoLabs agent workflow.