applied-ai-018 commited on
Commit
45b75ea
·
verified ·
1 Parent(s): b59b955

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. ckpts/universal/global_step120/zero/15.mlp.dense_h_to_4h.weight/exp_avg.pt +3 -0
  2. ckpts/universal/global_step120/zero/15.mlp.dense_h_to_4h.weight/exp_avg_sq.pt +3 -0
  3. ckpts/universal/global_step120/zero/15.mlp.dense_h_to_4h.weight/fp32.pt +3 -0
  4. ckpts/universal/global_step120/zero/21.attention.dense.weight/exp_avg_sq.pt +3 -0
  5. ckpts/universal/global_step120/zero/21.attention.dense.weight/fp32.pt +3 -0
  6. ckpts/universal/global_step120/zero/4.attention.dense.weight/exp_avg.pt +3 -0
  7. venv/lib/python3.10/site-packages/torch/_export/db/examples/__init__.py +52 -0
  8. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/__init__.cpython-310.pyc +0 -0
  9. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/assume_constant_result.cpython-310.pyc +0 -0
  10. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/autograd_function.cpython-310.pyc +0 -0
  11. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/class_method.cpython-310.pyc +0 -0
  12. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/cond_branch_class_method.cpython-310.pyc +0 -0
  13. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/cond_branch_nested_function.cpython-310.pyc +0 -0
  14. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/cond_branch_nonlocal_variables.cpython-310.pyc +0 -0
  15. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/cond_closed_over_variable.cpython-310.pyc +0 -0
  16. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/cond_operands.cpython-310.pyc +0 -0
  17. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/cond_predicate.cpython-310.pyc +0 -0
  18. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/constrain_as_size_example.cpython-310.pyc +0 -0
  19. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/constrain_as_value_example.cpython-310.pyc +0 -0
  20. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/decorator.cpython-310.pyc +0 -0
  21. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dictionary.cpython-310.pyc +0 -0
  22. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dynamic_shape_assert.cpython-310.pyc +0 -0
  23. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dynamic_shape_constructor.cpython-310.pyc +0 -0
  24. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dynamic_shape_if_guard.cpython-310.pyc +0 -0
  25. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dynamic_shape_map.cpython-310.pyc +0 -0
  26. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dynamic_shape_round.cpython-310.pyc +0 -0
  27. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dynamic_shape_slicing.cpython-310.pyc +0 -0
  28. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dynamic_shape_view.cpython-310.pyc +0 -0
  29. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/fn_with_kwargs.cpython-310.pyc +0 -0
  30. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/list_contains.cpython-310.pyc +0 -0
  31. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/list_unpack.cpython-310.pyc +0 -0
  32. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/model_attr_mutation.cpython-310.pyc +0 -0
  33. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/nested_function.cpython-310.pyc +0 -0
  34. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/null_context_manager.cpython-310.pyc +0 -0
  35. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/optional_input.cpython-310.pyc +0 -0
  36. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/pytree_flatten.cpython-310.pyc +0 -0
  37. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/scalar_output.cpython-310.pyc +0 -0
  38. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/specialized_attribute.cpython-310.pyc +0 -0
  39. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/static_for_loop.cpython-310.pyc +0 -0
  40. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/static_if.cpython-310.pyc +0 -0
  41. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/tensor_setattr.cpython-310.pyc +0 -0
  42. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/torch_sym_min.cpython-310.pyc +0 -0
  43. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/type_reflection_method.cpython-310.pyc +0 -0
  44. venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/user_input_mutation.cpython-310.pyc +0 -0
  45. venv/lib/python3.10/site-packages/torch/_export/db/examples/assume_constant_result.py +24 -0
  46. venv/lib/python3.10/site-packages/torch/_export/db/examples/class_method.py +24 -0
  47. venv/lib/python3.10/site-packages/torch/_export/db/examples/cond_branch_class_method.py +46 -0
  48. venv/lib/python3.10/site-packages/torch/_export/db/examples/cond_branch_nested_function.py +44 -0
  49. venv/lib/python3.10/site-packages/torch/_export/db/examples/cond_branch_nonlocal_variables.py +63 -0
  50. venv/lib/python3.10/site-packages/torch/_export/db/examples/cond_operands.py +39 -0
