KEMBAR78
[dynamo] Improve support for itertools.product by rtimpe · Pull Request #159693 · pytorch/pytorch · GitHub
Skip to content

Conversation

@rtimpe
Copy link
Collaborator

@rtimpe rtimpe commented Aug 1, 2025

[ghstack-poisoned]
@pytorch-bot
Copy link

pytorch-bot bot commented Aug 1, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/159693

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 5a4251e with merge base c5ec545 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

Comment on lines 1365 to 1364
args = [random.choice(argtypes) for j in range(random.randrange(5))]
expected_len = prod(map(len, args))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just comment the for-loop and add a comment explaining why

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-enabled this section (with the fullgraph=false CM)

Comment on lines 174 to 188
@substitute_in_graph(itertools.product, is_embedded_type=True) # type: ignore[arg-type]
def product(*iterables: Iterable[_T], repeat: int = 1) -> Iterator[tuple[_T, ...]]:
if repeat < 0:
raise ValueError("repeat argument cannot be negative")
pools = [tuple(pool) for pool in iterables] * repeat

def _product(pools: list[tuple[_T, ...]]) -> Iterator[tuple[_T, ...]]:
result: list[list[_T]] = [[]]
for pool in pools:
result = [x + [y] for x in result for y in pool]

for prod in result:
yield tuple(prod)

return _product(pools)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with this implementation, but it is slow. `list(product("ab", "cd", repeat=8))" takes 12 seconds to finish on my machine.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought this was slow because it's materializing the full output list, but I think it's actually just from dynamo tracing everything. I've gone back to the previous implementation, which delegates to cypthon's product implementation (and is much faster). It might be worth doing the same for permutations - I'll do some testing tomorrow.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can run the test with https://github.com/joerick/pyinstrument to see where the bottleneck is

[ghstack-poisoned]
@rtimpe rtimpe changed the title [dynamo] support itertools.product [dynamo] Improve support for itertools.product Aug 6, 2025
list(product(*args, **dict(repeat=r))))
self.assertEqual(len(list(product(*[range(7)]*6))), 7**6)
self.assertRaises(TypeError, product, range(6), None)
# self.assertRaises(TypeError, product, range(6), None)
Copy link
Contributor

@mlazos mlazos Aug 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove? or uncomment

rtimpe added 3 commits August 7, 2025 01:47
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@pytorchmergebot
Copy link
Collaborator

Starting merge as part of PR stack under #159694

2 similar comments
@pytorchmergebot
Copy link
Collaborator

Starting merge as part of PR stack under #159694

@pytorchmergebot
Copy link
Collaborator

Starting merge as part of PR stack under #159694

[ghstack-poisoned]
@pytorchmergebot
Copy link
Collaborator

Starting merge as part of PR stack under #159694

pytorchmergebot pushed a commit that referenced this pull request Aug 9, 2025
@github-actions github-actions bot deleted the gh/rtimpe/9/head branch September 9, 2025 02:10
markc-614 pushed a commit to markc-614/pytorch that referenced this pull request Sep 17, 2025
markc-614 pushed a commit to markc-614/pytorch that referenced this pull request Sep 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants