Avoids calling builtin iter if object is a generator (#162521)

The `iter(gen)` call will return the given `gen` object. So, we just avoid this call and shaves off a few ms of tracing time

Pull Request resolved: https://github.com/pytorch/pytorch/pull/162521
Approved by: https://github.com/mlazos
This commit is contained in:
Guilherme Leobas
2025-10-13 09:48:27 -03:00
committed by PyTorch MergeBot
parent 83cbba8759
commit 4e420415e8

View File

@ -1831,6 +1831,8 @@ class BuiltinVariable(VariableTracker):
ret = obj
elif isinstance(obj, variables.RangeVariable):
ret = obj.call_method(tx, "__iter__", [], {})
elif isinstance(obj, variables.LocalGeneratorObjectVariable):
ret = obj # type: ignore[assignment]
else:
# Handle the case where we are iterating over a tuple, list or iterator
ret = self._call_iter_tuple_list(tx, obj, *args, **kwargs)