Fix _find_device host overhead in tuple/dict recursion - #3283
Open
mengluy0125 wants to merge 1 commit into
Open
Conversation
Summary: `_find_device` scans kernel arguments for a `torch.device`/`torch.Tensor` to determine the launch device, recursing into nested `tuple`/`list`/`dict` containers. The recursion passed each element directly as `_find_device(item)`. `_find_device` treats its argument as a tuple of args and iterates it (`for arg in args`). Passing a bare `item` that is itself a `torch.Tensor` made the recursive call iterate over the tensor (`for arg in tensor`), materializing the rows of the tensor on the host before the first row matched the `isinstance(arg, torch.Tensor)` check -- a real per-call host overhead on the hot argument-processing path. A 0-dim scalar tensor also fails to iterate at all. Wrap the recursed element in a single-element tuple, `_find_device((item,))`, so the recursion sees exactly one arg and hits the `torch.Tensor` fast path directly. Behavior is unchanged for every previously-working case; the tensor is no longer iterated. Differential Revision: D114780871
|
@mengluy0125 has exported this pull request. If you are a Meta employee, you can view the originating Diff in D114780871. |
ethche
approved these changes
Aug 6, 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:
_find_devicescans kernel arguments for atorch.device/torch.Tensorto determine the launch device, recursing into nestedtuple/list/dictcontainers. The recursion passed each element directly as_find_device(item)._find_devicetreats its argument as a tuple of args and iterates it (for arg in args). Passing a bareitemthat is itself atorch.Tensormade the recursive call iterate over the tensor (for arg in tensor), materializing the rows of the tensor on the host before the first row matched theisinstance(arg, torch.Tensor)check -- a real per-call host overhead on the hot argument-processing path. A 0-dim scalar tensor also fails to iterate at all.Wrap the recursed element in a single-element tuple,
_find_device((item,)), so the recursion sees exactly one arg and hits thetorch.Tensorfast path directly. Behavior is unchanged for every previously-working case; the tensor is no longer iterated.Differential Revision: D114780871