Don't unconditionally import torch._dynamo, it's slow (#162595)

A trivial test on OS X.

Before:

```
real	0m6.550s
user	0m2.532s
sys	0m3.359s
```

After:

```
real	0m2.607s
user	0m1.898s
sys	0m3.344s
```

Signed-off-by: Edward Yang <ezyang@meta.com>
Pull Request resolved: https://github.com/pytorch/pytorch/pull/162595
Approved by: https://github.com/albanD
This commit is contained in:
Edward Yang
2025-09-10 10:35:32 -04:00
committed by PyTorch MergeBot
parent 2dc2613180
commit 1051c7dbc2

View File

@ -68,7 +68,6 @@ import torch.backends.xnnpack
import torch.cuda
from torch import Tensor
from torch._C import ScriptDict, ScriptList # type: ignore[attr-defined]
from torch._dynamo.trace_rules import _as_posix_path
from torch._utils_internal import get_writable_path
from torch._logging.scribe import open_source_signpost
from torch.nn import (
@ -5606,6 +5605,8 @@ class LazyVal:
def munge_exc(e, *, suppress_suffix=True, suppress_prefix=True, file=None, skip=0):
from torch._dynamo.trace_rules import _as_posix_path
if file is None:
file = inspect.stack()[1 + skip].filename # skip one frame