applied-ai-018 commited on
Commit
6528e3e
·
verified ·
1 Parent(s): 8fd3817

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. llmeval-env/lib/python3.10/site-packages/torch/_export/__pycache__/__init__.cpython-310.pyc +0 -0
  2. llmeval-env/lib/python3.10/site-packages/torch/_export/__pycache__/error.cpython-310.pyc +0 -0
  3. llmeval-env/lib/python3.10/site-packages/torch/_export/__pycache__/exported_program.cpython-310.pyc +0 -0
  4. llmeval-env/lib/python3.10/site-packages/torch/_export/__pycache__/non_strict_utils.cpython-310.pyc +0 -0
  5. llmeval-env/lib/python3.10/site-packages/torch/_export/__pycache__/pass_base.cpython-310.pyc +0 -0
  6. llmeval-env/lib/python3.10/site-packages/torch/_export/__pycache__/utils.cpython-310.pyc +0 -0
  7. llmeval-env/lib/python3.10/site-packages/torch/_export/__pycache__/verifier.cpython-310.pyc +0 -0
  8. llmeval-env/lib/python3.10/site-packages/torch/_export/__pycache__/wrappers.cpython-310.pyc +0 -0
  9. llmeval-env/lib/python3.10/site-packages/torch/_export/db/__init__.py +5 -0
  10. llmeval-env/lib/python3.10/site-packages/torch/_export/db/case.py +188 -0
  11. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/__init__.cpython-310.pyc +0 -0
  12. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/assume_constant_result.cpython-310.pyc +0 -0
  13. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/class_method.cpython-310.pyc +0 -0
  14. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/cond_branch_class_method.cpython-310.pyc +0 -0
  15. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/cond_branch_nested_function.cpython-310.pyc +0 -0
  16. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/cond_closed_over_variable.cpython-310.pyc +0 -0
  17. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/cond_operands.cpython-310.pyc +0 -0
  18. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/cond_predicate.cpython-310.pyc +0 -0
  19. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/constrain_as_size_example.cpython-310.pyc +0 -0
  20. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/decorator.cpython-310.pyc +0 -0
  21. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dictionary.cpython-310.pyc +0 -0
  22. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dynamic_shape_assert.cpython-310.pyc +0 -0
  23. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dynamic_shape_constructor.cpython-310.pyc +0 -0
  24. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dynamic_shape_if_guard.cpython-310.pyc +0 -0
  25. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dynamic_shape_map.cpython-310.pyc +0 -0
  26. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dynamic_shape_round.cpython-310.pyc +0 -0
  27. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dynamic_shape_slicing.cpython-310.pyc +0 -0
  28. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dynamic_shape_view.cpython-310.pyc +0 -0
  29. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/fn_with_kwargs.cpython-310.pyc +0 -0
  30. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/list_unpack.cpython-310.pyc +0 -0
  31. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/nested_function.cpython-310.pyc +0 -0
  32. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/null_context_manager.cpython-310.pyc +0 -0
  33. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/pytree_flatten.cpython-310.pyc +0 -0
  34. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/specialized_attribute.cpython-310.pyc +0 -0
  35. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/static_for_loop.cpython-310.pyc +0 -0
  36. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/static_if.cpython-310.pyc +0 -0
  37. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/tensor_setattr.cpython-310.pyc +0 -0
  38. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/type_reflection_method.cpython-310.pyc +0 -0
  39. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/user_input_mutation.cpython-310.pyc +0 -0
  40. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/autograd_function.py +26 -0
  41. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/class_method.py +24 -0
  42. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/cond_branch_nested_function.py +44 -0
  43. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/cond_branch_nonlocal_variables.py +63 -0
  44. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/cond_closed_over_variable.py +23 -0
  45. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/cond_operands.py +39 -0
  46. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/cond_predicate.py +29 -0
  47. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/constrain_as_size_example.py +27 -0
  48. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/decorator.py +26 -0
  49. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/dictionary.py +21 -0
  50. llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/dynamic_shape_assert.py +22 -0
