Skip to content

Fix backward probabilities and gradients for resumed trajectories - #535

Merged
josephdviviano merged 2 commits into
GFNOrg:masterfrom
davrens101:fix/resumed-backward-probabilities
Nov 6, 2026
Merged

josephdviviano merged 2 commits into
GFNOrg:masterfrom
davrens101:fix/resumed-backward-probabilities

Conversation

@davrens101

@davrens101 davrens101 commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Sampler.sample_trajectories(..., states=...) can start at interior states. The stepwise get_trajectory_pbs path nevertheless masked the entire first transition, assuming every trajectory began at the unique root. This silently changes resumed trajectory-balance objectives and removes the first backward-policy gradient.

On an actual two-dimensional HyperGrid rollout with an interior start and uniform backward logits, the vectorized path gives first-step log P_B = -0.69314718 and bias gradient [-0.5, 0.5]; the previous stepwise path gave zero for both. A root-start control correctly has log probability zero.

Change

Remove the timestep-zero override. Use the existing successor-state/action masks at every step, preserving root, sink, exit and padding handling. Document interior-start support in the existing docstring.

The regression combines an interior start, a root start, immediate exit and padding. It checks probabilities against the analytic categorical distribution and checks the backward-policy gradient against [-0.25, 0.25], not merely a second implementation.

Verification

  • python -m pytest testing/test_probability_calculations.py -q: 10 passed.
  • Required pre-commit run --all-files: autoflake, isort, black, flake8 and pyright passed. The full pytest hook is not green: 2,062 passed, 709 skipped, 2 failed, 35 errors. Every listed failure/error is in chip-design integration and reaches socket.AF_UNIX, unavailable on this Windows Python runtime. No skips, platform shims or unrelated test changes were added to hide this.
  • Actual sampled HyperGrid trajectories and three SGD updates through SubTBGFlowNet: vectorized and stepwise losses/gradients agree; losses 8.55848026 -> 8.07797337 -> 7.63690805, maximum observed parameter-gradient difference 7.153e-7.
  • Separate read-only AI code review found no blocker.

No GPU or research-quality/benchmark improvement claim. This corrects probability accounting, not the underlying model design.

Fixes #534.

Review and assistance

I supervised and checked the AI-assisted work as it progressed. AI assistance was used for investigation, implementation and verification. No maintainer review or approval is claimed. Verification was CPU-only; no GPU validation is claimed.

@codecov

codecov Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 75.62%. Comparing base (f39bfc9) to head (5a58b4d).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #535      +/-   ##
==========================================
- Coverage   75.62%   75.62%   -0.01%     
==========================================
  Files          62       62              
  Lines       11329    11327       -2     
  Branches     1670     1669       -1     
==========================================
- Hits         8568     8566       -2     
  Misses       2234     2234              
  Partials      527      527              
Files with missing lines Coverage Δ
src/gfn/utils/prob_calculations.py 83.49% <ø> (-0.16%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@josephdviviano

Copy link
Copy Markdown
Collaborator

on it

…formula

- get_trajectory_pbs: restore the per-step PB mask formula in the docstring.
- test_trajectory_pb_vectorized_vs_nonvectorized_parity: also cover
  trajectories resumed from interior states, and check that the vectorized
  and per-step paths give the same gradients.
- test_resumed_trajectory_pb_probabilities_and_gradients: also check the
  weight gradient analytically, and that per-step and vectorized gradients
  match.
- test_local_search_metropolis_hastings_acceptance_ratio: check that
  LocalSearchSampler's Metropolis-Hastings step accepts with the correct
  ratio, for vectorized and per-step PB, on HyperGrid and DiscreteEBM.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@josephdviviano

Copy link
Copy Markdown
Collaborator

Thanks for the fix! I pushed one commit on top (5a58b4d) w tests and a docstring touch-up.

Docstring: restored the per-step PB mask formula in get_trajectory_pbs.
Gradient parity: test_trajectory_pb_vectorized_vs_nonvectorized_parity now also runs on trajectories resumed from interior states. It checks that the vectorized and per-step paths give the same gradients.
Analytic gradients: test_resumed_trajectory_pb_probabilities_and_gradients also checks the weight gradient analytically, and that both paths give the same gradients.
Local search: new test_local_search_metropolis_hastings_acceptance_ratio checks that LocalSearchSampler's Metropolis-Hastings step accepts with the correct ratio. It covers vectorized and per-step PB on HyperGrid and DiscreteEBM. Local search is the main place the library computes PB on resumed trajectories currently but it may well be needed in new modules down the line.

@davrens101

davrens101 commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor Author

Rechecked your head 5a58b4d locally: the resumed-trajectory gradient-parity, analytic-gradient and local-search MH cases all pass (7 passed), so I'm not changing the branch. The failed Claude review run dies before reviewing anything ("Unable to get ACTIONS_ID_TOKEN_REQUEST_URL env variable", then the missing id-token: write error). I left workflow permissions alone so this fix stays code-only. This check was AI-assisted.

@josephdviviano
josephdviviano merged commit 754612b into GFNOrg:master Nov 6, 2026
6 of 8 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.

Stepwise backward probabilities drop the first transition of resumed trajectories

2 participants