[Bugfix] Make GPU sync checks safe under torch.compile (#56904)
Signed-off-by: Kevin Luu <51931015+khluu@users.noreply.github.com> Co-authored-by: Sherlock <sherlock@raft.ai> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
Sherlock
mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent
6ecd97f1b7
commit
af1c01499b
@@ -225,6 +225,16 @@ _torch_to = torch.Tensor.to
|
||||
_torch_cuda = torch.Tensor.cuda
|
||||
_torch_copy_ = torch.Tensor.copy_
|
||||
|
||||
|
||||
# Once ``Tensor.to`` is replaced below, Dynamo sees the saved C++ descriptor
|
||||
# through this module global instead of as a recognized tensor method. Put the
|
||||
# descriptor behind a weak-referenceable Python callable so ``allow_in_graph``
|
||||
# can admit it and the downstream compiler can lower the original operation.
|
||||
@torch.compiler.allow_in_graph
|
||||
def _torch_to_in_graph(self, *args, **kwargs):
|
||||
return _torch_to(self, *args, **kwargs)
|
||||
|
||||
|
||||
# torch's own parser for `Tensor.to`'s overloaded signature, returning
|
||||
# (device, dtype, non_blocking, memory_format) (also used by torch._dynamo).
|
||||
_parse_to = getattr(torch._C._nn, "_parse_to", None)
|
||||
@@ -232,7 +242,7 @@ _parse_to = getattr(torch._C._nn, "_parse_to", None)
|
||||
|
||||
def _active_check_mode() -> str | None:
|
||||
"""The mode the calling thread is being checked in, if any."""
|
||||
if not _sync_check_enabled or _allow_depth.get() or torch.compiler.is_compiling():
|
||||
if not _sync_check_enabled or torch.compiler.is_compiling() or _allow_depth.get():
|
||||
return None
|
||||
return _checking.get()
|
||||
|
||||
@@ -296,7 +306,7 @@ def _checked_to(self, *args, **kwargs):
|
||||
else:
|
||||
if non_blocking and (reason := _cpu_copy_stall_reason(self, device)):
|
||||
_report_implicit_sync("to", "H2D", reason, mode)
|
||||
return _torch_to(self, *args, **kwargs)
|
||||
return _torch_to_in_graph(self, *args, **kwargs)
|
||||
|
||||
|
||||
def _checked_cuda(self, *args, **kwargs):
|
||||
|
||||
Reference in New Issue
Block a user