ckpts/universal/global_step120/zero/15.mlp.dense_h_to_4h.weight/exp_avg.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:75c0eececb9fd5a8fbac12870f7f9459bac43404e6851bc728d7d104ee4037e5
3
+ size 33555612
ckpts/universal/global_step120/zero/15.mlp.dense_h_to_4h.weight/exp_avg_sq.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0a8379030427fb2efc50d6a3163ea8c99d70f29b80392233a0a4e6cafa4ce60f
3
+ size 33555627
ckpts/universal/global_step120/zero/15.mlp.dense_h_to_4h.weight/fp32.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b915f5d379be5bc0206384f4550e01ae4d44761ee5d63b9e775aa9c7ec49187e
3
+ size 33555533
ckpts/universal/global_step120/zero/21.attention.dense.weight/exp_avg_sq.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0ef5663fc4992676357ab082014a4b6f14c7e388c385f2779948ed1de0212f74
3
+ size 16778411
ckpts/universal/global_step120/zero/21.attention.dense.weight/fp32.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2250056ed84e089995ac97203e2b6cfde6272c7c5dd35a6d44f9f146c9b0d43d
3
+ size 16778317
ckpts/universal/global_step120/zero/4.attention.dense.weight/exp_avg.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ba07ef3cd53856a7271abf8c504c9cd767078a8f30a9d8cf383c1687ecb7d433
3
+ size 16778396
venv/lib/python3.10/site-packages/torch/_export/db/examples/__init__.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import glob
2
+ import importlib
3
+ from os.path import basename, dirname, isfile, join
4
+
5
+ import torch
6
+ from torch._export.db.case import (
7
+ _EXAMPLE_CASES,
8
+ _EXAMPLE_CONFLICT_CASES,
9
+ _EXAMPLE_REWRITE_CASES,
10
+ SupportLevel,
11
+ )
12
+
13
+
14
+ modules = glob.glob(join(dirname(__file__), "*.py"))
15
+ __all__ = [
16
+ basename(f)[:-3] for f in modules if isfile(f) and not f.endswith("__init__.py")
17
+ ]
18
+
19
+ # Import all module in the current directory.
20
+ from . import * # noqa: F403
21
+
22
+
23
+ def all_examples():
24
+ return _EXAMPLE_CASES
25
+
26
+
27
+ if len(_EXAMPLE_CONFLICT_CASES) > 0:
28
+
29
+ def get_name(case):
30
+ model = case.model
31
+ if isinstance(model, torch.nn.Module):
32
+ model = type(model)
33
+ return model.__name__
34
+
35
+ msg = "Error on conflict export case name.\n"
36
+ for case_name, cases in _EXAMPLE_CONFLICT_CASES.items():
37
+ msg += f"Case name {case_name} is associated with multiple cases:\n "
38
+ msg += f"[{','.join(map(get_name, cases))}]\n"
39
+
40
+ raise RuntimeError(msg)
41
+
42
+
43
+ def filter_examples_by_support_level(support_level: SupportLevel):
44
+ return {
45
+ key: val
46
+ for key, val in all_examples().items()
47
+ if val.support_level == support_level
48
+ }
49
+
50
+
51
+ def get_rewrite_cases(case):
52
+ return _EXAMPLE_REWRITE_CASES.get(case.name, [])
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (1.84 kB). View file
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/assume_constant_result.cpython-310.pyc ADDED
Binary file (1.27 kB). View file
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/autograd_function.cpython-310.pyc ADDED
Binary file (1.31 kB). View file
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/class_method.cpython-310.pyc ADDED
Binary file (1.14 kB). View file
 
venv/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
 
venv/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
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/cond_branch_nonlocal_variables.cpython-310.pyc ADDED
Binary file (2.64 kB). View file
 
venv/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
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/cond_operands.cpython-310.pyc ADDED
Binary file (1.66 kB). View file
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/cond_predicate.cpython-310.pyc ADDED
Binary file (1.49 kB). View file
 
