Fix unravel_index coordinate order in TensorFlow backend - #23434
Fix unravel_index coordinate order in TensorFlow backend#23434divyashreepathihalli merged 3 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request simplifies and corrects the unravel_index implementation in the TensorFlow backend. It removes the redundant conditional block for 1D indices and the incorrect forward-loop logic for multi-dimensional indices, replacing them with a single, unified loop that iterates over the shape in reverse order. This correctly handles multi-dimensional inputs and asymmetric shapes without requiring explicit reshaping. Additionally, corresponding unit tests for asymmetric shapes and multi-dimensional inputs have been added to verify correctness against NumPy. I have no feedback to provide as the changes are clean, correct, and well-tested.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #23434 +/- ##
==========================================
+ Coverage 84.90% 85.24% +0.33%
==========================================
Files 468 468
Lines 70651 73037 +2386
Branches 11708 12248 +540
==========================================
+ Hits 59986 62258 +2272
- Misses 7658 7722 +64
- Partials 3007 3057 +50
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
@jules can you verify the changes in this PR? |
JulesI have verified the changes in this PR. The PR successfully fixes a bug in Analysis:
The fix is mathematically correct, simplifies the codebase, and is well-tested. Asked by @divyashreepathihalli · session |
Description
This PR resolves a discrepancy in ops.unravel_index between the TensorFlow backend and NumPy (and other Keras backends). Previously, the TF backend implementation had separate logic for 1D and non-1D indices. The non-1D logic incorrectly iterated through the shape in forward order while calculating coordinates, leading to inverted results for asymmetric shapes.
Example of the bug:
reproduction script: https://colab.research.google.com/drive/1SdZRRVDUV3Dg8ORpTQmlOIA_U4f3_yyo?resourcekey=0-7XMyFCvdBdNJLhrdHE9Whw&usp=sharing
Contributor Agreement
Please review our AI-Assisted Contribution Policy and check all boxes below before submitting your PR for review:
Note: Failing to adhere to this agreement may result in your future PRs no longer being reviewed.