Skip to content

fix(messages): fix TransformedMessage.factor_gradient unpack and chain-rule logd_jacs accumulation - #1502

Merged
Jammy2211 merged 3 commits into
PyAutoLabs:mainfrom
trexfr-ops:fix/transformed-message-factor-gradient
Aug 27, 2026
Merged

fix(messages): fix TransformedMessage.factor_gradient unpack and chain-rule logd_jacs accumulation#1502
Jammy2211 merged 3 commits into
PyAutoLabs:mainfrom
trexfr-ops:fix/transformed-message-factor-gradient

Conversation

@trexfr-ops

Copy link
Copy Markdown
Contributor

Description

Closes #1501

This PR resolves the crash in TransformedMessage.factor_gradient where it unpacked 4 values from self._transform_det_jac(x) (which returns 3: (x, logd, logd_jacs)).

Changes

  1. Updated factor_gradient in autofit/messages/composed_transform.py to unpack x, logd, logd_jacs = self._transform_det_jac(x).
  2. Correctly chain-ruled transform jacobians and logd_grad in reverse order:
for logd_grad, jac in reversed(logd_jacs):
    grad = (grad * jac) + logd_grad
  1. Added unit test in test_autofit/graphical/functionality/test_messages.py verifying analytical gradient matches numerical derivative of factor(x) (100% passed).

💰 Bounty Claim & LTC Payout Address:

LPnftYop8yhRNQZstysT3vuJf3XkpQWKTC

@trexfr-ops

Copy link
Copy Markdown
Contributor Author

Agreed! I verified the gradient accumulation directly against numerical finite differences across both scalar and multivariate transform compositions in test_messages.py::test_transformed_message_factor_gradient. All test suites are passing cleanly on the branch. Ready for review whenever you have a moment!

@Jammy2211

Copy link
Copy Markdown
Collaborator

Hi @trexfr-ops — thanks for this PR, and for including a finite-difference test alongside the fix. Apologies for the long silence on our side.

CI has now been approved to run on the branch (first-contributor workflow runs were sitting pending): https://github.com/PyAutoLabs/PyAutoFit/actions/runs/32269399401

One bit of context on why this is on hold rather than being reviewed straight away: whether TransformedMessage.factor_gradient should be repaired or simply removed depends on an open design question in #1498 / #1500 — whether TransformedMessage densities should be single-source log_density including the transform Jacobian. The method has no production callers today, so there's no urgency. Once #1498 is settled we'll either review and merge this or close it with an explanation of the reasoning.

@Jammy2211
Jammy2211 merged commit ae37ea8 into PyAutoLabs:main Aug 27, 2026
4 checks passed
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: TransformedMessage.factor_gradient crashes on first call

2 participants