Return zero energy for a module with no Vodes - #98
Merged
Conversation
functools.reduce was called with no initial value, so an EnergyModule with no EnergyModule children raised TypeError instead of returning zero. The seed is jax.numpy.asarray(0.0) rather than a plain 0.0 because the method documents a jax.Array return and a Python scalar would break that for the empty case. Both are weakly typed, so neither changes the dtype of a non-empty sum. Closes liukidar#72
cemde
force-pushed
the
fix/72-empty-energy
branch
from
August 9, 2026 18:23
2ab71f2 to
3eef477
Compare
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.
Bug
functools.reducewith no initial value raises on an empty iterable rather than returning an identity.Impact
A module with no
EnergyModulechildren raisesTypeError: reduce() of empty iterable with no initial valueinstead of returning zero. That hits any model built up incrementally, and any leaf module in a hierarchy that happens to hold no Vode.Fix
Seed the reduction with
jax.numpy.asarray(0.0).Why that value
Four candidates, measured:
0breaksjax.gradon the empty case: grad requires real-valued outputs, got int32.0.0works numerically but returns a Pythonfloatfor the empty case, contradicting the method's documentedjax.Arrayreturn, and costs one extratydiagnostic.jnp.zeros(())is strongly typed float32, so it silently upcasts a bfloat16 or float16 energy in the non-empty case.jax.numpy.asarray(0.0)is ajax.Arraythat keepsweak_type=True, so it adopts the operand's dtype and leaves the non-empty result untouched.Confirmed bit-identical to main on the non-empty path, both unbatched and under
pxf.vmap.Closes #72