Fix small-angle quaternion gradients #2021
<!-- This is an auto-generated comment: summarize by coderabbit.ai -->
<!-- review_stack_entry_start -->
<a href="https://app.coderabbit.ai/change-stack/NVIDIA/warp/pull/2021?cs_source=review_comment"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui-dark.svg?v=2" alt="Review in Change Stack →" width="220" height="32"></a>
Navigate logical layers of code changes, visualize relationships, and explore their blast radius.
<!-- review_stack_entry_end -->
<details>
<summary>🧰 Additional context used</summary>
<details>
<summary>📚 Code guidelines (1)</summary>
AGENTS.md — auto-discovered
</details>
</details>
<!-- recent_review_start -->
No actionable comments were generated in the recent review. 🎉
<details>
<summary>ℹ️ Recent review info</summary>
<details>
<summary>⚙️ Run configuration</summary>
Configuration used: Repository: NVIDIA/warp/.coderabbit.yml
Review profile: CHILL
Plan: Enterprise
Run ID: 54ba89e6-d085-4e78-b05a-2a8c5c5b4e8c
</details>
<details>
<summary>📥 Commits</summary>
Reviewing files that changed from the base of the PR and between 2edb7567a6ebed22bc1c7d6b7fe0dfd2ef3f954f and a7926e17a88ec33e2466c81ad85104a3a94652c5.
</details>
<details>
<summary>📒 Files selected for processing (1)</summary>
warp/tests/test_quat.py</details>
Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 9 remain after this review.
</details>
---
<!-- recent_review_end -->
<!-- walkthrough_start -->
<details>
<summary>📝 Walkthrough</summary>
Walkthrough
The quaternion-to-axis-angle adjoint now computes axis-derivative coefficients from normalized imaginary quaternion components, instead of using inverse-cube scaling. A regression test checks small-angle gradients, and the changelog records the fix.
Changes
Quaternion Gradient
|Layer / File(s)|Summary|
|---|---|
|Adjoint calculation and regression test <br> warp/native/quat.h, warp/tests/test_quat.py, changelog/2020.fixed.md|The adjoint calculates axis derivatives from normalized imaginary components, using a flip / l scale. The test checks small-angle gradients for float16, float32, and float64. The changelog records the NaN-gradient fix.|
<!-- change_assessment_start -->
Priority: ➖ Normal
Estimated code review effort: 2 (Simple) | ~10 minutes
<!-- change_assessment_commit:"a7926e17a88ec33e2466c81ad85104a3a94652c5" -->
Change: Bug fix · Severity of issue fixed: Medium
<!-- change_assessment_end -->
Suggested reviewers: shi-eric
</details>
<!-- walkthrough_end -->
<!-- final_review_risk_start -->
Merge Risk: _⚪ Minimal_ · up to a7926
<!-- final_review_risk_coverage:{"sourceCommitId":"a7926e17a88ec33e2466c81ad85104a3a94652c5","coveredCommitId":"a7926e17a88ec33e2466c81ad85104a3a94652c5","kind":"reviewed"} -->
The change addresses the reported small-angle gradient issue, with no newly introduced material problem established. Merge after normal checks.
<!-- final_review_risk_end -->
<!-- pre_merge_checks_walkthrough_start -->
<details>
<summary>🚥 Pre-merge checks | ✅ 4 | ❌ 1</summary>
❌ Failed checks (1 warning)
| Check name | Status | Explanation | Resolution |
| :----------------: | :--------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------- |
| Docstring Coverage | ⚠️ Warning | Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files. | Write docstrings for the functions missing them to satisfy the coverage threshold. |
<details>
<summary>✅ Passed checks (4 passed)</summary>
| Check name | Status | Explanation |
| :------------------------: | :------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
| Title check | ✅ Passed | The title, "Fix small-angle quaternion gradients," directly and concisely describes the main change to wp.quat_to_axis_angle() gradients. |
| Linked Issues check | ✅ Passed | Issue #2020 requires finite small-angle gradients for wp.quat_to_axis_angle(). warp/native/quat.h now uses the normalized quaternion vector and flip / l, so it avoids the overflowing 1 / l^3 i… |
| Out of Scope Changes check | ✅ Passed | The changes are limited to the Issue #2020 implementation, regression tests, and a related changelog entry. No unrelated implementation or test changes are identified. |
</details>
</details>
<!-- pre_merge_checks_walkthrough_end -->
<!-- finishing_touch_checkbox_start -->
<details>
<summary>✨ Finishing Touches</summary>
<details open>
<summary>🧪 Generate unit tests (beta)</summary>
</details>
</details>
<!-- finishing_touch_checkbox_end -->
<!-- autopilot:start -->
Autopilot is currently an internal CodeRabbit preview.
<!-- autopilot:end -->
<!-- tips_start -->
---
<sub>Comment @coderabbitai help to get the list of available commands.</sub>
<!-- tips_end -->
<!-- greptile_summary -->
<h2><a href="https://app.greptile.com/api/retrigger?id=72951426"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/RetriggerDark.svg?v=2"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2"><img alt="Retrigger" src="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2" align="right"></picture></a>Confidence Score: 5/5</h2>
[Medium risk] Fixes numerical stability in quaternion gradient computation\.
The PR appears safe to merge; no outstanding finding or new actionable issue was identified.
<h3>Summary</h3>
The PR rewrites the quaternion axis-angle adjoint to avoid an overflowing cubic inverse at small angles and adds regression coverage for axis and angle gradients.
<!-- greptile_confidence_score:5 -->
<sub>Reviews (2) · Last reviewed commit: "test: cover small-angle quaternion axis ..."</sub>
<a href="#"><img alt="P2" src="https://greptile-static-assets.s3.amazonaws.com/badges/p2.svg?v=9" align="top"></a> Axis gradients remain unchecked
This assertion selects coordinate 3, the angle. It catches NaNs that leak from the axis calculations, but it cannot tell whether the rewritten axis derivatives return the correct finite values at small angles. Please also assert an axis-coordinate gradient here; otherwise a finite but incorrect axis Jacobian could pass the regression test.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Added an axis-coordinate regression assertion for the same small-angle quaternion cases. It now checks the Y-axis output gradient against the expected normalized-axis Jacobian, in addition to the existing angle gradient assertion. Focused and full quaternion tests pass on the CPU-only macOS build.
efegokdemir
What problem are you trying to solve?
Closes #2020. The adjoint of
wp.quat_to_axis_angle()computes Jacobian terms with1 / |q.xyz|^3. For sufficiently small rotations, that intermediate overflows in float16/float32; multiplying the resulting infinity by zero produces NaN gradients even though the angle gradient is finite.What does this change?
Compute the same Jacobian using the normalized quaternion vector and
1 / |q.xyz|, avoiding the overflowing cubic inverse while preserving the existing behavior for ordinary rotations. Add a focused small-rotation regression and a changelog fragment.Existing pull requests
Validation
mainwith NaN gradients for float16 at 0.03 rad and float32 at 1e-13 rad. After the fix, the existing quaternion gradient suite passes and checks the analytic[2, 0, 0, -angle]gradient for float16, float32, and float64.uv run warp/tests/test_quat.py -q(85 passed);uvx pre-commit run --files warp/native/quat.h warp/tests/test_quat.py changelog/2020.fixed.md(passed);git diff --check(passed).Checklist
mainand contains one commit, unless a maintainer agreed to keep the change as multiple commits.<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
Summary by CodeRabbit
Bug Fixes
Improved gradient calculations when converting quaternions to axis-angle for small rotations, preventing NaN gradients and providing more reliable axis and angle gradients across supported numeric types. Small-angle gradient behavior is now covered by additional checks.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->