Repository navigation
Mesh: keep float32 accuracy in transform when TF32 matmuls are allowed - #2319
Conversation
… round float32 coordinates, normals and fields. Signed-off-by: Carmelo Gonzales <43048528+melo-gonzo@users.noreply.github.com>
Signed-off-by: Carmelo Gonzales <43048528+melo-gonzo@users.noreply.github.com>
CODEOWNERS review mapCurrent for commit ⏳ @peterdsharpe — 5 file(s)
No CODEOWNER
Comment |
|
[Medium impact] The changes affect mesh transformation workflows and canonical-frame coordinate calculations. Fix the float16 overflow before merging; valid finite coordinates can become NaN. FindingsSummaryReplaces mesh matrix products with scaled-row sums to avoid TF32 rounding.
Acknowledged intentional changes and costs:
Reviews (1) · Last reviewed commit: "Merge upstream main into fix/mesh-transf..." · Reviewed by Greptile |
…f length zero in thin_matmul. Signed-off-by: Carmelo Gonzales <43048528+melo-gonzo@users.noreply.github.com>
peterdsharpe
left a comment
There was a problem hiding this comment.
Thanks Carmelo, great catch! I reproduced it on an RTX 4090 and a GB300: your new accuracy tests fail their three CUDA cases on main's library and pass here, as does the rest of test/mesh. One suggestion inline makes thin_matmul memory-neutral and a bit faster. With main merged (the CHANGELOG conflicts), this looks good to me.
Signed-off-by: Carmelo Gonzales <43048528+melo-gonzo@users.noreply.github.com>
…ngelog entry of the TF32 fix. Signed-off-by: Carmelo Gonzales <43048528+melo-gonzo@users.noreply.github.com>
PhysicsNeMo Pull Request
Description
Mesh.transformmoves the points with a float32 matrix product,mesh.points @ matrix.T.rotateandscalebuild a matrix and calltransform. The cached centroids, the cached normals, and vector and rank-2 tensor fields go through products of the same kind. On CUDA, a float32 matrix product runs in TF32 whentorch.backends.cuda.matmul.allow_tf32is set, and TF32 rounds each input to about three significant digits. That precision is meant for network layers. Here it rounds the coordinates of the mesh.The setting applies to the whole process. A training script that allows TF32 for its model also allows it for every mesh transform in its data pipeline. NGC PyTorch containers allow it by default through
TORCH_ALLOW_TF32_CUBLAS_OVERRIDE=1. The test suite starts every test withtorch.set_float32_matmul_precision("highest")(full_fp32_matmulintest/conftest.py), so CI cannot see the rounding.Measured on a GB300 with torch 2.14.1, on 200,000 float32 points with coordinates of a few units. Each error is an absolute error divided by the largest value of the result, against the same product in float64.
points @ matrix.T, a rotationpoints @ matrix.T, a diagonal scaleeinsum("ij,bjk,lk->bil", ...)A B300 with torch 2.13.0 gives the same errors with TF32 on.
On a mesh 8 m long, 4.9e-4 is 4 mm. Two vertices closer together than the rounding step can land on the same value, and their face then has zero area.
This PR computes those products without a GEMM, so TF32 does not apply to them.
Changes:
physicsnemo/mesh/utilities/_thin_matmul.py: newthin_matmul(rows, matrix). It computesrows @ matrixas one scaled copy of each matrix row, summed. It keeps the dtype of its inputs and raises aValueErrorwhen the length of the rows differs from the number of matrix rows. It sums float16 and bfloat16 inputs in float32, as a partial sum can overflow float16 where the result does not, and it returns zeros for rows of length zero. It accumulates the sum in place, so it holds no temporaries beyond the result.frame_coordinatesalready computed its product this way and now callsthin_matmul.transforminphysicsnemo/mesh/transformations/geometric.py: the points, the cached cell centroids, and the cached cell and point normals usethin_matmul._transform_tensordictin the same file: one loop over the spatial indices of a field replaces the vector product, the two rank-2einsumcalls and the generatedeinsumstrings for higher ranks.transformraises aValueErrorfor a matrix whose dtype differs from the dtype of the points, and for a vector or tensor field of an integer or boolean dtype. The matrix product raised aRuntimeErrorfor both inputs. The column products would promote them instead.test/mesh/transformations/test_transformations.py:TestTransformAccuracyrotates, scales and shears a float32 sphere of 40,962 points with TF32 allowed, on CPU and on CUDA. It checks the points, a vector field, a rank-2 and a rank-3 tensor field, the cached centroids, the cached cell normals and, for the rotation, the cached point normals. The references are float64 products, and the tolerance is 16 float32epsof the largest value. Two more tests cover the newValueErrorcases.TestTransformEdgeShapescovers a float16 transform whose partial sums exceed the float16 range, and a mesh with zero spatial dimensions.test/mesh/utilities/test_thin_matmul.py:thin_matmulequalsrows @ matrixfor a vector, for rows, for a batch of rows, and for matrices that change the row length. It raises for rows of the wrong length, stays finite for float16 and bfloat16 partial sums beyond the float16 range, and gives zeros for rows of length zero.projecttransforms fields through_transform_tensordict, so itstransform_*_dataflags get the same fix with no change to its file.test_project_selects_field_components_exactlyintest/mesh/projections/test_projections.pyprojects a float32 vector field and a rank-2 field of 50,000 points with TF32 allowed, on CPU and on CUDA, and compares them with the selected components bytorch.equal.CHANGELOG.md.Cost, measured on the same GPU with TF32 off:
einsumwith 69 MiB.transformof a mesh with four caches and one vector field takes 1.31 to 1.44 ms, against 1.13 to 1.28 ms onmain, from 162 to 655,362 points. The difference is 0.16 to 0.19 ms.This PR covers
transformand, through_transform_tensordict,project. Other float32 matrix products on coordinates, in the library or in the examples, were not audited.Tested on a GB300 with torch 2.14.1.
TestTransformAccuracyfails its three CUDA cases on the library files ofmainand passes all six cases here. Putting the matrix product back at any one of the five sites (points, centroids, cell normals, point normals, fields) fails it again.test/meshgives 5,600 passed and 31 skipped here, and 5,591 passed and 31 skipped on the library files ofmainwith the nine new transform tests deselected. With the twoprojectcases added since,test/meshgives 5,602 passed and 31 skipped here, and theprojecttest fails its CUDA case on the library files ofmain. On the Arm CPU of the same machine in float64, the field transforms ofmainand of this branch give equal results for ranks 1 to 4, and so does the wholetransform.Minimal reproducer
Run from a checkout of
mainon a CUDA GPU.Observed on
main(GB300, torch 2.14.1):Observed on this branch:
Expected: errors at float32 round-off, about 1e-6 for these coordinates, at both settings.
Checklist
Dependencies
None.