llmeval-env/lib/python3.10/site-packages/torch/_export/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (13.4 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/__pycache__/error.cpython-310.pyc ADDED
Binary file (2.09 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/__pycache__/exported_program.cpython-310.pyc ADDED
Binary file (1.07 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/__pycache__/non_strict_utils.cpython-310.pyc ADDED
Binary file (6.71 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/__pycache__/pass_base.cpython-310.pyc ADDED
Binary file (14.8 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/__pycache__/utils.cpython-310.pyc ADDED
Binary file (11.4 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/__pycache__/verifier.cpython-310.pyc ADDED
Binary file (12.5 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/__pycache__/wrappers.cpython-310.pyc ADDED
Binary file (4.27 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/db/__init__.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ # Copyright (c) Facebook, Inc. and its affiliates.
2
+ # All rights reserved.
3
+ #
4
+ # This source code is licensed under the BSD-style license found in the
5
+ # LICENSE file in the root directory of this source tree.
llmeval-env/lib/python3.10/site-packages/torch/_export/db/case.py ADDED
@@ -0,0 +1,188 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import inspect
2
+ import re
3
+ import string
4
+ from dataclasses import dataclass, field
5
+ from enum import Enum
6
+ from typing import Any, Dict, List, Optional, Set, Tuple, Union
7
+ from types import ModuleType
8
+
9
+ import torch
10
+
11
+ _TAGS: Dict[str, Dict[str, Any]] = {
12
+ "torch": {
13
+ "cond": {},
14
+ "dynamic-shape": {},
15
+ "escape-hatch": {},
16
+ "map": {},
17
+ "dynamic-value": {},
18
+ "operator": {},
19
+ "mutation": {},
20
+ },
21
+ "python": {
22
+ "assert": {},
23
+ "builtin": {},
24
+ "closure": {},
25
+ "context-manager": {},
26
+ "control-flow": {},
27
+ "data-structure": {},
28
+ "standard-library": {},
29
+ "object-model": {},
30
+ },
31
+ }
32
+
33
+
34
+ class SupportLevel(Enum):
35
+ """
36
+ Indicates at what stage the feature
37
+ used in the example is handled in export.
38
+ """
39
+
40
+ SUPPORTED = 1
41
+ NOT_SUPPORTED_YET = 0
42
+
43
+
44
+ class ExportArgs:
45
+ __slots__ = ("args", "kwargs")
46
+
47
+ def __init__(self, *args, **kwargs):
48
+ self.args = args
49
+ self.kwargs = kwargs
50
+
51
+
52
+ InputsType = Union[Tuple[Any, ...], ExportArgs]
53
+
54
+
55
+ def check_inputs_type(x):
56
+ if not isinstance(x, (ExportArgs, tuple)):
57
+ raise ValueError(
58
+ f"Expecting inputs type to be either a tuple, or ExportArgs, got: {type(x)}"
59
+ )
60
+
61
+
62
+ def _validate_tag(tag: str):
63
+ parts = tag.split(".")
64
+ t = _TAGS
65
+ for part in parts:
66
+ assert set(part) <= set(
67
+ string.ascii_lowercase + "-"
68
+ ), f"Tag contains invalid characters: {part}"
69
+ if part in t:
70
+ t = t[part]
71
+ else:
72
+ raise ValueError(f"Tag {tag} is not found in registered tags.")
73
+
74
+
75
+ @dataclass(frozen=True)
76
+ class ExportCase:
77
+ example_inputs: InputsType
78
+ description: str # A description of the use case.
79
+ model: torch.nn.Module
80
+ name: str
81
+ extra_inputs: Optional[InputsType] = None # For testing graph generalization.
82
+ # Tags associated with the use case. (e.g dynamic-shape, escape-hatch)
83
+ tags: Set[str] = field(default_factory=set)
84
+ support_level: SupportLevel = SupportLevel.SUPPORTED
85
+ dynamic_shapes: Optional[Dict[str, Any]] = None
86
+
87
+ def __post_init__(self):
88
+ check_inputs_type(self.example_inputs)
89
+ if self.extra_inputs is not None:
90
+ check_inputs_type(self.extra_inputs)
91
+
92
+ for tag in self.tags:
93
+ _validate_tag(tag)
94
+
95
+ if not isinstance(self.description, str) or len(self.description) == 0:
96
+ raise ValueError(f'Invalid description: "{self.description}"')
97
+
98
+
99
+ _EXAMPLE_CASES: Dict[str, ExportCase] = {}
100
+ _MODULES: Set[ModuleType] = set()
101
+ _EXAMPLE_CONFLICT_CASES: Dict[str, List[ExportCase]] = {}
102
+ _EXAMPLE_REWRITE_CASES: Dict[str, List[ExportCase]] = {}
103
+
104
+
105
+ def register_db_case(case: ExportCase) -> None:
106
+ """
107
+ Registers a user provided ExportCase into example bank.
108
+ """
109
+ if case.name in _EXAMPLE_CASES:
110
+ if case.name not in _EXAMPLE_CONFLICT_CASES:
111
+ _EXAMPLE_CONFLICT_CASES[case.name] = [_EXAMPLE_CASES[case.name]]
112
+ _EXAMPLE_CONFLICT_CASES[case.name].append(case)
113
+ return
114
+
115
+ _EXAMPLE_CASES[case.name] = case
116
+
117
+
118
+ def to_snake_case(name):
119
+ name = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", name)
120
+ return re.sub("([a-z0-9])([A-Z])", r"\1_\2", name).lower()
121
+
122
+
123
+ def _make_export_case(m, name, configs):
124
+ if not issubclass(m, torch.nn.Module):
125
+ raise TypeError("Export case class should be a torch.nn.Module.")
126
+ m = m()
127
+
128
+ if "description" not in configs:
129
+ # Fallback to docstring if description is missing.
130
+ assert (
131
+ m.__doc__ is not None
132
+ ), f"Could not find description or docstring for export case: {m}"
133
+ configs = {**configs, "description": m.__doc__}
134
+ return ExportCase(**{**configs, "model": m, "name": name})
135
+
136
+
137
+ def export_case(**kwargs):
138
+ """
139
+ Decorator for registering a user provided case into example bank.
140
+ """
141
+
142
+ def wrapper(m):
143
+ configs = kwargs
144
+ module = inspect.getmodule(m)
145
+ if module in _MODULES:
146
+ raise RuntimeError("export_case should only be used once per example file.")
147
+
148
+ assert module is not None
149
+ _MODULES.add(module)
150
+ normalized_name = to_snake_case(m.__name__)
151
+ module_name = module.__name__.split(".")[-1]
152
+ if module_name != normalized_name:
153
+ raise RuntimeError(
154
+ f'Module name "{module.__name__}" is inconsistent with exported program '
155
+ + f'name "{m.__name__}". Please rename the module to "{normalized_name}".'
156
+ )
157
+
158
+ case = _make_export_case(m, module_name, configs)
159
+ register_db_case(case)
160
+ return case
161
+
162
+ return wrapper
163
+
164
+
165
+ def export_rewrite_case(**kwargs):
166
+ def wrapper(m):
167
+ configs = kwargs
168
+
169
+ parent = configs.pop("parent")
170
+ assert isinstance(parent, ExportCase)
171
+ key = parent.name
172
+ if key not in _EXAMPLE_REWRITE_CASES:
173
+ _EXAMPLE_REWRITE_CASES[key] = []
174
+
175
+ configs["example_inputs"] = parent.example_inputs
176
+ case = _make_export_case(m, to_snake_case(m.__name__), configs)
177
+ _EXAMPLE_REWRITE_CASES[key].append(case)
178
+ return case
179
+
180
+ return wrapper
181
+
182
+
183
+ def normalize_inputs(x: InputsType) -> ExportArgs:
184
+ if isinstance(x, tuple):
185
+ return ExportArgs(*x)
186
+
187
+ assert isinstance(x, ExportArgs)
188
+ return x
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (1.84 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/assume_constant_result.cpython-310.pyc ADDED
Binary file (1.27 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/class_method.cpython-310.pyc ADDED
Binary file (1.14 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/cond_branch_class_method.cpython-310.pyc ADDED
Binary file (2.24 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/cond_branch_nested_function.cpython-310.pyc ADDED
Binary file (2.37 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/cond_closed_over_variable.cpython-310.pyc ADDED
Binary file (1.23 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/cond_operands.cpython-310.pyc ADDED
Binary file (1.66 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/cond_predicate.cpython-310.pyc ADDED
Binary file (1.5 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/constrain_as_size_example.cpython-310.pyc ADDED
Binary file (1.28 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/decorator.cpython-310.pyc ADDED
Binary file (1.08 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dictionary.cpython-310.pyc ADDED
Binary file (1.01 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dynamic_shape_assert.cpython-310.pyc ADDED
Binary file (1.02 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dynamic_shape_constructor.cpython-310.pyc ADDED
Binary file (1.07 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dynamic_shape_if_guard.cpython-310.pyc ADDED
Binary file (1.03 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dynamic_shape_map.cpython-310.pyc ADDED
Binary file (1.19 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dynamic_shape_round.cpython-310.pyc ADDED
Binary file (1.17 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dynamic_shape_slicing.cpython-310.pyc ADDED
Binary file (1.06 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dynamic_shape_view.cpython-310.pyc ADDED
Binary file (1.1 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/fn_with_kwargs.cpython-310.pyc ADDED
Binary file (1.24 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/list_unpack.cpython-310.pyc ADDED
Binary file (1.24 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/nested_function.cpython-310.pyc ADDED
Binary file (1.18 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/null_context_manager.cpython-310.pyc ADDED
Binary file (1.14 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/pytree_flatten.cpython-310.pyc ADDED
Binary file (1.09 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/specialized_attribute.cpython-310.pyc ADDED
Binary file (1.2 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/static_for_loop.cpython-310.pyc ADDED
Binary file (1.03 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/static_if.cpython-310.pyc ADDED
Binary file (1.02 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/tensor_setattr.cpython-310.pyc ADDED
Binary file (868 Bytes). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/type_reflection_method.cpython-310.pyc ADDED
Binary file (1.87 kB). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/user_input_mutation.cpython-310.pyc ADDED
Binary file (831 Bytes). View file
 
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/autograd_function.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+ from torch._export.db.case import export_case
4
+
5
+
6
+ class MyAutogradFunction(torch.autograd.Function):
7
+ @staticmethod
8
+ def forward(ctx, x):
9
+ return x.clone()
10
+
11
+ @staticmethod
12
+ def backward(ctx, grad_output):
13
+ return grad_output + 1
14
+
15
+
16
+ @export_case(
17
+ example_inputs=(torch.randn(3, 2),),
18
+ )
19
+ class AutogradFunction(torch.nn.Module):
20
+ """
21
+ TorchDynamo does not keep track of backward() on autograd functions. We recommend to
22
+ use `allow_in_graph` to mitigate this problem.
23
+ """
24
+
25
+ def forward(self, x):
26
+ return MyAutogradFunction.apply(x)
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/class_method.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+ from torch._export.db.case import export_case
4
+
5
+
6
+ @export_case(
7
+ example_inputs=(torch.ones(3, 4),),
8
+ )
9
+ class ClassMethod(torch.nn.Module):
10
+ """
11
+ Class methods are inlined during tracing.
12
+ """
13
+
14
+ @classmethod
15
+ def method(cls, x):
16
+ return x + 1
17
+
18
+ def __init__(self):
19
+ super().__init__()
20
+ self.linear = torch.nn.Linear(4, 2)
21
+
22
+ def forward(self, x):
23
+ x = self.linear(x)
24
+ return self.method(x) * self.__class__.method(x) * type(self).method(x)
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/cond_branch_nested_function.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+ from torch._export.db.case import export_case
4
+ from functorch.experimental.control_flow import cond
5
+
6
+
7
+ @export_case(
8
+ example_inputs=(torch.ones(3),),
9
+ tags={
10
+ "torch.cond",
11
+ "torch.dynamic-shape",
12
+ },
13
+ )
14
+ class CondBranchNestedFunction(torch.nn.Module):
15
+ """
16
+ The branch functions (`true_fn` and `false_fn`) passed to cond() must follow these rules:
17
+ - both branches must take the same args, which must also match the branch args passed to cond.
18
+ - both branches must return a single tensor
19
+ - returned tensor must have the same tensor metadata, e.g. shape and dtype
20
+ - branch function can be free function, nested function, lambda, class methods
21
+ - branch function can not have closure variables
22
+ - no inplace mutations on inputs or global variables
23
+
24
+ This example demonstrates using nested function in cond().
25
+
26
+ NOTE: If the `pred` is test on a dim with batch size < 2, it will be specialized.
27
+ """
28
+ def __init__(self):
29
+ super().__init__()
30
+
31
+ def forward(self, x):
32
+ def true_fn(x):
33
+ def inner_true_fn(y):
34
+ return x + y
35
+
36
+ return inner_true_fn(x)
37
+
38
+ def false_fn(x):
39
+ def inner_false_fn(y):
40
+ return x - y
41
+
42
+ return inner_false_fn(x)
43
+
44
+ return cond(x.shape[0] < 10, true_fn, false_fn, [x])
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/cond_branch_nonlocal_variables.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+ from torch._export.db.case import export_case
4
+ from functorch.experimental.control_flow import cond
5
+
6
+
7
+ @export_case(
8
+ example_inputs=(torch.ones(6),),
9
+ tags={
10
+ "torch.cond",
11
+ "torch.dynamic-shape",
12
+ },
13
+ )
14
+ class CondBranchNonlocalVariables(torch.nn.Module):
15
+ """
16
+ The branch functions (`true_fn` and `false_fn`) passed to cond() must follow these rules:
17
+ - both branches must take the same args, which must also match the branch args passed to cond.
18
+ - both branches must return a single tensor
19
+ - returned tensor must have the same tensor metadata, e.g. shape and dtype
20
+ - branch function can be free function, nested function, lambda, class methods
21
+ - branch function can not have closure variables
22
+ - no inplace mutations on inputs or global variables
23
+
24
+ This example demonstrates how to rewrite code to avoid capturing closure variables in branch functions.
25
+
26
+ The code below will not work because capturing closure variables is not supported.
27
+ ```
28
+ my_tensor_var = x + 100
29
+ my_primitive_var = 3.14
30
+
31
+ def true_fn(y):
32
+ nonlocal my_tensor_var, my_primitive_var
33
+ return y + my_tensor_var + my_primitive_var
34
+
35
+ def false_fn(y):
36
+ nonlocal my_tensor_var, my_primitive_var
37
+ return y - my_tensor_var - my_primitive_var
38
+
39
+ return cond(x.shape[0] > 5, true_fn, false_fn, [x])
40
+ ```
41
+
42
+ NOTE: If the `pred` is test on a dim with batch size < 2, it will be specialized.
43
+ """
44
+
45
+ def __init__(self):
46
+ super().__init__()
47
+
48
+ def forward(self, x):
49
+ my_tensor_var = x + 100
50
+ my_primitive_var = 3.14
51
+
52
+ def true_fn(x, y, z):
53
+ return x + y + z
54
+
55
+ def false_fn(x, y, z):
56
+ return x - y - z
57
+
58
+ return cond(
59
+ x.shape[0] > 5,
60
+ true_fn,
61
+ false_fn,
62
+ [x, my_tensor_var, torch.tensor(my_primitive_var)],
63
+ )
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/cond_closed_over_variable.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+ from torch._export.db.case import export_case
4
+ from functorch.experimental.control_flow import cond
5
+
6
+
7
+ @export_case(
8
+ example_inputs=(torch.tensor(True), torch.ones(3, 2)),
9
+ tags={"torch.cond", "python.closure"},
10
+ )
11
+ class CondClosedOverVariable(torch.nn.Module):
12
+ """
13
+ torch.cond() supports branches closed over arbitrary variables.
14
+ """
15
+
16
+ def forward(self, pred, x):
17
+ def true_fn(val):
18
+ return x * 2
19
+
20
+ def false_fn(val):
21
+ return x - 2
22
+
23
+ return cond(pred, true_fn, false_fn, [x + 1])
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/cond_operands.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+ from torch._export.db.case import export_case
4
+ from torch.export import Dim
5
+ from functorch.experimental.control_flow import cond
6
+
7
+ x = torch.randn(3, 2)
8
+ y = torch.ones(2)
9
+ dim0_x = Dim("dim0_x")
10
+
11
+ @export_case(
12
+ example_inputs=(x, y),
13
+ tags={
14
+ "torch.cond",
15
+ "torch.dynamic-shape",
16
+ },
17
+ extra_inputs=(torch.randn(2, 2), torch.ones(2)),
18
+ dynamic_shapes={"x": {0: dim0_x}, "y": None},
19
+ )
20
+ class CondOperands(torch.nn.Module):
21
+ """
22
+ The operands passed to cond() must be:
23
+ - a list of tensors
24
+ - match arguments of `true_fn` and `false_fn`
25
+
26
+ NOTE: If the `pred` is test on a dim with batch size < 2, it will be specialized.
27
+ """
28
+
29
+ def __init__(self):
30
+ super().__init__()
31
+
32
+ def forward(self, x, y):
33
+ def true_fn(x, y):
34
+ return x + y
35
+
36
+ def false_fn(x, y):
37
+ return x - y
38
+
39
+ return cond(x.shape[0] > 2, true_fn, false_fn, [x, y])
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/cond_predicate.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+ from torch._export.db.case import export_case
4
+ from functorch.experimental.control_flow import cond
5
+
6
+
7
+ @export_case(
8
+ example_inputs=(torch.ones(6, 4, 3),),
9
+ tags={
10
+ "torch.cond",
11
+ "torch.dynamic-shape",
12
+ },
13
+ )
14
+ class CondPredicate(torch.nn.Module):
15
+ """
16
+ The conditional statement (aka predicate) passed to cond() must be one of the following:
17
+ - torch.Tensor with a single element
18
+ - boolean expression
19
+
20
+ NOTE: If the `pred` is test on a dim with batch size < 2, it will be specialized.
21
+ """
22
+
23
+ def __init__(self):
24
+ super().__init__()
25
+
26
+ def forward(self, x):
27
+ pred = x.dim() > 2 and x.shape[2] > 10
28
+
29
+ return cond(pred, lambda x: x.cos(), lambda y: y.sin(), [x])
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/constrain_as_size_example.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+ from torch._export.db.case import export_case
4
+
5
+
6
+ @export_case(
7
+ example_inputs=(torch.tensor(4),),
8
+ tags={
9
+ "torch.dynamic-value",
10
+ "torch.escape-hatch",
11
+ },
12
+ )
13
+ class ConstrainAsSizeExample(torch.nn.Module):
14
+ """
15
+ If the value is not known at tracing time, you can provide hint so that we
16
+ can trace further. Please look at constrain_as_value and constrain_as_size APIs
17
+ constrain_as_size is used for values that NEED to be used for constructing
18
+ tensor.
19
+ """
20
+
21
+ def __init__(self):
22
+ super().__init__()
23
+
24
+ def forward(self, x):
25
+ a = x.item()
26
+ torch._constrain_as_size(a, min=0, max=5)
27
+ return torch.ones((a, 5))
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/decorator.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import functools
2
+
3
+ import torch
4
+
5
+ from torch._export.db.case import export_case
6
+
7
+
8
+ def test_decorator(func):
9
+ @functools.wraps(func)
10
+ def wrapper(*args, **kwargs):
11
+ return func(*args, **kwargs) + 1
12
+
13
+ return wrapper
14
+
15
+
16
+ @export_case(
17
+ example_inputs=(torch.ones(3, 2), torch.ones(3, 2)),
18
+ )
19
+ class Decorator(torch.nn.Module):
20
+ """
21
+ Decorators calls are inlined into the exported function during tracing.
22
+ """
23
+
24
+ @test_decorator
25
+ def forward(self, x, y):
26
+ return x + y
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/dictionary.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+ from torch._export.db.case import export_case
4
+
5
+
6
+ @export_case(
7
+ example_inputs=(torch.ones(3, 2), torch.tensor(4)),
8
+ tags={"python.data-structure"},
9
+ )
10
+ class Dictionary(torch.nn.Module):
11
+ """
12
+ Dictionary structures are inlined and flattened along tracing.
13
+ """
14
+ def __init__(self):
15
+ super().__init__()
16
+
17
+ def forward(self, x, y):
18
+ elements = {}
19
+ elements["x2"] = x * x
20
+ y = y * elements["x2"]
21
+ return {"y": y}
llmeval-env/lib/python3.10/site-packages/torch/_export/db/examples/dynamic_shape_assert.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+ from torch._export.db.case import export_case
4
+
5
+
6
+ @export_case(
7
+ example_inputs=(torch.ones(3, 2),),
8
+ tags={"python.assert"},
9
+ )
10
+ class DynamicShapeAssert(torch.nn.Module):
11
+ """
12
+ A basic usage of python assertion.
13
+ """
14
+ def __init__(self):
15
+ super().__init__()
16
+
17
+ def forward(self, x):
18
+ # assertion with error message
19
+ assert x.shape[0] > 2, f"{x.shape[0]} is greater than 2"
20
+ # assertion without error message
21
+ assert x.shape[0] > 1
22
+ return x