venv/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
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/constrain_as_value_example.cpython-310.pyc ADDED
Binary file (1.34 kB). View file
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/decorator.cpython-310.pyc ADDED
Binary file (1.07 kB). View file
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dictionary.cpython-310.pyc ADDED
Binary file (1 kB). View file
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dynamic_shape_assert.cpython-310.pyc ADDED
Binary file (1.01 kB). View file
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dynamic_shape_constructor.cpython-310.pyc ADDED
Binary file (1.07 kB). View file
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dynamic_shape_if_guard.cpython-310.pyc ADDED
Binary file (1.02 kB). View file
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dynamic_shape_map.cpython-310.pyc ADDED
Binary file (1.18 kB). View file
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dynamic_shape_round.cpython-310.pyc ADDED
Binary file (1.17 kB). View file
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dynamic_shape_slicing.cpython-310.pyc ADDED
Binary file (1.05 kB). View file
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/dynamic_shape_view.cpython-310.pyc ADDED
Binary file (1.09 kB). View file
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/fn_with_kwargs.cpython-310.pyc ADDED
Binary file (1.24 kB). View file
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/list_contains.cpython-310.pyc ADDED
Binary file (1.1 kB). View file
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/list_unpack.cpython-310.pyc ADDED
Binary file (1.23 kB). View file
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/model_attr_mutation.cpython-310.pyc ADDED
Binary file (1.26 kB). View file
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/nested_function.cpython-310.pyc ADDED
Binary file (1.17 kB). View file
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/null_context_manager.cpython-310.pyc ADDED
Binary file (1.13 kB). View file
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/optional_input.cpython-310.pyc ADDED
Binary file (868 Bytes). View file
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/pytree_flatten.cpython-310.pyc ADDED
Binary file (1.08 kB). View file
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/scalar_output.cpython-310.pyc ADDED
Binary file (1.14 kB). View file
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/specialized_attribute.cpython-310.pyc ADDED
Binary file (1.19 kB). View file
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/static_for_loop.cpython-310.pyc ADDED
Binary file (1.02 kB). View file
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/static_if.cpython-310.pyc ADDED
Binary file (1.01 kB). View file
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/tensor_setattr.cpython-310.pyc ADDED
Binary file (863 Bytes). View file
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/torch_sym_min.cpython-310.pyc ADDED
Binary file (854 Bytes). View file
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/type_reflection_method.cpython-310.pyc ADDED
Binary file (1.86 kB). View file
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/__pycache__/user_input_mutation.cpython-310.pyc ADDED
Binary file (826 Bytes). View file
 
venv/lib/python3.10/site-packages/torch/_export/db/examples/assume_constant_result.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch._dynamo as torchdynamo
3
+
4
+ from torch._export.db.case import export_case
5
+
6
+
7
+ @export_case(
8
+ example_inputs=(torch.ones(3, 2), torch.tensor(4)),
9
+ tags={"torch.escape-hatch"},
10
+ )
11
+ class AssumeConstantResult(torch.nn.Module):
12
+ """
13
+ Applying `assume_constant_result` decorator to burn make non-tracable code as constant.
14
+ """
15
+
16
+ def __init__(self):
17
+ super().__init__()
18
+
19
+ @torchdynamo.assume_constant_result
20
+ def get_item(self, y):
21
+ return y.int().item()
22
+
23
+ def forward(self, x, y):
24
+ return x[: self.get_item(y)]
venv/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)
venv/lib/python3.10/site-packages/torch/_export/db/examples/cond_branch_class_method.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+ from torch._export.db.case import export_case
4
+ from functorch.experimental.control_flow import cond
5
+
6
+
7
+ class MySubModule(torch.nn.Module):
8
+ def foo(self, x):
9
+ return x.cos()
10
+
11
+ def forward(self, x):
12
+ return self.foo(x)
13
+
14
+
15
+ @export_case(
16
+ example_inputs=(torch.ones(3),),
17
+ tags={
18
+ "torch.cond",
19
+ "torch.dynamic-shape",
20
+ },
21
+ )
22
+ class CondBranchClassMethod(torch.nn.Module):
23
+ """
24
+ The branch functions (`true_fn` and `false_fn`) passed to cond() must follow these rules:
25
+ - both branches must take the same args, which must also match the branch args passed to cond.
26
+ - both branches must return a single tensor
27
+ - returned tensor must have the same tensor metadata, e.g. shape and dtype
28
+ - branch function can be free function, nested function, lambda, class methods
29
+ - branch function can not have closure variables
30
+ - no inplace mutations on inputs or global variables
31
+
32
+
33
+ This example demonstrates using class method in cond().
34
+
35
+ NOTE: If the `pred` is test on a dim with batch size < 2, it will be specialized.
36
+ """
37
+
38
+ def __init__(self):
39
+ super().__init__()
40
+ self.subm = MySubModule()
41
+
42
+ def bar(self, x):
43
+ return x.sin()
44
+
45
+ def forward(self, x):
46
+ return cond(x.shape[0] <= 2, self.subm.forward, self.bar, [x])
venv/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])
venv/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
+ )
venv/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])