Skip to content

Detect torch function in lists as well #160256

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

Open
wants to merge 6 commits into
base: gh/ezyang/3128/base
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update
[ghstack-poisoned]
  • Loading branch information
ezyang committed Aug 11, 2025
commit c8f54e74da47e6be1c87d765de22ab42d6ed25b6
16 changes: 8 additions & 8 deletions test/test_overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,9 +904,9 @@ def __torch_function__(self, func, types, args=(), kwargs=None):

# High priority should be called first
self.assertEqual(call_order[0], 'high',
"Higher priority torch_function should be called first")
"Higher priority torch_function should be called first")
self.assertEqual(call_order[1], 'low',
"Lower priority torch_function should be called after NotImplemented")
"Lower priority torch_function should be called after NotImplemented")

def test_torch_function_mixed_lists(self):
"""Test lists with mix of regular values and torch function objects"""
Expand Down Expand Up @@ -941,7 +941,7 @@ def __index__(self):
_ = F.pad(x, [1, obj, 0, 0])

self.assertEqual(CountingInt.call_count, 1,
"torch_function should be called exactly once for mixed list")
"torch_function should be called exactly once for mixed list")

def test_torch_function_empty_lists(self):
"""Test that empty lists work correctly"""
Expand All @@ -954,7 +954,7 @@ def test_torch_function_empty_lists(self):
# but empty size lists should work
result = x.view([]) # Empty list means scalar
self.assertEqual(result.shape, torch.Size([]),
"Empty list should work for size arguments")
"Empty list should work for size arguments")

def test_torch_function_not_first_in_list(self):
"""Test that torch_function is called even when object is not first in list"""
Expand All @@ -978,15 +978,15 @@ def __index__(self):

# Test with torch_function object as second item
obj_second = IntLikeNotFirst(2)
result = F.pad(x, [1, obj_second, 0, 0])
_ = F.pad(x, [1, obj_second, 0, 0])
self.assertTrue(obj_second.torch_function_called,
"torch_function should be called when object is second in list")
"torch_function should be called when object is second in list")

# Test with torch_function object as third item
obj_third = IntLikeNotFirst(1)
result = F.pad(x, [1, 1, obj_third, 0])
_ = F.pad(x, [1, 1, obj_third, 0])
self.assertTrue(obj_third.torch_function_called,
"torch_function should be called when object is third in list")
"torch_function should be called when object is third in list")

# Test with torch_function object as last item
obj_last = IntLikeNotFirst(1)
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.