perf(base): speed up trnorm and tr2adjoint - #214
Open
petercorke wants to merge 1 commit into
Open
Conversation
Both functions built small, fixed-size (3x3/4x4/6x6) results out of generic NumPy helpers (np.cross/unitvec/np.stack in trnorm, np.block in tr2adjoint) whose dispatch overhead - built for arbitrary shapes and broadcasting - dominates cost at this size. Same pattern as the isR/ishom speedup in rai-opensource#213. Replaced with explicit scalar arithmetic (trnorm) and direct pre-allocated slice-assignment (tr2adjoint), dtype preserved via np.zeros(..., dtype=T.dtype) so tr2adjoint's documented SymPy support is unaffected. ~12x faster trnorm, ~2.7x faster tr2adjoint standalone. End to end: SE3 @ SE3 (which normalizes via trnorm) drops from ~30us to ~4us. Verified bit-for-bit numeric equivalence against the prior implementation over 200 random SO(3)/SE(3) trials, plus symbolic (SymPy dtype=object) equivalence for tr2adjoint. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This was referenced Aug 21, 2026
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
trnorm(Gram-Schmidt re-orthonormalization used by e.g.SE3.__matmul__/@) andtr2adjoint(adjoint matrix, used bySE3.Ad()/Twist3.Ad()) both built small, fixed-size results (3x3/4x4/6x6) via generic NumPy helpers -np.cross/unitvec/np.stackintrnorm,np.blockintr2adjoint- whose dispatch overhead (built for arbitrary shapes/broadcasting) dominates cost at this size. Same pattern as theisR/ishomspeedup in perf(base): speed up isR/ishom/ishom2 orthogonality checks #213.trnorm: replaced with explicit scalar cross-product/normalization arithmetic and direct in-place array construction.tr2adjoint: replacednp.block([[...]])with a pre-allocatednp.zeros((6,6), dtype=T.dtype)and direct slice assignment.dtype=T.dtypeis preserved specifically so the function's documented:SymPy: supportedcontract still holds for symbolic (dtype=object) input.timeit,trnorm/tr2adjointcalled directly):trnorm~12x faster (27.4us -> 2.3us),tr2adjoint~2.7x faster (8.8us -> 3.3us). End to end,SE3 @ SE3(which normalizes viatrnorm) drops from ~30us to ~4us.SE3.Ad()/base.tr2adjoint()calls on an already-known SE3/rotation matrix land around ~3.3us.Twist3.Ad()itself is not meaningfully faster from this change - its cost is dominated by the separateself.SE3()->trexp()conversion step (matrix exponential), not bytr2adjoint. That's a different, inherent cost (same class asTwist3 * Twist3's "product of exponentials"), not addressed here.Test plan
pytest tests/- 343 passedtrnormandtr2adjoint, both 3x3 and 4x4 forms)dtype=object) equivalence fortr2adjointspecifically, since it's the one with a documented:SymPy: supportedcontract🤖 Generated with Claude Code