Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/__init__.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/_trace_wrapped_higher_order_op.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/bytecode_analysis.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/bytecode_transformation.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/cache_size.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/callback.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/code_context.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/codegen.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/compiled_autograd.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/comptime.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/config.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/convert_frame.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/create_parameter_op.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/current_scope_id.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/debug_utils.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/decorators.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/device_interface.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/distributed.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/eval_frame.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/exc.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/external_utils.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/funcname_cache.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/hooks.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/logging.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/mutation_guard.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/profiler.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/replay_record.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/resume_execution.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/side_effects.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/source.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/tensor_version_op.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/test_case.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/test_minifier_common.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/testing.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/types.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/backends/__init__.py +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/backends/__pycache__/__init__.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/backends/__pycache__/common.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/backends/__pycache__/cudagraphs.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/backends/__pycache__/debugging.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/backends/__pycache__/distributed.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/backends/__pycache__/inductor.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/backends/__pycache__/onnxrt.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/backends/__pycache__/registry.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/backends/__pycache__/tensorrt.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/backends/__pycache__/torchxla.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/backends/__pycache__/tvm.cpython-311.pyc +0 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/backends/common.py +128 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/backends/cudagraphs.py +256 -0
- data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/backends/debugging.py +336 -0
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (4.98 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/_trace_wrapped_higher_order_op.cpython-311.pyc
ADDED
|
Binary file (6.43 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/bytecode_analysis.cpython-311.pyc
ADDED
|
Binary file (14 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/bytecode_transformation.cpython-311.pyc
ADDED
|
Binary file (71.6 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/cache_size.cpython-311.pyc
ADDED
|
Binary file (4.92 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/callback.cpython-311.pyc
ADDED
|
Binary file (3.97 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/code_context.cpython-311.pyc
ADDED
|
Binary file (1.94 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/codegen.cpython-311.pyc
ADDED
|
Binary file (31 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/compiled_autograd.cpython-311.pyc
ADDED
|
Binary file (29.7 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/comptime.cpython-311.pyc
ADDED
|
Binary file (24 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/config.cpython-311.pyc
ADDED
|
Binary file (9.01 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/convert_frame.cpython-311.pyc
ADDED
|
Binary file (59.1 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/create_parameter_op.cpython-311.pyc
ADDED
|
Binary file (3.51 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/current_scope_id.cpython-311.pyc
ADDED
|
Binary file (1.11 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/debug_utils.cpython-311.pyc
ADDED
|
Binary file (41.4 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/decorators.cpython-311.pyc
ADDED
|
Binary file (27.5 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/device_interface.cpython-311.pyc
ADDED
|
Binary file (20 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/distributed.cpython-311.pyc
ADDED
|
Binary file (1.1 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/eval_frame.cpython-311.pyc
ADDED
|
Binary file (76.9 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/exc.cpython-311.pyc
ADDED
|
Binary file (21.8 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/external_utils.cpython-311.pyc
ADDED
|
Binary file (7.13 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/funcname_cache.cpython-311.pyc
ADDED
|
Binary file (2.88 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/hooks.cpython-311.pyc
ADDED
|
Binary file (955 Bytes). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/logging.cpython-311.pyc
ADDED
|
Binary file (1.87 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/mutation_guard.cpython-311.pyc
ADDED
|
Binary file (7.48 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/profiler.cpython-311.pyc
ADDED
|
Binary file (8.95 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/replay_record.cpython-311.pyc
ADDED
|
Binary file (7.41 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/resume_execution.cpython-311.pyc
ADDED
|
Binary file (28.7 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/side_effects.cpython-311.pyc
ADDED
|
Binary file (37.5 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/source.cpython-311.pyc
ADDED
|
Binary file (50.2 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/tensor_version_op.cpython-311.pyc
ADDED
|
Binary file (2.59 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/test_case.cpython-311.pyc
ADDED
|
Binary file (4.31 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/test_minifier_common.cpython-311.pyc
ADDED
|
Binary file (15.1 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/testing.cpython-311.pyc
ADDED
|
Binary file (22.2 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/__pycache__/types.cpython-311.pyc
ADDED
|
Binary file (5.11 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/backends/__init__.py
ADDED
|
File without changes
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/backends/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (210 Bytes). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/backends/__pycache__/common.cpython-311.pyc
ADDED
|
Binary file (6.75 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/backends/__pycache__/cudagraphs.cpython-311.pyc
ADDED
|
Binary file (13.4 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/backends/__pycache__/debugging.cpython-311.pyc
ADDED
|
Binary file (15.4 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/backends/__pycache__/distributed.cpython-311.pyc
ADDED
|
Binary file (28.8 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/backends/__pycache__/inductor.cpython-311.pyc
ADDED
|
Binary file (558 Bytes). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/backends/__pycache__/onnxrt.cpython-311.pyc
ADDED
|
Binary file (1.57 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/backends/__pycache__/registry.cpython-311.pyc
ADDED
|
Binary file (6.26 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/backends/__pycache__/tensorrt.cpython-311.pyc
ADDED
|
Binary file (296 Bytes). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/backends/__pycache__/torchxla.cpython-311.pyc
ADDED
|
Binary file (2.09 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/backends/__pycache__/tvm.cpython-311.pyc
ADDED
|
Binary file (10.9 kB). View file
|
|
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/backends/common.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# mypy: ignore-errors
|
| 2 |
+
|
| 3 |
+
import contextlib
|
| 4 |
+
import functools
|
| 5 |
+
import logging
|
| 6 |
+
from unittest.mock import patch
|
| 7 |
+
|
| 8 |
+
import torch
|
| 9 |
+
from torch._dynamo import disable
|
| 10 |
+
from torch._dynamo.utils import counters, defake, flatten_graph_inputs
|
| 11 |
+
from torch._functorch.aot_autograd import aot_module_simplified
|
| 12 |
+
from torch.utils._python_dispatch import _disable_current_modes
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
log = logging.getLogger(__name__)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class AotAutograd:
|
| 19 |
+
def __init__(self, **kwargs) -> None:
|
| 20 |
+
self.__name__ = "compiler_fn"
|
| 21 |
+
self.kwargs = kwargs
|
| 22 |
+
|
| 23 |
+
def __call__(self, gm: torch.fx.GraphModule, example_inputs, **kwargs):
|
| 24 |
+
if kwargs:
|
| 25 |
+
log.warning("aot_autograd-based backend ignoring extra kwargs %s", kwargs)
|
| 26 |
+
|
| 27 |
+
if any(isinstance(x, (list, tuple, dict)) for x in example_inputs):
|
| 28 |
+
return flatten_graph_inputs(
|
| 29 |
+
gm,
|
| 30 |
+
example_inputs,
|
| 31 |
+
self,
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# Hack to get around circular import problems with aot_eager_decomp_partition
|
| 35 |
+
if callable(self.kwargs.get("decompositions")):
|
| 36 |
+
self.kwargs["decompositions"] = self.kwargs["decompositions"]()
|
| 37 |
+
|
| 38 |
+
# NB: dont delete counter increment
|
| 39 |
+
counters["aot_autograd"]["total"] += 1
|
| 40 |
+
use_fallback = False
|
| 41 |
+
|
| 42 |
+
if use_fallback:
|
| 43 |
+
log.debug("Unable to use AOT Autograd because graph has mutation")
|
| 44 |
+
counters["aot_autograd"]["not_ok"] += 1
|
| 45 |
+
return gm
|
| 46 |
+
|
| 47 |
+
# OK attempt to compile
|
| 48 |
+
|
| 49 |
+
def _wrapped_bw_compiler(*args, **kwargs):
|
| 50 |
+
# stop TorchDynamo from trying to compile our generated backwards pass
|
| 51 |
+
return disable(disable(bw_compiler)(*args, **kwargs))
|
| 52 |
+
|
| 53 |
+
bw_compiler = self.kwargs.get("bw_compiler") or self.kwargs["fw_compiler"]
|
| 54 |
+
self.kwargs["bw_compiler"] = _wrapped_bw_compiler
|
| 55 |
+
self.kwargs["inference_compiler"] = (
|
| 56 |
+
self.kwargs.get("inference_compiler") or self.kwargs["fw_compiler"]
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
from functorch.compile import nop
|
| 60 |
+
from torch._inductor.debug import enable_aot_logging
|
| 61 |
+
|
| 62 |
+
# debug asserts slow down compile time noticeably,
|
| 63 |
+
# So only default them on when the aot_eager backend is used.
|
| 64 |
+
if self.kwargs.get("fw_compiler", None) == nop:
|
| 65 |
+
patch_config = patch("functorch.compile.config.debug_assert", True)
|
| 66 |
+
else:
|
| 67 |
+
patch_config = contextlib.nullcontext()
|
| 68 |
+
|
| 69 |
+
try:
|
| 70 |
+
# NB: NOT cloned!
|
| 71 |
+
with enable_aot_logging(), patch_config:
|
| 72 |
+
cg = aot_module_simplified(gm, example_inputs, **self.kwargs)
|
| 73 |
+
counters["aot_autograd"]["ok"] += 1
|
| 74 |
+
return disable(cg)
|
| 75 |
+
except Exception:
|
| 76 |
+
counters["aot_autograd"]["not_ok"] += 1
|
| 77 |
+
raise
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def aot_autograd(**kwargs):
|
| 81 |
+
return AotAutograd(**kwargs)
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def mem_efficient_fusion_kwargs(use_decomps):
|
| 85 |
+
from functorch.compile import (
|
| 86 |
+
default_decompositions,
|
| 87 |
+
min_cut_rematerialization_partition,
|
| 88 |
+
ts_compile,
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
kwargs = {
|
| 92 |
+
# these are taken from memory_efficient_fusion()
|
| 93 |
+
"fw_compiler": ts_compile,
|
| 94 |
+
"bw_compiler": ts_compile,
|
| 95 |
+
"partition_fn": min_cut_rematerialization_partition,
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
if use_decomps:
|
| 99 |
+
kwargs["decompositions"] = default_decompositions
|
| 100 |
+
|
| 101 |
+
return kwargs
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
def fake_tensor_unsupported(fn):
|
| 105 |
+
"""
|
| 106 |
+
Decorator for backends that need real inputs. We swap out fake
|
| 107 |
+
tensors for zero tensors.
|
| 108 |
+
"""
|
| 109 |
+
|
| 110 |
+
@functools.wraps(fn)
|
| 111 |
+
def wrapper(model, inputs, **kwargs):
|
| 112 |
+
with _disable_current_modes():
|
| 113 |
+
inputs = list(map(defake, inputs))
|
| 114 |
+
return fn(model, inputs, **kwargs)
|
| 115 |
+
|
| 116 |
+
return wrapper
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
def device_from_inputs(example_inputs) -> torch.device:
|
| 120 |
+
for x in example_inputs:
|
| 121 |
+
if hasattr(x, "device"):
|
| 122 |
+
return x.device
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
def dtype_from_inputs(example_inputs) -> torch.dtype:
|
| 126 |
+
for x in example_inputs:
|
| 127 |
+
if hasattr(x, "dtype"):
|
| 128 |
+
return x.dtype
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/backends/cudagraphs.py
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# mypy: ignore-errors
|
| 2 |
+
|
| 3 |
+
import functools
|
| 4 |
+
from collections import defaultdict
|
| 5 |
+
from typing import Dict, List, Optional
|
| 6 |
+
|
| 7 |
+
import torch
|
| 8 |
+
from torch._dynamo import config
|
| 9 |
+
from torch._dynamo.backends.common import aot_autograd
|
| 10 |
+
from torch._dynamo.backends.debugging import boxed_nop
|
| 11 |
+
from torch._inductor.cudagraph_utils import (
|
| 12 |
+
BoxedDeviceIndex,
|
| 13 |
+
check_multiple_devices_or_any_cpu_nodes,
|
| 14 |
+
format_default_skip_message,
|
| 15 |
+
get_mutation_stack_trace,
|
| 16 |
+
get_placeholder_info,
|
| 17 |
+
log_cudagraph_skip_and_bump_counter,
|
| 18 |
+
)
|
| 19 |
+
from torch._inductor.utils import (
|
| 20 |
+
BoxedBool,
|
| 21 |
+
count_tangents,
|
| 22 |
+
get_first_incompatible_cudagraph_node,
|
| 23 |
+
num_fw_fixed_arguments,
|
| 24 |
+
output_node,
|
| 25 |
+
)
|
| 26 |
+
from torch.multiprocessing.reductions import StorageWeakRef
|
| 27 |
+
|
| 28 |
+
from .registry import register_backend
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def find_input_mutations(g):
|
| 32 |
+
def meta_fk(meta):
|
| 33 |
+
return meta["val"] if "val" in meta else meta["fake_result"]
|
| 34 |
+
|
| 35 |
+
inputs = defaultdict(set)
|
| 36 |
+
input_idx = 0
|
| 37 |
+
mutated_inputs = set()
|
| 38 |
+
for n in g.nodes:
|
| 39 |
+
if n.op == "placeholder":
|
| 40 |
+
if isinstance(meta_fk(n.meta), torch.Tensor):
|
| 41 |
+
inputs[StorageWeakRef(meta_fk(n.meta)._typed_storage())].add(input_idx)
|
| 42 |
+
input_idx += 1
|
| 43 |
+
elif n.op == "call_function":
|
| 44 |
+
if not hasattr(n.target, "_schema"):
|
| 45 |
+
continue
|
| 46 |
+
|
| 47 |
+
schema = n.target._schema
|
| 48 |
+
for i, arg in enumerate(schema.arguments):
|
| 49 |
+
if i < len(n.args):
|
| 50 |
+
argument = n.args[i]
|
| 51 |
+
else:
|
| 52 |
+
if arg.name not in n.kwargs:
|
| 53 |
+
continue
|
| 54 |
+
argument = n.kwargs[arg.name]
|
| 55 |
+
mut_arg = False
|
| 56 |
+
if arg.alias_info:
|
| 57 |
+
if arg.alias_info.is_write:
|
| 58 |
+
mut_arg = True
|
| 59 |
+
if mut_arg:
|
| 60 |
+
# TODO: not correct for args that contain tensors in a struct
|
| 61 |
+
# like list
|
| 62 |
+
mutated_inputs |= inputs[
|
| 63 |
+
StorageWeakRef(meta_fk(argument.meta)._typed_storage())
|
| 64 |
+
]
|
| 65 |
+
|
| 66 |
+
# TODO: error on unrecognized nodes
|
| 67 |
+
return mutated_inputs
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def get_device_node_mapping(gm: torch.fx.GraphModule):
|
| 71 |
+
device_node_mapping: Dict[torch.device, torch.fx.Node] = {}
|
| 72 |
+
for n in gm.graph.nodes:
|
| 73 |
+
t = n.meta.get("val", None)
|
| 74 |
+
if isinstance(t, torch.Tensor) and t.device not in device_node_mapping:
|
| 75 |
+
device_node_mapping[t.device] = n
|
| 76 |
+
return device_node_mapping
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def check_for_mutation_ignore_cuda_graph_managed_tensor(
|
| 80 |
+
aot_model: torch.fx.GraphModule, num_fixed
|
| 81 |
+
) -> Optional[str]:
|
| 82 |
+
mutation_indices = find_input_mutations(aot_model.graph) - set(range(num_fixed))
|
| 83 |
+
if not mutation_indices:
|
| 84 |
+
return None
|
| 85 |
+
|
| 86 |
+
placeholders = get_placeholder_info(aot_model.graph)
|
| 87 |
+
return get_mutation_stack_trace(placeholders, mutation_indices)
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def check_for_skip(aot_model: torch.fx.GraphModule, num_fixed) -> Optional[str]:
|
| 91 |
+
if not config.cudagraph_backend_support_input_mutation:
|
| 92 |
+
if mut_skip := check_for_mutation_ignore_cuda_graph_managed_tensor(
|
| 93 |
+
aot_model, num_fixed
|
| 94 |
+
):
|
| 95 |
+
return mut_skip
|
| 96 |
+
|
| 97 |
+
if skip := check_multiple_devices_or_any_cpu_nodes(
|
| 98 |
+
get_device_node_mapping(aot_model)
|
| 99 |
+
):
|
| 100 |
+
return skip
|
| 101 |
+
|
| 102 |
+
if node := get_first_incompatible_cudagraph_node(aot_model):
|
| 103 |
+
return format_default_skip_message(f"incompatible op ({node.name})")
|
| 104 |
+
|
| 105 |
+
return None
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
def get_device_index(gm) -> int:
|
| 109 |
+
device = next(iter(get_device_node_mapping(gm)))
|
| 110 |
+
assert device.type == "cuda"
|
| 111 |
+
return device.index
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
def get_stack_traces(gm) -> List[Optional[str]]:
|
| 115 |
+
output = output_node(gm)
|
| 116 |
+
assert len(output.args) == 1
|
| 117 |
+
return [
|
| 118 |
+
(arg.stack_trace if isinstance(arg, torch.fx.node.Node) else None)
|
| 119 |
+
for arg in output.args[0]
|
| 120 |
+
]
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
def cudagraphs(dynamo_model, dynamo_inputs):
|
| 124 |
+
from torch._inductor.cudagraph_trees import cudagraphify_impl
|
| 125 |
+
|
| 126 |
+
do_cudagraphs = BoxedBool(True)
|
| 127 |
+
boxed_device_index = BoxedDeviceIndex(None)
|
| 128 |
+
|
| 129 |
+
def forward_cudagraphs(aot_model, aot_inputs, is_inference=False):
|
| 130 |
+
interp = boxed_nop(aot_model, aot_inputs)
|
| 131 |
+
fixed = num_fw_fixed_arguments(len(dynamo_inputs), len(aot_inputs))
|
| 132 |
+
if skip_msg := check_for_skip(aot_model, fixed):
|
| 133 |
+
BoxedBool.disable(do_cudagraphs)
|
| 134 |
+
log_cudagraph_skip_and_bump_counter(
|
| 135 |
+
f"skipping cudagraphs due to {skip_msg}"
|
| 136 |
+
)
|
| 137 |
+
return interp
|
| 138 |
+
|
| 139 |
+
boxed_device_index.set(get_device_index(aot_model))
|
| 140 |
+
out = cudagraphify_impl(
|
| 141 |
+
interp,
|
| 142 |
+
aot_inputs,
|
| 143 |
+
range(fixed),
|
| 144 |
+
device_index=boxed_device_index.value,
|
| 145 |
+
is_backward=False,
|
| 146 |
+
is_inference=False,
|
| 147 |
+
stack_traces=get_stack_traces(aot_model),
|
| 148 |
+
placeholders=get_placeholder_info(aot_model.graph),
|
| 149 |
+
mutated_input_idxs=find_input_mutations(aot_model.graph),
|
| 150 |
+
)
|
| 151 |
+
out._boxed_call = True
|
| 152 |
+
return out
|
| 153 |
+
|
| 154 |
+
def backward_cudagraphs(aot_model, aot_inputs):
|
| 155 |
+
interp = boxed_nop(aot_model, aot_inputs)
|
| 156 |
+
if not do_cudagraphs:
|
| 157 |
+
return aot_model
|
| 158 |
+
|
| 159 |
+
fixed = count_tangents(aot_model)
|
| 160 |
+
if skip_msg := check_for_skip(aot_model, fixed):
|
| 161 |
+
log_cudagraph_skip_and_bump_counter(
|
| 162 |
+
"skipping cudagraphs due to %s", skip_msg
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
# See [Backward Generation Handling]
|
| 166 |
+
manager = torch._inductor.cudagraph_trees.get_manager(
|
| 167 |
+
boxed_device_index.value, create_if_none_exists=False
|
| 168 |
+
)
|
| 169 |
+
assert manager is not None
|
| 170 |
+
|
| 171 |
+
def fn(inputs):
|
| 172 |
+
manager.set_to_running_backward()
|
| 173 |
+
return aot_model(inputs)
|
| 174 |
+
|
| 175 |
+
fn._boxed_call = True
|
| 176 |
+
return fn
|
| 177 |
+
|
| 178 |
+
out = cudagraphify_impl(
|
| 179 |
+
interp,
|
| 180 |
+
aot_inputs,
|
| 181 |
+
range(fixed),
|
| 182 |
+
device_index=get_device_index(aot_model),
|
| 183 |
+
is_backward=True,
|
| 184 |
+
is_inference=False,
|
| 185 |
+
stack_traces=get_stack_traces(aot_model),
|
| 186 |
+
placeholders=get_placeholder_info(aot_model.graph),
|
| 187 |
+
mutated_input_idxs=find_input_mutations(aot_model.graph),
|
| 188 |
+
)
|
| 189 |
+
out._boxed_call = True
|
| 190 |
+
return out
|
| 191 |
+
|
| 192 |
+
aot_cudagraphs = aot_autograd(
|
| 193 |
+
fw_compiler=forward_cudagraphs,
|
| 194 |
+
bw_compiler=backward_cudagraphs,
|
| 195 |
+
inference_compiler=functools.partial(forward_cudagraphs, is_inference=True),
|
| 196 |
+
keep_inference_input_mutations=torch._dynamo.config.cudagraph_backend_keep_input_mutation,
|
| 197 |
+
)
|
| 198 |
+
return aot_cudagraphs(dynamo_model, dynamo_inputs)
|
| 199 |
+
|
| 200 |
+
|
| 201 |
+
class CudagraphsBackend:
|
| 202 |
+
compiler_name = "cudagraphs"
|
| 203 |
+
|
| 204 |
+
@staticmethod
|
| 205 |
+
def reset():
|
| 206 |
+
from torch._inductor.cudagraph_trees import reset_cudagraph_trees
|
| 207 |
+
|
| 208 |
+
reset_cudagraph_trees()
|
| 209 |
+
|
| 210 |
+
@staticmethod
|
| 211 |
+
def __call__(model, inputs):
|
| 212 |
+
return cudagraphs(model, inputs)
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
# aot_cudagraphs only applies CUDA graphs to the graph. It is also helpful
|
| 216 |
+
# for debugging and can serve as a perf baseline.
|
| 217 |
+
register_backend(name="cudagraphs", compiler_fn=CudagraphsBackend())
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
def cudagraphs_inner(model, inputs, copy_outputs=True, copy_inputs=True):
|
| 221 |
+
"""This isn't registered as a backend, but is used in some benchmarks"""
|
| 222 |
+
assert isinstance(inputs, (list, tuple))
|
| 223 |
+
if copy_inputs:
|
| 224 |
+
static_inputs = [torch.zeros_like(x) for x in inputs]
|
| 225 |
+
else:
|
| 226 |
+
static_inputs = list(inputs)
|
| 227 |
+
|
| 228 |
+
# warmup
|
| 229 |
+
torch.cuda.synchronize()
|
| 230 |
+
stream = torch.cuda.Stream()
|
| 231 |
+
stream.wait_stream(torch.cuda.current_stream())
|
| 232 |
+
with torch.cuda.stream(stream):
|
| 233 |
+
model(*inputs)
|
| 234 |
+
stream.synchronize()
|
| 235 |
+
torch.cuda.current_stream().wait_stream(stream)
|
| 236 |
+
torch.cuda.synchronize()
|
| 237 |
+
|
| 238 |
+
# record
|
| 239 |
+
graph = torch.cuda.CUDAGraph()
|
| 240 |
+
with torch.cuda.graph(graph, stream=stream):
|
| 241 |
+
static_outputs = model(*static_inputs)
|
| 242 |
+
if not isinstance(static_outputs, (list, tuple)):
|
| 243 |
+
static_outputs = (static_outputs,)
|
| 244 |
+
|
| 245 |
+
def run(*new_inputs):
|
| 246 |
+
assert len(static_inputs) == len(new_inputs)
|
| 247 |
+
if copy_inputs:
|
| 248 |
+
for dst, src in zip(static_inputs, new_inputs):
|
| 249 |
+
dst.copy_(src)
|
| 250 |
+
graph.replay()
|
| 251 |
+
if copy_outputs:
|
| 252 |
+
return [x.clone() for x in static_outputs]
|
| 253 |
+
else:
|
| 254 |
+
return static_outputs
|
| 255 |
+
|
| 256 |
+
return run
|
data/conda_envs/evalclaw/lib/python3.11/site-packages/torch/_dynamo/backends/debugging.py
ADDED
|
@@ -0,0 +1,336 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# mypy: ignore-errors
|
| 2 |
+
|
| 3 |
+
import dataclasses
|
| 4 |
+
import functools
|
| 5 |
+
import logging
|
| 6 |
+
from importlib import import_module
|
| 7 |
+
from typing import Any, List, Optional
|
| 8 |
+
|
| 9 |
+
import torch
|
| 10 |
+
from functorch.compile import min_cut_rematerialization_partition
|
| 11 |
+
from torch import _guards
|
| 12 |
+
from torch._functorch import config as functorch_config
|
| 13 |
+
from torch._functorch.compilers import ts_compile
|
| 14 |
+
|
| 15 |
+
from .common import aot_autograd
|
| 16 |
+
from .registry import register_debug_backend as register_backend
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
log = logging.getLogger(__name__)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
"""
|
| 23 |
+
This file contains TorchDynamo backends intended for debugging uses.
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
@register_backend
|
| 28 |
+
def eager(gm, fake_tensor_inputs, **kwargs):
|
| 29 |
+
if kwargs:
|
| 30 |
+
log.warning("eager backend ignoring extra kwargs %s", kwargs)
|
| 31 |
+
return gm.forward
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
@register_backend
|
| 35 |
+
def eager_noexcept(gm, fake_tensor_inputs, **kwargs):
|
| 36 |
+
if kwargs:
|
| 37 |
+
log.warning("eager_noexcept backend ignoring extra kwargs %s", kwargs)
|
| 38 |
+
|
| 39 |
+
# This backend is intended to check that dynamo-generated GraphModules
|
| 40 |
+
# do not cause errors.
|
| 41 |
+
def inner(*args):
|
| 42 |
+
try:
|
| 43 |
+
return gm(*args)
|
| 44 |
+
except Exception as e:
|
| 45 |
+
raise torch._dynamo.exc.TorchDynamoException(
|
| 46 |
+
"Unexpected exception when running generated GraphModule"
|
| 47 |
+
) from e
|
| 48 |
+
|
| 49 |
+
return inner
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
@register_backend
|
| 53 |
+
def pre_dispatch_eager(gm, fake_tensor_inputs, **kwargs):
|
| 54 |
+
if kwargs:
|
| 55 |
+
log.warning("pre_dispatch_eager backend ignoring extra kwargs %s", kwargs)
|
| 56 |
+
|
| 57 |
+
from torch.fx.experimental.proxy_tensor import make_fx
|
| 58 |
+
|
| 59 |
+
def runnable_gm(*args):
|
| 60 |
+
return torch.fx.Interpreter(gm).run(*args)
|
| 61 |
+
|
| 62 |
+
pre_dispatch_gm = make_fx(runnable_gm, pre_dispatch=True)(*fake_tensor_inputs)
|
| 63 |
+
pre_dispatch_gm.print_readable()
|
| 64 |
+
|
| 65 |
+
return pre_dispatch_gm
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
@register_backend
|
| 69 |
+
def eager_debug(gm, fake_tensor_inputs, **kwargs):
|
| 70 |
+
if kwargs:
|
| 71 |
+
log.warning("eager_debug backend ignoring extra kwargs %s", kwargs)
|
| 72 |
+
|
| 73 |
+
from torch._subclasses.schema_check_mode import SchemaCheckMode
|
| 74 |
+
|
| 75 |
+
# We could add more debugging bits here.
|
| 76 |
+
# Right now, this backend can be used to check for and error on
|
| 77 |
+
# custom dispatcher ops that have incorrect schemas.
|
| 78 |
+
def inner(*args):
|
| 79 |
+
with SchemaCheckMode():
|
| 80 |
+
return torch.fx.Interpreter(gm).run(*args)
|
| 81 |
+
|
| 82 |
+
return inner
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
@register_backend(name="ts")
|
| 86 |
+
def torchscript(gm, fake_tensor_inputs):
|
| 87 |
+
return torch.jit.script(gm)
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
# used boxed call to discard inputs when they are no longer needed
|
| 91 |
+
def boxed_nop(fx_g, example_inputs):
|
| 92 |
+
def run(args):
|
| 93 |
+
return torch.fx.Interpreter(fx_g).boxed_run(args)
|
| 94 |
+
|
| 95 |
+
run._boxed_call = True
|
| 96 |
+
return run
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
# Useful for debugging purpose
|
| 100 |
+
# aot_eager uses AOT Autograd backend with nop compiler. It is helpful in debugging.
|
| 101 |
+
aot_eager = aot_autograd(
|
| 102 |
+
fw_compiler=boxed_nop,
|
| 103 |
+
partition_fn=min_cut_rematerialization_partition,
|
| 104 |
+
keep_inference_input_mutations=True,
|
| 105 |
+
)
|
| 106 |
+
register_backend(name="aot_eager", compiler_fn=aot_eager)
|
| 107 |
+
|
| 108 |
+
aot_eager_default_partitioner = aot_autograd(
|
| 109 |
+
fw_compiler=boxed_nop, keep_inference_input_mutations=True
|
| 110 |
+
)
|
| 111 |
+
register_backend(
|
| 112 |
+
name="aot_eager_default_partitioner", compiler_fn=aot_eager_default_partitioner
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
# Uses TorchInductor AOT Autograd decomps and partitioner to isolate aot vs
|
| 117 |
+
# inductor problems.
|
| 118 |
+
# aot_eager_decomp_partition just replaces the inductor compiler with nop to help
|
| 119 |
+
# isolate inductor vs aot_eager errors
|
| 120 |
+
def aot_eager_decomp_partition(gm, fake_tensor_inputs, **kwargs):
|
| 121 |
+
if kwargs:
|
| 122 |
+
log.warning(
|
| 123 |
+
"aot_eager_decomp_partition backend ignoring extra kwargs %s", kwargs
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
with functorch_config.patch(unlift_effect_tokens=True):
|
| 127 |
+
return aot_autograd(
|
| 128 |
+
# these are taken from memory_efficient_fusion()
|
| 129 |
+
fw_compiler=boxed_nop,
|
| 130 |
+
bw_compiler=boxed_nop,
|
| 131 |
+
# NB: lambda here is to delay import of inductor
|
| 132 |
+
decompositions=lambda: import_module(
|
| 133 |
+
"torch._inductor.compile_fx"
|
| 134 |
+
).select_decomp_table(),
|
| 135 |
+
partition_fn=functools.partial(
|
| 136 |
+
min_cut_rematerialization_partition, compiler="inductor"
|
| 137 |
+
),
|
| 138 |
+
)(gm, fake_tensor_inputs)
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
register_backend(
|
| 142 |
+
name="aot_eager_decomp_partition", compiler_fn=aot_eager_decomp_partition
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
# AOT Autograd with torchscript backend. Default partitioner.
|
| 147 |
+
# aot_ts uses torchscript backend. We can use this with both nnc and nvfuser
|
| 148 |
+
# by using the relevant fuser with torch.jit.fuser(...)
|
| 149 |
+
aot_ts = aot_autograd(fw_compiler=ts_compile)
|
| 150 |
+
register_backend(name="aot_ts", compiler_fn=aot_ts)
|
| 151 |
+
|
| 152 |
+
# These buggy backends are used for inducing bugs so that we can test
|
| 153 |
+
# our repro extraction / minifier scripts
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
class ReluCompileError(Exception):
|
| 157 |
+
pass
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
class TestingOnlyCompileError(Exception):
|
| 161 |
+
pass
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
@register_backend
|
| 165 |
+
def relu_compile_error_TESTING_ONLY(gm: torch.fx.GraphModule, example_inputs):
|
| 166 |
+
for node in gm.graph.nodes:
|
| 167 |
+
if node.target == torch.relu:
|
| 168 |
+
raise ReluCompileError
|
| 169 |
+
return gm
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
@register_backend
|
| 173 |
+
def relu_runtime_error_TESTING_ONLY(gm: torch.fx.GraphModule, example_inputs):
|
| 174 |
+
for node in gm.graph.nodes:
|
| 175 |
+
if node.target == torch.relu:
|
| 176 |
+
node.target = torch._assert
|
| 177 |
+
node.args = (False, "ReluRuntimeError")
|
| 178 |
+
gm.recompile()
|
| 179 |
+
return gm
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
@register_backend
|
| 183 |
+
def relu_accuracy_error_TESTING_ONLY(gm: torch.fx.GraphModule, example_inputs):
|
| 184 |
+
for node in gm.graph.nodes:
|
| 185 |
+
if node.target == torch.relu:
|
| 186 |
+
node.target = torch.add
|
| 187 |
+
node.args = (node.args[0], 1)
|
| 188 |
+
gm.recompile()
|
| 189 |
+
|
| 190 |
+
return gm
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
@register_backend
|
| 194 |
+
def non_leaf_compile_error_TESTING_ONLY(gm: torch.fx.GraphModule, example_inputs):
|
| 195 |
+
# Require at least one non-trivial thing in the graph,
|
| 196 |
+
# see https://github.com/pytorch/pytorch/issues/102898
|
| 197 |
+
for node in gm.graph.nodes:
|
| 198 |
+
if node.op == "call_function":
|
| 199 |
+
break
|
| 200 |
+
else:
|
| 201 |
+
return gm
|
| 202 |
+
for t in example_inputs:
|
| 203 |
+
if not t.is_leaf:
|
| 204 |
+
raise TestingOnlyCompileError
|
| 205 |
+
return gm
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
@dataclasses.dataclass
|
| 209 |
+
class ExplainOutput:
|
| 210 |
+
"""
|
| 211 |
+
This is the output of :func:`torch._dynamo.explain()`
|
| 212 |
+
There is no reason to create this class directly.
|
| 213 |
+
"""
|
| 214 |
+
|
| 215 |
+
graphs: List[torch.fx.GraphModule]
|
| 216 |
+
graph_count: int
|
| 217 |
+
graph_break_count: int
|
| 218 |
+
break_reasons: List[
|
| 219 |
+
Any
|
| 220 |
+
] # Type is GraphCompileReason but doesn't matter for this purpose
|
| 221 |
+
op_count: int
|
| 222 |
+
ops_per_graph: Optional[List[torch.fx.Node]] = None
|
| 223 |
+
out_guards: Optional[List[_guards.Guard]] = None
|
| 224 |
+
compile_times: Optional[str] = None
|
| 225 |
+
|
| 226 |
+
def __str__(self) -> str:
|
| 227 |
+
output = f"Graph Count: {self.graph_count}\n"
|
| 228 |
+
output += f"Graph Break Count: {self.graph_break_count}\n"
|
| 229 |
+
output += f"Op Count: {self.op_count}\n"
|
| 230 |
+
|
| 231 |
+
output += "Break Reasons:\n"
|
| 232 |
+
for idx, break_reason in enumerate(self.break_reasons):
|
| 233 |
+
output += f" Break Reason {idx+1}:\n"
|
| 234 |
+
output += f" Reason: {break_reason.reason}\n"
|
| 235 |
+
output += " User Stack:\n"
|
| 236 |
+
for frame_summary in break_reason.user_stack:
|
| 237 |
+
output += f" {frame_summary}\n"
|
| 238 |
+
|
| 239 |
+
if self.ops_per_graph is not None:
|
| 240 |
+
output += "Ops per Graph:\n"
|
| 241 |
+
for idx, ops in enumerate(self.ops_per_graph):
|
| 242 |
+
output += f" Ops {idx+1}:\n"
|
| 243 |
+
for op in ops:
|
| 244 |
+
output += f" {op}\n"
|
| 245 |
+
|
| 246 |
+
if self.out_guards is not None:
|
| 247 |
+
output += "Out Guards:\n"
|
| 248 |
+
for i, guard in enumerate(self.out_guards):
|
| 249 |
+
output += f" Guard {i+1}:\n"
|
| 250 |
+
output += f" {str(guard)}"
|
| 251 |
+
|
| 252 |
+
if self.compile_times is not None:
|
| 253 |
+
output += f"Compile Times: {self.compile_times}\n"
|
| 254 |
+
return output
|
| 255 |
+
|
| 256 |
+
|
| 257 |
+
def _explain_graph_detail(
|
| 258 |
+
gm: torch.fx.GraphModule, graphs, op_count, ops_per_graph, break_reasons
|
| 259 |
+
):
|
| 260 |
+
"""
|
| 261 |
+
This function is a utility which processes a torch.fx.GraphModule and
|
| 262 |
+
accumulates information about its ops, graph breaks, and other details. It
|
| 263 |
+
is intended to be used by the ExplainWithBackend class and
|
| 264 |
+
`torch._dynamo.explain()` to provide details from Dynamo's graph capture.
|
| 265 |
+
|
| 266 |
+
Parameters:
|
| 267 |
+
gm (torch.fx.GraphModule): The GraphModule to be processed.
|
| 268 |
+
graphs (list): A list that accumulates all the GraphModules processed.
|
| 269 |
+
op_count (int): The total count of operations in all GraphModules processed so far.
|
| 270 |
+
ops_per_graph (list): A list that accumulates the operations of each GraphModule.
|
| 271 |
+
break_reasons (list): A list that accumulates the reasons for breaks in each GraphModule.
|
| 272 |
+
|
| 273 |
+
Returns:
|
| 274 |
+
tuple: A tuple containing the processed GraphModule, the updated lists of graphs,
|
| 275 |
+
operations per graph, and break reasons, and the updated operation count.
|
| 276 |
+
"""
|
| 277 |
+
graphs.append(gm)
|
| 278 |
+
ops = [node.target for node in gm.graph.nodes if node.op == "call_function"]
|
| 279 |
+
op_count += len(ops)
|
| 280 |
+
ops_per_graph.append(ops)
|
| 281 |
+
if gm.compile_subgraph_reason.graph_break:
|
| 282 |
+
break_reasons.append(gm.compile_subgraph_reason)
|
| 283 |
+
|
| 284 |
+
return gm, graphs, op_count, ops_per_graph, break_reasons
|
| 285 |
+
|
| 286 |
+
|
| 287 |
+
class ExplainWithBackend:
|
| 288 |
+
"""
|
| 289 |
+
This class is intended to be used as a backend for `torch.compile`. It is
|
| 290 |
+
composable with other backends. When used in this way, it accumulates
|
| 291 |
+
information about graph breaks, ops, and other info and provides a string
|
| 292 |
+
representation summarizing this information.
|
| 293 |
+
|
| 294 |
+
Attributes:
|
| 295 |
+
backend (str): The name of the backend to use for optimization.
|
| 296 |
+
graphs (list): A list of the graphs captured by TorchDynamo.
|
| 297 |
+
op_count (int): The total number of operations in all optimized graphs.
|
| 298 |
+
break_reasons (list): A list of graph break reasons with stack traces.
|
| 299 |
+
|
| 300 |
+
Example Usage:
|
| 301 |
+
def fn(x):
|
| 302 |
+
x = torch.sigmoid(x)
|
| 303 |
+
return x
|
| 304 |
+
|
| 305 |
+
torch._dynamo.reset()
|
| 306 |
+
eb = ExplainWithBackend("inductor")
|
| 307 |
+
optimized_fn = torch.compile(fn, backend=eb)
|
| 308 |
+
result = optimized_fn(torch.randn(5))
|
| 309 |
+
print(eb.output())
|
| 310 |
+
"""
|
| 311 |
+
|
| 312 |
+
def __init__(self, backend) -> None:
|
| 313 |
+
from .registry import lookup_backend
|
| 314 |
+
|
| 315 |
+
self.backend = lookup_backend(backend)
|
| 316 |
+
self.graphs = []
|
| 317 |
+
self.op_count = 0
|
| 318 |
+
self.break_reasons = []
|
| 319 |
+
|
| 320 |
+
def __call__(self, gm: torch.fx.GraphModule, example_inputs):
|
| 321 |
+
gm, self.graphs, self.op_count, _, self.break_reasons = _explain_graph_detail(
|
| 322 |
+
gm, self.graphs, self.op_count, [], self.break_reasons
|
| 323 |
+
)
|
| 324 |
+
return self.backend(gm, example_inputs)
|
| 325 |
+
|
| 326 |
+
def output(self) -> ExplainOutput:
|
| 327 |
+
graph_count = len(self.graphs)
|
| 328 |
+
output = ExplainOutput(
|
| 329 |
+
self.graphs,
|
| 330 |
+
graph_count,
|
| 331 |
+
graph_count - 1,
|
| 332 |
+
self.break_reasons,
|
| 333 |
+
self.op_count,
|
| 334 |
+
)
|
| 335 |
+
|
| 336 |
+
return output
|