Unify graph-energy summation behind a single core helper - #37
Open
aprotsenko24 wants to merge 1 commit into
Open
Unify graph-energy summation behind a single core helper#37aprotsenko24 wants to merge 1 commit into
aprotsenko24 wants to merge 1 commit into
Conversation
Add fabricpc.core.energy.total_graph_energy(state, structure, *, internal_only) and route the duplicated inline "sum per-node graph energies" loops through it across train.py, train_autoregressive.py, the dashboarding extractors/inference_tracking utils, and the energy-decrease test assertions. Export it from fabricpc.core and add focused unit tests in tests/test_energy.py. internal_only=True skips terminal input nodes (in_degree == 0), the PC training objective; False sums all nodes (evaluation). The helper returns the summed energy without batch normalization; callers divide by batch_size for a per-sample mean. Behavior-preserving: same node set (in_degree==0 skip), same insertion order, no energy computation removed. The pmap padding-trim path in evaluate_pcn and per-node/mean reductions are intentionally left inline (they are not graph-energy reductions). Full test suite green (pytest tests/ -> 217 passed).
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
Across the codebase, "sum the graph's per-node energies" was re-implemented inline in many places (
sum/jnp.sumloops overstructure.nodes). This PR introduces one reusable core function,fabricpc.core.energy.total_graph_energy(state, structure, *, internal_only), and routes every clean call site through it.This is a behavior-preserving deduplication — no energy computation is removed. The per-node energy functionals and the actual reductions are unchanged; the duplicated loops are replaced by one shared, tested helper.
The helper
internal_only=Trueskips terminal input nodes (in_degree == 0) — the PC training objective;Falsesums all nodes (evaluation).batch_sizewhen they want a per-sample mean (several callers add an MSE term, compare relatively, or want the raw sum, so a built-in mean would be wrong for them).structure.nodesin insertion order (float summation is order-sensitive).fabricpc.core(import block +__all__).Call sites routed through the helper
training/train.pyget_graph_param_gradientenergyinternal_only=True,/batch_sizetraining/train.pyeval_stepenergyinternal_only=False,/batch_sizetraining/train.pyevaluate_transformerper-device internal energy (insidevmap)internal_only=True; output-MSE term kept separatetraining/train_autoregressive.pytrain_step_autoregressiveenergyinternal_only=True,/batch_sizeutils/dashboarding/extractors.pyextract_total_energyfloat(... internal_only=True)utils/dashboarding/inference_tracking.pytrain_step_with_historyenergyinternal_only=Falsetests/test_fabricpc.pyinternal_only=True(×2)tests/test_storkey_hopfield.pyinternal_only=True(×2)Plus three focused unit tests for the helper in
tests/test_energy.py.Intentionally left inline
training/train.pyevaluate_pcnmulti-GPU (pmap) path: each node's energy is reshaped across devices and sliced to drop padded samples before summing. The helper sums the full per-node array, so it cannot reproduce the padding-trim — routing it would wrongly include padded samples.jnp.sum(state.energy)returns inside nodeforward()methods, andjnp.mean-based diagnostics — these are single-node returns or mean (not sum) reductions, not graph-level aggregations.Verification
JAX_PLATFORMS=cpu PYTHONPATH=. python -m pytest tests/ -q→ 217 passed.The swaps preserve behavior: same node set (the
in_degree > 0keep is equivalent to the helper'sin_degree == 0skip), same insertion order, and the same/batch_sizenormalization at each caller.