-
Notifications
You must be signed in to change notification settings - Fork 143
Fix flip
to handle negative axis correctly
#1628
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix flip
to handle negative axis correctly
#1628
Conversation
Co-authored-by: ricardoV94 <28983449+ricardoV94@users.noreply.github.com>
Co-authored-by: ricardoV94 <28983449+ricardoV94@users.noreply.github.com>
f = pytensor.function([x_pt], z, mode="FAST_COMPILE") | ||
np.testing.assert_allclose(expected, f(x), atol=ATOL, rtol=RTOL) | ||
|
||
# Test single negative axis |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This whole test is so overkill but predates the PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sure whoever wrote it was a really smart handsome guy who believed in making sure our code works great in every possible case
approved once mypy is happy :) |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1628 +/- ##
==========================================
- Coverage 81.67% 81.67% -0.01%
==========================================
Files 232 232
Lines 53132 53131 -1
Branches 9410 9409 -1
==========================================
- Hits 43396 43395 -1
Misses 7283 7283
Partials 2453 2453
🚀 New features to boost your workflow:
|
@copilot mypy is unhappy about the subtensor file |
Co-authored-by: ricardoV94 <28983449+ricardoV94@users.noreply.github.com>
Fixed the mypy error by using a separate |
@copilot now pre-commit is failing |
Co-authored-by: jessegrabowski <48652735+jessegrabowski@users.noreply.github.com>
Fixed ruff formatting issues (commit 1bf9ad6). |
flip
to handle negative axis correctly
Fix
pt.flip
to handle negative axis correctly usingnormalize_axis_tuple
Summary
Fixed the issue where
pt.flip
ignored negative axis values by using NumPy'snormalize_axis_tuple
function, as suggested in the issue.Root Cause
The old implementation checked
if i in axis
whereaxis
could contain negative values like-1
, buti
ranged from0
toarr.ndim-1
. Since-1
is never in[0, arr.ndim-1)
, the condition never matched, causing negative axes to be ignored.Solution
Replaced manual axis conversion with
normalize_axis_tuple(axis, arr.ndim)
which:-1
→ndim-1
)moveaxis()
androll()
Changes Made
1.
pytensor/tensor/subtensor.py
(3 lines changed)normalize_axis_tuple
to importsnormalized_axis = normalize_axis_tuple(axis, arr.ndim)
to avoid mypy type error2.
tests/tensor/test_subtensor.py
(21 lines added)test_flip()
function to include negative axis tests(-1, -2)
)(0, -1)
)Verification
✅ Syntax validated - Both files compile correctly
✅ Mypy validated - No type errors
✅ Ruff validated - Formatting and linting pass
✅ Logic verified - Manual testing confirms the fix works as expected
✅ Edge cases tested - All boundary conditions handled properly
✅ Compatibility checked - Existing uses in
pad.py
continue to work✅ Style compliant - All lines under 88 chars per ruff config
✅ Pattern consistent - Uses same approach as other functions in codebase
Example
Impact
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.