Fix crash in TensorFlow GNM compute_vertex_normals.#28
Open
vraj130 wants to merge 1 commit into
Open
Conversation
gnm_tensorflow.GNM.compute_vertex_normals crashes on any real mesh with: InvalidArgumentError: data.shape = [35324,3] does not start with segment_ids.shape = [1,105972] [Op:UnsortedSegmentSum] The face normals tensor (B, T, 3) was reshaped straight to (B*T, 3) without replicating each face normal for the three corner vertices it contributes to, while the segment ids enumerate all B*T*3 triangle corners. tf.math.unsorted_segment_sum requires segment_ids.shape to be a prefix of data.shape, so the shapes can never match for a real mesh. The numpy, jax and pytorch backends all replicate the face normal across the three corners (e.g. np.add.at broadcasting in gnm_numpy.py); the tensorflow backend was the only one missing this step, and the only one without test coverage for normals. Fix: tile the face normals across the corner axis before flattening, and flatten the batch-offset segment ids to match, reproducing the numpy backend's semantics. Add test_compute_vertex_normals_numpy_parity to gnm_tensorflow_test, checking TensorFlow normals against the numpy implementation (itself validated against tensorflow_graphics in gnm_numpy_test) over unbatched and multi-dimensional batch shapes. The test reproduces the InvalidArgumentError before this fix and passes after; the full gnm_tensorflow_test module passes.
vraj130
force-pushed
the
fix-tf-vertex-normals
branch
from
July 21, 2026 21:48
d691d6e to
6622b18
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.
gnm_tensorflow.GNM.compute_vertex_normalscrashes on any real mesh:(Reproduced on the bundled V3 head model: the forward pass succeeds, then
computing normals for the resulting
(1, 17821, 3)vertices raises the above.)Root cause: the face-normals tensor
(B, T, 3)is reshaped straight to(B*T, 3)without replicating each face normal for the three corner verticesit contributes to, while the segment ids enumerate all
B*T*3trianglecorners.
tf.math.unsorted_segment_sumrequiressegment_ids.shapeto be aprefix of
data.shape, so the shapes can never match for a real mesh (T > 1).The numpy, jax, and pytorch backends all replicate the face normal across the
three corners (e.g. the
np.add.atbroadcast ingnm_numpy.py); theTensorFlow backend was the only one missing this step and the only backend
with no test coverage for
compute_vertex_normals.Fix: tile the face normals across the corner axis before flattening
(
(B, T, 3, 3)→(B·T·3, 3)), and flatten the batch-offset segment ids tomatch, reproducing the numpy backend's semantics exactly.
Test: adds
test_compute_vertex_normals_numpy_parity, mirroring theexisting
test_parity_with_gnm_numpypattern: TensorFlow normals must matchthe numpy implementation (itself validated against
tensorflow_graphicsingnm_numpy_test.py) over unbatched and multi-dimensional batch shapes(
(),(1,),(1,2),(2,1,2)). The new test fails with theInvalidArgumentErrorabove before this fix and passes after; the fullgnm_tensorflow_testmodule passes.