ZhengyangZhang commited on
Commit
cc824a1
·
verified ·
1 Parent(s): 7e15510

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. lib/python3.12/site-packages/torch/_VF.pyi +0 -0
  2. lib/python3.12/site-packages/torch/__config__.py +22 -0
  3. lib/python3.12/site-packages/torch/_custom_ops.py +326 -0
  4. lib/python3.12/site-packages/torch/_environment.py +2 -0
  5. lib/python3.12/site-packages/torch/_guards.py +1180 -0
  6. lib/python3.12/site-packages/torch/_lobpcg.py +1157 -0
  7. lib/python3.12/site-packages/torch/_lowrank.py +294 -0
  8. lib/python3.12/site-packages/torch/_meta_registrations.py +0 -0
  9. lib/python3.12/site-packages/torch/_size_docs.py +39 -0
  10. lib/python3.12/site-packages/torch/_storage_docs.py +42 -0
  11. lib/python3.12/site-packages/torch/_streambase.py +20 -0
  12. lib/python3.12/site-packages/torch/_tensor.py +1837 -0
  13. lib/python3.12/site-packages/torch/_tensor_docs.py +0 -0
  14. lib/python3.12/site-packages/torch/_tensor_str.py +722 -0
  15. lib/python3.12/site-packages/torch/_thread_safe_fork.py +0 -0
  16. lib/python3.12/site-packages/torch/_utils_internal.py +367 -0
  17. lib/python3.12/site-packages/torch/_weights_only_unpickler.py +576 -0
  18. lib/python3.12/site-packages/torch/accelerator/__init__.py +272 -0
  19. lib/python3.12/site-packages/torch/accelerator/__pycache__/__init__.cpython-312.pyc +0 -0
  20. lib/python3.12/site-packages/torch/accelerator/__pycache__/_utils.cpython-312.pyc +0 -0
  21. lib/python3.12/site-packages/torch/accelerator/__pycache__/memory.cpython-312.pyc +0 -0
  22. lib/python3.12/site-packages/torch/accelerator/_utils.py +28 -0
  23. lib/python3.12/site-packages/torch/accelerator/memory.py +201 -0
  24. lib/python3.12/site-packages/torch/functional.py +2195 -0
  25. lib/python3.12/site-packages/torch/hub.py +880 -0
  26. lib/python3.12/site-packages/torch/include/ATen/ATen.h +37 -0
  27. lib/python3.12/site-packages/torch/include/ATen/AccumulateType.h +173 -0
  28. lib/python3.12/site-packages/torch/include/ATen/Backend.h +2 -0
  29. lib/python3.12/site-packages/torch/include/ATen/Backtrace.h +2 -0
  30. lib/python3.12/site-packages/torch/include/ATen/BlasBackend.h +31 -0
  31. lib/python3.12/site-packages/torch/include/ATen/CPUFixedAllocator.h +33 -0
  32. lib/python3.12/site-packages/torch/include/ATen/CPUFunctions.h +29 -0
  33. lib/python3.12/site-packages/torch/include/ATen/CPUFunctions_inl.h +544 -0
  34. lib/python3.12/site-packages/torch/include/ATen/CPUGeneratorImpl.h +49 -0
  35. lib/python3.12/site-packages/torch/include/ATen/CUDAFunctions.h +29 -0
  36. lib/python3.12/site-packages/torch/include/ATen/CUDAFunctions_inl.h +634 -0
  37. lib/python3.12/site-packages/torch/include/ATen/CachedTensorUtils.h +24 -0
  38. lib/python3.12/site-packages/torch/include/ATen/CompositeExplicitAutogradFunctions.h +29 -0
  39. lib/python3.12/site-packages/torch/include/ATen/CompositeExplicitAutogradFunctions_inl.h +560 -0
  40. lib/python3.12/site-packages/torch/include/ATen/CompositeExplicitAutogradNonFunctionalFunctions.h +29 -0
  41. lib/python3.12/site-packages/torch/include/ATen/CompositeExplicitAutogradNonFunctionalFunctions_inl.h +324 -0
  42. lib/python3.12/site-packages/torch/include/ATen/CompositeImplicitAutogradFunctions.h +29 -0
  43. lib/python3.12/site-packages/torch/include/ATen/CompositeImplicitAutogradFunctions_inl.h +503 -0
  44. lib/python3.12/site-packages/torch/include/ATen/CompositeImplicitAutogradNestedTensorFunctions.h +29 -0
  45. lib/python3.12/site-packages/torch/include/ATen/CompositeImplicitAutogradNestedTensorFunctions_inl.h +25 -0
  46. lib/python3.12/site-packages/torch/include/ATen/Config.h +23 -0
  47. lib/python3.12/site-packages/torch/include/ATen/Context.h +688 -0
  48. lib/python3.12/site-packages/torch/include/ATen/DLConvertor.h +69 -0
  49. lib/python3.12/site-packages/torch/include/ATen/DTensorState.h +34 -0
  50. lib/python3.12/site-packages/torch/include/ATen/Device.h +2 -0
lib/python3.12/site-packages/torch/_VF.pyi ADDED
The diff for this file is too large to render. See raw diff
 
lib/python3.12/site-packages/torch/__config__.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+
4
+ def show() -> str:
5
+ """
6
+ Return a human-readable string with descriptions of the
7
+ configuration of PyTorch.
8
+ """
9
+ return torch._C._show_config()
10
+
11
+
12
+ # TODO: In principle, we could provide more structured version/config
13
+ # information here. For now only CXX_FLAGS is exposed, as Timer
14
+ # uses them.
15
+ def _cxx_flags() -> str:
16
+ """Returns the CXX_FLAGS used when building PyTorch."""
17
+ return torch._C._cxx_flags()
18
+
19
+
20
+ def parallel_info() -> str:
21
+ r"""Returns detailed string with parallelization settings"""
22
+ return torch._C._parallel_info()
lib/python3.12/site-packages/torch/_custom_ops.py ADDED
@@ -0,0 +1,326 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # mypy: allow-untyped-defs
2
+ import inspect
3
+
4
+ from torch._custom_op.impl import (
5
+ _custom_op_with_schema,
6
+ _find_custom_op,
7
+ infer_schema,
8
+ parse_qualname,
9
+ validate_namespace,
10
+ )
11
+ from torch.library import get_ctx
12
+
13
+
14
+ __all__ = [
15
+ "custom_op",
16
+ "impl",
17
+ "impl_abstract",
18
+ "get_ctx",
19
+ "impl_save_for_backward",
20
+ "impl_backward",
21
+ ]
22
+
23
+
24
+ def custom_op(qualname, func_or_schema=None):
25
+ r"""Register a new custom operator
26
+
27
+ In PyTorch, defining an op (short for "operator") is a two step-process:
28
+ - we need to define the op (by providing an operator name and schema)
29
+ - we need to implement behavior for how the operator interacts with
30
+ various PyTorch subsystems, like CPU/CUDA Tensors, Autograd, etc.
31
+
32
+ This entrypoint defines the custom operator (the first step)
33
+ you must then perform the second step by calling various
34
+ ``impl_*`` APIs.
35
+
36
+ This API may be used as a decorator (see examples).
37
+
38
+ For a detailed guide on custom ops, please see
39
+ https://docs.google.com/document/d/1aGWtgxV3HppuxQAdddyPrs74_aEntpkYt9MalnCKnhk
40
+
41
+ Arguments:
42
+ qualname (str): Should be a string that looks like
43
+ "namespace::operator_name". Operators in PyTorch need a namespace to
44
+ avoid name collisions; a given operator may only be created once.
45
+ If you are writing a Python library, we recommend the namespace to
46
+ be the name of your top-level module.
47
+ func_or_schema (Union[Callable, str]): Each PyTorch operator needs a
48
+ schema that tells PyTorch the types of the inputs/outputs.
49
+ If this is a Callable, we will automatically infer the schema from
50
+ the type annotations on the function (see examples). Otherwise,
51
+ if you don't want to use type annotations, you may provide us the
52
+ schema string.
53
+
54
+ Example::
55
+
56
+ >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA)
57
+ >>> import torch
58
+ >>> import numpy as np
59
+ >>> from torch import Tensor
60
+ >>>
61
+ >>> # Step 1: define the custom op.
62
+ >>> # We need to provide the API a "prototype function"
63
+ >>> # (a function that returns NotImplementedError), from which
64
+ >>> # we will infer the types of the inputs and outputs.
65
+ >>> @torch._custom_ops.custom_op("mylibrary::numpy_sin")
66
+ >>> def numpy_sin(x: Tensor) -> Tensor:
67
+ >>> raise NotImplementedError
68
+ >>>
69
+ >>> # The custom op is now accessible via the torch.ops module:
70
+ >>> torch.ops.mylibrary.numpy_sin
71
+ >>>
72
+ >>> # Step 2: Register an implementation for various PyTorch subsystems
73
+ >>>
74
+ >>> # Register an implementation for CPU tensors
75
+ >>> @torch._custom_ops.impl("mylibrary::numpy_sin", device_types="cpu")
76
+ >>> def numpy_sin_impl_cpu(x):
77
+ >>> return torch.from_numpy(np.sin(x.numpy()))
78
+ >>>
79
+ >>> # Register an implementation for CUDA tensors
80
+ >>> @torch._custom_ops.impl("mylibrary::numpy_sin", device_types="cuda")
81
+ >>> def numpy_sin_impl_cuda(x):
82
+ >>> return torch.from_numpy(np.sin(x.cpu().numpy())).to(x.device)
83
+ >>>
84
+ >>> x = torch.randn(3)
85
+ >>> torch.ops.mylibrary.numpy_sin(x) # calls numpy_sin_impl_cpu
86
+ >>>
87
+ >>> x_cuda = x.cuda()
88
+ >>> torch.ops.mylibrary.numpy_sin(x) # calls numpy_sin_impl_cuda
89
+
90
+ """
91
+ ns, name = parse_qualname(qualname)
92
+ validate_namespace(ns)
93
+
94
+ def inner(func):
95
+ if not inspect.isfunction(func):
96
+ raise ValueError(
97
+ f"custom_op(...)(func): Expected `func` to be a Python "
98
+ f"function, got: {type(func)}"
99
+ )
100
+
101
+ if func.__name__ != name:
102
+ raise ValueError(
103
+ f"custom_op(qualname='{qualname}', ...)(func): expected `func` "
104
+ f"to have name '{name}' but got '{func.__name__}'. "
105
+ f"Please either change the name of `func` or the qualname that "
106
+ f"is passed to `custom_op`"
107
+ )
108
+
109
+ schema = infer_schema(func, mutates_args=())
110
+ _custom_op_with_schema(qualname, schema)
111
+ return func
112
+
113
+ if func_or_schema is None:
114
+ return inner
115
+ if isinstance(func_or_schema, str):
116
+ _custom_op_with_schema(qualname, func_or_schema)
117
+ else:
118
+ return inner(func_or_schema)
119
+
120
+
121
+ def impl(qualname, *, device_types=("cpu", "cuda"), func=None):
122
+ r"""Register an implementation for a device type for this custom op.
123
+
124
+ If the op is passed multiple Tensor inputs with different device
125
+ types, it will dispatch to the registered implementation for the highest
126
+ priority device type among those present.
127
+ The supported device types, in order of priority, are {'cuda', 'cpu'}.
128
+
129
+ This API may be used as a decorator (see examples).
130
+
131
+ For a detailed guide on custom ops, please see
132
+ https://docs.google.com/document/d/1aGWtgxV3HppuxQAdddyPrs74_aEntpkYt9MalnCKnhk
133
+
134
+ Arguments:
135
+ device_types (str or Iterable[str]): the device type(s) to register the function for.
136
+
137
+ Example::
138
+
139
+ >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA)
140
+ >>> import torch
141
+ >>> import numpy as np
142
+ >>> from torch import Tensor
143
+ >>>
144
+ >>> # Step 1: define the custom op.
145
+ >>> # We need to provide the API a "prototype function"
146
+ >>> # (a function that returns NotImplementedError), from which
147
+ >>> # we will infer the types of the inputs and outputs.
148
+ >>> @torch._custom_ops.custom_op("mylibrary::numpy_cos")
149
+ >>> def numpy_cos(x: Tensor) -> Tensor:
150
+ >>> raise NotImplementedError
151
+ >>>
152
+ >>> # The custom op is now accessible via the torch.ops module:
153
+ >>> torch.ops.mylibrary.numpy_cos
154
+ >>>
155
+ >>> # Step 2: Register an implementation for various PyTorch subsystems
156
+ >>>
157
+ >>> # Register an implementation for CPU tensors
158
+ >>> @torch._custom_ops.impl("mylibrary::numpy_cos", device_types="cpu")
159
+ >>> def numpy_cos_impl_cpu(x):
160
+ >>> return torch.from_numpy(np.cos(x.numpy()))
161
+ >>>
162
+ >>> # Register an implementation for CUDA tensors
163
+ >>> @torch._custom_ops.impl("mylibrary::numpy_cos", device_types="cuda")
164
+ >>> def numpy_cos_impl_cuda(x):
165
+ >>> return torch.from_numpy(np.cos(x.cpu().numpy())).to(x.device)
166
+ >>>
167
+ >>> x = torch.randn(3)
168
+ >>> torch.ops.mylibrary.numpy_cos(x) # calls numpy_cos_impl_cpu
169
+ >>>
170
+ >>> x_cuda = x.cuda()
171
+ >>> torch.ops.mylibrary.numpy_cos(x) # calls numpy_cos_impl_cuda
172
+
173
+ """
174
+
175
+ def inner(func):
176
+ custom_op = _find_custom_op(qualname, also_check_torch_library=True)
177
+ custom_op.impl(device_types, _stacklevel=3)(func)
178
+ return func
179
+
180
+ if func is None:
181
+ return inner
182
+ return inner(func)
183
+
184
+
185
+ def impl_abstract(qualname, *, func=None):
186
+ r"""Register an abstract implementation for this operator.
187
+
188
+ An "abstract implementation" specifies the behavior of this operator on
189
+ Tensors that carry no data. Given some input Tensors with certain properties
190
+ (sizes/strides/storage_offset/device), it specifies what the properties of
191
+ the output Tensors are.
192
+
193
+ The abstract implementation has the same signature as the operator.
194
+ It is run for both FakeTensors and meta tensors. To write an abstract
195
+ implementation, assume that all Tensor inputs to the operator are
196
+ regular CPU/CUDA/Meta tensors, but they do not have storage, and
197
+ you are trying to return regular CPU/CUDA/Meta tensor(s) as output.
198
+ The abstract implementation must consist of only PyTorch operations
199
+ (and may not directly access the storage or data of any input or
200
+ intermediate Tensors).
201
+
202
+ This API may be used as a decorator (see examples).
203
+
204
+ For a detailed guide on custom ops, please see
205
+ https://docs.google.com/document/d/1aGWtgxV3HppuxQAdddyPrs74_aEntpkYt9MalnCKnhk
206
+
207
+ Examples::
208
+ >>> import numpy as np
209
+ >>> from torch import Tensor
210
+ >>>
211
+ >>> # Example 1: an operator without data-dependent output shape
212
+ >>> @torch._custom_ops.custom_op("mylibrary::custom_linear")
213
+ >>> def custom_linear(x: Tensor, weight: Tensor, bias: Tensor) -> Tensor:
214
+ >>> raise NotImplementedError
215
+ >>>
216
+ >>> @torch._custom_ops.impl_abstract("mylibrary::custom_linear")
217
+ >>> def custom_linear_abstract(x, weight):
218
+ >>> assert x.dim() == 2
219
+ >>> assert weight.dim() == 2
220
+ >>> assert bias.dim() == 1
221
+ >>> assert x.shape[1] == weight.shape[1]
222
+ >>> assert weight.shape[0] == bias.shape[0]
223
+ >>> assert x.device == weight.device
224
+ >>>
225
+ >>> return (x @ weight.t()) + bias
226
+ >>>
227
+ >>> # Example 2: an operator with data-dependent output shape
228
+ >>> @torch._custom_ops.custom_op('mylibrary::custom_nonzero')
229
+ >>> def custom_nonzero(x: Tensor) -> Tensor:
230
+ >>> ...
231
+ >>>
232
+ >>> @torch._custom_ops.impl_abstract("mylibrary::custom_nonzero")
233
+ >>> def custom_nonzero_abstract(x):
234
+ >>> # Number of nonzero-elements is data-dependent.
235
+ >>> # Since we cannot peek at the data in an abstract impl,
236
+ >>> # we use the ctx object to construct a new symint that
237
+ >>> # represents the data-dependent size.
238
+ >>> ctx = torch._custom_ops.get_ctx()
239
+ >>> nnz = ctx.create_unbacked_symint()
240
+ >>> shape = [x.dim(), nnz]
241
+ >>> result = x.new_empty(shape, dtype=torch.long)
242
+ >>> return result
243
+ >>>
244
+ >>> @torch._custom_ops.impl("mylibrary::custom_nonzero")
245
+ >>> def custom_nonzero_impl(x):
246
+ >>> x_np = to_numpy(x)
247
+ >>> res = np.stack(np.nonzero(x_np), axis=1)
248
+ >>> # unbacked symbolic ints in PyTorch must be >= 2, so we
249
+ >>> # constrain the range to at least 2
250
+ >>> if res.shape[0] <= 1:
251
+ >>> raise RuntimeError("not supported")
252
+ >>> return torch.tensor(res, device=x.device)
253
+
254
+ """
255
+ import torch.library
256
+
257
+ return torch.library.register_fake(qualname, func, _stacklevel=2)
258
+
259
+
260
+ def impl_save_for_backward(qualname, *, func=None):
261
+ r"""Register a function that tells us what to save for backward.
262
+
263
+ Please see :func:`impl_backward` for more details.
264
+ """
265
+
266
+ def inner(func):
267
+ custom_op = _find_custom_op(qualname, also_check_torch_library=True)
268
+ custom_op.impl_save_for_backward(_stacklevel=3)(func)
269
+ return func
270
+
271
+ if func is None:
272
+ return inner
273
+ return inner(func)
274
+
275
+
276
+ def impl_backward(qualname, output_differentiability=None, *, func=None):
277
+ r"""Registers a backward formula for an operator.
278
+
279
+ In order for an operator to work with autograd, you need to register
280
+ a backward formula. There are two pieces to this:
281
+ 1. You must give us a function to specify what to save for backward.
282
+ Call this the "save for backward" function.
283
+ 2. You must give us a function that computes gradients. Call this the
284
+ "backward" function.
285
+
286
+ Use `impl_save_for_backward` to define a "save for backward" function
287
+ that specifies what gets saved for backward. The function should accept
288
+ two arguments ``(inputs, output)`` and return the quantities to be saved
289
+ for backward.
290
+
291
+ During runtime, when you call the operator in a forwards pass, PyTorch
292
+ will invoke the "save for backward" function with the inputs and output
293
+ of the operator.
294
+
295
+ Use `impl_backward` to define the "backward" function. The backward
296
+ function must accept ``(ctx, saved, *grads)``:
297
+ - ``ctx`` is a context object where we may provide information
298
+ - ``saved`` is exactly what gets returned from the "save for backward"
299
+ function
300
+ - ``grads`` is one or more gradients. The number of gradients matches
301
+ the number of outputs of the operator.
302
+
303
+ The backward function must return a dict that maps the name of
304
+ an input to the operator to its corresponding gradient. All inputs that
305
+ were declared to be Tensors in the operator definition must be accounted
306
+ for in the dict. The gradient may be a Tensor or None.
307
+
308
+ For a detailed guide on custom ops, please see
309
+ https://docs.google.com/document/d/1aGWtgxV3HppuxQAdddyPrs74_aEntpkYt9MalnCKnhk
310
+
311
+ """
312
+
313
+ def inner(func):
314
+ custom_op = _find_custom_op(qualname, also_check_torch_library=True)
315
+ custom_op.impl_backward(output_differentiability, _stacklevel=3)(func)
316
+ return func
317
+
318
+ if func is None:
319
+ return inner
320
+ return inner(func)
321
+
322
+
323
+ def _destroy(qualname):
324
+ """De-registers a custom op. For testing purposes only"""
325
+ custom_op = _find_custom_op(qualname)
326
+ custom_op._destroy()
lib/python3.12/site-packages/torch/_environment.py ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ def is_fbcode() -> bool:
2
+ return False
lib/python3.12/site-packages/torch/_guards.py ADDED
@@ -0,0 +1,1180 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import contextlib
4
+ import dataclasses
5
+ import enum
6
+ import functools
7
+ import logging
8
+ import re
9
+ import threading
10
+ import traceback
11
+ import unittest.mock
12
+ import weakref
13
+ from abc import abstractmethod
14
+ from collections import defaultdict
15
+ from contextlib import contextmanager
16
+ from dataclasses import dataclass
17
+ from typing import (
18
+ Any,
19
+ Callable,
20
+ Generic,
21
+ NamedTuple,
22
+ Optional,
23
+ TYPE_CHECKING,
24
+ TypeVar,
25
+ Union,
26
+ )
27
+
28
+ import torch
29
+ from torch.utils import _pytree as pytree
30
+ from torch.utils._backport_slots import dataclass_slots
31
+ from torch.utils._traceback import CapturedTraceback, format_frame
32
+ from torch.utils.weak import WeakTensorKeyDictionary
33
+
34
+
35
+ log = logging.getLogger(__name__)
36
+
37
+
38
+ if TYPE_CHECKING:
39
+ from collections.abc import Generator, Iterator
40
+ from types import CodeType
41
+
42
+ import sympy
43
+
44
+ from torch._dynamo.backends.distributed import DDPOptimizerContext
45
+ from torch._dynamo.codegen import PyCodegen
46
+ from torch._functorch._aot_autograd.schemas import ViewAndMutationMeta
47
+ from torch._subclasses.fake_tensor import FakeTensorMode
48
+
49
+
50
+ """
51
+ torch._guards is the definitional source of truth for general purpose guard structures.
52
+
53
+ An important thing to keep in mind here is the preservation of layering. There should be no dynamo notions,
54
+ and no guard installation notions here.
55
+ """
56
+
57
+ COMPILE_ID_PATTERN = re.compile(r"^(?P<frame_id>\d+)/(?P<frame_compile_id>\d+)$")
58
+ CA_COMPILE_ID_PATTERN = re.compile(
59
+ r"^!(?P<compiled_autograd_id>\d+)(?:/(?P<frame_id>\d+)/(?P<frame_compile_id>\d+))?$"
60
+ )
61
+
62
+ # [Note: Updating CompiledId]
63
+ #
64
+ # CompiledId represents a unique program-level identifier, and we want to keep that
65
+ # property as the codebase evolves. This property is relied on even outside of the pytorch
66
+ # repo, e.g. tlparse or other internal tooling. The in-memory format can be freely changed,
67
+ # as those dependencies only consume the string serialization.
68
+ #
69
+ # The string form should be:
70
+ # 1. Program-level uid: CompileId can uniquely identify a compiled graph.
71
+ # 2. Storage efficient: This object is logged in nearly every entry. We should elide symbols when possible.
72
+ # 3. Compact: The string form is directly displayed by some tools. Special symbols are okay.
73
+
74
+
75
+ # TODO: mark as kw_only=True once we drop support for <Python 3.10
76
+ @dataclass(frozen=True)
77
+ class CompileId:
78
+ frame_id: Optional[int]
79
+ # This id is per-frame, and counts how many times we've compiled this
80
+ # frame. This could have been a global id but having this be per-frame
81
+ # gives you a better intuitive sense for how many recompiles have occurred
82
+ # so far.
83
+ frame_compile_id: Optional[int]
84
+
85
+ # torch.compiling a compiled autograd graph
86
+ compiled_autograd_id: Optional[int] = None
87
+
88
+ # TODO: consider also tracking the recompilation count
89
+ # See Note: Updating CompileId
90
+
91
+ def __str__(self) -> str:
92
+ # NOTE: Keep this in sync with both from_string and the tlparse repo
93
+ if self.compiled_autograd_id is not None:
94
+ assert (self.frame_id is None) == (self.frame_compile_id is None)
95
+ frame_str = ""
96
+ if self.frame_id is not None:
97
+ frame_str = f"/{self.frame_id}/{self.frame_compile_id}"
98
+
99
+ return f"!{self.compiled_autograd_id}{frame_str}"
100
+ else:
101
+ assert self.frame_id is not None and self.frame_compile_id is not None
102
+ return f"{self.frame_id}/{self.frame_compile_id}"
103
+
104
+ @classmethod
105
+ def from_string(cls, compile_id: Optional[str]) -> Optional[CompileId]:
106
+ """
107
+ Factory method that creates a CompileId from its string representation.
108
+ Keep this in sync with the __str__ method.
109
+ """
110
+ if compile_id is None:
111
+ return None
112
+ try:
113
+ for pattern in (COMPILE_ID_PATTERN, CA_COMPILE_ID_PATTERN):
114
+ if match := pattern.match(compile_id):
115
+ groups = match.groupdict()
116
+ for k, v in groups.items():
117
+ if v is not None:
118
+ groups[k] = int(v)
119
+ return cls(**groups) # type: ignore[arg-type]
120
+ else:
121
+ raise ValueError
122
+
123
+ except Exception as e:
124
+ raise ValueError(f"Invalid compile_id '{compile_id}'") from e
125
+
126
+
127
+ class TraceId(NamedTuple):
128
+ compile_id: CompileId
129
+ # This starts off as 0, and every time we restart analysis it goes
130
+ # up by one
131
+ attempt: int
132
+
133
+ def __str__(self) -> str:
134
+ # Keep this in sync with tlparse repo
135
+ if self.attempt == 0:
136
+ return str(self.compile_id)
137
+ else:
138
+ return f"{self.compile_id}_{self.attempt}"
139
+
140
+
141
+ class GuardSource(enum.Enum):
142
+ LOCAL = 0
143
+ GLOBAL = 1
144
+ LOCAL_SPECIALIZED_NN_MODULE = 2
145
+ GLOBAL_SPECIALIZED_NN_MODULE = 3
146
+ CONSTANT = 4
147
+ RANDOM_VALUE = 5
148
+ SHAPE_ENV = 6
149
+ LOCAL_FSDP_MODULE = 7
150
+ GLOBAL_FSDP_MODULE = 8
151
+ BACKWARD_STATE = 9
152
+ EPHEMERAL = 10
153
+ SYNTHETIC_LOCAL = 11
154
+ LOCAL_UNSPECIALIZED_NN_MODULE = 12
155
+ GLOBAL_UNSPECIALIZED_NN_MODULE = 13
156
+ LOCAL_UNSPECIALIZED_BUILTIN_NN_MODULE = 14
157
+ GLOBAL_UNSPECIALIZED_BUILTIN_NN_MODULE = 15
158
+
159
+ def is_fsdp_module(self) -> bool:
160
+ return self in (GuardSource.GLOBAL_FSDP_MODULE, GuardSource.LOCAL_FSDP_MODULE)
161
+
162
+ def is_specialized_nn_module(self) -> bool:
163
+ import torch._dynamo.config as config
164
+
165
+ if config._unsafe_skip_fsdp_module_guards:
166
+ return (
167
+ self
168
+ in (
169
+ GuardSource.GLOBAL_SPECIALIZED_NN_MODULE,
170
+ GuardSource.LOCAL_SPECIALIZED_NN_MODULE,
171
+ )
172
+ or self.is_fsdp_module()
173
+ )
174
+ return self in (
175
+ GuardSource.GLOBAL_SPECIALIZED_NN_MODULE,
176
+ GuardSource.LOCAL_SPECIALIZED_NN_MODULE,
177
+ )
178
+
179
+ def is_unspecialized_nn_module(self) -> bool:
180
+ return self in (
181
+ GuardSource.GLOBAL_UNSPECIALIZED_NN_MODULE,
182
+ GuardSource.LOCAL_UNSPECIALIZED_NN_MODULE,
183
+ GuardSource.GLOBAL_UNSPECIALIZED_BUILTIN_NN_MODULE,
184
+ GuardSource.LOCAL_UNSPECIALIZED_BUILTIN_NN_MODULE,
185
+ )
186
+
187
+ def is_unspecialized_builtin_nn_module(self) -> bool:
188
+ return self in (
189
+ GuardSource.GLOBAL_UNSPECIALIZED_BUILTIN_NN_MODULE,
190
+ GuardSource.LOCAL_UNSPECIALIZED_BUILTIN_NN_MODULE,
191
+ )
192
+
193
+ def is_local(self) -> bool:
194
+ return self in (
195
+ GuardSource.LOCAL,
196
+ GuardSource.LOCAL_SPECIALIZED_NN_MODULE,
197
+ GuardSource.LOCAL_FSDP_MODULE,
198
+ GuardSource.LOCAL_UNSPECIALIZED_NN_MODULE,
199
+ GuardSource.LOCAL_UNSPECIALIZED_BUILTIN_NN_MODULE,
200
+ )
201
+
202
+
203
+ """
204
+ Base class for a "GuardBuilder" role.
205
+
206
+ The GuardBuilderBase role is to represent a scope within which to build a guard. The name is a little
207
+ confusing, as its not a builder, but for the sake of avoiding a lot of renames and keeping the original reference
208
+ to torchdynamo's GuardBuilder.
209
+
210
+ Note: create_fn is invoked with a GuardBuilderBase and a Guard. A GuardBuilder is chosen based
211
+ on GuardSource's select function.
212
+
213
+ There is value in keeping this GuardBuilderBase empty to keep layering clean.
214
+ """
215
+
216
+
217
+ class GuardBuilderBase:
218
+ pass
219
+
220
+
221
+ @dataclasses.dataclass(frozen=True)
222
+ class SLoc:
223
+ framework_loc: Optional[Union[traceback.FrameSummary, str]]
224
+ maybe_user_loc: Optional[str]
225
+
226
+ def __str__(self) -> str:
227
+ floc = (
228
+ self.framework_loc
229
+ if isinstance(self.framework_loc, str)
230
+ else format_frame(self.framework_loc)
231
+ )
232
+ if self.maybe_user_loc is not None:
233
+ return f"{self.maybe_user_loc} ({floc})"
234
+ else:
235
+ return f"({floc})"
236
+
237
+
238
+ class ShapeGuard(NamedTuple):
239
+ expr: sympy.logic.boolalg.Boolean
240
+ sloc: SLoc
241
+ size_oblivious: bool
242
+
243
+
244
+ @dataclass_slots
245
+ @dataclasses.dataclass
246
+ class Guard:
247
+ # originating_source is the source that called the make_guard method to
248
+ # construct this guard object. The property name specifies what exactly it
249
+ # is the guard is guarding on. The meaning of the name is dependent on the
250
+ # create_fn; you must look at the use-site inside create_fn to know what
251
+ # name means.
252
+ #
253
+ # That being said, although you might think this is just a "name", name is
254
+ # usually an arbitrary Python expression that will be evaluated with all
255
+ # globals (and locals, if you create a LOCAL guard) to extract the Python
256
+ # object that we want to perform guard tests on. This evaluation
257
+ # typically happens in GuardBuilder.eval. In these cases, name is
258
+ # typically produced by originating_source.name() (not to be confused with
259
+ # GuardSource - the property source).
260
+ #
261
+ # Occasionally, name is not a valid Python expression; sometimes
262
+ # it is meaningless. Example create_fns that are like this include
263
+ # GRAD_MODE and SHAPE_ENV.
264
+ originating_source: Source
265
+ create_fn: Callable[[GuardBuilderBase, Guard], None]
266
+
267
+ # Export only. These values are written to at time of guard check_fn creation.
268
+ guard_types: Optional[list[str]] = None
269
+ code_list: Optional[list[str]] = None
270
+ obj_weakref: Optional[object] = None
271
+ guarded_class_weakref: Optional[weakref.ReferenceType[Any]] = None
272
+
273
+ stack: Optional[CapturedTraceback] = None
274
+ user_stack: Optional[traceback.StackSummary] = None
275
+ _hash: Optional[int] = None
276
+ _unserializable: bool = False
277
+
278
+ def __hash__(self) -> int:
279
+ if self._hash is None:
280
+ self._hash = hash((self.name, self.source, id(self.create_fn)))
281
+ return self._hash
282
+
283
+ def sort_key(self) -> tuple[bool, int, int, str, int]:
284
+ # Put the duplicate input guards at the end. The duplicate guards have
285
+ # two sources while guard.name only considers one source.
286
+
287
+ is_duplicate_input = (
288
+ isinstance(self.create_fn, functools.partial)
289
+ and self.create_fn.func is torch._dynamo.guards.GuardBuilder.DUPLICATE_INPUT
290
+ )
291
+ return (
292
+ is_duplicate_input,
293
+ self.source.value if self.source else -1,
294
+ len(self.name),
295
+ self.name,
296
+ self.inner_create_fn().__code__.co_firstlineno,
297
+ )
298
+
299
+ def __lt__(self, other: Guard) -> bool:
300
+ return self.sort_key() < other.sort_key()
301
+
302
+ def inner_create_fn(self) -> Callable[[GuardBuilderBase, Guard], Any]:
303
+ if isinstance(self.create_fn, functools.partial):
304
+ return self.create_fn.func
305
+ else:
306
+ return self.create_fn
307
+
308
+ @property
309
+ def name(self) -> str:
310
+ return self.originating_source.name()
311
+
312
+ @property
313
+ def source(self) -> GuardSource:
314
+ return self.originating_source.guard_source()
315
+
316
+ @staticmethod
317
+ def weakref_to_str(obj_weakref: object) -> str:
318
+ """
319
+ This is a workaround of a Python weakref bug.
320
+
321
+ `obj_weakref` is instance returned by `weakref.ref`,
322
+ `str(obj_weakref)` is buggy if the original obj overrides __getattr__, e.g:
323
+
324
+ class MyConfig(dict):
325
+ def __getattr__(self, x):
326
+ return self[x]
327
+
328
+ obj = MyConfig(offset=5)
329
+ obj_weakref = weakref.ref(obj)
330
+ str(obj_weakref) # raise error: KeyError: '__name__'
331
+ """
332
+ if isinstance(obj_weakref, weakref.ReferenceType):
333
+ obj = obj_weakref()
334
+ if obj is not None:
335
+ return f"<weakref at {hex(id(obj_weakref))}; to '{obj.__class__.__name__}' at {hex(id(obj))}>"
336
+ else:
337
+ return f"<weakref at {hex(id(obj_weakref))}; dead>"
338
+ else:
339
+ return str(obj_weakref)
340
+
341
+ def __repr__(self) -> str:
342
+ s = f"""
343
+ {self.source.name.lower() if self.source else ""} {repr(self.name)} {self.inner_create_fn().__name__}
344
+ {{
345
+ 'guard_types': {self.guard_types},
346
+ 'code': {self.code_list},
347
+ 'obj_weakref': {self.weakref_to_str(self.obj_weakref)}
348
+ 'guarded_class': {self.guarded_class_weakref}
349
+ }}
350
+ """
351
+ return s
352
+
353
+ def __str__(self) -> str:
354
+ output = f"Name: {repr(self.name)}\n"
355
+ source = self.source.name.lower() if self.source else ""
356
+ output += f" Source: {source}\n"
357
+ output += f" Create Function: {self.inner_create_fn().__name__}\n"
358
+ output += f" Guard Types: {self.guard_types}\n"
359
+ output += f" Code List: {self.code_list}\n"
360
+ output += f" Object Weakref: {self.weakref_to_str(self.obj_weakref)}\n"
361
+ output += f" Guarded Class Weakref: {self.guarded_class_weakref}\n"
362
+ return output
363
+
364
+ def create(self, builder: GuardBuilderBase) -> Any:
365
+ try:
366
+ return self.create_fn(builder, self)
367
+ except Exception:
368
+ log.exception("Error while creating guard:\n%s", str(self).rstrip())
369
+ if self.stack:
370
+ log.error("Created at:\n%s", "".join(self.stack.format()[-4:]).rstrip())
371
+ raise
372
+
373
+ def is_specialized_nn_module(self) -> bool:
374
+ return self.source.is_specialized_nn_module()
375
+
376
+ def is_fsdp_module(self) -> bool:
377
+ return self.source.is_fsdp_module()
378
+
379
+ def is_local(self) -> bool:
380
+ return self.source.is_local()
381
+
382
+ def create_fn_name(self) -> str:
383
+ if isinstance(self.create_fn, functools.partial):
384
+ create_fn = self.create_fn.func # type: ignore[attr-defined]
385
+ else:
386
+ create_fn = self.create_fn
387
+ return create_fn.__name__
388
+
389
+ def set_export_info(
390
+ self,
391
+ guard_type: str,
392
+ guarded_class: Optional[weakref.ReferenceType[Any]],
393
+ code_list: list[str],
394
+ obj_weakref: object,
395
+ ) -> None:
396
+ if not self.guard_types:
397
+ self.guard_types = []
398
+
399
+ self.guard_types.append(guard_type)
400
+
401
+ assert self.guarded_class_weakref in (
402
+ guarded_class,
403
+ None,
404
+ ), "Guarded class id must be identical, or None"
405
+ self.guarded_class_weakref = guarded_class
406
+
407
+ if not self.code_list:
408
+ self.code_list = code_list
409
+ else:
410
+ self.code_list.extend(code_list)
411
+
412
+ # Some objects are ephemeral, e.g., list[slice(1, 2)]. If we have
413
+ # multiple guards on the same object, the weakref can die between the
414
+ # invocation of set_export_info calls. So a dead weakref is also
415
+ # acceptable.
416
+ assert (
417
+ self.obj_weakref in (obj_weakref, None)
418
+ or callable(self.obj_weakref)
419
+ and self.obj_weakref() is None
420
+ ), "Guarded object must be identical, None or ephemeral (dead weakref)"
421
+ self.obj_weakref = obj_weakref
422
+
423
+
424
+ T = TypeVar("T")
425
+
426
+ """
427
+ Parent structure for guard env expressions.
428
+ A GuardEnvExpr can have any subtype.
429
+ Note: All subtypes must be handled exhaustively in
430
+ torch._dynamo.guards._parse_guard_env_guards to avoid a RuntimeError.
431
+ """
432
+
433
+
434
+ @dataclasses.dataclass(frozen=True)
435
+ class GuardEnvExpr:
436
+ pass
437
+
438
+
439
+ """
440
+ A class representing a pair of duplicate inputs.
441
+ input_pos_a and input_pos_b are input positions we have deduped.
442
+ """
443
+
444
+
445
+ @dataclasses.dataclass(frozen=True)
446
+ class DuplicateInputs(GuardEnvExpr):
447
+ input_source_a: Source
448
+ input_source_b: Source
449
+
450
+ def __post_init__(self) -> None:
451
+ assert self.input_source_a != self.input_source_b
452
+
453
+
454
+ """
455
+ A class representing storage overlap relations among inputs that aliases the same storage.
456
+
457
+ Given that a set of tensors alias the same storage, this guard checks whether they actually
458
+ have overlapping storages.
459
+
460
+ While non_overlapping_sources represent input tensors that definitely don't have any storage
461
+ overlapping with any other input, overlapping_sources represent tensors that either:
462
+
463
+ 1. Do overlap some other input tensor
464
+ 2. Might not overlap some other input tensor, but we are not sure
465
+ """
466
+
467
+
468
+ @dataclasses.dataclass(frozen=True)
469
+ class StorageOverlap(GuardEnvExpr):
470
+ overlapping_sources: list[Source]
471
+ non_overlapping_sources: list[Source]
472
+
473
+
474
+ """
475
+ Checkpointable is an interface for driving state snapshotting, left purposely vague for now.
476
+
477
+ copy_graphstate() -> T, a somewhat legacy name, is expected to emit a snapshot of any type that
478
+ can also be taken in at restore_graphstate(T) calls.
479
+
480
+ When to snapshot, is, at the moment, an implementation detail of upstream callers. Checkpointable
481
+ does not provide any guarantees around consistency, idempotency, or safety of calling its APIs, yet.
482
+
483
+ In the future, it will have a closer coupling to a generic Checkpoint management system.
484
+ """
485
+
486
+
487
+ class Checkpointable(Generic[T]):
488
+ @abstractmethod
489
+ def copy_graphstate(self) -> T: ...
490
+
491
+ @abstractmethod
492
+ def restore_graphstate(self, state: T) -> None: ...
493
+
494
+
495
+ class GuardsCheckpointState:
496
+ """
497
+ The GuardCheckpointState - it is the T of Checkpointable[T] for GuardsContext
498
+ """
499
+
500
+ dynamo_guards: set[Guard] = set()
501
+
502
+ def __init__(self, dynamo_guards: set[Guard]) -> None:
503
+ self.dynamo_guards = dynamo_guards
504
+
505
+ def diff(self, other: GuardsCheckpointState) -> Optional[set[Guard]]:
506
+ """
507
+ Produces a delta against another GuardsCheckpointState.
508
+
509
+ Returns None if no delta is found, otherwise, return a set() of mismatched
510
+ Guard type objects.
511
+ """
512
+ r = self.dynamo_guards.difference(other.dynamo_guards)
513
+ if len(r) == 0:
514
+ return None
515
+ return r
516
+
517
+ def __eq__(self, other: object) -> bool:
518
+ if not isinstance(other, GuardsCheckpointState):
519
+ return False
520
+ return self.diff(other) is None
521
+
522
+
523
+ class ModuleContextCheckpointState:
524
+ nn_modules: dict[str, torch.nn.Module] = {}
525
+
526
+ def __init__(self, nn_modules: dict[str, torch.nn.Module]) -> None:
527
+ self.nn_modules = nn_modules
528
+
529
+ def diff(self, other: ModuleContextCheckpointState) -> Optional[set[str]]:
530
+ """
531
+ Produces a delta against another ModuleContextCheckpointState.
532
+
533
+ Returns None if no delta is found, otherwise, return a set() of mismatched
534
+ module key names.
535
+ """
536
+ r = set(self.nn_modules.keys()).difference(set(other.nn_modules.keys()))
537
+ if len(r) == 0:
538
+ return None
539
+ return r
540
+
541
+ def __eq__(self, other: object) -> bool:
542
+ if not isinstance(other, ModuleContextCheckpointState):
543
+ return False
544
+ return self.diff(other) is None
545
+
546
+
547
+ class ModuleContext(Checkpointable[ModuleContextCheckpointState]):
548
+ def __init__(self) -> None:
549
+ self.nn_modules: dict[str, Any] = {}
550
+
551
+ def copy_graphstate(self) -> ModuleContextCheckpointState:
552
+ return ModuleContextCheckpointState(dict(self.nn_modules))
553
+
554
+ def restore_graphstate(self, state: ModuleContextCheckpointState) -> None:
555
+ assert isinstance(state, ModuleContextCheckpointState)
556
+ self.nn_modules = state.nn_modules
557
+
558
+
559
+ class GlobalContextCheckpointState:
560
+ global_state: dict[str, tuple[Callable, Any]] = {}
561
+
562
+ def __init__(self, global_states: dict[str, tuple[Callable, Any]]) -> None:
563
+ self.global_state = global_states
564
+
565
+ def diff(self, other: GlobalContextCheckpointState) -> Optional[set[str]]:
566
+ """
567
+ Produces a delta against another GlobalContextCheckpointState.
568
+
569
+ Returns None if no delta is found, otherwise, return a set() of mismatched
570
+ global key names.
571
+ """
572
+ r = set(self.global_state.keys()).difference(set(other.global_state.keys()))
573
+ if len(r) == 0:
574
+ return None
575
+ return r
576
+
577
+ def __eq__(self, other: object) -> bool:
578
+ if not isinstance(other, GlobalContextCheckpointState):
579
+ return False
580
+ return self.diff(other) is None
581
+
582
+
583
+ class GlobalContext(Checkpointable[GlobalContextCheckpointState]):
584
+ """
585
+ This keeps track of the global torch state during tracing of a function.
586
+ For example, torch.is_grad_enabled.
587
+ """
588
+
589
+ _supported_global_states = {
590
+ "grad_enabled",
591
+ "autocast_enabled",
592
+ "autocast_cpu_enabled",
593
+ "autocast_gpu_dtype",
594
+ "autocast_cpu_dtype",
595
+ "autocast_cache_enabled",
596
+ }
597
+
598
+ def __init__(self) -> None:
599
+ self.global_state: dict[str, tuple[Callable, Any]] = {}
600
+
601
+ def copy_graphstate(self) -> GlobalContextCheckpointState:
602
+ return GlobalContextCheckpointState(self.global_state)
603
+
604
+ def restore_graphstate(self, state: GlobalContextCheckpointState) -> None:
605
+ assert isinstance(state, GlobalContextCheckpointState)
606
+ self.global_state = state.global_state
607
+ assert (
608
+ len(self.global_state) == len(self._supported_global_states)
609
+ and set(self.global_state.keys()) == self._supported_global_states
610
+ ), "Global state mismatch"
611
+ for func, args in self.global_state.values():
612
+ func(args)
613
+
614
+
615
+ # Like a Set[Guard] but will record the user stack on all guards at the
616
+ # time they were installed at their destination
617
+ class GuardsSet:
618
+ def __init__(self, inner: Optional[set[Guard]] = None) -> None:
619
+ if inner is None:
620
+ inner = set()
621
+ self.inner = inner
622
+
623
+ def __iter__(self) -> Iterator[Guard]:
624
+ return iter(self.inner)
625
+
626
+ def __len__(self) -> int:
627
+ return len(self.inner)
628
+
629
+ # Subtraction along with bool is typically used to determine the delta of
630
+ # added guards between checkpoints for higher order ops
631
+ def __sub__(self, other: GuardsSet) -> GuardsSet:
632
+ return GuardsSet(self.inner - other.inner)
633
+
634
+ def __bool__(self) -> bool:
635
+ return bool(self.inner)
636
+
637
+ def add(
638
+ self, guard: Guard, *, collect_debug_stack: bool = True, skip: int = 0
639
+ ) -> None:
640
+ if guard in self.inner:
641
+ return
642
+ if collect_debug_stack:
643
+ if guard.stack is None:
644
+ guard.stack = CapturedTraceback.extract(skip=1 + skip)
645
+ if guard.user_stack is None:
646
+ guard.user_stack = TracingContext.extract_stack()
647
+ self.inner.add(guard)
648
+
649
+ def update(self, *others: set[Guard]) -> None:
650
+ for o in others:
651
+ for g in o:
652
+ self.add(g, skip=1)
653
+
654
+ def remove_guards_with_source(self, source: Source) -> None:
655
+ """Delete all guards that contains a given source"""
656
+ from ._dynamo.source import is_from_source
657
+
658
+ self.inner = {
659
+ g for g in self.inner if not is_from_source(g.originating_source, source)
660
+ }
661
+
662
+
663
+ """
664
+ A GuardsContext is a checkpointable representation of all the guards in the current tracing
665
+ context. It's lifecycle is bound 1:1 to the tracing context, and it should never be instantiated
666
+ directly outside of it. For passing around internal state representations of this object,
667
+ prefer to extract them with copy_graphstate to produce a GuardsCheckpointState.
668
+ """
669
+
670
+
671
+ class GuardsContext(Checkpointable[GuardsCheckpointState]):
672
+ def __init__(self) -> None:
673
+ self.dynamo_guards: GuardsSet = GuardsSet()
674
+ self.aotautograd_guards: list[GuardEnvExpr] = []
675
+
676
+ def copy_graphstate(self) -> GuardsCheckpointState:
677
+ return GuardsCheckpointState(set(self.dynamo_guards.inner))
678
+
679
+ def restore_graphstate(self, state: GuardsCheckpointState) -> None:
680
+ # NB: "steals" the passed in state
681
+ assert isinstance(state, GuardsCheckpointState)
682
+ self.dynamo_guards = GuardsSet(state.dynamo_guards)
683
+
684
+
685
+ class HopSubgraphCache:
686
+ @abstractmethod
687
+ def add_dynamo_installed_submodule(self, fn_id: int, identifier: str) -> None: ...
688
+
689
+ @abstractmethod
690
+ def get_dynamo_installed_submodules(self, fn_id: int) -> list[str]: ...
691
+
692
+ @abstractmethod
693
+ def add_autograd_key_entry(self, identifier: str, key: Callable) -> None: ...
694
+
695
+ @abstractmethod
696
+ def get_autograd_key_entry(self, identifier: str) -> Optional[Callable]: ...
697
+
698
+ @abstractmethod
699
+ def add_proxy_dispatch_entry(self, identifier: str, key: Callable) -> None: ...
700
+
701
+ @abstractmethod
702
+ def get_proxy_dispatch_entry(self, identifier: str) -> Optional[Callable]: ...
703
+
704
+ @abstractmethod
705
+ def add_lazy_bwd_entry(
706
+ self,
707
+ identifier: str,
708
+ tangent_metadata: tuple[object],
709
+ gmod: torch.fx.GraphModule,
710
+ ) -> int: ...
711
+
712
+ @abstractmethod
713
+ def get_lazy_bwd_entry(
714
+ self, identifier: str, tangent_metadata: tuple[object]
715
+ ) -> tuple[Optional[torch.fx.GraphModule], Optional[int]]: ...
716
+
717
+
718
+ class InvokeSubgraphCache(HopSubgraphCache):
719
+ def __init__(self) -> None:
720
+ self.autograd_cache: dict[str, Callable] = {}
721
+ self.proxy_dispatch_cache: dict[str, Callable] = {}
722
+ self.dynamo_installed_submodules: dict[int, list[str]] = defaultdict(list)
723
+ self.lazy_bwd_cache: dict[
724
+ str, dict[tuple[object], tuple[torch.fx.GraphModule, int]]
725
+ ] = defaultdict(dict)
726
+
727
+ def add_dynamo_installed_submodule(self, fn_id: int, identifier: str) -> None:
728
+ self.dynamo_installed_submodules[fn_id].append(identifier)
729
+
730
+ def get_dynamo_installed_submodules(self, fn_id: int) -> list[str]:
731
+ return self.dynamo_installed_submodules.get(fn_id, [])
732
+
733
+ def add_autograd_key_entry(self, identifier: str, key: Callable) -> None:
734
+ self.autograd_cache[identifier] = key
735
+
736
+ def get_autograd_key_entry(self, identifier: str) -> Optional[Callable]:
737
+ return self.autograd_cache.get(identifier, None)
738
+
739
+ def add_proxy_dispatch_entry(self, identifier: str, key: Callable) -> None:
740
+ self.proxy_dispatch_cache[identifier] = key
741
+
742
+ def get_proxy_dispatch_entry(self, identifier: str) -> Optional[Callable]:
743
+ return self.proxy_dispatch_cache.get(identifier, None)
744
+
745
+ def add_lazy_bwd_entry(
746
+ self,
747
+ identifier: str,
748
+ tangent_metadata: tuple[object],
749
+ gmod: torch.fx.GraphModule,
750
+ ) -> int:
751
+ # Save the number of existing graph modules in the dictionary to get the suffix
752
+ num_gmods = len(self.lazy_bwd_cache[identifier])
753
+ self.lazy_bwd_cache[identifier][tangent_metadata] = (gmod, num_gmods)
754
+ return num_gmods
755
+
756
+ def get_lazy_bwd_entry(
757
+ self, identifier: str, tangent_metadata: tuple[object]
758
+ ) -> tuple[Optional[torch.fx.GraphModule], Optional[int]]:
759
+ if identifier not in self.lazy_bwd_cache:
760
+ return (None, None)
761
+
762
+ return self.lazy_bwd_cache[identifier].get(tangent_metadata, (None, None))
763
+
764
+
765
+ class HopDispatchSetCache:
766
+ def __init__(self) -> None:
767
+ # Delayed import to avoid circular dependency
768
+ from torch._higher_order_ops.invoke_subgraph import invoke_subgraph
769
+
770
+ self.hop_cache_map = {invoke_subgraph: InvokeSubgraphCache()}
771
+
772
+ def get_cache(
773
+ self, op: torch._ops.HigherOrderOperator
774
+ ) -> Optional[HopSubgraphCache]:
775
+ if op not in self.hop_cache_map:
776
+ return None
777
+ return self.hop_cache_map[op] # type: ignore[index]
778
+
779
+
780
+ _TLS = threading.local()
781
+
782
+ """
783
+ TracingContext is the source of truth for all currently accumulated information
784
+ needed to trace. Its lifecycle is kept 1:1 when using TorchDynamo, but other systems
785
+ are open to managing their own TracingContext with that in mind.
786
+
787
+ The purpose of TracingContext is not to be a dumping ground, or god object, but rather to avoid
788
+ having to plumb complex subsystems across multiple verticals.
789
+
790
+ Ex: A common example is guard accumulation between dynamo, shape_env, aot_autograd, and inductor.
791
+ Accessing the current tracing context via
792
+ TracingContext.get() allows users to accumulate their own guards for processing, without needing to know how
793
+ to plumb objects back up to where frame interpretation happened.
794
+
795
+ Note that you can end up with multiple TracingContext for a single compilation
796
+ of a frame, as we reset the TracingContext whenever we restart analysis.
797
+ CompileContext is a more overarching context that encompasses multiple restarts.
798
+ """
799
+
800
+
801
+ class CompileContext:
802
+ @staticmethod
803
+ def get() -> CompileContext:
804
+ assert _TLS.compile_context is not None
805
+ return _TLS.compile_context
806
+
807
+ @staticmethod
808
+ def try_get() -> Optional[CompileContext]:
809
+ return getattr(_TLS, "compile_context", None)
810
+
811
+ def __init__(self, compile_id: Optional[CompileId]) -> None:
812
+ assert compile_id is None or isinstance(compile_id, CompileId)
813
+ self.compile_id: Optional[CompileId] = compile_id
814
+ self.attempt = 0
815
+ # Verbose ShapeEnv guards produced.
816
+ self.shape_env_guards: list[str] = []
817
+
818
+ @staticmethod
819
+ def current_compile_id() -> Optional[CompileId]:
820
+ self = CompileContext.try_get()
821
+ if self is None:
822
+ return None
823
+ return self.compile_id
824
+
825
+ @staticmethod
826
+ def current_trace_id() -> Optional[TraceId]:
827
+ self = CompileContext.try_get()
828
+ if self is None:
829
+ return None
830
+ if self.compile_id is None:
831
+ return None
832
+ return TraceId(self.compile_id, self.attempt)
833
+
834
+
835
+ class TracingContext:
836
+ """
837
+ Provides the currently installed TracingContext, or None.
838
+
839
+ Note that it is a staticmethod, and invocations outside of `with tracing()` (see below), are valid but
840
+ will return None.
841
+ """
842
+
843
+ @staticmethod
844
+ def try_get() -> Optional[TracingContext]:
845
+ return getattr(_TLS, "tracing_context", None)
846
+
847
+ @staticmethod
848
+ def get() -> TracingContext:
849
+ if ctx := TracingContext.try_get():
850
+ return ctx
851
+ raise RuntimeError(
852
+ "TracingContext.get() must be called within an ongoing trace."
853
+ )
854
+
855
+ def __init__(self, fake_mode: Optional[FakeTensorMode]) -> None:
856
+ self.guards_context = GuardsContext()
857
+ self.module_context = ModuleContext()
858
+ self.global_context = GlobalContext()
859
+ self.previously_inlined_functions: dict[Any, Any] = dict()
860
+ self.previously_cleaned_instructions: dict[Any, Any] = dict()
861
+ self.fake_mode: Optional[FakeTensorMode] = fake_mode
862
+ self.frame_summary_stack: list[traceback.FrameSummary] = []
863
+ # This is morally part of frame_summary_stack, but it is kept separate
864
+ # for clarity. As we process a frame, this variable gets updated
865
+ # to keep track of what line we are in the function. We make a
866
+ # function call, this gets cleared and the frame location is pushed
867
+ # to frame_summary_stack (prepping this variable for the inner frame's
868
+ # progress)
869
+ self.loc_in_frame: Optional[tuple[str, int, str]] = None
870
+ # this is only set after aot_autograd
871
+ self.fw_metadata: Optional[ViewAndMutationMeta] = None
872
+ # this is only set when the DDPOptimizer is used
873
+ self.ddp_optimizer_ctx: Optional[DDPOptimizerContext] = None
874
+ # this is only set after aot_autograd
875
+ self.aot_graph_name: Optional[list[str]] = None
876
+ self.params_flat: Optional[list[Any]] = None
877
+ self.params_flat_unwrap_subclasses: Optional[list[Any]] = None
878
+ self.params_unwrapped_to_flat_index: Optional[list[Any]] = None
879
+ # this is for extended return calling convention from backend
880
+ # compiler to aot_autograd
881
+ # Per output, what the compiler specified stride of the output is,
882
+ # or None if no stride is known. This is always the HINT, it
883
+ # is never a SymInt (it would be better if it was a SymInt, but
884
+ # I can't conveniently get this from Inductor atm. Also, be
885
+ # careful not to accidentally induce guards on the SymInt if
886
+ # you ever do change this in aot_autograd.py; you should check
887
+ # on permutations preferentially.)
888
+ self.output_strides: Optional[list[Optional[tuple[int, ...]]]] = None
889
+ # When this is True, whenever we encounter an int in Dynamo tracing,
890
+ # we will (1) force unspec it and (2) force it as a size-like unbacked
891
+ # integer. This is currently used when processing certain lists of
892
+ # ints that are known to be size-like and may have 0/1 entries that we
893
+ # must not specialize on.
894
+ self.force_unspec_int_unbacked_size_like = False
895
+ # See note [Tensor Fakification and Symbol Caching]
896
+ self.tensor_to_context = WeakTensorKeyDictionary()
897
+
898
+ # If this true, Aot Autograd will return output Fake Tensors with appropriate
899
+ # meta on the first invocation
900
+ # see note: [Returning Fake Tensors on First AOT Autograd Call]
901
+ self.fakify_first_call = False
902
+ self.hop_dispatch_set_cache = HopDispatchSetCache()
903
+ # list of code objects for inlined functions
904
+ self.traced_code: list[CodeType] = []
905
+
906
+ def clear(self) -> None:
907
+ # Look at the note in output_graph.py in function `save_global_state`
908
+ # for the context on clearing global context.
909
+ self.global_context.global_state = {}
910
+ self.previously_inlined_functions.clear()
911
+ self.previously_cleaned_instructions.clear()
912
+
913
+ @staticmethod
914
+ @contextmanager
915
+ def patch(**kwargs: Any) -> Generator[None, None, None]:
916
+ prior = {}
917
+ ctx = TracingContext.get()
918
+
919
+ for key in kwargs.keys():
920
+ # KeyError on invalid entry
921
+ prior[key] = getattr(ctx, key)
922
+ for key, val in kwargs.items():
923
+ setattr(ctx, key, val)
924
+ try:
925
+ yield
926
+ finally:
927
+ for key, val in prior.items():
928
+ setattr(ctx, key, val)
929
+
930
+ @staticmethod
931
+ def extract_stack() -> traceback.StackSummary:
932
+ self = TracingContext.try_get()
933
+ if self is None:
934
+ return traceback.StackSummary()
935
+ stack = self.frame_summary_stack
936
+ if self.loc_in_frame is not None:
937
+ stack = stack + [self._populate_loc_in_frame_summary()]
938
+ return traceback.StackSummary.from_list(stack)
939
+
940
+ def _populate_loc_in_frame_summary(self) -> traceback.FrameSummary:
941
+ assert self.loc_in_frame is not None
942
+ filename, lineno, frame_name = self.loc_in_frame
943
+ return traceback.FrameSummary(filename, lineno, frame_name, lookup_line=False)
944
+
945
+ # Call this when you want to call into some code that isn't necessarily
946
+ # associated with the current frame state
947
+ @staticmethod
948
+ @contextlib.contextmanager
949
+ def clear_frame() -> Generator[None, None, None]:
950
+ tc = TracingContext.get()
951
+ with (
952
+ unittest.mock.patch.object(tc, "frame_summary_stack", []),
953
+ unittest.mock.patch.object(tc, "loc_in_frame", None),
954
+ ):
955
+ try:
956
+ yield
957
+ except Exception as e:
958
+ # Prevent real_stack from getting attached
959
+ #
960
+ # The invariant is that if an Exception as real_stack, we've
961
+ # appropriately attached a user stack and we no longer need to
962
+ # attach anything. Because we cannot conveniently interpose
963
+ # when an exception is thrown, we instead interpose everywhere
964
+ # we set what the user stack is set (using the context
965
+ # manager). However, our compiler stack does "tail calls"
966
+ # (when it calls into user compiler), at which point the
967
+ # parent exception frames would incorrectly attach an
968
+ # incorrect frame.
969
+ #
970
+ # However, if, somehow, someone raised an exception with this
971
+ # scope that had a stack (for example, because they are
972
+ # restoring the user stack state appropriately as they process
973
+ # node by node), we should respect it. Thus, we cannot
974
+ # unconditionally set None.
975
+ if not hasattr(e, "real_stack"):
976
+ e.real_stack = None # type: ignore[attr-defined]
977
+ raise
978
+
979
+ @staticmethod
980
+ @contextlib.contextmanager
981
+ def current_frame(
982
+ frame_summary: Optional[traceback.FrameSummary],
983
+ ) -> Generator[None, None, None]:
984
+ # frame_summary can be None to solely take advantage of real_stack
985
+ # attachment to thrown exceptions
986
+ tc = TracingContext.get()
987
+ if frame_summary is not None:
988
+ tc.frame_summary_stack.append(frame_summary)
989
+ old = tc.loc_in_frame
990
+ tc.loc_in_frame = None
991
+ try:
992
+ yield
993
+ except Exception as e:
994
+ if not hasattr(e, "real_stack"):
995
+ e.real_stack = tc.extract_stack() # type: ignore[attr-defined]
996
+ raise
997
+ finally:
998
+ if frame_summary is not None:
999
+ tc.frame_summary_stack.pop()
1000
+ tc.loc_in_frame = old
1001
+
1002
+ @staticmethod
1003
+ @contextlib.contextmanager
1004
+ def report_output_strides() -> Generator[
1005
+ Optional[list[Optional[tuple[int, ...]]]], None, None
1006
+ ]:
1007
+ tc = TracingContext.try_get()
1008
+ if tc is None:
1009
+ yield None
1010
+ return
1011
+ old_output_strides = tc.output_strides
1012
+ tc.output_strides = []
1013
+ try:
1014
+ yield tc.output_strides
1015
+ finally:
1016
+ tc.output_strides = old_output_strides
1017
+
1018
+ @staticmethod
1019
+ def set_current_loc(filename: str, lineno: int, frame_name: str) -> None:
1020
+ # Save the current location in the frame. Lazily generate the
1021
+ # framesummary.
1022
+ TracingContext.get().loc_in_frame = (filename, lineno, frame_name)
1023
+
1024
+ @staticmethod
1025
+ def get_traced_code() -> Optional[list[CodeType]]:
1026
+ tc = TracingContext.try_get()
1027
+ if tc is None:
1028
+ return None
1029
+ return tc.traced_code
1030
+
1031
+
1032
+ @contextmanager
1033
+ def compile_context(
1034
+ context: Optional[CompileContext],
1035
+ ) -> Generator[Optional[CompileContext], None, None]:
1036
+ old_context = getattr(_TLS, "compile_context", None)
1037
+ _TLS.compile_context = context
1038
+ try:
1039
+ yield context
1040
+ finally:
1041
+ _TLS.compile_context = old_context
1042
+
1043
+
1044
+ @contextmanager
1045
+ def tracing(
1046
+ context: Optional[TracingContext],
1047
+ ) -> Generator[Optional[TracingContext], None, None]:
1048
+ """
1049
+ This function installs the passed in tracing context as a dynamic scoped
1050
+ global variable.
1051
+
1052
+ Calls to TracingContext.get() while not under a `with tracing()` context
1053
+ will return None.
1054
+ """
1055
+ old_context = getattr(_TLS, "tracing_context", None)
1056
+ _TLS.tracing_context = context
1057
+ try:
1058
+ yield context
1059
+ except Exception as e:
1060
+ if not hasattr(e, "real_stack") and context is not None:
1061
+ e.real_stack = context.extract_stack() # type: ignore[attr-defined]
1062
+ raise
1063
+ finally:
1064
+ if (
1065
+ context is not None
1066
+ and context.fake_mode is not None
1067
+ and context.fake_mode.shape_env is not None
1068
+ ):
1069
+ context.fake_mode.shape_env.cleanup()
1070
+ _TLS.tracing_context = old_context
1071
+
1072
+
1073
+ # Subclasses can be found in torch/_dynamo/source.py
1074
+ # TODO(voz): Consider a toplevel torch/_source.py
1075
+ @dataclasses.dataclass(frozen=True)
1076
+ class Source:
1077
+ def is_dict_key(self) -> bool:
1078
+ return False
1079
+
1080
+ def is_ephemeral(self) -> bool:
1081
+ return False
1082
+
1083
+ def reconstruct(self, codegen: PyCodegen) -> None:
1084
+ raise NotImplementedError
1085
+
1086
+ def guard_source(self) -> GuardSource:
1087
+ raise NotImplementedError
1088
+
1089
+ def name(self) -> str:
1090
+ raise NotImplementedError
1091
+
1092
+ def make_guard(self, fn: Callable[..., Any]) -> Guard:
1093
+ if self.guard_source() is GuardSource.CONSTANT:
1094
+ raise NotImplementedError
1095
+ return Guard(self, fn)
1096
+
1097
+ def is_specialized_nn_module(self) -> bool:
1098
+ return self.guard_source().is_specialized_nn_module()
1099
+
1100
+ def subguards_allowed(self) -> bool:
1101
+ """True if you can guard on attributes of this"""
1102
+ return self.guard_source() != GuardSource.SYNTHETIC_LOCAL
1103
+
1104
+
1105
+ # Subclasses can be found in torch/_dynamo/source.py
1106
+ @dataclasses.dataclass(frozen=True)
1107
+ class ChainedSource(Source):
1108
+ base: Source
1109
+
1110
+ def is_dict_key(self) -> bool:
1111
+ # Recurse until you either hit a ConstDictKey or a Source
1112
+ return self.base.is_dict_key()
1113
+
1114
+ def is_ephemeral(self) -> bool:
1115
+ return self.base.is_ephemeral()
1116
+
1117
+ def get_base(self) -> Source:
1118
+ current: Source = self
1119
+ while isinstance(current, ChainedSource):
1120
+ current = current.base
1121
+ return current
1122
+
1123
+
1124
+ def detect_fake_mode(inputs: Any = None) -> Optional[FakeTensorMode]:
1125
+ """
1126
+ Attempts to "detect" what the current fake mode is. If there is one ambiently
1127
+ available from TracingContext, we preferentially use that. Otherwise, we
1128
+ heuristically detect the fake mode via the following sources, in order of
1129
+ priority:
1130
+
1131
+ - Currently active fake mode on stack
1132
+ - Fake mode associated with passed in tensors (inputs does not
1133
+ have to be flattened)
1134
+ """
1135
+ from torch._subclasses.fake_tensor import FakeTensor, FakeTensorMode
1136
+
1137
+ fake_modes = []
1138
+
1139
+ if context := TracingContext.try_get():
1140
+ fake_mode = context.fake_mode
1141
+ if fake_mode is not None:
1142
+ fake_modes.append((fake_mode, "tracing context", 0))
1143
+
1144
+ from torch.utils._python_dispatch import _get_current_dispatch_mode_stack
1145
+
1146
+ for i, m in enumerate(reversed(_get_current_dispatch_mode_stack())):
1147
+ if isinstance(m, FakeTensorMode):
1148
+ fake_modes.append((m, "active fake mode", i))
1149
+
1150
+ flat_inputs = pytree.tree_leaves(inputs)
1151
+ for i, flat_input in enumerate(flat_inputs):
1152
+ if isinstance(flat_input, FakeTensor):
1153
+ fake_modes.append((flat_input.fake_mode, "fake tensor input", i))
1154
+
1155
+ if fake_modes:
1156
+ fake_mode, desc1, i1 = fake_modes[0]
1157
+ for m, desc2, i2 in fake_modes[1:]:
1158
+ assert fake_mode is m, (
1159
+ f"fake mode ({fake_mode}) from {desc1} {i1} doesn't match mode ({m}) from {desc2} {i2}\n\n"
1160
+ f"fake mode from {desc1} {i1} allocated at:\n{fake_mode.stack}\n"
1161
+ f"fake mode from {desc2} {i2} allocated at:\n{m.stack}"
1162
+ )
1163
+ return fake_mode
1164
+ else:
1165
+ return None
1166
+
1167
+
1168
+ def active_fake_mode() -> Optional[FakeTensorMode]:
1169
+ """
1170
+ Inspects the dispatch mode stack for an active fake mode and returns it.
1171
+ Returns None if no fake mode is active.
1172
+ """
1173
+ from torch._subclasses.fake_tensor import FakeTensorMode
1174
+ from torch.utils._python_dispatch import _get_current_dispatch_mode_stack
1175
+
1176
+ for _, m in enumerate(reversed(_get_current_dispatch_mode_stack())):
1177
+ if isinstance(m, FakeTensorMode):
1178
+ return m
1179
+
1180
+ return None
lib/python3.12/site-packages/torch/_lobpcg.py ADDED
@@ -0,0 +1,1157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # mypy: allow-untyped-defs
2
+ """Locally Optimal Block Preconditioned Conjugate Gradient methods."""
3
+ # Author: Pearu Peterson
4
+ # Created: February 2020
5
+
6
+ from typing import Optional
7
+
8
+ import torch
9
+ from torch import _linalg_utils as _utils, Tensor
10
+ from torch.overrides import handle_torch_function, has_torch_function
11
+
12
+
13
+ __all__ = ["lobpcg"]
14
+
15
+
16
+ def _symeig_backward_complete_eigenspace(D_grad, U_grad, A, D, U):
17
+ # compute F, such that F_ij = (d_j - d_i)^{-1} for i != j, F_ii = 0
18
+ F = D.unsqueeze(-2) - D.unsqueeze(-1)
19
+ F.diagonal(dim1=-2, dim2=-1).fill_(float("inf"))
20
+ F.pow_(-1)
21
+
22
+ # A.grad = U (D.grad + (U^T U.grad * F)) U^T
23
+ Ut = U.mT.contiguous()
24
+ res = torch.matmul(
25
+ U, torch.matmul(torch.diag_embed(D_grad) + torch.matmul(Ut, U_grad) * F, Ut)
26
+ )
27
+
28
+ return res
29
+
30
+
31
+ def _polynomial_coefficients_given_roots(roots):
32
+ """
33
+ Given the `roots` of a polynomial, find the polynomial's coefficients.
34
+
35
+ If roots = (r_1, ..., r_n), then the method returns
36
+ coefficients (a_0, a_1, ..., a_n (== 1)) so that
37
+ p(x) = (x - r_1) * ... * (x - r_n)
38
+ = x^n + a_{n-1} * x^{n-1} + ... a_1 * x_1 + a_0
39
+
40
+ Note: for better performance requires writing a low-level kernel
41
+ """
42
+ poly_order = roots.shape[-1]
43
+ poly_coeffs_shape = list(roots.shape)
44
+ # we assume p(x) = x^n + a_{n-1} * x^{n-1} + ... + a_1 * x + a_0,
45
+ # so poly_coeffs = {a_0, ..., a_n, a_{n+1}(== 1)},
46
+ # but we insert one extra coefficient to enable better vectorization below
47
+ poly_coeffs_shape[-1] += 2
48
+ poly_coeffs = roots.new_zeros(poly_coeffs_shape)
49
+ poly_coeffs[..., 0] = 1
50
+ poly_coeffs[..., -1] = 1
51
+
52
+ # perform the Horner's rule
53
+ for i in range(1, poly_order + 1):
54
+ # note that it is computationally hard to compute backward for this method,
55
+ # because then given the coefficients it would require finding the roots and/or
56
+ # calculating the sensitivity based on the Vieta's theorem.
57
+ # So the code below tries to circumvent the explicit root finding by series
58
+ # of operations on memory copies imitating the Horner's method.
59
+ # The memory copies are required to construct nodes in the computational graph
60
+ # by exploiting the explicit (not in-place, separate node for each step)
61
+ # recursion of the Horner's method.
62
+ # Needs more memory, O(... * k^2), but with only O(... * k^2) complexity.
63
+ poly_coeffs_new = poly_coeffs.clone() if roots.requires_grad else poly_coeffs
64
+ out = poly_coeffs_new.narrow(-1, poly_order - i, i + 1)
65
+ out -= roots.narrow(-1, i - 1, 1) * poly_coeffs.narrow(
66
+ -1, poly_order - i + 1, i + 1
67
+ )
68
+ poly_coeffs = poly_coeffs_new
69
+
70
+ return poly_coeffs.narrow(-1, 1, poly_order + 1)
71
+
72
+
73
+ def _polynomial_value(poly, x, zero_power, transition):
74
+ """
75
+ A generic method for computing poly(x) using the Horner's rule.
76
+
77
+ Args:
78
+ poly (Tensor): the (possibly batched) 1D Tensor representing
79
+ polynomial coefficients such that
80
+ poly[..., i] = (a_{i_0}, ..., a{i_n} (==1)), and
81
+ poly(x) = poly[..., 0] * zero_power + ... + poly[..., n] * x^n
82
+
83
+ x (Tensor): the value (possible batched) to evaluate the polynomial `poly` at.
84
+
85
+ zero_power (Tensor): the representation of `x^0`. It is application-specific.
86
+
87
+ transition (Callable): the function that accepts some intermediate result `int_val`,
88
+ the `x` and a specific polynomial coefficient
89
+ `poly[..., k]` for some iteration `k`.
90
+ It basically performs one iteration of the Horner's rule
91
+ defined as `x * int_val + poly[..., k] * zero_power`.
92
+ Note that `zero_power` is not a parameter,
93
+ because the step `+ poly[..., k] * zero_power` depends on `x`,
94
+ whether it is a vector, a matrix, or something else, so this
95
+ functionality is delegated to the user.
96
+ """
97
+
98
+ res = zero_power.clone()
99
+ for k in range(poly.size(-1) - 2, -1, -1):
100
+ res = transition(res, x, poly[..., k])
101
+ return res
102
+
103
+
104
+ def _matrix_polynomial_value(poly, x, zero_power=None):
105
+ """
106
+ Evaluates `poly(x)` for the (batched) matrix input `x`.
107
+ Check out `_polynomial_value` function for more details.
108
+ """
109
+
110
+ # matrix-aware Horner's rule iteration
111
+ def transition(curr_poly_val, x, poly_coeff):
112
+ res = x.matmul(curr_poly_val)
113
+ res.diagonal(dim1=-2, dim2=-1).add_(poly_coeff.unsqueeze(-1))
114
+ return res
115
+
116
+ if zero_power is None:
117
+ zero_power = torch.eye(
118
+ x.size(-1), x.size(-1), dtype=x.dtype, device=x.device
119
+ ).view(*([1] * len(list(x.shape[:-2]))), x.size(-1), x.size(-1))
120
+
121
+ return _polynomial_value(poly, x, zero_power, transition)
122
+
123
+
124
+ def _vector_polynomial_value(poly, x, zero_power=None):
125
+ """
126
+ Evaluates `poly(x)` for the (batched) vector input `x`.
127
+ Check out `_polynomial_value` function for more details.
128
+ """
129
+
130
+ # vector-aware Horner's rule iteration
131
+ def transition(curr_poly_val, x, poly_coeff):
132
+ res = torch.addcmul(poly_coeff.unsqueeze(-1), x, curr_poly_val)
133
+ return res
134
+
135
+ if zero_power is None:
136
+ zero_power = x.new_ones(1).expand(x.shape)
137
+
138
+ return _polynomial_value(poly, x, zero_power, transition)
139
+
140
+
141
+ def _symeig_backward_partial_eigenspace(D_grad, U_grad, A, D, U, largest):
142
+ # compute a projection operator onto an orthogonal subspace spanned by the
143
+ # columns of U defined as (I - UU^T)
144
+ Ut = U.mT.contiguous()
145
+ proj_U_ortho = -U.matmul(Ut)
146
+ proj_U_ortho.diagonal(dim1=-2, dim2=-1).add_(1)
147
+
148
+ # compute U_ortho, a basis for the orthogonal complement to the span(U),
149
+ # by projecting a random [..., m, m - k] matrix onto the subspace spanned
150
+ # by the columns of U.
151
+ #
152
+ # fix generator for determinism
153
+ gen = torch.Generator(A.device)
154
+
155
+ # orthogonal complement to the span(U)
156
+ U_ortho = proj_U_ortho.matmul(
157
+ torch.randn(
158
+ (*A.shape[:-1], A.size(-1) - D.size(-1)),
159
+ dtype=A.dtype,
160
+ device=A.device,
161
+ generator=gen,
162
+ )
163
+ )
164
+ U_ortho_t = U_ortho.mT.contiguous()
165
+
166
+ # compute the coefficients of the characteristic polynomial of the tensor D.
167
+ # Note that D is diagonal, so the diagonal elements are exactly the roots
168
+ # of the characteristic polynomial.
169
+ chr_poly_D = _polynomial_coefficients_given_roots(D)
170
+
171
+ # the code below finds the explicit solution to the Sylvester equation
172
+ # U_ortho^T A U_ortho dX - dX D = -U_ortho^T A U
173
+ # and incorporates it into the whole gradient stored in the `res` variable.
174
+ #
175
+ # Equivalent to the following naive implementation:
176
+ # res = A.new_zeros(A.shape)
177
+ # p_res = A.new_zeros(*A.shape[:-1], D.size(-1))
178
+ # for k in range(1, chr_poly_D.size(-1)):
179
+ # p_res.zero_()
180
+ # for i in range(0, k):
181
+ # p_res += (A.matrix_power(k - 1 - i) @ U_grad) * D.pow(i).unsqueeze(-2)
182
+ # res -= chr_poly_D[k] * (U_ortho @ poly_D_at_A.inverse() @ U_ortho_t @ p_res @ U.t())
183
+ #
184
+ # Note that dX is a differential, so the gradient contribution comes from the backward sensitivity
185
+ # Tr(f(U_grad, D_grad, A, U, D)^T dX) = Tr(g(U_grad, A, U, D)^T dA) for some functions f and g,
186
+ # and we need to compute g(U_grad, A, U, D)
187
+ #
188
+ # The naive implementation is based on the paper
189
+ # Hu, Qingxi, and Daizhan Cheng.
190
+ # "The polynomial solution to the Sylvester matrix equation."
191
+ # Applied mathematics letters 19.9 (2006): 859-864.
192
+ #
193
+ # We can modify the computation of `p_res` from above in a more efficient way
194
+ # p_res = U_grad * (chr_poly_D[1] * D.pow(0) + ... + chr_poly_D[k] * D.pow(k)).unsqueeze(-2)
195
+ # + A U_grad * (chr_poly_D[2] * D.pow(0) + ... + chr_poly_D[k] * D.pow(k - 1)).unsqueeze(-2)
196
+ # + ...
197
+ # + A.matrix_power(k - 1) U_grad * chr_poly_D[k]
198
+ # Note that this saves us from redundant matrix products with A (elimination of matrix_power)
199
+ U_grad_projected = U_grad
200
+ series_acc = U_grad_projected.new_zeros(U_grad_projected.shape)
201
+ for k in range(1, chr_poly_D.size(-1)):
202
+ poly_D = _vector_polynomial_value(chr_poly_D[..., k:], D)
203
+ series_acc += U_grad_projected * poly_D.unsqueeze(-2)
204
+ U_grad_projected = A.matmul(U_grad_projected)
205
+
206
+ # compute chr_poly_D(A) which essentially is:
207
+ #
208
+ # chr_poly_D_at_A = A.new_zeros(A.shape)
209
+ # for k in range(chr_poly_D.size(-1)):
210
+ # chr_poly_D_at_A += chr_poly_D[k] * A.matrix_power(k)
211
+ #
212
+ # Note, however, for better performance we use the Horner's rule
213
+ chr_poly_D_at_A = _matrix_polynomial_value(chr_poly_D, A)
214
+
215
+ # compute the action of `chr_poly_D_at_A` restricted to U_ortho_t
216
+ chr_poly_D_at_A_to_U_ortho = torch.matmul(
217
+ U_ortho_t, torch.matmul(chr_poly_D_at_A, U_ortho)
218
+ )
219
+ # we need to invert 'chr_poly_D_at_A_to_U_ortho`, for that we compute its
220
+ # Cholesky decomposition and then use `torch.cholesky_solve` for better stability.
221
+ # Cholesky decomposition requires the input to be positive-definite.
222
+ # Note that `chr_poly_D_at_A_to_U_ortho` is positive-definite if
223
+ # 1. `largest` == False, or
224
+ # 2. `largest` == True and `k` is even
225
+ # under the assumption that `A` has distinct eigenvalues.
226
+ #
227
+ # check if `chr_poly_D_at_A_to_U_ortho` is positive-definite or negative-definite
228
+ chr_poly_D_at_A_to_U_ortho_sign = -1 if (largest and (k % 2 == 1)) else +1
229
+ chr_poly_D_at_A_to_U_ortho_L = torch.linalg.cholesky(
230
+ chr_poly_D_at_A_to_U_ortho_sign * chr_poly_D_at_A_to_U_ortho
231
+ )
232
+
233
+ # compute the gradient part in span(U)
234
+ res = _symeig_backward_complete_eigenspace(D_grad, U_grad, A, D, U)
235
+
236
+ # incorporate the Sylvester equation solution into the full gradient
237
+ # it resides in span(U_ortho)
238
+ res -= U_ortho.matmul(
239
+ chr_poly_D_at_A_to_U_ortho_sign
240
+ * torch.cholesky_solve(
241
+ U_ortho_t.matmul(series_acc), chr_poly_D_at_A_to_U_ortho_L
242
+ )
243
+ ).matmul(Ut)
244
+
245
+ return res
246
+
247
+
248
+ def _symeig_backward(D_grad, U_grad, A, D, U, largest):
249
+ # if `U` is square, then the columns of `U` is a complete eigenspace
250
+ if U.size(-1) == U.size(-2):
251
+ return _symeig_backward_complete_eigenspace(D_grad, U_grad, A, D, U)
252
+ else:
253
+ return _symeig_backward_partial_eigenspace(D_grad, U_grad, A, D, U, largest)
254
+
255
+
256
+ class LOBPCGAutogradFunction(torch.autograd.Function):
257
+ @staticmethod
258
+ def forward( # type: ignore[override]
259
+ ctx,
260
+ A: Tensor,
261
+ k: Optional[int] = None,
262
+ B: Optional[Tensor] = None,
263
+ X: Optional[Tensor] = None,
264
+ n: Optional[int] = None,
265
+ iK: Optional[Tensor] = None,
266
+ niter: Optional[int] = None,
267
+ tol: Optional[float] = None,
268
+ largest: Optional[bool] = None,
269
+ method: Optional[str] = None,
270
+ tracker: None = None,
271
+ ortho_iparams: Optional[dict[str, int]] = None,
272
+ ortho_fparams: Optional[dict[str, float]] = None,
273
+ ortho_bparams: Optional[dict[str, bool]] = None,
274
+ ) -> tuple[Tensor, Tensor]:
275
+ # makes sure that input is contiguous for efficiency.
276
+ # Note: autograd does not support dense gradients for sparse input yet.
277
+ A = A.contiguous() if (not A.is_sparse) else A
278
+ if B is not None:
279
+ B = B.contiguous() if (not B.is_sparse) else B
280
+
281
+ D, U = _lobpcg(
282
+ A,
283
+ k,
284
+ B,
285
+ X,
286
+ n,
287
+ iK,
288
+ niter,
289
+ tol,
290
+ largest,
291
+ method,
292
+ tracker,
293
+ ortho_iparams,
294
+ ortho_fparams,
295
+ ortho_bparams,
296
+ )
297
+
298
+ ctx.save_for_backward(A, B, D, U)
299
+ ctx.largest = largest
300
+
301
+ return D, U
302
+
303
+ @staticmethod
304
+ def backward(ctx, D_grad, U_grad):
305
+ A_grad = B_grad = None
306
+ grads = [None] * 14
307
+
308
+ A, B, D, U = ctx.saved_tensors
309
+ largest = ctx.largest
310
+
311
+ # lobpcg.backward has some limitations. Checks for unsupported input
312
+ if A.is_sparse or (B is not None and B.is_sparse and ctx.needs_input_grad[2]):
313
+ raise ValueError(
314
+ "lobpcg.backward does not support sparse input yet."
315
+ "Note that lobpcg.forward does though."
316
+ )
317
+ if (
318
+ A.dtype in (torch.complex64, torch.complex128)
319
+ or B is not None
320
+ and B.dtype in (torch.complex64, torch.complex128)
321
+ ):
322
+ raise ValueError(
323
+ "lobpcg.backward does not support complex input yet."
324
+ "Note that lobpcg.forward does though."
325
+ )
326
+ if B is not None:
327
+ raise ValueError(
328
+ "lobpcg.backward does not support backward with B != I yet."
329
+ )
330
+
331
+ if largest is None:
332
+ largest = True
333
+
334
+ # symeig backward
335
+ if B is None:
336
+ A_grad = _symeig_backward(D_grad, U_grad, A, D, U, largest)
337
+
338
+ # A has index 0
339
+ grads[0] = A_grad
340
+ # B has index 2
341
+ grads[2] = B_grad
342
+ return tuple(grads)
343
+
344
+
345
+ def lobpcg(
346
+ A: Tensor,
347
+ k: Optional[int] = None,
348
+ B: Optional[Tensor] = None,
349
+ X: Optional[Tensor] = None,
350
+ n: Optional[int] = None,
351
+ iK: Optional[Tensor] = None,
352
+ niter: Optional[int] = None,
353
+ tol: Optional[float] = None,
354
+ largest: Optional[bool] = None,
355
+ method: Optional[str] = None,
356
+ tracker: None = None,
357
+ ortho_iparams: Optional[dict[str, int]] = None,
358
+ ortho_fparams: Optional[dict[str, float]] = None,
359
+ ortho_bparams: Optional[dict[str, bool]] = None,
360
+ ) -> tuple[Tensor, Tensor]:
361
+ """Find the k largest (or smallest) eigenvalues and the corresponding
362
+ eigenvectors of a symmetric positive definite generalized
363
+ eigenvalue problem using matrix-free LOBPCG methods.
364
+
365
+ This function is a front-end to the following LOBPCG algorithms
366
+ selectable via `method` argument:
367
+
368
+ `method="basic"` - the LOBPCG method introduced by Andrew
369
+ Knyazev, see [Knyazev2001]. A less robust method, may fail when
370
+ Cholesky is applied to singular input.
371
+
372
+ `method="ortho"` - the LOBPCG method with orthogonal basis
373
+ selection [StathopoulosEtal2002]. A robust method.
374
+
375
+ Supported inputs are dense, sparse, and batches of dense matrices.
376
+
377
+ .. note:: In general, the basic method spends least time per
378
+ iteration. However, the robust methods converge much faster and
379
+ are more stable. So, the usage of the basic method is generally
380
+ not recommended but there exist cases where the usage of the
381
+ basic method may be preferred.
382
+
383
+ .. warning:: The backward method does not support sparse and complex inputs.
384
+ It works only when `B` is not provided (i.e. `B == None`).
385
+ We are actively working on extensions, and the details of
386
+ the algorithms are going to be published promptly.
387
+
388
+ .. warning:: While it is assumed that `A` is symmetric, `A.grad` is not.
389
+ To make sure that `A.grad` is symmetric, so that `A - t * A.grad` is symmetric
390
+ in first-order optimization routines, prior to running `lobpcg`
391
+ we do the following symmetrization map: `A -> (A + A.t()) / 2`.
392
+ The map is performed only when the `A` requires gradients.
393
+
394
+ .. warning:: LOBPCG algorithm is not applicable when the number of `A`'s rows
395
+ is smaller than 3x the number of requested eigenpairs `n`.
396
+
397
+ Args:
398
+
399
+ A (Tensor): the input tensor of size :math:`(*, m, m)`
400
+
401
+ k (integer, optional): the number of requested
402
+ eigenpairs. Default is the number of :math:`X`
403
+ columns (when specified) or `1`.
404
+
405
+ B (Tensor, optional): the input tensor of size :math:`(*, m,
406
+ m)`. When not specified, `B` is interpreted as
407
+ identity matrix.
408
+
409
+ X (tensor, optional): the input tensor of size :math:`(*, m, n)`
410
+ where `k <= n <= m`. When specified, it is used as
411
+ initial approximation of eigenvectors. X must be a
412
+ dense tensor.
413
+
414
+ n (integer, optional): if :math:`X` is not specified then `n`
415
+ specifies the size of the generated random
416
+ approximation of eigenvectors. Default value for `n`
417
+ is `k`. If :math:`X` is specified, any provided value of `n` is
418
+ ignored and `n` is automatically set to the number of
419
+ columns in :math:`X`.
420
+
421
+ iK (tensor, optional): the input tensor of size :math:`(*, m,
422
+ m)`. When specified, it will be used as preconditioner.
423
+
424
+ niter (int, optional): maximum number of iterations. When
425
+ reached, the iteration process is hard-stopped and
426
+ the current approximation of eigenpairs is returned.
427
+ For infinite iteration but until convergence criteria
428
+ is met, use `-1`.
429
+
430
+ tol (float, optional): residual tolerance for stopping
431
+ criterion. Default is `feps ** 0.5` where `feps` is
432
+ smallest non-zero floating-point number of the given
433
+ input tensor `A` data type.
434
+
435
+ largest (bool, optional): when True, solve the eigenproblem for
436
+ the largest eigenvalues. Otherwise, solve the
437
+ eigenproblem for smallest eigenvalues. Default is
438
+ `True`.
439
+
440
+ method (str, optional): select LOBPCG method. See the
441
+ description of the function above. Default is
442
+ "ortho".
443
+
444
+ tracker (callable, optional) : a function for tracing the
445
+ iteration process. When specified, it is called at
446
+ each iteration step with LOBPCG instance as an
447
+ argument. The LOBPCG instance holds the full state of
448
+ the iteration process in the following attributes:
449
+
450
+ `iparams`, `fparams`, `bparams` - dictionaries of
451
+ integer, float, and boolean valued input
452
+ parameters, respectively
453
+
454
+ `ivars`, `fvars`, `bvars`, `tvars` - dictionaries
455
+ of integer, float, boolean, and Tensor valued
456
+ iteration variables, respectively.
457
+
458
+ `A`, `B`, `iK` - input Tensor arguments.
459
+
460
+ `E`, `X`, `S`, `R` - iteration Tensor variables.
461
+
462
+ For instance:
463
+
464
+ `ivars["istep"]` - the current iteration step
465
+ `X` - the current approximation of eigenvectors
466
+ `E` - the current approximation of eigenvalues
467
+ `R` - the current residual
468
+ `ivars["converged_count"]` - the current number of converged eigenpairs
469
+ `tvars["rerr"]` - the current state of convergence criteria
470
+
471
+ Note that when `tracker` stores Tensor objects from
472
+ the LOBPCG instance, it must make copies of these.
473
+
474
+ If `tracker` sets `bvars["force_stop"] = True`, the
475
+ iteration process will be hard-stopped.
476
+
477
+ ortho_iparams, ortho_fparams, ortho_bparams (dict, optional):
478
+ various parameters to LOBPCG algorithm when using
479
+ `method="ortho"`.
480
+
481
+ Returns:
482
+
483
+ E (Tensor): tensor of eigenvalues of size :math:`(*, k)`
484
+
485
+ X (Tensor): tensor of eigenvectors of size :math:`(*, m, k)`
486
+
487
+ References:
488
+
489
+ [Knyazev2001] Andrew V. Knyazev. (2001) Toward the Optimal
490
+ Preconditioned Eigensolver: Locally Optimal Block Preconditioned
491
+ Conjugate Gradient Method. SIAM J. Sci. Comput., 23(2),
492
+ 517-541. (25 pages)
493
+ https://epubs.siam.org/doi/abs/10.1137/S1064827500366124
494
+
495
+ [StathopoulosEtal2002] Andreas Stathopoulos and Kesheng
496
+ Wu. (2002) A Block Orthogonalization Procedure with Constant
497
+ Synchronization Requirements. SIAM J. Sci. Comput., 23(6),
498
+ 2165-2182. (18 pages)
499
+ https://epubs.siam.org/doi/10.1137/S1064827500370883
500
+
501
+ [DuerschEtal2018] Jed A. Duersch, Meiyue Shao, Chao Yang, Ming
502
+ Gu. (2018) A Robust and Efficient Implementation of LOBPCG.
503
+ SIAM J. Sci. Comput., 40(5), C655-C676. (22 pages)
504
+ https://arxiv.org/abs/1704.07458
505
+
506
+ """
507
+
508
+ if not torch.jit.is_scripting():
509
+ tensor_ops = (A, B, X, iK)
510
+ if not set(map(type, tensor_ops)).issubset(
511
+ (torch.Tensor, type(None))
512
+ ) and has_torch_function(tensor_ops):
513
+ return handle_torch_function(
514
+ lobpcg,
515
+ tensor_ops,
516
+ A,
517
+ k=k,
518
+ B=B,
519
+ X=X,
520
+ n=n,
521
+ iK=iK,
522
+ niter=niter,
523
+ tol=tol,
524
+ largest=largest,
525
+ method=method,
526
+ tracker=tracker,
527
+ ortho_iparams=ortho_iparams,
528
+ ortho_fparams=ortho_fparams,
529
+ ortho_bparams=ortho_bparams,
530
+ )
531
+
532
+ if not torch._jit_internal.is_scripting():
533
+ if A.requires_grad or (B is not None and B.requires_grad):
534
+ # While it is expected that `A` is symmetric,
535
+ # the `A_grad` might be not. Therefore we perform the trick below,
536
+ # so that `A_grad` becomes symmetric.
537
+ # The symmetrization is important for first-order optimization methods,
538
+ # so that (A - alpha * A_grad) is still a symmetric matrix.
539
+ # Same holds for `B`.
540
+ A_sym = (A + A.mT) / 2
541
+ B_sym = (B + B.mT) / 2 if (B is not None) else None
542
+
543
+ return LOBPCGAutogradFunction.apply(
544
+ A_sym,
545
+ k,
546
+ B_sym,
547
+ X,
548
+ n,
549
+ iK,
550
+ niter,
551
+ tol,
552
+ largest,
553
+ method,
554
+ tracker,
555
+ ortho_iparams,
556
+ ortho_fparams,
557
+ ortho_bparams,
558
+ )
559
+ else:
560
+ if A.requires_grad or (B is not None and B.requires_grad):
561
+ raise RuntimeError(
562
+ "Script and require grads is not supported atm."
563
+ "If you just want to do the forward, use .detach()"
564
+ "on A and B before calling into lobpcg"
565
+ )
566
+
567
+ return _lobpcg(
568
+ A,
569
+ k,
570
+ B,
571
+ X,
572
+ n,
573
+ iK,
574
+ niter,
575
+ tol,
576
+ largest,
577
+ method,
578
+ tracker,
579
+ ortho_iparams,
580
+ ortho_fparams,
581
+ ortho_bparams,
582
+ )
583
+
584
+
585
+ def _lobpcg(
586
+ A: Tensor,
587
+ k: Optional[int] = None,
588
+ B: Optional[Tensor] = None,
589
+ X: Optional[Tensor] = None,
590
+ n: Optional[int] = None,
591
+ iK: Optional[Tensor] = None,
592
+ niter: Optional[int] = None,
593
+ tol: Optional[float] = None,
594
+ largest: Optional[bool] = None,
595
+ method: Optional[str] = None,
596
+ tracker: None = None,
597
+ ortho_iparams: Optional[dict[str, int]] = None,
598
+ ortho_fparams: Optional[dict[str, float]] = None,
599
+ ortho_bparams: Optional[dict[str, bool]] = None,
600
+ ) -> tuple[Tensor, Tensor]:
601
+ # A must be square:
602
+ assert A.shape[-2] == A.shape[-1], A.shape
603
+ if B is not None:
604
+ # A and B must have the same shapes:
605
+ assert A.shape == B.shape, (A.shape, B.shape)
606
+
607
+ dtype = _utils.get_floating_dtype(A)
608
+ device = A.device
609
+ if tol is None:
610
+ feps = {torch.float32: 1.2e-07, torch.float64: 2.23e-16}[dtype]
611
+ tol = feps**0.5
612
+
613
+ m = A.shape[-1]
614
+ k = (1 if X is None else X.shape[-1]) if k is None else k
615
+ n = (k if n is None else n) if X is None else X.shape[-1]
616
+
617
+ if m < 3 * n:
618
+ raise ValueError(
619
+ f"LPBPCG algorithm is not applicable when the number of A rows (={m})"
620
+ f" is smaller than 3 x the number of requested eigenpairs (={n})"
621
+ )
622
+
623
+ method = "ortho" if method is None else method
624
+
625
+ iparams = {
626
+ "m": m,
627
+ "n": n,
628
+ "k": k,
629
+ "niter": 1000 if niter is None else niter,
630
+ }
631
+
632
+ fparams = {
633
+ "tol": tol,
634
+ }
635
+
636
+ bparams = {"largest": True if largest is None else largest}
637
+
638
+ if method == "ortho":
639
+ if ortho_iparams is not None:
640
+ iparams.update(ortho_iparams)
641
+ if ortho_fparams is not None:
642
+ fparams.update(ortho_fparams)
643
+ if ortho_bparams is not None:
644
+ bparams.update(ortho_bparams)
645
+ iparams["ortho_i_max"] = iparams.get("ortho_i_max", 3)
646
+ iparams["ortho_j_max"] = iparams.get("ortho_j_max", 3)
647
+ fparams["ortho_tol"] = fparams.get("ortho_tol", tol)
648
+ fparams["ortho_tol_drop"] = fparams.get("ortho_tol_drop", tol)
649
+ fparams["ortho_tol_replace"] = fparams.get("ortho_tol_replace", tol)
650
+ bparams["ortho_use_drop"] = bparams.get("ortho_use_drop", False)
651
+
652
+ if not torch.jit.is_scripting():
653
+ LOBPCG.call_tracker = LOBPCG_call_tracker # type: ignore[method-assign]
654
+
655
+ if len(A.shape) > 2:
656
+ N = int(torch.prod(torch.tensor(A.shape[:-2])))
657
+ bA = A.reshape((N,) + A.shape[-2:])
658
+ bB = B.reshape((N,) + A.shape[-2:]) if B is not None else None
659
+ bX = X.reshape((N,) + X.shape[-2:]) if X is not None else None
660
+ bE = torch.empty((N, k), dtype=dtype, device=device)
661
+ bXret = torch.empty((N, m, k), dtype=dtype, device=device)
662
+
663
+ for i in range(N):
664
+ A_ = bA[i]
665
+ B_ = bB[i] if bB is not None else None
666
+ X_ = (
667
+ torch.randn((m, n), dtype=dtype, device=device) if bX is None else bX[i]
668
+ )
669
+ assert len(X_.shape) == 2 and X_.shape == (m, n), (X_.shape, (m, n))
670
+ iparams["batch_index"] = i
671
+ worker = LOBPCG(A_, B_, X_, iK, iparams, fparams, bparams, method, tracker)
672
+ worker.run()
673
+ bE[i] = worker.E[:k]
674
+ bXret[i] = worker.X[:, :k]
675
+
676
+ if not torch.jit.is_scripting():
677
+ LOBPCG.call_tracker = LOBPCG_call_tracker_orig # type: ignore[method-assign]
678
+
679
+ return bE.reshape(A.shape[:-2] + (k,)), bXret.reshape(A.shape[:-2] + (m, k))
680
+
681
+ X = torch.randn((m, n), dtype=dtype, device=device) if X is None else X
682
+ assert len(X.shape) == 2 and X.shape == (m, n), (X.shape, (m, n))
683
+
684
+ worker = LOBPCG(A, B, X, iK, iparams, fparams, bparams, method, tracker)
685
+
686
+ worker.run()
687
+
688
+ if not torch.jit.is_scripting():
689
+ LOBPCG.call_tracker = LOBPCG_call_tracker_orig # type: ignore[method-assign]
690
+
691
+ return worker.E[:k], worker.X[:, :k]
692
+
693
+
694
+ class LOBPCG:
695
+ """Worker class of LOBPCG methods."""
696
+
697
+ def __init__(
698
+ self,
699
+ A: Optional[Tensor],
700
+ B: Optional[Tensor],
701
+ X: Tensor,
702
+ iK: Optional[Tensor],
703
+ iparams: dict[str, int],
704
+ fparams: dict[str, float],
705
+ bparams: dict[str, bool],
706
+ method: str,
707
+ tracker: None,
708
+ ) -> None:
709
+ # constant parameters
710
+ self.A = A
711
+ self.B = B
712
+ self.iK = iK
713
+ self.iparams = iparams
714
+ self.fparams = fparams
715
+ self.bparams = bparams
716
+ self.method = method
717
+ self.tracker = tracker
718
+ m = iparams["m"]
719
+ n = iparams["n"]
720
+
721
+ # variable parameters
722
+ self.X = X
723
+ self.E = torch.zeros((n,), dtype=X.dtype, device=X.device)
724
+ self.R = torch.zeros((m, n), dtype=X.dtype, device=X.device)
725
+ self.S = torch.zeros((m, 3 * n), dtype=X.dtype, device=X.device)
726
+ self.tvars: dict[str, Tensor] = {}
727
+ self.ivars: dict[str, int] = {"istep": 0}
728
+ self.fvars: dict[str, float] = {"_": 0.0}
729
+ self.bvars: dict[str, bool] = {"_": False}
730
+
731
+ def __str__(self):
732
+ lines = ["LOPBCG:"]
733
+ lines += [f" iparams={self.iparams}"]
734
+ lines += [f" fparams={self.fparams}"]
735
+ lines += [f" bparams={self.bparams}"]
736
+ lines += [f" ivars={self.ivars}"]
737
+ lines += [f" fvars={self.fvars}"]
738
+ lines += [f" bvars={self.bvars}"]
739
+ lines += [f" tvars={self.tvars}"]
740
+ lines += [f" A={self.A}"]
741
+ lines += [f" B={self.B}"]
742
+ lines += [f" iK={self.iK}"]
743
+ lines += [f" X={self.X}"]
744
+ lines += [f" E={self.E}"]
745
+ r = ""
746
+ for line in lines:
747
+ r += line + "\n"
748
+ return r
749
+
750
+ def update(self):
751
+ """Set and update iteration variables."""
752
+ if self.ivars["istep"] == 0:
753
+ X_norm = float(torch.norm(self.X))
754
+ iX_norm = X_norm**-1
755
+ A_norm = float(torch.norm(_utils.matmul(self.A, self.X))) * iX_norm
756
+ B_norm = float(torch.norm(_utils.matmul(self.B, self.X))) * iX_norm
757
+ self.fvars["X_norm"] = X_norm
758
+ self.fvars["A_norm"] = A_norm
759
+ self.fvars["B_norm"] = B_norm
760
+ self.ivars["iterations_left"] = self.iparams["niter"]
761
+ self.ivars["converged_count"] = 0
762
+ self.ivars["converged_end"] = 0
763
+
764
+ if self.method == "ortho":
765
+ self._update_ortho()
766
+ else:
767
+ self._update_basic()
768
+
769
+ self.ivars["iterations_left"] = self.ivars["iterations_left"] - 1
770
+ self.ivars["istep"] = self.ivars["istep"] + 1
771
+
772
+ def update_residual(self):
773
+ """Update residual R from A, B, X, E."""
774
+ mm = _utils.matmul
775
+ self.R = mm(self.A, self.X) - mm(self.B, self.X) * self.E
776
+
777
+ def update_converged_count(self):
778
+ """Determine the number of converged eigenpairs using backward stable
779
+ convergence criterion, see discussion in Sec 4.3 of [DuerschEtal2018].
780
+
781
+ Users may redefine this method for custom convergence criteria.
782
+ """
783
+ # (...) -> int
784
+ prev_count = self.ivars["converged_count"]
785
+ tol = self.fparams["tol"]
786
+ A_norm = self.fvars["A_norm"]
787
+ B_norm = self.fvars["B_norm"]
788
+ E, X, R = self.E, self.X, self.R
789
+ rerr = torch.norm(R, 2, (0,)) / (
790
+ torch.norm(X, 2, (0,)) * (A_norm + torch.abs(E[: X.shape[-1]]) * B_norm)
791
+ )
792
+ converged = rerr < tol
793
+ count = 0
794
+ for b in converged:
795
+ if not b:
796
+ # ignore convergence of following pairs to ensure
797
+ # strict ordering of eigenpairs
798
+ break
799
+ count += 1
800
+ assert count >= prev_count, (
801
+ f"the number of converged eigenpairs (was {prev_count}, got {count}) cannot decrease"
802
+ )
803
+ self.ivars["converged_count"] = count
804
+ self.tvars["rerr"] = rerr
805
+ return count
806
+
807
+ def stop_iteration(self):
808
+ """Return True to stop iterations.
809
+
810
+ Note that tracker (if defined) can force-stop iterations by
811
+ setting ``worker.bvars['force_stop'] = True``.
812
+ """
813
+ return (
814
+ self.bvars.get("force_stop", False)
815
+ or self.ivars["iterations_left"] == 0
816
+ or self.ivars["converged_count"] >= self.iparams["k"]
817
+ )
818
+
819
+ def run(self):
820
+ """Run LOBPCG iterations.
821
+
822
+ Use this method as a template for implementing LOBPCG
823
+ iteration scheme with custom tracker that is compatible with
824
+ TorchScript.
825
+ """
826
+ self.update()
827
+
828
+ if not torch.jit.is_scripting() and self.tracker is not None:
829
+ self.call_tracker()
830
+
831
+ while not self.stop_iteration():
832
+ self.update()
833
+
834
+ if not torch.jit.is_scripting() and self.tracker is not None:
835
+ self.call_tracker()
836
+
837
+ @torch.jit.unused
838
+ def call_tracker(self):
839
+ """Interface for tracking iteration process in Python mode.
840
+
841
+ Tracking the iteration process is disabled in TorchScript
842
+ mode. In fact, one should specify tracker=None when JIT
843
+ compiling functions using lobpcg.
844
+ """
845
+ # do nothing when in TorchScript mode
846
+
847
+ # Internal methods
848
+
849
+ def _update_basic(self):
850
+ """
851
+ Update or initialize iteration variables when `method == "basic"`.
852
+ """
853
+ mm = torch.matmul
854
+ ns = self.ivars["converged_end"]
855
+ nc = self.ivars["converged_count"]
856
+ n = self.iparams["n"]
857
+ largest = self.bparams["largest"]
858
+
859
+ if self.ivars["istep"] == 0:
860
+ Ri = self._get_rayleigh_ritz_transform(self.X)
861
+ M = _utils.qform(_utils.qform(self.A, self.X), Ri)
862
+ E, Z = _utils.symeig(M, largest)
863
+ self.X[:] = mm(self.X, mm(Ri, Z))
864
+ self.E[:] = E
865
+ np = 0
866
+ self.update_residual()
867
+ nc = self.update_converged_count()
868
+ self.S[..., :n] = self.X
869
+
870
+ W = _utils.matmul(self.iK, self.R)
871
+ self.ivars["converged_end"] = ns = n + np + W.shape[-1]
872
+ self.S[:, n + np : ns] = W
873
+ else:
874
+ S_ = self.S[:, nc:ns]
875
+ Ri = self._get_rayleigh_ritz_transform(S_)
876
+ M = _utils.qform(_utils.qform(self.A, S_), Ri)
877
+ E_, Z = _utils.symeig(M, largest)
878
+ self.X[:, nc:] = mm(S_, mm(Ri, Z[:, : n - nc]))
879
+ self.E[nc:] = E_[: n - nc]
880
+ P = mm(S_, mm(Ri, Z[:, n : 2 * n - nc]))
881
+ np = P.shape[-1]
882
+
883
+ self.update_residual()
884
+ nc = self.update_converged_count()
885
+ self.S[..., :n] = self.X
886
+ self.S[:, n : n + np] = P
887
+ W = _utils.matmul(self.iK, self.R[:, nc:])
888
+
889
+ self.ivars["converged_end"] = ns = n + np + W.shape[-1]
890
+ self.S[:, n + np : ns] = W
891
+
892
+ def _update_ortho(self):
893
+ """
894
+ Update or initialize iteration variables when `method == "ortho"`.
895
+ """
896
+ mm = torch.matmul
897
+ ns = self.ivars["converged_end"]
898
+ nc = self.ivars["converged_count"]
899
+ n = self.iparams["n"]
900
+ largest = self.bparams["largest"]
901
+
902
+ if self.ivars["istep"] == 0:
903
+ Ri = self._get_rayleigh_ritz_transform(self.X)
904
+ M = _utils.qform(_utils.qform(self.A, self.X), Ri)
905
+ _E, Z = _utils.symeig(M, largest)
906
+ self.X = mm(self.X, mm(Ri, Z))
907
+ self.update_residual()
908
+ np = 0
909
+ nc = self.update_converged_count()
910
+ self.S[:, :n] = self.X
911
+ W = self._get_ortho(self.R, self.X)
912
+ ns = self.ivars["converged_end"] = n + np + W.shape[-1]
913
+ self.S[:, n + np : ns] = W
914
+
915
+ else:
916
+ S_ = self.S[:, nc:ns]
917
+ # Rayleigh-Ritz procedure
918
+ E_, Z = _utils.symeig(_utils.qform(self.A, S_), largest)
919
+
920
+ # Update E, X, P
921
+ self.X[:, nc:] = mm(S_, Z[:, : n - nc])
922
+ self.E[nc:] = E_[: n - nc]
923
+ P = mm(S_, mm(Z[:, n - nc :], _utils.basis(Z[: n - nc, n - nc :].mT)))
924
+ np = P.shape[-1]
925
+
926
+ # check convergence
927
+ self.update_residual()
928
+ nc = self.update_converged_count()
929
+
930
+ # update S
931
+ self.S[:, :n] = self.X
932
+ self.S[:, n : n + np] = P
933
+ W = self._get_ortho(self.R[:, nc:], self.S[:, : n + np])
934
+ ns = self.ivars["converged_end"] = n + np + W.shape[-1]
935
+ self.S[:, n + np : ns] = W
936
+
937
+ def _get_rayleigh_ritz_transform(self, S):
938
+ """Return a transformation matrix that is used in Rayleigh-Ritz
939
+ procedure for reducing a general eigenvalue problem :math:`(S^TAS)
940
+ C = (S^TBS) C E` to a standard eigenvalue problem :math: `(Ri^T
941
+ S^TAS Ri) Z = Z E` where `C = Ri Z`.
942
+
943
+ .. note:: In the original Rayleight-Ritz procedure in
944
+ [DuerschEtal2018], the problem is formulated as follows::
945
+
946
+ SAS = S^T A S
947
+ SBS = S^T B S
948
+ D = (<diagonal matrix of SBS>) ** -1/2
949
+ R^T R = Cholesky(D SBS D)
950
+ Ri = D R^-1
951
+ solve symeig problem Ri^T SAS Ri Z = Theta Z
952
+ C = Ri Z
953
+
954
+ To reduce the number of matrix products (denoted by empty
955
+ space between matrices), here we introduce element-wise
956
+ products (denoted by symbol `*`) so that the Rayleight-Ritz
957
+ procedure becomes::
958
+
959
+ SAS = S^T A S
960
+ SBS = S^T B S
961
+ d = (<diagonal of SBS>) ** -1/2 # this is 1-d column vector
962
+ dd = d d^T # this is 2-d matrix
963
+ R^T R = Cholesky(dd * SBS)
964
+ Ri = R^-1 * d # broadcasting
965
+ solve symeig problem Ri^T SAS Ri Z = Theta Z
966
+ C = Ri Z
967
+
968
+ where `dd` is 2-d matrix that replaces matrix products `D M
969
+ D` with one element-wise product `M * dd`; and `d` replaces
970
+ matrix product `D M` with element-wise product `M *
971
+ d`. Also, creating the diagonal matrix `D` is avoided.
972
+
973
+ Args:
974
+ S (Tensor): the matrix basis for the search subspace, size is
975
+ :math:`(m, n)`.
976
+
977
+ Returns:
978
+ Ri (tensor): upper-triangular transformation matrix of size
979
+ :math:`(n, n)`.
980
+
981
+ """
982
+ B = self.B
983
+ SBS = _utils.qform(B, S)
984
+ d_row = SBS.diagonal(0, -2, -1) ** -0.5
985
+ d_col = d_row.reshape(d_row.shape[0], 1)
986
+ # TODO use torch.linalg.cholesky_solve once it is implemented
987
+ R = torch.linalg.cholesky((SBS * d_row) * d_col, upper=True)
988
+ return torch.linalg.solve_triangular(
989
+ R, d_row.diag_embed(), upper=True, left=False
990
+ )
991
+
992
+ def _get_svqb(self, U: Tensor, drop: bool, tau: float) -> Tensor:
993
+ """Return B-orthonormal U.
994
+
995
+ .. note:: When `drop` is `False` then `svqb` is based on the
996
+ Algorithm 4 from [DuerschPhD2015] that is a slight
997
+ modification of the corresponding algorithm
998
+ introduced in [StathopolousWu2002].
999
+
1000
+ Args:
1001
+
1002
+ U (Tensor) : initial approximation, size is (m, n)
1003
+ drop (bool) : when True, drop columns that
1004
+ contribution to the `span([U])` is small.
1005
+ tau (float) : positive tolerance
1006
+
1007
+ Returns:
1008
+
1009
+ U (Tensor) : B-orthonormal columns (:math:`U^T B U = I`), size
1010
+ is (m, n1), where `n1 = n` if `drop` is `False,
1011
+ otherwise `n1 <= n`.
1012
+
1013
+ """
1014
+ if torch.numel(U) == 0:
1015
+ return U
1016
+ UBU = _utils.qform(self.B, U)
1017
+ d = UBU.diagonal(0, -2, -1)
1018
+
1019
+ # Detect and drop exact zero columns from U. While the test
1020
+ # `abs(d) == 0` is unlikely to be True for random data, it is
1021
+ # possible to construct input data to lobpcg where it will be
1022
+ # True leading to a failure (notice the `d ** -0.5` operation
1023
+ # in the original algorithm). To prevent the failure, we drop
1024
+ # the exact zero columns here and then continue with the
1025
+ # original algorithm below.
1026
+ nz = torch.where(abs(d) != 0.0)
1027
+ assert len(nz) == 1, nz
1028
+ if len(nz[0]) < len(d):
1029
+ U = U[:, nz[0]]
1030
+ if torch.numel(U) == 0:
1031
+ return U
1032
+ UBU = _utils.qform(self.B, U)
1033
+ d = UBU.diagonal(0, -2, -1)
1034
+ nz = torch.where(abs(d) != 0.0)
1035
+ assert len(nz[0]) == len(d)
1036
+
1037
+ # The original algorithm 4 from [DuerschPhD2015].
1038
+ d_col = (d**-0.5).reshape(d.shape[0], 1)
1039
+ DUBUD = (UBU * d_col) * d_col.mT
1040
+ E, Z = _utils.symeig(DUBUD)
1041
+ t = tau * abs(E).max()
1042
+ if drop:
1043
+ keep = torch.where(E > t)
1044
+ assert len(keep) == 1, keep
1045
+ E = E[keep[0]]
1046
+ Z = Z[:, keep[0]]
1047
+ d_col = d_col[keep[0]]
1048
+ else:
1049
+ E[(torch.where(E < t))[0]] = t
1050
+
1051
+ return torch.matmul(U * d_col.mT, Z * E**-0.5)
1052
+
1053
+ def _get_ortho(self, U, V):
1054
+ """Return B-orthonormal U with columns are B-orthogonal to V.
1055
+
1056
+ .. note:: When `bparams["ortho_use_drop"] == False` then
1057
+ `_get_ortho` is based on the Algorithm 3 from
1058
+ [DuerschPhD2015] that is a slight modification of
1059
+ the corresponding algorithm introduced in
1060
+ [StathopolousWu2002]. Otherwise, the method
1061
+ implements Algorithm 6 from [DuerschPhD2015]
1062
+
1063
+ .. note:: If all U columns are B-collinear to V then the
1064
+ returned tensor U will be empty.
1065
+
1066
+ Args:
1067
+
1068
+ U (Tensor) : initial approximation, size is (m, n)
1069
+ V (Tensor) : B-orthogonal external basis, size is (m, k)
1070
+
1071
+ Returns:
1072
+
1073
+ U (Tensor) : B-orthonormal columns (:math:`U^T B U = I`)
1074
+ such that :math:`V^T B U=0`, size is (m, n1),
1075
+ where `n1 = n` if `drop` is `False, otherwise
1076
+ `n1 <= n`.
1077
+ """
1078
+ mm = torch.matmul
1079
+ mm_B = _utils.matmul
1080
+ m = self.iparams["m"]
1081
+ tau_ortho = self.fparams["ortho_tol"]
1082
+ tau_drop = self.fparams["ortho_tol_drop"]
1083
+ tau_replace = self.fparams["ortho_tol_replace"]
1084
+ i_max = self.iparams["ortho_i_max"]
1085
+ j_max = self.iparams["ortho_j_max"]
1086
+ # when use_drop==True, enable dropping U columns that have
1087
+ # small contribution to the `span([U, V])`.
1088
+ use_drop = self.bparams["ortho_use_drop"]
1089
+
1090
+ # clean up variables from the previous call
1091
+ for vkey in list(self.fvars.keys()):
1092
+ if vkey.startswith("ortho_") and vkey.endswith("_rerr"):
1093
+ self.fvars.pop(vkey)
1094
+ self.ivars.pop("ortho_i", 0)
1095
+ self.ivars.pop("ortho_j", 0)
1096
+
1097
+ BV_norm = torch.norm(mm_B(self.B, V))
1098
+ BU = mm_B(self.B, U)
1099
+ VBU = mm(V.mT, BU)
1100
+ i = j = 0
1101
+ for i in range(i_max):
1102
+ U = U - mm(V, VBU)
1103
+ drop = False
1104
+ tau_svqb = tau_drop
1105
+ for j in range(j_max):
1106
+ if use_drop:
1107
+ U = self._get_svqb(U, drop, tau_svqb)
1108
+ drop = True
1109
+ tau_svqb = tau_replace
1110
+ else:
1111
+ U = self._get_svqb(U, False, tau_replace)
1112
+ if torch.numel(U) == 0:
1113
+ # all initial U columns are B-collinear to V
1114
+ self.ivars["ortho_i"] = i
1115
+ self.ivars["ortho_j"] = j
1116
+ return U
1117
+ BU = mm_B(self.B, U)
1118
+ UBU = mm(U.mT, BU)
1119
+ U_norm = torch.norm(U)
1120
+ BU_norm = torch.norm(BU)
1121
+ R = UBU - torch.eye(UBU.shape[-1], device=UBU.device, dtype=UBU.dtype)
1122
+ R_norm = torch.norm(R)
1123
+ # https://github.com/pytorch/pytorch/issues/33810 workaround:
1124
+ rerr = float(R_norm) * float(BU_norm * U_norm) ** -1
1125
+ vkey = f"ortho_UBUmI_rerr[{i}, {j}]"
1126
+ self.fvars[vkey] = rerr
1127
+ if rerr < tau_ortho:
1128
+ break
1129
+ VBU = mm(V.mT, BU)
1130
+ VBU_norm = torch.norm(VBU)
1131
+ U_norm = torch.norm(U)
1132
+ rerr = float(VBU_norm) * float(BV_norm * U_norm) ** -1
1133
+ vkey = f"ortho_VBU_rerr[{i}]"
1134
+ self.fvars[vkey] = rerr
1135
+ if rerr < tau_ortho:
1136
+ break
1137
+ if m < U.shape[-1] + V.shape[-1]:
1138
+ # TorchScript needs the class var to be assigned to a local to
1139
+ # do optional type refinement
1140
+ B = self.B
1141
+ assert B is not None
1142
+ raise ValueError(
1143
+ "Overdetermined shape of U:"
1144
+ f" #B-cols(={B.shape[-1]}) >= #U-cols(={U.shape[-1]}) + #V-cols(={V.shape[-1]}) must hold"
1145
+ )
1146
+ self.ivars["ortho_i"] = i
1147
+ self.ivars["ortho_j"] = j
1148
+ return U
1149
+
1150
+
1151
+ # Calling tracker is separated from LOBPCG definitions because
1152
+ # TorchScript does not support user-defined callback arguments:
1153
+ LOBPCG_call_tracker_orig = LOBPCG.call_tracker
1154
+
1155
+
1156
+ def LOBPCG_call_tracker(self):
1157
+ self.tracker(self)
lib/python3.12/site-packages/torch/_lowrank.py ADDED
@@ -0,0 +1,294 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Implement various linear algebra algorithms for low rank matrices."""
2
+
3
+ __all__ = ["svd_lowrank", "pca_lowrank"]
4
+
5
+ from typing import Optional
6
+
7
+ import torch
8
+ from torch import _linalg_utils as _utils, Tensor
9
+ from torch.overrides import handle_torch_function, has_torch_function
10
+
11
+
12
+ def get_approximate_basis(
13
+ A: Tensor,
14
+ q: int,
15
+ niter: Optional[int] = 2,
16
+ M: Optional[Tensor] = None,
17
+ ) -> Tensor:
18
+ """Return tensor :math:`Q` with :math:`q` orthonormal columns such
19
+ that :math:`Q Q^H A` approximates :math:`A`. If :math:`M` is
20
+ specified, then :math:`Q` is such that :math:`Q Q^H (A - M)`
21
+ approximates :math:`A - M`. without instantiating any tensors
22
+ of the size of :math:`A` or :math:`M`.
23
+
24
+ .. note:: The implementation is based on the Algorithm 4.4 from
25
+ Halko et al., 2009.
26
+
27
+ .. note:: For an adequate approximation of a k-rank matrix
28
+ :math:`A`, where k is not known in advance but could be
29
+ estimated, the number of :math:`Q` columns, q, can be
30
+ chosen according to the following criteria: in general,
31
+ :math:`k <= q <= min(2*k, m, n)`. For large low-rank
32
+ matrices, take :math:`q = k + 5..10`. If k is
33
+ relatively small compared to :math:`min(m, n)`, choosing
34
+ :math:`q = k + 0..2` may be sufficient.
35
+
36
+ .. note:: To obtain repeatable results, reset the seed for the
37
+ pseudorandom number generator
38
+
39
+ Args::
40
+ A (Tensor): the input tensor of size :math:`(*, m, n)`
41
+
42
+ q (int): the dimension of subspace spanned by :math:`Q`
43
+ columns.
44
+
45
+ niter (int, optional): the number of subspace iterations to
46
+ conduct; ``niter`` must be a
47
+ nonnegative integer. In most cases, the
48
+ default value 2 is more than enough.
49
+
50
+ M (Tensor, optional): the input tensor's mean of size
51
+ :math:`(*, m, n)`.
52
+
53
+ References::
54
+ - Nathan Halko, Per-Gunnar Martinsson, and Joel Tropp, Finding
55
+ structure with randomness: probabilistic algorithms for
56
+ constructing approximate matrix decompositions,
57
+ arXiv:0909.4061 [math.NA; math.PR], 2009 (available at
58
+ `arXiv <http://arxiv.org/abs/0909.4061>`_).
59
+ """
60
+
61
+ niter = 2 if niter is None else niter
62
+ dtype = _utils.get_floating_dtype(A) if not A.is_complex() else A.dtype
63
+ matmul = _utils.matmul
64
+
65
+ R = torch.randn(A.shape[-1], q, dtype=dtype, device=A.device)
66
+
67
+ # The following code could be made faster using torch.geqrf + torch.ormqr
68
+ # but geqrf is not differentiable
69
+
70
+ X = matmul(A, R)
71
+ if M is not None:
72
+ X = X - matmul(M, R)
73
+ Q = torch.linalg.qr(X).Q
74
+ for _ in range(niter):
75
+ X = matmul(A.mH, Q)
76
+ if M is not None:
77
+ X = X - matmul(M.mH, Q)
78
+ Q = torch.linalg.qr(X).Q
79
+ X = matmul(A, Q)
80
+ if M is not None:
81
+ X = X - matmul(M, Q)
82
+ Q = torch.linalg.qr(X).Q
83
+ return Q
84
+
85
+
86
+ def svd_lowrank(
87
+ A: Tensor,
88
+ q: Optional[int] = 6,
89
+ niter: Optional[int] = 2,
90
+ M: Optional[Tensor] = None,
91
+ ) -> tuple[Tensor, Tensor, Tensor]:
92
+ r"""Return the singular value decomposition ``(U, S, V)`` of a matrix,
93
+ batches of matrices, or a sparse matrix :math:`A` such that
94
+ :math:`A \approx U \operatorname{diag}(S) V^{\text{H}}`. In case :math:`M` is given, then
95
+ SVD is computed for the matrix :math:`A - M`.
96
+
97
+ .. note:: The implementation is based on the Algorithm 5.1 from
98
+ Halko et al., 2009.
99
+
100
+ .. note:: For an adequate approximation of a k-rank matrix
101
+ :math:`A`, where k is not known in advance but could be
102
+ estimated, the number of :math:`Q` columns, q, can be
103
+ chosen according to the following criteria: in general,
104
+ :math:`k <= q <= min(2*k, m, n)`. For large low-rank
105
+ matrices, take :math:`q = k + 5..10`. If k is
106
+ relatively small compared to :math:`min(m, n)`, choosing
107
+ :math:`q = k + 0..2` may be sufficient.
108
+
109
+ .. note:: This is a randomized method. To obtain repeatable results,
110
+ set the seed for the pseudorandom number generator
111
+
112
+ .. note:: In general, use the full-rank SVD implementation
113
+ :func:`torch.linalg.svd` for dense matrices due to its 10x
114
+ higher performance characteristics. The low-rank SVD
115
+ will be useful for huge sparse matrices that
116
+ :func:`torch.linalg.svd` cannot handle.
117
+
118
+ Args::
119
+ A (Tensor): the input tensor of size :math:`(*, m, n)`
120
+
121
+ q (int, optional): a slightly overestimated rank of A.
122
+
123
+ niter (int, optional): the number of subspace iterations to
124
+ conduct; niter must be a nonnegative
125
+ integer, and defaults to 2
126
+
127
+ M (Tensor, optional): the input tensor's mean of size
128
+ :math:`(*, m, n)`, which will be broadcasted
129
+ to the size of A in this function.
130
+
131
+ References::
132
+ - Nathan Halko, Per-Gunnar Martinsson, and Joel Tropp, Finding
133
+ structure with randomness: probabilistic algorithms for
134
+ constructing approximate matrix decompositions,
135
+ arXiv:0909.4061 [math.NA; math.PR], 2009 (available at
136
+ `arXiv <https://arxiv.org/abs/0909.4061>`_).
137
+
138
+ """
139
+ if not torch.jit.is_scripting():
140
+ tensor_ops = (A, M)
141
+ if not set(map(type, tensor_ops)).issubset(
142
+ (torch.Tensor, type(None))
143
+ ) and has_torch_function(tensor_ops):
144
+ return handle_torch_function(
145
+ svd_lowrank, tensor_ops, A, q=q, niter=niter, M=M
146
+ )
147
+ return _svd_lowrank(A, q=q, niter=niter, M=M)
148
+
149
+
150
+ def _svd_lowrank(
151
+ A: Tensor,
152
+ q: Optional[int] = 6,
153
+ niter: Optional[int] = 2,
154
+ M: Optional[Tensor] = None,
155
+ ) -> tuple[Tensor, Tensor, Tensor]:
156
+ # Algorithm 5.1 in Halko et al., 2009
157
+
158
+ q = 6 if q is None else q
159
+ m, n = A.shape[-2:]
160
+ matmul = _utils.matmul
161
+ if M is not None:
162
+ M = M.broadcast_to(A.size())
163
+
164
+ # Assume that A is tall
165
+ if m < n:
166
+ A = A.mH
167
+ if M is not None:
168
+ M = M.mH
169
+
170
+ Q = get_approximate_basis(A, q, niter=niter, M=M)
171
+ B = matmul(Q.mH, A)
172
+ if M is not None:
173
+ B = B - matmul(Q.mH, M)
174
+ U, S, Vh = torch.linalg.svd(B, full_matrices=False)
175
+ V = Vh.mH
176
+ U = Q.matmul(U)
177
+
178
+ if m < n:
179
+ U, V = V, U
180
+
181
+ return U, S, V
182
+
183
+
184
+ def pca_lowrank(
185
+ A: Tensor,
186
+ q: Optional[int] = None,
187
+ center: bool = True,
188
+ niter: int = 2,
189
+ ) -> tuple[Tensor, Tensor, Tensor]:
190
+ r"""Performs linear Principal Component Analysis (PCA) on a low-rank
191
+ matrix, batches of such matrices, or sparse matrix.
192
+
193
+ This function returns a namedtuple ``(U, S, V)`` which is the
194
+ nearly optimal approximation of a singular value decomposition of
195
+ a centered matrix :math:`A` such that :math:`A \approx U \operatorname{diag}(S) V^{\text{H}}`
196
+
197
+ .. note:: The relation of ``(U, S, V)`` to PCA is as follows:
198
+
199
+ - :math:`A` is a data matrix with ``m`` samples and
200
+ ``n`` features
201
+
202
+ - the :math:`V` columns represent the principal directions
203
+
204
+ - :math:`S ** 2 / (m - 1)` contains the eigenvalues of
205
+ :math:`A^T A / (m - 1)` which is the covariance of
206
+ ``A`` when ``center=True`` is provided.
207
+
208
+ - ``matmul(A, V[:, :k])`` projects data to the first k
209
+ principal components
210
+
211
+ .. note:: Different from the standard SVD, the size of returned
212
+ matrices depend on the specified rank and q
213
+ values as follows:
214
+
215
+ - :math:`U` is m x q matrix
216
+
217
+ - :math:`S` is q-vector
218
+
219
+ - :math:`V` is n x q matrix
220
+
221
+ .. note:: To obtain repeatable results, reset the seed for the
222
+ pseudorandom number generator
223
+
224
+ Args:
225
+
226
+ A (Tensor): the input tensor of size :math:`(*, m, n)`
227
+
228
+ q (int, optional): a slightly overestimated rank of
229
+ :math:`A`. By default, ``q = min(6, m,
230
+ n)``.
231
+
232
+ center (bool, optional): if True, center the input tensor,
233
+ otherwise, assume that the input is
234
+ centered.
235
+
236
+ niter (int, optional): the number of subspace iterations to
237
+ conduct; niter must be a nonnegative
238
+ integer, and defaults to 2.
239
+
240
+ References::
241
+
242
+ - Nathan Halko, Per-Gunnar Martinsson, and Joel Tropp, Finding
243
+ structure with randomness: probabilistic algorithms for
244
+ constructing approximate matrix decompositions,
245
+ arXiv:0909.4061 [math.NA; math.PR], 2009 (available at
246
+ `arXiv <http://arxiv.org/abs/0909.4061>`_).
247
+
248
+ """
249
+
250
+ if not torch.jit.is_scripting():
251
+ if type(A) is not torch.Tensor and has_torch_function((A,)):
252
+ return handle_torch_function(
253
+ pca_lowrank, (A,), A, q=q, center=center, niter=niter
254
+ )
255
+
256
+ (m, n) = A.shape[-2:]
257
+
258
+ if q is None:
259
+ q = min(6, m, n)
260
+ elif not (q >= 0 and q <= min(m, n)):
261
+ raise ValueError(
262
+ f"q(={q}) must be non-negative integer and not greater than min(m, n)={min(m, n)}"
263
+ )
264
+ if not (niter >= 0):
265
+ raise ValueError(f"niter(={niter}) must be non-negative integer")
266
+
267
+ dtype = _utils.get_floating_dtype(A)
268
+
269
+ if not center:
270
+ return _svd_lowrank(A, q, niter=niter, M=None)
271
+
272
+ if _utils.is_sparse(A):
273
+ if len(A.shape) != 2:
274
+ raise ValueError("pca_lowrank input is expected to be 2-dimensional tensor")
275
+ c = torch.sparse.sum(A, dim=(-2,)) / m
276
+ # reshape c
277
+ column_indices = c.indices()[0]
278
+ indices = torch.zeros(
279
+ 2,
280
+ len(column_indices),
281
+ dtype=column_indices.dtype,
282
+ device=column_indices.device,
283
+ )
284
+ indices[0] = column_indices
285
+ C_t = torch.sparse_coo_tensor(
286
+ indices, c.values(), (n, 1), dtype=dtype, device=A.device
287
+ )
288
+
289
+ ones_m1_t = torch.ones(A.shape[:-2] + (1, m), dtype=dtype, device=A.device)
290
+ M = torch.sparse.mm(C_t, ones_m1_t).mT
291
+ return _svd_lowrank(A, q, niter=niter, M=M)
292
+ else:
293
+ C = A.mean(dim=(-2,), keepdim=True)
294
+ return _svd_lowrank(A - C, q, niter=niter, M=None)
lib/python3.12/site-packages/torch/_meta_registrations.py ADDED
The diff for this file is too large to render. See raw diff
 
lib/python3.12/site-packages/torch/_size_docs.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Adds docstrings to torch.Size functions"""
2
+
3
+ import torch._C
4
+ from torch._C import _add_docstr as add_docstr
5
+
6
+
7
+ def add_docstr_all(method: str, docstr: str) -> None:
8
+ add_docstr(getattr(torch._C.Size, method), docstr)
9
+
10
+
11
+ add_docstr_all(
12
+ "numel",
13
+ """
14
+ numel() -> int
15
+
16
+ Returns the number of elements a :class:`torch.Tensor` with the given size would contain.
17
+
18
+ More formally, for a tensor ``x = tensor.ones(10, 10)`` with size ``s = torch.Size([10, 10])``,
19
+ ``x.numel() == x.size().numel() == s.numel() == 100`` holds true.
20
+
21
+ Example::
22
+
23
+ >>> x=torch.ones(10, 10)
24
+ >>> s=x.size()
25
+ >>> s
26
+ torch.Size([10, 10])
27
+ >>> s.numel()
28
+ 100
29
+ >>> x.numel() == s.numel()
30
+ True
31
+
32
+
33
+ .. warning::
34
+
35
+ This function does not return the number of dimensions described by :class:`torch.Size`, but instead the number
36
+ of elements a :class:`torch.Tensor` with that size would contain.
37
+
38
+ """,
39
+ )
lib/python3.12/site-packages/torch/_storage_docs.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # mypy: allow-untyped-defs
2
+ """Adds docstrings to Storage functions"""
3
+
4
+ import torch._C
5
+ from torch._C import _add_docstr as add_docstr
6
+
7
+
8
+ storage_classes = ["StorageBase"]
9
+
10
+
11
+ def add_docstr_all(method, docstr):
12
+ for cls_name in storage_classes:
13
+ cls = getattr(torch._C, cls_name)
14
+ try:
15
+ add_docstr(getattr(cls, method), docstr)
16
+ except AttributeError:
17
+ pass
18
+
19
+
20
+ add_docstr_all(
21
+ "from_file",
22
+ """
23
+ from_file(filename, shared=False, nbytes=0) -> Storage
24
+
25
+ Creates a CPU storage backed by a memory-mapped file.
26
+
27
+ If ``shared`` is ``True``, then memory is shared between all processes.
28
+ All changes are written to the file. If ``shared`` is ``False``, then the changes on
29
+ the storage do not affect the file.
30
+
31
+ ``nbytes`` is the number of bytes of storage. If ``shared`` is ``False``,
32
+ then the file must contain at least ``nbytes`` bytes. If ``shared`` is
33
+ ``True`` the file will be created if needed. (Note that for ``UntypedStorage``
34
+ this argument differs from that of ``TypedStorage.from_file``)
35
+
36
+ Args:
37
+ filename (str): file name to map
38
+ shared (bool): whether to share memory (whether ``MAP_SHARED`` or ``MAP_PRIVATE`` is passed to the
39
+ underlying `mmap(2) call <https://man7.org/linux/man-pages/man2/mmap.2.html>`_)
40
+ nbytes (int): number of bytes of storage
41
+ """,
42
+ )
lib/python3.12/site-packages/torch/_streambase.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing_extensions import deprecated
2
+
3
+ import torch
4
+
5
+
6
+ # Preserved only for BC reasons
7
+ @deprecated(
8
+ "`torch._streambase._StreamBase` is deprecated. Please use `torch.Stream` instead.",
9
+ category=FutureWarning,
10
+ )
11
+ class _StreamBase(torch.Stream):
12
+ pass
13
+
14
+
15
+ @deprecated(
16
+ "`torch._streambase._EventBase` is deprecated. Please use `torch.Event` instead.",
17
+ category=FutureWarning,
18
+ )
19
+ class _EventBase(torch.Event):
20
+ pass
lib/python3.12/site-packages/torch/_tensor.py ADDED
@@ -0,0 +1,1837 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # mypy: allow-untyped-defs
2
+ import copyreg
3
+ import enum
4
+ import functools
5
+ import warnings
6
+ from collections import OrderedDict
7
+ from copy import deepcopy
8
+ from numbers import Number
9
+ from typing import Any, Callable, cast, Optional, TypeVar, Union
10
+ from typing_extensions import Concatenate, ParamSpec
11
+
12
+ import torch
13
+ import torch._C as _C
14
+ from torch._namedtensor_internals import (
15
+ check_serializing_named_tensor,
16
+ is_ellipsis,
17
+ resolve_ellipsis,
18
+ single_ellipsis_index,
19
+ unzip_namedshape,
20
+ update_names,
21
+ )
22
+ from torch.overrides import (
23
+ get_default_nowrap_functions,
24
+ handle_torch_function,
25
+ has_torch_function,
26
+ has_torch_function_unary,
27
+ has_torch_function_variadic,
28
+ )
29
+
30
+
31
+ _P = ParamSpec("_P")
32
+ _TensorLike = TypeVar("_TensorLike", bound=_C.TensorBase)
33
+
34
+
35
+ def _handle_torch_function_and_wrap_type_error_to_not_implemented(
36
+ f: Callable[Concatenate[_TensorLike, _P], "Tensor"],
37
+ ) -> Callable[Concatenate[_TensorLike, _P], "Tensor"]:
38
+ @functools.wraps(f)
39
+ def wrapped(self: _TensorLike, *args: _P.args, **kwargs: _P.kwargs) -> "Tensor":
40
+ try:
41
+ # See https://github.com/pytorch/pytorch/issues/75462
42
+ sargs = self, *args
43
+ if has_torch_function(sargs):
44
+ return handle_torch_function(wrapped, sargs, *sargs, **kwargs)
45
+ return f(self, *args, **kwargs)
46
+ except TypeError:
47
+ return NotImplemented
48
+
49
+ return wrapped
50
+
51
+
52
+ # Should not be used, this is kept only for BC of loading old serialized Tensor subclasses
53
+ def _rebuild_from_type(func, type, args, dict):
54
+ if type is Tensor:
55
+ return func(*args)
56
+
57
+ ret = func(*args).as_subclass(type)
58
+ ret.__dict__ = dict
59
+ return ret
60
+
61
+
62
+ def _rebuild_from_type_v2(func, new_type, args, state):
63
+ ret = func(*args)
64
+ if type(ret) is not new_type:
65
+ ret = ret.as_subclass(new_type)
66
+ # Tensor does define __setstate__ even though it doesn't define
67
+ # __getstate__. So only use __setstate__ if it is NOT the one defined
68
+ # on Tensor
69
+ if (
70
+ getattr(ret.__class__, "__setstate__", Tensor.__setstate__)
71
+ is not Tensor.__setstate__
72
+ ):
73
+ ret.__setstate__(state)
74
+ else:
75
+ ret = torch._utils._set_obj_state(ret, state)
76
+ return ret
77
+
78
+
79
+ def _dtype_to_typestr(dtype):
80
+ # CUDA devices are little-endian and tensors are stored in native byte
81
+ # order. 1-byte entries are endian-agnostic.
82
+ return {
83
+ torch.complex64: "<c8",
84
+ torch.complex128: "<c16",
85
+ torch.bfloat16: "<V2", # Same as ml_dtypes.bfloat16.dtype.str.
86
+ torch.float16: "<f2",
87
+ torch.float32: "<f4",
88
+ torch.float64: "<f8",
89
+ torch.uint8: "|u1",
90
+ torch.int8: "|i1",
91
+ torch.uint16: "<u2",
92
+ torch.int16: "<i2",
93
+ torch.uint32: "<u4",
94
+ torch.int32: "<i4",
95
+ torch.uint64: "<u8",
96
+ torch.int64: "<i8",
97
+ torch.bool: "|b1",
98
+ }[dtype]
99
+
100
+
101
+ # NB: If you subclass Tensor, and want to share the subclassed class
102
+ # across processes, you must also update torch/multiprocessing/reductions.py
103
+ # to define a ForkingPickler serialization mode for the class.
104
+ #
105
+ # NB: If you add a new method to Tensor, you must update
106
+ # torch/_C/__init__.pyi.in to add a type annotation for your method;
107
+ # otherwise, it will not show up in autocomplete.
108
+ class Tensor(torch._C.TensorBase):
109
+ _is_param: bool
110
+
111
+ def _clear_non_serializable_cached_data(self):
112
+ r"""Clears any data cached in the tensor's ``__dict__`` that would prevent the tensor
113
+ from being serialized.
114
+
115
+ For example, subclasses with custom dispatched sizes / strides cache this info in
116
+ non-serializable PyCapsules within the ``__dict__``, and this must be cleared out for
117
+ serialization to function.
118
+
119
+ Any subclass that overrides this MUST call ``super()._clear_non_serializable_cached_data().``
120
+ Additional data cleared within the override must be able to be re-cached transparently
121
+ to avoid breaking subclass functionality.
122
+ """
123
+ if has_torch_function_unary(self):
124
+ return handle_torch_function(
125
+ Tensor._clear_non_serializable_cached_data, (self,), self
126
+ )
127
+ # NB: Wrapper subclasses that implement custom-dispatched sizes / strides cache
128
+ # this info via non-serializable PyCapsules.
129
+ CACHED_SIZES_STRIDES_KEYS = [
130
+ "_sym_sizes_capsule",
131
+ "_sym_sizes_capsule_len",
132
+ "_sym_strides_capsule",
133
+ "_sym_strides_capsule_len",
134
+ ]
135
+ for key in CACHED_SIZES_STRIDES_KEYS:
136
+ self.__dict__.pop(key, None)
137
+
138
+ def __deepcopy__(self, memo):
139
+ if has_torch_function_unary(self):
140
+ return handle_torch_function(Tensor.__deepcopy__, (self,), self, memo)
141
+ if not self.is_leaf:
142
+ raise RuntimeError(
143
+ "Only Tensors created explicitly by the user "
144
+ "(graph leaves) support the deepcopy protocol at the moment. "
145
+ "If you were attempting to deepcopy a module, this may be because "
146
+ "of a torch.nn.utils.weight_norm usage, "
147
+ "see https://github.com/pytorch/pytorch/pull/103001"
148
+ )
149
+ if id(self) in memo:
150
+ return memo[id(self)]
151
+ with torch.no_grad():
152
+ # TODO: skipping storage copy is wrong for meta, as meta
153
+ # does accurate alias tracking; however, the code below
154
+ # doesn't work because of
155
+ # https://github.com/pytorch/pytorch/issues/47442
156
+ # Update the test in test_serialization if you remove 'meta' from here
157
+ if (
158
+ self.is_sparse
159
+ or self.device.type
160
+ in ["lazy", "xla", "mtia", "mps", "maia", "meta", "ipu"]
161
+ or (
162
+ not torch._C._has_storage(self)
163
+ and self.device.type == torch._C._get_privateuse1_backend_name()
164
+ )
165
+ or (type(self) is not Tensor and self.data_ptr() == 0)
166
+ ):
167
+ new_tensor = self.clone()
168
+ if type(new_tensor) is not type(self):
169
+ raise RuntimeError(
170
+ "The default implementation of __deepcopy__() for wrapper subclasses "
171
+ "only works for subclass types that implement clone() and for which "
172
+ "cloning returns another instance of the same subclass. You should either "
173
+ "properly implement clone() for your subclass or override __deepcopy__() "
174
+ "if it is intended behavior for clone() to return an instance of a "
175
+ "different type."
176
+ )
177
+ else:
178
+ new_storage = self._typed_storage()._deepcopy(memo)
179
+ if self.is_quantized:
180
+ # quantizer_params can be different type based on torch attribute
181
+ quantizer_params: Union[
182
+ tuple[torch.qscheme, float, int],
183
+ tuple[torch.qscheme, Tensor, Tensor, int],
184
+ ]
185
+ if self.qscheme() == torch.per_tensor_affine:
186
+ quantizer_params = (
187
+ self.qscheme(),
188
+ self.q_scale(),
189
+ self.q_zero_point(),
190
+ )
191
+ elif self.qscheme() in (
192
+ torch.per_channel_affine,
193
+ torch.per_channel_affine_float_qparams,
194
+ ):
195
+ quantizer_params = (
196
+ self.qscheme(),
197
+ self.q_per_channel_scales(),
198
+ self.q_per_channel_zero_points(),
199
+ self.q_per_channel_axis(),
200
+ )
201
+ else:
202
+ raise RuntimeError(
203
+ f"Unsupported qscheme {self.qscheme()} in deepcopy"
204
+ )
205
+ # TODO: Once we decide to break serialization FC, no longer
206
+ # need to wrap with TypedStorage
207
+ new_tensor = torch._utils._rebuild_qtensor(
208
+ torch.storage.TypedStorage(
209
+ wrap_storage=new_storage._untyped_storage,
210
+ dtype=self.dtype,
211
+ _internal=True,
212
+ ),
213
+ self.storage_offset(),
214
+ self.size(),
215
+ self.stride(),
216
+ quantizer_params,
217
+ self.requires_grad,
218
+ self._backward_hooks,
219
+ )
220
+ if type(new_tensor) is not type(self):
221
+ raise RuntimeError(
222
+ "The default implementation of __deepcopy__() for quantized tensors "
223
+ "expects the tensor returned by torch._utils._rebuild_qtensor() to "
224
+ "match the type of the instance being copied. If you encounter this, "
225
+ "please open an issue on PyTorch's GitHub."
226
+ )
227
+ else:
228
+ new_tensor = self.new_empty([])
229
+ if type(new_tensor) is not type(self):
230
+ raise RuntimeError(
231
+ "The default implementation of __deepcopy__() for non-wrapper subclasses "
232
+ "only works for subclass types that implement new_empty() and for which "
233
+ "that function returns another instance of the same subclass. You should "
234
+ "either properly implement new_empty() for your subclass or override "
235
+ "__deepcopy__() if it is intended behavior for new_empty() to return "
236
+ "an instance of a different type."
237
+ )
238
+ new_tensor.set_(
239
+ new_storage, self.storage_offset(), self.size(), self.stride()
240
+ )
241
+ if self.is_conj():
242
+ new_tensor = new_tensor.conj_physical()
243
+ if self.is_neg():
244
+ new_tensor = new_tensor.neg()
245
+ if self.requires_grad:
246
+ new_tensor.requires_grad_()
247
+ if self.grad is not None:
248
+ new_tensor.grad = self.grad.__deepcopy__(memo)
249
+
250
+ if type(self) is not Tensor:
251
+ if type(new_tensor) is not type(self):
252
+ raise RuntimeError(
253
+ "Type of deepcopy result does not match the type of the source tensor. "
254
+ "If you encounter this, please open an issue on PyTorch's GitHub."
255
+ )
256
+
257
+ # Plain Tensors don't have slots
258
+ slots_to_save = copyreg._slotnames(self.__class__) # type: ignore[attr-defined]
259
+ for slot in slots_to_save:
260
+ if hasattr(self, slot):
261
+ setattr(new_tensor, slot, deepcopy(getattr(self, slot), memo))
262
+
263
+ # don't try to deepcopy non-serializable cached data
264
+ self._clear_non_serializable_cached_data()
265
+ new_tensor.__dict__ = deepcopy(self.__dict__, memo)
266
+
267
+ memo[id(self)] = new_tensor
268
+ return new_tensor
269
+
270
+ def __reduce_ex__(self, proto):
271
+ materialize_fake_tensors = (
272
+ torch.serialization._serialization_tls.materialize_fake_tensors
273
+ )
274
+ state = torch._utils._get_obj_state(self)
275
+ # Ignore all state when using FakeTensor with skip_data(materialize_fake_tensors) because FakeTensor has
276
+ # some state that cannot be pickled
277
+ if (
278
+ # TODO: remove hasattr, it's a hack to support versions of torch that
279
+ # don't have _subclasses
280
+ hasattr(torch, "_subclasses")
281
+ and type(self) is torch._subclasses.fake_tensor.FakeTensor
282
+ and materialize_fake_tensors
283
+ ) or (type(self) is Tensor and not state):
284
+ # Fast path for regular tensor without Python state.
285
+ return self._reduce_ex_internal(proto)
286
+ if has_torch_function_unary(self):
287
+ return handle_torch_function(Tensor.__reduce_ex__, (self,), self, proto)
288
+ func, args = self._reduce_ex_internal(proto)
289
+ # sizes / strides cache needs to be cleared here because it'll just be re-cached
290
+ # if cleared earlier. Note that state references the -actual- tensor dict.
291
+ self._clear_non_serializable_cached_data()
292
+ return (_rebuild_from_type_v2, (func, type(self), args, state))
293
+
294
+ def storage(self):
295
+ r"""
296
+ storage() -> torch.TypedStorage
297
+
298
+ Returns the underlying :class:`TypedStorage`.
299
+
300
+ .. warning::
301
+
302
+ :class:`TypedStorage` is deprecated. It will be removed in the future, and
303
+ :class:`UntypedStorage` will be the only storage class. To access the
304
+ :class:`UntypedStorage` directly, use :attr:`Tensor.untyped_storage()`.
305
+ """
306
+ if has_torch_function_unary(self):
307
+ return handle_torch_function(Tensor.storage, (self,), self)
308
+
309
+ torch.storage._warn_typed_storage_removal(stacklevel=2)
310
+ return self._typed_storage()
311
+
312
+ # For internal use only, to avoid raising deprecation warning
313
+ def _typed_storage(self):
314
+ untyped_storage = self.untyped_storage()
315
+ return torch.TypedStorage(
316
+ wrap_storage=untyped_storage, dtype=self.dtype, _internal=True
317
+ )
318
+
319
+ def _reduce_ex_internal(self, proto):
320
+ check_serializing_named_tensor(self)
321
+
322
+ from torch.utils.hooks import warn_if_has_hooks
323
+
324
+ # See Note [Don't serialize hooks]
325
+ warn_if_has_hooks(self)
326
+ backward_hooks: dict[Any, Any] = OrderedDict()
327
+
328
+ skip_data = torch.serialization._serialization_tls.skip_data
329
+ materialize_fake_tensors = (
330
+ torch.serialization._serialization_tls.materialize_fake_tensors
331
+ )
332
+
333
+ if self.device.type in ["xla", "maia", "mtia"] or (
334
+ not torch._C._has_storage(self)
335
+ and self.device.type == torch._C._get_privateuse1_backend_name()
336
+ ):
337
+ if skip_data:
338
+ raise RuntimeError(
339
+ "Cannot serialize tensors on backends with no storage under skip_data context manager"
340
+ )
341
+ cpu_tensor = self.cpu()
342
+ return (
343
+ torch._utils._rebuild_device_tensor_from_cpu_tensor,
344
+ (cpu_tensor, self.dtype, str(self.device), self.requires_grad),
345
+ )
346
+ if self.device.type == "meta":
347
+ # NB: This implementation BREAKS storage sharing. Current
348
+ # hypothesis is that no one cares for meta tensors.
349
+ if skip_data:
350
+ warnings.warn(
351
+ "Serializing tensors on the meta device under skip_data context manager is a no-op"
352
+ )
353
+ arg_meta = (
354
+ self.dtype,
355
+ tuple(self.size()),
356
+ self.stride(),
357
+ self.requires_grad,
358
+ )
359
+ return (torch._utils._rebuild_meta_tensor_no_storage, arg_meta)
360
+ if self.is_quantized:
361
+ if skip_data:
362
+ raise RuntimeError(
363
+ "Cannot serialize qtensor under skip_data context manager, file an issue if you need this feature"
364
+ )
365
+ # quantizer_params can be different type based on torch attribute
366
+ quantizer_params: Union[
367
+ tuple[torch.qscheme, float, int], tuple[Any, Tensor, Tensor, int]
368
+ ]
369
+ if self.qscheme() == torch.per_tensor_affine:
370
+ quantizer_params = (
371
+ torch.per_tensor_affine,
372
+ self.q_scale(),
373
+ self.q_zero_point(),
374
+ )
375
+ elif self.qscheme() in (
376
+ torch.per_channel_affine,
377
+ torch.per_channel_affine_float_qparams,
378
+ ):
379
+ # convert scales and zero points to tuple to avoid recursive calls
380
+ # when/if we get multi-axis quantized tensors in the future, the shape
381
+ # is recoverable from the main tensor shape
382
+ quantizer_params = (
383
+ torch.per_channel_affine,
384
+ self.q_per_channel_scales(),
385
+ self.q_per_channel_zero_points(),
386
+ self.q_per_channel_axis(),
387
+ )
388
+ else:
389
+ raise RuntimeError(
390
+ f"Serialization is not supported for tensors of type {self.qscheme()}"
391
+ )
392
+ # TODO: Once we decide to break serialization FC, no longer
393
+ # need to wrap with TypedStorage
394
+ args_qtensor = (
395
+ torch.storage.TypedStorage(
396
+ wrap_storage=self._typed_storage()._untyped_storage,
397
+ dtype=self.dtype,
398
+ _internal=True,
399
+ ),
400
+ self.storage_offset(),
401
+ tuple(self.size()),
402
+ self.stride(),
403
+ quantizer_params,
404
+ self.requires_grad,
405
+ backward_hooks,
406
+ )
407
+ return (torch._utils._rebuild_qtensor, args_qtensor)
408
+ elif self.is_sparse:
409
+ if self.layout == torch.sparse_coo:
410
+ args_sparse = (
411
+ self.layout,
412
+ (self._indices(), self._values(), self.size(), self.is_coalesced()),
413
+ )
414
+ else:
415
+ raise NotImplementedError(
416
+ f"sparse tensor __reduce_ex__ for layout `{self.layout}`"
417
+ )
418
+ return (torch._utils._rebuild_sparse_tensor, args_sparse)
419
+ elif self.layout in {
420
+ torch.sparse_csr,
421
+ torch.sparse_csc,
422
+ torch.sparse_bsr,
423
+ torch.sparse_bsc,
424
+ }:
425
+ if self.layout in {torch.sparse_csr, torch.sparse_bsr}:
426
+ compressed_indices, plain_indices = (
427
+ self.crow_indices(),
428
+ self.col_indices(),
429
+ )
430
+ else:
431
+ compressed_indices, plain_indices = (
432
+ self.ccol_indices(),
433
+ self.row_indices(),
434
+ )
435
+ args_sparse_compressed = (
436
+ self.layout,
437
+ (
438
+ compressed_indices,
439
+ plain_indices,
440
+ self.values(),
441
+ self.size(),
442
+ ),
443
+ )
444
+ return (torch._utils._rebuild_sparse_tensor, args_sparse_compressed)
445
+ elif self.is_nested:
446
+ if skip_data:
447
+ raise RuntimeError(
448
+ "Cannot serialize nested tensor under skip_data context manager, file an issue if you need this feature"
449
+ )
450
+ args_nested = (
451
+ # NB: values() currently returns the storage as a buffer in an unsafe way.
452
+ # Ideally, we'd use a private API for this instead. TODO: Switch to this if
453
+ # we ever get around to adding it.
454
+ self.values(),
455
+ self._nested_tensor_size(),
456
+ self._nested_tensor_strides(),
457
+ self._nested_tensor_storage_offsets(),
458
+ )
459
+ return (torch._utils._rebuild_nested_tensor, args_nested)
460
+ elif (
461
+ type(self) is not torch.Tensor
462
+ and type(self).__torch_dispatch__ is not torch.Tensor.__torch_dispatch__
463
+ and (
464
+ isinstance(self, torch._subclasses.functional_tensor.FunctionalTensor)
465
+ or (
466
+ not isinstance(self, torch._subclasses.fake_tensor.FakeTensor)
467
+ and self.data_ptr() == 0
468
+ )
469
+ )
470
+ ):
471
+ arg_wrapper_subclass = (
472
+ type(self),
473
+ self.dtype,
474
+ tuple(self.size()),
475
+ self.stride(),
476
+ self.storage_offset(),
477
+ self.layout,
478
+ self.device,
479
+ self.requires_grad,
480
+ )
481
+ return (torch._utils._rebuild_wrapper_subclass, arg_wrapper_subclass)
482
+ elif (
483
+ type(self) is not torch.Tensor
484
+ and type(self).__torch_dispatch__ is not torch.Tensor.__torch_dispatch__
485
+ and (
486
+ isinstance(self, torch._subclasses.fake_tensor.FakeTensor)
487
+ and not (skip_data and materialize_fake_tensors)
488
+ )
489
+ ):
490
+ arg_wrapper_subclass = (
491
+ type(self),
492
+ self.dtype,
493
+ tuple(self.size()),
494
+ self.stride(),
495
+ self.storage_offset(),
496
+ self.layout,
497
+ self.device,
498
+ self.requires_grad,
499
+ )
500
+ return (torch._utils._rebuild_wrapper_subclass, arg_wrapper_subclass)
501
+ else:
502
+ v3_dtypes = torch.storage._new_dtypes()
503
+ if self.dtype in v3_dtypes:
504
+ rebuild_func = torch._utils._rebuild_tensor_v3
505
+ storage = self.untyped_storage()
506
+ else:
507
+ # TODO: Once we decide to break serialization FC, no longer
508
+ # need to wrap with TypedStorage
509
+ rebuild_func = torch._utils._rebuild_tensor_v2 # type: ignore[assignment]
510
+ storage = torch.storage.TypedStorage(
511
+ wrap_storage=self._typed_storage()._untyped_storage,
512
+ dtype=self.dtype,
513
+ _internal=True,
514
+ ) # type: ignore[assignment]
515
+
516
+ # TODO: remove hasattr, it's a hack to support versions of torch that
517
+ # don't have _subclasses
518
+ if (
519
+ hasattr(torch, "_subclasses")
520
+ and isinstance(self, torch._subclasses.fake_tensor.FakeTensor)
521
+ and skip_data
522
+ ):
523
+ storage._fake_device = self.device
524
+
525
+ args = (
526
+ storage,
527
+ self.storage_offset(),
528
+ tuple(self.size()),
529
+ self.stride(),
530
+ self.requires_grad,
531
+ backward_hooks,
532
+ ) # previously was self._backward_hooks
533
+
534
+ if isinstance(storage, torch.storage.UntypedStorage):
535
+ args = args + (self.dtype,) # type: ignore[assignment]
536
+
537
+ metadata = torch._utils.get_tensor_metadata(self)
538
+ if metadata:
539
+ args = args + (metadata,) # type: ignore[assignment]
540
+
541
+ return (rebuild_func, args)
542
+
543
+ def __setstate__(self, state):
544
+ if has_torch_function_unary(self):
545
+ return handle_torch_function(Tensor.__setstate__, (self,), self, state)
546
+ # Warning: this method is NOT called when you torch.load() a tensor;
547
+ # that is managed by _rebuild_tensor_v2
548
+ if not self.is_leaf:
549
+ raise RuntimeError("__setstate__ can be only called on leaf Tensors")
550
+ if len(state) == 4:
551
+ # legacy serialization of Tensor
552
+ self.set_(*state)
553
+ return
554
+ elif len(state) == 5:
555
+ # legacy serialization of Variable
556
+ self.data = state[0]
557
+ state = (state[3], state[4], state[2])
558
+ # The setting of _backward_hooks is expected to be a no-op.
559
+ # See Note [Don't serialize hooks]
560
+ self.requires_grad, _, self._backward_hooks = state
561
+
562
+ def __repr__(self, *, tensor_contents=None):
563
+ if has_torch_function_unary(self):
564
+ return handle_torch_function(
565
+ Tensor.__repr__, (self,), self, tensor_contents=tensor_contents
566
+ )
567
+ # All strings are unicode in Python 3.
568
+ return torch._tensor_str._str(self, tensor_contents=tensor_contents)
569
+
570
+ def backward(
571
+ self, gradient=None, retain_graph=None, create_graph=False, inputs=None
572
+ ):
573
+ r"""Computes the gradient of current tensor wrt graph leaves.
574
+
575
+ The graph is differentiated using the chain rule. If the tensor is
576
+ non-scalar (i.e. its data has more than one element) and requires
577
+ gradient, the function additionally requires specifying a ``gradient``.
578
+ It should be a tensor of matching type and shape, that represents
579
+ the gradient of the differentiated function w.r.t. ``self``.
580
+
581
+ This function accumulates gradients in the leaves - you might need to zero
582
+ ``.grad`` attributes or set them to ``None`` before calling it.
583
+ See :ref:`Default gradient layouts<default-grad-layouts>`
584
+ for details on the memory layout of accumulated gradients.
585
+
586
+ .. note::
587
+
588
+ If you run any forward ops, create ``gradient``, and/or call ``backward``
589
+ in a user-specified CUDA stream context, see
590
+ :ref:`Stream semantics of backward passes<bwd-cuda-stream-semantics>`.
591
+
592
+ .. note::
593
+
594
+ When ``inputs`` are provided and a given input is not a leaf,
595
+ the current implementation will call its grad_fn (though it is not strictly needed to get this gradients).
596
+ It is an implementation detail on which the user should not rely.
597
+ See https://github.com/pytorch/pytorch/pull/60521#issuecomment-867061780 for more details.
598
+
599
+ Args:
600
+ gradient (Tensor, optional): The gradient of the function
601
+ being differentiated w.r.t. ``self``.
602
+ This argument can be omitted if ``self`` is a scalar. Defaults to ``None``.
603
+ retain_graph (bool, optional): If ``False``, the graph used to compute the grads will be freed;
604
+ If ``True``, it will be retained. The default is ``None``, in which case the value is inferred from ``create_graph``
605
+ (i.e., the graph is retained only when higher-order derivative tracking is requested). Note that in nearly all cases
606
+ setting this option to True is not needed and often can be worked around in a much more efficient way.
607
+ create_graph (bool, optional): If ``True``, graph of the derivative will
608
+ be constructed, allowing to compute higher order derivative
609
+ products. Defaults to ``False``.
610
+ inputs (Sequence[Tensor], optional): Inputs w.r.t. which the gradient will be
611
+ accumulated into ``.grad``. All other tensors will be ignored. If not
612
+ provided, the gradient is accumulated into all the leaf Tensors that were
613
+ used to compute the :attr:`tensors`. Defaults to ``None``.
614
+ """
615
+ if has_torch_function_unary(self):
616
+ return handle_torch_function(
617
+ Tensor.backward,
618
+ (self,),
619
+ self,
620
+ gradient=gradient,
621
+ retain_graph=retain_graph,
622
+ create_graph=create_graph,
623
+ inputs=inputs,
624
+ )
625
+ torch.autograd.backward(
626
+ self, gradient, retain_graph, create_graph, inputs=inputs
627
+ )
628
+
629
+ def register_hook(self, hook):
630
+ r"""Registers a backward hook.
631
+
632
+ The hook will be called every time a gradient with respect to the
633
+ Tensor is computed. The hook should have the following signature::
634
+
635
+ hook(grad) -> Tensor or None
636
+
637
+
638
+ The hook should not modify its argument, but it can optionally return
639
+ a new gradient which will be used in place of :attr:`grad`.
640
+
641
+ This function returns a handle with a method ``handle.remove()``
642
+ that removes the hook from the module.
643
+
644
+ .. note::
645
+ See :ref:`backward-hooks-execution` for more information on how when this hook
646
+ is executed, and how its execution is ordered relative to other hooks.
647
+
648
+ Example::
649
+
650
+ >>> v = torch.tensor([0., 0., 0.], requires_grad=True)
651
+ >>> h = v.register_hook(lambda grad: grad * 2) # double the gradient
652
+ >>> v.backward(torch.tensor([1., 2., 3.]))
653
+ >>> v.grad
654
+
655
+ 2
656
+ 4
657
+ 6
658
+ [torch.FloatTensor of size (3,)]
659
+
660
+ >>> h.remove() # removes the hook
661
+ """
662
+ if has_torch_function_unary(self):
663
+ return handle_torch_function(Tensor.register_hook, (self,), self, hook)
664
+ if not self.requires_grad:
665
+ raise RuntimeError(
666
+ "cannot register a hook on a tensor that doesn't require gradient"
667
+ )
668
+ if self._backward_hooks is None:
669
+ self._backward_hooks = OrderedDict()
670
+ if self.grad_fn is not None:
671
+ self.grad_fn._register_hook_dict(self)
672
+
673
+ from torch.utils.hooks import RemovableHandle
674
+
675
+ handle = RemovableHandle(self._backward_hooks)
676
+ self._backward_hooks[handle.id] = hook
677
+ return handle
678
+
679
+ def register_post_accumulate_grad_hook(self, hook):
680
+ r"""Registers a backward hook that runs after grad accumulation.
681
+
682
+ The hook will be called after all gradients for a tensor have been accumulated,
683
+ meaning that the .grad field has been updated on that tensor. The post
684
+ accumulate grad hook is ONLY applicable for leaf tensors (tensors without a
685
+ .grad_fn field). Registering this hook on a non-leaf tensor will error!
686
+
687
+ The hook should have the following signature::
688
+
689
+ hook(param: Tensor) -> None
690
+
691
+ Note that, unlike other autograd hooks, this hook operates on the tensor
692
+ that requires grad and not the grad itself. The hook can in-place modify
693
+ and access its Tensor argument, including its .grad field.
694
+
695
+ This function returns a handle with a method ``handle.remove()``
696
+ that removes the hook from the module.
697
+
698
+ .. note::
699
+ See :ref:`backward-hooks-execution` for more information on how when this hook
700
+ is executed, and how its execution is ordered relative to other hooks. Since
701
+ this hook runs during the backward pass, it will run in no_grad mode (unless
702
+ create_graph is True). You can use torch.enable_grad() to re-enable autograd
703
+ within the hook if you need it.
704
+
705
+ Example::
706
+
707
+ >>> v = torch.tensor([0., 0., 0.], requires_grad=True)
708
+ >>> lr = 0.01
709
+ >>> # simulate a simple SGD update
710
+ >>> h = v.register_post_accumulate_grad_hook(lambda p: p.add_(p.grad, alpha=-lr))
711
+ >>> v.backward(torch.tensor([1., 2., 3.]))
712
+ >>> v
713
+ tensor([-0.0100, -0.0200, -0.0300], requires_grad=True)
714
+
715
+ >>> h.remove() # removes the hook
716
+ """
717
+ if has_torch_function_unary(self):
718
+ return handle_torch_function(
719
+ Tensor.register_post_accumulate_grad_hook, (self,), self, hook
720
+ )
721
+ if not self.requires_grad:
722
+ raise RuntimeError(
723
+ "cannot register a hook on a tensor that doesn't require gradient"
724
+ )
725
+ if self.grad_fn is not None:
726
+ raise RuntimeError(
727
+ "post accumulate grad hooks cannot be registered on non-leaf tensors"
728
+ )
729
+ if self._post_accumulate_grad_hooks is None:
730
+ self._post_accumulate_grad_hooks: dict[Any, Any] = OrderedDict()
731
+
732
+ from torch.utils.hooks import RemovableHandle
733
+
734
+ handle = RemovableHandle(self._post_accumulate_grad_hooks)
735
+ self._post_accumulate_grad_hooks[handle.id] = hook
736
+ return handle
737
+
738
+ def reinforce(self, reward):
739
+ def trim(str):
740
+ return "\n".join([line.strip() for line in str.split("\n")])
741
+
742
+ raise RuntimeError(
743
+ trim(
744
+ r"""reinforce() was removed.
745
+ Use torch.distributions instead.
746
+ See https://pytorch.org/docs/main/distributions.html
747
+
748
+ Instead of:
749
+
750
+ probs = policy_network(state)
751
+ action = probs.multinomial()
752
+ next_state, reward = env.step(action)
753
+ action.reinforce(reward)
754
+ action.backward()
755
+
756
+ Use:
757
+
758
+ probs = policy_network(state)
759
+ # NOTE: categorical is equivalent to what used to be called multinomial
760
+ m = torch.distributions.Categorical(probs)
761
+ action = m.sample()
762
+ next_state, reward = env.step(action)
763
+ loss = -m.log_prob(action) * reward
764
+ loss.backward()
765
+ """
766
+ )
767
+ )
768
+
769
+ detach = _C._add_docstr(
770
+ _C.TensorBase.detach,
771
+ r"""
772
+ Returns a new Tensor, detached from the current graph.
773
+
774
+ The result will never require gradient.
775
+
776
+ This method also affects forward mode AD gradients and the result will never
777
+ have forward mode AD gradients.
778
+
779
+ .. note::
780
+
781
+ Returned Tensor shares the same storage with the original one.
782
+ In-place modifications on either of them will be seen, and may trigger
783
+ errors in correctness checks.
784
+ """,
785
+ )
786
+
787
+ detach_ = _C._add_docstr(
788
+ _C.TensorBase.detach_,
789
+ r"""
790
+ Detaches the Tensor from the graph that created it, making it a leaf.
791
+ Views cannot be detached in-place.
792
+
793
+ This method also affects forward mode AD gradients and the result will never
794
+ have forward mode AD gradients.
795
+ """,
796
+ )
797
+
798
+ def is_shared(self):
799
+ r"""Checks if tensor is in shared memory.
800
+
801
+ This is always ``True`` for CUDA tensors.
802
+ """
803
+ if has_torch_function_unary(self):
804
+ return handle_torch_function(Tensor.is_shared, (self,), self)
805
+ return self._typed_storage()._is_shared()
806
+
807
+ def share_memory_(self):
808
+ r"""Moves the underlying storage to shared memory.
809
+
810
+ This is a no-op if the underlying storage is already in shared memory
811
+ and for CUDA tensors. Tensors in shared memory cannot be resized.
812
+
813
+ See :meth:`torch.UntypedStorage.share_memory_` for more details.
814
+ """
815
+ if has_torch_function_unary(self):
816
+ return handle_torch_function(Tensor.share_memory_, (self,), self)
817
+ self._typed_storage()._share_memory_()
818
+ return self
819
+
820
+ def module_load(self, other, assign=False):
821
+ r"""Defines how to transform ``other`` when loading it into ``self`` in :meth:`~nn.Module.load_state_dict`.
822
+
823
+ Used when :func:`~torch.__future__.get_swap_module_params_on_conversion` is ``True``.
824
+
825
+ It is expected that ``self`` is a parameter or buffer in an ``nn.Module`` and ``other`` is the
826
+ value in the state dictionary with the corresponding key, this method defines
827
+ how ``other`` is remapped before being swapped with ``self`` via
828
+ :func:`~torch.utils.swap_tensors` in :meth:`~nn.Module.load_state_dict`.
829
+
830
+ .. note::
831
+ This method should always return a new object that is not ``self`` or ``other``.
832
+ For example, the default implementation returns ``self.copy_(other).detach()``
833
+ if ``assign`` is ``False`` or ``other.detach()`` if ``assign`` is ``True``.
834
+
835
+ Args:
836
+ other (Tensor): value in state dict with key corresponding to ``self``
837
+ assign (bool): the assign argument passed to :meth:`nn.Module.load_state_dict`
838
+
839
+ """
840
+ if has_torch_function_variadic(self, other):
841
+ return handle_torch_function(
842
+ Tensor.module_load, (self, other), self, other, assign=assign
843
+ )
844
+
845
+ if assign:
846
+ return other.detach()
847
+ else:
848
+ return self.copy_(other).detach()
849
+
850
+ def __reversed__(self):
851
+ r"""Reverses the tensor along dimension 0."""
852
+ if has_torch_function_unary(self):
853
+ return handle_torch_function(Tensor.__reversed__, (self,), self)
854
+ if self.dim() == 0:
855
+ return self
856
+ else:
857
+ return self.flip(0)
858
+
859
+ def norm(
860
+ self,
861
+ p: Optional[Union[float, str]] = "fro",
862
+ dim=None,
863
+ keepdim=False,
864
+ dtype=None,
865
+ ):
866
+ r"""See :func:`torch.norm`"""
867
+ if has_torch_function_unary(self):
868
+ return handle_torch_function(
869
+ Tensor.norm, (self,), self, p=p, dim=dim, keepdim=keepdim, dtype=dtype
870
+ )
871
+ return torch.norm(self, p, dim, keepdim, dtype=dtype)
872
+
873
+ def solve(self, other):
874
+ from torch._linalg_utils import solve
875
+
876
+ return solve(self, other)
877
+
878
+ def lstsq(self, other):
879
+ from torch._linalg_utils import lstsq
880
+
881
+ return lstsq(self, other)
882
+
883
+ def eig(self, eigenvectors=False):
884
+ from torch._linalg_utils import eig
885
+
886
+ return eig(self, eigenvectors=eigenvectors)
887
+
888
+ def symeig(self, eigenvectors=False):
889
+ from torch._linalg_utils import _symeig
890
+
891
+ return _symeig(self, eigenvectors=eigenvectors)
892
+
893
+ def lu(self, pivot=True, get_infos=False):
894
+ r"""See :func:`torch.lu`"""
895
+ # If get_infos is True, then we don't need to check for errors and vice versa
896
+ if has_torch_function_unary(self):
897
+ return handle_torch_function(
898
+ Tensor.lu, (self,), self, pivot=pivot, get_infos=get_infos
899
+ )
900
+
901
+ LU, pivots, infos = torch._lu_with_info(
902
+ self, pivot=pivot, check_errors=(not get_infos)
903
+ )
904
+ if get_infos:
905
+ return LU, pivots, infos
906
+ else:
907
+ return LU, pivots
908
+
909
+ def stft(
910
+ self,
911
+ n_fft: int,
912
+ hop_length: Optional[int] = None,
913
+ win_length: Optional[int] = None,
914
+ window: "Optional[Tensor]" = None,
915
+ center: bool = True,
916
+ pad_mode: str = "reflect",
917
+ normalized: bool = False,
918
+ onesided: Optional[bool] = None,
919
+ return_complex: Optional[bool] = None,
920
+ align_to_window: Optional[bool] = None,
921
+ ):
922
+ r"""See :func:`torch.stft`
923
+
924
+ .. warning::
925
+ This function changed signature at version 0.4.1. Calling with
926
+ the previous signature may cause error or return incorrect result.
927
+ """
928
+ if has_torch_function_unary(self):
929
+ return handle_torch_function(
930
+ Tensor.stft,
931
+ (self,),
932
+ self,
933
+ n_fft,
934
+ hop_length=hop_length,
935
+ win_length=win_length,
936
+ window=window,
937
+ center=center,
938
+ pad_mode=pad_mode,
939
+ normalized=normalized,
940
+ onesided=onesided,
941
+ return_complex=return_complex,
942
+ align_to_window=align_to_window,
943
+ )
944
+ return torch.stft(
945
+ self,
946
+ n_fft,
947
+ hop_length,
948
+ win_length,
949
+ window,
950
+ center,
951
+ pad_mode,
952
+ normalized,
953
+ onesided,
954
+ return_complex=return_complex,
955
+ align_to_window=align_to_window,
956
+ )
957
+
958
+ def istft(
959
+ self,
960
+ n_fft: int,
961
+ hop_length: Optional[int] = None,
962
+ win_length: Optional[int] = None,
963
+ window: "Optional[Tensor]" = None,
964
+ center: bool = True,
965
+ normalized: bool = False,
966
+ onesided: Optional[bool] = None,
967
+ length: Optional[int] = None,
968
+ return_complex: bool = False,
969
+ ):
970
+ r"""See :func:`torch.istft`"""
971
+ if has_torch_function_unary(self):
972
+ return handle_torch_function(
973
+ Tensor.istft,
974
+ (self,),
975
+ self,
976
+ n_fft,
977
+ hop_length=hop_length,
978
+ win_length=win_length,
979
+ window=window,
980
+ center=center,
981
+ normalized=normalized,
982
+ onesided=onesided,
983
+ length=length,
984
+ return_complex=return_complex,
985
+ )
986
+ return torch.istft(
987
+ self,
988
+ n_fft,
989
+ hop_length,
990
+ win_length,
991
+ window,
992
+ center,
993
+ normalized,
994
+ onesided,
995
+ length,
996
+ return_complex=return_complex,
997
+ )
998
+
999
+ def resize(self, *sizes):
1000
+ if has_torch_function_unary(self):
1001
+ return handle_torch_function(Tensor.resize, (self,), self, *sizes)
1002
+ warnings.warn("non-inplace resize is deprecated")
1003
+ from torch.autograd._functions import Resize
1004
+
1005
+ return Resize.apply(self, sizes)
1006
+
1007
+ def resize_as(self, tensor):
1008
+ if has_torch_function_variadic(self, tensor):
1009
+ return handle_torch_function(Tensor.resize_as, (self, tensor), self, tensor)
1010
+ warnings.warn("non-inplace resize_as is deprecated")
1011
+ from torch.autograd._functions import Resize
1012
+
1013
+ return Resize.apply(self, tensor.size())
1014
+
1015
+ def split(self, split_size, dim=0):
1016
+ r"""See :func:`torch.split`"""
1017
+ if has_torch_function_unary(self):
1018
+ return handle_torch_function(
1019
+ Tensor.split, (self,), self, split_size, dim=dim
1020
+ )
1021
+ if isinstance(split_size, Tensor):
1022
+ try:
1023
+ split_size = int(split_size)
1024
+ except ValueError:
1025
+ pass
1026
+
1027
+ if isinstance(split_size, (int, torch.SymInt)):
1028
+ return torch._VF.split(self, split_size, dim) # type: ignore[attr-defined]
1029
+ else:
1030
+ return torch._VF.split_with_sizes(self, split_size, dim)
1031
+
1032
+ def unique(self, sorted=True, return_inverse=False, return_counts=False, dim=None):
1033
+ r"""Returns the unique elements of the input tensor.
1034
+
1035
+ See :func:`torch.unique`
1036
+ """
1037
+ if has_torch_function_unary(self):
1038
+ return handle_torch_function(
1039
+ Tensor.unique,
1040
+ (self,),
1041
+ self,
1042
+ sorted=sorted,
1043
+ return_inverse=return_inverse,
1044
+ return_counts=return_counts,
1045
+ dim=dim,
1046
+ )
1047
+ return torch.unique(
1048
+ self,
1049
+ sorted=sorted,
1050
+ return_inverse=return_inverse,
1051
+ return_counts=return_counts,
1052
+ dim=dim,
1053
+ )
1054
+
1055
+ def unique_consecutive(self, return_inverse=False, return_counts=False, dim=None):
1056
+ r"""Eliminates all but the first element from every consecutive group of equivalent elements.
1057
+
1058
+ See :func:`torch.unique_consecutive`
1059
+ """
1060
+ if has_torch_function_unary(self):
1061
+ return handle_torch_function(
1062
+ Tensor.unique_consecutive,
1063
+ (self,),
1064
+ self,
1065
+ return_inverse=return_inverse,
1066
+ return_counts=return_counts,
1067
+ dim=dim,
1068
+ )
1069
+ return torch.unique_consecutive(
1070
+ self, return_inverse=return_inverse, return_counts=return_counts, dim=dim
1071
+ )
1072
+
1073
+ @_handle_torch_function_and_wrap_type_error_to_not_implemented
1074
+ def __rsub__(self, other: Union["Tensor", int, float, bool, complex]) -> "Tensor":
1075
+ return _C._VariableFunctions.rsub(self, other)
1076
+
1077
+ @_handle_torch_function_and_wrap_type_error_to_not_implemented
1078
+ def __rdiv__(self, other: Union["Tensor", int, float, bool, complex]) -> "Tensor":
1079
+ return self.reciprocal() * other
1080
+
1081
+ __rtruediv__ = __rdiv__
1082
+ __itruediv__ = _C.TensorBase.__idiv__
1083
+
1084
+ __pow__ = cast(
1085
+ Callable[
1086
+ ["torch._C.TensorBase", Union["Tensor", int, float, bool, complex]],
1087
+ "Tensor",
1088
+ ],
1089
+ _handle_torch_function_and_wrap_type_error_to_not_implemented(
1090
+ _C.TensorBase.pow
1091
+ ),
1092
+ )
1093
+
1094
+ __ipow__ = _handle_torch_function_and_wrap_type_error_to_not_implemented(
1095
+ _C.TensorBase.pow_
1096
+ )
1097
+
1098
+ @_handle_torch_function_and_wrap_type_error_to_not_implemented
1099
+ def __rmod__(self, other: Union["Tensor", int, float, bool, complex]) -> "Tensor":
1100
+ return torch.remainder(other, self)
1101
+
1102
+ def __format__(self, format_spec):
1103
+ if has_torch_function_unary(self):
1104
+ return handle_torch_function(Tensor.__format__, (self,), self, format_spec)
1105
+ if self.dim() == 0 and not self.is_meta and type(self) is Tensor:
1106
+ # Use detach() here to avoid the warning when converting a scalar Tensor that
1107
+ # requires gradients to a python number. It is ok for formatting.
1108
+ return self.detach().item().__format__(format_spec)
1109
+ return object.__format__(self, format_spec)
1110
+
1111
+ @_handle_torch_function_and_wrap_type_error_to_not_implemented
1112
+ def __rpow__(self, other: Union["Tensor", int, float, bool, complex]) -> "Tensor":
1113
+ return torch.pow(other, self)
1114
+
1115
+ @_handle_torch_function_and_wrap_type_error_to_not_implemented
1116
+ def __floordiv__(self, other: Union["Tensor", int, float, bool]) -> "Tensor": # type: ignore[override]
1117
+ # TODO(rec): the superclass says it accepts complex here,
1118
+ # but torch.floor_divide says it doesn't.
1119
+ return torch.floor_divide(self, other)
1120
+
1121
+ @_handle_torch_function_and_wrap_type_error_to_not_implemented
1122
+ def __rfloordiv__(self, other: Union["Tensor", int, float, bool]) -> "Tensor": # type: ignore[override]
1123
+ return torch.floor_divide(other, self)
1124
+
1125
+ @_handle_torch_function_and_wrap_type_error_to_not_implemented
1126
+ def __rlshift__(
1127
+ self, other: Union["Tensor", int, float, bool, complex]
1128
+ ) -> "Tensor":
1129
+ return torch.bitwise_left_shift(other, self)
1130
+
1131
+ @_handle_torch_function_and_wrap_type_error_to_not_implemented
1132
+ def __rrshift__(
1133
+ self, other: Union["Tensor", int, float, bool, complex]
1134
+ ) -> "Tensor":
1135
+ return torch.bitwise_right_shift(other, self)
1136
+
1137
+ @_handle_torch_function_and_wrap_type_error_to_not_implemented
1138
+ def __rmatmul__(self, other: "Tensor") -> "Tensor":
1139
+ return torch.matmul(other, self)
1140
+
1141
+ __pos__ = _C.TensorBase.positive
1142
+ __neg__ = _C.TensorBase.neg
1143
+ __abs__ = _C.TensorBase.abs
1144
+
1145
+ def __len__(self):
1146
+ if has_torch_function_unary(self):
1147
+ return handle_torch_function(Tensor.__len__, (self,), self)
1148
+ if self.dim() == 0:
1149
+ raise TypeError("len() of a 0-d tensor")
1150
+ if torch._C._get_tracing_state():
1151
+ warnings.warn(
1152
+ "Using len to get tensor shape might cause the trace to be incorrect. "
1153
+ "Recommended usage would be tensor.shape[0]. "
1154
+ "Passing a tensor of different shape might lead to errors or silently give "
1155
+ "incorrect results.",
1156
+ category=torch.jit.TracerWarning,
1157
+ stacklevel=2,
1158
+ )
1159
+ return self.shape[0]
1160
+
1161
+ def __iter__(self):
1162
+ # NB: we use 'imap' and not 'map' here, so that in Python 2 we get a
1163
+ # generator and don't eagerly perform all the indexes. This could
1164
+ # save us work, and also helps keep trace ordering deterministic
1165
+ # (e.g., if you zip(*hiddens), the eager map will force all the
1166
+ # indexes of hiddens[0] before hiddens[1], while the generator
1167
+ # map will interleave them.)
1168
+ # NB: We have intentionally skipped __torch_function__ dispatch here.
1169
+ # See gh-54457
1170
+ if self.dim() == 0:
1171
+ raise TypeError("iteration over a 0-d tensor")
1172
+ if torch._C._get_tracing_state():
1173
+ warnings.warn(
1174
+ "Iterating over a tensor might cause the trace to be incorrect. "
1175
+ "Passing a tensor of different shape won't change the number of "
1176
+ "iterations executed (and might lead to errors or silently give "
1177
+ "incorrect results).",
1178
+ category=torch.jit.TracerWarning,
1179
+ stacklevel=2,
1180
+ )
1181
+ return iter(self.unbind(0))
1182
+
1183
+ def __hash__(self):
1184
+ # Do NOT handle __torch_function__ here as user's default
1185
+ # implementation that handle most functions will most likely do it wrong.
1186
+ # It can be easily overridden by defining this method on the user
1187
+ # subclass if needed.
1188
+ return id(self)
1189
+
1190
+ def __dir__(self):
1191
+ if has_torch_function_unary(self):
1192
+ return handle_torch_function(Tensor.__dir__, (self,), self)
1193
+ tensor_methods = dir(self.__class__)
1194
+ tensor_methods.remove("volatile") # deprecated
1195
+ attrs = list(self.__dict__.keys())
1196
+ keys = tensor_methods + attrs
1197
+
1198
+ # property only available dense, cuda tensors
1199
+ if (not self.is_cuda) or self.is_sparse:
1200
+ keys.remove("__cuda_array_interface__")
1201
+
1202
+ return sorted(keys)
1203
+
1204
+ # Numpy array interface, to support `numpy.asarray(tensor) -> ndarray`
1205
+ __array_priority__ = 1000 # prefer Tensor ops over numpy ones
1206
+
1207
+ def __array__(self, dtype=None):
1208
+ if has_torch_function_unary(self):
1209
+ return handle_torch_function(Tensor.__array__, (self,), self, dtype=dtype)
1210
+ if dtype is None:
1211
+ return self.numpy()
1212
+ else:
1213
+ return self.numpy().astype(dtype, copy=False)
1214
+
1215
+ # Wrap Numpy array again in a suitable tensor when done, to support e.g.
1216
+ # `numpy.sin(tensor) -> tensor` or `numpy.greater(tensor, 0) -> ByteTensor`
1217
+ def __array_wrap__(self, array):
1218
+ if has_torch_function_unary(self):
1219
+ return handle_torch_function(
1220
+ Tensor.__array_wrap__, (self,), self, array=array
1221
+ )
1222
+ if array.dtype == bool:
1223
+ # Workaround, torch has no built-in bool tensor
1224
+ array = array.astype("uint8")
1225
+ return torch.from_numpy(array)
1226
+
1227
+ def __contains__(self, element: Any, /) -> bool:
1228
+ r"""Check if `element` is present in tensor
1229
+
1230
+ Args:
1231
+ element (Tensor or scalar): element to be checked
1232
+ for presence in current tensor"
1233
+ """
1234
+ if has_torch_function_unary(self):
1235
+ return handle_torch_function(Tensor.__contains__, (self,), self, element)
1236
+ if isinstance(
1237
+ element, (torch.Tensor, Number, torch.SymInt, torch.SymFloat, torch.SymBool)
1238
+ ):
1239
+ # type hint doesn't understand the __contains__ result array
1240
+ return bool((element == self).any().item()) # type: ignore[union-attr]
1241
+
1242
+ raise RuntimeError(
1243
+ f"Tensor.__contains__ only supports Tensor or scalar, but you passed in a {type(element)}."
1244
+ )
1245
+
1246
+ @property
1247
+ def __cuda_array_interface__(self):
1248
+ """Array view description for cuda tensors.
1249
+
1250
+ See:
1251
+ https://numba.pydata.org/numba-doc/dev/cuda/cuda_array_interface.html
1252
+ """
1253
+ if has_torch_function_unary(self):
1254
+ # TODO mypy doesn't support @property, see: https://github.com/python/mypy/issues/6185
1255
+ return handle_torch_function(
1256
+ Tensor.__cuda_array_interface__.__get__, # type: ignore[attr-defined]
1257
+ (self,),
1258
+ self,
1259
+ )
1260
+
1261
+ # raise AttributeError for unsupported tensors, so that
1262
+ # hasattr(cpu_tensor, "__cuda_array_interface__") is False.
1263
+ if not self.is_cuda:
1264
+ raise AttributeError(
1265
+ f"Can't get __cuda_array_interface__ on non-CUDA tensor type: {self.type()} "
1266
+ "If CUDA data is required use tensor.cuda() to copy tensor to device memory."
1267
+ )
1268
+
1269
+ if self.is_sparse:
1270
+ raise AttributeError(
1271
+ f"Can't get __cuda_array_interface__ on sparse type: {self.type()} "
1272
+ "Use Tensor.to_dense() to convert to a dense tensor first."
1273
+ )
1274
+
1275
+ # RuntimeError, matching tensor.__array__() behavior.
1276
+ if self.requires_grad:
1277
+ raise RuntimeError(
1278
+ "Can't get __cuda_array_interface__ on Variable that requires grad. "
1279
+ "If gradients aren't required, use var.detach() to get Variable that doesn't require grad."
1280
+ )
1281
+
1282
+ typestr = _dtype_to_typestr(self.dtype)
1283
+ itemsize = self.element_size()
1284
+ shape = tuple(self.shape)
1285
+ if self.is_contiguous():
1286
+ # __cuda_array_interface__ v2 requires the strides to be omitted
1287
+ # (either not set or set to None) for C-contiguous arrays.
1288
+ strides = None
1289
+ else:
1290
+ strides = tuple(s * itemsize for s in self.stride())
1291
+ data_ptr = self.data_ptr() if self.numel() > 0 else 0
1292
+ data = (data_ptr, False) # read-only is false
1293
+
1294
+ return dict(typestr=typestr, shape=shape, strides=strides, data=data, version=2)
1295
+
1296
+ def storage_type(self):
1297
+ r"""storage_type() -> type
1298
+
1299
+ Returns the type of the underlying storage.
1300
+
1301
+ """
1302
+ if has_torch_function_unary(self):
1303
+ return handle_torch_function(Tensor.storage_type, (self,), self)
1304
+
1305
+ torch.storage._warn_typed_storage_removal()
1306
+
1307
+ return self._typed_storage()._get_legacy_storage_class()
1308
+
1309
+ def refine_names(self, *names):
1310
+ r"""Refines the dimension names of :attr:`self` according to :attr:`names`.
1311
+
1312
+ Refining is a special case of renaming that "lifts" unnamed dimensions.
1313
+ A ``None`` dim can be refined to have any name; a named dim can only be
1314
+ refined to have the same name.
1315
+
1316
+ Because named tensors can coexist with unnamed tensors, refining names
1317
+ gives a nice way to write named-tensor-aware code that works with both
1318
+ named and unnamed tensors.
1319
+
1320
+ :attr:`names` may contain up to one Ellipsis (``...``).
1321
+ The Ellipsis is expanded greedily; it is expanded in-place to fill
1322
+ :attr:`names` to the same length as ``self.dim()`` using names from the
1323
+ corresponding indices of ``self.names``.
1324
+
1325
+ Python 2 does not support Ellipsis but one may use a string literal
1326
+ instead (``'...'``).
1327
+
1328
+ Args:
1329
+ names (iterable of str): The desired names of the output tensor. May
1330
+ contain up to one Ellipsis.
1331
+
1332
+ Examples::
1333
+
1334
+ >>> imgs = torch.randn(32, 3, 128, 128)
1335
+ >>> named_imgs = imgs.refine_names('N', 'C', 'H', 'W')
1336
+ >>> named_imgs.names
1337
+ ('N', 'C', 'H', 'W')
1338
+
1339
+ >>> tensor = torch.randn(2, 3, 5, 7, 11)
1340
+ >>> tensor = tensor.refine_names('A', ..., 'B', 'C')
1341
+ >>> tensor.names
1342
+ ('A', None, None, 'B', 'C')
1343
+
1344
+ .. warning::
1345
+ The named tensor API is experimental and subject to change.
1346
+
1347
+ """
1348
+ if has_torch_function_unary(self):
1349
+ return handle_torch_function(Tensor.refine_names, (self,), self, *names)
1350
+ names = resolve_ellipsis(names, self.names, "refine_names")
1351
+ return super().refine_names(names)
1352
+
1353
+ def align_to(self, *names):
1354
+ r"""Permutes the dimensions of the :attr:`self` tensor to match the order
1355
+ specified in :attr:`names`, adding size-one dims for any new names.
1356
+
1357
+ All of the dims of :attr:`self` must be named in order to use this method.
1358
+ The resulting tensor is a view on the original tensor.
1359
+
1360
+ All dimension names of :attr:`self` must be present in :attr:`names`.
1361
+ :attr:`names` may contain additional names that are not in ``self.names``;
1362
+ the output tensor has a size-one dimension for each of those new names.
1363
+
1364
+ :attr:`names` may contain up to one Ellipsis (``...``).
1365
+ The Ellipsis is expanded to be equal to all dimension names of :attr:`self`
1366
+ that are not mentioned in :attr:`names`, in the order that they appear
1367
+ in :attr:`self`.
1368
+
1369
+ Python 2 does not support Ellipsis but one may use a string literal
1370
+ instead (``'...'``).
1371
+
1372
+ Args:
1373
+ names (iterable of str): The desired dimension ordering of the
1374
+ output tensor. May contain up to one Ellipsis that is expanded
1375
+ to all unmentioned dim names of :attr:`self`.
1376
+
1377
+ Examples::
1378
+
1379
+ >>> tensor = torch.randn(2, 2, 2, 2, 2, 2)
1380
+ >>> named_tensor = tensor.refine_names('A', 'B', 'C', 'D', 'E', 'F')
1381
+
1382
+ # Move the F and E dims to the front while keeping the rest in order
1383
+ >>> named_tensor.align_to('F', 'E', ...)
1384
+
1385
+ .. warning::
1386
+ The named tensor API is experimental and subject to change.
1387
+
1388
+ """
1389
+ if has_torch_function_unary(self):
1390
+ return handle_torch_function(Tensor.align_to, (self,), self, *names)
1391
+ ellipsis_idx = single_ellipsis_index(names, "align_to")
1392
+ if ellipsis_idx is None:
1393
+ return super().align_to(names)
1394
+ return super().align_to(
1395
+ [name for name in names if not is_ellipsis(name)], ellipsis_idx
1396
+ )
1397
+
1398
+ def unflatten(self, dim, sizes): # type: ignore[override]
1399
+ r"""
1400
+ unflatten(dim, sizes) -> Tensor
1401
+
1402
+ See :func:`torch.unflatten`.
1403
+
1404
+ """
1405
+ if has_torch_function_unary(self):
1406
+ return handle_torch_function(Tensor.unflatten, (self,), self, dim, sizes)
1407
+
1408
+ if not sizes:
1409
+ raise RuntimeError("unflatten: sizes must be non-empty")
1410
+
1411
+ names = None
1412
+ if isinstance(sizes, OrderedDict) or (
1413
+ isinstance(sizes, (tuple, list)) and isinstance(sizes[0], (tuple, list))
1414
+ ):
1415
+ names, sizes = unzip_namedshape(sizes)
1416
+ return super().unflatten(dim, sizes, names)
1417
+ else:
1418
+ return super().unflatten(dim, sizes)
1419
+
1420
+ def rename_(self, *names, **rename_map):
1421
+ """In-place version of :meth:`~Tensor.rename`."""
1422
+
1423
+ if has_torch_function_unary(self):
1424
+ return handle_torch_function(
1425
+ Tensor.rename_, (self,), self, *names, **rename_map
1426
+ )
1427
+
1428
+ # Note [rename_ / rename API]
1429
+ # The Python API for these is different from the C++ API. In Python:
1430
+ # 1) tensor.rename(*names) takes a vararglist of names
1431
+ # 2) tensor.rename(**rename_map) takes a map of names to rename.
1432
+ # C++ is static, making it difficult to implement similar behavior.
1433
+ return update_names(self, names, rename_map, inplace=True)
1434
+
1435
+ def rename(self, *names, **rename_map):
1436
+ """Renames dimension names of :attr:`self`.
1437
+
1438
+ There are two main usages:
1439
+
1440
+ ``self.rename(**rename_map)`` returns a view on tensor that has dims
1441
+ renamed as specified in the mapping :attr:`rename_map`.
1442
+
1443
+ ``self.rename(*names)`` returns a view on tensor, renaming all
1444
+ dimensions positionally using :attr:`names`.
1445
+ Use ``self.rename(None)`` to drop names on a tensor.
1446
+
1447
+ One cannot specify both positional args :attr:`names` and keyword args
1448
+ :attr:`rename_map`.
1449
+
1450
+ Examples::
1451
+
1452
+ >>> imgs = torch.rand(2, 3, 5, 7, names=('N', 'C', 'H', 'W'))
1453
+ >>> renamed_imgs = imgs.rename(N='batch', C='channels')
1454
+ >>> renamed_imgs.names
1455
+ ('batch', 'channels', 'H', 'W')
1456
+
1457
+ >>> renamed_imgs = imgs.rename(None)
1458
+ >>> renamed_imgs.names
1459
+ (None, None, None, None)
1460
+
1461
+ >>> renamed_imgs = imgs.rename('batch', 'channel', 'height', 'width')
1462
+ >>> renamed_imgs.names
1463
+ ('batch', 'channel', 'height', 'width')
1464
+
1465
+ .. warning::
1466
+ The named tensor API is experimental and subject to change.
1467
+
1468
+ """
1469
+ if has_torch_function_unary(self):
1470
+ return handle_torch_function(
1471
+ Tensor.rename, (self,), self, *names, **rename_map
1472
+ )
1473
+
1474
+ # See Note [rename_ / rename API]
1475
+ return update_names(self, names, rename_map, inplace=False)
1476
+
1477
+ def to_sparse_coo(self):
1478
+ """Convert a tensor to :ref:`coordinate format <sparse-coo-docs>`.
1479
+
1480
+ Examples::
1481
+
1482
+ >>> dense = torch.randn(5, 5)
1483
+ >>> sparse = dense.to_sparse_coo()
1484
+ >>> sparse._nnz()
1485
+ 25
1486
+
1487
+ """
1488
+ return self.to_sparse()
1489
+
1490
+ def dim_order(
1491
+ self, *, ambiguity_check: Union[bool, list[torch.memory_format]] = False
1492
+ ):
1493
+ """
1494
+ dim_order(ambiguity_check=False) -> tuple
1495
+
1496
+ Returns the uniquely determined tuple of int describing the dim order or
1497
+ physical layout of :attr:`self`.
1498
+
1499
+ The dim order represents how dimensions are laid out in memory of dense tensors,
1500
+ starting from the outermost to the innermost dimension.
1501
+
1502
+ Note that the dim order may not always be uniquely determined.
1503
+ If `ambiguity_check` is True, this function raises a RuntimeError when the dim order cannot be uniquely determined;
1504
+ If `ambiguity_check` is a list of memory formats, this function raises a RuntimeError when tensor can not be interpreted
1505
+ into exactly one of the given memory formats, or it cannot be uniquely determined.
1506
+ If `ambiguity_check` is False, it will return one of legal dim order(s) without checking its uniqueness.
1507
+ Otherwise, it will raise TypeError.
1508
+
1509
+ Args:
1510
+ ambiguity_check (bool or List[torch.memory_format]): The check method for ambiguity of dim order.
1511
+
1512
+ Examples::
1513
+
1514
+ >>> torch.empty((2, 3, 5, 7)).dim_order()
1515
+ (0, 1, 2, 3)
1516
+ >>> torch.empty((2, 3, 5, 7)).transpose(1, 2).dim_order()
1517
+ (0, 2, 1, 3)
1518
+ >>> torch.empty((2, 3, 5, 7), memory_format=torch.channels_last).dim_order()
1519
+ (0, 2, 3, 1)
1520
+ >>> torch.empty((1, 2, 3, 4)).dim_order()
1521
+ (0, 1, 2, 3)
1522
+ >>> try:
1523
+ ... torch.empty((1, 2, 3, 4)).dim_order(ambiguity_check=True)
1524
+ ... except RuntimeError as e:
1525
+ ... print(e)
1526
+ The tensor does not have unique dim order, or cannot map to exact one of the given memory formats.
1527
+ >>> torch.empty((1, 2, 3, 4)).dim_order(
1528
+ ... ambiguity_check=[torch.contiguous_format, torch.channels_last]
1529
+ ... ) # It can be mapped to contiguous format
1530
+ (0, 1, 2, 3)
1531
+ >>> try:
1532
+ ... torch.empty((1, 2, 3, 4)).dim_order(ambiguity_check="ILLEGAL")
1533
+ ... except TypeError as e:
1534
+ ... print(e)
1535
+ The ambiguity_check argument must be a bool or a list of memory formats.
1536
+
1537
+ .. warning::
1538
+ The dim_order tensor API is experimental and subject to change.
1539
+ """
1540
+ if has_torch_function_unary(self):
1541
+ return handle_torch_function(Tensor.dim_order, (self,), self)
1542
+
1543
+ if self.is_sparse:
1544
+ raise AttributeError(
1545
+ f"Can't get dim order on sparse type: {self.type()} "
1546
+ "Use Tensor.to_dense() to convert to a dense tensor first."
1547
+ )
1548
+
1549
+ # Sanity check ambiguity_check data types
1550
+ if not isinstance(ambiguity_check, bool):
1551
+ if not isinstance(ambiguity_check, list):
1552
+ raise TypeError(
1553
+ "The ambiguity_check argument must be a bool or a list of memory formats."
1554
+ )
1555
+ for memory_format in ambiguity_check:
1556
+ if not isinstance(memory_format, torch.memory_format):
1557
+ raise TypeError(
1558
+ "The ambiguity_check argument must be a bool or a list of memory formats."
1559
+ )
1560
+
1561
+ def invalid_unique_memory_format(tensor, valid_memory_formats):
1562
+ """
1563
+ Returns True if the tensor cannot be uniquely mapped to any of the given memory formats, False otherwise.
1564
+ """
1565
+
1566
+ n_legality = 0
1567
+
1568
+ for memory_format in valid_memory_formats:
1569
+ if tensor.is_contiguous(memory_format=memory_format):
1570
+ n_legality += 1
1571
+
1572
+ return n_legality != 1
1573
+
1574
+ def has_multiple_dim_order(tensor):
1575
+ """
1576
+ Returns True if there're multiple legal dim orders for given tensor, False otherwise.
1577
+
1578
+ The tensor is considered to have multiple legal dim orders if either of the following conditions is met:
1579
+
1580
+ * Singleton Dimensions: There's at least one singleteon dimension in the tensor.
1581
+ Since their size is 1, they don't affect the memory offset (stride * index
1582
+ is zero because index is always zero). Therefore, they can be placed anywhere
1583
+ in the dimension order without changing how data is accessed.
1584
+ * Same strides: Strides reflect how the tensor is stored in memory.
1585
+ If any two dimensions have the same stride, swapping these dimensions won't
1586
+ change how data is accessed, leading to multiple correct dimension orders.
1587
+ """
1588
+
1589
+ sizes = tensor.size()
1590
+ strides = tensor.stride()
1591
+
1592
+ # Check if there are any duplicate strides
1593
+ has_duplicate_strides = any(
1594
+ earlier == later for earlier, later in zip(strides, strides[1:])
1595
+ )
1596
+
1597
+ # Check if there are any singleton dimensions
1598
+ has_singleton_dims = any(size == 1 for size in sizes)
1599
+
1600
+ return has_duplicate_strides or has_singleton_dims
1601
+
1602
+ valid_memory_formats = (
1603
+ ambiguity_check if isinstance(ambiguity_check, list) else []
1604
+ )
1605
+ check_multiple_dim_order = (
1606
+ ambiguity_check if isinstance(ambiguity_check, bool) else True
1607
+ )
1608
+
1609
+ if (
1610
+ check_multiple_dim_order and has_multiple_dim_order(self)
1611
+ ) and invalid_unique_memory_format(self, valid_memory_formats):
1612
+ raise RuntimeError(
1613
+ "The tensor does not have unique dim order, or cannot map to exact one of the given memory formats."
1614
+ )
1615
+
1616
+ import torch._prims_common as utils
1617
+
1618
+ return tuple(utils.compute_elementwise_output_logical_to_physical_perm(self))
1619
+
1620
+ def _update_names(self, names, inplace):
1621
+ if has_torch_function_unary(self):
1622
+ return handle_torch_function(
1623
+ Tensor._update_names, (self,), self, names, inplace
1624
+ )
1625
+
1626
+ # See Note [rename_ / rename API]
1627
+ if inplace:
1628
+ return super().rename_(names)
1629
+ else:
1630
+ return super().rename(names)
1631
+
1632
+ @classmethod
1633
+ def __torch_function__(cls, func, types, args=(), kwargs=None):
1634
+ """
1635
+ This __torch_function__ implementation wraps subclasses such that
1636
+ methods called on subclasses return a subclass instance instead of
1637
+ a ``torch.Tensor`` instance.
1638
+
1639
+ One corollary to this is that you need coverage for torch.Tensor
1640
+ methods if implementing __torch_function__ for subclasses.
1641
+
1642
+ We recommend always calling ``super().__torch_function__`` as the base
1643
+ case when doing the above.
1644
+
1645
+ While not mandatory, we recommend making `__torch_function__` a classmethod.
1646
+ """
1647
+ if kwargs is None:
1648
+ kwargs = {}
1649
+
1650
+ if not all(issubclass(cls, t) for t in types):
1651
+ return NotImplemented
1652
+
1653
+ with _C.DisableTorchFunctionSubclass():
1654
+ ret = func(*args, **kwargs)
1655
+ if func in get_default_nowrap_functions():
1656
+ return ret
1657
+ else:
1658
+ return _convert(ret, cls)
1659
+
1660
+ __torch_dispatch__ = _C._disabled_torch_dispatch_impl
1661
+
1662
+ def __dlpack__(
1663
+ self,
1664
+ *,
1665
+ stream: Optional[Any] = None,
1666
+ max_version: Optional[tuple[int, int]] = None,
1667
+ dl_device: Optional[tuple[enum.IntEnum, int]] = None,
1668
+ copy: Optional[bool] = None,
1669
+ ):
1670
+ """
1671
+ Creates a DLpack `capsule https://data-apis.org/array-api/latest/design_topics/data_interchange.html#data-interchange`_
1672
+ of the current tensor to be exported to other libraries.
1673
+
1674
+ This function will be called from the `from_dlpack` method
1675
+ of the library that will consume the capsule. `from_dlpack` passes the current
1676
+ stream to this method as part of the specification.
1677
+
1678
+ Args:
1679
+ stream (integer or None): An optional Python integer representing a
1680
+ pointer to a CUDA stream. The current stream is synchronized with
1681
+ this stream before the capsule is created, and since the capsule
1682
+ shares its storage with the tensor this make it safe to access from
1683
+ both streams. If None or -1 is passed then no synchronization is performed.
1684
+ If 1 (on CUDA) or 0 (on ROCM) then the default stream is used for
1685
+ synchronization.
1686
+
1687
+ max_version (tuple[int, int] or None): An optional Python tuple with
1688
+ 2 integers, representing the maximum version the caller supports. If
1689
+ None (default), PyTorch will fallback to DLPack 0.8.
1690
+
1691
+ dl_device (tuple[DLDeviceType, int] or None): An optional tuple specifying
1692
+ in which device the exported DLPack capsule should be on. If None (default),
1693
+ the exported DLPack capsule will be on the same device as ``self``.
1694
+
1695
+ copy (bool or None): An optional boolean indicating whether or not to copy
1696
+ ``self``. If None, PyTorch will copy only if necessary.
1697
+ """
1698
+ if has_torch_function_unary(self):
1699
+ args = (self,)
1700
+ kwargs = {
1701
+ "stream": stream,
1702
+ "max_version": max_version,
1703
+ "dl_device": dl_device,
1704
+ "copy": copy,
1705
+ }
1706
+ return handle_torch_function(Tensor.__dlpack__, (self,), *args, **kwargs)
1707
+
1708
+ # DLPack capsules can't capture all of PyTorch's semantics,
1709
+ # so we prohibit exporting tensors that would lose their properties like
1710
+ # requires_grad and having the conjugate bit set.
1711
+ if self.requires_grad:
1712
+ raise BufferError(
1713
+ "Can't export tensors that require gradient, use tensor.detach()"
1714
+ )
1715
+ if self.is_conj():
1716
+ raise BufferError("Can't export tensors with the conjugate bit set")
1717
+ if self.layout != torch.strided:
1718
+ raise BufferError(
1719
+ "Can't export tensors with layout other than torch.strided"
1720
+ )
1721
+
1722
+ if (
1723
+ self.device.type == "cuda"
1724
+ and self.device.index != torch.cuda.current_device()
1725
+ ):
1726
+ raise BufferError(
1727
+ "Can't export tensors on a different CUDA device index. "
1728
+ f"Expected: {self.device.index}. "
1729
+ f"Current device: {torch.cuda.current_device()}."
1730
+ )
1731
+
1732
+ if stream is not None and type(stream) is not int:
1733
+ # Stream pointers in CUDA/ROCm are uniquely numbered and can
1734
+ # be retrieved from their integer value.
1735
+ raise TypeError("stream must be ``int`` or ``none``")
1736
+ elif self.device.type == "cuda" and stream != -1:
1737
+ # NB: This logic handles the special case values for default
1738
+ # streams and must be kept in sync with from_dlpack in
1739
+ # torch/utils/dlpack.py
1740
+ is_rocm = torch.version.hip is not None
1741
+ is_cuda = not is_rocm
1742
+
1743
+ if stream is None or (is_rocm and stream == 0) or (is_cuda and stream == 1):
1744
+ stream = torch.cuda.default_stream()
1745
+ else:
1746
+ if is_cuda and stream == 2:
1747
+ raise BufferError("per-thread default stream is not supported.")
1748
+
1749
+ device_str = "CUDA" if is_cuda else "ROCm"
1750
+ assert (is_cuda and stream != 0) or (
1751
+ is_rocm and stream not in (1, 2)
1752
+ ), f"unsupported stream on {device_str}: {stream}."
1753
+
1754
+ stream = torch.cuda.ExternalStream(stream)
1755
+
1756
+ # Only synchronize on different streams
1757
+ current_stream = torch.cuda.current_stream()
1758
+ if stream != current_stream:
1759
+ event = torch.cuda.Event()
1760
+ event.record(current_stream)
1761
+ stream.wait_event(event)
1762
+ elif self.device.type == "cpu":
1763
+ assert stream is None, "stream should be None on cpu."
1764
+
1765
+ if self.device.type == "xla":
1766
+ import torch_xla
1767
+ import torch_xla.utils.dlpack as xla_dlpack
1768
+
1769
+ if (
1770
+ len(torch_xla.real_devices()) <= 0
1771
+ or "cuda" not in torch_xla.real_devices()[0].lower()
1772
+ ):
1773
+ raise RuntimeError(
1774
+ "Can't export to dlpack an XLA tensor that is not on CUDA."
1775
+ )
1776
+
1777
+ # Does not support DLPack 1.0, yet.
1778
+ return xla_dlpack.to_dlpack(self)
1779
+
1780
+ if max_version is None or max_version[0] < 1:
1781
+ # Fallback to the old, unversioned variant.
1782
+ return _C._to_dlpack(self, dl_device=dl_device, copy=copy)
1783
+
1784
+ return _C._to_dlpack_versioned(self, dl_device=dl_device, copy=copy)
1785
+
1786
+ def __dlpack_device__(self) -> tuple[enum.IntEnum, int]:
1787
+ if has_torch_function_unary(self):
1788
+ return handle_torch_function(Tensor.__dlpack_device__, (self,), self)
1789
+
1790
+ from torch.utils.dlpack import DLDeviceType
1791
+
1792
+ device = self.device
1793
+ idx = device.index if device.index is not None else 0
1794
+ torch_device_type = device.type
1795
+ if torch_device_type == "cuda" and torch.version.hip is not None:
1796
+ device_type = DLDeviceType.kDLROCM
1797
+ elif torch_device_type == "cpu" and self.is_pinned():
1798
+ device_type = DLDeviceType.kDLCUDAHost
1799
+ elif torch_device_type == "cuda":
1800
+ device_type = DLDeviceType.kDLCUDA
1801
+ elif torch_device_type == "cpu":
1802
+ device_type = DLDeviceType.kDLCPU
1803
+ elif torch_device_type == "xpu":
1804
+ device_type = DLDeviceType.kDLOneAPI
1805
+ elif self.device.type == "privateuse1":
1806
+ device_type = DLDeviceType.kDLExtDev
1807
+ elif torch_device_type == "xla":
1808
+ import torch_xla
1809
+
1810
+ if (
1811
+ len(torch_xla.real_devices()) <= 0
1812
+ or "cuda" not in torch_xla.real_devices()[0].lower()
1813
+ ):
1814
+ raise ValueError(f"Unknown device type {torch_device_type} for Dlpack")
1815
+
1816
+ device_type = DLDeviceType.kDLCUDA
1817
+ elif torch_device_type == "mps":
1818
+ device_type = DLDeviceType.kDLMetal
1819
+ else:
1820
+ raise ValueError(f"Unknown device type {torch_device_type} for Dlpack")
1821
+ return (device_type, idx)
1822
+
1823
+ __module__ = "torch"
1824
+
1825
+
1826
+ def _convert(ret, cls):
1827
+ if cls is Tensor:
1828
+ return ret
1829
+
1830
+ if isinstance(ret, Tensor) and not isinstance(ret, cls):
1831
+ ret = ret.as_subclass(cls)
1832
+
1833
+ if isinstance(ret, (tuple, list)):
1834
+ # Also handles things like namedtuples
1835
+ ret = type(ret)(_convert(r, cls) for r in ret)
1836
+
1837
+ return ret
lib/python3.12/site-packages/torch/_tensor_docs.py ADDED
The diff for this file is too large to render. See raw diff
 
lib/python3.12/site-packages/torch/_tensor_str.py ADDED
@@ -0,0 +1,722 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # mypy: allow-untyped-defs
2
+ import contextlib
3
+ import dataclasses
4
+ import math
5
+ import textwrap
6
+ from typing import Any, Optional
7
+
8
+ import torch
9
+ from torch import inf
10
+
11
+
12
+ @dataclasses.dataclass
13
+ class __PrinterOptions:
14
+ precision: int = 4
15
+ threshold: float = 1000
16
+ edgeitems: int = 3
17
+ linewidth: int = 80
18
+ sci_mode: Optional[bool] = None
19
+
20
+
21
+ PRINT_OPTS = __PrinterOptions()
22
+
23
+
24
+ # We could use **kwargs, but this will give better docs
25
+ def set_printoptions(
26
+ precision=None,
27
+ threshold=None,
28
+ edgeitems=None,
29
+ linewidth=None,
30
+ profile=None,
31
+ sci_mode=None,
32
+ ):
33
+ r"""Set options for printing. Items shamelessly taken from NumPy
34
+
35
+ Args:
36
+ precision: Number of digits of precision for floating point output
37
+ (default = 4).
38
+ threshold: Total number of array elements which trigger summarization
39
+ rather than full `repr` (default = 1000).
40
+ edgeitems: Number of array items in summary at beginning and end of
41
+ each dimension (default = 3).
42
+ linewidth: The number of characters per line for the purpose of
43
+ inserting line breaks (default = 80). Thresholded matrices will
44
+ ignore this parameter.
45
+ profile: Sane defaults for pretty printing. Can override with any of
46
+ the above options. (any one of `default`, `short`, `full`)
47
+ sci_mode: Enable (True) or disable (False) scientific notation. If
48
+ None (default) is specified, the value is defined by
49
+ `torch._tensor_str._Formatter`. This value is automatically chosen
50
+ by the framework.
51
+
52
+ Example::
53
+
54
+ >>> # Limit the precision of elements
55
+ >>> torch.set_printoptions(precision=2)
56
+ >>> torch.tensor([1.12345])
57
+ tensor([1.12])
58
+ >>> # Limit the number of elements shown
59
+ >>> torch.set_printoptions(threshold=5)
60
+ >>> torch.arange(10)
61
+ tensor([0, 1, 2, ..., 7, 8, 9])
62
+ >>> # Restore defaults
63
+ >>> torch.set_printoptions(profile='default')
64
+ >>> torch.tensor([1.12345])
65
+ tensor([1.1235])
66
+ >>> torch.arange(10)
67
+ tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
68
+
69
+ """
70
+ if profile is not None:
71
+ if profile == "default":
72
+ PRINT_OPTS.precision = 4
73
+ PRINT_OPTS.threshold = 1000
74
+ PRINT_OPTS.edgeitems = 3
75
+ PRINT_OPTS.linewidth = 80
76
+ elif profile == "short":
77
+ PRINT_OPTS.precision = 2
78
+ PRINT_OPTS.threshold = 1000
79
+ PRINT_OPTS.edgeitems = 2
80
+ PRINT_OPTS.linewidth = 80
81
+ elif profile == "full":
82
+ PRINT_OPTS.precision = 4
83
+ PRINT_OPTS.threshold = inf
84
+ PRINT_OPTS.edgeitems = 3
85
+ PRINT_OPTS.linewidth = 80
86
+
87
+ if precision is not None:
88
+ PRINT_OPTS.precision = precision
89
+ if threshold is not None:
90
+ PRINT_OPTS.threshold = threshold
91
+ if edgeitems is not None:
92
+ PRINT_OPTS.edgeitems = edgeitems
93
+ if linewidth is not None:
94
+ PRINT_OPTS.linewidth = linewidth
95
+ PRINT_OPTS.sci_mode = sci_mode
96
+
97
+
98
+ def get_printoptions() -> dict[str, Any]:
99
+ r"""Gets the current options for printing, as a dictionary that
100
+ can be passed as ``**kwargs`` to set_printoptions().
101
+ """
102
+ return dataclasses.asdict(PRINT_OPTS)
103
+
104
+
105
+ @contextlib.contextmanager
106
+ def printoptions(**kwargs):
107
+ r"""Context manager that temporarily changes the print options. Accepted
108
+ arguments are same as :func:`set_printoptions`."""
109
+ old_kwargs = get_printoptions()
110
+ set_printoptions(**kwargs)
111
+ try:
112
+ yield
113
+ finally:
114
+ set_printoptions(**old_kwargs)
115
+
116
+
117
+ def tensor_totype(t):
118
+ dtype = (
119
+ torch.float
120
+ if (
121
+ t.is_mps
122
+ or (t.is_xpu and not torch.xpu.get_device_properties(t.device).has_fp64)
123
+ or t.is_maia
124
+ )
125
+ else torch.double
126
+ )
127
+ return t.to(dtype=dtype)
128
+
129
+
130
+ class _Formatter:
131
+ def __init__(self, tensor):
132
+ self.floating_dtype = tensor.dtype.is_floating_point
133
+ self.int_mode = True
134
+ self.sci_mode = False
135
+ self.max_width = 1
136
+
137
+ with torch.no_grad():
138
+ tensor_view = tensor.reshape(-1)
139
+
140
+ if not self.floating_dtype:
141
+ for value in tensor_view:
142
+ value_str = f"{value}"
143
+ self.max_width = max(self.max_width, len(value_str))
144
+
145
+ else:
146
+ if tensor.dtype == torch.float4_e2m1fn_x2: # type: ignore[attr-defined]
147
+ # torch.float4_e2m1fn_x2 is special and does not support the casts necessary
148
+ # to print it, we choose to display the uint8 representation here for
149
+ # convenience of being able to print a tensor.
150
+ # TODO(#146647): extend this to other dtypes without casts defined, such
151
+ # as the bits, uint1..7 and int1..7 dtypes.
152
+ tensor_view = tensor_view.view(torch.uint8)
153
+
154
+ nonzero_finite_vals = torch.masked_select(
155
+ tensor_view, torch.isfinite(tensor_view) & tensor_view.ne(0)
156
+ )
157
+
158
+ if nonzero_finite_vals.numel() == 0:
159
+ # no valid number, do nothing
160
+ return
161
+
162
+ if tensor.dtype == torch.float8_e8m0fnu: # type: ignore[attr-defined]
163
+ # float8_e8m0fnu is special and does not define arithmetic ops,
164
+ # and printing code further in this file assumes the existence
165
+ # of various arithmetic ops to figure out what to print. We hack
166
+ # and convert to float here to make printing work correctly.
167
+ # TODO(#113663): also add the other float8 dtypes here after arithmetic
168
+ # support for them is removed
169
+ nonzero_finite_vals = nonzero_finite_vals.float()
170
+
171
+ # Convert to double (or float) for easy calculation. HalfTensor overflows with 1e8, and there's no div() on CPU.
172
+ nonzero_finite_abs = tensor_totype(nonzero_finite_vals.abs())
173
+ nonzero_finite_min = tensor_totype(nonzero_finite_abs.min())
174
+ nonzero_finite_max = tensor_totype(nonzero_finite_abs.max())
175
+
176
+ for value in nonzero_finite_vals:
177
+ if value != torch.ceil(value):
178
+ self.int_mode = False
179
+ break
180
+
181
+ self.sci_mode = (
182
+ nonzero_finite_max / nonzero_finite_min > 1000.0
183
+ or nonzero_finite_max > 1.0e8
184
+ or nonzero_finite_min < 1.0e-4
185
+ if PRINT_OPTS.sci_mode is None
186
+ else PRINT_OPTS.sci_mode
187
+ )
188
+
189
+ if self.int_mode:
190
+ # in int_mode for floats, all numbers are integers, and we append a decimal to nonfinites
191
+ # to indicate that the tensor is of floating type. add 1 to the len to account for this.
192
+ if self.sci_mode:
193
+ for value in nonzero_finite_vals:
194
+ value_str = f"{{:.{PRINT_OPTS.precision}e}}".format(value)
195
+ self.max_width = max(self.max_width, len(value_str))
196
+ else:
197
+ for value in nonzero_finite_vals:
198
+ value_str = f"{value:.0f}"
199
+ self.max_width = max(self.max_width, len(value_str) + 1)
200
+ else:
201
+ # Check if scientific representation should be used.
202
+ if self.sci_mode:
203
+ for value in nonzero_finite_vals:
204
+ value_str = f"{{:.{PRINT_OPTS.precision}e}}".format(value)
205
+ self.max_width = max(self.max_width, len(value_str))
206
+ else:
207
+ for value in nonzero_finite_vals:
208
+ value_str = f"{{:.{PRINT_OPTS.precision}f}}".format(value)
209
+ self.max_width = max(self.max_width, len(value_str))
210
+
211
+ def width(self):
212
+ return self.max_width
213
+
214
+ def format(self, value):
215
+ if self.floating_dtype:
216
+ if self.sci_mode:
217
+ ret = f"{{:{self.max_width}.{PRINT_OPTS.precision}e}}".format(value)
218
+ elif self.int_mode:
219
+ ret = f"{value:.0f}"
220
+ if not (math.isinf(value) or math.isnan(value)):
221
+ ret += "."
222
+ else:
223
+ ret = f"{{:.{PRINT_OPTS.precision}f}}".format(value)
224
+ else:
225
+ ret = f"{value}"
226
+ return (self.max_width - len(ret)) * " " + ret
227
+
228
+
229
+ def _scalar_str(self, formatter1, formatter2=None):
230
+ if formatter2 is not None:
231
+ real_str = _scalar_str(self.real, formatter1)
232
+ imag_str = (_scalar_str(self.imag, formatter2) + "j").lstrip()
233
+ # handles negative numbers, +0.0, -0.0
234
+ if imag_str[0] == "+" or imag_str[0] == "-":
235
+ return real_str + imag_str
236
+ else:
237
+ return real_str + "+" + imag_str
238
+ else:
239
+ return formatter1.format(self.item())
240
+
241
+
242
+ def _vector_str(self, indent, summarize, formatter1, formatter2=None):
243
+ # length includes spaces and comma between elements
244
+ element_length = formatter1.width() + 2
245
+ if formatter2 is not None:
246
+ # width for imag_formatter + an extra j for complex
247
+ element_length += formatter2.width() + 1
248
+
249
+ elements_per_line = max(
250
+ 1, int(math.floor((PRINT_OPTS.linewidth - indent) / (element_length)))
251
+ )
252
+
253
+ def _val_formatter(val, formatter1=formatter1, formatter2=formatter2):
254
+ if formatter2 is not None:
255
+ real_str = formatter1.format(val.real)
256
+ imag_str = (formatter2.format(val.imag) + "j").lstrip()
257
+ # handles negative numbers, +0.0, -0.0
258
+ if imag_str[0] == "+" or imag_str[0] == "-":
259
+ return real_str + imag_str
260
+ else:
261
+ return real_str + "+" + imag_str
262
+ else:
263
+ return formatter1.format(val)
264
+
265
+ if self.dtype == torch.float4_e2m1fn_x2: # type: ignore[attr-defined]
266
+ # torch.float4_e2m1fn_x2 is special and does not support the casts necessary
267
+ # to print it, we choose to display the uint8 representation here for
268
+ # convenience of being able to print a tensor.
269
+ # TODO(#146647): extend this to other dtypes without casts defined, such
270
+ # as the bits, uint1..7 and int1..7 dtypes.
271
+ self = self.view(torch.uint8)
272
+
273
+ if summarize and not PRINT_OPTS.edgeitems:
274
+ # Deal with edge case that negative zero is zero
275
+ data = ["..."]
276
+ elif summarize and self.size(0) > 2 * PRINT_OPTS.edgeitems:
277
+ data = (
278
+ [_val_formatter(val) for val in self[: PRINT_OPTS.edgeitems].tolist()]
279
+ + [" ..."]
280
+ + [_val_formatter(val) for val in self[-PRINT_OPTS.edgeitems :].tolist()]
281
+ )
282
+ else:
283
+ data = [_val_formatter(val) for val in self.tolist()]
284
+
285
+ data_lines = [
286
+ data[i : i + elements_per_line] for i in range(0, len(data), elements_per_line)
287
+ ]
288
+ lines = [", ".join(line) for line in data_lines]
289
+ return "[" + ("," + "\n" + " " * (indent + 1)).join(lines) + "]"
290
+
291
+
292
+ # formatter2 is only used for printing complex tensors.
293
+ # For complex tensors, formatter1 and formatter2 are the formatters for tensor.real
294
+ # and tensor.imag respesectively
295
+ def _tensor_str_with_formatter(self, indent, summarize, formatter1, formatter2=None):
296
+ dim = self.dim()
297
+
298
+ if dim == 0:
299
+ return _scalar_str(self, formatter1, formatter2)
300
+
301
+ if dim == 1:
302
+ return _vector_str(self, indent, summarize, formatter1, formatter2)
303
+
304
+ if summarize and self.size(0) > 2 * PRINT_OPTS.edgeitems:
305
+ slices = (
306
+ [
307
+ _tensor_str_with_formatter(
308
+ self[i], indent + 1, summarize, formatter1, formatter2
309
+ )
310
+ for i in range(0, PRINT_OPTS.edgeitems)
311
+ ]
312
+ + ["..."]
313
+ + [
314
+ _tensor_str_with_formatter(
315
+ self[i], indent + 1, summarize, formatter1, formatter2
316
+ )
317
+ for i in range(len(self) - PRINT_OPTS.edgeitems, len(self))
318
+ ]
319
+ )
320
+ else:
321
+ slices = [
322
+ _tensor_str_with_formatter(
323
+ self[i], indent + 1, summarize, formatter1, formatter2
324
+ )
325
+ for i in range(0, self.size(0))
326
+ ]
327
+
328
+ tensor_str = ("," + "\n" * (dim - 1) + " " * (indent + 1)).join(slices)
329
+ return "[" + tensor_str + "]"
330
+
331
+
332
+ def _tensor_str(self, indent):
333
+ if self.numel() == 0:
334
+ return "[]"
335
+
336
+ if self.has_names():
337
+ # There are two main codepaths (possibly more) that tensor printing goes through:
338
+ # - tensor data can fit comfortably on screen
339
+ # - tensor data needs to be summarized
340
+ # Some of the codepaths don't fully support named tensors, so we send in
341
+ # an unnamed tensor to the formatting code as a workaround.
342
+ self = self.rename(None)
343
+
344
+ summarize = self.numel() > PRINT_OPTS.threshold
345
+
346
+ if self._is_zerotensor():
347
+ self = self.clone()
348
+
349
+ # handle the negative bit
350
+ if self.is_neg():
351
+ self = self.resolve_neg()
352
+
353
+ # TODO: Remove me when `masked_select` is implemented for FP8
354
+ if self.dtype in [
355
+ torch.float8_e5m2,
356
+ torch.float8_e5m2fnuz,
357
+ torch.float8_e4m3fn,
358
+ torch.float8_e4m3fnuz,
359
+ ]:
360
+ self = self.half()
361
+
362
+ if self.dtype.is_complex:
363
+ # handle the conjugate bit
364
+ self = self.resolve_conj()
365
+ real_formatter = _Formatter(
366
+ get_summarized_data(self.real) if summarize else self.real
367
+ )
368
+ imag_formatter = _Formatter(
369
+ get_summarized_data(self.imag) if summarize else self.imag
370
+ )
371
+ return _tensor_str_with_formatter(
372
+ self, indent, summarize, real_formatter, imag_formatter
373
+ )
374
+ else:
375
+ formatter = _Formatter(get_summarized_data(self) if summarize else self)
376
+ return _tensor_str_with_formatter(self, indent, summarize, formatter)
377
+
378
+
379
+ def _add_suffixes(tensor_str, suffixes, indent, force_newline):
380
+ tensor_strs = [tensor_str]
381
+ last_line_len = len(tensor_str) - tensor_str.rfind("\n") + 1
382
+ for suffix in suffixes:
383
+ suffix_len = len(suffix)
384
+ if force_newline or last_line_len + suffix_len + 2 > PRINT_OPTS.linewidth:
385
+ tensor_strs.append(",\n" + " " * indent + suffix)
386
+ last_line_len = indent + suffix_len
387
+ force_newline = False
388
+ else:
389
+ tensor_strs.append(", " + suffix)
390
+ last_line_len += suffix_len + 2
391
+ tensor_strs.append(")")
392
+ return "".join(tensor_strs)
393
+
394
+
395
+ def get_summarized_data(self):
396
+ dim = self.dim()
397
+ if dim == 0:
398
+ return self
399
+ if dim == 1:
400
+ if self.size(0) > 2 * PRINT_OPTS.edgeitems:
401
+ return torch.cat(
402
+ (self[: PRINT_OPTS.edgeitems], self[-PRINT_OPTS.edgeitems :])
403
+ )
404
+ else:
405
+ return self
406
+ if not PRINT_OPTS.edgeitems:
407
+ return self.new_empty([0] * self.dim())
408
+ elif self.size(0) > 2 * PRINT_OPTS.edgeitems:
409
+ start = [self[i] for i in range(0, PRINT_OPTS.edgeitems)]
410
+ end = [self[i] for i in range(len(self) - PRINT_OPTS.edgeitems, len(self))]
411
+ return torch.stack([get_summarized_data(x) for x in (start + end)])
412
+ else:
413
+ return torch.stack([get_summarized_data(x) for x in self])
414
+
415
+
416
+ def _str_intern(inp, *, tensor_contents=None):
417
+ if torch._C._functorch.is_functorch_wrapped_tensor(inp):
418
+ return _functorch_wrapper_str_intern(inp, tensor_contents=tensor_contents)
419
+ is_plain_tensor = type(inp) is torch.Tensor or type(inp) is torch.nn.Parameter
420
+ if inp.is_nested:
421
+ prefix = "nested_tensor("
422
+ elif is_plain_tensor:
423
+ prefix = "tensor("
424
+ else:
425
+ prefix = f"{type(inp).__name__}("
426
+ indent = len(prefix)
427
+ suffixes = []
428
+ custom_contents_provided = tensor_contents is not None
429
+ if custom_contents_provided:
430
+ tensor_str = tensor_contents
431
+
432
+ # This is used to extract the primal value and thus disable the forward AD
433
+ # within this function.
434
+ # TODO(albanD) This needs to be updated when more than one level is supported
435
+ self, tangent = torch.autograd.forward_ad.unpack_dual(inp)
436
+
437
+ # Note [Print tensor device]:
438
+ # A general logic here is we only print device when it doesn't match
439
+ # the device specified in default tensor type.
440
+ # Currently torch.set_default_tensor_type() only supports CPU/CUDA, thus
441
+ # torch._C._get_default_device() only returns either cpu or cuda.
442
+ # In other cases, we don't have a way to set them as default yet,
443
+ # and we should always print out device for them.
444
+ if (
445
+ self.device.type != torch._C._get_default_device()
446
+ or (
447
+ self.device.type == "cuda"
448
+ and torch.cuda.current_device() != self.device.index
449
+ )
450
+ or (self.device.type == "mps")
451
+ ):
452
+ suffixes.append("device='" + str(self.device) + "'")
453
+
454
+ # Tensor printing performs tensor operations like slice, indexing, etc to make it in a
455
+ # representable format. These operations on ipu/xla/lazy/mtia tensor results in compilations. Hence,
456
+ # to avoid compilations, copying the tensor to cpu before printing.
457
+ if self.device.type in ["xla", "lazy", "ipu", "mtia"]:
458
+ self = self.to("cpu")
459
+
460
+ # TODO: add an API to map real -> complex dtypes
461
+ _default_complex_dtype = (
462
+ torch.cdouble if torch.get_default_dtype() == torch.double else torch.cfloat
463
+ )
464
+ has_default_dtype = self.dtype in (
465
+ torch.get_default_dtype(),
466
+ _default_complex_dtype,
467
+ torch.int64,
468
+ torch.bool,
469
+ )
470
+ if self.is_sparse:
471
+ suffixes.append("size=" + str(tuple(self.shape)))
472
+ from torch._subclasses.fake_tensor import FakeTensor
473
+
474
+ is_meta = self.is_meta or isinstance(self, FakeTensor)
475
+ if not is_meta:
476
+ suffixes.append("nnz=" + str(self._nnz()))
477
+ if not has_default_dtype:
478
+ suffixes.append("dtype=" + str(self.dtype))
479
+ if not custom_contents_provided:
480
+ indices_prefix = "indices=tensor("
481
+ indices = self._indices().detach()
482
+ if is_meta:
483
+ indices_str = "..."
484
+ else:
485
+ indices_str = _tensor_str(indices, indent + len(indices_prefix))
486
+ if is_meta or indices.numel() == 0:
487
+ indices_str += ", size=" + str(tuple(indices.shape))
488
+ values_prefix = "values=tensor("
489
+ values = self._values().detach()
490
+ if is_meta:
491
+ values_str = "..."
492
+ else:
493
+ values_str = _tensor_str(values, indent + len(values_prefix))
494
+ if is_meta or values.numel() == 0:
495
+ values_str += ", size=" + str(tuple(values.shape))
496
+ tensor_str = (
497
+ indices_prefix
498
+ + indices_str
499
+ + "),\n"
500
+ + " " * indent
501
+ + values_prefix
502
+ + values_str
503
+ + ")"
504
+ )
505
+ elif self.layout in {
506
+ torch.sparse_csr,
507
+ torch.sparse_csc,
508
+ torch.sparse_bsr,
509
+ torch.sparse_bsc,
510
+ }:
511
+ from torch._subclasses.fake_tensor import FakeTensor
512
+
513
+ suffixes.append("size=" + str(tuple(self.shape)))
514
+ is_meta = self.is_meta or isinstance(self, FakeTensor)
515
+ if not is_meta:
516
+ suffixes.append("nnz=" + str(self._nnz()))
517
+ if not has_default_dtype:
518
+ suffixes.append("dtype=" + str(self.dtype))
519
+ if not custom_contents_provided:
520
+ compressed_indices_method, plain_indices_method = {
521
+ torch.sparse_csr: (torch.Tensor.crow_indices, torch.Tensor.col_indices),
522
+ torch.sparse_csc: (torch.Tensor.ccol_indices, torch.Tensor.row_indices),
523
+ torch.sparse_bsr: (torch.Tensor.crow_indices, torch.Tensor.col_indices),
524
+ torch.sparse_bsc: (torch.Tensor.ccol_indices, torch.Tensor.row_indices),
525
+ }[self.layout]
526
+ if self.layout in {torch.sparse_csr, torch.sparse_bsr}:
527
+ cdimname, pdimname = "row", "column"
528
+ else:
529
+ cdimname, pdimname = "column", "row"
530
+ compressed_indices_prefix = f"c{cdimname[:3]}_indices=tensor("
531
+ compressed_indices = compressed_indices_method(self).detach()
532
+ if is_meta:
533
+ compressed_indices_str = "..."
534
+ else:
535
+ compressed_indices_str = _tensor_str(
536
+ compressed_indices, indent + len(compressed_indices_prefix)
537
+ )
538
+ if compressed_indices.numel() == 0 or is_meta:
539
+ compressed_indices_str += ", size=" + str(
540
+ tuple(compressed_indices.shape)
541
+ )
542
+ plain_indices_prefix = f"{pdimname[:3]}_indices=tensor("
543
+ plain_indices = plain_indices_method(self).detach()
544
+ if is_meta:
545
+ plain_indices_str = "..."
546
+ else:
547
+ plain_indices_str = _tensor_str(
548
+ plain_indices, indent + len(plain_indices_prefix)
549
+ )
550
+ if plain_indices.numel() == 0 or is_meta:
551
+ plain_indices_str += ", size=" + str(tuple(plain_indices.shape))
552
+ values_prefix = "values=tensor("
553
+ values = self.values().detach()
554
+ if is_meta:
555
+ values_str = "..."
556
+ else:
557
+ values_str = _tensor_str(values, indent + len(values_prefix))
558
+ if values.numel() == 0 or is_meta:
559
+ values_str += ", size=" + str(tuple(values.shape))
560
+ tensor_str = (
561
+ compressed_indices_prefix
562
+ + compressed_indices_str
563
+ + "),\n"
564
+ + " " * indent
565
+ + plain_indices_prefix
566
+ + plain_indices_str
567
+ + "),\n"
568
+ + " " * indent
569
+ + values_prefix
570
+ + values_str
571
+ + ")"
572
+ )
573
+ elif self.is_quantized:
574
+ suffixes.append("size=" + str(tuple(self.shape)))
575
+ if not has_default_dtype:
576
+ suffixes.append("dtype=" + str(self.dtype))
577
+ suffixes.append("quantization_scheme=" + str(self.qscheme()))
578
+ if (
579
+ self.qscheme() == torch.per_tensor_affine
580
+ or self.qscheme() == torch.per_tensor_symmetric
581
+ ):
582
+ suffixes.append("scale=" + str(self.q_scale()))
583
+ suffixes.append("zero_point=" + str(self.q_zero_point()))
584
+ elif (
585
+ self.qscheme() == torch.per_channel_affine
586
+ or self.qscheme() == torch.per_channel_symmetric
587
+ or self.qscheme() == torch.per_channel_affine_float_qparams
588
+ ):
589
+ suffixes.append("scale=" + str(self.q_per_channel_scales()))
590
+ suffixes.append("zero_point=" + str(self.q_per_channel_zero_points()))
591
+ suffixes.append("axis=" + str(self.q_per_channel_axis()))
592
+ if not custom_contents_provided:
593
+ tensor_str = _tensor_str(self.dequantize(), indent)
594
+ elif self.is_nested:
595
+ if not custom_contents_provided:
596
+
597
+ def indented_str(s, indent):
598
+ return "\n".join(f" {line}" for line in s.split("\n"))
599
+
600
+ strs = ",\n".join(
601
+ indented_str(str(t), indent + 1)
602
+ for t in torch.ops.aten.unbind.int(self, 0)
603
+ )
604
+ tensor_str = f"[\n{strs}\n]"
605
+ elif torch._is_functional_tensor(self):
606
+ prefix = "_to_functional_tensor("
607
+ tensor_str = repr(torch._from_functional_tensor(self))
608
+ else:
609
+ # Circular import problem, so we import it here
610
+ from torch._subclasses.fake_tensor import FakeTensor
611
+
612
+ if self.is_meta or isinstance(self, FakeTensor):
613
+ suffixes.append("size=" + str(tuple(self.shape)))
614
+ if self.dtype != torch.get_default_dtype():
615
+ suffixes.append("dtype=" + str(self.dtype))
616
+ # TODO: This implies that ellipses is valid syntax for allocating
617
+ # a meta tensor or FakeTensor, which it could be, but it isn't right now
618
+ if not custom_contents_provided:
619
+ tensor_str = "..."
620
+ else:
621
+ if self.numel() == 0 and not self.is_sparse:
622
+ # Explicitly print the shape if it is not (0,), to match NumPy behavior
623
+ if self.dim() != 1:
624
+ suffixes.append("size=" + str(tuple(self.shape)))
625
+
626
+ # In an empty tensor, there are no elements to infer if the dtype
627
+ # should be int64, so it must be shown explicitly.
628
+ if self.dtype != torch.get_default_dtype():
629
+ suffixes.append("dtype=" + str(self.dtype))
630
+ if not custom_contents_provided:
631
+ tensor_str = "[]"
632
+ else:
633
+ if not PRINT_OPTS.edgeitems:
634
+ suffixes.append("size=" + str(tuple(self.shape)))
635
+
636
+ if not has_default_dtype:
637
+ suffixes.append("dtype=" + str(self.dtype))
638
+
639
+ if not custom_contents_provided:
640
+ if self.layout != torch.strided:
641
+ tensor_str = _tensor_str(self.to_dense(), indent)
642
+ else:
643
+ tensor_str = _tensor_str(self, indent)
644
+
645
+ if self.layout != torch.strided:
646
+ suffixes.append("layout=" + str(self.layout))
647
+
648
+ # Use inp here to get the original grad_fn and not the one generated by the forward grad
649
+ # unpacking.
650
+ grad_fn_name = None
651
+ try:
652
+ grad_fn = inp.grad_fn
653
+ except RuntimeError:
654
+ # Accessing the grad_fn calls rebasing logic which would cause an error
655
+ # if that tensor is a view created in no-grad mode modified in-place in
656
+ # no-grad mode. See: https://github.com/pytorch/pytorch/issues/99968
657
+ grad_fn_name = "Invalid"
658
+
659
+ if grad_fn_name is None and grad_fn is not None: # type: ignore[possibly-undefined]
660
+ grad_fn_name = type(grad_fn).__name__
661
+ if grad_fn_name == "CppFunction":
662
+ grad_fn_name = grad_fn.name().rsplit("::", 1)[-1]
663
+
664
+ if grad_fn_name is not None:
665
+ suffixes.append(f"grad_fn=<{grad_fn_name}>")
666
+ elif inp.requires_grad:
667
+ suffixes.append("requires_grad=True")
668
+
669
+ if self.has_names():
670
+ suffixes.append(f"names={self.names}")
671
+
672
+ if tangent is not None:
673
+ suffixes.append(f"tangent={tangent}")
674
+
675
+ string_repr = _add_suffixes(
676
+ prefix + tensor_str, # type: ignore[possibly-undefined]
677
+ suffixes,
678
+ indent,
679
+ force_newline=self.is_sparse,
680
+ )
681
+
682
+ # Check if this instance is flagged as a parameter and change the repr accordingly.
683
+ # Unfortunately, this function has to be aware of this detail.
684
+ # NB: This is currently skipped for plain tensor parameters to maintain BC. In the future,
685
+ # this should be done for those as well to produce a valid repr.
686
+ if isinstance(self, torch.nn.Parameter) and not is_plain_tensor:
687
+ string_repr = f"Parameter({string_repr})"
688
+
689
+ return string_repr
690
+
691
+
692
+ def _functorch_wrapper_str_intern(tensor, *, tensor_contents=None):
693
+ level = torch._C._functorch.maybe_get_level(tensor)
694
+ assert level != -1
695
+
696
+ if torch._C._functorch.is_functionaltensor(tensor):
697
+ # Since we're unwrapping the FunctionalTensorWrapper, we need to make sure
698
+ # that it's up to date first
699
+ torch._sync(tensor)
700
+
701
+ value = torch._C._functorch.get_unwrapped(tensor)
702
+ value_repr = repr(value)
703
+
704
+ indented_value_repr = textwrap.indent(value_repr, " " * 4)
705
+ if torch._C._functorch.is_batchedtensor(tensor):
706
+ bdim = torch._C._functorch.maybe_get_bdim(tensor)
707
+ assert bdim != -1
708
+ return (
709
+ f"BatchedTensor(lvl={level}, bdim={bdim}, value=\n{indented_value_repr}\n)"
710
+ )
711
+ if torch._C._functorch.is_gradtrackingtensor(tensor):
712
+ return f"GradTrackingTensor(lvl={level}, value=\n{indented_value_repr}\n)"
713
+ if torch._C._functorch.is_functionaltensor(tensor):
714
+ return f"FunctionalTensor(lvl={level}, value=\\\n{value_repr})"
715
+
716
+ raise ValueError("We don't know how to print this, please file us an issue")
717
+
718
+
719
+ def _str(self, *, tensor_contents=None):
720
+ with torch.no_grad(), torch.utils._python_dispatch._disable_current_modes():
721
+ guard = torch._C._DisableFuncTorch() # noqa: F841
722
+ return _str_intern(self, tensor_contents=tensor_contents)
lib/python3.12/site-packages/torch/_thread_safe_fork.py ADDED
File without changes
lib/python3.12/site-packages/torch/_utils_internal.py ADDED
@@ -0,0 +1,367 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # mypy: allow-untyped-defs
2
+ import functools
3
+ import logging
4
+ import os
5
+ import sys
6
+ import tempfile
7
+ import typing_extensions
8
+ from typing import Any, Callable, Optional, TypeVar
9
+ from typing_extensions import ParamSpec
10
+
11
+ import torch
12
+ from torch._strobelight.compile_time_profiler import StrobelightCompileTimeProfiler
13
+
14
+
15
+ _T = TypeVar("_T")
16
+ _P = ParamSpec("_P")
17
+
18
+ log = logging.getLogger(__name__)
19
+
20
+ if os.environ.get("TORCH_COMPILE_STROBELIGHT", False):
21
+ import shutil
22
+
23
+ if not shutil.which("strobeclient"):
24
+ log.info(
25
+ "TORCH_COMPILE_STROBELIGHT is true, but seems like you are not on a FB machine."
26
+ )
27
+ else:
28
+ log.info("Strobelight profiler is enabled via environment variable")
29
+ StrobelightCompileTimeProfiler.enable()
30
+
31
+ # this arbitrary-looking assortment of functionality is provided here
32
+ # to have a central place for overridable behavior. The motivating
33
+ # use is the FB build environment, where this source file is replaced
34
+ # by an equivalent.
35
+
36
+ if os.path.basename(os.path.dirname(__file__)) == "shared":
37
+ torch_parent = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
38
+ else:
39
+ torch_parent = os.path.dirname(os.path.dirname(__file__))
40
+
41
+
42
+ def get_file_path(*path_components: str) -> str:
43
+ return os.path.join(torch_parent, *path_components)
44
+
45
+
46
+ def get_file_path_2(*path_components: str) -> str:
47
+ return os.path.join(*path_components)
48
+
49
+
50
+ def get_writable_path(path: str) -> str:
51
+ if os.access(path, os.W_OK):
52
+ return path
53
+ return tempfile.mkdtemp(suffix=os.path.basename(path))
54
+
55
+
56
+ def prepare_multiprocessing_environment(path: str) -> None:
57
+ pass
58
+
59
+
60
+ def resolve_library_path(path: str) -> str:
61
+ return os.path.realpath(path)
62
+
63
+
64
+ def throw_abstract_impl_not_imported_error(opname, module, context):
65
+ if module in sys.modules:
66
+ raise NotImplementedError(
67
+ f"{opname}: We could not find the fake impl for this operator. "
68
+ )
69
+ else:
70
+ raise NotImplementedError(
71
+ f"{opname}: We could not find the fake impl for this operator. "
72
+ f"The operator specified that you may need to import the '{module}' "
73
+ f"Python module to load the fake impl. {context}"
74
+ )
75
+
76
+
77
+ # NB! This treats "skip" kwarg specially!!
78
+ def compile_time_strobelight_meta(
79
+ phase_name: str,
80
+ ) -> Callable[[Callable[_P, _T]], Callable[_P, _T]]:
81
+ def compile_time_strobelight_meta_inner(
82
+ function: Callable[_P, _T],
83
+ ) -> Callable[_P, _T]:
84
+ @functools.wraps(function)
85
+ def wrapper_function(*args: _P.args, **kwargs: _P.kwargs) -> _T:
86
+ if "skip" in kwargs and isinstance(skip := kwargs["skip"], int):
87
+ kwargs["skip"] = skip + 1
88
+
89
+ # This is not needed but we have it here to avoid having profile_compile_time
90
+ # in stack traces when profiling is not enabled.
91
+ if not StrobelightCompileTimeProfiler.enabled:
92
+ return function(*args, **kwargs)
93
+
94
+ return StrobelightCompileTimeProfiler.profile_compile_time(
95
+ function, phase_name, *args, **kwargs
96
+ )
97
+
98
+ return wrapper_function
99
+
100
+ return compile_time_strobelight_meta_inner
101
+
102
+
103
+ # Meta only, see
104
+ # https://www.internalfb.com/intern/wiki/ML_Workflow_Observability/User_Guides/Adding_instrumentation_to_your_code/
105
+ #
106
+ # This will cause an event to get logged to Scuba via the signposts API. You
107
+ # can view samples on the API at https://fburl.com/scuba/workflow_signpost/zh9wmpqs
108
+ # we log to subsystem "torch", and the category and name you provide here.
109
+ # Each of the arguments translate into a Scuba column. We're still figuring
110
+ # out local conventions in PyTorch, but category should be something like
111
+ # "dynamo" or "inductor", and name should be a specific string describing what
112
+ # kind of event happened.
113
+ #
114
+ # Killswitch is at
115
+ # https://www.internalfb.com/intern/justknobs/?name=pytorch%2Fsignpost#event
116
+ def signpost_event(category: str, name: str, parameters: dict[str, Any]):
117
+ log.info("%s %s: %r", category, name, parameters)
118
+
119
+
120
+ def add_mlhub_insight(category: str, insight: str, insight_description: str):
121
+ pass
122
+
123
+
124
+ def log_compilation_event(metrics):
125
+ log.info("%s", metrics)
126
+
127
+
128
+ def upload_graph(graph):
129
+ pass
130
+
131
+
132
+ def set_pytorch_distributed_envs_from_justknobs():
133
+ pass
134
+
135
+
136
+ def log_export_usage(**kwargs):
137
+ pass
138
+
139
+
140
+ def log_draft_export_usage(**kwargs):
141
+ pass
142
+
143
+
144
+ def log_trace_structured_event(*args, **kwargs) -> None:
145
+ pass
146
+
147
+
148
+ def log_cache_bypass(*args, **kwargs) -> None:
149
+ pass
150
+
151
+
152
+ def log_torchscript_usage(api: str, **kwargs):
153
+ _ = api
154
+ return
155
+
156
+
157
+ def check_if_torch_exportable():
158
+ return False
159
+
160
+
161
+ def export_training_ir_rollout_check() -> bool:
162
+ return True
163
+
164
+
165
+ def full_aoti_runtime_assert() -> bool:
166
+ return True
167
+
168
+
169
+ def log_torch_jit_trace_exportability(
170
+ api: str,
171
+ type_of_export: str,
172
+ export_outcome: str,
173
+ result: str,
174
+ ):
175
+ _, _, _, _ = api, type_of_export, export_outcome, result
176
+ return
177
+
178
+
179
+ def justknobs_check(name: str, default: bool = True) -> bool:
180
+ """
181
+ This function can be used to killswitch functionality in FB prod,
182
+ where you can toggle this value to False in JK without having to
183
+ do a code push. In OSS, we always have everything turned on all
184
+ the time, because downstream users can simply choose to not update
185
+ PyTorch. (If more fine-grained enable/disable is needed, we could
186
+ potentially have a map we lookup name in to toggle behavior. But
187
+ the point is that it's all tied to source code in OSS, since there's
188
+ no live server to query.)
189
+
190
+ This is the bare minimum functionality I needed to do some killswitches.
191
+ We have a more detailed plan at
192
+ https://docs.google.com/document/d/1Ukerh9_42SeGh89J-tGtecpHBPwGlkQ043pddkKb3PU/edit
193
+ In particular, in some circumstances it may be necessary to read in
194
+ a knob once at process start, and then use it consistently for the
195
+ rest of the process. Future functionality will codify these patterns
196
+ into a better high level API.
197
+
198
+ WARNING: Do NOT call this function at module import time, JK is not
199
+ fork safe and you will break anyone who forks the process and then
200
+ hits JK again.
201
+ """
202
+ return default
203
+
204
+
205
+ def justknobs_getval_int(name: str) -> int:
206
+ """
207
+ Read warning on justknobs_check
208
+ """
209
+ return 0
210
+
211
+
212
+ def is_fb_unit_test() -> bool:
213
+ return False
214
+
215
+
216
+ @functools.cache
217
+ def max_clock_rate():
218
+ """
219
+ unit: MHz
220
+ """
221
+ if not torch.version.hip:
222
+ from triton.testing import nvsmi
223
+
224
+ return nvsmi(["clocks.max.sm"])[0]
225
+ else:
226
+ # Manually set max-clock speeds on ROCm until equivalent nvmsi
227
+ # functionality in triton.testing or via pyamdsmi enablement. Required
228
+ # for test_snode_runtime unit tests.
229
+ gcn_arch = str(torch.cuda.get_device_properties(0).gcnArchName.split(":", 1)[0])
230
+ if "gfx94" in gcn_arch:
231
+ return 1700
232
+ elif "gfx90a" in gcn_arch:
233
+ return 1700
234
+ elif "gfx908" in gcn_arch:
235
+ return 1502
236
+ elif "gfx12" in gcn_arch:
237
+ return 1700
238
+ elif "gfx11" in gcn_arch:
239
+ return 1700
240
+ elif "gfx103" in gcn_arch:
241
+ return 1967
242
+ elif "gfx101" in gcn_arch:
243
+ return 1144
244
+ elif "gfx95" in gcn_arch:
245
+ return 1700 # TODO: placeholder, get actual value
246
+ else:
247
+ return 1100
248
+
249
+
250
+ def get_mast_job_name_version() -> Optional[tuple[str, int]]:
251
+ return None
252
+
253
+
254
+ TEST_MASTER_ADDR = "127.0.0.1"
255
+ TEST_MASTER_PORT = 29500
256
+ # USE_GLOBAL_DEPS controls whether __init__.py tries to load
257
+ # libtorch_global_deps, see Note [Global dependencies]
258
+ USE_GLOBAL_DEPS = True
259
+ # USE_RTLD_GLOBAL_WITH_LIBTORCH controls whether __init__.py tries to load
260
+ # _C.so with RTLD_GLOBAL during the call to dlopen.
261
+ USE_RTLD_GLOBAL_WITH_LIBTORCH = False
262
+ # If an op was defined in C++ and extended from Python using the
263
+ # torch.library.register_fake, returns if we require that there be a
264
+ # m.set_python_module("mylib.ops") call from C++ that associates
265
+ # the C++ op with a python module.
266
+ REQUIRES_SET_PYTHON_MODULE = False
267
+
268
+
269
+ def maybe_upload_prof_stats_to_manifold(profile_path: str) -> Optional[str]:
270
+ print("Uploading profile stats (fb-only otherwise no-op)")
271
+ return None
272
+
273
+
274
+ def log_chromium_event_internal(
275
+ event: dict[str, Any],
276
+ stack: list[str],
277
+ logger_uuid: str,
278
+ start_time_ns: int,
279
+ ):
280
+ return None
281
+
282
+
283
+ def record_chromium_event_internal(
284
+ event: dict[str, Any],
285
+ ):
286
+ return None
287
+
288
+
289
+ def profiler_allow_cudagraph_cupti_lazy_reinit_cuda12():
290
+ return True
291
+
292
+
293
+ def deprecated():
294
+ """
295
+ When we deprecate a function that might still be in use, we make it internal
296
+ by adding a leading underscore. This decorator is used with a private function,
297
+ and creates a public alias without the leading underscore, but has a deprecation
298
+ warning. This tells users "THIS FUNCTION IS DEPRECATED, please use something else"
299
+ without breaking them, however, if they still really really want to use the
300
+ deprecated function without the warning, they can do so by using the internal
301
+ function name.
302
+ """
303
+
304
+ def decorator(func: Callable[_P, _T]) -> Callable[_P, _T]:
305
+ # Validate naming convention – single leading underscore, not dunder
306
+ if not (func.__name__.startswith("_")):
307
+ raise ValueError(
308
+ "@deprecate must decorate a function whose name "
309
+ "starts with a single leading underscore (e.g. '_foo') as the api should be considered internal for deprecation."
310
+ )
311
+
312
+ public_name = func.__name__[1:] # drop exactly one leading underscore
313
+ module = sys.modules[func.__module__]
314
+
315
+ # Don't clobber an existing symbol accidentally.
316
+ if hasattr(module, public_name):
317
+ raise RuntimeError(
318
+ f"Cannot create alias '{public_name}' -> symbol already exists in {module.__name__}. \
319
+ Please rename it or consult a pytorch developer on what to do"
320
+ )
321
+
322
+ warning_msg = f"{func.__name__[1:]} is DEPRECATED, please consider using an alternative API(s). "
323
+
324
+ # public deprecated alias
325
+ alias = typing_extensions.deprecated(
326
+ warning_msg, category=UserWarning, stacklevel=1
327
+ )(func)
328
+
329
+ alias.__name__ = public_name
330
+
331
+ # Adjust qualname if nested inside a class or another function
332
+ if "." in func.__qualname__:
333
+ alias.__qualname__ = func.__qualname__.rsplit(".", 1)[0] + "." + public_name
334
+ else:
335
+ alias.__qualname__ = public_name
336
+
337
+ setattr(module, public_name, alias)
338
+
339
+ return func
340
+
341
+ return decorator
342
+
343
+
344
+ def get_default_numa_options():
345
+ """
346
+ When using elastic agent, if no numa options are provided, we will use these
347
+ as the default.
348
+
349
+ For external use cases, we return None, i.e. no numa binding. If you would like
350
+ to use torch's automatic numa binding capabilities, you should provide
351
+ NumaOptions to your launch config directly or use the numa binding option
352
+ available in torchrun.
353
+
354
+ Must return None or NumaOptions, but not specifying to avoid circular import.
355
+ """
356
+ return None
357
+
358
+
359
+ def log_triton_builds(fail: Optional[str]):
360
+ pass
361
+
362
+
363
+ def find_compile_subproc_binary() -> Optional[str]:
364
+ """
365
+ Allows overriding the binary used for subprocesses
366
+ """
367
+ return None
lib/python3.12/site-packages/torch/_weights_only_unpickler.py ADDED
@@ -0,0 +1,576 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # mypy: allow-untyped-defs
2
+ # Unpickler restricted to loading only state dicts
3
+ # Restrict constructing types to a list defined in _get_allowed_globals()
4
+ # Restrict BUILD operation to `Tensor`, `Parameter` and `OrderedDict` types only
5
+ # Restrict APPEND/APPENDS to `list`
6
+ # In `GLOBALS` operation do not do class lookup by name, but rather rely on dictionary
7
+ # defined by `_get_allowed_globals()` method, that contains:
8
+ # - torch types (Storage, dtypes, Tensor, `torch.Size`),
9
+ # - `torch._utils._rebuild` functions.
10
+ # - `torch.nn.Parameter`
11
+ # - `collections.Counter`
12
+ # - `collections.OrderedDict`
13
+ # Additionally, users can use an allowlist for adding classes they have deemed as safe using
14
+ # `_add_safe_globals()` (`torch.serialization.add_safe_globals`)
15
+ # `_clear_safe_globals()` (`torch.serialization.clear_safe_globals`)
16
+ # `_get_safe_globals()` (`torch.serialization.get_safe_globals`)
17
+
18
+ # Based of https://github.com/python/cpython/blob/main/Lib/pickle.py
19
+ # Expected to be useful for loading PyTorch model weights
20
+ # For example:
21
+ # data = urllib.request.urlopen('https://download.pytorch.org/models/resnet50-0676ba61.pth').read()
22
+ # buf = io.BytesIO(data)
23
+ # weights = torch.load(buf, weights_only = True)
24
+
25
+ import functools as _functools
26
+ import warnings
27
+
28
+ from _codecs import encode
29
+ from collections import Counter, OrderedDict
30
+ from pickle import (
31
+ APPEND,
32
+ APPENDS,
33
+ BINFLOAT,
34
+ BINGET,
35
+ BININT,
36
+ BININT1,
37
+ BININT2,
38
+ BINPERSID,
39
+ BINPUT,
40
+ BINUNICODE,
41
+ BUILD,
42
+ bytes_types,
43
+ decode_long,
44
+ EMPTY_DICT,
45
+ EMPTY_LIST,
46
+ EMPTY_SET,
47
+ EMPTY_TUPLE,
48
+ GLOBAL,
49
+ LONG1,
50
+ LONG_BINGET,
51
+ LONG_BINPUT,
52
+ MARK,
53
+ NEWFALSE,
54
+ NEWOBJ,
55
+ NEWTRUE,
56
+ NONE,
57
+ PROTO,
58
+ REDUCE,
59
+ SETITEM,
60
+ SETITEMS,
61
+ SHORT_BINSTRING,
62
+ STOP,
63
+ TUPLE,
64
+ TUPLE1,
65
+ TUPLE2,
66
+ TUPLE3,
67
+ UnpicklingError,
68
+ )
69
+ from struct import unpack
70
+ from sys import maxsize
71
+ from typing import Any, Callable, Union
72
+
73
+ import torch
74
+ from torch._utils import _sparse_tensors_to_validate, IMPORT_MAPPING, NAME_MAPPING
75
+
76
+
77
+ # modules in this list are never allowed, even if the user attempts to allowlist
78
+ # functions/classes from them
79
+ _blocklisted_modules = [
80
+ "sys",
81
+ "os",
82
+ "posix",
83
+ "nt",
84
+ ]
85
+
86
+ _marked_safe_globals_set: set[Union[Callable, tuple[Callable, str]]] = set()
87
+
88
+
89
+ def _add_safe_globals(safe_globals: list[Union[Callable, tuple[Callable, str]]]):
90
+ global _marked_safe_globals_set
91
+ _marked_safe_globals_set = _marked_safe_globals_set.union(set(safe_globals))
92
+
93
+
94
+ def _get_safe_globals() -> list[Union[Callable, tuple[Callable, str]]]:
95
+ global _marked_safe_globals_set
96
+ return list(_marked_safe_globals_set)
97
+
98
+
99
+ def _clear_safe_globals():
100
+ global _marked_safe_globals_set
101
+ _marked_safe_globals_set = set()
102
+
103
+
104
+ def _remove_safe_globals(
105
+ globals_to_remove: list[Union[Callable, tuple[Callable, str]]],
106
+ ):
107
+ global _marked_safe_globals_set
108
+ _marked_safe_globals_set = _marked_safe_globals_set - set(globals_to_remove)
109
+
110
+
111
+ class _safe_globals:
112
+ def __init__(self, safe_globals: list[Union[Callable, tuple[Callable, str]]]):
113
+ self.safe_globals = safe_globals
114
+
115
+ def __enter__(self):
116
+ _add_safe_globals(self.safe_globals)
117
+
118
+ def __exit__(self, type, value, tb):
119
+ _remove_safe_globals(self.safe_globals)
120
+
121
+
122
+ # Separate from _get_allowed_globals because of the lru_cache on _get_allowed_globals
123
+ # For example if user had a script like
124
+ # torch.load(file_a)
125
+ # torch.serialization._add_safe_globals([torch.foo])
126
+ # torch.load(file_b)
127
+ # the dynamic additions to safe_globals would not be picked up by
128
+ # _get_allowed_globals due to the lru_cache
129
+ def _get_user_allowed_globals():
130
+ rc: dict[str, Any] = {}
131
+ for f in _marked_safe_globals_set:
132
+ if isinstance(f, tuple):
133
+ if len(f) != 2:
134
+ raise ValueError(
135
+ f"Expected tuple of length 2 (global, str of callable full path), but got tuple of length: {len(f)}"
136
+ )
137
+ if type(f[1]) is not str:
138
+ raise TypeError(
139
+ f"Expected second item in tuple to be str of callable full path, but got: {type(f[1])}"
140
+ )
141
+ f, name = f
142
+ rc[name] = f
143
+ else:
144
+ module, name = f.__module__, f.__qualname__
145
+ rc[f"{module}.{name}"] = f
146
+ return rc
147
+
148
+
149
+ def _tensor_rebuild_functions():
150
+ return {
151
+ torch._utils._rebuild_parameter,
152
+ torch._utils._rebuild_parameter_with_state,
153
+ torch._utils._rebuild_qtensor,
154
+ torch._utils._rebuild_tensor,
155
+ torch._utils._rebuild_tensor_v2,
156
+ torch._utils._rebuild_tensor_v3,
157
+ torch._utils._rebuild_sparse_tensor,
158
+ torch._utils._rebuild_meta_tensor_no_storage,
159
+ torch._utils._rebuild_nested_tensor,
160
+ torch._utils._rebuild_wrapper_subclass,
161
+ # Allowlisting this, but not allowlisting the numpy functions by default
162
+ # Reasoning is that we don't have control over the numpy functions, but
163
+ # this utility is provided by pytorch
164
+ torch._utils._rebuild_device_tensor_from_numpy,
165
+ # In 2.6, we should no longer have a dependency on numpy and the above
166
+ # _rebuild_device_tensor_from_numpy function.
167
+ torch._utils._rebuild_device_tensor_from_cpu_tensor,
168
+ }
169
+
170
+
171
+ # Unpickling machinery
172
+ @_functools.lru_cache(maxsize=1)
173
+ def _get_allowed_globals():
174
+ rc: dict[str, Any] = {
175
+ "collections.OrderedDict": OrderedDict,
176
+ "collections.Counter": Counter,
177
+ "torch.nn.parameter.Parameter": torch.nn.Parameter,
178
+ "torch.serialization._get_layout": torch.serialization._get_layout,
179
+ "torch.Size": torch.Size,
180
+ "torch.Tensor": torch.Tensor,
181
+ "torch.device": torch.device,
182
+ "_codecs.encode": encode, # for bytes
183
+ "builtins.bytearray": bytearray, # for bytearray
184
+ "builtins.set": set, # for set
185
+ "builtins.complex": complex, # for complex
186
+ }
187
+
188
+ # dtype
189
+ for t in torch.storage._dtype_to_storage_type_map().keys():
190
+ rc[str(t)] = t
191
+ for t in torch.storage._new_dtypes():
192
+ rc[str(t)] = t
193
+ for t in [getattr(torch, f"uint{x}") for x in range(1, 8)]:
194
+ rc[str(t)] = t
195
+ for t in [getattr(torch, f"int{x}") for x in range(1, 8)]:
196
+ rc[str(t)] = t
197
+
198
+ # Tensor classes
199
+ for tt in torch._tensor_classes:
200
+ rc[f"{tt.__module__}.{tt.__name__}"] = tt
201
+ # Storage classes
202
+ for ts in torch._storage_classes:
203
+ if ts not in (torch.storage.TypedStorage, torch.storage.UntypedStorage):
204
+ # Wrap legacy storage types in a dummy class
205
+ rc[f"{ts.__module__}.{ts.__name__}"] = torch.serialization.StorageType(
206
+ ts.__name__
207
+ )
208
+ else:
209
+ rc[f"{ts.__module__}.{ts.__name__}"] = ts
210
+ # Quantization specific
211
+ for qt in [
212
+ torch.per_tensor_affine,
213
+ torch.per_tensor_symmetric,
214
+ torch.per_channel_affine,
215
+ torch.per_channel_symmetric,
216
+ torch.per_channel_affine_float_qparams,
217
+ ]:
218
+ rc[str(qt)] = qt
219
+ # Rebuild functions
220
+ for f in _tensor_rebuild_functions():
221
+ rc[f"torch._utils.{f.__name__}"] = f
222
+
223
+ # Handles Tensor Subclasses, Tensor's with attributes.
224
+ # NOTE: It calls into above rebuild functions for regular Tensor types.
225
+ rc["torch._tensor._rebuild_from_type_v2"] = torch._tensor._rebuild_from_type_v2
226
+ return rc
227
+
228
+
229
+ def _read_global_instruction(readline: Callable) -> tuple[str, str]:
230
+ module = readline()[:-1].decode("utf-8")
231
+ name = readline()[:-1].decode("utf-8")
232
+ # Patch since torch.save default protocol is 2
233
+ # users will be running this code in python > 3
234
+ if (module, name) in NAME_MAPPING:
235
+ module, name = NAME_MAPPING[(module, name)]
236
+ elif module in IMPORT_MAPPING:
237
+ module = IMPORT_MAPPING[module]
238
+ return module, name
239
+
240
+
241
+ def get_globals_in_pkl(file) -> set[str]:
242
+ globals_in_checkpoint = set()
243
+ read = file.read
244
+ readline = file.readline
245
+ op_to_bytes_to_read = {
246
+ NEWOBJ[0]: 0,
247
+ REDUCE[0]: 0,
248
+ BUILD[0]: 0,
249
+ APPEND[0]: 0,
250
+ APPENDS[0]: 0,
251
+ SETITEM[0]: 0,
252
+ SETITEMS[0]: 0,
253
+ MARK[0]: 0,
254
+ TUPLE[0]: 0,
255
+ TUPLE1[0]: 0,
256
+ TUPLE2[0]: 0,
257
+ TUPLE3[0]: 0,
258
+ NONE[0]: 0,
259
+ NEWFALSE[0]: 0,
260
+ NEWTRUE[0]: 0,
261
+ EMPTY_TUPLE[0]: 0,
262
+ EMPTY_LIST[0]: 0,
263
+ EMPTY_DICT[0]: 0,
264
+ EMPTY_SET[0]: 0,
265
+ BINPERSID[0]: 0,
266
+ BININT[0]: 4,
267
+ BININT1[0]: 1,
268
+ BININT2[0]: 2,
269
+ BINFLOAT[0]: 8,
270
+ BINGET[0]: 1,
271
+ LONG_BINGET[0]: 4,
272
+ BINPUT[0]: 1,
273
+ LONG_BINPUT[0]: 4,
274
+ }
275
+ while True:
276
+ key = read(1)
277
+ if not key:
278
+ raise EOFError
279
+ assert isinstance(key, bytes_types)
280
+ if key[0] == GLOBAL[0]:
281
+ module, name = _read_global_instruction(readline)
282
+ globals_in_checkpoint.add(f"{module}.{name}")
283
+ elif key[0] in op_to_bytes_to_read:
284
+ bytes_to_read = op_to_bytes_to_read[key[0]]
285
+ if bytes_to_read:
286
+ read(bytes_to_read)
287
+ # ops where bytes to read depends on the data
288
+ elif key[0] == BINUNICODE[0]:
289
+ strlen = unpack("<I", read(4))[0]
290
+ if strlen > maxsize:
291
+ raise UnpicklingError("String is too long")
292
+ read(strlen)
293
+ elif key[0] in {SHORT_BINSTRING[0], LONG1[0]}:
294
+ strlen = read(1)[0]
295
+ read(strlen)
296
+ # first and last op
297
+ elif key[0] == PROTO[0]:
298
+ read(1)[0]
299
+ elif key[0] == STOP[0]:
300
+ return globals_in_checkpoint
301
+ else:
302
+ raise UnpicklingError(f"Unsupported operand {key[0]}")
303
+
304
+
305
+ class Unpickler:
306
+ def __init__(self, file, *, encoding: str = "bytes"):
307
+ self.encoding = encoding
308
+ self.readline = file.readline
309
+ self.read = file.read
310
+ self.memo: dict[int, Any] = {}
311
+ self.proto: int = -1
312
+
313
+ def load(self):
314
+ """Read a pickled object representation from the open file.
315
+
316
+ Return the reconstituted object hierarchy specified in the file.
317
+ """
318
+ self.metastack = []
319
+ self.stack: list[Any] = []
320
+ self.append = self.stack.append
321
+ read = self.read
322
+ while True:
323
+ key = read(1)
324
+ if not key:
325
+ raise EOFError
326
+ assert isinstance(key, bytes_types)
327
+ # Risky operators
328
+ if key[0] == GLOBAL[0]:
329
+ module, name = _read_global_instruction(self.readline)
330
+ full_path = f"{module}.{name}"
331
+ if module in _blocklisted_modules:
332
+ raise UnpicklingError(
333
+ f"Trying to load unsupported GLOBAL {full_path} whose module {module} is blocked."
334
+ )
335
+ if full_path in _get_allowed_globals():
336
+ self.append(_get_allowed_globals()[full_path])
337
+ elif full_path in _get_user_allowed_globals():
338
+ self.append(_get_user_allowed_globals()[full_path])
339
+ elif full_path in (
340
+ [
341
+ "torch.nested._internal.nested_tensor.NestedTensor",
342
+ "torch.nested._internal.nested_tensor._rebuild_njt",
343
+ "torch._dynamo.decorators._DimRange",
344
+ ]
345
+ ):
346
+ raise UnpicklingError(
347
+ "``torch.nested`` and ``torch._dynamo`` must be imported to load nested jagged tensors (NJTs)"
348
+ )
349
+ elif full_path in (
350
+ [
351
+ "torch.distributed.device_mesh.DeviceMesh",
352
+ "torch.distributed.tensor._dtensor_spec.DTensorSpec",
353
+ "torch.distributed.tensor._dtensor_spec.TensorMeta",
354
+ "torch.distributed.tensor.DTensor",
355
+ "torch.distributed.tensor.placement_types.Partial",
356
+ "torch.distributed.tensor.placement_types.Replicate",
357
+ "torch.distributed.tensor.placement_types.Shard",
358
+ ]
359
+ ):
360
+ raise UnpicklingError(
361
+ "``torch.distributed.tensor`` must be imported to load DTensors"
362
+ )
363
+ else:
364
+ builtins_name = "builtins"
365
+ if (
366
+ builtins_name in full_path
367
+ and builtins_name == full_path[: len(builtins_name)]
368
+ ):
369
+ full_path = full_path[len(builtins_name) :]
370
+ full_path = (
371
+ full_path[1:]
372
+ if len(full_path) > 0 and full_path[0] == "."
373
+ else builtins_name + full_path
374
+ )
375
+ raise UnpicklingError(
376
+ f"Unsupported global: GLOBAL {full_path} was not an allowed global by default. "
377
+ f"Please use `torch.serialization.add_safe_globals([{full_path}])` or the "
378
+ f"`torch.serialization.safe_globals([{full_path}])` context manager to allowlist this global "
379
+ "if you trust this class/function."
380
+ )
381
+ elif key[0] == NEWOBJ[0]:
382
+ args = self.stack.pop()
383
+ cls = self.stack.pop()
384
+ if cls is torch.nn.Parameter:
385
+ self.append(torch.nn.Parameter(*args))
386
+ elif (
387
+ cls in _get_user_allowed_globals().values()
388
+ or cls in _get_allowed_globals().values()
389
+ ):
390
+ result = cls.__new__(cls, *args)
391
+ if cls in torch._tensor_classes and "sparse" in cls.__module__:
392
+ _sparse_tensors_to_validate.append(result)
393
+ self.append(result)
394
+ else:
395
+ raise UnpicklingError(
396
+ "Can only create new object for nn.Parameter or classes allowlisted "
397
+ f"via `add_safe_globals` but got {cls}"
398
+ )
399
+ elif key[0] == REDUCE[0]:
400
+ args = self.stack.pop()
401
+ func = self.stack[-1]
402
+ if (
403
+ func not in _get_allowed_globals().values()
404
+ and func not in _get_user_allowed_globals().values()
405
+ ):
406
+ error_msg = (
407
+ f"Trying to call reduce for unrecognized function {func}"
408
+ )
409
+ if hasattr(func, "__self__"):
410
+ error_msg += f" which belongs to {func.__self__}"
411
+ raise UnpicklingError(error_msg)
412
+ result = func(*args)
413
+ if func in torch._tensor_classes and "sparse" in func.__module__:
414
+ _sparse_tensors_to_validate.append(result)
415
+ self.stack[-1] = result
416
+ elif key[0] == BUILD[0]:
417
+ state = self.stack.pop()
418
+ inst = self.stack[-1]
419
+ if type(inst) is torch.Tensor:
420
+ # Legacy unpickling
421
+ inst.set_(*state)
422
+ elif type(inst) is torch.nn.Parameter:
423
+ inst.__setstate__(state)
424
+ elif type(inst) is OrderedDict:
425
+ inst.__dict__.update(state)
426
+ elif (
427
+ type(inst) in _get_user_allowed_globals().values()
428
+ or type(inst) in _get_allowed_globals().values()
429
+ ):
430
+ if hasattr(inst, "__setstate__"):
431
+ inst.__setstate__(state)
432
+ else:
433
+ # mimics load_build in pickle
434
+ # https://github.com/python/cpython/blob/f0c6fccd08904787a39269367f09f263d496114c/Lib/pickle.py#L1854-L1867
435
+ slotstate = None
436
+ if isinstance(state, tuple) and len(state) == 2:
437
+ state, slotstate = state
438
+ if state:
439
+ inst.__dict__.update(state)
440
+ if slotstate:
441
+ for k, v in slotstate.items():
442
+ setattr(inst, k, v)
443
+ else:
444
+ raise UnpicklingError(
445
+ "Can only build Tensor, Parameter, OrderedDict or types allowlisted "
446
+ f"via `add_safe_globals`, but got {type(inst)}"
447
+ )
448
+ # Stack manipulation
449
+ elif key[0] == APPEND[0]:
450
+ item = self.stack.pop()
451
+ list_obj = self.stack[-1]
452
+ if type(list_obj) is not list:
453
+ raise UnpicklingError(
454
+ f"Can only append to lists, but got {type(list_obj)}"
455
+ )
456
+ list_obj.append(item)
457
+ elif key[0] == APPENDS[0]:
458
+ items = self.pop_mark()
459
+ list_obj = self.stack[-1]
460
+ if type(list_obj) is not list:
461
+ raise UnpicklingError(
462
+ f"Can only extend lists, but got {type(list_obj)}"
463
+ )
464
+ list_obj.extend(items)
465
+ elif key[0] == SETITEM[0]:
466
+ (v, k) = (self.stack.pop(), self.stack.pop())
467
+ self.stack[-1][k] = v
468
+ elif key[0] == SETITEMS[0]:
469
+ items = self.pop_mark()
470
+ for i in range(0, len(items), 2):
471
+ self.stack[-1][items[i]] = items[i + 1]
472
+ elif key[0] == MARK[0]:
473
+ self.metastack.append(self.stack)
474
+ self.stack = []
475
+ self.append = self.stack.append
476
+ elif key[0] == TUPLE[0]:
477
+ items = self.pop_mark()
478
+ self.append(tuple(items))
479
+ elif key[0] == TUPLE1[0]:
480
+ self.stack[-1] = (self.stack[-1],)
481
+ elif key[0] == TUPLE2[0]:
482
+ self.stack[-2:] = [(self.stack[-2], self.stack[-1])]
483
+ elif key[0] == TUPLE3[0]:
484
+ self.stack[-3:] = [(self.stack[-3], self.stack[-2], self.stack[-1])]
485
+ # Basic types construction
486
+ elif key[0] == NONE[0]:
487
+ self.append(None)
488
+ elif key[0] == NEWFALSE[0]:
489
+ self.append(False)
490
+ elif key[0] == NEWTRUE[0]:
491
+ self.append(True)
492
+ elif key[0] == EMPTY_TUPLE[0]:
493
+ self.append(())
494
+ elif key[0] == EMPTY_LIST[0]:
495
+ self.append([])
496
+ elif key[0] == EMPTY_DICT[0]:
497
+ self.append({})
498
+ elif key[0] == EMPTY_SET[0]:
499
+ self.append(set())
500
+ elif key[0] == BININT[0]:
501
+ self.append(unpack("<i", read(4))[0])
502
+ elif key[0] == BININT1[0]:
503
+ self.append(self.read(1)[0])
504
+ elif key[0] == BININT2[0]:
505
+ self.append(unpack("<H", read(2))[0])
506
+ elif key[0] == BINFLOAT[0]:
507
+ self.append(unpack(">d", self.read(8))[0])
508
+ elif key[0] == BINUNICODE[0]:
509
+ strlen = unpack("<I", read(4))[0]
510
+ if strlen > maxsize:
511
+ raise UnpicklingError("String is too long")
512
+ strval = str(read(strlen), "utf-8", "surrogatepass")
513
+ self.append(strval)
514
+ elif key[0] == SHORT_BINSTRING[0]:
515
+ strlen = read(1)[0]
516
+ strdata = read(strlen)
517
+ if self.encoding != "bytes":
518
+ strdata = strdata.decode(self.encoding, "strict")
519
+ self.append(strdata)
520
+ elif key[0] == BINPERSID[0]:
521
+ pid = self.stack.pop()
522
+ # Only allow persistent load of storage
523
+ if type(pid) is not tuple and type(pid) is not int:
524
+ raise UnpicklingError(
525
+ f"persistent_load id must be tuple or int, but got {type(pid)}"
526
+ )
527
+ if (
528
+ type(pid) is tuple
529
+ and len(pid) > 0
530
+ and torch.serialization._maybe_decode_ascii(pid[0]) != "storage"
531
+ ):
532
+ raise UnpicklingError(
533
+ f"Only persistent_load of storage is allowed, but got {pid[0]}"
534
+ )
535
+ self.append(self.persistent_load(pid))
536
+ elif key[0] in [BINGET[0], LONG_BINGET[0]]:
537
+ idx = (read(1) if key[0] == BINGET[0] else unpack("<I", read(4)))[0]
538
+ self.append(self.memo[idx])
539
+ elif key[0] in [BINPUT[0], LONG_BINPUT[0]]:
540
+ i = (read(1) if key[0] == BINPUT[0] else unpack("<I", read(4)))[0]
541
+ if i < 0:
542
+ raise ValueError("negative argument")
543
+ self.memo[i] = self.stack[-1]
544
+ elif key[0] == LONG1[0]:
545
+ n = read(1)[0]
546
+ data = read(n)
547
+ self.append(decode_long(data))
548
+ # First and last deserializer ops
549
+ elif key[0] == PROTO[0]:
550
+ self.proto = read(1)[0]
551
+ if self.proto != 2:
552
+ warnings.warn(
553
+ f"Detected pickle protocol {self.proto} in the checkpoint, which was "
554
+ "not the default pickle protocol used by `torch.load` (2). The weights_only "
555
+ "Unpickler might not support all instructions implemented by this protocol, "
556
+ "please file an issue for adding support if you encounter this."
557
+ )
558
+ elif key[0] == STOP[0]:
559
+ rc = self.stack.pop()
560
+ return rc
561
+ else:
562
+ raise UnpicklingError(f"Unsupported operand {key[0]}")
563
+
564
+ # Return a list of items pushed in the stack after last MARK instruction.
565
+ def pop_mark(self):
566
+ items = self.stack
567
+ self.stack = self.metastack.pop()
568
+ self.append = self.stack.append
569
+ return items
570
+
571
+ def persistent_load(self, pid):
572
+ raise UnpicklingError("unsupported persistent id encountered")
573
+
574
+
575
+ def load(file, *, encoding: str = "ASCII"):
576
+ return Unpickler(file, encoding=encoding).load()
lib/python3.12/site-packages/torch/accelerator/__init__.py ADDED
@@ -0,0 +1,272 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ r"""
2
+ This package introduces support for the current :ref:`accelerator<accelerators>` in python.
3
+ """
4
+
5
+ from typing import Optional
6
+ from typing_extensions import deprecated
7
+
8
+ import torch
9
+
10
+ from ._utils import _device_t, _get_device_index
11
+ from .memory import (
12
+ empty_cache,
13
+ max_memory_allocated,
14
+ max_memory_reserved,
15
+ memory_allocated,
16
+ memory_reserved,
17
+ memory_stats,
18
+ reset_accumulated_memory_stats,
19
+ reset_peak_memory_stats,
20
+ )
21
+
22
+
23
+ __all__ = [
24
+ "current_accelerator",
25
+ "current_device_idx", # deprecated
26
+ "current_device_index",
27
+ "current_stream",
28
+ "empty_cache",
29
+ "device_count",
30
+ "device_index",
31
+ "is_available",
32
+ "max_memory_allocated",
33
+ "max_memory_reserved",
34
+ "memory_allocated",
35
+ "memory_reserved",
36
+ "memory_stats",
37
+ "reset_accumulated_memory_stats",
38
+ "reset_peak_memory_stats",
39
+ "set_device_idx", # deprecated
40
+ "set_device_index",
41
+ "set_stream",
42
+ "synchronize",
43
+ ]
44
+
45
+
46
+ def device_count() -> int:
47
+ r"""Return the number of current :ref:`accelerator<accelerators>` available.
48
+
49
+ Returns:
50
+ int: the number of the current :ref:`accelerator<accelerators>` available.
51
+ If there is no available accelerators, return 0.
52
+
53
+ .. note:: This API delegates to the device-specific version of `device_count`.
54
+ On CUDA, this API will NOT poison fork if NVML discovery succeeds.
55
+ Otherwise, it will. For more details, see :ref:`multiprocessing-poison-fork-note`.
56
+ """
57
+ acc = current_accelerator()
58
+ if acc is None:
59
+ return 0
60
+
61
+ mod = torch.get_device_module(acc)
62
+ return mod.device_count()
63
+
64
+
65
+ def is_available() -> bool:
66
+ r"""Check if the current accelerator is available at runtime: it was build, all the
67
+ required drivers are available and at least one device is visible.
68
+ See :ref:`accelerator<accelerators>` for details.
69
+
70
+ Returns:
71
+ bool: A boolean indicating if there is an available :ref:`accelerator<accelerators>`.
72
+
73
+ .. note:: This API delegates to the device-specific version of `is_available`.
74
+ On CUDA, when the environment variable ``PYTORCH_NVML_BASED_CUDA_CHECK=1`` is set,
75
+ this function will NOT poison fork. Otherwise, it will. For more details, see
76
+ :ref:`multiprocessing-poison-fork-note`.
77
+
78
+ Example::
79
+
80
+ >>> assert torch.accelerator.is_available() "No available accelerators detected."
81
+ """
82
+ # Why not just check "device_count() > 0" like other is_available call?
83
+ # Because device like CUDA have a python implementation of is_available that is
84
+ # non-poisoning and some features like Dataloader rely on it.
85
+ # So we are careful to delegate to the Python version of the accelerator here
86
+ acc = current_accelerator()
87
+ if acc is None:
88
+ return False
89
+
90
+ mod = torch.get_device_module(acc)
91
+ return mod.is_available()
92
+
93
+
94
+ def current_accelerator(check_available: bool = False) -> Optional[torch.device]:
95
+ r"""Return the device of the accelerator available at compilation time.
96
+ If no accelerator were available at compilation time, returns None.
97
+ See :ref:`accelerator<accelerators>` for details.
98
+
99
+ Args:
100
+ check_available (bool, optional): if True, will also do a runtime check to see
101
+ if the device :func:`torch.accelerator.is_available` on top of the compile-time
102
+ check.
103
+ Default: ``False``
104
+
105
+ Returns:
106
+ torch.device: return the current accelerator as :class:`torch.device`.
107
+
108
+ .. note:: The index of the returned :class:`torch.device` will be ``None``, please use
109
+ :func:`torch.accelerator.current_device_index` to know the current index being used.
110
+ This API does NOT poison fork. For more details, see :ref:`multiprocessing-poison-fork-note`.
111
+
112
+ Example::
113
+
114
+ >>> # xdoctest:
115
+ >>> # If an accelerator is available, sent the model to it
116
+ >>> model = torch.nn.Linear(2, 2)
117
+ >>> if (current_device := current_accelerator(check_available=True)) is not None:
118
+ >>> model.to(current_device)
119
+ """
120
+ if (acc := torch._C._accelerator_getAccelerator()) is not None:
121
+ if (not check_available) or (check_available and is_available()):
122
+ return acc
123
+ return None
124
+
125
+
126
+ def current_device_index() -> int:
127
+ r"""Return the index of a currently selected device for the current :ref:`accelerator<accelerators>`.
128
+
129
+ Returns:
130
+ int: the index of a currently selected device.
131
+ """
132
+ return torch._C._accelerator_getDeviceIndex()
133
+
134
+
135
+ current_device_idx = deprecated(
136
+ "Use `current_device_index` instead.",
137
+ category=FutureWarning,
138
+ )(current_device_index)
139
+
140
+ current_device_idx.__doc__ = r"""
141
+ (Deprecated) Return the index of a currently selected device for the current :ref:`accelerator<accelerators>`.
142
+
143
+ Returns:
144
+ int: the index of a currently selected device.
145
+
146
+ .. warning::
147
+
148
+ :func:`torch.accelerator.current_device_idx` is deprecated in favor of :func:`torch.accelerator.current_device_index`
149
+ and will be removed in a future PyTorch release.
150
+ """
151
+
152
+
153
+ def set_device_index(device: _device_t, /) -> None:
154
+ r"""Set the current device index to a given device.
155
+
156
+ Args:
157
+ device (:class:`torch.device`, str, int): a given device that must match the current
158
+ :ref:`accelerator<accelerators>` device type.
159
+
160
+ .. note:: This function is a no-op if this device index is negative.
161
+ """
162
+ device_index = _get_device_index(device, optional=False)
163
+ torch._C._accelerator_setDeviceIndex(device_index)
164
+
165
+
166
+ set_device_idx = deprecated(
167
+ "Use `set_device_index` instead.",
168
+ category=FutureWarning,
169
+ )(set_device_index)
170
+
171
+ set_device_idx.__doc__ = r"""
172
+ (Deprecated) Set the current device index to a given device.
173
+
174
+ Args:
175
+ device (:class:`torch.device`, str, int): a given device that must match the current
176
+ :ref:`accelerator<accelerators>` device type.
177
+
178
+ .. warning::
179
+
180
+ :func:`torch.accelerator.set_device_idx` is deprecated in favor of :func:`torch.accelerator.set_device_index`
181
+ and will be removed in a future PyTorch release.
182
+ """
183
+
184
+
185
+ def current_stream(device: _device_t = None, /) -> torch.Stream:
186
+ r"""Return the currently selected stream for a given device.
187
+
188
+ Args:
189
+ device (:class:`torch.device`, str, int, optional): a given device that must match the current
190
+ :ref:`accelerator<accelerators>` device type. If not given,
191
+ use :func:`torch.accelerator.current_device_index` by default.
192
+
193
+ Returns:
194
+ torch.Stream: the currently selected stream for a given device.
195
+ """
196
+ device_index = _get_device_index(device, optional=True)
197
+ return torch._C._accelerator_getStream(device_index)
198
+
199
+
200
+ def set_stream(stream: torch.Stream) -> None:
201
+ r"""Set the current stream to a given stream.
202
+
203
+ Args:
204
+ stream (torch.Stream): a given stream that must match the current :ref:`accelerator<accelerators>` device type.
205
+
206
+ .. note:: This function will set the current device index to the device index of the given stream.
207
+ """
208
+ torch._C._accelerator_setStream(stream)
209
+
210
+
211
+ def synchronize(device: _device_t = None, /) -> None:
212
+ r"""Wait for all kernels in all streams on the given device to complete.
213
+
214
+ Args:
215
+ device (:class:`torch.device`, str, int, optional): device for which to synchronize. It must match
216
+ the current :ref:`accelerator<accelerators>` device type. If not given,
217
+ use :func:`torch.accelerator.current_device_index` by default.
218
+
219
+ .. note:: This function is a no-op if the current :ref:`accelerator<accelerators>` is not initialized.
220
+
221
+ Example::
222
+
223
+ >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA)
224
+ >>> assert torch.accelerator.is_available() "No available accelerators detected."
225
+ >>> start_event = torch.Event(enable_timing=True)
226
+ >>> end_event = torch.Event(enable_timing=True)
227
+ >>> start_event.record()
228
+ >>> tensor = torch.randn(100, device=torch.accelerator.current_accelerator())
229
+ >>> sum = torch.sum(tensor)
230
+ >>> end_event.record()
231
+ >>> torch.accelerator.synchronize()
232
+ >>> elapsed_time_ms = start_event.elapsed_time(end_event)
233
+ """
234
+ device_index = _get_device_index(device, optional=True)
235
+ torch._C._accelerator_synchronizeDevice(device_index)
236
+
237
+
238
+ class device_index:
239
+ r"""Context manager to set the current device index for the current :ref:`accelerator<accelerators>`.
240
+ Temporarily changes the current device index to the specified value for the duration
241
+ of the context, and automatically restores the previous device index when exiting
242
+ the context.
243
+
244
+ Args:
245
+ device (Optional[int]): a given device index to temporarily set. If None,
246
+ no device index switching occurs.
247
+
248
+ Examples:
249
+
250
+ >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA)
251
+ >>> # Set device 0 as the current device temporarily
252
+ >>> with torch.accelerator.device_index(0):
253
+ ... # Code here runs with device 0 as the current device
254
+ ... pass
255
+ >>> # Original device is now restored
256
+ >>> # No-op when None is passed
257
+ >>> with torch.accelerator.device_index(None):
258
+ ... # No device switching occurs
259
+ ... pass
260
+ """
261
+
262
+ def __init__(self, device: Optional[int], /) -> None:
263
+ self.idx = device
264
+ self.prev_idx = -1
265
+
266
+ def __enter__(self) -> None:
267
+ if self.idx is not None:
268
+ self.prev_idx = torch._C._accelerator_exchangeDevice(self.idx)
269
+
270
+ def __exit__(self, *exc_info: object) -> None:
271
+ if self.idx is not None:
272
+ torch._C._accelerator_maybeExchangeDevice(self.prev_idx)
lib/python3.12/site-packages/torch/accelerator/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (11.1 kB). View file
 
lib/python3.12/site-packages/torch/accelerator/__pycache__/_utils.cpython-312.pyc ADDED
Binary file (1.48 kB). View file
 
lib/python3.12/site-packages/torch/accelerator/__pycache__/memory.cpython-312.pyc ADDED
Binary file (10.3 kB). View file
 
lib/python3.12/site-packages/torch/accelerator/_utils.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Optional
2
+
3
+ import torch
4
+ from torch.types import Device as _device_t
5
+
6
+
7
+ def _get_device_index(device: _device_t, optional: bool = False) -> int:
8
+ if isinstance(device, int):
9
+ return device
10
+ if isinstance(device, str):
11
+ device = torch.device(device)
12
+ device_index: Optional[int] = None
13
+ if isinstance(device, torch.device):
14
+ acc = torch.accelerator.current_accelerator()
15
+ if acc is None:
16
+ raise RuntimeError("Accelerator expected")
17
+ if acc.type != device.type:
18
+ raise ValueError(
19
+ f"{device.type} doesn't match the current accelerator {acc}."
20
+ )
21
+ device_index = device.index
22
+ if device_index is None:
23
+ if not optional:
24
+ raise ValueError(
25
+ f"Expected a torch.device with a specified index or an integer, but got:{device}"
26
+ )
27
+ return torch.accelerator.current_device_index()
28
+ return device_index
lib/python3.12/site-packages/torch/accelerator/memory.py ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from collections import OrderedDict
2
+ from typing import Any
3
+
4
+ import torch
5
+
6
+ from ._utils import _device_t, _get_device_index
7
+
8
+
9
+ __all__ = [
10
+ "empty_cache",
11
+ "max_memory_allocated",
12
+ "max_memory_reserved",
13
+ "memory_allocated",
14
+ "memory_reserved",
15
+ "memory_stats",
16
+ "reset_accumulated_memory_stats",
17
+ "reset_peak_memory_stats",
18
+ ]
19
+
20
+
21
+ def empty_cache() -> None:
22
+ r"""Release all unoccupied cached memory currently held by the caching
23
+ allocator so that those can be used in other application.
24
+
25
+ .. note:: This function is a no-op if the memory allocator for the current
26
+ :ref:`accelerator <accelerators>` has not been initialized.
27
+ """
28
+ if not torch._C._accelerator_isAllocatorInitialized():
29
+ return
30
+ torch._C._accelerator_emptyCache()
31
+
32
+
33
+ def memory_stats(device_index: _device_t = None, /) -> OrderedDict[str, Any]:
34
+ r"""Return a dictionary of accelerator device memory allocator statistics for a given device index.
35
+
36
+ The return value of this function is a dictionary of statistics, each of
37
+ which is a non-negative integer.
38
+
39
+ Core statistics:
40
+
41
+ - ``"allocated.{all,large_pool,small_pool}.{current,peak,allocated,freed}"``:
42
+ number of allocation requests received by the memory allocator.
43
+ - ``"allocated_bytes.{all,large_pool,small_pool}.{current,peak,allocated,freed}"``:
44
+ amount of allocated memory.
45
+ - ``"segment.{all,large_pool,small_pool}.{current,peak,allocated,freed}"``:
46
+ number of reserved segments from device memory allocation.
47
+ - ``"reserved_bytes.{all,large_pool,small_pool}.{current,peak,allocated,freed}"``:
48
+ amount of reserved memory.
49
+ - ``"active.{all,large_pool,small_pool}.{current,peak,allocated,freed}"``:
50
+ number of active memory blocks.
51
+ - ``"active_bytes.{all,large_pool,small_pool}.{current,peak,allocated,freed}"``:
52
+ amount of active memory.
53
+ - ``"inactive_split.{all,large_pool,small_pool}.{current,peak,allocated,freed}"``:
54
+ number of inactive, non-releasable memory blocks.
55
+ - ``"inactive_split_bytes.{all,large_pool,small_pool}.{current,peak,allocated,freed}"``:
56
+ amount of inactive, non-releasable memory.
57
+
58
+ For these core statistics, values are broken down as follows.
59
+
60
+ Pool type:
61
+
62
+ - ``all``: combined statistics across all memory pools.
63
+ - ``large_pool``: statistics for the large allocation pool
64
+ (as of June 2025, for size >= 1MB allocations).
65
+ - ``small_pool``: statistics for the small allocation pool
66
+ (as of June 2025, for size < 1MB allocations).
67
+
68
+ Metric type:
69
+
70
+ - ``current``: current value of this metric.
71
+ - ``peak``: maximum value of this metric.
72
+ - ``allocated``: historical total increase in this metric.
73
+ - ``freed``: historical total decrease in this metric.
74
+
75
+ In addition to the core statistics, we also provide some simple event
76
+ counters:
77
+
78
+ - ``"num_alloc_retries"``: number of failed device memory allocation calls that
79
+ result in a cache flush and retry.
80
+ - ``"num_ooms"``: number of out-of-memory errors thrown.
81
+ - ``"num_sync_all_streams"``: number of ``synchronize_and_free_events`` calls.
82
+ - ``"num_device_alloc"``: number of device memory allocation calls.
83
+ - ``"num_device_free"``: number of device memory free calls.
84
+
85
+ Args:
86
+ device_index (:class:`torch.device`, str, int, optional): the index of the device to target.
87
+ If not given, use :func:`torch.accelerator.current_device_index` by default.
88
+ If a :class:`torch.device` or str is provided, its type must match the current
89
+ :ref:`accelerator<accelerators>` device type.
90
+ """
91
+ if not torch._C._accelerator_isAllocatorInitialized():
92
+ return OrderedDict()
93
+ device_index = _get_device_index(device_index, optional=True)
94
+ stats = torch._C._accelerator_getDeviceStats(device_index)
95
+ flat_stats = []
96
+
97
+ def flatten(prefix: str, value: Any) -> None:
98
+ if isinstance(value, dict):
99
+ for k, v in value.items():
100
+ nested_prefix = f"{prefix}.{k}" if prefix else k
101
+ flatten(nested_prefix, v)
102
+ else:
103
+ flat_stats.append((prefix, value))
104
+
105
+ flatten("", stats)
106
+ flat_stats.sort()
107
+ return OrderedDict(flat_stats)
108
+
109
+
110
+ def memory_allocated(device_index: _device_t = None, /) -> int:
111
+ r"""Return the current :ref:`accelerator<accelerators>` device memory occupied by tensors
112
+ in bytes for a given device index.
113
+
114
+ Args:
115
+ device_index (:class:`torch.device`, str, int, optional): the index of the device to target.
116
+ If not given, use :func:`torch.accelerator.current_device_index` by default.
117
+ If a :class:`torch.device` or str is provided, its type must match the current
118
+ :ref:`accelerator<accelerators>` device type.
119
+ """
120
+ return memory_stats(device_index).get("allocated_bytes.all.current", 0)
121
+
122
+
123
+ def max_memory_allocated(device_index: _device_t = None, /) -> int:
124
+ r"""Return the current :ref:`accelerator<accelerators>` maximum device memory occupied by tensors
125
+ in bytes for a given device index.
126
+
127
+ By default, this returns the peak allocated memory since the beginning of
128
+ this program. :func:`~torch.accelerator.reset_peak_memory_stats` can be used to
129
+ reset the starting point in tracking this metric.
130
+
131
+ Args:
132
+ device_index (:class:`torch.device`, str, int, optional): the index of the device to target.
133
+ If not given, use :func:`torch.accelerator.current_device_index` by default.
134
+ If a :class:`torch.device` or str is provided, its type must match the current
135
+ :ref:`accelerator<accelerators>` device type.
136
+ """
137
+ return memory_stats(device_index).get("allocated_bytes.all.peak", 0)
138
+
139
+
140
+ def memory_reserved(device_index: _device_t = None, /) -> int:
141
+ r"""Return the current :ref:`accelerator<accelerators>` device memory managed by the caching allocator
142
+ in bytes for a given device index.
143
+
144
+ Args:
145
+ device_index (:class:`torch.device`, str, int, optional): the index of the device to target.
146
+ If not given, use :func:`torch.accelerator.current_device_index` by default.
147
+ If a :class:`torch.device` or str is provided, its type must match the current
148
+ :ref:`accelerator<accelerators>` device type.
149
+ """
150
+ return memory_stats(device_index).get("reserved_bytes.all.current", 0)
151
+
152
+
153
+ def max_memory_reserved(device_index: _device_t = None, /) -> int:
154
+ r"""Return the current :ref:`accelerator<accelerators>` maximum device memory managed by the caching allocator
155
+ in bytes for a given device index.
156
+
157
+ By default, this returns the peak cached memory since the beginning of this
158
+ program. :func:`~torch.accelerator.reset_peak_memory_stats` can be used to reset
159
+ the starting point in tracking this metric.
160
+
161
+ Args:
162
+ device_index (:class:`torch.device`, str, int, optional): the index of the device to target.
163
+ If not given, use :func:`torch.accelerator.current_device_index` by default.
164
+ If a :class:`torch.device` or str is provided, its type must match the current
165
+ :ref:`accelerator<accelerators>` device type.
166
+ """
167
+ return memory_stats(device_index).get("reserved_bytes.all.peak", 0)
168
+
169
+
170
+ def reset_accumulated_memory_stats(device_index: _device_t = None, /) -> None:
171
+ r"""Reset the "accumulated" (historical) stats tracked by the current :ref:`accelerator<accelerators>`
172
+ memory allocator for a given device index.
173
+
174
+ Args:
175
+ device_index (:class:`torch.device`, str, int, optional): the index of the device to target.
176
+ If not given, use :func:`torch.accelerator.current_device_index` by default.
177
+ If a :class:`torch.device` or str is provided, its type must match the current
178
+ :ref:`accelerator<accelerators>` device type.
179
+
180
+ .. note:: This function is a no-op if the memory allocator for the current
181
+ :ref:`accelerator <accelerators>` has not been initialized.
182
+ """
183
+ device_index = _get_device_index(device_index, optional=True)
184
+ return torch._C._accelerator_resetAccumulatedStats(device_index)
185
+
186
+
187
+ def reset_peak_memory_stats(device_index: _device_t = None, /) -> None:
188
+ r"""Reset the "peak" stats tracked by the current :ref:`accelerator<accelerators>`
189
+ memory allocator for a given device index.
190
+
191
+ Args:
192
+ device_index (:class:`torch.device`, str, int, optional): the index of the device to target.
193
+ If not given, use :func:`torch.accelerator.current_device_index` by default.
194
+ If a :class:`torch.device` or str is provided, its type must match the current
195
+ :ref:`accelerator<accelerators>` device type.
196
+
197
+ .. note:: This function is a no-op if the memory allocator for the current
198
+ :ref:`accelerator <accelerators>` has not been initialized.
199
+ """
200
+ device_index = _get_device_index(device_index, optional=True)
201
+ return torch._C._accelerator_resetPeakStats(device_index)
lib/python3.12/site-packages/torch/functional.py ADDED
@@ -0,0 +1,2195 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # mypy: allow-untyped-defs
2
+ import itertools
3
+ import operator
4
+ from collections.abc import Sequence
5
+ from typing import Any, Optional, TYPE_CHECKING, Union
6
+
7
+ import torch
8
+ import torch.nn.functional as F
9
+ from torch import _VF, Tensor
10
+ from torch._C import _add_docstr
11
+ from torch._jit_internal import _overload as overload, boolean_dispatch
12
+ from torch._lowrank import pca_lowrank, svd_lowrank
13
+ from torch.overrides import (
14
+ handle_torch_function,
15
+ has_torch_function,
16
+ has_torch_function_unary,
17
+ has_torch_function_variadic,
18
+ )
19
+
20
+
21
+ __all__ = [
22
+ "atleast_1d",
23
+ "atleast_2d",
24
+ "atleast_3d",
25
+ "align_tensors",
26
+ "broadcast_shapes",
27
+ "broadcast_tensors",
28
+ "cartesian_prod",
29
+ "block_diag",
30
+ "cdist",
31
+ "chain_matmul",
32
+ "einsum",
33
+ "istft",
34
+ "lu",
35
+ "norm",
36
+ "meshgrid",
37
+ "pca_lowrank",
38
+ "split",
39
+ "stft",
40
+ "svd_lowrank",
41
+ "tensordot",
42
+ "unique",
43
+ "unique_consecutive",
44
+ "unravel_index",
45
+ ]
46
+
47
+
48
+ def broadcast_tensors(*tensors):
49
+ r"""broadcast_tensors(*tensors) -> List of Tensors
50
+
51
+ Broadcasts the given tensors according to :ref:`broadcasting-semantics`.
52
+
53
+ Args:
54
+ *tensors: any number of tensors of the same type
55
+
56
+ .. warning::
57
+
58
+ More than one element of a broadcasted tensor may refer to a single
59
+ memory location. As a result, in-place operations (especially ones that
60
+ are vectorized) may result in incorrect behavior. If you need to write
61
+ to the tensors, please clone them first.
62
+
63
+ Example::
64
+
65
+ >>> x = torch.arange(3).view(1, 3)
66
+ >>> y = torch.arange(2).view(2, 1)
67
+ >>> a, b = torch.broadcast_tensors(x, y)
68
+ >>> a.size()
69
+ torch.Size([2, 3])
70
+ >>> a
71
+ tensor([[0, 1, 2],
72
+ [0, 1, 2]])
73
+ """
74
+ # This wrapper exists to support variadic args.
75
+ if has_torch_function(tensors):
76
+ return handle_torch_function(broadcast_tensors, tensors, *tensors)
77
+ return _VF.broadcast_tensors(tensors) # type: ignore[attr-defined]
78
+
79
+
80
+ def broadcast_shapes(*shapes):
81
+ r"""broadcast_shapes(*shapes) -> Size
82
+
83
+ Similar to :func:`broadcast_tensors` but for shapes.
84
+
85
+ This is equivalent to
86
+ ``torch.broadcast_tensors(*map(torch.empty, shapes))[0].shape``
87
+ but avoids the need create to intermediate tensors. This is useful for
88
+ broadcasting tensors of common batch shape but different rightmost shape,
89
+ e.g. to broadcast mean vectors with covariance matrices.
90
+
91
+ Example::
92
+
93
+ >>> torch.broadcast_shapes((2,), (3, 1), (1, 1, 1))
94
+ torch.Size([1, 3, 2])
95
+
96
+ Args:
97
+ \*shapes (torch.Size): Shapes of tensors.
98
+
99
+ Returns:
100
+ shape (torch.Size): A shape compatible with all input shapes.
101
+
102
+ Raises:
103
+ RuntimeError: If shapes are incompatible.
104
+ """
105
+ # This wrapper exists to support variadic args.
106
+ # TODO Move this to C++ once the jit has better support for torch.Size.
107
+ if not torch.jit.is_tracing():
108
+ result = torch._refs._broadcast_shapes(*shapes)
109
+ if result is None:
110
+ return torch.Size([])
111
+ return torch.Size(result)
112
+ else:
113
+ # with implementation above, torch.jit.trace hardcodes the sizes which makes subsequent replays fail
114
+ with torch.no_grad():
115
+ scalar = torch.zeros((), device="cpu")
116
+ tensors = [scalar.expand(shape) for shape in shapes]
117
+ tensors = broadcast_tensors(*tensors)
118
+ return tensors[0].shape
119
+
120
+
121
+ def split(
122
+ tensor: Tensor,
123
+ split_size_or_sections: Union[int, list[int]],
124
+ dim: int = 0,
125
+ ) -> tuple[Tensor, ...]:
126
+ r"""Splits the tensor into chunks. Each chunk is a view of the original tensor.
127
+
128
+ If :attr:`split_size_or_sections` is an integer type, then :attr:`tensor` will
129
+ be split into equally sized chunks (if possible). Last chunk will be smaller if
130
+ the tensor size along the given dimension :attr:`dim` is not divisible by
131
+ :attr:`split_size`.
132
+
133
+ If :attr:`split_size_or_sections` is a list, then :attr:`tensor` will be split
134
+ into ``len(split_size_or_sections)`` chunks with sizes in :attr:`dim` according
135
+ to :attr:`split_size_or_sections`.
136
+
137
+ Args:
138
+ tensor (Tensor): tensor to split.
139
+ split_size_or_sections (int) or (list(int)): size of a single chunk or
140
+ list of sizes for each chunk
141
+ dim (int): dimension along which to split the tensor.
142
+
143
+ Example::
144
+
145
+ >>> a = torch.arange(10).reshape(5, 2)
146
+ >>> a
147
+ tensor([[0, 1],
148
+ [2, 3],
149
+ [4, 5],
150
+ [6, 7],
151
+ [8, 9]])
152
+ >>> torch.split(a, 2)
153
+ (tensor([[0, 1],
154
+ [2, 3]]),
155
+ tensor([[4, 5],
156
+ [6, 7]]),
157
+ tensor([[8, 9]]))
158
+ >>> torch.split(a, [1, 4])
159
+ (tensor([[0, 1]]),
160
+ tensor([[2, 3],
161
+ [4, 5],
162
+ [6, 7],
163
+ [8, 9]]))
164
+ """
165
+ if has_torch_function_unary(tensor):
166
+ return handle_torch_function(
167
+ split, (tensor,), tensor, split_size_or_sections, dim=dim
168
+ )
169
+ # Overwriting reason:
170
+ # This dispatches to two ATen functions depending on the type of
171
+ # split_size_or_sections. The branching code is in _tensor.py, which we
172
+ # call here.
173
+ return tensor.split(split_size_or_sections, dim)
174
+
175
+
176
+ def einsum(*args: Any) -> Tensor:
177
+ r"""einsum(equation, *operands) -> Tensor
178
+
179
+ Sums the product of the elements of the input :attr:`operands` along dimensions specified using a notation
180
+ based on the Einstein summation convention.
181
+
182
+ Einsum allows computing many common multi-dimensional linear algebraic array operations by representing them
183
+ in a short-hand format based on the Einstein summation convention, given by :attr:`equation`. The details of
184
+ this format are described below, but the general idea is to label every dimension of the input :attr:`operands`
185
+ with some subscript and define which subscripts are part of the output. The output is then computed by summing
186
+ the product of the elements of the :attr:`operands` along the dimensions whose subscripts are not part of the
187
+ output. For example, matrix multiplication can be computed using einsum as `torch.einsum("ij,jk->ik", A, B)`.
188
+ Here, j is the summation subscript and i and k the output subscripts (see section below for more details on why).
189
+
190
+ Equation:
191
+
192
+ The :attr:`equation` string specifies the subscripts (letters in `[a-zA-Z]`) for each dimension of
193
+ the input :attr:`operands` in the same order as the dimensions, separating subscripts for each operand by a
194
+ comma (','), e.g. `'ij,jk'` specify subscripts for two 2D operands. The dimensions labeled with the same subscript
195
+ must be broadcastable, that is, their size must either match or be `1`. The exception is if a subscript is
196
+ repeated for the same input operand, in which case the dimensions labeled with this subscript for this operand
197
+ must match in size and the operand will be replaced by its diagonal along these dimensions. The subscripts that
198
+ appear exactly once in the :attr:`equation` will be part of the output, sorted in increasing alphabetical order.
199
+ The output is computed by multiplying the input :attr:`operands` element-wise, with their dimensions aligned based
200
+ on the subscripts, and then summing out the dimensions whose subscripts are not part of the output.
201
+
202
+ Optionally, the output subscripts can be explicitly defined by adding an arrow ('->') at the end of the equation
203
+ followed by the subscripts for the output. For instance, the following equation computes the transpose of a
204
+ matrix multiplication: 'ij,jk->ki'. The output subscripts must appear at least once for some input operand and
205
+ at most once for the output.
206
+
207
+ Ellipsis ('...') can be used in place of subscripts to broadcast the dimensions covered by the ellipsis.
208
+ Each input operand may contain at most one ellipsis which will cover the dimensions not covered by subscripts,
209
+ e.g. for an input operand with 5 dimensions, the ellipsis in the equation `'ab...c'` cover the third and fourth
210
+ dimensions. The ellipsis does not need to cover the same number of dimensions across the :attr:`operands` but the
211
+ 'shape' of the ellipsis (the size of the dimensions covered by them) must broadcast together. If the output is not
212
+ explicitly defined with the arrow ('->') notation, the ellipsis will come first in the output (left-most dimensions),
213
+ before the subscript labels that appear exactly once for the input operands. e.g. the following equation implements
214
+ batch matrix multiplication `'...ij,...jk'`.
215
+
216
+ A few final notes: the equation may contain whitespaces between the different elements (subscripts, ellipsis,
217
+ arrow and comma) but something like `'. . .'` is not valid. An empty string `''` is valid for scalar operands.
218
+
219
+ .. note::
220
+
221
+ ``torch.einsum`` handles ellipsis ('...') differently from NumPy in that it allows dimensions
222
+ covered by the ellipsis to be summed over, that is, ellipsis are not required to be part of the output.
223
+
224
+ .. note::
225
+
226
+ Please install opt-einsum (https://optimized-einsum.readthedocs.io/en/stable/) in order to enroll into a more
227
+ performant einsum. You can install when installing torch like so: `pip install torch[opt-einsum]` or by itself
228
+ with `pip install opt-einsum`.
229
+
230
+ If opt-einsum is available, this function will automatically speed up computation and/or consume less memory
231
+ by optimizing contraction order through our opt_einsum backend :mod:`torch.backends.opt_einsum` (The _ vs - is
232
+ confusing, I know). This optimization occurs when there are at least three inputs, since the order does not matter
233
+ otherwise. Note that finding `the` optimal path is an NP-hard problem, thus, opt-einsum relies on different
234
+ heuristics to achieve near-optimal results. If opt-einsum is not available, the default order is to contract
235
+ from left to right.
236
+
237
+ To bypass this default behavior, add the following to disable opt_einsum and skip path calculation:
238
+ ``torch.backends.opt_einsum.enabled = False``
239
+
240
+ To specify which strategy you'd like for opt_einsum to compute the contraction path, add the following line:
241
+ ``torch.backends.opt_einsum.strategy = 'auto'``. The default strategy is 'auto', and we also support 'greedy' and
242
+ 'optimal'. Disclaimer that the runtime of 'optimal' is factorial in the number of inputs! See more details in
243
+ the opt_einsum documentation (https://optimized-einsum.readthedocs.io/en/stable/path_finding.html).
244
+
245
+ .. note::
246
+
247
+ As of PyTorch 1.10 :func:`torch.einsum` also supports the sublist format (see examples below). In this format,
248
+ subscripts for each operand are specified by sublists, list of integers in the range [0, 52). These sublists
249
+ follow their operands, and an extra sublist can appear at the end of the input to specify the output's
250
+ subscripts., e.g. `torch.einsum(op1, sublist1, op2, sublist2, ..., [subslist_out])`. Python's `Ellipsis` object
251
+ may be provided in a sublist to enable broadcasting as described in the Equation section above.
252
+
253
+ Args:
254
+ equation (str): The subscripts for the Einstein summation.
255
+ operands (List[Tensor]): The tensors to compute the Einstein summation of.
256
+
257
+ Examples::
258
+
259
+ >>> # xdoctest: +IGNORE_WANT("non-deterministic")
260
+ >>> # trace
261
+ >>> torch.einsum('ii', torch.randn(4, 4))
262
+ tensor(-1.2104)
263
+
264
+ >>> # xdoctest: +IGNORE_WANT("non-deterministic")
265
+ >>> # diagonal
266
+ >>> torch.einsum('ii->i', torch.randn(4, 4))
267
+ tensor([-0.1034, 0.7952, -0.2433, 0.4545])
268
+
269
+ >>> # xdoctest: +IGNORE_WANT("non-deterministic")
270
+ >>> # outer product
271
+ >>> x = torch.randn(5)
272
+ >>> y = torch.randn(4)
273
+ >>> torch.einsum('i,j->ij', x, y)
274
+ tensor([[ 0.1156, -0.2897, -0.3918, 0.4963],
275
+ [-0.3744, 0.9381, 1.2685, -1.6070],
276
+ [ 0.7208, -1.8058, -2.4419, 3.0936],
277
+ [ 0.1713, -0.4291, -0.5802, 0.7350],
278
+ [ 0.5704, -1.4290, -1.9323, 2.4480]])
279
+
280
+ >>> # xdoctest: +IGNORE_WANT("non-deterministic")
281
+ >>> # batch matrix multiplication
282
+ >>> As = torch.randn(3, 2, 5)
283
+ >>> Bs = torch.randn(3, 5, 4)
284
+ >>> torch.einsum('bij,bjk->bik', As, Bs)
285
+ tensor([[[-1.0564, -1.5904, 3.2023, 3.1271],
286
+ [-1.6706, -0.8097, -0.8025, -2.1183]],
287
+
288
+ [[ 4.2239, 0.3107, -0.5756, -0.2354],
289
+ [-1.4558, -0.3460, 1.5087, -0.8530]],
290
+
291
+ [[ 2.8153, 1.8787, -4.3839, -1.2112],
292
+ [ 0.3728, -2.1131, 0.0921, 0.8305]]])
293
+
294
+ >>> # xdoctest: +IGNORE_WANT("non-deterministic")
295
+ >>> # with sublist format and ellipsis
296
+ >>> torch.einsum(As, [..., 0, 1], Bs, [..., 1, 2], [..., 0, 2])
297
+ tensor([[[-1.0564, -1.5904, 3.2023, 3.1271],
298
+ [-1.6706, -0.8097, -0.8025, -2.1183]],
299
+
300
+ [[ 4.2239, 0.3107, -0.5756, -0.2354],
301
+ [-1.4558, -0.3460, 1.5087, -0.8530]],
302
+
303
+ [[ 2.8153, 1.8787, -4.3839, -1.2112],
304
+ [ 0.3728, -2.1131, 0.0921, 0.8305]]])
305
+
306
+ >>> # batch permute
307
+ >>> A = torch.randn(2, 3, 4, 5)
308
+ >>> torch.einsum('...ij->...ji', A).shape
309
+ torch.Size([2, 3, 5, 4])
310
+
311
+ >>> # equivalent to torch.nn.functional.bilinear
312
+ >>> A = torch.randn(3, 5, 4)
313
+ >>> l = torch.randn(2, 5)
314
+ >>> r = torch.randn(2, 4)
315
+ >>> torch.einsum('bn,anm,bm->ba', l, A, r)
316
+ tensor([[-0.3430, -5.2405, 0.4494],
317
+ [ 0.3311, 5.5201, -3.0356]])
318
+ """
319
+ import torch.backends.opt_einsum as opt_einsum
320
+
321
+ # This wrapper exists to support variadic args.
322
+ if len(args) < 2:
323
+ raise ValueError(
324
+ "einsum(): must specify the equation string and at least one operand, "
325
+ "or at least one operand and its subscripts list"
326
+ )
327
+
328
+ equation = None
329
+ operands = None
330
+
331
+ if isinstance(args[0], torch.Tensor):
332
+ # Convert the subscript list format which is an interleaving of operand and its subscripts
333
+ # list with an optional output subscripts list at the end (see documentation for more details on this)
334
+ # to the equation string format by creating the equation string from the subscripts list and grouping the
335
+ # input operands into a tensorlist (List[Tensor]).
336
+ def parse_subscript(n: int) -> str:
337
+ if n == Ellipsis:
338
+ return "..."
339
+ if n >= 0 and n < 26:
340
+ return chr(ord("A") + n)
341
+ if n >= 26 and n < 52:
342
+ return chr(ord("a") + n - 26)
343
+ raise ValueError(
344
+ "einsum(): subscript in subscript list is not within the valid range [0, 52)"
345
+ )
346
+
347
+ # Parse subscripts for input operands
348
+ equation = ",".join("".join(parse_subscript(s) for s in l) for l in args[1::2])
349
+
350
+ # Parse optional output subscripts (provided when the number of arguments is odd)
351
+ if len(args) % 2 == 1:
352
+ equation += "->" + "".join(parse_subscript(s) for s in args[-1])
353
+ operands = args[:-1:2]
354
+ else:
355
+ operands = args[::2]
356
+ else:
357
+ equation = args[0]
358
+ operands = args[1:]
359
+
360
+ if has_torch_function(operands):
361
+ return handle_torch_function(einsum, operands, equation, *operands)
362
+
363
+ if len(operands) == 1 and isinstance(operands[0], (list, tuple)):
364
+ # the old interface of passing the operands as one list argument
365
+ _operands = operands[0]
366
+ # recurse in case operands contains value that has torch function
367
+ # in the original implementation this line is omitted
368
+ return einsum(equation, *_operands)
369
+
370
+ if len(operands) <= 2 or not opt_einsum.enabled:
371
+ # the path for contracting 0 or 1 time(s) is already optimized
372
+ # or the user has disabled using opt_einsum
373
+ return _VF.einsum(equation, operands) # type: ignore[attr-defined]
374
+
375
+ path = None
376
+ if opt_einsum.is_available():
377
+ _opt_einsum = opt_einsum.get_opt_einsum()
378
+ tupled_path = _opt_einsum.contract_path(
379
+ equation, *operands, optimize=opt_einsum.strategy
380
+ )[0]
381
+ # flatten path for dispatching to C++
382
+ path = [*itertools.chain.from_iterable(tupled_path)]
383
+ return _VF.einsum(equation, operands, path=path) # type: ignore[attr-defined]
384
+
385
+
386
+ # This wrapper exists to support variadic args.
387
+ if TYPE_CHECKING:
388
+ # The JIT doesn't understand Union, so only add type annotation for mypy
389
+ def meshgrid(
390
+ *tensors: Union[Tensor, list[Tensor]], indexing: Optional[str] = None
391
+ ) -> tuple[Tensor, ...]:
392
+ return _meshgrid(*tensors, indexing=indexing)
393
+
394
+ else:
395
+
396
+ def meshgrid(*tensors, indexing: Optional[str] = None) -> tuple[Tensor, ...]:
397
+ r"""Creates grids of coordinates specified by the 1D inputs in `attr`:tensors.
398
+
399
+ This is helpful when you want to visualize data over some
400
+ range of inputs. See below for a plotting example.
401
+
402
+ Given :math:`N` 1D tensors :math:`T_0 \ldots T_{N-1}` as
403
+ inputs with corresponding sizes :math:`S_0 \ldots S_{N-1}`,
404
+ this creates :math:`N` N-dimensional tensors :math:`G_0 \ldots
405
+ G_{N-1}`, each with shape :math:`(S_0, ..., S_{N-1})` where
406
+ the output :math:`G_i` is constructed by expanding :math:`T_i`
407
+ to the result shape.
408
+
409
+ .. note::
410
+ 0D inputs are treated equivalently to 1D inputs of a
411
+ single element.
412
+
413
+ .. warning::
414
+ `torch.meshgrid(*tensors)` currently has the same behavior
415
+ as calling `numpy.meshgrid(*arrays, indexing='ij')`.
416
+
417
+ In the future `torch.meshgrid` will transition to
418
+ `indexing='xy'` as the default.
419
+
420
+ https://github.com/pytorch/pytorch/issues/50276 tracks
421
+ this issue with the goal of migrating to NumPy's behavior.
422
+
423
+ .. seealso::
424
+
425
+ :func:`torch.cartesian_prod` has the same effect but it
426
+ collects the data in a tensor of vectors.
427
+
428
+ Args:
429
+ tensors (list of Tensor): list of scalars or 1 dimensional tensors. Scalars will be
430
+ treated as tensors of size :math:`(1,)` automatically
431
+
432
+ indexing: (str, optional): the indexing mode, either "xy"
433
+ or "ij", defaults to "ij". See warning for future changes.
434
+
435
+ If "xy" is selected, the first dimension corresponds
436
+ to the cardinality of the second input and the second
437
+ dimension corresponds to the cardinality of the first
438
+ input.
439
+
440
+ If "ij" is selected, the dimensions are in the same
441
+ order as the cardinality of the inputs.
442
+
443
+ Returns:
444
+ seq (sequence of Tensors): If the input has :math:`N`
445
+ tensors of size :math:`S_0 \ldots S_{N-1}``, then the
446
+ output will also have :math:`N` tensors, where each tensor
447
+ is of shape :math:`(S_0, ..., S_{N-1})`.
448
+
449
+ Example::
450
+
451
+ >>> x = torch.tensor([1, 2, 3])
452
+ >>> y = torch.tensor([4, 5, 6])
453
+
454
+ Observe the element-wise pairings across the grid, (1, 4),
455
+ (1, 5), ..., (3, 6). This is the same thing as the
456
+ cartesian product.
457
+ >>> grid_x, grid_y = torch.meshgrid(x, y, indexing='ij')
458
+ >>> grid_x
459
+ tensor([[1, 1, 1],
460
+ [2, 2, 2],
461
+ [3, 3, 3]])
462
+ >>> grid_y
463
+ tensor([[4, 5, 6],
464
+ [4, 5, 6],
465
+ [4, 5, 6]])
466
+
467
+ This correspondence can be seen when these grids are
468
+ stacked properly.
469
+ >>> torch.equal(torch.cat(tuple(torch.dstack([grid_x, grid_y]))),
470
+ ... torch.cartesian_prod(x, y))
471
+ True
472
+
473
+ `torch.meshgrid` is commonly used to produce a grid for
474
+ plotting.
475
+ >>> # xdoctest: +REQUIRES(module:matplotlib)
476
+ >>> # xdoctest: +REQUIRES(env:DOCTEST_SHOW)
477
+ >>> import matplotlib.pyplot as plt
478
+ >>> xs = torch.linspace(-5, 5, steps=100)
479
+ >>> ys = torch.linspace(-5, 5, steps=100)
480
+ >>> x, y = torch.meshgrid(xs, ys, indexing='xy')
481
+ >>> z = torch.sin(torch.sqrt(x * x + y * y))
482
+ >>> ax = plt.axes(projection='3d')
483
+ >>> ax.plot_surface(x.numpy(), y.numpy(), z.numpy())
484
+ >>> plt.show()
485
+
486
+ .. image:: ../_static/img/meshgrid.png
487
+ :width: 512
488
+
489
+ """
490
+ return _meshgrid(*tensors, indexing=indexing)
491
+
492
+
493
+ def _meshgrid(*tensors, indexing: Optional[str]):
494
+ if has_torch_function(tensors):
495
+ return handle_torch_function(meshgrid, tensors, *tensors, indexing=indexing)
496
+ if len(tensors) == 1 and isinstance(tensors[0], (list, tuple)):
497
+ # the old interface of passing the operands as one list argument
498
+ tensors = tensors[0] # type: ignore[assignment]
499
+
500
+ # Continue allowing call of old method that takes no indexing
501
+ # kwarg for forward compatibility reasons.
502
+ #
503
+ # Remove this two weeks after landing.
504
+ kwargs = {} if indexing is None else {"indexing": indexing}
505
+ return _VF.meshgrid(tensors, **kwargs) # type: ignore[attr-defined]
506
+
507
+
508
+ def stft(
509
+ input: Tensor,
510
+ n_fft: int,
511
+ hop_length: Optional[int] = None,
512
+ win_length: Optional[int] = None,
513
+ window: Optional[Tensor] = None,
514
+ center: bool = True,
515
+ pad_mode: str = "reflect",
516
+ normalized: bool = False,
517
+ onesided: Optional[bool] = None,
518
+ return_complex: Optional[bool] = None,
519
+ align_to_window: Optional[bool] = None,
520
+ ) -> Tensor:
521
+ r"""Short-time Fourier transform (STFT).
522
+
523
+ .. warning::
524
+ From version 1.8.0, :attr:`return_complex` must always be given
525
+ explicitly for real inputs and `return_complex=False` has been
526
+ deprecated. Strongly prefer `return_complex=True` as in a future
527
+ pytorch release, this function will only return complex tensors.
528
+
529
+ Note that :func:`torch.view_as_real` can be used to recover a real
530
+ tensor with an extra last dimension for real and imaginary components.
531
+
532
+ .. warning::
533
+ From version 2.1, a warning will be provided if a :attr:`window` is
534
+ not specified. In a future release, this attribute will be required.
535
+ Not providing a window currently defaults to using a rectangular window,
536
+ which may result in undesirable artifacts. Consider using tapered windows,
537
+ such as :func:`torch.hann_window`.
538
+
539
+ The STFT computes the Fourier transform of short overlapping windows of the
540
+ input. This giving frequency components of the signal as they change over
541
+ time. The interface of this function is modeled after (but *not* a drop-in
542
+ replacement for) librosa_ stft function.
543
+
544
+ .. _librosa: https://librosa.org/doc/latest/generated/librosa.stft.html
545
+
546
+ Ignoring the optional batch dimension, this method computes the following
547
+ expression:
548
+
549
+ .. math::
550
+ X[\omega, m] = \sum_{k = 0}^{\text{win\_length-1}}%
551
+ \text{window}[k]\ \text{input}[m \times \text{hop\_length} + k]\ %
552
+ \exp\left(- j \frac{2 \pi \cdot \omega k}{\text{n\_fft}}\right),
553
+
554
+ where :math:`m` is the index of the sliding window, and :math:`\omega` is
555
+ the frequency :math:`0 \leq \omega < \text{n\_fft}` for ``onesided=False``,
556
+ or :math:`0 \leq \omega < \lfloor \text{n\_fft} / 2 \rfloor + 1` for ``onesided=True``.
557
+
558
+ * :attr:`input` must be either a 1-D time sequence or a 2-D batch of time
559
+ sequences.
560
+
561
+ * If :attr:`hop_length` is ``None`` (default), it is treated as equal to
562
+ ``floor(n_fft / 4)``.
563
+
564
+ * If :attr:`win_length` is ``None`` (default), it is treated as equal to
565
+ :attr:`n_fft`.
566
+
567
+ * :attr:`window` can be a 1-D tensor of size :attr:`win_length`, e.g., from
568
+ :meth:`torch.hann_window`. If :attr:`window` is ``None`` (default), it is
569
+ treated as if having :math:`1` everywhere in the window. If
570
+ :math:`\text{win\_length} < \text{n\_fft}`, :attr:`window` will be padded on
571
+ both sides to length :attr:`n_fft` before being applied.
572
+
573
+ * If :attr:`center` is ``True`` (default), :attr:`input` will be padded on
574
+ both sides so that the :math:`t`-th frame is centered at time
575
+ :math:`t \times \text{hop\_length}`. Otherwise, the :math:`t`-th frame
576
+ begins at time :math:`t \times \text{hop\_length}`.
577
+
578
+ * :attr:`pad_mode` determines the padding method used on :attr:`input` when
579
+ :attr:`center` is ``True``. See :meth:`torch.nn.functional.pad` for
580
+ all available options. Default is ``"reflect"``.
581
+
582
+ * If :attr:`onesided` is ``True`` (default for real input), only values for
583
+ :math:`\omega` in :math:`\left[0, 1, 2, \dots, \left\lfloor
584
+ \frac{\text{n\_fft}}{2} \right\rfloor + 1\right]` are returned because
585
+ the real-to-complex Fourier transform satisfies the conjugate symmetry,
586
+ i.e., :math:`X[m, \omega] = X[m, \text{n\_fft} - \omega]^*`.
587
+ Note if the input or window tensors are complex, then :attr:`onesided`
588
+ output is not possible.
589
+
590
+ * If :attr:`normalized` is ``True`` (default is ``False``), the function
591
+ returns the normalized STFT results, i.e., multiplied by :math:`(\text{frame\_length})^{-0.5}`.
592
+
593
+ * If :attr:`return_complex` is ``True`` (default if input is complex), the
594
+ return is a ``input.dim() + 1`` dimensional complex tensor. If ``False``,
595
+ the output is a ``input.dim() + 2`` dimensional real tensor where the last
596
+ dimension represents the real and imaginary components.
597
+
598
+ Returns either a complex tensor of size :math:`(* \times N \times T)` if
599
+ :attr:`return_complex` is true, or a real tensor of size :math:`(* \times N
600
+ \times T \times 2)`. Where :math:`*` is the optional batch size of
601
+ :attr:`input`, :math:`N` is the number of frequencies where STFT is applied
602
+ and :math:`T` is the total number of frames used.
603
+
604
+ .. warning::
605
+ This function changed signature at version 0.4.1. Calling with the
606
+ previous signature may cause error or return incorrect result.
607
+
608
+ Args:
609
+ input (Tensor): the input tensor of shape `(B?, L)` where `B?` is an optional
610
+ batch dimension
611
+ n_fft (int): size of Fourier transform
612
+ hop_length (int, optional): the distance between neighboring sliding window
613
+ frames. Default: ``None`` (treated as equal to ``floor(n_fft / 4)``)
614
+ win_length (int, optional): the size of window frame and STFT filter.
615
+ Default: ``None`` (treated as equal to :attr:`n_fft`)
616
+ window (Tensor, optional): the optional window function.
617
+ Shape must be 1d and `<= n_fft`
618
+ Default: ``None`` (treated as window of all :math:`1` s)
619
+ center (bool, optional): whether to pad :attr:`input` on both sides so
620
+ that the :math:`t`-th frame is centered at time :math:`t \times \text{hop\_length}`.
621
+ Default: ``True``
622
+ pad_mode (str, optional): controls the padding method used when
623
+ :attr:`center` is ``True``. Default: ``"reflect"``
624
+ normalized (bool, optional): controls whether to return the normalized STFT results
625
+ Default: ``False``
626
+ onesided (bool, optional): controls whether to return half of results to
627
+ avoid redundancy for real inputs.
628
+ Default: ``True`` for real :attr:`input` and :attr:`window`, ``False`` otherwise.
629
+ return_complex (bool, optional): whether to return a complex tensor, or
630
+ a real tensor with an extra last dimension for the real and
631
+ imaginary components.
632
+
633
+ .. versionchanged:: 2.0
634
+ ``return_complex`` is now a required argument for real inputs,
635
+ as the default is being transitioned to ``True``.
636
+
637
+ .. deprecated:: 2.0
638
+ ``return_complex=False`` is deprecated, instead use ``return_complex=True``
639
+ Note that calling :func:`torch.view_as_real` on the output will
640
+ recover the deprecated output format.
641
+
642
+ Returns:
643
+ Tensor: A tensor containing the STFT result with shape `(B?, N, T, C?)` where
644
+ - `B?` is an optional batch dimension from the input.
645
+ - `N` is the number of frequency samples, `(n_fft // 2) + 1` for
646
+ `onesided=True`, or otherwise `n_fft`.
647
+ - `T` is the number of frames, `1 + L // hop_length`
648
+ for `center=True`, or `1 + (L - n_fft) // hop_length` otherwise.
649
+ - `C?` is an optional length-2 dimension of real and imaginary
650
+ components, present when `return_complex=False`.
651
+
652
+ """
653
+ if has_torch_function_unary(input):
654
+ return handle_torch_function(
655
+ stft,
656
+ (input,),
657
+ input,
658
+ n_fft,
659
+ hop_length=hop_length,
660
+ win_length=win_length,
661
+ window=window,
662
+ center=center,
663
+ pad_mode=pad_mode,
664
+ normalized=normalized,
665
+ onesided=onesided,
666
+ return_complex=return_complex,
667
+ align_to_window=align_to_window,
668
+ )
669
+ if center and align_to_window is not None:
670
+ raise RuntimeError(
671
+ "stft align_to_window should only be set when center = false"
672
+ )
673
+ # NOTE: Do not edit. This code will be removed once the forward-compatibility
674
+ # period is over for PR #73432
675
+ if center:
676
+ signal_dim = input.dim()
677
+ extended_shape = [1] * (3 - signal_dim) + list(input.size())
678
+ pad = int(n_fft // 2)
679
+ input = F.pad(input.view(extended_shape), [pad, pad], pad_mode)
680
+ input = input.view(input.shape[-signal_dim:])
681
+ return _VF.stft( # type: ignore[attr-defined]
682
+ input,
683
+ n_fft,
684
+ hop_length,
685
+ win_length,
686
+ window,
687
+ normalized,
688
+ onesided,
689
+ return_complex,
690
+ align_to_window,
691
+ )
692
+
693
+
694
+ istft = _add_docstr(
695
+ torch.istft,
696
+ "istft(input, n_fft, hop_length=None, win_length=None, window=None, center=True, "
697
+ "normalized=False, onesided=None, length=None, return_complex=False) -> Tensor:\n"
698
+ r"""
699
+ Inverse short time Fourier Transform. This is expected to be the inverse of :func:`~torch.stft`.
700
+
701
+ .. warning::
702
+ From version 2.1, a warning will be provided if a :attr:`window` is
703
+ not specified. In a future release, this attribute will be required.
704
+ Please provide the same window used in the stft call.
705
+
706
+ It has the same parameters (+ additional optional parameter of :attr:`length`) and it should return the
707
+ least squares estimation of the original signal. The algorithm will check using the NOLA condition (
708
+ nonzero overlap).
709
+
710
+ Important consideration in the parameters :attr:`window` and :attr:`center` so that the envelope
711
+ created by the summation of all the windows is never zero at certain point in time. Specifically,
712
+ :math:`\sum_{t=-\infty}^{\infty} |w|^2[n-t\times hop\_length] \cancel{=} 0`.
713
+
714
+ Since :func:`~torch.stft` discards elements at the end of the signal if they do not fit in a frame,
715
+ ``istft`` may return a shorter signal than the original signal (can occur if :attr:`center` is False
716
+ since the signal isn't padded). If `length` is given in the arguments and is longer than expected,
717
+ ``istft`` will pad zeros to the end of the returned signal.
718
+
719
+ If :attr:`center` is ``True``, then there will be padding e.g. ``'constant'``, ``'reflect'``, etc.
720
+ Left padding can be trimmed off exactly because they can be calculated but right padding cannot be
721
+ calculated without additional information.
722
+
723
+ Example: Suppose the last window is:
724
+ ``[17, 18, 0, 0, 0]`` vs ``[18, 0, 0, 0, 0]``
725
+
726
+ The :attr:`n_fft`, :attr:`hop_length`, :attr:`win_length` are all the same which prevents the calculation
727
+ of right padding. These additional values could be zeros or a reflection of the signal so providing
728
+ :attr:`length` could be useful. If :attr:`length` is ``None`` then padding will be aggressively removed
729
+ (some loss of signal).
730
+
731
+ [1] D. W. Griffin and J. S. Lim, "Signal estimation from modified short-time Fourier transform,"
732
+ IEEE Trans. ASSP, vol.32, no.2, pp.236-243, Apr. 1984.
733
+
734
+ Args:
735
+ input (Tensor): The input tensor. Expected to be in the format of :func:`~torch.stft`,
736
+ output. That is a complex tensor of shape `(B?, N, T)` where
737
+
738
+ - `B?` is an optional batch dimension
739
+ - `N` is the number of frequency samples, `(n_fft // 2) + 1`
740
+ for onesided input, or otherwise `n_fft`.
741
+ - `T` is the number of frames, `1 + length // hop_length` for centered stft,
742
+ or `1 + (length - n_fft) // hop_length` otherwise.
743
+
744
+ .. versionchanged:: 2.0
745
+ Real datatype inputs are no longer supported. Input must now have a
746
+ complex datatype, as returned by ``stft(..., return_complex=True)``.
747
+ n_fft (int): Size of Fourier transform
748
+ hop_length (Optional[int]): The distance between neighboring sliding window frames.
749
+ (Default: ``n_fft // 4``)
750
+ win_length (Optional[int]): The size of window frame and STFT filter. (Default: ``n_fft``)
751
+ window (Optional[torch.Tensor]): The optional window function.
752
+ Shape must be 1d and `<= n_fft`
753
+ (Default: ``torch.ones(win_length)``)
754
+ center (bool): Whether :attr:`input` was padded on both sides so that the :math:`t`-th frame is
755
+ centered at time :math:`t \times \text{hop\_length}`.
756
+ (Default: ``True``)
757
+ normalized (bool): Whether the STFT was normalized. (Default: ``False``)
758
+ onesided (Optional[bool]): Whether the STFT was onesided.
759
+ (Default: ``True`` if `n_fft != fft_size` in the input size)
760
+ length (Optional[int]): The amount to trim the signal by (i.e. the
761
+ original signal length). Defaults to `(T - 1) * hop_length` for
762
+ centered stft, or `n_fft + (T - 1) * hop_length` otherwise, where `T`
763
+ is the number of input frames.
764
+ return_complex (Optional[bool]):
765
+ Whether the output should be complex, or if the input should be
766
+ assumed to derive from a real signal and window.
767
+ Note that this is incompatible with ``onesided=True``.
768
+ (Default: ``False``)
769
+
770
+ Returns:
771
+ Tensor: Least squares estimation of the original signal of shape `(B?, length)` where
772
+ `B?` is an optional batch dimension from the input tensor.
773
+ """,
774
+ )
775
+
776
+
777
+ if TYPE_CHECKING:
778
+ # These _impl functions return a variable number of tensors as output with
779
+ # __torch_function__; tuple unpacking is done already rather than being
780
+ # done by the caller of the _impl function
781
+ _unique_impl_out = Any
782
+ else:
783
+ _unique_impl_out = tuple[Tensor, Tensor, Tensor]
784
+
785
+
786
+ def _unique_impl(
787
+ input: Tensor,
788
+ sorted: bool = True,
789
+ return_inverse: bool = False,
790
+ return_counts: bool = False,
791
+ dim: Optional[int] = None,
792
+ ) -> _unique_impl_out:
793
+ r"""unique(input, sorted=True, return_inverse=False, return_counts=False, dim=None) -> tuple[Tensor, Tensor, Tensor]
794
+
795
+ Returns the unique elements of the input tensor.
796
+
797
+ .. note:: This function is different from :func:`torch.unique_consecutive` in the sense that
798
+ this function also eliminates non-consecutive duplicate values.
799
+
800
+ .. note:: Currently in the CUDA implementation and the CPU implementation,
801
+ `torch.unique` always sort the tensor at the beginning regardless of the `sort` argument.
802
+ Sorting could be slow, so if your input tensor is already sorted, it is recommended to use
803
+ :func:`torch.unique_consecutive` which avoids the sorting.
804
+
805
+ Args:
806
+ input (Tensor): the input tensor
807
+ sorted (bool): Whether to sort the unique elements in ascending order
808
+ before returning as output.
809
+ return_inverse (bool): Whether to also return the indices for where
810
+ elements in the original input ended up in the returned unique list.
811
+ return_counts (bool): Whether to also return the counts for each unique
812
+ element.
813
+ dim (int, optional): the dimension to operate upon. If ``None``, the
814
+ unique of the flattened input is returned. Otherwise, each of the
815
+ tensors indexed by the given dimension is treated as one of the
816
+ elements to apply the unique operation upon. See examples for more
817
+ details. Default: ``None``
818
+
819
+ Returns:
820
+ (Tensor, Tensor (optional), Tensor (optional)): A tensor or a tuple of tensors containing
821
+
822
+ - **output** (*Tensor*): the output list of unique scalar elements.
823
+ - **inverse_indices** (*Tensor*): (optional) if
824
+ :attr:`return_inverse` is True, there will be an additional
825
+ returned tensor (same shape as input) representing the indices
826
+ for where elements in the original input map to in the output;
827
+ otherwise, this function will only return a single tensor.
828
+ - **counts** (*Tensor*): (optional) if
829
+ :attr:`return_counts` is True, there will be an additional
830
+ returned tensor (same shape as output or output.size(dim),
831
+ if dim was specified) representing the number of occurrences
832
+ for each unique value or tensor.
833
+
834
+ Example::
835
+
836
+ >>> output = torch.unique(torch.tensor([1, 3, 2, 3], dtype=torch.long))
837
+ >>> output
838
+ tensor([1, 2, 3])
839
+
840
+ >>> output, inverse_indices = torch.unique(
841
+ ... torch.tensor([1, 3, 2, 3], dtype=torch.long), sorted=True, return_inverse=True)
842
+ >>> output
843
+ tensor([1, 2, 3])
844
+ >>> inverse_indices
845
+ tensor([0, 2, 1, 2])
846
+
847
+ >>> output, inverse_indices = torch.unique(
848
+ ... torch.tensor([[1, 3], [2, 3]], dtype=torch.long), sorted=True, return_inverse=True)
849
+ >>> output
850
+ tensor([1, 2, 3])
851
+ >>> inverse_indices
852
+ tensor([[0, 2],
853
+ [1, 2]])
854
+
855
+ >>> a = torch.tensor([
856
+ ... [
857
+ ... [1, 1, 0, 0],
858
+ ... [1, 1, 0, 0],
859
+ ... [0, 0, 1, 1],
860
+ ... ],
861
+ ... [
862
+ ... [0, 0, 1, 1],
863
+ ... [0, 0, 1, 1],
864
+ ... [1, 1, 1, 1],
865
+ ... ],
866
+ ... [
867
+ ... [1, 1, 0, 0],
868
+ ... [1, 1, 0, 0],
869
+ ... [0, 0, 1, 1],
870
+ ... ],
871
+ ... ])
872
+
873
+ >>> # If we call `torch.unique(a, dim=0)`, each of the tensors `a[idx, :, :]`
874
+ >>> # will be compared. We can see that `a[0, :, :]` and `a[2, :, :]` match
875
+ >>> # each other, so one of them will be removed.
876
+ >>> (a[0, :, :] == a[2, :, :]).all()
877
+ tensor(True)
878
+ >>> a_unique_dim0 = torch.unique(a, dim=0)
879
+ >>> a_unique_dim0
880
+ tensor([[[0, 0, 1, 1],
881
+ [0, 0, 1, 1],
882
+ [1, 1, 1, 1]],
883
+ [[1, 1, 0, 0],
884
+ [1, 1, 0, 0],
885
+ [0, 0, 1, 1]]])
886
+
887
+ >>> # Notice which sub-tensors from `a` match with the sub-tensors from
888
+ >>> # `a_unique_dim0`:
889
+ >>> (a_unique_dim0[0, :, :] == a[1, :, :]).all()
890
+ tensor(True)
891
+ >>> (a_unique_dim0[1, :, :] == a[0, :, :]).all()
892
+ tensor(True)
893
+
894
+ >>> # For `torch.unique(a, dim=1)`, each of the tensors `a[:, idx, :]` are
895
+ >>> # compared. `a[:, 0, :]` and `a[:, 1, :]` match each other, so one of
896
+ >>> # them will be removed.
897
+ >>> (a[:, 0, :] == a[:, 1, :]).all()
898
+ tensor(True)
899
+ >>> torch.unique(a, dim=1)
900
+ tensor([[[0, 0, 1, 1],
901
+ [1, 1, 0, 0]],
902
+ [[1, 1, 1, 1],
903
+ [0, 0, 1, 1]],
904
+ [[0, 0, 1, 1],
905
+ [1, 1, 0, 0]]])
906
+
907
+ >>> # For `torch.unique(a, dim=2)`, the tensors `a[:, :, idx]` are compared.
908
+ >>> # `a[:, :, 0]` and `a[:, :, 1]` match each other. Also, `a[:, :, 2]` and
909
+ >>> # `a[:, :, 3]` match each other as well. So in this case, two of the
910
+ >>> # sub-tensors will be removed.
911
+ >>> (a[:, :, 0] == a[:, :, 1]).all()
912
+ tensor(True)
913
+ >>> (a[:, :, 2] == a[:, :, 3]).all()
914
+ tensor(True)
915
+ >>> torch.unique(a, dim=2)
916
+ tensor([[[0, 1],
917
+ [0, 1],
918
+ [1, 0]],
919
+ [[1, 0],
920
+ [1, 0],
921
+ [1, 1]],
922
+ [[0, 1],
923
+ [0, 1],
924
+ [1, 0]]])
925
+ """
926
+ if has_torch_function_unary(input):
927
+ return handle_torch_function(
928
+ unique,
929
+ (input,),
930
+ input,
931
+ sorted=sorted,
932
+ return_inverse=return_inverse,
933
+ return_counts=return_counts,
934
+ dim=dim,
935
+ )
936
+
937
+ if dim is not None:
938
+ output, inverse_indices, counts = _VF.unique_dim(
939
+ input,
940
+ dim,
941
+ sorted=sorted,
942
+ return_inverse=return_inverse,
943
+ return_counts=return_counts,
944
+ )
945
+ else:
946
+ output, inverse_indices, counts = torch._unique2(
947
+ input,
948
+ sorted=sorted,
949
+ return_inverse=return_inverse,
950
+ return_counts=return_counts,
951
+ )
952
+ return output, inverse_indices, counts
953
+
954
+
955
+ def _unique_consecutive_impl(
956
+ input: Tensor,
957
+ return_inverse: bool = False,
958
+ return_counts: bool = False,
959
+ dim: Optional[int] = None,
960
+ ) -> _unique_impl_out:
961
+ r"""Eliminates all but the first element from every consecutive group of equivalent elements.
962
+
963
+ .. note:: This function is different from :func:`torch.unique` in the sense that this function
964
+ only eliminates consecutive duplicate values. This semantics is similar to `std::unique`
965
+ in C++.
966
+
967
+ Args:
968
+ input (Tensor): the input tensor
969
+ return_inverse (bool): Whether to also return the indices for where
970
+ elements in the original input ended up in the returned unique list.
971
+ return_counts (bool): Whether to also return the counts for each unique
972
+ element.
973
+ dim (int): the dimension to apply unique. If ``None``, the unique of the
974
+ flattened input is returned. default: ``None``
975
+
976
+ Returns:
977
+ (Tensor, Tensor (optional), Tensor (optional)): A tensor or a tuple of tensors containing
978
+
979
+ - **output** (*Tensor*): the output list of unique scalar elements.
980
+ - **inverse_indices** (*Tensor*): (optional) if
981
+ :attr:`return_inverse` is True, there will be an additional
982
+ returned tensor (same shape as input) representing the indices
983
+ for where elements in the original input map to in the output;
984
+ otherwise, this function will only return a single tensor.
985
+ - **counts** (*Tensor*): (optional) if
986
+ :attr:`return_counts` is True, there will be an additional
987
+ returned tensor (same shape as output or output.size(dim),
988
+ if dim was specified) representing the number of occurrences
989
+ for each unique value or tensor.
990
+
991
+ Example::
992
+
993
+ >>> x = torch.tensor([1, 1, 2, 2, 3, 1, 1, 2])
994
+ >>> output = torch.unique_consecutive(x)
995
+ >>> output
996
+ tensor([1, 2, 3, 1, 2])
997
+
998
+ >>> output, inverse_indices = torch.unique_consecutive(x, return_inverse=True)
999
+ >>> output
1000
+ tensor([1, 2, 3, 1, 2])
1001
+ >>> inverse_indices
1002
+ tensor([0, 0, 1, 1, 2, 3, 3, 4])
1003
+
1004
+ >>> output, counts = torch.unique_consecutive(x, return_counts=True)
1005
+ >>> output
1006
+ tensor([1, 2, 3, 1, 2])
1007
+ >>> counts
1008
+ tensor([2, 2, 1, 2, 1])
1009
+ """
1010
+ if has_torch_function_unary(input):
1011
+ return handle_torch_function(
1012
+ unique_consecutive,
1013
+ (input,),
1014
+ input,
1015
+ return_inverse=return_inverse,
1016
+ return_counts=return_counts,
1017
+ dim=dim,
1018
+ )
1019
+ output, inverse_indices, counts = _VF.unique_consecutive( # type: ignore[attr-defined]
1020
+ input, return_inverse=return_inverse, return_counts=return_counts, dim=dim
1021
+ )
1022
+ return output, inverse_indices, counts
1023
+
1024
+
1025
+ def _return_counts(
1026
+ input,
1027
+ sorted=True,
1028
+ return_inverse=False,
1029
+ return_counts=False,
1030
+ dim=None,
1031
+ ):
1032
+ # type: (Tensor, bool, bool, bool, Optional[int]) -> tuple[Tensor, Tensor]
1033
+
1034
+ if has_torch_function_unary(input):
1035
+ return _unique_impl(input, sorted, return_inverse, return_counts, dim)
1036
+
1037
+ output, _, counts = _unique_impl(input, sorted, return_inverse, return_counts, dim)
1038
+ return output, counts
1039
+
1040
+
1041
+ def _return_output(
1042
+ input,
1043
+ sorted=True,
1044
+ return_inverse=False,
1045
+ return_counts=False,
1046
+ dim=None,
1047
+ ):
1048
+ # type: (Tensor, bool, bool, bool, Optional[int]) -> Tensor
1049
+
1050
+ if has_torch_function_unary(input):
1051
+ return _unique_impl(input, sorted, return_inverse, return_counts, dim)
1052
+
1053
+ output, _, _ = _unique_impl(input, sorted, return_inverse, return_counts, dim)
1054
+ return output
1055
+
1056
+
1057
+ def _return_inverse(
1058
+ input,
1059
+ sorted=True,
1060
+ return_inverse=False,
1061
+ return_counts=False,
1062
+ dim=None,
1063
+ ):
1064
+ # type: (Tensor, bool, bool, bool, Optional[int]) -> tuple[Tensor, Tensor]
1065
+
1066
+ if has_torch_function_unary(input):
1067
+ return _unique_impl(input, sorted, return_inverse, return_counts, dim)
1068
+
1069
+ output, inverse_indices, _ = _unique_impl(
1070
+ input, sorted, return_inverse, return_counts, dim
1071
+ )
1072
+ return output, inverse_indices
1073
+
1074
+
1075
+ _return_inverse_false = boolean_dispatch(
1076
+ arg_name="return_counts",
1077
+ arg_index=3,
1078
+ default=False,
1079
+ if_true=_return_counts,
1080
+ if_false=_return_output,
1081
+ module_name=__name__,
1082
+ func_name="unique",
1083
+ )
1084
+
1085
+ _return_inverse_true = boolean_dispatch(
1086
+ arg_name="return_counts",
1087
+ arg_index=3,
1088
+ default=False,
1089
+ if_true=_unique_impl,
1090
+ if_false=_return_inverse,
1091
+ module_name=__name__,
1092
+ func_name="unique",
1093
+ )
1094
+
1095
+ # The return type of unique depends on `return_inverse`, and `return_counts` so in order to
1096
+ # resolve the output type in TorchScript we need to statically know the value of both parameters
1097
+
1098
+ unique = boolean_dispatch(
1099
+ arg_name="return_inverse",
1100
+ arg_index=2,
1101
+ default=False,
1102
+ if_true=_return_inverse_true,
1103
+ if_false=_return_inverse_false,
1104
+ module_name=__name__,
1105
+ func_name="unique",
1106
+ )
1107
+ unique.__doc__ = _unique_impl.__doc__
1108
+
1109
+
1110
+ def _consecutive_return_counts(
1111
+ input,
1112
+ return_inverse=False,
1113
+ return_counts=False,
1114
+ dim=None,
1115
+ ):
1116
+ # type: (Tensor, bool, bool, Optional[int]) -> tuple[Tensor, Tensor]
1117
+
1118
+ if has_torch_function_unary(input):
1119
+ return _unique_consecutive_impl(input, return_inverse, return_counts, dim)
1120
+
1121
+ output, _, counts = _unique_consecutive_impl(
1122
+ input, return_inverse, return_counts, dim
1123
+ )
1124
+ return output, counts
1125
+
1126
+
1127
+ def _consecutive_return_output(
1128
+ input,
1129
+ return_inverse=False,
1130
+ return_counts=False,
1131
+ dim=None,
1132
+ ):
1133
+ # type: (Tensor, bool, bool, Optional[int]) -> Tensor
1134
+
1135
+ if has_torch_function_unary(input):
1136
+ return _unique_consecutive_impl(input, return_inverse, return_counts, dim)
1137
+
1138
+ output, _, _ = _unique_consecutive_impl(input, return_inverse, return_counts, dim)
1139
+ return output
1140
+
1141
+
1142
+ def _consecutive_return_inverse(
1143
+ input,
1144
+ return_inverse=False,
1145
+ return_counts=False,
1146
+ dim=None,
1147
+ ):
1148
+ # type: (Tensor, bool, bool, Optional[int]) -> tuple[Tensor, Tensor]
1149
+
1150
+ if has_torch_function_unary(input):
1151
+ return _unique_consecutive_impl(input, return_inverse, return_counts, dim)
1152
+
1153
+ output, inverse_indices, _ = _unique_consecutive_impl(
1154
+ input, return_inverse, return_counts, dim
1155
+ )
1156
+ return output, inverse_indices
1157
+
1158
+
1159
+ _consecutive_return_inverse_false = boolean_dispatch(
1160
+ arg_name="return_counts",
1161
+ arg_index=1,
1162
+ default=False,
1163
+ if_true=_consecutive_return_counts,
1164
+ if_false=_consecutive_return_output,
1165
+ module_name=__name__,
1166
+ func_name="unique_consecutive",
1167
+ )
1168
+
1169
+ _consecutive_return_inverse_true = boolean_dispatch(
1170
+ arg_name="return_counts",
1171
+ arg_index=1,
1172
+ default=False,
1173
+ if_true=_unique_consecutive_impl,
1174
+ if_false=_consecutive_return_inverse,
1175
+ module_name=__name__,
1176
+ func_name="unique_consecutive",
1177
+ )
1178
+
1179
+ # The return type of unique depends on `return_inverse`, and `return_counts` so in order to
1180
+ # resolve the output type in TorchScript we need to statically know the value of both parameters
1181
+
1182
+ unique_consecutive = boolean_dispatch(
1183
+ arg_name="return_inverse",
1184
+ arg_index=2,
1185
+ default=False,
1186
+ if_true=_consecutive_return_inverse_true,
1187
+ if_false=_consecutive_return_inverse_false,
1188
+ module_name=__name__,
1189
+ func_name="unique_consecutive",
1190
+ )
1191
+ unique_consecutive.__doc__ = _unique_consecutive_impl.__doc__
1192
+
1193
+ if TYPE_CHECKING:
1194
+ pass
1195
+ # There's no good way to use this type annotation without breaking JIT
1196
+ # overloads. So leave untyped for mypy for now.
1197
+ else:
1198
+
1199
+ @overload
1200
+ def tensordot(
1201
+ a,
1202
+ b,
1203
+ dims: int = 2,
1204
+ out: Optional[torch.Tensor] = None,
1205
+ ):
1206
+ pass
1207
+
1208
+ @overload
1209
+ def tensordot( # noqa: F811
1210
+ a,
1211
+ b,
1212
+ dims: tuple[list[int], list[int]],
1213
+ out: Optional[torch.Tensor] = None,
1214
+ ):
1215
+ pass
1216
+
1217
+ @overload
1218
+ def tensordot( # noqa: F811
1219
+ a,
1220
+ b,
1221
+ dims: list[list[int]],
1222
+ out: Optional[torch.Tensor] = None,
1223
+ ):
1224
+ pass
1225
+
1226
+ @overload
1227
+ def tensordot( # noqa: F811
1228
+ a,
1229
+ b,
1230
+ dims: torch.Tensor,
1231
+ out: Optional[torch.Tensor] = None,
1232
+ ):
1233
+ pass
1234
+
1235
+
1236
+ def tensordot( # noqa: F811
1237
+ a,
1238
+ b,
1239
+ dims=2,
1240
+ out: Optional[torch.Tensor] = None,
1241
+ ):
1242
+ r"""Returns a contraction of a and b over multiple dimensions.
1243
+
1244
+ :attr:`tensordot` implements a generalized matrix product.
1245
+
1246
+ Args:
1247
+ a (Tensor): Left tensor to contract
1248
+ b (Tensor): Right tensor to contract
1249
+ dims (int or Tuple[List[int], List[int]] or List[List[int]] containing two lists or Tensor): number of dimensions to
1250
+ contract or explicit lists of dimensions for :attr:`a` and
1251
+ :attr:`b` respectively
1252
+
1253
+ When called with a non-negative integer argument :attr:`dims` = :math:`d`, and
1254
+ the number of dimensions of :attr:`a` and :attr:`b` is :math:`m` and :math:`n`,
1255
+ respectively, :func:`~torch.tensordot` computes
1256
+
1257
+ .. math::
1258
+ r_{i_0,...,i_{m-d}, i_d,...,i_n}
1259
+ = \sum_{k_0,...,k_{d-1}} a_{i_0,...,i_{m-d},k_0,...,k_{d-1}} \times b_{k_0,...,k_{d-1}, i_d,...,i_n}.
1260
+
1261
+ When called with :attr:`dims` of the list form, the given dimensions will be contracted
1262
+ in place of the last :math:`d` of :attr:`a` and the first :math:`d` of :math:`b`. The sizes
1263
+ in these dimensions must match, but :func:`~torch.tensordot` will deal with broadcasted
1264
+ dimensions.
1265
+
1266
+ Examples::
1267
+
1268
+ >>> a = torch.arange(60.).reshape(3, 4, 5)
1269
+ >>> b = torch.arange(24.).reshape(4, 3, 2)
1270
+ >>> torch.tensordot(a, b, dims=([1, 0], [0, 1]))
1271
+ tensor([[4400., 4730.],
1272
+ [4532., 4874.],
1273
+ [4664., 5018.],
1274
+ [4796., 5162.],
1275
+ [4928., 5306.]])
1276
+
1277
+ >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA)
1278
+ >>> a = torch.randn(3, 4, 5, device='cuda')
1279
+ >>> b = torch.randn(4, 5, 6, device='cuda')
1280
+ >>> c = torch.tensordot(a, b, dims=2).cpu()
1281
+ tensor([[ 8.3504, -2.5436, 6.2922, 2.7556, -1.0732, 3.2741],
1282
+ [ 3.3161, 0.0704, 5.0187, -0.4079, -4.3126, 4.8744],
1283
+ [ 0.8223, 3.9445, 3.2168, -0.2400, 3.4117, 1.7780]])
1284
+
1285
+ >>> a = torch.randn(3, 5, 4, 6)
1286
+ >>> b = torch.randn(6, 4, 5, 3)
1287
+ >>> torch.tensordot(a, b, dims=([2, 1, 3], [1, 2, 0]))
1288
+ tensor([[ 7.7193, -2.4867, -10.3204],
1289
+ [ 1.5513, -14.4737, -6.5113],
1290
+ [ -0.2850, 4.2573, -3.5997]])
1291
+ """
1292
+ if has_torch_function_variadic(a, b):
1293
+ return handle_torch_function(tensordot, (a, b), a, b, dims=dims, out=out)
1294
+
1295
+ if not isinstance(dims, (tuple, list, torch.Tensor, int, torch.SymInt)):
1296
+ raise RuntimeError(
1297
+ "tensordot expects dims to be int or "
1298
+ + "tuple[list[int], list[int]] or "
1299
+ + "list[list[int]] containing two lists, but got "
1300
+ + f"dims={dims}"
1301
+ )
1302
+
1303
+ dims_a: list[int] = []
1304
+ dims_b: list[int] = []
1305
+
1306
+ if isinstance(dims, (tuple, list)):
1307
+ dims_a, dims_b = dims
1308
+
1309
+ if isinstance(dims, torch.Tensor):
1310
+ num_elements = dims.numel()
1311
+ if num_elements > 1:
1312
+ assert dims.size()[0] == 2
1313
+ dims_a = torch.jit.annotate(list[int], dims[0].tolist())
1314
+ dims_b = torch.jit.annotate(list[int], dims[1].tolist())
1315
+ else:
1316
+ dims_val = int(dims.item())
1317
+ if dims_val < 0:
1318
+ raise RuntimeError(f"tensordot expects dims >= 0, but got dims={dims}")
1319
+ dims_a = list(range(-dims_val, 0))
1320
+ dims_b = list(range(dims_val))
1321
+
1322
+ if isinstance(dims, (int, torch.SymInt)):
1323
+ if dims < 0:
1324
+ raise RuntimeError(f"tensordot expects dims >= 0, but got dims={dims}")
1325
+ if dims > min(a.dim(), b.dim()):
1326
+ raise RuntimeError(
1327
+ f"tensordot expects dims < ndim_a or ndim_b, but got dims={dims}"
1328
+ )
1329
+ dims_a = list(range(-dims, 0))
1330
+ dims_b = list(range(dims))
1331
+
1332
+ if out is None:
1333
+ return _VF.tensordot(a, b, dims_a, dims_b) # type: ignore[attr-defined]
1334
+ else:
1335
+ return _VF.tensordot(a, b, dims_a, dims_b, out=out) # type: ignore[attr-defined]
1336
+
1337
+
1338
+ def cartesian_prod(*tensors: Tensor) -> Tensor:
1339
+ """Do cartesian product of the given sequence of tensors. The behavior is similar to
1340
+ python's `itertools.product`.
1341
+
1342
+ Args:
1343
+ *tensors: any number of 1 dimensional tensors.
1344
+
1345
+ Returns:
1346
+ Tensor: A tensor equivalent to converting all the input tensors into lists,
1347
+ do `itertools.product` on these lists, and finally convert the resulting list
1348
+ into tensor.
1349
+
1350
+ Example::
1351
+
1352
+ >>> import itertools
1353
+ >>> a = [1, 2, 3]
1354
+ >>> b = [4, 5]
1355
+ >>> list(itertools.product(a, b))
1356
+ [(1, 4), (1, 5), (2, 4), (2, 5), (3, 4), (3, 5)]
1357
+ >>> tensor_a = torch.tensor(a)
1358
+ >>> tensor_b = torch.tensor(b)
1359
+ >>> torch.cartesian_prod(tensor_a, tensor_b)
1360
+ tensor([[1, 4],
1361
+ [1, 5],
1362
+ [2, 4],
1363
+ [2, 5],
1364
+ [3, 4],
1365
+ [3, 5]])
1366
+ """
1367
+ # This wrapper exists to support variadic args.
1368
+ if has_torch_function(tensors):
1369
+ return handle_torch_function(cartesian_prod, tensors, *tensors)
1370
+ return _VF.cartesian_prod(tensors) # type: ignore[attr-defined]
1371
+
1372
+
1373
+ def block_diag(*tensors):
1374
+ """Create a block diagonal matrix from provided tensors.
1375
+
1376
+ Args:
1377
+ *tensors: One or more tensors with 0, 1, or 2 dimensions.
1378
+
1379
+ Returns:
1380
+ Tensor: A 2 dimensional tensor with all the input tensors arranged in
1381
+ order such that their upper left and lower right corners are
1382
+ diagonally adjacent. All other elements are set to 0.
1383
+
1384
+ Example::
1385
+
1386
+ >>> import torch
1387
+ >>> A = torch.tensor([[0, 1], [1, 0]])
1388
+ >>> B = torch.tensor([[3, 4, 5], [6, 7, 8]])
1389
+ >>> C = torch.tensor(7)
1390
+ >>> D = torch.tensor([1, 2, 3])
1391
+ >>> E = torch.tensor([[4], [5], [6]])
1392
+ >>> torch.block_diag(A, B, C, D, E)
1393
+ tensor([[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
1394
+ [1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1395
+ [0, 0, 3, 4, 5, 0, 0, 0, 0, 0],
1396
+ [0, 0, 6, 7, 8, 0, 0, 0, 0, 0],
1397
+ [0, 0, 0, 0, 0, 7, 0, 0, 0, 0],
1398
+ [0, 0, 0, 0, 0, 0, 1, 2, 3, 0],
1399
+ [0, 0, 0, 0, 0, 0, 0, 0, 0, 4],
1400
+ [0, 0, 0, 0, 0, 0, 0, 0, 0, 5],
1401
+ [0, 0, 0, 0, 0, 0, 0, 0, 0, 6]])
1402
+ """
1403
+ # This wrapper exists to support variadic args.
1404
+ if has_torch_function(tensors):
1405
+ return handle_torch_function(block_diag, tensors, *tensors)
1406
+ return torch._C._VariableFunctions.block_diag(tensors) # type: ignore[attr-defined]
1407
+
1408
+
1409
+ def cdist(x1, x2, p=2.0, compute_mode="use_mm_for_euclid_dist_if_necessary"):
1410
+ # type: (Tensor, Tensor, float, str) -> (Tensor)
1411
+ r"""Computes batched the p-norm distance between each pair of the two collections of row vectors.
1412
+
1413
+ Args:
1414
+ x1 (Tensor): input tensor where the last two dimensions represent the points and the feature dimension respectively.
1415
+ The shape can be :math:`D_1 \times D_2 \times \cdots \times D_n \times P \times M`,
1416
+ where :math:`P` is the number of points and :math:`M` is the feature dimension.
1417
+ x2 (Tensor): input tensor where the last two dimensions also represent the points and the feature dimension respectively.
1418
+ The shape can be :math:`D_1' \times D_2' \times \cdots \times D_m' \times R \times M`,
1419
+ where :math:`R` is the number of points and :math:`M` is the feature dimension,
1420
+ which should match the feature dimension of `x1`.
1421
+ p: p value for the p-norm distance to calculate between each vector pair
1422
+ :math:`\in [0, \infty]`.
1423
+ compute_mode:
1424
+ 'use_mm_for_euclid_dist_if_necessary' - will use matrix multiplication approach to calculate
1425
+ euclidean distance (p = 2) if P > 25 or R > 25
1426
+ 'use_mm_for_euclid_dist' - will always use matrix multiplication approach to calculate
1427
+ euclidean distance (p = 2)
1428
+ 'donot_use_mm_for_euclid_dist' - will never use matrix multiplication approach to calculate
1429
+ euclidean distance (p = 2)
1430
+ Default: use_mm_for_euclid_dist_if_necessary.
1431
+
1432
+ If x1 has shape :math:`B \times P \times M` and x2 has shape :math:`B \times R \times M` then the
1433
+ output will have shape :math:`B \times P \times R`.
1434
+
1435
+ This function is equivalent to `scipy.spatial.distance.cdist(input,'minkowski', p=p)`
1436
+ if :math:`p \in (0, \infty)`. When :math:`p = 0` it is equivalent to
1437
+ `scipy.spatial.distance.cdist(input, 'hamming') * M`. When :math:`p = \infty`, the closest
1438
+ scipy function is `scipy.spatial.distance.cdist(xn, lambda x, y: np.abs(x - y).max())`.
1439
+
1440
+ Example:
1441
+
1442
+ >>> a = torch.tensor([[0.9041, 0.0196], [-0.3108, -2.4423], [-0.4821, 1.059]])
1443
+ >>> a
1444
+ tensor([[ 0.9041, 0.0196],
1445
+ [-0.3108, -2.4423],
1446
+ [-0.4821, 1.0590]])
1447
+ >>> b = torch.tensor([[-2.1763, -0.4713], [-0.6986, 1.3702]])
1448
+ >>> b
1449
+ tensor([[-2.1763, -0.4713],
1450
+ [-0.6986, 1.3702]])
1451
+ >>> torch.cdist(a, b, p=2)
1452
+ tensor([[3.1193, 2.0959],
1453
+ [2.7138, 3.8322],
1454
+ [2.2830, 0.3791]])
1455
+ """
1456
+ if has_torch_function_variadic(x1, x2):
1457
+ return handle_torch_function(
1458
+ cdist, (x1, x2), x1, x2, p=p, compute_mode=compute_mode
1459
+ )
1460
+ if compute_mode == "use_mm_for_euclid_dist_if_necessary":
1461
+ return _VF.cdist(x1, x2, p, None) # type: ignore[attr-defined]
1462
+ elif compute_mode == "use_mm_for_euclid_dist":
1463
+ return _VF.cdist(x1, x2, p, 1) # type: ignore[attr-defined]
1464
+ elif compute_mode == "donot_use_mm_for_euclid_dist":
1465
+ return _VF.cdist(x1, x2, p, 2) # type: ignore[attr-defined]
1466
+ else:
1467
+ raise ValueError(f"{compute_mode} is not a valid value for compute_mode")
1468
+
1469
+
1470
+ def atleast_1d(*tensors):
1471
+ r"""
1472
+ Returns a 1-dimensional view of each input tensor with zero dimensions.
1473
+ Input tensors with one or more dimensions are returned as-is.
1474
+
1475
+ Args:
1476
+ input (Tensor or sequence of Tensors): tensor(s) to be converted to at least 1-dimensional.
1477
+
1478
+ Returns:
1479
+ output (Tensor or tuple of Tensors)
1480
+
1481
+ Example::
1482
+
1483
+ >>> x = torch.arange(2)
1484
+ >>> x
1485
+ tensor([0, 1])
1486
+ >>> torch.atleast_1d(x)
1487
+ tensor([0, 1])
1488
+ >>> x = torch.tensor(1.)
1489
+ >>> x
1490
+ tensor(1.)
1491
+ >>> torch.atleast_1d(x)
1492
+ tensor([1.])
1493
+ >>> x = torch.tensor(0.5)
1494
+ >>> y = torch.tensor(1.)
1495
+ >>> torch.atleast_1d((x, y))
1496
+ (tensor([0.5000]), tensor([1.]))
1497
+ >>> torch.atleast_1d()
1498
+ ()
1499
+ """
1500
+ # This wrapper exists to support variadic args.
1501
+ if has_torch_function(tensors):
1502
+ return handle_torch_function(atleast_1d, tensors, *tensors)
1503
+ if len(tensors) == 1:
1504
+ tensors = tensors[0]
1505
+ return _VF.atleast_1d(tensors) # type: ignore[attr-defined]
1506
+
1507
+
1508
+ def atleast_2d(*tensors):
1509
+ r"""
1510
+ Returns a 2-dimensional view of each input tensor with zero dimensions.
1511
+ Input tensors with two or more dimensions are returned as-is.
1512
+
1513
+ Args:
1514
+ input (Tensor or sequence of Tensors): tensor(s) to be converted to at least 2-dimensional.
1515
+
1516
+ Returns:
1517
+ output (Tensor or tuple of Tensors)
1518
+
1519
+ Example::
1520
+
1521
+ >>> x = torch.tensor(1.)
1522
+ >>> x
1523
+ tensor(1.)
1524
+ >>> torch.atleast_2d(x)
1525
+ tensor([[1.]])
1526
+ >>> x = torch.arange(4).view(2, 2)
1527
+ >>> x
1528
+ tensor([[0, 1],
1529
+ [2, 3]])
1530
+ >>> torch.atleast_2d(x)
1531
+ tensor([[0, 1],
1532
+ [2, 3]])
1533
+ >>> x = torch.tensor(0.5)
1534
+ >>> y = torch.tensor(1.)
1535
+ >>> torch.atleast_2d((x, y))
1536
+ (tensor([[0.5000]]), tensor([[1.]]))
1537
+ >>> torch.atleast_2d()
1538
+ ()
1539
+ """
1540
+ # This wrapper exists to support variadic args.
1541
+ if has_torch_function(tensors):
1542
+ return handle_torch_function(atleast_2d, tensors, *tensors)
1543
+ if len(tensors) == 1:
1544
+ tensors = tensors[0]
1545
+ return _VF.atleast_2d(tensors) # type: ignore[attr-defined]
1546
+
1547
+
1548
+ def atleast_3d(*tensors):
1549
+ r"""
1550
+ Returns a 3-dimensional view of each input tensor with zero dimensions.
1551
+ Input tensors with three or more dimensions are returned as-is.
1552
+
1553
+ Args:
1554
+ input (Tensor or sequence of Tensors): tensor(s) to be converted to at least 3-dimensional.
1555
+
1556
+ Returns:
1557
+ output (Tensor or tuple of Tensors)
1558
+
1559
+ Example:
1560
+
1561
+ >>> x = torch.tensor(0.5)
1562
+ >>> x
1563
+ tensor(0.5000)
1564
+ >>> torch.atleast_3d(x)
1565
+ tensor([[[0.5000]]])
1566
+ >>> y = torch.arange(4).view(2, 2)
1567
+ >>> y
1568
+ tensor([[0, 1],
1569
+ [2, 3]])
1570
+ >>> torch.atleast_3d(y)
1571
+ tensor([[[0],
1572
+ [1]],
1573
+ <BLANKLINE>
1574
+ [[2],
1575
+ [3]]])
1576
+ >>> x = torch.tensor(1).view(1, 1, 1)
1577
+ >>> x
1578
+ tensor([[[1]]])
1579
+ >>> torch.atleast_3d(x)
1580
+ tensor([[[1]]])
1581
+ >>> x = torch.tensor(0.5)
1582
+ >>> y = torch.tensor(1.0)
1583
+ >>> torch.atleast_3d((x, y))
1584
+ (tensor([[[0.5000]]]), tensor([[[1.]]]))
1585
+ >>> torch.atleast_3d()
1586
+ ()
1587
+ """
1588
+ # This wrapper exists to support variadic args.
1589
+ if has_torch_function(tensors):
1590
+ return handle_torch_function(atleast_3d, tensors, *tensors)
1591
+ if len(tensors) == 1:
1592
+ tensors = tensors[0]
1593
+ return _VF.atleast_3d(tensors) # type: ignore[attr-defined]
1594
+
1595
+
1596
+ if TYPE_CHECKING:
1597
+ pass
1598
+ # There's no good way to use this type annotation; cannot rename norm() to
1599
+ # _norm_impl() in a way that doesn't break JIT overloads. So leave untyped
1600
+ # for mypy for now.
1601
+ # def norm(input: Tensor,
1602
+ # p: Optional[Union[str, Number]] = "fro",
1603
+ # dim: Optional[Union[int, List[int]]] = None,
1604
+ # keepdim: bool = False,
1605
+ # out: Optional[Tensor] = None,
1606
+ # dtype: _dtype = None) -> Tensor:
1607
+ # return _norm_impl(input, p, dim, keepdim, out, dtype)
1608
+ else:
1609
+ # TODO: type dim as BroadcastingList when
1610
+ # https://github.com/pytorch/pytorch/issues/33782 is fixed
1611
+ @overload
1612
+ def norm(
1613
+ input,
1614
+ p="fro",
1615
+ dim=None,
1616
+ keepdim=False,
1617
+ out=None,
1618
+ dtype=None,
1619
+ ):
1620
+ # type: (Tensor, str, Optional[List[int]], bool, Optional[Tensor], Optional[int]) -> Tensor
1621
+ pass
1622
+
1623
+ @overload
1624
+ def norm( # noqa: F811
1625
+ input,
1626
+ p="fro",
1627
+ dim=None,
1628
+ keepdim=False,
1629
+ out=None,
1630
+ dtype=None,
1631
+ ):
1632
+ # type: (Tensor, Optional[number], Optional[List[int]], bool, Optional[Tensor], Optional[int]) -> Tensor
1633
+ pass
1634
+
1635
+ @overload
1636
+ def norm( # noqa: F811
1637
+ input,
1638
+ p="fro",
1639
+ dim=None,
1640
+ keepdim=False,
1641
+ out=None,
1642
+ dtype=None,
1643
+ ):
1644
+ # type: (Tensor, Optional[number], Optional[int], bool, Optional[Tensor], Optional[int]) -> Tensor
1645
+ pass
1646
+
1647
+ @overload
1648
+ def norm( # noqa: F811
1649
+ input,
1650
+ p="fro",
1651
+ dim=None,
1652
+ keepdim=False,
1653
+ out=None,
1654
+ dtype=None,
1655
+ ):
1656
+ # type: (Tensor, str, Optional[int], bool, Optional[Tensor], Optional[int]) -> Tensor
1657
+ pass
1658
+
1659
+
1660
+ def norm( # noqa: F811
1661
+ input,
1662
+ p: Optional[Union[float, str]] = "fro",
1663
+ dim=None,
1664
+ keepdim=False,
1665
+ out=None,
1666
+ dtype=None,
1667
+ ):
1668
+ r"""Returns the matrix norm or vector norm of a given tensor.
1669
+
1670
+ .. warning::
1671
+
1672
+ torch.norm is deprecated and may be removed in a future PyTorch release.
1673
+ Its documentation and behavior may be incorrect, and it is no longer
1674
+ actively maintained.
1675
+
1676
+ Use :func:`torch.linalg.vector_norm` when computing vector norms and
1677
+ :func:`torch.linalg.matrix_norm` when computing matrix norms.
1678
+ For a function with a similar behavior as this one see :func:`torch.linalg.norm`.
1679
+ Note, however, the signature for these functions is slightly different than the
1680
+ signature for ``torch.norm``.
1681
+
1682
+ Args:
1683
+ input (Tensor): The input tensor. Its data type must be either a floating
1684
+ point or complex type. For complex inputs, the norm is calculated using the
1685
+ absolute value of each element. If the input is complex and neither
1686
+ :attr:`dtype` nor :attr:`out` is specified, the result's data type will
1687
+ be the corresponding floating point type (e.g. float if :attr:`input` is
1688
+ complexfloat).
1689
+
1690
+ p (int, float, inf, -inf, 'fro', 'nuc', optional): the order of norm. Default: ``'fro'``
1691
+ The following norms can be calculated:
1692
+
1693
+ ====== ============== ==========================
1694
+ ord matrix norm vector norm
1695
+ ====== ============== ==========================
1696
+ 'fro' Frobenius norm --
1697
+ 'nuc' nuclear norm --
1698
+ Number -- sum(abs(x)**ord)**(1./ord)
1699
+ ====== ============== ==========================
1700
+
1701
+ The vector norm can be calculated across any number of dimensions.
1702
+ The corresponding dimensions of :attr:`input` are flattened into
1703
+ one dimension, and the norm is calculated on the flattened
1704
+ dimension.
1705
+
1706
+ Frobenius norm produces the same result as ``p=2`` in all cases
1707
+ except when :attr:`dim` is a list of three or more dims, in which
1708
+ case Frobenius norm throws an error.
1709
+
1710
+ Nuclear norm can only be calculated across exactly two dimensions.
1711
+
1712
+ dim (int, tuple of ints, list of ints, optional):
1713
+ Specifies which dimension or dimensions of :attr:`input` to
1714
+ calculate the norm across. If :attr:`dim` is ``None``, the norm will
1715
+ be calculated across all dimensions of :attr:`input`. If the norm
1716
+ type indicated by :attr:`p` does not support the specified number of
1717
+ dimensions, an error will occur.
1718
+ keepdim (bool, optional): whether the output tensors have :attr:`dim`
1719
+ retained or not. Ignored if :attr:`dim` = ``None`` and
1720
+ :attr:`out` = ``None``. Default: ``False``
1721
+ out (Tensor, optional): the output tensor. Ignored if
1722
+ :attr:`dim` = ``None`` and :attr:`out` = ``None``.
1723
+ dtype (:class:`torch.dtype`, optional): the desired data type of
1724
+ returned tensor. If specified, the input tensor is casted to
1725
+ :attr:`dtype` while performing the operation. Default: None.
1726
+
1727
+ .. note::
1728
+ Even though ``p='fro'`` supports any number of dimensions, the true
1729
+ mathematical definition of Frobenius norm only applies to tensors with
1730
+ exactly two dimensions. :func:`torch.linalg.matrix_norm` with ``ord='fro'``
1731
+ aligns with the mathematical definition, since it can only be applied across
1732
+ exactly two dimensions.
1733
+
1734
+ Example::
1735
+
1736
+ >>> import torch
1737
+ >>> a = torch.arange(9, dtype= torch.float) - 4
1738
+ >>> b = a.reshape((3, 3))
1739
+ >>> torch.norm(a)
1740
+ tensor(7.7460)
1741
+ >>> torch.norm(b)
1742
+ tensor(7.7460)
1743
+ >>> torch.norm(a, float('inf'))
1744
+ tensor(4.)
1745
+ >>> torch.norm(b, float('inf'))
1746
+ tensor(4.)
1747
+ >>> c = torch.tensor([[ 1, 2, 3], [-1, 1, 4]] , dtype=torch.float)
1748
+ >>> torch.norm(c, dim=0)
1749
+ tensor([1.4142, 2.2361, 5.0000])
1750
+ >>> torch.norm(c, dim=1)
1751
+ tensor([3.7417, 4.2426])
1752
+ >>> torch.norm(c, p=1, dim=1)
1753
+ tensor([6., 6.])
1754
+ >>> d = torch.arange(8, dtype=torch.float).reshape(2, 2, 2)
1755
+ >>> torch.norm(d, dim=(1, 2))
1756
+ tensor([ 3.7417, 11.2250])
1757
+ >>> torch.norm(d[0, :, :]), torch.norm(d[1, :, :])
1758
+ (tensor(3.7417), tensor(11.2250))
1759
+ """
1760
+
1761
+ if has_torch_function_unary(input):
1762
+ return handle_torch_function(
1763
+ norm, (input,), input, p=p, dim=dim, keepdim=keepdim, out=out, dtype=dtype
1764
+ )
1765
+
1766
+ # NB. All the repeated code and weird python is to please TorchScript.
1767
+ # For a more compact implementation see the relevant function in `_refs/__init__.py`
1768
+
1769
+ # We don't do this for MPS or sparse tensors
1770
+ if input.layout == torch.strided and input.device.type in (
1771
+ "cpu",
1772
+ "cuda",
1773
+ "xpu",
1774
+ "meta",
1775
+ torch.utils.backend_registration._privateuse1_backend_name,
1776
+ ):
1777
+ if dim is not None:
1778
+ if isinstance(dim, (int, torch.SymInt)):
1779
+ _dim = [dim]
1780
+ else:
1781
+ _dim = dim
1782
+ else:
1783
+ _dim = None # type: ignore[assignment]
1784
+
1785
+ if isinstance(p, str):
1786
+ if p == "fro" and (
1787
+ dim is None or isinstance(dim, (int, torch.SymInt)) or len(dim) <= 2
1788
+ ):
1789
+ if out is None:
1790
+ return torch.linalg.vector_norm(
1791
+ input, 2, _dim, keepdim, dtype=dtype
1792
+ )
1793
+ else:
1794
+ return torch.linalg.vector_norm(
1795
+ input, 2, _dim, keepdim, dtype=dtype, out=out
1796
+ )
1797
+
1798
+ # Here we either call the nuclear norm, or we call matrix_norm with some arguments
1799
+ # that will throw an error
1800
+ if _dim is None:
1801
+ _dim = list(range(input.ndim))
1802
+ if out is None:
1803
+ return torch.linalg.matrix_norm(input, p, _dim, keepdim, dtype=dtype)
1804
+ else:
1805
+ return torch.linalg.matrix_norm(
1806
+ input, p, _dim, keepdim, dtype=dtype, out=out
1807
+ )
1808
+ else:
1809
+ # NB. p should be Union[str, number], not Optional!
1810
+ _p = 2.0 if p is None else p
1811
+ if out is None:
1812
+ return torch.linalg.vector_norm(input, _p, _dim, keepdim, dtype=dtype)
1813
+ else:
1814
+ return torch.linalg.vector_norm(
1815
+ input, _p, _dim, keepdim, dtype=dtype, out=out
1816
+ )
1817
+
1818
+ ndim = input.dim()
1819
+
1820
+ # catch default case
1821
+ if dim is None and out is None and dtype is None and p is not None:
1822
+ if isinstance(p, str):
1823
+ if p == "fro":
1824
+ return _VF.frobenius_norm(input, dim=(), keepdim=keepdim)
1825
+ if not isinstance(p, str):
1826
+ _dim = list(range(ndim))
1827
+ return _VF.norm(input, p, dim=_dim, keepdim=keepdim) # type: ignore[attr-defined]
1828
+
1829
+ # TODO: when https://github.com/pytorch/pytorch/issues/33782 is fixed
1830
+ # remove the overloads where dim is an int and replace with BraodcastingList1
1831
+ # and remove next four lines, replace _dim with dim
1832
+ if dim is not None:
1833
+ if isinstance(dim, (int, torch.SymInt)):
1834
+ _dim = [dim]
1835
+ else:
1836
+ _dim = dim
1837
+ else:
1838
+ _dim = None # type: ignore[assignment]
1839
+
1840
+ if isinstance(p, str):
1841
+ if p == "fro":
1842
+ if dtype is not None:
1843
+ raise ValueError("dtype argument is not supported in frobenius norm")
1844
+
1845
+ if _dim is None:
1846
+ _dim = list(range(ndim))
1847
+ if out is None:
1848
+ return _VF.frobenius_norm(input, _dim, keepdim=keepdim) # type: ignore[arg-type]
1849
+ else:
1850
+ return _VF.frobenius_norm(input, _dim, keepdim=keepdim, out=out) # type: ignore[arg-type]
1851
+ elif p == "nuc":
1852
+ if dtype is not None:
1853
+ raise ValueError("dtype argument is not supported in nuclear norm")
1854
+ if _dim is None:
1855
+ if out is None:
1856
+ return _VF.nuclear_norm(input, keepdim=keepdim) # type: ignore[arg-type]
1857
+ else:
1858
+ return _VF.nuclear_norm(input, keepdim=keepdim, out=out) # type: ignore[arg-type]
1859
+ else:
1860
+ if out is None:
1861
+ return _VF.nuclear_norm(input, _dim, keepdim=keepdim) # type: ignore[arg-type]
1862
+ else:
1863
+ return _VF.nuclear_norm(input, _dim, keepdim=keepdim, out=out) # type: ignore[arg-type]
1864
+ raise RuntimeError(f"only valid string values are 'fro' and 'nuc', found {p}")
1865
+ else:
1866
+ if _dim is None:
1867
+ _dim = list(range(ndim))
1868
+
1869
+ if out is None:
1870
+ if dtype is None:
1871
+ return _VF.norm(input, p, _dim, keepdim=keepdim) # type: ignore[attr-defined]
1872
+ else:
1873
+ return _VF.norm(input, p, _dim, keepdim=keepdim, dtype=dtype) # type: ignore[attr-defined]
1874
+ else:
1875
+ if dtype is None:
1876
+ return _VF.norm(input, p, _dim, keepdim=keepdim, out=out) # type: ignore[attr-defined]
1877
+ else:
1878
+ return _VF.norm(input, p, _dim, keepdim=keepdim, dtype=dtype, out=out) # type: ignore[attr-defined]
1879
+
1880
+
1881
+ def unravel_index(
1882
+ indices: Tensor,
1883
+ shape: Union[int, Sequence[int], torch.Size],
1884
+ ) -> tuple[Tensor, ...]:
1885
+ r"""Converts a tensor of flat indices into a tuple of coordinate tensors that
1886
+ index into an arbitrary tensor of the specified shape.
1887
+
1888
+ Args:
1889
+ indices (Tensor): An integer tensor containing indices into the
1890
+ flattened version of an arbitrary tensor of shape :attr:`shape`.
1891
+ All elements must be in the range ``[0, prod(shape) - 1]``.
1892
+
1893
+ shape (int, sequence of ints, or torch.Size): The shape of the arbitrary
1894
+ tensor. All elements must be non-negative.
1895
+
1896
+ Returns:
1897
+ tuple of Tensors: Each ``i``-th tensor in the output corresponds with
1898
+ dimension ``i`` of :attr:`shape`. Each tensor has the same shape as
1899
+ ``indices`` and contains one index into dimension ``i`` for each of the
1900
+ flat indices given by ``indices``.
1901
+
1902
+ Example::
1903
+
1904
+ >>> import torch
1905
+ >>> torch.unravel_index(torch.tensor(4), (3, 2))
1906
+ (tensor(2),
1907
+ tensor(0))
1908
+
1909
+ >>> torch.unravel_index(torch.tensor([4, 1]), (3, 2))
1910
+ (tensor([2, 0]),
1911
+ tensor([0, 1]))
1912
+
1913
+ >>> torch.unravel_index(torch.tensor([0, 1, 2, 3, 4, 5]), (3, 2))
1914
+ (tensor([0, 0, 1, 1, 2, 2]),
1915
+ tensor([0, 1, 0, 1, 0, 1]))
1916
+
1917
+ >>> torch.unravel_index(torch.tensor([1234, 5678]), (10, 10, 10, 10))
1918
+ (tensor([1, 5]),
1919
+ tensor([2, 6]),
1920
+ tensor([3, 7]),
1921
+ tensor([4, 8]))
1922
+
1923
+ >>> torch.unravel_index(torch.tensor([[1234], [5678]]), (10, 10, 10, 10))
1924
+ (tensor([[1], [5]]),
1925
+ tensor([[2], [6]]),
1926
+ tensor([[3], [7]]),
1927
+ tensor([[4], [8]]))
1928
+
1929
+ >>> torch.unravel_index(torch.tensor([[1234], [5678]]), (100, 100))
1930
+ (tensor([[12], [56]]),
1931
+ tensor([[34], [78]]))
1932
+ """
1933
+ if has_torch_function_unary(indices):
1934
+ return handle_torch_function(unravel_index, (indices,), indices, shape=shape)
1935
+ res_tensor = _unravel_index(indices, shape)
1936
+ return res_tensor.unbind(-1)
1937
+
1938
+
1939
+ def _unravel_index(indices: Tensor, shape: Union[int, Sequence[int]]) -> Tensor:
1940
+ torch._check_type(
1941
+ not indices.is_complex()
1942
+ and not indices.is_floating_point()
1943
+ and not indices.dtype == torch.bool,
1944
+ lambda: f"expected 'indices' to be integer dtype, but got {indices.dtype}",
1945
+ )
1946
+
1947
+ torch._check_type(
1948
+ isinstance(shape, (int, torch.SymInt, Sequence)),
1949
+ lambda: f"expected 'shape' to be int or sequence of ints, but got {type(shape)}",
1950
+ )
1951
+
1952
+ if isinstance(shape, (int, torch.SymInt)):
1953
+ shape = torch.Size([shape])
1954
+ else:
1955
+ for dim in shape:
1956
+ torch._check_type(
1957
+ isinstance(dim, (int, torch.SymInt)),
1958
+ lambda: f"expected 'shape' sequence to only contain ints, but got {type(dim)}",
1959
+ )
1960
+ shape = torch.Size(shape)
1961
+
1962
+ torch._check_value(
1963
+ all(dim >= 0 for dim in shape),
1964
+ lambda: f"'shape' cannot have negative values, but got {tuple(shape)}",
1965
+ )
1966
+
1967
+ coefs = list(
1968
+ reversed(
1969
+ list(
1970
+ itertools.accumulate(
1971
+ reversed(shape[1:] + torch.Size([1])), func=operator.mul
1972
+ )
1973
+ )
1974
+ )
1975
+ )
1976
+ return indices.unsqueeze(-1).floor_divide(
1977
+ torch.tensor(coefs, device=indices.device, dtype=torch.int64)
1978
+ ) % torch.tensor(shape, device=indices.device, dtype=torch.int64)
1979
+
1980
+
1981
+ def chain_matmul(*matrices, out=None):
1982
+ r"""Returns the matrix product of the :math:`N` 2-D tensors. This product is efficiently computed
1983
+ using the matrix chain order algorithm which selects the order in which incurs the lowest cost in terms
1984
+ of arithmetic operations (`[CLRS]`_). Note that since this is a function to compute the product, :math:`N`
1985
+ needs to be greater than or equal to 2; if equal to 2 then a trivial matrix-matrix product is returned.
1986
+ If :math:`N` is 1, then this is a no-op - the original matrix is returned as is.
1987
+
1988
+ .. warning::
1989
+
1990
+ :func:`torch.chain_matmul` is deprecated and will be removed in a future PyTorch release.
1991
+ Use :func:`torch.linalg.multi_dot` instead, which accepts a list of two or more tensors
1992
+ rather than multiple arguments.
1993
+
1994
+ Args:
1995
+ matrices (Tensors...): a sequence of 2 or more 2-D tensors whose product is to be determined.
1996
+ out (Tensor, optional): the output tensor. Ignored if :attr:`out` = ``None``.
1997
+
1998
+ Returns:
1999
+ Tensor: if the :math:`i^{th}` tensor was of dimensions :math:`p_{i} \times p_{i + 1}`, then the product
2000
+ would be of dimensions :math:`p_{1} \times p_{N + 1}`.
2001
+
2002
+ Example::
2003
+
2004
+ >>> # xdoctest: +SKIP
2005
+ >>> # xdoctest: +IGNORE_WANT("non-deterministic")
2006
+ >>> a = torch.randn(3, 4)
2007
+ >>> b = torch.randn(4, 5)
2008
+ >>> c = torch.randn(5, 6)
2009
+ >>> d = torch.randn(6, 7)
2010
+ >>> # will raise a deprecation warning
2011
+ >>> torch.chain_matmul(a, b, c, d)
2012
+ tensor([[ -2.3375, -3.9790, -4.1119, -6.6577, 9.5609, -11.5095, -3.2614],
2013
+ [ 21.4038, 3.3378, -8.4982, -5.2457, -10.2561, -2.4684, 2.7163],
2014
+ [ -0.9647, -5.8917, -2.3213, -5.2284, 12.8615, -12.2816, -2.5095]])
2015
+
2016
+ .. _`[CLRS]`: https://mitpress.mit.edu/books/introduction-algorithms-third-edition
2017
+ """
2018
+ # This wrapper exists to support variadic args.
2019
+ if has_torch_function(matrices):
2020
+ return handle_torch_function(chain_matmul, matrices, *matrices)
2021
+
2022
+ if out is None:
2023
+ return _VF.chain_matmul(matrices) # type: ignore[attr-defined]
2024
+ else:
2025
+ return _VF.chain_matmul(matrices, out=out) # type: ignore[attr-defined]
2026
+
2027
+
2028
+ def _lu_impl(A, pivot=True, get_infos=False, out=None):
2029
+ # type: (Tensor, bool, bool, Any) -> tuple[Tensor, Tensor, Tensor]
2030
+ r"""Computes the LU factorization of a matrix or batches of matrices
2031
+ :attr:`A`. Returns a tuple containing the LU factorization and
2032
+ pivots of :attr:`A`. Pivoting is done if :attr:`pivot` is set to
2033
+ ``True``.
2034
+
2035
+ .. warning::
2036
+
2037
+ :func:`torch.lu` is deprecated in favor of :func:`torch.linalg.lu_factor`
2038
+ and :func:`torch.linalg.lu_factor_ex`. :func:`torch.lu` will be removed in a
2039
+ future PyTorch release.
2040
+ ``LU, pivots, info = torch.lu(A, compute_pivots)`` should be replaced with
2041
+
2042
+ .. code:: python
2043
+
2044
+ LU, pivots = torch.linalg.lu_factor(A, compute_pivots)
2045
+
2046
+ ``LU, pivots, info = torch.lu(A, compute_pivots, get_infos=True)`` should be replaced with
2047
+
2048
+ .. code:: python
2049
+
2050
+ LU, pivots, info = torch.linalg.lu_factor_ex(A, compute_pivots)
2051
+
2052
+ .. note::
2053
+ * The returned permutation matrix for every matrix in the batch is
2054
+ represented by a 1-indexed vector of size ``min(A.shape[-2], A.shape[-1])``.
2055
+ ``pivots[i] == j`` represents that in the ``i``-th step of the algorithm,
2056
+ the ``i``-th row was permuted with the ``j-1``-th row.
2057
+ * LU factorization with :attr:`pivot` = ``False`` is not available
2058
+ for CPU, and attempting to do so will throw an error. However,
2059
+ LU factorization with :attr:`pivot` = ``False`` is available for
2060
+ CUDA.
2061
+ * This function does not check if the factorization was successful
2062
+ or not if :attr:`get_infos` is ``True`` since the status of the
2063
+ factorization is present in the third element of the return tuple.
2064
+ * In the case of batches of square matrices with size less or equal
2065
+ to 32 on a CUDA device, the LU factorization is repeated for
2066
+ singular matrices due to the bug in the MAGMA library
2067
+ (see magma issue 13).
2068
+ * ``L``, ``U``, and ``P`` can be derived using :func:`torch.lu_unpack`.
2069
+
2070
+ .. warning::
2071
+ The gradients of this function will only be finite when :attr:`A` is full rank.
2072
+ This is because the LU decomposition is just differentiable at full rank matrices.
2073
+ Furthermore, if :attr:`A` is close to not being full rank,
2074
+ the gradient will be numerically unstable as it depends on the computation of :math:`L^{-1}` and :math:`U^{-1}`.
2075
+
2076
+ Args:
2077
+ A (Tensor): the tensor to factor of size :math:`(*, m, n)`
2078
+ pivot (bool, optional): Whether to compute the LU decomposition with partial pivoting, or the regular LU
2079
+ decomposition. :attr:`pivot`\ `= False` not supported on CPU. Default: `True`.
2080
+ get_infos (bool, optional): if set to ``True``, returns an info IntTensor.
2081
+ Default: ``False``
2082
+ out (tuple, optional): optional output tuple. If :attr:`get_infos` is ``True``,
2083
+ then the elements in the tuple are Tensor, IntTensor,
2084
+ and IntTensor. If :attr:`get_infos` is ``False``, then the
2085
+ elements in the tuple are Tensor, IntTensor. Default: ``None``
2086
+
2087
+ Returns:
2088
+ (Tensor, IntTensor, IntTensor (optional)): A tuple of tensors containing
2089
+
2090
+ - **factorization** (*Tensor*): the factorization of size :math:`(*, m, n)`
2091
+
2092
+ - **pivots** (*IntTensor*): the pivots of size :math:`(*, \text{min}(m, n))`.
2093
+ ``pivots`` stores all the intermediate transpositions of rows.
2094
+ The final permutation ``perm`` could be reconstructed by
2095
+ applying ``swap(perm[i], perm[pivots[i] - 1])`` for ``i = 0, ..., pivots.size(-1) - 1``,
2096
+ where ``perm`` is initially the identity permutation of :math:`m` elements
2097
+ (essentially this is what :func:`torch.lu_unpack` is doing).
2098
+
2099
+ - **infos** (*IntTensor*, *optional*): if :attr:`get_infos` is ``True``, this is a tensor of
2100
+ size :math:`(*)` where non-zero values indicate whether factorization for the matrix or
2101
+ each minibatch has succeeded or failed
2102
+
2103
+ Example::
2104
+
2105
+ >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_LAPACK)
2106
+ >>> # xdoctest: +IGNORE_WANT("non-deterministic")
2107
+ >>> A = torch.randn(2, 3, 3)
2108
+ >>> A_LU, pivots = torch.lu(A)
2109
+ >>> A_LU
2110
+ tensor([[[ 1.3506, 2.5558, -0.0816],
2111
+ [ 0.1684, 1.1551, 0.1940],
2112
+ [ 0.1193, 0.6189, -0.5497]],
2113
+
2114
+ [[ 0.4526, 1.2526, -0.3285],
2115
+ [-0.7988, 0.7175, -0.9701],
2116
+ [ 0.2634, -0.9255, -0.3459]]])
2117
+ >>> pivots
2118
+ tensor([[ 3, 3, 3],
2119
+ [ 3, 3, 3]], dtype=torch.int32)
2120
+ >>> A_LU, pivots, info = torch.lu(A, get_infos=True)
2121
+ >>> if info.nonzero().size(0) == 0:
2122
+ ... print('LU factorization succeeded for all samples!')
2123
+ LU factorization succeeded for all samples!
2124
+ """
2125
+ # If get_infos is True, then we don't need to check for errors and vice versa
2126
+ return torch._lu_with_info(A, pivot=pivot, check_errors=(not get_infos))
2127
+
2128
+
2129
+ if TYPE_CHECKING:
2130
+ _ListOrSeq = Sequence[Tensor]
2131
+ else:
2132
+ _ListOrSeq = list[Tensor]
2133
+
2134
+
2135
+ def _check_list_size(out_len: int, get_infos: bool, out: _ListOrSeq) -> None:
2136
+ get_infos_int = 1 if get_infos else 0
2137
+ if out_len - get_infos_int != 2:
2138
+ raise TypeError(
2139
+ f"expected tuple of {2 + int(get_infos)} elements but got {out_len}"
2140
+ )
2141
+ if not isinstance(out, (tuple, list)):
2142
+ raise TypeError(
2143
+ f"argument 'out' must be tuple of Tensors, not {type(out).__name__}"
2144
+ )
2145
+
2146
+
2147
+ def _lu_with_infos(A, pivot=True, get_infos=False, out=None):
2148
+ # type: (Tensor, bool, bool, Optional[tuple[Tensor, Tensor, Tensor]]) -> tuple[Tensor, Tensor, Tensor]
2149
+ if has_torch_function_unary(A):
2150
+ return handle_torch_function(
2151
+ lu, (A,), A, pivot=pivot, get_infos=get_infos, out=out
2152
+ )
2153
+ result = _lu_impl(A, pivot, get_infos, out)
2154
+ if out is not None:
2155
+ _check_list_size(len(out), get_infos, out)
2156
+ for i in range(len(out)):
2157
+ out[i].resize_as_(result[i]).copy_(result[i])
2158
+ return out
2159
+ else:
2160
+ return result # A_LU, pivots, infos
2161
+
2162
+
2163
+ def _lu_no_infos(A, pivot=True, get_infos=False, out=None):
2164
+ # type: (Tensor, bool, bool, Optional[tuple[Tensor, Tensor]]) -> tuple[Tensor, Tensor]
2165
+ # need to check for torch_function here so that we exit if
2166
+ if has_torch_function_unary(A):
2167
+ return handle_torch_function(
2168
+ lu, (A,), A, pivot=pivot, get_infos=get_infos, out=out
2169
+ )
2170
+ result = _lu_impl(A, pivot, get_infos, out)
2171
+ if out is not None:
2172
+ _check_list_size(len(out), get_infos, out)
2173
+ for i in range(len(out)):
2174
+ out[i].resize_as_(result[i]).copy_(result[i])
2175
+ return out
2176
+ else:
2177
+ return result[0], result[1] # A_LU, pivots
2178
+
2179
+
2180
+ # The return type of lu depends on `get_infos`, so in order to resolve the output type
2181
+ # of lu in TorchScript we need to statically know the value of `get_infos`
2182
+ lu = boolean_dispatch(
2183
+ arg_name="get_infos",
2184
+ arg_index=2,
2185
+ default=False,
2186
+ if_true=_lu_with_infos,
2187
+ if_false=_lu_no_infos,
2188
+ module_name=__name__,
2189
+ func_name="lu",
2190
+ )
2191
+ lu.__doc__ = _lu_impl.__doc__
2192
+
2193
+
2194
+ def align_tensors(*tensors):
2195
+ raise RuntimeError("`align_tensors` not yet implemented.")
lib/python3.12/site-packages/torch/hub.py ADDED
@@ -0,0 +1,880 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # mypy: allow-untyped-defs
2
+ import contextlib
3
+ import errno
4
+ import hashlib
5
+ import json
6
+ import os
7
+ import re
8
+ import shutil
9
+ import sys
10
+ import tempfile
11
+ import uuid
12
+ import warnings
13
+ import zipfile
14
+ from pathlib import Path
15
+ from typing import Any, Optional, Union
16
+ from typing_extensions import deprecated
17
+ from urllib.error import HTTPError, URLError
18
+ from urllib.parse import urlparse # noqa: F401
19
+ from urllib.request import Request, urlopen
20
+
21
+ import torch
22
+ from torch.serialization import MAP_LOCATION
23
+
24
+
25
+ class _Faketqdm: # type: ignore[no-redef]
26
+ def __init__(self, total=None, disable=False, unit=None, *args, **kwargs):
27
+ self.total = total
28
+ self.disable = disable
29
+ self.n = 0
30
+ # Ignore all extra *args and **kwargs lest you want to reinvent tqdm
31
+
32
+ def update(self, n):
33
+ if self.disable:
34
+ return
35
+
36
+ self.n += n
37
+ if self.total is None:
38
+ sys.stderr.write(f"\r{self.n:.1f} bytes")
39
+ else:
40
+ sys.stderr.write(f"\r{100 * self.n / float(self.total):.1f}%")
41
+ sys.stderr.flush()
42
+
43
+ # Don't bother implementing; use real tqdm if you want
44
+ def set_description(self, *args, **kwargs):
45
+ pass
46
+
47
+ def write(self, s):
48
+ sys.stderr.write(f"{s}\n")
49
+
50
+ def close(self):
51
+ self.disable = True
52
+
53
+ def __enter__(self):
54
+ return self
55
+
56
+ def __exit__(self, exc_type, exc_val, exc_tb):
57
+ if self.disable:
58
+ return
59
+
60
+ sys.stderr.write("\n")
61
+
62
+
63
+ try:
64
+ from tqdm import tqdm # If tqdm is installed use it, otherwise use the fake wrapper
65
+ except ImportError:
66
+ tqdm = _Faketqdm
67
+
68
+ __all__ = [
69
+ "download_url_to_file",
70
+ "get_dir",
71
+ "help",
72
+ "list",
73
+ "load",
74
+ "load_state_dict_from_url",
75
+ "set_dir",
76
+ ]
77
+
78
+ # matches bfd8deac from resnet18-bfd8deac.pth
79
+ HASH_REGEX = re.compile(r"-([a-f0-9]*)\.")
80
+
81
+ _TRUSTED_REPO_OWNERS = (
82
+ "facebookresearch",
83
+ "facebookincubator",
84
+ "pytorch",
85
+ "fairinternal",
86
+ )
87
+ ENV_GITHUB_TOKEN = "GITHUB_TOKEN"
88
+ ENV_TORCH_HOME = "TORCH_HOME"
89
+ ENV_XDG_CACHE_HOME = "XDG_CACHE_HOME"
90
+ DEFAULT_CACHE_DIR = "~/.cache"
91
+ VAR_DEPENDENCY = "dependencies"
92
+ MODULE_HUBCONF = "hubconf.py"
93
+ READ_DATA_CHUNK = 128 * 1024
94
+ _hub_dir: Optional[str] = None
95
+
96
+
97
+ @contextlib.contextmanager
98
+ def _add_to_sys_path(path):
99
+ sys.path.insert(0, path)
100
+ try:
101
+ yield
102
+ finally:
103
+ sys.path.remove(path)
104
+
105
+
106
+ # Copied from tools/shared/module_loader to be included in torch package
107
+ def _import_module(name, path):
108
+ import importlib.util
109
+ from importlib.abc import Loader
110
+
111
+ spec = importlib.util.spec_from_file_location(name, path)
112
+ assert spec is not None
113
+ module = importlib.util.module_from_spec(spec)
114
+ assert isinstance(spec.loader, Loader)
115
+ spec.loader.exec_module(module)
116
+ return module
117
+
118
+
119
+ def _remove_if_exists(path):
120
+ if os.path.exists(path):
121
+ if os.path.isfile(path):
122
+ os.remove(path)
123
+ else:
124
+ shutil.rmtree(path)
125
+
126
+
127
+ def _git_archive_link(repo_owner, repo_name, ref):
128
+ # See https://docs.github.com/en/rest/reference/repos#download-a-repository-archive-zip
129
+ return f"https://github.com/{repo_owner}/{repo_name}/zipball/{ref}"
130
+
131
+
132
+ def _load_attr_from_module(module, func_name):
133
+ # Check if callable is defined in the module
134
+ if func_name not in dir(module):
135
+ return None
136
+ return getattr(module, func_name)
137
+
138
+
139
+ def _get_torch_home():
140
+ torch_home = os.path.expanduser(
141
+ os.getenv(
142
+ ENV_TORCH_HOME,
143
+ os.path.join(os.getenv(ENV_XDG_CACHE_HOME, DEFAULT_CACHE_DIR), "torch"),
144
+ )
145
+ )
146
+ return torch_home
147
+
148
+
149
+ def _parse_repo_info(github):
150
+ if ":" in github:
151
+ repo_info, ref = github.split(":")
152
+ else:
153
+ repo_info, ref = github, None
154
+ repo_owner, repo_name = repo_info.split("/")
155
+
156
+ if ref is None:
157
+ # The ref wasn't specified by the user, so we need to figure out the
158
+ # default branch: main or master. Our assumption is that if main exists
159
+ # then it's the default branch, otherwise it's master.
160
+ try:
161
+ with urlopen(f"https://github.com/{repo_owner}/{repo_name}/tree/main/"):
162
+ ref = "main"
163
+ except HTTPError as e:
164
+ if e.code == 404:
165
+ ref = "master"
166
+ else:
167
+ raise
168
+ except URLError as e:
169
+ # No internet connection, need to check for cache as last resort
170
+ for possible_ref in ("main", "master"):
171
+ if os.path.exists(
172
+ f"{get_dir()}/{repo_owner}_{repo_name}_{possible_ref}"
173
+ ):
174
+ ref = possible_ref
175
+ break
176
+ if ref is None:
177
+ raise RuntimeError(
178
+ "It looks like there is no internet connection and the "
179
+ f"repo could not be found in the cache ({get_dir()})"
180
+ ) from e
181
+ return repo_owner, repo_name, ref
182
+
183
+
184
+ def _read_url(url):
185
+ with urlopen(url) as r:
186
+ return r.read().decode(r.headers.get_content_charset("utf-8"))
187
+
188
+
189
+ def _validate_not_a_forked_repo(repo_owner, repo_name, ref):
190
+ # Use urlopen to avoid depending on local git.
191
+ headers = {"Accept": "application/vnd.github.v3+json"}
192
+ token = os.environ.get(ENV_GITHUB_TOKEN)
193
+ if token is not None:
194
+ headers["Authorization"] = f"token {token}"
195
+ for url_prefix in (
196
+ f"https://api.github.com/repos/{repo_owner}/{repo_name}/branches",
197
+ f"https://api.github.com/repos/{repo_owner}/{repo_name}/tags",
198
+ ):
199
+ page = 0
200
+ while True:
201
+ page += 1
202
+ url = f"{url_prefix}?per_page=100&page={page}"
203
+ try:
204
+ response = json.loads(_read_url(Request(url, headers=headers)))
205
+ except HTTPError:
206
+ # Retry without token in case it had insufficient permissions.
207
+ del headers["Authorization"]
208
+ response = json.loads(_read_url(Request(url, headers=headers)))
209
+ # Empty response means no more data to process
210
+ if not response:
211
+ break
212
+ for br in response:
213
+ if br["name"] == ref or br["commit"]["sha"].startswith(ref):
214
+ return
215
+
216
+ raise ValueError(
217
+ f"Cannot find {ref} in https://github.com/{repo_owner}/{repo_name}. "
218
+ "If it's a commit from a forked repo, please call hub.load() with forked repo directly."
219
+ )
220
+
221
+
222
+ def _get_cache_or_reload(
223
+ github,
224
+ force_reload,
225
+ trust_repo,
226
+ calling_fn,
227
+ verbose=True,
228
+ skip_validation=False,
229
+ ):
230
+ # Setup hub_dir to save downloaded files
231
+ hub_dir = get_dir()
232
+ os.makedirs(hub_dir, exist_ok=True)
233
+ # Parse github repo information
234
+ repo_owner, repo_name, ref = _parse_repo_info(github)
235
+ # Github allows branch name with slash '/',
236
+ # this causes confusion with path on both Linux and Windows.
237
+ # Backslash is not allowed in Github branch name so no need to
238
+ # to worry about it.
239
+ normalized_br = ref.replace("/", "_")
240
+ # Github renames folder repo-v1.x.x to repo-1.x.x
241
+ # We don't know the repo name before downloading the zip file
242
+ # and inspect name from it.
243
+ # To check if cached repo exists, we need to normalize folder names.
244
+ owner_name_branch = "_".join([repo_owner, repo_name, normalized_br])
245
+ repo_dir = os.path.join(hub_dir, owner_name_branch)
246
+ # Check that the repo is in the trusted list
247
+ _check_repo_is_trusted(
248
+ repo_owner,
249
+ repo_name,
250
+ owner_name_branch,
251
+ trust_repo=trust_repo,
252
+ calling_fn=calling_fn,
253
+ )
254
+
255
+ use_cache = (not force_reload) and os.path.exists(repo_dir)
256
+
257
+ if use_cache:
258
+ if verbose:
259
+ sys.stderr.write(f"Using cache found in {repo_dir}\n")
260
+ else:
261
+ # Validate the tag/branch is from the original repo instead of a forked repo
262
+ if not skip_validation:
263
+ _validate_not_a_forked_repo(repo_owner, repo_name, ref)
264
+
265
+ cached_file = os.path.join(hub_dir, normalized_br + ".zip")
266
+ _remove_if_exists(cached_file)
267
+
268
+ try:
269
+ url = _git_archive_link(repo_owner, repo_name, ref)
270
+ sys.stdout.write(f'Downloading: "{url}" to {cached_file}\n')
271
+ download_url_to_file(url, cached_file, progress=False)
272
+ except HTTPError as err:
273
+ if err.code == 300:
274
+ # Getting a 300 Multiple Choices error likely means that the ref is both a tag and a branch
275
+ # in the repo. This can be disambiguated by explicitly using refs/heads/ or refs/tags
276
+ # See https://git-scm.com/book/en/v2/Git-Internals-Git-References
277
+ # Here, we do the same as git: we throw a warning, and assume the user wanted the branch
278
+ warnings.warn(
279
+ f"The ref {ref} is ambiguous. Perhaps it is both a tag and a branch in the repo? "
280
+ "Torchhub will now assume that it's a branch. "
281
+ "You can disambiguate tags and branches by explicitly passing refs/heads/branch_name or "
282
+ "refs/tags/tag_name as the ref. That might require using skip_validation=True."
283
+ )
284
+ disambiguated_branch_ref = f"refs/heads/{ref}"
285
+ url = _git_archive_link(
286
+ repo_owner, repo_name, ref=disambiguated_branch_ref
287
+ )
288
+ download_url_to_file(url, cached_file, progress=False)
289
+ else:
290
+ raise
291
+
292
+ with zipfile.ZipFile(cached_file) as cached_zipfile:
293
+ extraced_repo_name = cached_zipfile.infolist()[0].filename
294
+ extracted_repo = os.path.join(hub_dir, extraced_repo_name)
295
+ _remove_if_exists(extracted_repo)
296
+ # Unzip the code and rename the base folder
297
+ cached_zipfile.extractall(hub_dir)
298
+
299
+ _remove_if_exists(cached_file)
300
+ _remove_if_exists(repo_dir)
301
+ shutil.move(extracted_repo, repo_dir) # rename the repo
302
+
303
+ return repo_dir
304
+
305
+
306
+ def _check_repo_is_trusted(
307
+ repo_owner,
308
+ repo_name,
309
+ owner_name_branch,
310
+ trust_repo,
311
+ calling_fn="load",
312
+ ):
313
+ hub_dir = get_dir()
314
+ filepath = os.path.join(hub_dir, "trusted_list")
315
+
316
+ if not os.path.exists(filepath):
317
+ Path(filepath).touch()
318
+ with open(filepath) as file:
319
+ trusted_repos = tuple(line.strip() for line in file)
320
+
321
+ # To minimize friction of introducing the new trust_repo mechanism, we consider that
322
+ # if a repo was already downloaded by torchhub, then it is already trusted (even if it's not in the allowlist)
323
+ trusted_repos_legacy = next(os.walk(hub_dir))[1]
324
+
325
+ owner_name = "_".join([repo_owner, repo_name])
326
+ is_trusted = (
327
+ owner_name in trusted_repos
328
+ or owner_name_branch in trusted_repos_legacy
329
+ or repo_owner in _TRUSTED_REPO_OWNERS
330
+ )
331
+
332
+ # TODO: Remove `None` option in 2.0 and change the default to "check"
333
+ if trust_repo is None:
334
+ if not is_trusted:
335
+ warnings.warn(
336
+ "You are about to download and run code from an untrusted repository. In a future release, this won't "
337
+ "be allowed. To add the repository to your trusted list, change the command to {calling_fn}(..., "
338
+ "trust_repo=False) and a command prompt will appear asking for an explicit confirmation of trust, "
339
+ f"or {calling_fn}(..., trust_repo=True), which will assume that the prompt is to be answered with "
340
+ f"'yes'. You can also use {calling_fn}(..., trust_repo='check') which will only prompt for "
341
+ f"confirmation if the repo is not already trusted. This will eventually be the default behaviour"
342
+ )
343
+ return
344
+
345
+ if (trust_repo is False) or (trust_repo == "check" and not is_trusted):
346
+ response = input(
347
+ f"The repository {owner_name} does not belong to the list of trusted repositories and as such cannot be downloaded. "
348
+ "Do you trust this repository and wish to add it to the trusted list of repositories (y/N)?"
349
+ )
350
+ if response.lower() in ("y", "yes"):
351
+ if is_trusted:
352
+ print("The repository is already trusted.")
353
+ elif response.lower() in ("n", "no", ""):
354
+ raise Exception("Untrusted repository.") # noqa: TRY002
355
+ else:
356
+ raise ValueError(f"Unrecognized response {response}.")
357
+
358
+ # At this point we're sure that the user trusts the repo (or wants to trust it)
359
+ if not is_trusted:
360
+ with open(filepath, "a") as file:
361
+ file.write(owner_name + "\n")
362
+
363
+
364
+ def _check_module_exists(name):
365
+ import importlib.util
366
+
367
+ return importlib.util.find_spec(name) is not None
368
+
369
+
370
+ def _check_dependencies(m):
371
+ dependencies = _load_attr_from_module(m, VAR_DEPENDENCY)
372
+
373
+ if dependencies is not None:
374
+ missing_deps = [pkg for pkg in dependencies if not _check_module_exists(pkg)]
375
+ if len(missing_deps):
376
+ raise RuntimeError(f"Missing dependencies: {', '.join(missing_deps)}")
377
+
378
+
379
+ def _load_entry_from_hubconf(m, model):
380
+ if not isinstance(model, str):
381
+ raise ValueError("Invalid input: model should be a string of function name")
382
+
383
+ # Note that if a missing dependency is imported at top level of hubconf, it will
384
+ # throw before this function. It's a chicken and egg situation where we have to
385
+ # load hubconf to know what're the dependencies, but to import hubconf it requires
386
+ # a missing package. This is fine, Python will throw proper error message for users.
387
+ _check_dependencies(m)
388
+
389
+ func = _load_attr_from_module(m, model)
390
+
391
+ if func is None or not callable(func):
392
+ raise RuntimeError(f"Cannot find callable {model} in hubconf")
393
+
394
+ return func
395
+
396
+
397
+ def get_dir() -> str:
398
+ r"""
399
+ Get the Torch Hub cache directory used for storing downloaded models & weights.
400
+
401
+ If :func:`~torch.hub.set_dir` is not called, default path is ``$TORCH_HOME/hub`` where
402
+ environment variable ``$TORCH_HOME`` defaults to ``$XDG_CACHE_HOME/torch``.
403
+ ``$XDG_CACHE_HOME`` follows the X Design Group specification of the Linux
404
+ filesystem layout, with a default value ``~/.cache`` if the environment
405
+ variable is not set.
406
+ """
407
+ # Issue warning to move data if old env is set
408
+ if os.getenv("TORCH_HUB"):
409
+ warnings.warn("TORCH_HUB is deprecated, please use env TORCH_HOME instead")
410
+
411
+ if _hub_dir is not None:
412
+ return _hub_dir
413
+ return os.path.join(_get_torch_home(), "hub")
414
+
415
+
416
+ def set_dir(d: Union[str, os.PathLike]) -> None:
417
+ r"""
418
+ Optionally set the Torch Hub directory used to save downloaded models & weights.
419
+
420
+ Args:
421
+ d (str): path to a local folder to save downloaded models & weights.
422
+ """
423
+ global _hub_dir
424
+ _hub_dir = os.path.expanduser(d)
425
+
426
+
427
+ def list(
428
+ github,
429
+ force_reload=False,
430
+ skip_validation=False,
431
+ trust_repo=None,
432
+ verbose=True,
433
+ ):
434
+ r"""
435
+ List all callable entrypoints available in the repo specified by ``github``.
436
+
437
+ Args:
438
+ github (str): a string with format "repo_owner/repo_name[:ref]" with an optional
439
+ ref (tag or branch). If ``ref`` is not specified, the default branch is assumed to be ``main`` if
440
+ it exists, and otherwise ``master``.
441
+ Example: 'pytorch/vision:0.10'
442
+ force_reload (bool, optional): whether to discard the existing cache and force a fresh download.
443
+ Default is ``False``.
444
+ skip_validation (bool, optional): if ``False``, torchhub will check that the branch or commit
445
+ specified by the ``github`` argument properly belongs to the repo owner. This will make
446
+ requests to the GitHub API; you can specify a non-default GitHub token by setting the
447
+ ``GITHUB_TOKEN`` environment variable. Default is ``False``.
448
+ trust_repo (bool, str or None): ``"check"``, ``True``, ``False`` or ``None``.
449
+ This parameter was introduced in v1.12 and helps ensuring that users
450
+ only run code from repos that they trust.
451
+
452
+ - If ``False``, a prompt will ask the user whether the repo should
453
+ be trusted.
454
+ - If ``True``, the repo will be added to the trusted list and loaded
455
+ without requiring explicit confirmation.
456
+ - If ``"check"``, the repo will be checked against the list of
457
+ trusted repos in the cache. If it is not present in that list, the
458
+ behaviour will fall back onto the ``trust_repo=False`` option.
459
+ - If ``None``: this will raise a warning, inviting the user to set
460
+ ``trust_repo`` to either ``False``, ``True`` or ``"check"``. This
461
+ is only present for backward compatibility and will be removed in
462
+ v2.0.
463
+
464
+ Default is ``None`` and will eventually change to ``"check"`` in v2.0.
465
+ verbose (bool, optional): If ``False``, mute messages about hitting
466
+ local caches. Note that the message about first download cannot be
467
+ muted. Default is ``True``.
468
+
469
+ Returns:
470
+ list: The available callables entrypoint
471
+
472
+ Example:
473
+ >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_HUB)
474
+ >>> entrypoints = torch.hub.list("pytorch/vision", force_reload=True)
475
+ """
476
+ repo_dir = _get_cache_or_reload(
477
+ github,
478
+ force_reload,
479
+ trust_repo,
480
+ "list",
481
+ verbose=verbose,
482
+ skip_validation=skip_validation,
483
+ )
484
+
485
+ with _add_to_sys_path(repo_dir):
486
+ hubconf_path = os.path.join(repo_dir, MODULE_HUBCONF)
487
+ hub_module = _import_module(MODULE_HUBCONF, hubconf_path)
488
+
489
+ # We take functions starts with '_' as internal helper functions
490
+ entrypoints = [
491
+ f
492
+ for f in dir(hub_module)
493
+ if callable(getattr(hub_module, f)) and not f.startswith("_")
494
+ ]
495
+
496
+ return entrypoints
497
+
498
+
499
+ def help(github, model, force_reload=False, skip_validation=False, trust_repo=None):
500
+ r"""
501
+ Show the docstring of entrypoint ``model``.
502
+
503
+ Args:
504
+ github (str): a string with format <repo_owner/repo_name[:ref]> with an optional
505
+ ref (a tag or a branch). If ``ref`` is not specified, the default branch is assumed
506
+ to be ``main`` if it exists, and otherwise ``master``.
507
+ Example: 'pytorch/vision:0.10'
508
+ model (str): a string of entrypoint name defined in repo's ``hubconf.py``
509
+ force_reload (bool, optional): whether to discard the existing cache and force a fresh download.
510
+ Default is ``False``.
511
+ skip_validation (bool, optional): if ``False``, torchhub will check that the ref
512
+ specified by the ``github`` argument properly belongs to the repo owner. This will make
513
+ requests to the GitHub API; you can specify a non-default GitHub token by setting the
514
+ ``GITHUB_TOKEN`` environment variable. Default is ``False``.
515
+ trust_repo (bool, str or None): ``"check"``, ``True``, ``False`` or ``None``.
516
+ This parameter was introduced in v1.12 and helps ensuring that users
517
+ only run code from repos that they trust.
518
+
519
+ - If ``False``, a prompt will ask the user whether the repo should
520
+ be trusted.
521
+ - If ``True``, the repo will be added to the trusted list and loaded
522
+ without requiring explicit confirmation.
523
+ - If ``"check"``, the repo will be checked against the list of
524
+ trusted repos in the cache. If it is not present in that list, the
525
+ behaviour will fall back onto the ``trust_repo=False`` option.
526
+ - If ``None``: this will raise a warning, inviting the user to set
527
+ ``trust_repo`` to either ``False``, ``True`` or ``"check"``. This
528
+ is only present for backward compatibility and will be removed in
529
+ v2.0.
530
+
531
+ Default is ``None`` and will eventually change to ``"check"`` in v2.0.
532
+ Example:
533
+ >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_HUB)
534
+ >>> print(torch.hub.help("pytorch/vision", "resnet18", force_reload=True))
535
+ """
536
+ repo_dir = _get_cache_or_reload(
537
+ github,
538
+ force_reload,
539
+ trust_repo,
540
+ "help",
541
+ verbose=True,
542
+ skip_validation=skip_validation,
543
+ )
544
+
545
+ with _add_to_sys_path(repo_dir):
546
+ hubconf_path = os.path.join(repo_dir, MODULE_HUBCONF)
547
+ hub_module = _import_module(MODULE_HUBCONF, hubconf_path)
548
+
549
+ entry = _load_entry_from_hubconf(hub_module, model)
550
+
551
+ return entry.__doc__
552
+
553
+
554
+ def load(
555
+ repo_or_dir,
556
+ model,
557
+ *args,
558
+ source="github",
559
+ trust_repo=None,
560
+ force_reload=False,
561
+ verbose=True,
562
+ skip_validation=False,
563
+ **kwargs,
564
+ ):
565
+ r"""
566
+ Load a model from a github repo or a local directory.
567
+
568
+ Note: Loading a model is the typical use case, but this can also be used to
569
+ for loading other objects such as tokenizers, loss functions, etc.
570
+
571
+ If ``source`` is 'github', ``repo_or_dir`` is expected to be
572
+ of the form ``repo_owner/repo_name[:ref]`` with an optional
573
+ ref (a tag or a branch).
574
+
575
+ If ``source`` is 'local', ``repo_or_dir`` is expected to be a
576
+ path to a local directory.
577
+
578
+ Args:
579
+ repo_or_dir (str): If ``source`` is 'github',
580
+ this should correspond to a github repo with format ``repo_owner/repo_name[:ref]`` with
581
+ an optional ref (tag or branch), for example 'pytorch/vision:0.10'. If ``ref`` is not specified,
582
+ the default branch is assumed to be ``main`` if it exists, and otherwise ``master``.
583
+ If ``source`` is 'local' then it should be a path to a local directory.
584
+ model (str): the name of a callable (entrypoint) defined in the
585
+ repo/dir's ``hubconf.py``.
586
+ *args (optional): the corresponding args for callable ``model``.
587
+ source (str, optional): 'github' or 'local'. Specifies how
588
+ ``repo_or_dir`` is to be interpreted. Default is 'github'.
589
+ trust_repo (bool, str or None): ``"check"``, ``True``, ``False`` or ``None``.
590
+ This parameter was introduced in v1.12 and helps ensuring that users
591
+ only run code from repos that they trust.
592
+
593
+ - If ``False``, a prompt will ask the user whether the repo should
594
+ be trusted.
595
+ - If ``True``, the repo will be added to the trusted list and loaded
596
+ without requiring explicit confirmation.
597
+ - If ``"check"``, the repo will be checked against the list of
598
+ trusted repos in the cache. If it is not present in that list, the
599
+ behaviour will fall back onto the ``trust_repo=False`` option.
600
+ - If ``None``: this will raise a warning, inviting the user to set
601
+ ``trust_repo`` to either ``False``, ``True`` or ``"check"``. This
602
+ is only present for backward compatibility and will be removed in
603
+ v2.0.
604
+
605
+ Default is ``None`` and will eventually change to ``"check"`` in v2.0.
606
+ force_reload (bool, optional): whether to force a fresh download of
607
+ the github repo unconditionally. Does not have any effect if
608
+ ``source = 'local'``. Default is ``False``.
609
+ verbose (bool, optional): If ``False``, mute messages about hitting
610
+ local caches. Note that the message about first download cannot be
611
+ muted. Does not have any effect if ``source = 'local'``.
612
+ Default is ``True``.
613
+ skip_validation (bool, optional): if ``False``, torchhub will check that the branch or commit
614
+ specified by the ``github`` argument properly belongs to the repo owner. This will make
615
+ requests to the GitHub API; you can specify a non-default GitHub token by setting the
616
+ ``GITHUB_TOKEN`` environment variable. Default is ``False``.
617
+ **kwargs (optional): the corresponding kwargs for callable ``model``.
618
+
619
+ Returns:
620
+ The output of the ``model`` callable when called with the given
621
+ ``*args`` and ``**kwargs``.
622
+
623
+ Example:
624
+ >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_HUB)
625
+ >>> # from a github repo
626
+ >>> repo = "pytorch/vision"
627
+ >>> model = torch.hub.load(
628
+ ... repo, "resnet50", weights="ResNet50_Weights.IMAGENET1K_V1"
629
+ ... )
630
+ >>> # from a local directory
631
+ >>> path = "/some/local/path/pytorch/vision"
632
+ >>> # xdoctest: +SKIP
633
+ >>> model = torch.hub.load(path, "resnet50", weights="ResNet50_Weights.DEFAULT")
634
+ """
635
+ source = source.lower()
636
+
637
+ if source not in ("github", "local"):
638
+ raise ValueError(
639
+ f'Unknown source: "{source}". Allowed values: "github" | "local".'
640
+ )
641
+
642
+ if source == "github":
643
+ repo_or_dir = _get_cache_or_reload(
644
+ repo_or_dir,
645
+ force_reload,
646
+ trust_repo,
647
+ "load",
648
+ verbose=verbose,
649
+ skip_validation=skip_validation,
650
+ )
651
+
652
+ model = _load_local(repo_or_dir, model, *args, **kwargs)
653
+ return model
654
+
655
+
656
+ def _load_local(hubconf_dir, model, *args, **kwargs):
657
+ r"""
658
+ Load a model from a local directory with a ``hubconf.py``.
659
+
660
+ Args:
661
+ hubconf_dir (str): path to a local directory that contains a
662
+ ``hubconf.py``.
663
+ model (str): name of an entrypoint defined in the directory's
664
+ ``hubconf.py``.
665
+ *args (optional): the corresponding args for callable ``model``.
666
+ **kwargs (optional): the corresponding kwargs for callable ``model``.
667
+
668
+ Returns:
669
+ a single model with corresponding pretrained weights.
670
+
671
+ Example:
672
+ >>> # xdoctest: +SKIP("stub local path")
673
+ >>> path = "/some/local/path/pytorch/vision"
674
+ >>> model = _load_local(
675
+ ... path,
676
+ ... "resnet50",
677
+ ... weights="ResNet50_Weights.IMAGENET1K_V1",
678
+ ... )
679
+ """
680
+ with _add_to_sys_path(hubconf_dir):
681
+ hubconf_path = os.path.join(hubconf_dir, MODULE_HUBCONF)
682
+ hub_module = _import_module(MODULE_HUBCONF, hubconf_path)
683
+
684
+ entry = _load_entry_from_hubconf(hub_module, model)
685
+ model = entry(*args, **kwargs)
686
+
687
+ return model
688
+
689
+
690
+ def download_url_to_file(
691
+ url: str,
692
+ dst: str,
693
+ hash_prefix: Optional[str] = None,
694
+ progress: bool = True,
695
+ ) -> None:
696
+ r"""Download object at the given URL to a local path.
697
+
698
+ Args:
699
+ url (str): URL of the object to download
700
+ dst (str): Full path where object will be saved, e.g. ``/tmp/temporary_file``
701
+ hash_prefix (str, optional): If not None, the SHA256 downloaded file should start with ``hash_prefix``.
702
+ Default: None
703
+ progress (bool, optional): whether or not to display a progress bar to stderr
704
+ Default: True
705
+
706
+ Example:
707
+ >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_HUB)
708
+ >>> # xdoctest: +REQUIRES(POSIX)
709
+ >>> torch.hub.download_url_to_file(
710
+ ... "https://s3.amazonaws.com/pytorch/models/resnet18-5c106cde.pth",
711
+ ... "/tmp/temporary_file",
712
+ ... )
713
+
714
+ """
715
+ file_size = None
716
+ req = Request(url, headers={"User-Agent": "torch.hub"})
717
+ u = urlopen(req)
718
+ meta = u.info()
719
+ if hasattr(meta, "getheaders"):
720
+ content_length = meta.getheaders("Content-Length")
721
+ else:
722
+ content_length = meta.get_all("Content-Length")
723
+ if content_length is not None and len(content_length) > 0:
724
+ file_size = int(content_length[0])
725
+
726
+ # We deliberately save it in a temp file and move it after
727
+ # download is complete. This prevents a local working checkpoint
728
+ # being overridden by a broken download.
729
+ # We deliberately do not use NamedTemporaryFile to avoid restrictive
730
+ # file permissions being applied to the downloaded file.
731
+ dst = os.path.expanduser(dst)
732
+ for _ in range(tempfile.TMP_MAX):
733
+ tmp_dst = dst + "." + uuid.uuid4().hex + ".partial"
734
+ try:
735
+ f = open(tmp_dst, "w+b")
736
+ except FileExistsError:
737
+ continue
738
+ break
739
+ else:
740
+ raise FileExistsError(errno.EEXIST, "No usable temporary file name found")
741
+
742
+ try:
743
+ if hash_prefix is not None:
744
+ sha256 = hashlib.sha256()
745
+ with tqdm(
746
+ total=file_size,
747
+ disable=not progress,
748
+ unit="B",
749
+ unit_scale=True,
750
+ unit_divisor=1024,
751
+ ) as pbar:
752
+ while True:
753
+ buffer = u.read(READ_DATA_CHUNK)
754
+ if len(buffer) == 0:
755
+ break
756
+ f.write(buffer) # type: ignore[possibly-undefined]
757
+ if hash_prefix is not None:
758
+ sha256.update(buffer) # type: ignore[possibly-undefined]
759
+ pbar.update(len(buffer))
760
+
761
+ f.close()
762
+ if hash_prefix is not None:
763
+ digest = sha256.hexdigest() # type: ignore[possibly-undefined]
764
+ if digest[: len(hash_prefix)] != hash_prefix:
765
+ raise RuntimeError(
766
+ f'invalid hash value (expected "{hash_prefix}", got "{digest}")'
767
+ )
768
+ shutil.move(f.name, dst)
769
+ finally:
770
+ f.close()
771
+ if os.path.exists(f.name):
772
+ os.remove(f.name)
773
+
774
+
775
+ # Hub used to support automatically extracts from zipfile manually compressed by users.
776
+ # The legacy zip format expects only one file from torch.save() < 1.6 in the zip.
777
+ # We should remove this support since zipfile is now default zipfile format for torch.save().
778
+ def _is_legacy_zip_format(filename: str) -> bool:
779
+ if zipfile.is_zipfile(filename):
780
+ infolist = zipfile.ZipFile(filename).infolist()
781
+ return len(infolist) == 1 and not infolist[0].is_dir()
782
+ return False
783
+
784
+
785
+ @deprecated(
786
+ "Falling back to the old format < 1.6. This support will be "
787
+ "deprecated in favor of default zipfile format introduced in 1.6. "
788
+ "Please redo torch.save() to save it in the new zipfile format.",
789
+ category=FutureWarning,
790
+ )
791
+ def _legacy_zip_load(
792
+ filename: str,
793
+ model_dir: str,
794
+ map_location: MAP_LOCATION,
795
+ weights_only: bool,
796
+ ) -> dict[str, Any]:
797
+ # Note: extractall() defaults to overwrite file if exists. No need to clean up beforehand.
798
+ # We deliberately don't handle tarfile here since our legacy serialization format was in tar.
799
+ # E.g. resnet18-5c106cde.pth which is widely used.
800
+ with zipfile.ZipFile(filename) as f:
801
+ members = f.infolist()
802
+ if len(members) != 1:
803
+ raise RuntimeError("Only one file(not dir) is allowed in the zipfile")
804
+ f.extractall(model_dir)
805
+ extraced_name = members[0].filename
806
+ extracted_file = os.path.join(model_dir, extraced_name)
807
+ return torch.load(
808
+ extracted_file, map_location=map_location, weights_only=weights_only
809
+ )
810
+
811
+
812
+ def load_state_dict_from_url(
813
+ url: str,
814
+ model_dir: Optional[str] = None,
815
+ map_location: MAP_LOCATION = None,
816
+ progress: bool = True,
817
+ check_hash: bool = False,
818
+ file_name: Optional[str] = None,
819
+ weights_only: bool = False,
820
+ ) -> dict[str, Any]:
821
+ r"""Loads the Torch serialized object at the given URL.
822
+
823
+ If downloaded file is a zip file, it will be automatically
824
+ decompressed.
825
+
826
+ If the object is already present in `model_dir`, it's deserialized and
827
+ returned.
828
+ The default value of ``model_dir`` is ``<hub_dir>/checkpoints`` where
829
+ ``hub_dir`` is the directory returned by :func:`~torch.hub.get_dir`.
830
+
831
+ Args:
832
+ url (str): URL of the object to download
833
+ model_dir (str, optional): directory in which to save the object
834
+ map_location (optional): a function or a dict specifying how to remap storage locations (see torch.load)
835
+ progress (bool, optional): whether or not to display a progress bar to stderr.
836
+ Default: True
837
+ check_hash(bool, optional): If True, the filename part of the URL should follow the naming convention
838
+ ``filename-<sha256>.ext`` where ``<sha256>`` is the first eight or more
839
+ digits of the SHA256 hash of the contents of the file. The hash is used to
840
+ ensure unique names and to verify the contents of the file.
841
+ Default: False
842
+ file_name (str, optional): name for the downloaded file. Filename from ``url`` will be used if not set.
843
+ weights_only(bool, optional): If True, only weights will be loaded and no complex pickled objects.
844
+ Recommended for untrusted sources. See :func:`~torch.load` for more details.
845
+
846
+ Example:
847
+ >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_HUB)
848
+ >>> state_dict = torch.hub.load_state_dict_from_url(
849
+ ... "https://s3.amazonaws.com/pytorch/models/resnet18-5c106cde.pth"
850
+ ... )
851
+
852
+ """
853
+ # Issue warning to move data if old env is set
854
+ if os.getenv("TORCH_MODEL_ZOO"):
855
+ warnings.warn(
856
+ "TORCH_MODEL_ZOO is deprecated, please use env TORCH_HOME instead"
857
+ )
858
+
859
+ if model_dir is None:
860
+ hub_dir = get_dir()
861
+ model_dir = os.path.join(hub_dir, "checkpoints")
862
+
863
+ os.makedirs(model_dir, exist_ok=True)
864
+
865
+ parts = urlparse(url)
866
+ filename = os.path.basename(parts.path)
867
+ if file_name is not None:
868
+ filename = file_name
869
+ cached_file = os.path.join(model_dir, filename)
870
+ if not os.path.exists(cached_file):
871
+ sys.stdout.write(f'Downloading: "{url}" to {cached_file}\n')
872
+ hash_prefix = None
873
+ if check_hash:
874
+ r = HASH_REGEX.search(filename) # r is Optional[Match[str]]
875
+ hash_prefix = r.group(1) if r else None
876
+ download_url_to_file(url, cached_file, hash_prefix, progress=progress)
877
+
878
+ if _is_legacy_zip_format(cached_file):
879
+ return _legacy_zip_load(cached_file, model_dir, map_location, weights_only)
880
+ return torch.load(cached_file, map_location=map_location, weights_only=weights_only)
lib/python3.12/site-packages/torch/include/ATen/ATen.h ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #if !defined(_MSC_VER) && __cplusplus < 201703L
4
+ #error C++17 or later compatible compiler is required to use ATen.
5
+ #endif
6
+
7
+ #include <ATen/Context.h>
8
+ #include <ATen/Device.h>
9
+ #include <ATen/DeviceGuard.h>
10
+ #include <ATen/DimVector.h>
11
+ #include <ATen/Dispatch.h>
12
+ #include <ATen/Formatting.h>
13
+ #include <ATen/Functions.h>
14
+ #include <ATen/NamedTensor.h>
15
+ #include <ATen/ScalarOps.h>
16
+ #include <ATen/Tensor.h>
17
+ #include <ATen/TensorGeometry.h>
18
+ #include <ATen/TensorIndexing.h>
19
+ #include <ATen/TensorOperators.h>
20
+ #include <ATen/Version.h>
21
+ #include <ATen/core/ATenGeneral.h>
22
+ #include <ATen/core/Generator.h>
23
+ #include <ATen/core/Reduction.h>
24
+ #include <ATen/core/Scalar.h>
25
+ #include <ATen/core/UnsafeFromTH.h>
26
+ #include <ATen/core/ivalue.h>
27
+ #include <ATen/core/jit_type.h>
28
+ #include <c10/core/Allocator.h>
29
+ #include <c10/core/InferenceMode.h>
30
+ #include <c10/core/Layout.h>
31
+ #include <c10/core/Storage.h>
32
+ #include <c10/core/TensorOptions.h>
33
+ #include <c10/util/Exception.h>
34
+
35
+ // TODO: try to remove this
36
+ // There is some back story, see https://github.com/pytorch/pytorch/issues/48684
37
+ #include <ATen/NativeFunctions.h>
lib/python3.12/site-packages/torch/include/ATen/AccumulateType.h ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+ #include <ATen/Config.h>
3
+ #include <c10/core/DeviceType.h>
4
+ #include <c10/core/ScalarType.h>
5
+ #include <c10/util/BFloat16.h>
6
+ #include <c10/util/Float8_e4m3fn.h>
7
+ #include <c10/util/Float8_e4m3fnuz.h>
8
+ #include <c10/util/Float8_e5m2.h>
9
+ #include <c10/util/Float8_e5m2fnuz.h>
10
+ #include <c10/util/Half.h>
11
+
12
+ // Defines the accumulation type for a scalar type.
13
+ // Example:
14
+ // using accscalar_t = acc_type<scalar_t, /*is_cuda*/true>;
15
+ //
16
+ // Accumulation types are an important concept in numeric computing
17
+ // because you frequently want to perform intermediate computations
18
+ // at a higher precision than the input and output precision, to avoid
19
+ // compounding internal rounding errors. Accumulation is the most
20
+ // well-known intermediate computation (it is of great importance for
21
+ // sum reduction and matrix multiply, for example), but in PyTorch
22
+ // acc_type ends up getting used for all sorts of other intermediate
23
+ // computations, so it perhaps would be more accurately (ahem) called an
24
+ // "accurate" type. acc_type is especially important for reduced
25
+ // precision operations like float16 and bfloat16, where relatively
26
+ // benign looking inputs can easily end up overflowing/underflowing.
27
+ //
28
+ // acc_type is parametrized by whether or not you are running on CUDA
29
+ // or not, because on CUDA double precision operations are expensive
30
+ // and so by default, we don't actually want to use double as an
31
+ // acc_type on CUDA. A lot of things are typed out below, but
32
+ // basically, the table is generated by a few rules:
33
+ //
34
+ // If bool:
35
+ // Use 'bool' as acc_type.
36
+ // If floating point:
37
+ // If CUDA, use 'float' as acc_type (unless scalar_t is double),
38
+ // otherwise (CPU) use 'double'
39
+ // If integral:
40
+ // Use 'int64_t' as acc_type
41
+ //
42
+ // You're not forced to use this template; if you happen to know
43
+ // something specific about your use case, you can specify your own
44
+ // desired behavior. This template, however, will give you a reasonable
45
+ // default that will work for all dtypes supported in PyTorch.
46
+
47
+ #if defined(__CUDACC__)
48
+ #include <cuda.h>
49
+ #include <cuda_fp16.h>
50
+ #elif defined(__HIPCC__)
51
+ #include <hip/hip_fp16.h>
52
+ #include <hip/hip_runtime.h>
53
+ #endif
54
+
55
+ namespace at {
56
+
57
+ template <typename T, c10::DeviceType D>
58
+ struct AccumulateTypeDevice {};
59
+
60
+ template <typename T, bool>
61
+ struct AccumulateType {};
62
+
63
+ template <typename T>
64
+ struct AccumulateType<T, false> {
65
+ using type = typename AccumulateTypeDevice<T, c10::DeviceType::CPU>::type;
66
+ };
67
+
68
+ template <typename T>
69
+ struct AccumulateType<T, true> {
70
+ using type = typename AccumulateTypeDevice<T, c10::DeviceType::CUDA>::type;
71
+ };
72
+
73
+ template <typename T, c10::DeviceType device>
74
+ using acc_type_device = typename AccumulateTypeDevice<T, device>::type;
75
+
76
+ template <typename T, bool is_cuda>
77
+ using acc_type = typename AccumulateType<T, is_cuda>::type;
78
+
79
+ #define ACC_TYPE(t, acc_t, device_type) \
80
+ template <> \
81
+ struct AccumulateTypeDevice<t, device_type> { \
82
+ using type = acc_t; \
83
+ };
84
+ #define MPS_ACC_TYPE(t, acc_t) ACC_TYPE(t, acc_t, c10::DeviceType::MPS)
85
+ #define XPU_ACC_TYPE(t, acc_t) ACC_TYPE(t, acc_t, c10::DeviceType::XPU)
86
+ #define CUDA_ACC_TYPE(t, acc_t) ACC_TYPE(t, acc_t, c10::DeviceType::CUDA)
87
+ #define CPU_ACC_TYPE(t, acc_t) ACC_TYPE(t, acc_t, c10::DeviceType::CPU)
88
+
89
+ MPS_ACC_TYPE(BFloat16, float)
90
+ MPS_ACC_TYPE(Half, float)
91
+ MPS_ACC_TYPE(Float8_e5m2, float)
92
+ MPS_ACC_TYPE(Float8_e4m3fn, float)
93
+ MPS_ACC_TYPE(Float8_e5m2fnuz, float)
94
+ MPS_ACC_TYPE(Float8_e4m3fnuz, float)
95
+ MPS_ACC_TYPE(float, float)
96
+ MPS_ACC_TYPE(double, float)
97
+ MPS_ACC_TYPE(int8_t, int64_t)
98
+ MPS_ACC_TYPE(uint8_t, int64_t)
99
+ MPS_ACC_TYPE(char, int64_t)
100
+ MPS_ACC_TYPE(int16_t, int64_t)
101
+ MPS_ACC_TYPE(int32_t, int64_t)
102
+ MPS_ACC_TYPE(int64_t, int64_t)
103
+ MPS_ACC_TYPE(bool, bool)
104
+ MPS_ACC_TYPE(c10::complex<Half>, c10::complex<float>)
105
+ MPS_ACC_TYPE(c10::complex<float>, c10::complex<float>)
106
+ MPS_ACC_TYPE(c10::complex<double>, c10::complex<float>)
107
+
108
+ XPU_ACC_TYPE(BFloat16, float)
109
+ XPU_ACC_TYPE(Half, float)
110
+ XPU_ACC_TYPE(Float8_e5m2, float)
111
+ XPU_ACC_TYPE(Float8_e4m3fn, float)
112
+ XPU_ACC_TYPE(Float8_e5m2fnuz, float)
113
+ XPU_ACC_TYPE(Float8_e4m3fnuz, float)
114
+ XPU_ACC_TYPE(float, float)
115
+ XPU_ACC_TYPE(double, double)
116
+ XPU_ACC_TYPE(int8_t, int64_t)
117
+ XPU_ACC_TYPE(uint8_t, int64_t)
118
+ XPU_ACC_TYPE(char, int64_t)
119
+ XPU_ACC_TYPE(int16_t, int64_t)
120
+ XPU_ACC_TYPE(int32_t, int64_t)
121
+ XPU_ACC_TYPE(int64_t, int64_t)
122
+ XPU_ACC_TYPE(bool, bool)
123
+ XPU_ACC_TYPE(c10::complex<Half>, c10::complex<float>)
124
+ XPU_ACC_TYPE(c10::complex<float>, c10::complex<float>)
125
+ XPU_ACC_TYPE(c10::complex<double>, c10::complex<double>)
126
+
127
+ #if defined(__CUDACC__) || defined(__HIPCC__)
128
+ CUDA_ACC_TYPE(half, float)
129
+ #endif
130
+ CUDA_ACC_TYPE(BFloat16, float)
131
+ CUDA_ACC_TYPE(Half, float)
132
+ CUDA_ACC_TYPE(Float8_e5m2, float)
133
+ CUDA_ACC_TYPE(Float8_e4m3fn, float)
134
+ CUDA_ACC_TYPE(Float8_e5m2fnuz, float)
135
+ CUDA_ACC_TYPE(Float8_e4m3fnuz, float)
136
+ CUDA_ACC_TYPE(float, float)
137
+ CUDA_ACC_TYPE(double, double)
138
+ CUDA_ACC_TYPE(int8_t, int64_t)
139
+ CUDA_ACC_TYPE(uint8_t, int64_t)
140
+ CUDA_ACC_TYPE(char, int64_t)
141
+ CUDA_ACC_TYPE(int16_t, int64_t)
142
+ CUDA_ACC_TYPE(int32_t, int64_t)
143
+ CUDA_ACC_TYPE(int64_t, int64_t)
144
+ CUDA_ACC_TYPE(bool, bool)
145
+ CUDA_ACC_TYPE(c10::complex<Half>, c10::complex<float>)
146
+ CUDA_ACC_TYPE(c10::complex<float>, c10::complex<float>)
147
+ CUDA_ACC_TYPE(c10::complex<double>, c10::complex<double>)
148
+
149
+ CPU_ACC_TYPE(BFloat16, float)
150
+ CPU_ACC_TYPE(Half, float)
151
+ CPU_ACC_TYPE(Float8_e5m2, float)
152
+ CPU_ACC_TYPE(Float8_e4m3fn, float)
153
+ CPU_ACC_TYPE(Float8_e5m2fnuz, float)
154
+ CPU_ACC_TYPE(Float8_e4m3fnuz, float)
155
+ CPU_ACC_TYPE(float, double)
156
+ CPU_ACC_TYPE(double, double)
157
+ CPU_ACC_TYPE(int8_t, int64_t)
158
+ CPU_ACC_TYPE(uint8_t, int64_t)
159
+ CPU_ACC_TYPE(char, int64_t)
160
+ CPU_ACC_TYPE(int16_t, int64_t)
161
+ CPU_ACC_TYPE(int32_t, int64_t)
162
+ CPU_ACC_TYPE(int64_t, int64_t)
163
+ CPU_ACC_TYPE(bool, bool)
164
+ CPU_ACC_TYPE(c10::complex<Half>, c10::complex<float>)
165
+ CPU_ACC_TYPE(c10::complex<float>, c10::complex<double>)
166
+ CPU_ACC_TYPE(c10::complex<double>, c10::complex<double>)
167
+
168
+ TORCH_API c10::ScalarType toAccumulateType(
169
+ c10::ScalarType type,
170
+ c10::DeviceType device);
171
+ TORCH_API c10::ScalarType toAccumulateType(c10::ScalarType type, bool is_cuda);
172
+
173
+ } // namespace at
lib/python3.12/site-packages/torch/include/ATen/Backend.h ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ #pragma once
2
+ #include <c10/core/Backend.h>
lib/python3.12/site-packages/torch/include/ATen/Backtrace.h ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ #pragma once
2
+ #include <ATen/core/Backtrace.h>
lib/python3.12/site-packages/torch/include/ATen/BlasBackend.h ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <c10/util/Exception.h>
4
+
5
+ #include <ostream>
6
+ #include <string>
7
+
8
+ namespace at {
9
+
10
+ enum class BlasBackend : int8_t { Default, Cublas, Cublaslt, Ck };
11
+
12
+ inline std::string BlasBackendToString(at::BlasBackend backend) {
13
+ switch (backend) {
14
+ case BlasBackend::Default:
15
+ return "at::BlasBackend::Default";
16
+ case BlasBackend::Cublas:
17
+ return "at::BlasBackend::Cublas";
18
+ case BlasBackend::Cublaslt:
19
+ return "at::BlasBackend::Cublaslt";
20
+ case BlasBackend::Ck:
21
+ return "at::BlasBackend::Ck";
22
+ default:
23
+ TORCH_CHECK(false, "Unknown blas backend");
24
+ }
25
+ }
26
+
27
+ inline std::ostream& operator<<(std::ostream& stream, at::BlasBackend backend) {
28
+ return stream << BlasBackendToString(backend);
29
+ }
30
+
31
+ } // namespace at
lib/python3.12/site-packages/torch/include/ATen/CPUFixedAllocator.h ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <c10/core/Allocator.h>
4
+ #include <c10/util/Exception.h>
5
+
6
+ // This file creates a fake allocator that just throws exceptions if
7
+ // it is actually used.
8
+
9
+ // state passed to the allocator is the std::function<void(void*)> called
10
+ // when the blob is release by ATen
11
+
12
+ namespace at {
13
+
14
+ static void* cpu_fixed_malloc(void*, ptrdiff_t) {
15
+ TORCH_CHECK(false, "attempting to resize a tensor view of an external blob");
16
+ }
17
+
18
+ static void* cpu_fixed_realloc(void*, void*, ptrdiff_t) {
19
+ TORCH_CHECK(false, "attempting to resize a tensor view of an external blob");
20
+ }
21
+
22
+ static void cpu_fixed_free(void* state, void* allocation) {
23
+ auto on_release = static_cast<std::function<void(void*)>*>(state);
24
+ (*on_release)(allocation);
25
+ delete on_release;
26
+ }
27
+
28
+ static Allocator CPU_fixed_allocator = {
29
+ cpu_fixed_malloc,
30
+ cpu_fixed_realloc,
31
+ cpu_fixed_free};
32
+
33
+ } // namespace at
lib/python3.12/site-packages/torch/include/ATen/CPUFunctions.h ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <ATen/core/TensorBody.h>
2
+
3
+ // TODO Undo all logic introduced for Note [Avoiding Include Cycles In Static Dispatch]
4
+ // Code introduced to avoid cyclic dependency in static dispatch is no longer
5
+ // needed as static dispatch logic is moved from TensorBody.h, which caused cycles in the first place,
6
+ // to Operators.cpp for supporting multiple backends with multiple kernels.
7
+ //
8
+ // Note [Avoiding Include Cycles In Static Dispatch]
9
+ // In order to avoid #include cycles in the static dispatch build, we've carefully split out
10
+ // the static function definition files into {DispatchKey}Functions.h and {DispatchKey}Functions_inl.h.
11
+ //
12
+ // Without this split, the include cycle looks like TensorBody.h -> CPUFunctions.h -> TensorBody.h.
13
+ // - TensorBody.h #includes CPUFunctions.h in the static dispatch build, because the tensor methods
14
+ // all need to call into the fastpath C++ API defined in CPUFunctions.h. The methods are also all
15
+ // directly inlined into TensorBody.h.
16
+ // - CPUFunctions.h #includes TensorBody.h because it contains function declarations for the entire C++ API,
17
+ // which include functions that have defaultable std::optional<Tensor> arguments.
18
+ // That requires knowing the full Tensor class definition.
19
+ //
20
+ // We break the cycle by doing the following:
21
+ // - Split out CPUFunction.h into two files: CPUFunctions.h and CPUFunctions_inl.h
22
+ // - CPUFunction.h is a dummy file that just includes the Tensor class and includes CPUFunctions_inl.,
23
+ // - CPUFunctions_inl.h includes everything else
24
+ // - (only in the static dispatch build) TensorBody.h makes sure to finish defining the Tensor class,
25
+ // and then it includes CPUFunctions_inl.h.
26
+ // - All other files that want the cpu fastpath functions can include CPUFunctions.h directly.
27
+ // - This also means that static dispatch build, CPUFunctions.h only needs to
28
+ // #include TensorBody.h, and it will automatically bring in CPUFunctions_inl.h.
29
+ #include <ATen/CPUFunctions_inl.h>
lib/python3.12/site-packages/torch/include/ATen/CPUFunctions_inl.h ADDED
@@ -0,0 +1,544 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+ // @generated by torchgen/gen.py from DispatchKeyFunctions_inl.h
3
+
4
+ // NB: The implementing C++ file is RegisterDispatchKey.cpp
5
+
6
+ // The only #includes we need are for custom classes that have defaults in the C++ API
7
+ #include <c10/core/MemoryFormat.h>
8
+ #include <c10/core/Scalar.h>
9
+ #include <ATen/core/Reduction.h>
10
+
11
+ #if defined(AT_PER_OPERATOR_HEADERS) && defined(TORCH_ASSERT_ONLY_METHOD_OPERATORS)
12
+ #error This change adds a dependency on all pytorch operators, meaning the \
13
+ file will need to be re-compiled every time an operator is changed or added. \
14
+ Consider including a specific operator from \
15
+ <ATen/ops/{my_operator}_cpu_dispatch.h>. \
16
+ See NOTE [TORCH_ASSERT_ONLY_METHOD_OPERATORS].
17
+ #endif
18
+
19
+ #include <ATen/ops/_adaptive_avg_pool2d_cpu_dispatch.h>
20
+ #include <ATen/ops/_adaptive_avg_pool2d_backward_cpu_dispatch.h>
21
+ #include <ATen/ops/_adaptive_avg_pool3d_cpu_dispatch.h>
22
+ #include <ATen/ops/_adaptive_avg_pool3d_backward_cpu_dispatch.h>
23
+ #include <ATen/ops/_add_relu_cpu_dispatch.h>
24
+ #include <ATen/ops/_addmm_activation_cpu_dispatch.h>
25
+ #include <ATen/ops/_aminmax_cpu_dispatch.h>
26
+ #include <ATen/ops/_amp_foreach_non_finite_check_and_unscale_cpu_dispatch.h>
27
+ #include <ATen/ops/_amp_update_scale_cpu_dispatch.h>
28
+ #include <ATen/ops/_assert_async_cpu_dispatch.h>
29
+ #include <ATen/ops/_batch_norm_with_update_cpu_dispatch.h>
30
+ #include <ATen/ops/_cdist_backward_cpu_dispatch.h>
31
+ #include <ATen/ops/_cdist_forward_cpu_dispatch.h>
32
+ #include <ATen/ops/_cholesky_solve_helper_cpu_dispatch.h>
33
+ #include <ATen/ops/_compute_linear_combination_cpu_dispatch.h>
34
+ #include <ATen/ops/_convert_indices_from_coo_to_csr_cpu_dispatch.h>
35
+ #include <ATen/ops/_convert_indices_from_csr_to_coo_cpu_dispatch.h>
36
+ #include <ATen/ops/_convert_weight_to_int4pack_for_cpu_cpu_dispatch.h>
37
+ #include <ATen/ops/_ctc_loss_cpu_dispatch.h>
38
+ #include <ATen/ops/_ctc_loss_backward_cpu_dispatch.h>
39
+ #include <ATen/ops/_cummax_helper_cpu_dispatch.h>
40
+ #include <ATen/ops/_cummin_helper_cpu_dispatch.h>
41
+ #include <ATen/ops/_dirichlet_grad_cpu_dispatch.h>
42
+ #include <ATen/ops/_dyn_quant_matmul_4bit_cpu_dispatch.h>
43
+ #include <ATen/ops/_dyn_quant_pack_4bit_weight_cpu_dispatch.h>
44
+ #include <ATen/ops/_efficientzerotensor_cpu_dispatch.h>
45
+ #include <ATen/ops/_embedding_bag_cpu_dispatch.h>
46
+ #include <ATen/ops/_embedding_bag_backward_cpu_dispatch.h>
47
+ #include <ATen/ops/_embedding_bag_dense_backward_cpu_dispatch.h>
48
+ #include <ATen/ops/_embedding_bag_forward_only_cpu_dispatch.h>
49
+ #include <ATen/ops/_embedding_bag_per_sample_weights_backward_cpu_dispatch.h>
50
+ #include <ATen/ops/_empty_affine_quantized_cpu_dispatch.h>
51
+ #include <ATen/ops/_empty_per_channel_affine_quantized_cpu_dispatch.h>
52
+ #include <ATen/ops/_fake_quantize_learnable_per_channel_affine_cpu_dispatch.h>
53
+ #include <ATen/ops/_fake_quantize_learnable_per_channel_affine_backward_cpu_dispatch.h>
54
+ #include <ATen/ops/_fake_quantize_learnable_per_tensor_affine_cpu_dispatch.h>
55
+ #include <ATen/ops/_fake_quantize_learnable_per_tensor_affine_backward_cpu_dispatch.h>
56
+ #include <ATen/ops/_fake_quantize_per_tensor_affine_cachemask_tensor_qparams_cpu_dispatch.h>
57
+ #include <ATen/ops/_fft_c2c_cpu_dispatch.h>
58
+ #include <ATen/ops/_fft_c2r_cpu_dispatch.h>
59
+ #include <ATen/ops/_fft_r2c_cpu_dispatch.h>
60
+ #include <ATen/ops/_foobar_cpu_dispatch.h>
61
+ #include <ATen/ops/_functional_assert_async_cpu_dispatch.h>
62
+ #include <ATen/ops/_fused_adagrad_cpu_dispatch.h>
63
+ #include <ATen/ops/_fused_adam_cpu_dispatch.h>
64
+ #include <ATen/ops/_fused_adamw_cpu_dispatch.h>
65
+ #include <ATen/ops/_fused_moving_avg_obs_fq_helper_cpu_dispatch.h>
66
+ #include <ATen/ops/_fused_sdp_choice_cpu_dispatch.h>
67
+ #include <ATen/ops/_fused_sgd_cpu_dispatch.h>
68
+ #include <ATen/ops/_histogramdd_bin_edges_cpu_dispatch.h>
69
+ #include <ATen/ops/_histogramdd_from_bin_cts_cpu_dispatch.h>
70
+ #include <ATen/ops/_histogramdd_from_bin_tensors_cpu_dispatch.h>
71
+ #include <ATen/ops/_index_put_impl_cpu_dispatch.h>
72
+ #include <ATen/ops/_int_mm_cpu_dispatch.h>
73
+ #include <ATen/ops/_jagged_to_padded_dense_forward_cpu_dispatch.h>
74
+ #include <ATen/ops/_linalg_det_cpu_dispatch.h>
75
+ #include <ATen/ops/_linalg_eigh_cpu_dispatch.h>
76
+ #include <ATen/ops/_linalg_eigvals_cpu_dispatch.h>
77
+ #include <ATen/ops/_linalg_slogdet_cpu_dispatch.h>
78
+ #include <ATen/ops/_linalg_solve_ex_cpu_dispatch.h>
79
+ #include <ATen/ops/_linalg_svd_cpu_dispatch.h>
80
+ #include <ATen/ops/_local_scalar_dense_cpu_dispatch.h>
81
+ #include <ATen/ops/_log_softmax_cpu_dispatch.h>
82
+ #include <ATen/ops/_log_softmax_backward_data_cpu_dispatch.h>
83
+ #include <ATen/ops/_logcumsumexp_cpu_dispatch.h>
84
+ #include <ATen/ops/_make_dep_token_cpu_dispatch.h>
85
+ #include <ATen/ops/_make_per_channel_quantized_tensor_cpu_dispatch.h>
86
+ #include <ATen/ops/_make_per_tensor_quantized_tensor_cpu_dispatch.h>
87
+ #include <ATen/ops/_masked_softmax_cpu_dispatch.h>
88
+ #include <ATen/ops/_masked_softmax_backward_cpu_dispatch.h>
89
+ #include <ATen/ops/_native_batch_norm_legit_cpu_dispatch.h>
90
+ #include <ATen/ops/_native_multi_head_attention_cpu_dispatch.h>
91
+ #include <ATen/ops/_nested_compute_contiguous_strides_offsets_cpu_dispatch.h>
92
+ #include <ATen/ops/_nested_from_padded_cpu_dispatch.h>
93
+ #include <ATen/ops/_nested_tensor_from_mask_cpu_dispatch.h>
94
+ #include <ATen/ops/_nested_tensor_from_mask_left_aligned_cpu_dispatch.h>
95
+ #include <ATen/ops/_nested_view_from_buffer_cpu_dispatch.h>
96
+ #include <ATen/ops/_padded_dense_to_jagged_forward_cpu_dispatch.h>
97
+ #include <ATen/ops/_pdist_backward_cpu_dispatch.h>
98
+ #include <ATen/ops/_pdist_forward_cpu_dispatch.h>
99
+ #include <ATen/ops/_prelu_kernel_cpu_dispatch.h>
100
+ #include <ATen/ops/_prelu_kernel_backward_cpu_dispatch.h>
101
+ #include <ATen/ops/_reshape_alias_cpu_dispatch.h>
102
+ #include <ATen/ops/_sample_dirichlet_cpu_dispatch.h>
103
+ #include <ATen/ops/_scaled_dot_product_flash_attention_for_cpu_cpu_dispatch.h>
104
+ #include <ATen/ops/_scaled_dot_product_flash_attention_for_cpu_backward_cpu_dispatch.h>
105
+ #include <ATen/ops/_scaled_mm_cpu_dispatch.h>
106
+ #include <ATen/ops/_segment_reduce_backward_cpu_dispatch.h>
107
+ #include <ATen/ops/_slow_conv2d_backward_cpu_dispatch.h>
108
+ #include <ATen/ops/_slow_conv2d_forward_cpu_dispatch.h>
109
+ #include <ATen/ops/_softmax_cpu_dispatch.h>
110
+ #include <ATen/ops/_softmax_backward_data_cpu_dispatch.h>
111
+ #include <ATen/ops/_spdiags_cpu_dispatch.h>
112
+ #include <ATen/ops/_stack_cpu_dispatch.h>
113
+ #include <ATen/ops/_standard_gamma_cpu_dispatch.h>
114
+ #include <ATen/ops/_standard_gamma_grad_cpu_dispatch.h>
115
+ #include <ATen/ops/_test_functorch_fallback_cpu_dispatch.h>
116
+ #include <ATen/ops/_test_optional_filled_intlist_cpu_dispatch.h>
117
+ #include <ATen/ops/_test_optional_floatlist_cpu_dispatch.h>
118
+ #include <ATen/ops/_test_optional_intlist_cpu_dispatch.h>
119
+ #include <ATen/ops/_to_sparse_cpu_dispatch.h>
120
+ #include <ATen/ops/_to_sparse_bsc_cpu_dispatch.h>
121
+ #include <ATen/ops/_to_sparse_bsr_cpu_dispatch.h>
122
+ #include <ATen/ops/_to_sparse_csc_cpu_dispatch.h>
123
+ #include <ATen/ops/_to_sparse_csr_cpu_dispatch.h>
124
+ #include <ATen/ops/_transform_bias_rescale_qkv_cpu_dispatch.h>
125
+ #include <ATen/ops/_transformer_encoder_layer_fwd_cpu_dispatch.h>
126
+ #include <ATen/ops/_unique_cpu_dispatch.h>
127
+ #include <ATen/ops/_unique2_cpu_dispatch.h>
128
+ #include <ATen/ops/_upsample_bicubic2d_aa_cpu_dispatch.h>
129
+ #include <ATen/ops/_upsample_bicubic2d_aa_backward_cpu_dispatch.h>
130
+ #include <ATen/ops/_upsample_bilinear2d_aa_cpu_dispatch.h>
131
+ #include <ATen/ops/_upsample_bilinear2d_aa_backward_cpu_dispatch.h>
132
+ #include <ATen/ops/_upsample_nearest_exact1d_cpu_dispatch.h>
133
+ #include <ATen/ops/_upsample_nearest_exact1d_backward_cpu_dispatch.h>
134
+ #include <ATen/ops/_upsample_nearest_exact2d_cpu_dispatch.h>
135
+ #include <ATen/ops/_upsample_nearest_exact2d_backward_cpu_dispatch.h>
136
+ #include <ATen/ops/_upsample_nearest_exact3d_cpu_dispatch.h>
137
+ #include <ATen/ops/_upsample_nearest_exact3d_backward_cpu_dispatch.h>
138
+ #include <ATen/ops/_validate_compressed_sparse_indices_cpu_dispatch.h>
139
+ #include <ATen/ops/_weight_int4pack_mm_for_cpu_cpu_dispatch.h>
140
+ #include <ATen/ops/_weight_int8pack_mm_cpu_dispatch.h>
141
+ #include <ATen/ops/_weight_norm_interface_cpu_dispatch.h>
142
+ #include <ATen/ops/_weight_norm_interface_backward_cpu_dispatch.h>
143
+ #include <ATen/ops/abs_cpu_dispatch.h>
144
+ #include <ATen/ops/acos_cpu_dispatch.h>
145
+ #include <ATen/ops/acosh_cpu_dispatch.h>
146
+ #include <ATen/ops/adaptive_avg_pool2d_cpu_dispatch.h>
147
+ #include <ATen/ops/adaptive_avg_pool3d_cpu_dispatch.h>
148
+ #include <ATen/ops/adaptive_avg_pool3d_backward_cpu_dispatch.h>
149
+ #include <ATen/ops/adaptive_max_pool2d_cpu_dispatch.h>
150
+ #include <ATen/ops/adaptive_max_pool2d_backward_cpu_dispatch.h>
151
+ #include <ATen/ops/adaptive_max_pool3d_cpu_dispatch.h>
152
+ #include <ATen/ops/adaptive_max_pool3d_backward_cpu_dispatch.h>
153
+ #include <ATen/ops/add_cpu_dispatch.h>
154
+ #include <ATen/ops/addbmm_cpu_dispatch.h>
155
+ #include <ATen/ops/addcdiv_cpu_dispatch.h>
156
+ #include <ATen/ops/addcmul_cpu_dispatch.h>
157
+ #include <ATen/ops/addmm_cpu_dispatch.h>
158
+ #include <ATen/ops/addmv_cpu_dispatch.h>
159
+ #include <ATen/ops/addr_cpu_dispatch.h>
160
+ #include <ATen/ops/all_cpu_dispatch.h>
161
+ #include <ATen/ops/amax_cpu_dispatch.h>
162
+ #include <ATen/ops/amin_cpu_dispatch.h>
163
+ #include <ATen/ops/aminmax_cpu_dispatch.h>
164
+ #include <ATen/ops/angle_cpu_dispatch.h>
165
+ #include <ATen/ops/any_cpu_dispatch.h>
166
+ #include <ATen/ops/arange_cpu_dispatch.h>
167
+ #include <ATen/ops/argmax_cpu_dispatch.h>
168
+ #include <ATen/ops/argmin_cpu_dispatch.h>
169
+ #include <ATen/ops/as_strided_cpu_dispatch.h>
170
+ #include <ATen/ops/asin_cpu_dispatch.h>
171
+ #include <ATen/ops/asinh_cpu_dispatch.h>
172
+ #include <ATen/ops/atan_cpu_dispatch.h>
173
+ #include <ATen/ops/atan2_cpu_dispatch.h>
174
+ #include <ATen/ops/atanh_cpu_dispatch.h>
175
+ #include <ATen/ops/avg_pool2d_cpu_dispatch.h>
176
+ #include <ATen/ops/avg_pool2d_backward_cpu_dispatch.h>
177
+ #include <ATen/ops/avg_pool3d_cpu_dispatch.h>
178
+ #include <ATen/ops/avg_pool3d_backward_cpu_dispatch.h>
179
+ #include <ATen/ops/baddbmm_cpu_dispatch.h>
180
+ #include <ATen/ops/batch_norm_backward_cpu_dispatch.h>
181
+ #include <ATen/ops/batch_norm_update_stats_cpu_dispatch.h>
182
+ #include <ATen/ops/bernoulli_cpu_dispatch.h>
183
+ #include <ATen/ops/binary_cross_entropy_cpu_dispatch.h>
184
+ #include <ATen/ops/binary_cross_entropy_backward_cpu_dispatch.h>
185
+ #include <ATen/ops/bincount_cpu_dispatch.h>
186
+ #include <ATen/ops/binomial_cpu_dispatch.h>
187
+ #include <ATen/ops/bitwise_and_cpu_dispatch.h>
188
+ #include <ATen/ops/bitwise_left_shift_cpu_dispatch.h>
189
+ #include <ATen/ops/bitwise_not_cpu_dispatch.h>
190
+ #include <ATen/ops/bitwise_or_cpu_dispatch.h>
191
+ #include <ATen/ops/bitwise_right_shift_cpu_dispatch.h>
192
+ #include <ATen/ops/bitwise_xor_cpu_dispatch.h>
193
+ #include <ATen/ops/bmm_cpu_dispatch.h>
194
+ #include <ATen/ops/bucketize_cpu_dispatch.h>
195
+ #include <ATen/ops/cat_cpu_dispatch.h>
196
+ #include <ATen/ops/cauchy_cpu_dispatch.h>
197
+ #include <ATen/ops/ceil_cpu_dispatch.h>
198
+ #include <ATen/ops/channel_shuffle_cpu_dispatch.h>
199
+ #include <ATen/ops/cholesky_cpu_dispatch.h>
200
+ #include <ATen/ops/cholesky_inverse_cpu_dispatch.h>
201
+ #include <ATen/ops/clamp_cpu_dispatch.h>
202
+ #include <ATen/ops/clamp_max_cpu_dispatch.h>
203
+ #include <ATen/ops/clamp_min_cpu_dispatch.h>
204
+ #include <ATen/ops/col2im_cpu_dispatch.h>
205
+ #include <ATen/ops/complex_cpu_dispatch.h>
206
+ #include <ATen/ops/conj_physical_cpu_dispatch.h>
207
+ #include <ATen/ops/copysign_cpu_dispatch.h>
208
+ #include <ATen/ops/cos_cpu_dispatch.h>
209
+ #include <ATen/ops/cosh_cpu_dispatch.h>
210
+ #include <ATen/ops/count_nonzero_cpu_dispatch.h>
211
+ #include <ATen/ops/cumprod_cpu_dispatch.h>
212
+ #include <ATen/ops/cumsum_cpu_dispatch.h>
213
+ #include <ATen/ops/dequantize_cpu_dispatch.h>
214
+ #include <ATen/ops/digamma_cpu_dispatch.h>
215
+ #include <ATen/ops/div_cpu_dispatch.h>
216
+ #include <ATen/ops/dot_cpu_dispatch.h>
217
+ #include <ATen/ops/elu_cpu_dispatch.h>
218
+ #include <ATen/ops/elu_backward_cpu_dispatch.h>
219
+ #include <ATen/ops/embedding_dense_backward_cpu_dispatch.h>
220
+ #include <ATen/ops/embedding_renorm_cpu_dispatch.h>
221
+ #include <ATen/ops/empty_cpu_dispatch.h>
222
+ #include <ATen/ops/empty_strided_cpu_dispatch.h>
223
+ #include <ATen/ops/eq_cpu_dispatch.h>
224
+ #include <ATen/ops/equal_cpu_dispatch.h>
225
+ #include <ATen/ops/erf_cpu_dispatch.h>
226
+ #include <ATen/ops/erfc_cpu_dispatch.h>
227
+ #include <ATen/ops/erfinv_cpu_dispatch.h>
228
+ #include <ATen/ops/exp_cpu_dispatch.h>
229
+ #include <ATen/ops/exp2_cpu_dispatch.h>
230
+ #include <ATen/ops/expm1_cpu_dispatch.h>
231
+ #include <ATen/ops/exponential_cpu_dispatch.h>
232
+ #include <ATen/ops/eye_cpu_dispatch.h>
233
+ #include <ATen/ops/fake_quantize_per_channel_affine_cachemask_cpu_dispatch.h>
234
+ #include <ATen/ops/fake_quantize_per_tensor_affine_cachemask_cpu_dispatch.h>
235
+ #include <ATen/ops/fill_cpu_dispatch.h>
236
+ #include <ATen/ops/flip_cpu_dispatch.h>
237
+ #include <ATen/ops/floor_cpu_dispatch.h>
238
+ #include <ATen/ops/floor_divide_cpu_dispatch.h>
239
+ #include <ATen/ops/fmax_cpu_dispatch.h>
240
+ #include <ATen/ops/fmin_cpu_dispatch.h>
241
+ #include <ATen/ops/fmod_cpu_dispatch.h>
242
+ #include <ATen/ops/frac_cpu_dispatch.h>
243
+ #include <ATen/ops/fractional_max_pool2d_cpu_dispatch.h>
244
+ #include <ATen/ops/fractional_max_pool2d_backward_cpu_dispatch.h>
245
+ #include <ATen/ops/fractional_max_pool3d_cpu_dispatch.h>
246
+ #include <ATen/ops/fractional_max_pool3d_backward_cpu_dispatch.h>
247
+ #include <ATen/ops/frexp_cpu_dispatch.h>
248
+ #include <ATen/ops/from_file_cpu_dispatch.h>
249
+ #include <ATen/ops/gather_cpu_dispatch.h>
250
+ #include <ATen/ops/gcd_cpu_dispatch.h>
251
+ #include <ATen/ops/ge_cpu_dispatch.h>
252
+ #include <ATen/ops/gelu_cpu_dispatch.h>
253
+ #include <ATen/ops/gelu_backward_cpu_dispatch.h>
254
+ #include <ATen/ops/geometric_cpu_dispatch.h>
255
+ #include <ATen/ops/geqrf_cpu_dispatch.h>
256
+ #include <ATen/ops/glu_cpu_dispatch.h>
257
+ #include <ATen/ops/glu_backward_cpu_dispatch.h>
258
+ #include <ATen/ops/glu_backward_jvp_cpu_dispatch.h>
259
+ #include <ATen/ops/glu_jvp_cpu_dispatch.h>
260
+ #include <ATen/ops/grid_sampler_2d_cpu_dispatch.h>
261
+ #include <ATen/ops/grid_sampler_2d_backward_cpu_dispatch.h>
262
+ #include <ATen/ops/grid_sampler_3d_cpu_dispatch.h>
263
+ #include <ATen/ops/grid_sampler_3d_backward_cpu_dispatch.h>
264
+ #include <ATen/ops/gt_cpu_dispatch.h>
265
+ #include <ATen/ops/hardshrink_cpu_dispatch.h>
266
+ #include <ATen/ops/hardshrink_backward_cpu_dispatch.h>
267
+ #include <ATen/ops/hardsigmoid_cpu_dispatch.h>
268
+ #include <ATen/ops/hardsigmoid_backward_cpu_dispatch.h>
269
+ #include <ATen/ops/hardswish_cpu_dispatch.h>
270
+ #include <ATen/ops/hardswish_backward_cpu_dispatch.h>
271
+ #include <ATen/ops/hardtanh_cpu_dispatch.h>
272
+ #include <ATen/ops/hardtanh_backward_cpu_dispatch.h>
273
+ #include <ATen/ops/hash_tensor_cpu_dispatch.h>
274
+ #include <ATen/ops/heaviside_cpu_dispatch.h>
275
+ #include <ATen/ops/histc_cpu_dispatch.h>
276
+ #include <ATen/ops/histogram_cpu_dispatch.h>
277
+ #include <ATen/ops/huber_loss_cpu_dispatch.h>
278
+ #include <ATen/ops/huber_loss_backward_cpu_dispatch.h>
279
+ #include <ATen/ops/hypot_cpu_dispatch.h>
280
+ #include <ATen/ops/i0_cpu_dispatch.h>
281
+ #include <ATen/ops/igamma_cpu_dispatch.h>
282
+ #include <ATen/ops/igammac_cpu_dispatch.h>
283
+ #include <ATen/ops/im2col_cpu_dispatch.h>
284
+ #include <ATen/ops/index_cpu_dispatch.h>
285
+ #include <ATen/ops/index_add_cpu_dispatch.h>
286
+ #include <ATen/ops/index_copy_cpu_dispatch.h>
287
+ #include <ATen/ops/index_fill_cpu_dispatch.h>
288
+ #include <ATen/ops/index_reduce_cpu_dispatch.h>
289
+ #include <ATen/ops/index_select_cpu_dispatch.h>
290
+ #include <ATen/ops/is_set_to_cpu_dispatch.h>
291
+ #include <ATen/ops/isin_cpu_dispatch.h>
292
+ #include <ATen/ops/isnan_cpu_dispatch.h>
293
+ #include <ATen/ops/isneginf_cpu_dispatch.h>
294
+ #include <ATen/ops/isposinf_cpu_dispatch.h>
295
+ #include <ATen/ops/kthvalue_cpu_dispatch.h>
296
+ #include <ATen/ops/lcm_cpu_dispatch.h>
297
+ #include <ATen/ops/le_cpu_dispatch.h>
298
+ #include <ATen/ops/leaky_relu_cpu_dispatch.h>
299
+ #include <ATen/ops/leaky_relu_backward_cpu_dispatch.h>
300
+ #include <ATen/ops/lerp_cpu_dispatch.h>
301
+ #include <ATen/ops/lgamma_cpu_dispatch.h>
302
+ #include <ATen/ops/linalg_cholesky_ex_cpu_dispatch.h>
303
+ #include <ATen/ops/linalg_cross_cpu_dispatch.h>
304
+ #include <ATen/ops/linalg_eig_cpu_dispatch.h>
305
+ #include <ATen/ops/linalg_eigvals_cpu_dispatch.h>
306
+ #include <ATen/ops/linalg_householder_product_cpu_dispatch.h>
307
+ #include <ATen/ops/linalg_inv_ex_cpu_dispatch.h>
308
+ #include <ATen/ops/linalg_ldl_factor_ex_cpu_dispatch.h>
309
+ #include <ATen/ops/linalg_ldl_solve_cpu_dispatch.h>
310
+ #include <ATen/ops/linalg_lstsq_cpu_dispatch.h>
311
+ #include <ATen/ops/linalg_lu_cpu_dispatch.h>
312
+ #include <ATen/ops/linalg_lu_factor_ex_cpu_dispatch.h>
313
+ #include <ATen/ops/linalg_lu_solve_cpu_dispatch.h>
314
+ #include <ATen/ops/linalg_matrix_exp_cpu_dispatch.h>
315
+ #include <ATen/ops/linalg_qr_cpu_dispatch.h>
316
+ #include <ATen/ops/linalg_solve_triangular_cpu_dispatch.h>
317
+ #include <ATen/ops/linalg_vector_norm_cpu_dispatch.h>
318
+ #include <ATen/ops/linspace_cpu_dispatch.h>
319
+ #include <ATen/ops/log_cpu_dispatch.h>
320
+ #include <ATen/ops/log10_cpu_dispatch.h>
321
+ #include <ATen/ops/log1p_cpu_dispatch.h>
322
+ #include <ATen/ops/log2_cpu_dispatch.h>
323
+ #include <ATen/ops/log_normal_cpu_dispatch.h>
324
+ #include <ATen/ops/log_sigmoid_backward_cpu_dispatch.h>
325
+ #include <ATen/ops/log_sigmoid_forward_cpu_dispatch.h>
326
+ #include <ATen/ops/logaddexp_cpu_dispatch.h>
327
+ #include <ATen/ops/logaddexp2_cpu_dispatch.h>
328
+ #include <ATen/ops/logical_and_cpu_dispatch.h>
329
+ #include <ATen/ops/logical_not_cpu_dispatch.h>
330
+ #include <ATen/ops/logical_or_cpu_dispatch.h>
331
+ #include <ATen/ops/logical_xor_cpu_dispatch.h>
332
+ #include <ATen/ops/logit_cpu_dispatch.h>
333
+ #include <ATen/ops/logit_backward_cpu_dispatch.h>
334
+ #include <ATen/ops/logspace_cpu_dispatch.h>
335
+ #include <ATen/ops/lshift_cpu_dispatch.h>
336
+ #include <ATen/ops/lt_cpu_dispatch.h>
337
+ #include <ATen/ops/lu_unpack_cpu_dispatch.h>
338
+ #include <ATen/ops/masked_fill_cpu_dispatch.h>
339
+ #include <ATen/ops/masked_scatter_cpu_dispatch.h>
340
+ #include <ATen/ops/masked_select_cpu_dispatch.h>
341
+ #include <ATen/ops/max_cpu_dispatch.h>
342
+ #include <ATen/ops/max_pool2d_with_indices_cpu_dispatch.h>
343
+ #include <ATen/ops/max_pool2d_with_indices_backward_cpu_dispatch.h>
344
+ #include <ATen/ops/max_pool3d_with_indices_cpu_dispatch.h>
345
+ #include <ATen/ops/max_pool3d_with_indices_backward_cpu_dispatch.h>
346
+ #include <ATen/ops/max_unpool2d_cpu_dispatch.h>
347
+ #include <ATen/ops/max_unpool3d_cpu_dispatch.h>
348
+ #include <ATen/ops/maximum_cpu_dispatch.h>
349
+ #include <ATen/ops/mean_cpu_dispatch.h>
350
+ #include <ATen/ops/median_cpu_dispatch.h>
351
+ #include <ATen/ops/min_cpu_dispatch.h>
352
+ #include <ATen/ops/minimum_cpu_dispatch.h>
353
+ #include <ATen/ops/mish_cpu_dispatch.h>
354
+ #include <ATen/ops/mish_backward_cpu_dispatch.h>
355
+ #include <ATen/ops/mkldnn_rnn_layer_cpu_dispatch.h>
356
+ #include <ATen/ops/mkldnn_rnn_layer_backward_cpu_dispatch.h>
357
+ #include <ATen/ops/mm_cpu_dispatch.h>
358
+ #include <ATen/ops/mode_cpu_dispatch.h>
359
+ #include <ATen/ops/mse_loss_cpu_dispatch.h>
360
+ #include <ATen/ops/mse_loss_backward_cpu_dispatch.h>
361
+ #include <ATen/ops/mul_cpu_dispatch.h>
362
+ #include <ATen/ops/multi_margin_loss_cpu_dispatch.h>
363
+ #include <ATen/ops/multi_margin_loss_backward_cpu_dispatch.h>
364
+ #include <ATen/ops/multilabel_margin_loss_backward_cpu_dispatch.h>
365
+ #include <ATen/ops/multilabel_margin_loss_forward_cpu_dispatch.h>
366
+ #include <ATen/ops/multinomial_cpu_dispatch.h>
367
+ #include <ATen/ops/mvlgamma_cpu_dispatch.h>
368
+ #include <ATen/ops/nan_to_num_cpu_dispatch.h>
369
+ #include <ATen/ops/nanmedian_cpu_dispatch.h>
370
+ #include <ATen/ops/nansum_cpu_dispatch.h>
371
+ #include <ATen/ops/narrow_copy_cpu_dispatch.h>
372
+ #include <ATen/ops/native_batch_norm_cpu_dispatch.h>
373
+ #include <ATen/ops/native_batch_norm_backward_cpu_dispatch.h>
374
+ #include <ATen/ops/native_channel_shuffle_cpu_dispatch.h>
375
+ #include <ATen/ops/native_dropout_cpu_dispatch.h>
376
+ #include <ATen/ops/native_dropout_backward_cpu_dispatch.h>
377
+ #include <ATen/ops/native_group_norm_cpu_dispatch.h>
378
+ #include <ATen/ops/native_group_norm_backward_cpu_dispatch.h>
379
+ #include <ATen/ops/native_layer_norm_cpu_dispatch.h>
380
+ #include <ATen/ops/native_layer_norm_backward_cpu_dispatch.h>
381
+ #include <ATen/ops/ne_cpu_dispatch.h>
382
+ #include <ATen/ops/neg_cpu_dispatch.h>
383
+ #include <ATen/ops/nextafter_cpu_dispatch.h>
384
+ #include <ATen/ops/nll_loss2d_backward_cpu_dispatch.h>
385
+ #include <ATen/ops/nll_loss2d_forward_cpu_dispatch.h>
386
+ #include <ATen/ops/nll_loss_backward_cpu_dispatch.h>
387
+ #include <ATen/ops/nll_loss_forward_cpu_dispatch.h>
388
+ #include <ATen/ops/nonzero_cpu_dispatch.h>
389
+ #include <ATen/ops/nonzero_static_cpu_dispatch.h>
390
+ #include <ATen/ops/norm_cpu_dispatch.h>
391
+ #include <ATen/ops/normal_cpu_dispatch.h>
392
+ #include <ATen/ops/ormqr_cpu_dispatch.h>
393
+ #include <ATen/ops/pixel_shuffle_cpu_dispatch.h>
394
+ #include <ATen/ops/pixel_unshuffle_cpu_dispatch.h>
395
+ #include <ATen/ops/poisson_cpu_dispatch.h>
396
+ #include <ATen/ops/polar_cpu_dispatch.h>
397
+ #include <ATen/ops/polygamma_cpu_dispatch.h>
398
+ #include <ATen/ops/pow_cpu_dispatch.h>
399
+ #include <ATen/ops/prod_cpu_dispatch.h>
400
+ #include <ATen/ops/put_cpu_dispatch.h>
401
+ #include <ATen/ops/quantize_per_channel_cpu_dispatch.h>
402
+ #include <ATen/ops/quantize_per_tensor_cpu_dispatch.h>
403
+ #include <ATen/ops/quantize_per_tensor_dynamic_cpu_dispatch.h>
404
+ #include <ATen/ops/random_cpu_dispatch.h>
405
+ #include <ATen/ops/randperm_cpu_dispatch.h>
406
+ #include <ATen/ops/range_cpu_dispatch.h>
407
+ #include <ATen/ops/reciprocal_cpu_dispatch.h>
408
+ #include <ATen/ops/reflection_pad1d_cpu_dispatch.h>
409
+ #include <ATen/ops/reflection_pad1d_backward_cpu_dispatch.h>
410
+ #include <ATen/ops/reflection_pad2d_cpu_dispatch.h>
411
+ #include <ATen/ops/reflection_pad2d_backward_cpu_dispatch.h>
412
+ #include <ATen/ops/reflection_pad3d_cpu_dispatch.h>
413
+ #include <ATen/ops/reflection_pad3d_backward_cpu_dispatch.h>
414
+ #include <ATen/ops/relu_cpu_dispatch.h>
415
+ #include <ATen/ops/remainder_cpu_dispatch.h>
416
+ #include <ATen/ops/renorm_cpu_dispatch.h>
417
+ #include <ATen/ops/repeat_interleave_cpu_dispatch.h>
418
+ #include <ATen/ops/replication_pad1d_cpu_dispatch.h>
419
+ #include <ATen/ops/replication_pad1d_backward_cpu_dispatch.h>
420
+ #include <ATen/ops/replication_pad2d_cpu_dispatch.h>
421
+ #include <ATen/ops/replication_pad2d_backward_cpu_dispatch.h>
422
+ #include <ATen/ops/replication_pad3d_cpu_dispatch.h>
423
+ #include <ATen/ops/replication_pad3d_backward_cpu_dispatch.h>
424
+ #include <ATen/ops/resize_cpu_dispatch.h>
425
+ #include <ATen/ops/roll_cpu_dispatch.h>
426
+ #include <ATen/ops/round_cpu_dispatch.h>
427
+ #include <ATen/ops/rrelu_with_noise_cpu_dispatch.h>
428
+ #include <ATen/ops/rshift_cpu_dispatch.h>
429
+ #include <ATen/ops/rsqrt_cpu_dispatch.h>
430
+ #include <ATen/ops/rsub_cpu_dispatch.h>
431
+ #include <ATen/ops/scatter_cpu_dispatch.h>
432
+ #include <ATen/ops/scatter_add_cpu_dispatch.h>
433
+ #include <ATen/ops/scatter_reduce_cpu_dispatch.h>
434
+ #include <ATen/ops/searchsorted_cpu_dispatch.h>
435
+ #include <ATen/ops/segment_reduce_cpu_dispatch.h>
436
+ #include <ATen/ops/set_cpu_dispatch.h>
437
+ #include <ATen/ops/sgn_cpu_dispatch.h>
438
+ #include <ATen/ops/sigmoid_cpu_dispatch.h>
439
+ #include <ATen/ops/sigmoid_backward_cpu_dispatch.h>
440
+ #include <ATen/ops/sign_cpu_dispatch.h>
441
+ #include <ATen/ops/signbit_cpu_dispatch.h>
442
+ #include <ATen/ops/silu_cpu_dispatch.h>
443
+ #include <ATen/ops/silu_backward_cpu_dispatch.h>
444
+ #include <ATen/ops/sin_cpu_dispatch.h>
445
+ #include <ATen/ops/sinc_cpu_dispatch.h>
446
+ #include <ATen/ops/sinh_cpu_dispatch.h>
447
+ #include <ATen/ops/slow_conv3d_forward_cpu_dispatch.h>
448
+ #include <ATen/ops/slow_conv_dilated2d_cpu_dispatch.h>
449
+ #include <ATen/ops/slow_conv_dilated3d_cpu_dispatch.h>
450
+ #include <ATen/ops/slow_conv_transpose2d_cpu_dispatch.h>
451
+ #include <ATen/ops/slow_conv_transpose3d_cpu_dispatch.h>
452
+ #include <ATen/ops/smooth_l1_loss_cpu_dispatch.h>
453
+ #include <ATen/ops/smooth_l1_loss_backward_cpu_dispatch.h>
454
+ #include <ATen/ops/softplus_cpu_dispatch.h>
455
+ #include <ATen/ops/softplus_backward_cpu_dispatch.h>
456
+ #include <ATen/ops/softshrink_cpu_dispatch.h>
457
+ #include <ATen/ops/softshrink_backward_cpu_dispatch.h>
458
+ #include <ATen/ops/sort_cpu_dispatch.h>
459
+ #include <ATen/ops/special_airy_ai_cpu_dispatch.h>
460
+ #include <ATen/ops/special_bessel_j0_cpu_dispatch.h>
461
+ #include <ATen/ops/special_bessel_j1_cpu_dispatch.h>
462
+ #include <ATen/ops/special_bessel_y0_cpu_dispatch.h>
463
+ #include <ATen/ops/special_bessel_y1_cpu_dispatch.h>
464
+ #include <ATen/ops/special_chebyshev_polynomial_t_cpu_dispatch.h>
465
+ #include <ATen/ops/special_chebyshev_polynomial_u_cpu_dispatch.h>
466
+ #include <ATen/ops/special_chebyshev_polynomial_v_cpu_dispatch.h>
467
+ #include <ATen/ops/special_chebyshev_polynomial_w_cpu_dispatch.h>
468
+ #include <ATen/ops/special_entr_cpu_dispatch.h>
469
+ #include <ATen/ops/special_erfcx_cpu_dispatch.h>
470
+ #include <ATen/ops/special_hermite_polynomial_h_cpu_dispatch.h>
471
+ #include <ATen/ops/special_hermite_polynomial_he_cpu_dispatch.h>
472
+ #include <ATen/ops/special_i0e_cpu_dispatch.h>
473
+ #include <ATen/ops/special_i1_cpu_dispatch.h>
474
+ #include <ATen/ops/special_i1e_cpu_dispatch.h>
475
+ #include <ATen/ops/special_laguerre_polynomial_l_cpu_dispatch.h>
476
+ #include <ATen/ops/special_legendre_polynomial_p_cpu_dispatch.h>
477
+ #include <ATen/ops/special_log_ndtr_cpu_dispatch.h>
478
+ #include <ATen/ops/special_modified_bessel_i0_cpu_dispatch.h>
479
+ #include <ATen/ops/special_modified_bessel_i1_cpu_dispatch.h>
480
+ #include <ATen/ops/special_modified_bessel_k0_cpu_dispatch.h>
481
+ #include <ATen/ops/special_modified_bessel_k1_cpu_dispatch.h>
482
+ #include <ATen/ops/special_ndtri_cpu_dispatch.h>
483
+ #include <ATen/ops/special_scaled_modified_bessel_k0_cpu_dispatch.h>
484
+ #include <ATen/ops/special_scaled_modified_bessel_k1_cpu_dispatch.h>
485
+ #include <ATen/ops/special_shifted_chebyshev_polynomial_t_cpu_dispatch.h>
486
+ #include <ATen/ops/special_shifted_chebyshev_polynomial_u_cpu_dispatch.h>
487
+ #include <ATen/ops/special_shifted_chebyshev_polynomial_v_cpu_dispatch.h>
488
+ #include <ATen/ops/special_shifted_chebyshev_polynomial_w_cpu_dispatch.h>
489
+ #include <ATen/ops/special_spherical_bessel_j0_cpu_dispatch.h>
490
+ #include <ATen/ops/special_xlog1py_cpu_dispatch.h>
491
+ #include <ATen/ops/special_zeta_cpu_dispatch.h>
492
+ #include <ATen/ops/sqrt_cpu_dispatch.h>
493
+ #include <ATen/ops/sspaddmm_cpu_dispatch.h>
494
+ #include <ATen/ops/std_cpu_dispatch.h>
495
+ #include <ATen/ops/std_mean_cpu_dispatch.h>
496
+ #include <ATen/ops/sub_cpu_dispatch.h>
497
+ #include <ATen/ops/sum_cpu_dispatch.h>
498
+ #include <ATen/ops/take_cpu_dispatch.h>
499
+ #include <ATen/ops/tan_cpu_dispatch.h>
500
+ #include <ATen/ops/tanh_cpu_dispatch.h>
501
+ #include <ATen/ops/tanh_backward_cpu_dispatch.h>
502
+ #include <ATen/ops/threshold_cpu_dispatch.h>
503
+ #include <ATen/ops/threshold_backward_cpu_dispatch.h>
504
+ #include <ATen/ops/to_mkldnn_cpu_dispatch.h>
505
+ #include <ATen/ops/topk_cpu_dispatch.h>
506
+ #include <ATen/ops/trace_cpu_dispatch.h>
507
+ #include <ATen/ops/triangular_solve_cpu_dispatch.h>
508
+ #include <ATen/ops/tril_cpu_dispatch.h>
509
+ #include <ATen/ops/tril_indices_cpu_dispatch.h>
510
+ #include <ATen/ops/triu_cpu_dispatch.h>
511
+ #include <ATen/ops/triu_indices_cpu_dispatch.h>
512
+ #include <ATen/ops/trunc_cpu_dispatch.h>
513
+ #include <ATen/ops/unfold_cpu_dispatch.h>
514
+ #include <ATen/ops/unfold_backward_cpu_dispatch.h>
515
+ #include <ATen/ops/uniform_cpu_dispatch.h>
516
+ #include <ATen/ops/unique_consecutive_cpu_dispatch.h>
517
+ #include <ATen/ops/unique_dim_cpu_dispatch.h>
518
+ #include <ATen/ops/unique_dim_consecutive_cpu_dispatch.h>
519
+ #include <ATen/ops/upsample_bicubic2d_cpu_dispatch.h>
520
+ #include <ATen/ops/upsample_bicubic2d_backward_cpu_dispatch.h>
521
+ #include <ATen/ops/upsample_bilinear2d_cpu_dispatch.h>
522
+ #include <ATen/ops/upsample_bilinear2d_backward_cpu_dispatch.h>
523
+ #include <ATen/ops/upsample_linear1d_cpu_dispatch.h>
524
+ #include <ATen/ops/upsample_linear1d_backward_cpu_dispatch.h>
525
+ #include <ATen/ops/upsample_nearest1d_cpu_dispatch.h>
526
+ #include <ATen/ops/upsample_nearest1d_backward_cpu_dispatch.h>
527
+ #include <ATen/ops/upsample_nearest2d_cpu_dispatch.h>
528
+ #include <ATen/ops/upsample_nearest2d_backward_cpu_dispatch.h>
529
+ #include <ATen/ops/upsample_nearest3d_cpu_dispatch.h>
530
+ #include <ATen/ops/upsample_nearest3d_backward_cpu_dispatch.h>
531
+ #include <ATen/ops/upsample_trilinear3d_cpu_dispatch.h>
532
+ #include <ATen/ops/upsample_trilinear3d_backward_cpu_dispatch.h>
533
+ #include <ATen/ops/var_cpu_dispatch.h>
534
+ #include <ATen/ops/var_mean_cpu_dispatch.h>
535
+ #include <ATen/ops/vdot_cpu_dispatch.h>
536
+ #include <ATen/ops/view_cpu_dispatch.h>
537
+ #include <ATen/ops/view_as_complex_cpu_dispatch.h>
538
+ #include <ATen/ops/view_as_real_cpu_dispatch.h>
539
+ #include <ATen/ops/where_cpu_dispatch.h>
540
+ #include <ATen/ops/xlogy_cpu_dispatch.h>
541
+ #include <ATen/ops/zero_cpu_dispatch.h>
542
+
543
+
544
+
lib/python3.12/site-packages/torch/include/ATen/CPUGeneratorImpl.h ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <ATen/core/Generator.h>
4
+ #include <ATen/core/MT19937RNGEngine.h>
5
+ #include <c10/core/GeneratorImpl.h>
6
+ #include <optional>
7
+
8
+ namespace at {
9
+
10
+ struct TORCH_API CPUGeneratorImpl : public c10::GeneratorImpl {
11
+ // Constructors
12
+ CPUGeneratorImpl(uint64_t seed_in = default_rng_seed_val);
13
+ ~CPUGeneratorImpl() override = default;
14
+
15
+ // CPUGeneratorImpl methods
16
+ std::shared_ptr<CPUGeneratorImpl> clone() const;
17
+ void set_current_seed(uint64_t seed) override;
18
+ void set_offset(uint64_t offset) override;
19
+ uint64_t get_offset() const override;
20
+ uint64_t current_seed() const override;
21
+ uint64_t seed() override;
22
+ void set_state(const c10::TensorImpl& new_state) override;
23
+ c10::intrusive_ptr<c10::TensorImpl> get_state() const override;
24
+ static c10::DeviceType device_type();
25
+ uint32_t random();
26
+ uint64_t random64();
27
+ std::optional<float> next_float_normal_sample();
28
+ std::optional<double> next_double_normal_sample();
29
+ void set_next_float_normal_sample(std::optional<float> randn);
30
+ void set_next_double_normal_sample(std::optional<double> randn);
31
+ at::mt19937 engine();
32
+ void set_engine(at::mt19937 engine);
33
+
34
+ private:
35
+ CPUGeneratorImpl* clone_impl() const override;
36
+ at::mt19937 engine_;
37
+ std::optional<float> next_float_normal_sample_;
38
+ std::optional<double> next_double_normal_sample_;
39
+ };
40
+
41
+ namespace detail {
42
+
43
+ TORCH_API const Generator& getDefaultCPUGenerator();
44
+ TORCH_API Generator
45
+ createCPUGenerator(uint64_t seed_val = default_rng_seed_val);
46
+
47
+ } // namespace detail
48
+
49
+ } // namespace at
lib/python3.12/site-packages/torch/include/ATen/CUDAFunctions.h ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <ATen/core/TensorBody.h>
2
+
3
+ // TODO Undo all logic introduced for Note [Avoiding Include Cycles In Static Dispatch]
4
+ // Code introduced to avoid cyclic dependency in static dispatch is no longer
5
+ // needed as static dispatch logic is moved from TensorBody.h, which caused cycles in the first place,
6
+ // to Operators.cpp for supporting multiple backends with multiple kernels.
7
+ //
8
+ // Note [Avoiding Include Cycles In Static Dispatch]
9
+ // In order to avoid #include cycles in the static dispatch build, we've carefully split out
10
+ // the static function definition files into {DispatchKey}Functions.h and {DispatchKey}Functions_inl.h.
11
+ //
12
+ // Without this split, the include cycle looks like TensorBody.h -> CPUFunctions.h -> TensorBody.h.
13
+ // - TensorBody.h #includes CPUFunctions.h in the static dispatch build, because the tensor methods
14
+ // all need to call into the fastpath C++ API defined in CPUFunctions.h. The methods are also all
15
+ // directly inlined into TensorBody.h.
16
+ // - CPUFunctions.h #includes TensorBody.h because it contains function declarations for the entire C++ API,
17
+ // which include functions that have defaultable std::optional<Tensor> arguments.
18
+ // That requires knowing the full Tensor class definition.
19
+ //
20
+ // We break the cycle by doing the following:
21
+ // - Split out CPUFunction.h into two files: CPUFunctions.h and CPUFunctions_inl.h
22
+ // - CPUFunction.h is a dummy file that just includes the Tensor class and includes CPUFunctions_inl.,
23
+ // - CPUFunctions_inl.h includes everything else
24
+ // - (only in the static dispatch build) TensorBody.h makes sure to finish defining the Tensor class,
25
+ // and then it includes CPUFunctions_inl.h.
26
+ // - All other files that want the cpu fastpath functions can include CPUFunctions.h directly.
27
+ // - This also means that static dispatch build, CPUFunctions.h only needs to
28
+ // #include TensorBody.h, and it will automatically bring in CPUFunctions_inl.h.
29
+ #include <ATen/CUDAFunctions_inl.h>
lib/python3.12/site-packages/torch/include/ATen/CUDAFunctions_inl.h ADDED
@@ -0,0 +1,634 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+ // @generated by torchgen/gen.py from DispatchKeyFunctions_inl.h
3
+
4
+ // NB: The implementing C++ file is RegisterDispatchKey.cpp
5
+
6
+ // The only #includes we need are for custom classes that have defaults in the C++ API
7
+ #include <c10/core/MemoryFormat.h>
8
+ #include <c10/core/Scalar.h>
9
+ #include <ATen/core/Reduction.h>
10
+
11
+ #if defined(AT_PER_OPERATOR_HEADERS) && defined(TORCH_ASSERT_ONLY_METHOD_OPERATORS)
12
+ #error This change adds a dependency on all pytorch operators, meaning the \
13
+ file will need to be re-compiled every time an operator is changed or added. \
14
+ Consider including a specific operator from \
15
+ <ATen/ops/{my_operator}_cuda_dispatch.h>. \
16
+ See NOTE [TORCH_ASSERT_ONLY_METHOD_OPERATORS].
17
+ #endif
18
+
19
+ #include <ATen/ops/_adaptive_avg_pool2d_cuda_dispatch.h>
20
+ #include <ATen/ops/_adaptive_avg_pool2d_backward_cuda_dispatch.h>
21
+ #include <ATen/ops/_adaptive_avg_pool3d_cuda_dispatch.h>
22
+ #include <ATen/ops/_adaptive_avg_pool3d_backward_cuda_dispatch.h>
23
+ #include <ATen/ops/_addmm_activation_cuda_dispatch.h>
24
+ #include <ATen/ops/_aminmax_cuda_dispatch.h>
25
+ #include <ATen/ops/_amp_foreach_non_finite_check_and_unscale_cuda_dispatch.h>
26
+ #include <ATen/ops/_amp_update_scale_cuda_dispatch.h>
27
+ #include <ATen/ops/_assert_async_cuda_dispatch.h>
28
+ #include <ATen/ops/_batch_norm_with_update_cuda_dispatch.h>
29
+ #include <ATen/ops/_cdist_backward_cuda_dispatch.h>
30
+ #include <ATen/ops/_cdist_forward_cuda_dispatch.h>
31
+ #include <ATen/ops/_cholesky_solve_helper_cuda_dispatch.h>
32
+ #include <ATen/ops/_chunk_cat_cuda_dispatch.h>
33
+ #include <ATen/ops/_compute_linear_combination_cuda_dispatch.h>
34
+ #include <ATen/ops/_conv_depthwise2d_cuda_dispatch.h>
35
+ #include <ATen/ops/_convert_indices_from_coo_to_csr_cuda_dispatch.h>
36
+ #include <ATen/ops/_convert_indices_from_csr_to_coo_cuda_dispatch.h>
37
+ #include <ATen/ops/_convert_weight_to_int4pack_cuda_dispatch.h>
38
+ #include <ATen/ops/_cslt_compress_cuda_dispatch.h>
39
+ #include <ATen/ops/_cslt_sparse_mm_cuda_dispatch.h>
40
+ #include <ATen/ops/_cslt_sparse_mm_search_cuda_dispatch.h>
41
+ #include <ATen/ops/_ctc_loss_cuda_dispatch.h>
42
+ #include <ATen/ops/_ctc_loss_backward_cuda_dispatch.h>
43
+ #include <ATen/ops/_cudnn_attention_backward_cuda_dispatch.h>
44
+ #include <ATen/ops/_cudnn_attention_forward_cuda_dispatch.h>
45
+ #include <ATen/ops/_cudnn_ctc_loss_cuda_dispatch.h>
46
+ #include <ATen/ops/_cudnn_init_dropout_state_cuda_dispatch.h>
47
+ #include <ATen/ops/_cudnn_rnn_cuda_dispatch.h>
48
+ #include <ATen/ops/_cudnn_rnn_backward_cuda_dispatch.h>
49
+ #include <ATen/ops/_cudnn_rnn_flatten_weight_cuda_dispatch.h>
50
+ #include <ATen/ops/_cummax_helper_cuda_dispatch.h>
51
+ #include <ATen/ops/_cummin_helper_cuda_dispatch.h>
52
+ #include <ATen/ops/_dirichlet_grad_cuda_dispatch.h>
53
+ #include <ATen/ops/_efficient_attention_backward_cuda_dispatch.h>
54
+ #include <ATen/ops/_efficient_attention_forward_cuda_dispatch.h>
55
+ #include <ATen/ops/_efficientzerotensor_cuda_dispatch.h>
56
+ #include <ATen/ops/_embedding_bag_cuda_dispatch.h>
57
+ #include <ATen/ops/_embedding_bag_backward_cuda_dispatch.h>
58
+ #include <ATen/ops/_embedding_bag_dense_backward_cuda_dispatch.h>
59
+ #include <ATen/ops/_embedding_bag_forward_only_cuda_dispatch.h>
60
+ #include <ATen/ops/_embedding_bag_per_sample_weights_backward_cuda_dispatch.h>
61
+ #include <ATen/ops/_fake_quantize_learnable_per_channel_affine_cuda_dispatch.h>
62
+ #include <ATen/ops/_fake_quantize_learnable_per_channel_affine_backward_cuda_dispatch.h>
63
+ #include <ATen/ops/_fake_quantize_learnable_per_tensor_affine_cuda_dispatch.h>
64
+ #include <ATen/ops/_fake_quantize_learnable_per_tensor_affine_backward_cuda_dispatch.h>
65
+ #include <ATen/ops/_fake_quantize_per_tensor_affine_cachemask_tensor_qparams_cuda_dispatch.h>
66
+ #include <ATen/ops/_fft_c2c_cuda_dispatch.h>
67
+ #include <ATen/ops/_fft_c2r_cuda_dispatch.h>
68
+ #include <ATen/ops/_fft_r2c_cuda_dispatch.h>
69
+ #include <ATen/ops/_fill_mem_eff_dropout_mask_cuda_dispatch.h>
70
+ #include <ATen/ops/_flash_attention_backward_cuda_dispatch.h>
71
+ #include <ATen/ops/_flash_attention_forward_cuda_dispatch.h>
72
+ #include <ATen/ops/_foreach_abs_cuda_dispatch.h>
73
+ #include <ATen/ops/_foreach_acos_cuda_dispatch.h>
74
+ #include <ATen/ops/_foreach_add_cuda_dispatch.h>
75
+ #include <ATen/ops/_foreach_addcdiv_cuda_dispatch.h>
76
+ #include <ATen/ops/_foreach_addcmul_cuda_dispatch.h>
77
+ #include <ATen/ops/_foreach_asin_cuda_dispatch.h>
78
+ #include <ATen/ops/_foreach_atan_cuda_dispatch.h>
79
+ #include <ATen/ops/_foreach_ceil_cuda_dispatch.h>
80
+ #include <ATen/ops/_foreach_clamp_max_cuda_dispatch.h>
81
+ #include <ATen/ops/_foreach_clamp_min_cuda_dispatch.h>
82
+ #include <ATen/ops/_foreach_copy_cuda_dispatch.h>
83
+ #include <ATen/ops/_foreach_cos_cuda_dispatch.h>
84
+ #include <ATen/ops/_foreach_cosh_cuda_dispatch.h>
85
+ #include <ATen/ops/_foreach_div_cuda_dispatch.h>
86
+ #include <ATen/ops/_foreach_erf_cuda_dispatch.h>
87
+ #include <ATen/ops/_foreach_erfc_cuda_dispatch.h>
88
+ #include <ATen/ops/_foreach_exp_cuda_dispatch.h>
89
+ #include <ATen/ops/_foreach_expm1_cuda_dispatch.h>
90
+ #include <ATen/ops/_foreach_floor_cuda_dispatch.h>
91
+ #include <ATen/ops/_foreach_frac_cuda_dispatch.h>
92
+ #include <ATen/ops/_foreach_lerp_cuda_dispatch.h>
93
+ #include <ATen/ops/_foreach_lgamma_cuda_dispatch.h>
94
+ #include <ATen/ops/_foreach_log_cuda_dispatch.h>
95
+ #include <ATen/ops/_foreach_log10_cuda_dispatch.h>
96
+ #include <ATen/ops/_foreach_log1p_cuda_dispatch.h>
97
+ #include <ATen/ops/_foreach_log2_cuda_dispatch.h>
98
+ #include <ATen/ops/_foreach_max_cuda_dispatch.h>
99
+ #include <ATen/ops/_foreach_maximum_cuda_dispatch.h>
100
+ #include <ATen/ops/_foreach_minimum_cuda_dispatch.h>
101
+ #include <ATen/ops/_foreach_mul_cuda_dispatch.h>
102
+ #include <ATen/ops/_foreach_neg_cuda_dispatch.h>
103
+ #include <ATen/ops/_foreach_norm_cuda_dispatch.h>
104
+ #include <ATen/ops/_foreach_pow_cuda_dispatch.h>
105
+ #include <ATen/ops/_foreach_reciprocal_cuda_dispatch.h>
106
+ #include <ATen/ops/_foreach_round_cuda_dispatch.h>
107
+ #include <ATen/ops/_foreach_rsqrt_cuda_dispatch.h>
108
+ #include <ATen/ops/_foreach_sigmoid_cuda_dispatch.h>
109
+ #include <ATen/ops/_foreach_sign_cuda_dispatch.h>
110
+ #include <ATen/ops/_foreach_sin_cuda_dispatch.h>
111
+ #include <ATen/ops/_foreach_sinh_cuda_dispatch.h>
112
+ #include <ATen/ops/_foreach_sqrt_cuda_dispatch.h>
113
+ #include <ATen/ops/_foreach_sub_cuda_dispatch.h>
114
+ #include <ATen/ops/_foreach_tan_cuda_dispatch.h>
115
+ #include <ATen/ops/_foreach_tanh_cuda_dispatch.h>
116
+ #include <ATen/ops/_foreach_trunc_cuda_dispatch.h>
117
+ #include <ATen/ops/_foreach_zero_cuda_dispatch.h>
118
+ #include <ATen/ops/_fused_adagrad_cuda_dispatch.h>
119
+ #include <ATen/ops/_fused_adam_cuda_dispatch.h>
120
+ #include <ATen/ops/_fused_adamw_cuda_dispatch.h>
121
+ #include <ATen/ops/_fused_dropout_cuda_dispatch.h>
122
+ #include <ATen/ops/_fused_moving_avg_obs_fq_helper_cuda_dispatch.h>
123
+ #include <ATen/ops/_fused_rms_norm_cuda_dispatch.h>
124
+ #include <ATen/ops/_fused_rms_norm_backward_cuda_dispatch.h>
125
+ #include <ATen/ops/_fused_sdp_choice_cuda_dispatch.h>
126
+ #include <ATen/ops/_fused_sgd_cuda_dispatch.h>
127
+ #include <ATen/ops/_grouped_mm_cuda_dispatch.h>
128
+ #include <ATen/ops/_index_put_impl_cuda_dispatch.h>
129
+ #include <ATen/ops/_int_mm_cuda_dispatch.h>
130
+ #include <ATen/ops/_jagged_to_padded_dense_forward_cuda_dispatch.h>
131
+ #include <ATen/ops/_linalg_det_cuda_dispatch.h>
132
+ #include <ATen/ops/_linalg_eigh_cuda_dispatch.h>
133
+ #include <ATen/ops/_linalg_eigvals_cuda_dispatch.h>
134
+ #include <ATen/ops/_linalg_slogdet_cuda_dispatch.h>
135
+ #include <ATen/ops/_linalg_solve_ex_cuda_dispatch.h>
136
+ #include <ATen/ops/_linalg_svd_cuda_dispatch.h>
137
+ #include <ATen/ops/_local_scalar_dense_cuda_dispatch.h>
138
+ #include <ATen/ops/_log_softmax_cuda_dispatch.h>
139
+ #include <ATen/ops/_log_softmax_backward_data_cuda_dispatch.h>
140
+ #include <ATen/ops/_logcumsumexp_cuda_dispatch.h>
141
+ #include <ATen/ops/_make_per_channel_quantized_tensor_cuda_dispatch.h>
142
+ #include <ATen/ops/_make_per_tensor_quantized_tensor_cuda_dispatch.h>
143
+ #include <ATen/ops/_masked_scale_cuda_dispatch.h>
144
+ #include <ATen/ops/_masked_softmax_cuda_dispatch.h>
145
+ #include <ATen/ops/_masked_softmax_backward_cuda_dispatch.h>
146
+ #include <ATen/ops/_mixed_dtypes_linear_cuda_dispatch.h>
147
+ #include <ATen/ops/_native_batch_norm_legit_cuda_dispatch.h>
148
+ #include <ATen/ops/_native_multi_head_attention_cuda_dispatch.h>
149
+ #include <ATen/ops/_nested_compute_contiguous_strides_offsets_cuda_dispatch.h>
150
+ #include <ATen/ops/_nested_from_padded_cuda_dispatch.h>
151
+ #include <ATen/ops/_nested_tensor_from_mask_cuda_dispatch.h>
152
+ #include <ATen/ops/_nested_tensor_from_mask_left_aligned_cuda_dispatch.h>
153
+ #include <ATen/ops/_nested_view_from_buffer_cuda_dispatch.h>
154
+ #include <ATen/ops/_padded_dense_to_jagged_forward_cuda_dispatch.h>
155
+ #include <ATen/ops/_pdist_backward_cuda_dispatch.h>
156
+ #include <ATen/ops/_pdist_forward_cuda_dispatch.h>
157
+ #include <ATen/ops/_prelu_kernel_cuda_dispatch.h>
158
+ #include <ATen/ops/_prelu_kernel_backward_cuda_dispatch.h>
159
+ #include <ATen/ops/_reshape_alias_cuda_dispatch.h>
160
+ #include <ATen/ops/_sample_dirichlet_cuda_dispatch.h>
161
+ #include <ATen/ops/_scaled_dot_product_cudnn_attention_cuda_dispatch.h>
162
+ #include <ATen/ops/_scaled_dot_product_cudnn_attention_backward_cuda_dispatch.h>
163
+ #include <ATen/ops/_scaled_dot_product_efficient_attention_cuda_dispatch.h>
164
+ #include <ATen/ops/_scaled_dot_product_efficient_attention_backward_cuda_dispatch.h>
165
+ #include <ATen/ops/_scaled_dot_product_flash_attention_cuda_dispatch.h>
166
+ #include <ATen/ops/_scaled_dot_product_flash_attention_backward_cuda_dispatch.h>
167
+ #include <ATen/ops/_scaled_grouped_mm_cuda_dispatch.h>
168
+ #include <ATen/ops/_scaled_mm_cuda_dispatch.h>
169
+ #include <ATen/ops/_segment_reduce_backward_cuda_dispatch.h>
170
+ #include <ATen/ops/_slow_conv2d_backward_cuda_dispatch.h>
171
+ #include <ATen/ops/_slow_conv2d_forward_cuda_dispatch.h>
172
+ #include <ATen/ops/_softmax_cuda_dispatch.h>
173
+ #include <ATen/ops/_softmax_backward_data_cuda_dispatch.h>
174
+ #include <ATen/ops/_sparse_semi_structured_addmm_cuda_dispatch.h>
175
+ #include <ATen/ops/_sparse_semi_structured_apply_cuda_dispatch.h>
176
+ #include <ATen/ops/_sparse_semi_structured_apply_dense_cuda_dispatch.h>
177
+ #include <ATen/ops/_sparse_semi_structured_linear_cuda_dispatch.h>
178
+ #include <ATen/ops/_sparse_semi_structured_mm_cuda_dispatch.h>
179
+ #include <ATen/ops/_sparse_semi_structured_tile_cuda_dispatch.h>
180
+ #include <ATen/ops/_standard_gamma_cuda_dispatch.h>
181
+ #include <ATen/ops/_standard_gamma_grad_cuda_dispatch.h>
182
+ #include <ATen/ops/_thnn_fused_gru_cell_cuda_dispatch.h>
183
+ #include <ATen/ops/_thnn_fused_gru_cell_backward_cuda_dispatch.h>
184
+ #include <ATen/ops/_thnn_fused_lstm_cell_cuda_dispatch.h>
185
+ #include <ATen/ops/_thnn_fused_lstm_cell_backward_impl_cuda_dispatch.h>
186
+ #include <ATen/ops/_to_sparse_cuda_dispatch.h>
187
+ #include <ATen/ops/_to_sparse_bsc_cuda_dispatch.h>
188
+ #include <ATen/ops/_to_sparse_bsr_cuda_dispatch.h>
189
+ #include <ATen/ops/_to_sparse_csc_cuda_dispatch.h>
190
+ #include <ATen/ops/_to_sparse_csr_cuda_dispatch.h>
191
+ #include <ATen/ops/_to_sparse_semi_structured_cuda_dispatch.h>
192
+ #include <ATen/ops/_transform_bias_rescale_qkv_cuda_dispatch.h>
193
+ #include <ATen/ops/_transformer_encoder_layer_fwd_cuda_dispatch.h>
194
+ #include <ATen/ops/_triton_multi_head_attention_cuda_dispatch.h>
195
+ #include <ATen/ops/_triton_scaled_dot_attention_cuda_dispatch.h>
196
+ #include <ATen/ops/_unique_cuda_dispatch.h>
197
+ #include <ATen/ops/_unique2_cuda_dispatch.h>
198
+ #include <ATen/ops/_upsample_bicubic2d_aa_cuda_dispatch.h>
199
+ #include <ATen/ops/_upsample_bicubic2d_aa_backward_cuda_dispatch.h>
200
+ #include <ATen/ops/_upsample_bilinear2d_aa_cuda_dispatch.h>
201
+ #include <ATen/ops/_upsample_bilinear2d_aa_backward_cuda_dispatch.h>
202
+ #include <ATen/ops/_upsample_nearest_exact1d_cuda_dispatch.h>
203
+ #include <ATen/ops/_upsample_nearest_exact1d_backward_cuda_dispatch.h>
204
+ #include <ATen/ops/_upsample_nearest_exact2d_cuda_dispatch.h>
205
+ #include <ATen/ops/_upsample_nearest_exact2d_backward_cuda_dispatch.h>
206
+ #include <ATen/ops/_upsample_nearest_exact3d_cuda_dispatch.h>
207
+ #include <ATen/ops/_upsample_nearest_exact3d_backward_cuda_dispatch.h>
208
+ #include <ATen/ops/_use_cudnn_ctc_loss_cuda_dispatch.h>
209
+ #include <ATen/ops/_validate_compressed_sparse_indices_cuda_dispatch.h>
210
+ #include <ATen/ops/_weight_int4pack_mm_cuda_dispatch.h>
211
+ #include <ATen/ops/_weight_int8pack_mm_cuda_dispatch.h>
212
+ #include <ATen/ops/_weight_norm_interface_cuda_dispatch.h>
213
+ #include <ATen/ops/_weight_norm_interface_backward_cuda_dispatch.h>
214
+ #include <ATen/ops/abs_cuda_dispatch.h>
215
+ #include <ATen/ops/acos_cuda_dispatch.h>
216
+ #include <ATen/ops/acosh_cuda_dispatch.h>
217
+ #include <ATen/ops/adaptive_avg_pool2d_cuda_dispatch.h>
218
+ #include <ATen/ops/adaptive_avg_pool3d_cuda_dispatch.h>
219
+ #include <ATen/ops/adaptive_avg_pool3d_backward_cuda_dispatch.h>
220
+ #include <ATen/ops/adaptive_max_pool2d_cuda_dispatch.h>
221
+ #include <ATen/ops/adaptive_max_pool2d_backward_cuda_dispatch.h>
222
+ #include <ATen/ops/adaptive_max_pool3d_cuda_dispatch.h>
223
+ #include <ATen/ops/adaptive_max_pool3d_backward_cuda_dispatch.h>
224
+ #include <ATen/ops/add_cuda_dispatch.h>
225
+ #include <ATen/ops/addbmm_cuda_dispatch.h>
226
+ #include <ATen/ops/addcdiv_cuda_dispatch.h>
227
+ #include <ATen/ops/addcmul_cuda_dispatch.h>
228
+ #include <ATen/ops/addmm_cuda_dispatch.h>
229
+ #include <ATen/ops/addmv_cuda_dispatch.h>
230
+ #include <ATen/ops/addr_cuda_dispatch.h>
231
+ #include <ATen/ops/all_cuda_dispatch.h>
232
+ #include <ATen/ops/amax_cuda_dispatch.h>
233
+ #include <ATen/ops/amin_cuda_dispatch.h>
234
+ #include <ATen/ops/aminmax_cuda_dispatch.h>
235
+ #include <ATen/ops/angle_cuda_dispatch.h>
236
+ #include <ATen/ops/any_cuda_dispatch.h>
237
+ #include <ATen/ops/arange_cuda_dispatch.h>
238
+ #include <ATen/ops/argmax_cuda_dispatch.h>
239
+ #include <ATen/ops/argmin_cuda_dispatch.h>
240
+ #include <ATen/ops/as_strided_cuda_dispatch.h>
241
+ #include <ATen/ops/asin_cuda_dispatch.h>
242
+ #include <ATen/ops/asinh_cuda_dispatch.h>
243
+ #include <ATen/ops/atan_cuda_dispatch.h>
244
+ #include <ATen/ops/atan2_cuda_dispatch.h>
245
+ #include <ATen/ops/atanh_cuda_dispatch.h>
246
+ #include <ATen/ops/avg_pool2d_cuda_dispatch.h>
247
+ #include <ATen/ops/avg_pool2d_backward_cuda_dispatch.h>
248
+ #include <ATen/ops/avg_pool3d_cuda_dispatch.h>
249
+ #include <ATen/ops/avg_pool3d_backward_cuda_dispatch.h>
250
+ #include <ATen/ops/baddbmm_cuda_dispatch.h>
251
+ #include <ATen/ops/batch_norm_backward_cuda_dispatch.h>
252
+ #include <ATen/ops/batch_norm_backward_elemt_cuda_dispatch.h>
253
+ #include <ATen/ops/batch_norm_backward_reduce_cuda_dispatch.h>
254
+ #include <ATen/ops/batch_norm_elemt_cuda_dispatch.h>
255
+ #include <ATen/ops/batch_norm_gather_stats_cuda_dispatch.h>
256
+ #include <ATen/ops/batch_norm_gather_stats_with_counts_cuda_dispatch.h>
257
+ #include <ATen/ops/batch_norm_stats_cuda_dispatch.h>
258
+ #include <ATen/ops/batch_norm_update_stats_cuda_dispatch.h>
259
+ #include <ATen/ops/bernoulli_cuda_dispatch.h>
260
+ #include <ATen/ops/binary_cross_entropy_cuda_dispatch.h>
261
+ #include <ATen/ops/binary_cross_entropy_backward_cuda_dispatch.h>
262
+ #include <ATen/ops/bincount_cuda_dispatch.h>
263
+ #include <ATen/ops/binomial_cuda_dispatch.h>
264
+ #include <ATen/ops/bitwise_and_cuda_dispatch.h>
265
+ #include <ATen/ops/bitwise_left_shift_cuda_dispatch.h>
266
+ #include <ATen/ops/bitwise_not_cuda_dispatch.h>
267
+ #include <ATen/ops/bitwise_or_cuda_dispatch.h>
268
+ #include <ATen/ops/bitwise_right_shift_cuda_dispatch.h>
269
+ #include <ATen/ops/bitwise_xor_cuda_dispatch.h>
270
+ #include <ATen/ops/bmm_cuda_dispatch.h>
271
+ #include <ATen/ops/bucketize_cuda_dispatch.h>
272
+ #include <ATen/ops/cat_cuda_dispatch.h>
273
+ #include <ATen/ops/cauchy_cuda_dispatch.h>
274
+ #include <ATen/ops/ceil_cuda_dispatch.h>
275
+ #include <ATen/ops/channel_shuffle_cuda_dispatch.h>
276
+ #include <ATen/ops/cholesky_cuda_dispatch.h>
277
+ #include <ATen/ops/cholesky_inverse_cuda_dispatch.h>
278
+ #include <ATen/ops/clamp_cuda_dispatch.h>
279
+ #include <ATen/ops/clamp_max_cuda_dispatch.h>
280
+ #include <ATen/ops/clamp_min_cuda_dispatch.h>
281
+ #include <ATen/ops/col2im_cuda_dispatch.h>
282
+ #include <ATen/ops/complex_cuda_dispatch.h>
283
+ #include <ATen/ops/conj_physical_cuda_dispatch.h>
284
+ #include <ATen/ops/conv_depthwise3d_cuda_dispatch.h>
285
+ #include <ATen/ops/convolution_backward_cuda_dispatch.h>
286
+ #include <ATen/ops/copysign_cuda_dispatch.h>
287
+ #include <ATen/ops/cos_cuda_dispatch.h>
288
+ #include <ATen/ops/cosh_cuda_dispatch.h>
289
+ #include <ATen/ops/count_nonzero_cuda_dispatch.h>
290
+ #include <ATen/ops/cudnn_affine_grid_generator_cuda_dispatch.h>
291
+ #include <ATen/ops/cudnn_affine_grid_generator_backward_cuda_dispatch.h>
292
+ #include <ATen/ops/cudnn_batch_norm_cuda_dispatch.h>
293
+ #include <ATen/ops/cudnn_batch_norm_backward_cuda_dispatch.h>
294
+ #include <ATen/ops/cudnn_convolution_cuda_dispatch.h>
295
+ #include <ATen/ops/cudnn_convolution_add_relu_cuda_dispatch.h>
296
+ #include <ATen/ops/cudnn_convolution_relu_cuda_dispatch.h>
297
+ #include <ATen/ops/cudnn_convolution_transpose_cuda_dispatch.h>
298
+ #include <ATen/ops/cudnn_grid_sampler_cuda_dispatch.h>
299
+ #include <ATen/ops/cudnn_grid_sampler_backward_cuda_dispatch.h>
300
+ #include <ATen/ops/cumprod_cuda_dispatch.h>
301
+ #include <ATen/ops/cumsum_cuda_dispatch.h>
302
+ #include <ATen/ops/dequantize_cuda_dispatch.h>
303
+ #include <ATen/ops/digamma_cuda_dispatch.h>
304
+ #include <ATen/ops/div_cuda_dispatch.h>
305
+ #include <ATen/ops/dot_cuda_dispatch.h>
306
+ #include <ATen/ops/elu_cuda_dispatch.h>
307
+ #include <ATen/ops/elu_backward_cuda_dispatch.h>
308
+ #include <ATen/ops/embedding_dense_backward_cuda_dispatch.h>
309
+ #include <ATen/ops/embedding_renorm_cuda_dispatch.h>
310
+ #include <ATen/ops/empty_cuda_dispatch.h>
311
+ #include <ATen/ops/empty_strided_cuda_dispatch.h>
312
+ #include <ATen/ops/eq_cuda_dispatch.h>
313
+ #include <ATen/ops/equal_cuda_dispatch.h>
314
+ #include <ATen/ops/erf_cuda_dispatch.h>
315
+ #include <ATen/ops/erfc_cuda_dispatch.h>
316
+ #include <ATen/ops/erfinv_cuda_dispatch.h>
317
+ #include <ATen/ops/exp_cuda_dispatch.h>
318
+ #include <ATen/ops/exp2_cuda_dispatch.h>
319
+ #include <ATen/ops/expm1_cuda_dispatch.h>
320
+ #include <ATen/ops/exponential_cuda_dispatch.h>
321
+ #include <ATen/ops/eye_cuda_dispatch.h>
322
+ #include <ATen/ops/fake_quantize_per_channel_affine_cachemask_cuda_dispatch.h>
323
+ #include <ATen/ops/fake_quantize_per_tensor_affine_cachemask_cuda_dispatch.h>
324
+ #include <ATen/ops/fill_cuda_dispatch.h>
325
+ #include <ATen/ops/flip_cuda_dispatch.h>
326
+ #include <ATen/ops/floor_cuda_dispatch.h>
327
+ #include <ATen/ops/floor_divide_cuda_dispatch.h>
328
+ #include <ATen/ops/fmax_cuda_dispatch.h>
329
+ #include <ATen/ops/fmin_cuda_dispatch.h>
330
+ #include <ATen/ops/fmod_cuda_dispatch.h>
331
+ #include <ATen/ops/frac_cuda_dispatch.h>
332
+ #include <ATen/ops/fractional_max_pool2d_cuda_dispatch.h>
333
+ #include <ATen/ops/fractional_max_pool2d_backward_cuda_dispatch.h>
334
+ #include <ATen/ops/fractional_max_pool3d_cuda_dispatch.h>
335
+ #include <ATen/ops/fractional_max_pool3d_backward_cuda_dispatch.h>
336
+ #include <ATen/ops/frexp_cuda_dispatch.h>
337
+ #include <ATen/ops/gather_cuda_dispatch.h>
338
+ #include <ATen/ops/gcd_cuda_dispatch.h>
339
+ #include <ATen/ops/ge_cuda_dispatch.h>
340
+ #include <ATen/ops/gelu_cuda_dispatch.h>
341
+ #include <ATen/ops/gelu_backward_cuda_dispatch.h>
342
+ #include <ATen/ops/geometric_cuda_dispatch.h>
343
+ #include <ATen/ops/geqrf_cuda_dispatch.h>
344
+ #include <ATen/ops/glu_cuda_dispatch.h>
345
+ #include <ATen/ops/glu_backward_cuda_dispatch.h>
346
+ #include <ATen/ops/glu_backward_jvp_cuda_dispatch.h>
347
+ #include <ATen/ops/glu_jvp_cuda_dispatch.h>
348
+ #include <ATen/ops/grid_sampler_2d_cuda_dispatch.h>
349
+ #include <ATen/ops/grid_sampler_2d_backward_cuda_dispatch.h>
350
+ #include <ATen/ops/grid_sampler_3d_cuda_dispatch.h>
351
+ #include <ATen/ops/grid_sampler_3d_backward_cuda_dispatch.h>
352
+ #include <ATen/ops/gt_cuda_dispatch.h>
353
+ #include <ATen/ops/hardshrink_cuda_dispatch.h>
354
+ #include <ATen/ops/hardshrink_backward_cuda_dispatch.h>
355
+ #include <ATen/ops/hardsigmoid_cuda_dispatch.h>
356
+ #include <ATen/ops/hardsigmoid_backward_cuda_dispatch.h>
357
+ #include <ATen/ops/hardswish_cuda_dispatch.h>
358
+ #include <ATen/ops/hardswish_backward_cuda_dispatch.h>
359
+ #include <ATen/ops/hardtanh_cuda_dispatch.h>
360
+ #include <ATen/ops/hardtanh_backward_cuda_dispatch.h>
361
+ #include <ATen/ops/hash_tensor_cuda_dispatch.h>
362
+ #include <ATen/ops/heaviside_cuda_dispatch.h>
363
+ #include <ATen/ops/histc_cuda_dispatch.h>
364
+ #include <ATen/ops/huber_loss_cuda_dispatch.h>
365
+ #include <ATen/ops/huber_loss_backward_cuda_dispatch.h>
366
+ #include <ATen/ops/hypot_cuda_dispatch.h>
367
+ #include <ATen/ops/i0_cuda_dispatch.h>
368
+ #include <ATen/ops/igamma_cuda_dispatch.h>
369
+ #include <ATen/ops/igammac_cuda_dispatch.h>
370
+ #include <ATen/ops/im2col_cuda_dispatch.h>
371
+ #include <ATen/ops/index_cuda_dispatch.h>
372
+ #include <ATen/ops/index_add_cuda_dispatch.h>
373
+ #include <ATen/ops/index_copy_cuda_dispatch.h>
374
+ #include <ATen/ops/index_fill_cuda_dispatch.h>
375
+ #include <ATen/ops/index_reduce_cuda_dispatch.h>
376
+ #include <ATen/ops/index_select_cuda_dispatch.h>
377
+ #include <ATen/ops/is_set_to_cuda_dispatch.h>
378
+ #include <ATen/ops/isin_cuda_dispatch.h>
379
+ #include <ATen/ops/isnan_cuda_dispatch.h>
380
+ #include <ATen/ops/isneginf_cuda_dispatch.h>
381
+ #include <ATen/ops/isposinf_cuda_dispatch.h>
382
+ #include <ATen/ops/kthvalue_cuda_dispatch.h>
383
+ #include <ATen/ops/lcm_cuda_dispatch.h>
384
+ #include <ATen/ops/le_cuda_dispatch.h>
385
+ #include <ATen/ops/leaky_relu_cuda_dispatch.h>
386
+ #include <ATen/ops/leaky_relu_backward_cuda_dispatch.h>
387
+ #include <ATen/ops/lerp_cuda_dispatch.h>
388
+ #include <ATen/ops/lgamma_cuda_dispatch.h>
389
+ #include <ATen/ops/linalg_cholesky_ex_cuda_dispatch.h>
390
+ #include <ATen/ops/linalg_cross_cuda_dispatch.h>
391
+ #include <ATen/ops/linalg_eig_cuda_dispatch.h>
392
+ #include <ATen/ops/linalg_eigvals_cuda_dispatch.h>
393
+ #include <ATen/ops/linalg_householder_product_cuda_dispatch.h>
394
+ #include <ATen/ops/linalg_inv_ex_cuda_dispatch.h>
395
+ #include <ATen/ops/linalg_ldl_factor_ex_cuda_dispatch.h>
396
+ #include <ATen/ops/linalg_ldl_solve_cuda_dispatch.h>
397
+ #include <ATen/ops/linalg_lstsq_cuda_dispatch.h>
398
+ #include <ATen/ops/linalg_lu_cuda_dispatch.h>
399
+ #include <ATen/ops/linalg_lu_factor_ex_cuda_dispatch.h>
400
+ #include <ATen/ops/linalg_lu_solve_cuda_dispatch.h>
401
+ #include <ATen/ops/linalg_matrix_exp_cuda_dispatch.h>
402
+ #include <ATen/ops/linalg_qr_cuda_dispatch.h>
403
+ #include <ATen/ops/linalg_solve_triangular_cuda_dispatch.h>
404
+ #include <ATen/ops/linalg_vector_norm_cuda_dispatch.h>
405
+ #include <ATen/ops/linspace_cuda_dispatch.h>
406
+ #include <ATen/ops/log_cuda_dispatch.h>
407
+ #include <ATen/ops/log10_cuda_dispatch.h>
408
+ #include <ATen/ops/log1p_cuda_dispatch.h>
409
+ #include <ATen/ops/log2_cuda_dispatch.h>
410
+ #include <ATen/ops/log_normal_cuda_dispatch.h>
411
+ #include <ATen/ops/log_sigmoid_backward_cuda_dispatch.h>
412
+ #include <ATen/ops/log_sigmoid_forward_cuda_dispatch.h>
413
+ #include <ATen/ops/logaddexp_cuda_dispatch.h>
414
+ #include <ATen/ops/logaddexp2_cuda_dispatch.h>
415
+ #include <ATen/ops/logical_and_cuda_dispatch.h>
416
+ #include <ATen/ops/logical_not_cuda_dispatch.h>
417
+ #include <ATen/ops/logical_or_cuda_dispatch.h>
418
+ #include <ATen/ops/logical_xor_cuda_dispatch.h>
419
+ #include <ATen/ops/logit_cuda_dispatch.h>
420
+ #include <ATen/ops/logit_backward_cuda_dispatch.h>
421
+ #include <ATen/ops/logspace_cuda_dispatch.h>
422
+ #include <ATen/ops/lshift_cuda_dispatch.h>
423
+ #include <ATen/ops/lt_cuda_dispatch.h>
424
+ #include <ATen/ops/lu_unpack_cuda_dispatch.h>
425
+ #include <ATen/ops/masked_fill_cuda_dispatch.h>
426
+ #include <ATen/ops/masked_scatter_cuda_dispatch.h>
427
+ #include <ATen/ops/masked_select_cuda_dispatch.h>
428
+ #include <ATen/ops/max_cuda_dispatch.h>
429
+ #include <ATen/ops/max_pool2d_with_indices_cuda_dispatch.h>
430
+ #include <ATen/ops/max_pool2d_with_indices_backward_cuda_dispatch.h>
431
+ #include <ATen/ops/max_pool3d_with_indices_cuda_dispatch.h>
432
+ #include <ATen/ops/max_pool3d_with_indices_backward_cuda_dispatch.h>
433
+ #include <ATen/ops/max_unpool2d_cuda_dispatch.h>
434
+ #include <ATen/ops/max_unpool3d_cuda_dispatch.h>
435
+ #include <ATen/ops/maximum_cuda_dispatch.h>
436
+ #include <ATen/ops/mean_cuda_dispatch.h>
437
+ #include <ATen/ops/median_cuda_dispatch.h>
438
+ #include <ATen/ops/min_cuda_dispatch.h>
439
+ #include <ATen/ops/minimum_cuda_dispatch.h>
440
+ #include <ATen/ops/miopen_batch_norm_cuda_dispatch.h>
441
+ #include <ATen/ops/miopen_batch_norm_backward_cuda_dispatch.h>
442
+ #include <ATen/ops/miopen_convolution_cuda_dispatch.h>
443
+ #include <ATen/ops/miopen_convolution_add_relu_cuda_dispatch.h>
444
+ #include <ATen/ops/miopen_convolution_relu_cuda_dispatch.h>
445
+ #include <ATen/ops/miopen_convolution_transpose_cuda_dispatch.h>
446
+ #include <ATen/ops/miopen_depthwise_convolution_cuda_dispatch.h>
447
+ #include <ATen/ops/miopen_rnn_cuda_dispatch.h>
448
+ #include <ATen/ops/miopen_rnn_backward_cuda_dispatch.h>
449
+ #include <ATen/ops/mish_cuda_dispatch.h>
450
+ #include <ATen/ops/mish_backward_cuda_dispatch.h>
451
+ #include <ATen/ops/mm_cuda_dispatch.h>
452
+ #include <ATen/ops/mode_cuda_dispatch.h>
453
+ #include <ATen/ops/mse_loss_cuda_dispatch.h>
454
+ #include <ATen/ops/mse_loss_backward_cuda_dispatch.h>
455
+ #include <ATen/ops/mul_cuda_dispatch.h>
456
+ #include <ATen/ops/multi_margin_loss_cuda_dispatch.h>
457
+ #include <ATen/ops/multi_margin_loss_backward_cuda_dispatch.h>
458
+ #include <ATen/ops/multilabel_margin_loss_backward_cuda_dispatch.h>
459
+ #include <ATen/ops/multilabel_margin_loss_forward_cuda_dispatch.h>
460
+ #include <ATen/ops/multinomial_cuda_dispatch.h>
461
+ #include <ATen/ops/mvlgamma_cuda_dispatch.h>
462
+ #include <ATen/ops/nan_to_num_cuda_dispatch.h>
463
+ #include <ATen/ops/nanmedian_cuda_dispatch.h>
464
+ #include <ATen/ops/nansum_cuda_dispatch.h>
465
+ #include <ATen/ops/native_batch_norm_cuda_dispatch.h>
466
+ #include <ATen/ops/native_batch_norm_backward_cuda_dispatch.h>
467
+ #include <ATen/ops/native_dropout_cuda_dispatch.h>
468
+ #include <ATen/ops/native_dropout_backward_cuda_dispatch.h>
469
+ #include <ATen/ops/native_group_norm_cuda_dispatch.h>
470
+ #include <ATen/ops/native_group_norm_backward_cuda_dispatch.h>
471
+ #include <ATen/ops/native_layer_norm_cuda_dispatch.h>
472
+ #include <ATen/ops/native_layer_norm_backward_cuda_dispatch.h>
473
+ #include <ATen/ops/ne_cuda_dispatch.h>
474
+ #include <ATen/ops/neg_cuda_dispatch.h>
475
+ #include <ATen/ops/nextafter_cuda_dispatch.h>
476
+ #include <ATen/ops/nll_loss2d_backward_cuda_dispatch.h>
477
+ #include <ATen/ops/nll_loss2d_forward_cuda_dispatch.h>
478
+ #include <ATen/ops/nll_loss_backward_cuda_dispatch.h>
479
+ #include <ATen/ops/nll_loss_forward_cuda_dispatch.h>
480
+ #include <ATen/ops/nonzero_cuda_dispatch.h>
481
+ #include <ATen/ops/nonzero_static_cuda_dispatch.h>
482
+ #include <ATen/ops/norm_cuda_dispatch.h>
483
+ #include <ATen/ops/normal_cuda_dispatch.h>
484
+ #include <ATen/ops/ormqr_cuda_dispatch.h>
485
+ #include <ATen/ops/poisson_cuda_dispatch.h>
486
+ #include <ATen/ops/polar_cuda_dispatch.h>
487
+ #include <ATen/ops/polygamma_cuda_dispatch.h>
488
+ #include <ATen/ops/pow_cuda_dispatch.h>
489
+ #include <ATen/ops/prod_cuda_dispatch.h>
490
+ #include <ATen/ops/put_cuda_dispatch.h>
491
+ #include <ATen/ops/quantize_per_channel_cuda_dispatch.h>
492
+ #include <ATen/ops/quantize_per_tensor_cuda_dispatch.h>
493
+ #include <ATen/ops/quantize_per_tensor_dynamic_cuda_dispatch.h>
494
+ #include <ATen/ops/random_cuda_dispatch.h>
495
+ #include <ATen/ops/randperm_cuda_dispatch.h>
496
+ #include <ATen/ops/range_cuda_dispatch.h>
497
+ #include <ATen/ops/reciprocal_cuda_dispatch.h>
498
+ #include <ATen/ops/record_stream_cuda_dispatch.h>
499
+ #include <ATen/ops/reflection_pad1d_cuda_dispatch.h>
500
+ #include <ATen/ops/reflection_pad1d_backward_cuda_dispatch.h>
501
+ #include <ATen/ops/reflection_pad2d_cuda_dispatch.h>
502
+ #include <ATen/ops/reflection_pad2d_backward_cuda_dispatch.h>
503
+ #include <ATen/ops/reflection_pad3d_cuda_dispatch.h>
504
+ #include <ATen/ops/reflection_pad3d_backward_cuda_dispatch.h>
505
+ #include <ATen/ops/relu_cuda_dispatch.h>
506
+ #include <ATen/ops/remainder_cuda_dispatch.h>
507
+ #include <ATen/ops/renorm_cuda_dispatch.h>
508
+ #include <ATen/ops/repeat_interleave_cuda_dispatch.h>
509
+ #include <ATen/ops/replication_pad1d_cuda_dispatch.h>
510
+ #include <ATen/ops/replication_pad1d_backward_cuda_dispatch.h>
511
+ #include <ATen/ops/replication_pad2d_cuda_dispatch.h>
512
+ #include <ATen/ops/replication_pad2d_backward_cuda_dispatch.h>
513
+ #include <ATen/ops/replication_pad3d_cuda_dispatch.h>
514
+ #include <ATen/ops/replication_pad3d_backward_cuda_dispatch.h>
515
+ #include <ATen/ops/resize_cuda_dispatch.h>
516
+ #include <ATen/ops/roll_cuda_dispatch.h>
517
+ #include <ATen/ops/round_cuda_dispatch.h>
518
+ #include <ATen/ops/rrelu_with_noise_cuda_dispatch.h>
519
+ #include <ATen/ops/rshift_cuda_dispatch.h>
520
+ #include <ATen/ops/rsqrt_cuda_dispatch.h>
521
+ #include <ATen/ops/rsub_cuda_dispatch.h>
522
+ #include <ATen/ops/scatter_cuda_dispatch.h>
523
+ #include <ATen/ops/scatter_add_cuda_dispatch.h>
524
+ #include <ATen/ops/scatter_reduce_cuda_dispatch.h>
525
+ #include <ATen/ops/searchsorted_cuda_dispatch.h>
526
+ #include <ATen/ops/segment_reduce_cuda_dispatch.h>
527
+ #include <ATen/ops/set_cuda_dispatch.h>
528
+ #include <ATen/ops/sgn_cuda_dispatch.h>
529
+ #include <ATen/ops/sigmoid_cuda_dispatch.h>
530
+ #include <ATen/ops/sigmoid_backward_cuda_dispatch.h>
531
+ #include <ATen/ops/sign_cuda_dispatch.h>
532
+ #include <ATen/ops/signbit_cuda_dispatch.h>
533
+ #include <ATen/ops/silu_cuda_dispatch.h>
534
+ #include <ATen/ops/silu_backward_cuda_dispatch.h>
535
+ #include <ATen/ops/sin_cuda_dispatch.h>
536
+ #include <ATen/ops/sinc_cuda_dispatch.h>
537
+ #include <ATen/ops/sinh_cuda_dispatch.h>
538
+ #include <ATen/ops/slow_conv_dilated2d_cuda_dispatch.h>
539
+ #include <ATen/ops/slow_conv_dilated3d_cuda_dispatch.h>
540
+ #include <ATen/ops/slow_conv_transpose2d_cuda_dispatch.h>
541
+ #include <ATen/ops/slow_conv_transpose3d_cuda_dispatch.h>
542
+ #include <ATen/ops/smooth_l1_loss_cuda_dispatch.h>
543
+ #include <ATen/ops/smooth_l1_loss_backward_cuda_dispatch.h>
544
+ #include <ATen/ops/softplus_cuda_dispatch.h>
545
+ #include <ATen/ops/softplus_backward_cuda_dispatch.h>
546
+ #include <ATen/ops/softshrink_cuda_dispatch.h>
547
+ #include <ATen/ops/softshrink_backward_cuda_dispatch.h>
548
+ #include <ATen/ops/sort_cuda_dispatch.h>
549
+ #include <ATen/ops/special_airy_ai_cuda_dispatch.h>
550
+ #include <ATen/ops/special_bessel_j0_cuda_dispatch.h>
551
+ #include <ATen/ops/special_bessel_j1_cuda_dispatch.h>
552
+ #include <ATen/ops/special_bessel_y0_cuda_dispatch.h>
553
+ #include <ATen/ops/special_bessel_y1_cuda_dispatch.h>
554
+ #include <ATen/ops/special_chebyshev_polynomial_t_cuda_dispatch.h>
555
+ #include <ATen/ops/special_chebyshev_polynomial_u_cuda_dispatch.h>
556
+ #include <ATen/ops/special_chebyshev_polynomial_v_cuda_dispatch.h>
557
+ #include <ATen/ops/special_chebyshev_polynomial_w_cuda_dispatch.h>
558
+ #include <ATen/ops/special_entr_cuda_dispatch.h>
559
+ #include <ATen/ops/special_erfcx_cuda_dispatch.h>
560
+ #include <ATen/ops/special_hermite_polynomial_h_cuda_dispatch.h>
561
+ #include <ATen/ops/special_hermite_polynomial_he_cuda_dispatch.h>
562
+ #include <ATen/ops/special_i0e_cuda_dispatch.h>
563
+ #include <ATen/ops/special_i1_cuda_dispatch.h>
564
+ #include <ATen/ops/special_i1e_cuda_dispatch.h>
565
+ #include <ATen/ops/special_laguerre_polynomial_l_cuda_dispatch.h>
566
+ #include <ATen/ops/special_legendre_polynomial_p_cuda_dispatch.h>
567
+ #include <ATen/ops/special_log_ndtr_cuda_dispatch.h>
568
+ #include <ATen/ops/special_modified_bessel_i0_cuda_dispatch.h>
569
+ #include <ATen/ops/special_modified_bessel_i1_cuda_dispatch.h>
570
+ #include <ATen/ops/special_modified_bessel_k0_cuda_dispatch.h>
571
+ #include <ATen/ops/special_modified_bessel_k1_cuda_dispatch.h>
572
+ #include <ATen/ops/special_ndtri_cuda_dispatch.h>
573
+ #include <ATen/ops/special_scaled_modified_bessel_k0_cuda_dispatch.h>
574
+ #include <ATen/ops/special_scaled_modified_bessel_k1_cuda_dispatch.h>
575
+ #include <ATen/ops/special_shifted_chebyshev_polynomial_t_cuda_dispatch.h>
576
+ #include <ATen/ops/special_shifted_chebyshev_polynomial_u_cuda_dispatch.h>
577
+ #include <ATen/ops/special_shifted_chebyshev_polynomial_v_cuda_dispatch.h>
578
+ #include <ATen/ops/special_shifted_chebyshev_polynomial_w_cuda_dispatch.h>
579
+ #include <ATen/ops/special_spherical_bessel_j0_cuda_dispatch.h>
580
+ #include <ATen/ops/special_xlog1py_cuda_dispatch.h>
581
+ #include <ATen/ops/special_zeta_cuda_dispatch.h>
582
+ #include <ATen/ops/split_with_sizes_copy_cuda_dispatch.h>
583
+ #include <ATen/ops/sqrt_cuda_dispatch.h>
584
+ #include <ATen/ops/sspaddmm_cuda_dispatch.h>
585
+ #include <ATen/ops/std_cuda_dispatch.h>
586
+ #include <ATen/ops/std_mean_cuda_dispatch.h>
587
+ #include <ATen/ops/sub_cuda_dispatch.h>
588
+ #include <ATen/ops/sum_cuda_dispatch.h>
589
+ #include <ATen/ops/take_cuda_dispatch.h>
590
+ #include <ATen/ops/tan_cuda_dispatch.h>
591
+ #include <ATen/ops/tanh_cuda_dispatch.h>
592
+ #include <ATen/ops/tanh_backward_cuda_dispatch.h>
593
+ #include <ATen/ops/threshold_cuda_dispatch.h>
594
+ #include <ATen/ops/threshold_backward_cuda_dispatch.h>
595
+ #include <ATen/ops/topk_cuda_dispatch.h>
596
+ #include <ATen/ops/trace_cuda_dispatch.h>
597
+ #include <ATen/ops/triangular_solve_cuda_dispatch.h>
598
+ #include <ATen/ops/tril_cuda_dispatch.h>
599
+ #include <ATen/ops/tril_indices_cuda_dispatch.h>
600
+ #include <ATen/ops/triu_cuda_dispatch.h>
601
+ #include <ATen/ops/triu_indices_cuda_dispatch.h>
602
+ #include <ATen/ops/trunc_cuda_dispatch.h>
603
+ #include <ATen/ops/unfold_cuda_dispatch.h>
604
+ #include <ATen/ops/unfold_backward_cuda_dispatch.h>
605
+ #include <ATen/ops/uniform_cuda_dispatch.h>
606
+ #include <ATen/ops/unique_consecutive_cuda_dispatch.h>
607
+ #include <ATen/ops/unique_dim_cuda_dispatch.h>
608
+ #include <ATen/ops/unique_dim_consecutive_cuda_dispatch.h>
609
+ #include <ATen/ops/upsample_bicubic2d_cuda_dispatch.h>
610
+ #include <ATen/ops/upsample_bicubic2d_backward_cuda_dispatch.h>
611
+ #include <ATen/ops/upsample_bilinear2d_cuda_dispatch.h>
612
+ #include <ATen/ops/upsample_bilinear2d_backward_cuda_dispatch.h>
613
+ #include <ATen/ops/upsample_linear1d_cuda_dispatch.h>
614
+ #include <ATen/ops/upsample_linear1d_backward_cuda_dispatch.h>
615
+ #include <ATen/ops/upsample_nearest1d_cuda_dispatch.h>
616
+ #include <ATen/ops/upsample_nearest1d_backward_cuda_dispatch.h>
617
+ #include <ATen/ops/upsample_nearest2d_cuda_dispatch.h>
618
+ #include <ATen/ops/upsample_nearest2d_backward_cuda_dispatch.h>
619
+ #include <ATen/ops/upsample_nearest3d_cuda_dispatch.h>
620
+ #include <ATen/ops/upsample_nearest3d_backward_cuda_dispatch.h>
621
+ #include <ATen/ops/upsample_trilinear3d_cuda_dispatch.h>
622
+ #include <ATen/ops/upsample_trilinear3d_backward_cuda_dispatch.h>
623
+ #include <ATen/ops/var_cuda_dispatch.h>
624
+ #include <ATen/ops/var_mean_cuda_dispatch.h>
625
+ #include <ATen/ops/vdot_cuda_dispatch.h>
626
+ #include <ATen/ops/view_cuda_dispatch.h>
627
+ #include <ATen/ops/view_as_complex_cuda_dispatch.h>
628
+ #include <ATen/ops/view_as_real_cuda_dispatch.h>
629
+ #include <ATen/ops/where_cuda_dispatch.h>
630
+ #include <ATen/ops/xlogy_cuda_dispatch.h>
631
+ #include <ATen/ops/zero_cuda_dispatch.h>
632
+
633
+
634
+
lib/python3.12/site-packages/torch/include/ATen/CachedTensorUtils.h ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <ATen/ATen.h>
4
+
5
+ namespace at::caching {
6
+
7
+ // Some systems (just cudagraphs currently) will persist a static tensor output
8
+ // whose TensorImpl does not change across iterations. For these tensors caching
9
+ // dtype conversions is invalid. Additionally, there will be an extra reference
10
+ // count to these cached tensors that would prevent buffer inplacing and other
11
+ // checks on tensor uniqueness. If we are not using these systems the enabled
12
+ // flag will be false and we will avoid the hash lookup.
13
+
14
+ TORCH_API bool is_cached_tensor(const at::Tensor& t);
15
+ TORCH_API void add_cached_tensor(const at::Tensor& t);
16
+ TORCH_API void remove_cached_tensor(const at::Tensor& t);
17
+ TORCH_API void set_cached_tensors_enabled(bool enable);
18
+
19
+ // For gradient buffer stealing we will adjust the use count of tensors
20
+ // which are persisted by cudagraphs, just as we need to adjust reference
21
+ // count of tensors with hooks.
22
+ TORCH_API size_t adjusted_use_count(const at::Tensor& t);
23
+
24
+ } // namespace at::caching
lib/python3.12/site-packages/torch/include/ATen/CompositeExplicitAutogradFunctions.h ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <ATen/core/TensorBody.h>
2
+
3
+ // TODO Undo all logic introduced for Note [Avoiding Include Cycles In Static Dispatch]
4
+ // Code introduced to avoid cyclic dependency in static dispatch is no longer
5
+ // needed as static dispatch logic is moved from TensorBody.h, which caused cycles in the first place,
6
+ // to Operators.cpp for supporting multiple backends with multiple kernels.
7
+ //
8
+ // Note [Avoiding Include Cycles In Static Dispatch]
9
+ // In order to avoid #include cycles in the static dispatch build, we've carefully split out
10
+ // the static function definition files into {DispatchKey}Functions.h and {DispatchKey}Functions_inl.h.
11
+ //
12
+ // Without this split, the include cycle looks like TensorBody.h -> CPUFunctions.h -> TensorBody.h.
13
+ // - TensorBody.h #includes CPUFunctions.h in the static dispatch build, because the tensor methods
14
+ // all need to call into the fastpath C++ API defined in CPUFunctions.h. The methods are also all
15
+ // directly inlined into TensorBody.h.
16
+ // - CPUFunctions.h #includes TensorBody.h because it contains function declarations for the entire C++ API,
17
+ // which include functions that have defaultable std::optional<Tensor> arguments.
18
+ // That requires knowing the full Tensor class definition.
19
+ //
20
+ // We break the cycle by doing the following:
21
+ // - Split out CPUFunction.h into two files: CPUFunctions.h and CPUFunctions_inl.h
22
+ // - CPUFunction.h is a dummy file that just includes the Tensor class and includes CPUFunctions_inl.,
23
+ // - CPUFunctions_inl.h includes everything else
24
+ // - (only in the static dispatch build) TensorBody.h makes sure to finish defining the Tensor class,
25
+ // and then it includes CPUFunctions_inl.h.
26
+ // - All other files that want the cpu fastpath functions can include CPUFunctions.h directly.
27
+ // - This also means that static dispatch build, CPUFunctions.h only needs to
28
+ // #include TensorBody.h, and it will automatically bring in CPUFunctions_inl.h.
29
+ #include <ATen/CompositeExplicitAutogradFunctions_inl.h>
lib/python3.12/site-packages/torch/include/ATen/CompositeExplicitAutogradFunctions_inl.h ADDED
@@ -0,0 +1,560 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+ // @generated by torchgen/gen.py from DispatchKeyFunctions_inl.h
3
+
4
+ // NB: The implementing C++ file is RegisterDispatchKey.cpp
5
+
6
+ // The only #includes we need are for custom classes that have defaults in the C++ API
7
+ #include <c10/core/MemoryFormat.h>
8
+ #include <c10/core/Scalar.h>
9
+ #include <ATen/core/Reduction.h>
10
+
11
+ #if defined(AT_PER_OPERATOR_HEADERS) && defined(TORCH_ASSERT_ONLY_METHOD_OPERATORS)
12
+ #error This change adds a dependency on all pytorch operators, meaning the \
13
+ file will need to be re-compiled every time an operator is changed or added. \
14
+ Consider including a specific operator from \
15
+ <ATen/ops/{my_operator}_compositeexplicitautograd_dispatch.h>. \
16
+ See NOTE [TORCH_ASSERT_ONLY_METHOD_OPERATORS].
17
+ #endif
18
+
19
+ #include <ATen/ops/_adaptive_avg_pool2d_compositeexplicitautograd_dispatch.h>
20
+ #include <ATen/ops/_adaptive_avg_pool2d_backward_compositeexplicitautograd_dispatch.h>
21
+ #include <ATen/ops/_adaptive_avg_pool3d_compositeexplicitautograd_dispatch.h>
22
+ #include <ATen/ops/_adaptive_avg_pool3d_backward_compositeexplicitautograd_dispatch.h>
23
+ #include <ATen/ops/_add_relu_compositeexplicitautograd_dispatch.h>
24
+ #include <ATen/ops/_aminmax_compositeexplicitautograd_dispatch.h>
25
+ #include <ATen/ops/_amp_foreach_non_finite_check_and_unscale_compositeexplicitautograd_dispatch.h>
26
+ #include <ATen/ops/_amp_update_scale_compositeexplicitautograd_dispatch.h>
27
+ #include <ATen/ops/_assert_scalar_compositeexplicitautograd_dispatch.h>
28
+ #include <ATen/ops/_assert_tensor_metadata_compositeexplicitautograd_dispatch.h>
29
+ #include <ATen/ops/_batch_norm_no_update_compositeexplicitautograd_dispatch.h>
30
+ #include <ATen/ops/_batch_norm_with_update_compositeexplicitautograd_dispatch.h>
31
+ #include <ATen/ops/_cdist_backward_compositeexplicitautograd_dispatch.h>
32
+ #include <ATen/ops/_cdist_forward_compositeexplicitautograd_dispatch.h>
33
+ #include <ATen/ops/_cholesky_solve_helper_compositeexplicitautograd_dispatch.h>
34
+ #include <ATen/ops/_chunk_cat_compositeexplicitautograd_dispatch.h>
35
+ #include <ATen/ops/_coalesce_compositeexplicitautograd_dispatch.h>
36
+ #include <ATen/ops/_coalesced_compositeexplicitautograd_dispatch.h>
37
+ #include <ATen/ops/_conj_compositeexplicitautograd_dispatch.h>
38
+ #include <ATen/ops/_conj_copy_compositeexplicitautograd_dispatch.h>
39
+ #include <ATen/ops/_conj_physical_compositeexplicitautograd_dispatch.h>
40
+ #include <ATen/ops/_convolution_compositeexplicitautograd_dispatch.h>
41
+ #include <ATen/ops/_copy_from_compositeexplicitautograd_dispatch.h>
42
+ #include <ATen/ops/_copy_from_and_resize_compositeexplicitautograd_dispatch.h>
43
+ #include <ATen/ops/_ctc_loss_compositeexplicitautograd_dispatch.h>
44
+ #include <ATen/ops/_ctc_loss_backward_compositeexplicitautograd_dispatch.h>
45
+ #include <ATen/ops/_cudnn_ctc_loss_compositeexplicitautograd_dispatch.h>
46
+ #include <ATen/ops/_cudnn_init_dropout_state_compositeexplicitautograd_dispatch.h>
47
+ #include <ATen/ops/_cudnn_rnn_compositeexplicitautograd_dispatch.h>
48
+ #include <ATen/ops/_cudnn_rnn_backward_compositeexplicitautograd_dispatch.h>
49
+ #include <ATen/ops/_cudnn_rnn_flatten_weight_compositeexplicitautograd_dispatch.h>
50
+ #include <ATen/ops/_dirichlet_grad_compositeexplicitautograd_dispatch.h>
51
+ #include <ATen/ops/_efficientzerotensor_compositeexplicitautograd_dispatch.h>
52
+ #include <ATen/ops/_embedding_bag_compositeexplicitautograd_dispatch.h>
53
+ #include <ATen/ops/_embedding_bag_dense_backward_compositeexplicitautograd_dispatch.h>
54
+ #include <ATen/ops/_embedding_bag_forward_only_compositeexplicitautograd_dispatch.h>
55
+ #include <ATen/ops/_embedding_bag_per_sample_weights_backward_compositeexplicitautograd_dispatch.h>
56
+ #include <ATen/ops/_empty_affine_quantized_compositeexplicitautograd_dispatch.h>
57
+ #include <ATen/ops/_empty_per_channel_affine_quantized_compositeexplicitautograd_dispatch.h>
58
+ #include <ATen/ops/_euclidean_dist_compositeexplicitautograd_dispatch.h>
59
+ #include <ATen/ops/_fake_quantize_learnable_per_channel_affine_compositeexplicitautograd_dispatch.h>
60
+ #include <ATen/ops/_fake_quantize_learnable_per_tensor_affine_compositeexplicitautograd_dispatch.h>
61
+ #include <ATen/ops/_fake_quantize_per_tensor_affine_cachemask_tensor_qparams_compositeexplicitautograd_dispatch.h>
62
+ #include <ATen/ops/_foobar_compositeexplicitautograd_dispatch.h>
63
+ #include <ATen/ops/_foreach_abs_compositeexplicitautograd_dispatch.h>
64
+ #include <ATen/ops/_foreach_acos_compositeexplicitautograd_dispatch.h>
65
+ #include <ATen/ops/_foreach_add_compositeexplicitautograd_dispatch.h>
66
+ #include <ATen/ops/_foreach_addcdiv_compositeexplicitautograd_dispatch.h>
67
+ #include <ATen/ops/_foreach_addcmul_compositeexplicitautograd_dispatch.h>
68
+ #include <ATen/ops/_foreach_asin_compositeexplicitautograd_dispatch.h>
69
+ #include <ATen/ops/_foreach_atan_compositeexplicitautograd_dispatch.h>
70
+ #include <ATen/ops/_foreach_ceil_compositeexplicitautograd_dispatch.h>
71
+ #include <ATen/ops/_foreach_clamp_max_compositeexplicitautograd_dispatch.h>
72
+ #include <ATen/ops/_foreach_clamp_min_compositeexplicitautograd_dispatch.h>
73
+ #include <ATen/ops/_foreach_copy_compositeexplicitautograd_dispatch.h>
74
+ #include <ATen/ops/_foreach_cos_compositeexplicitautograd_dispatch.h>
75
+ #include <ATen/ops/_foreach_cosh_compositeexplicitautograd_dispatch.h>
76
+ #include <ATen/ops/_foreach_div_compositeexplicitautograd_dispatch.h>
77
+ #include <ATen/ops/_foreach_erf_compositeexplicitautograd_dispatch.h>
78
+ #include <ATen/ops/_foreach_erfc_compositeexplicitautograd_dispatch.h>
79
+ #include <ATen/ops/_foreach_exp_compositeexplicitautograd_dispatch.h>
80
+ #include <ATen/ops/_foreach_expm1_compositeexplicitautograd_dispatch.h>
81
+ #include <ATen/ops/_foreach_floor_compositeexplicitautograd_dispatch.h>
82
+ #include <ATen/ops/_foreach_frac_compositeexplicitautograd_dispatch.h>
83
+ #include <ATen/ops/_foreach_lerp_compositeexplicitautograd_dispatch.h>
84
+ #include <ATen/ops/_foreach_lgamma_compositeexplicitautograd_dispatch.h>
85
+ #include <ATen/ops/_foreach_log_compositeexplicitautograd_dispatch.h>
86
+ #include <ATen/ops/_foreach_log10_compositeexplicitautograd_dispatch.h>
87
+ #include <ATen/ops/_foreach_log1p_compositeexplicitautograd_dispatch.h>
88
+ #include <ATen/ops/_foreach_log2_compositeexplicitautograd_dispatch.h>
89
+ #include <ATen/ops/_foreach_max_compositeexplicitautograd_dispatch.h>
90
+ #include <ATen/ops/_foreach_maximum_compositeexplicitautograd_dispatch.h>
91
+ #include <ATen/ops/_foreach_minimum_compositeexplicitautograd_dispatch.h>
92
+ #include <ATen/ops/_foreach_mul_compositeexplicitautograd_dispatch.h>
93
+ #include <ATen/ops/_foreach_neg_compositeexplicitautograd_dispatch.h>
94
+ #include <ATen/ops/_foreach_norm_compositeexplicitautograd_dispatch.h>
95
+ #include <ATen/ops/_foreach_pow_compositeexplicitautograd_dispatch.h>
96
+ #include <ATen/ops/_foreach_reciprocal_compositeexplicitautograd_dispatch.h>
97
+ #include <ATen/ops/_foreach_round_compositeexplicitautograd_dispatch.h>
98
+ #include <ATen/ops/_foreach_rsqrt_compositeexplicitautograd_dispatch.h>
99
+ #include <ATen/ops/_foreach_sigmoid_compositeexplicitautograd_dispatch.h>
100
+ #include <ATen/ops/_foreach_sign_compositeexplicitautograd_dispatch.h>
101
+ #include <ATen/ops/_foreach_sin_compositeexplicitautograd_dispatch.h>
102
+ #include <ATen/ops/_foreach_sinh_compositeexplicitautograd_dispatch.h>
103
+ #include <ATen/ops/_foreach_sqrt_compositeexplicitautograd_dispatch.h>
104
+ #include <ATen/ops/_foreach_sub_compositeexplicitautograd_dispatch.h>
105
+ #include <ATen/ops/_foreach_tan_compositeexplicitautograd_dispatch.h>
106
+ #include <ATen/ops/_foreach_tanh_compositeexplicitautograd_dispatch.h>
107
+ #include <ATen/ops/_foreach_trunc_compositeexplicitautograd_dispatch.h>
108
+ #include <ATen/ops/_foreach_zero_compositeexplicitautograd_dispatch.h>
109
+ #include <ATen/ops/_functional_assert_scalar_compositeexplicitautograd_dispatch.h>
110
+ #include <ATen/ops/_functional_sym_constrain_range_compositeexplicitautograd_dispatch.h>
111
+ #include <ATen/ops/_functional_sym_constrain_range_for_size_compositeexplicitautograd_dispatch.h>
112
+ #include <ATen/ops/_fused_adagrad_compositeexplicitautograd_dispatch.h>
113
+ #include <ATen/ops/_fused_adam_compositeexplicitautograd_dispatch.h>
114
+ #include <ATen/ops/_fused_adamw_compositeexplicitautograd_dispatch.h>
115
+ #include <ATen/ops/_fused_dropout_compositeexplicitautograd_dispatch.h>
116
+ #include <ATen/ops/_fused_moving_avg_obs_fq_helper_compositeexplicitautograd_dispatch.h>
117
+ #include <ATen/ops/_fused_sgd_compositeexplicitautograd_dispatch.h>
118
+ #include <ATen/ops/_fw_primal_compositeexplicitautograd_dispatch.h>
119
+ #include <ATen/ops/_fw_primal_copy_compositeexplicitautograd_dispatch.h>
120
+ #include <ATen/ops/_grid_sampler_2d_cpu_fallback_compositeexplicitautograd_dispatch.h>
121
+ #include <ATen/ops/_grouped_mm_compositeexplicitautograd_dispatch.h>
122
+ #include <ATen/ops/_has_same_storage_numel_compositeexplicitautograd_dispatch.h>
123
+ #include <ATen/ops/_histogramdd_bin_edges_compositeexplicitautograd_dispatch.h>
124
+ #include <ATen/ops/_histogramdd_from_bin_cts_compositeexplicitautograd_dispatch.h>
125
+ #include <ATen/ops/_histogramdd_from_bin_tensors_compositeexplicitautograd_dispatch.h>
126
+ #include <ATen/ops/_index_put_impl_compositeexplicitautograd_dispatch.h>
127
+ #include <ATen/ops/_indices_copy_compositeexplicitautograd_dispatch.h>
128
+ #include <ATen/ops/_is_all_true_compositeexplicitautograd_dispatch.h>
129
+ #include <ATen/ops/_is_any_true_compositeexplicitautograd_dispatch.h>
130
+ #include <ATen/ops/_lazy_clone_compositeexplicitautograd_dispatch.h>
131
+ #include <ATen/ops/_linalg_check_errors_compositeexplicitautograd_dispatch.h>
132
+ #include <ATen/ops/_lstm_mps_compositeexplicitautograd_dispatch.h>
133
+ #include <ATen/ops/_make_dual_compositeexplicitautograd_dispatch.h>
134
+ #include <ATen/ops/_make_dual_copy_compositeexplicitautograd_dispatch.h>
135
+ #include <ATen/ops/_make_per_channel_quantized_tensor_compositeexplicitautograd_dispatch.h>
136
+ #include <ATen/ops/_make_per_tensor_quantized_tensor_compositeexplicitautograd_dispatch.h>
137
+ #include <ATen/ops/_masked_scale_compositeexplicitautograd_dispatch.h>
138
+ #include <ATen/ops/_masked_softmax_compositeexplicitautograd_dispatch.h>
139
+ #include <ATen/ops/_masked_softmax_backward_compositeexplicitautograd_dispatch.h>
140
+ #include <ATen/ops/_mkldnn_reshape_compositeexplicitautograd_dispatch.h>
141
+ #include <ATen/ops/_mkldnn_transpose_compositeexplicitautograd_dispatch.h>
142
+ #include <ATen/ops/_mps_convolution_compositeexplicitautograd_dispatch.h>
143
+ #include <ATen/ops/_mps_convolution_transpose_compositeexplicitautograd_dispatch.h>
144
+ #include <ATen/ops/_native_batch_norm_legit_compositeexplicitautograd_dispatch.h>
145
+ #include <ATen/ops/_native_batch_norm_legit_no_training_compositeexplicitautograd_dispatch.h>
146
+ #include <ATen/ops/_native_multi_head_attention_compositeexplicitautograd_dispatch.h>
147
+ #include <ATen/ops/_neg_view_compositeexplicitautograd_dispatch.h>
148
+ #include <ATen/ops/_neg_view_copy_compositeexplicitautograd_dispatch.h>
149
+ #include <ATen/ops/_nested_from_padded_compositeexplicitautograd_dispatch.h>
150
+ #include <ATen/ops/_nested_from_padded_and_nested_example_compositeexplicitautograd_dispatch.h>
151
+ #include <ATen/ops/_nested_get_values_copy_compositeexplicitautograd_dispatch.h>
152
+ #include <ATen/ops/_nested_tensor_from_mask_compositeexplicitautograd_dispatch.h>
153
+ #include <ATen/ops/_nested_tensor_from_tensor_list_compositeexplicitautograd_dispatch.h>
154
+ #include <ATen/ops/_nested_tensor_size_compositeexplicitautograd_dispatch.h>
155
+ #include <ATen/ops/_nested_tensor_storage_offsets_compositeexplicitautograd_dispatch.h>
156
+ #include <ATen/ops/_nested_tensor_strides_compositeexplicitautograd_dispatch.h>
157
+ #include <ATen/ops/_nested_view_from_buffer_copy_compositeexplicitautograd_dispatch.h>
158
+ #include <ATen/ops/_nested_view_from_jagged_copy_compositeexplicitautograd_dispatch.h>
159
+ #include <ATen/ops/_new_zeros_with_same_feature_meta_compositeexplicitautograd_dispatch.h>
160
+ #include <ATen/ops/_nnpack_spatial_convolution_compositeexplicitautograd_dispatch.h>
161
+ #include <ATen/ops/_pack_padded_sequence_compositeexplicitautograd_dispatch.h>
162
+ #include <ATen/ops/_pdist_backward_compositeexplicitautograd_dispatch.h>
163
+ #include <ATen/ops/_pdist_forward_compositeexplicitautograd_dispatch.h>
164
+ #include <ATen/ops/_pin_memory_compositeexplicitautograd_dispatch.h>
165
+ #include <ATen/ops/_print_compositeexplicitautograd_dispatch.h>
166
+ #include <ATen/ops/_reshape_alias_copy_compositeexplicitautograd_dispatch.h>
167
+ #include <ATen/ops/_reshape_copy_compositeexplicitautograd_dispatch.h>
168
+ #include <ATen/ops/_resize_output_compositeexplicitautograd_dispatch.h>
169
+ #include <ATen/ops/_safe_softmax_compositeexplicitautograd_dispatch.h>
170
+ #include <ATen/ops/_sample_dirichlet_compositeexplicitautograd_dispatch.h>
171
+ #include <ATen/ops/_scaled_dot_product_fused_attention_overrideable_compositeexplicitautograd_dispatch.h>
172
+ #include <ATen/ops/_scaled_dot_product_fused_attention_overrideable_backward_compositeexplicitautograd_dispatch.h>
173
+ #include <ATen/ops/_segment_reduce_backward_compositeexplicitautograd_dispatch.h>
174
+ #include <ATen/ops/_slow_conv2d_backward_compositeexplicitautograd_dispatch.h>
175
+ #include <ATen/ops/_sparse_addmm_compositeexplicitautograd_dispatch.h>
176
+ #include <ATen/ops/_sparse_broadcast_to_copy_compositeexplicitautograd_dispatch.h>
177
+ #include <ATen/ops/_sparse_compressed_tensor_with_dims_compositeexplicitautograd_dispatch.h>
178
+ #include <ATen/ops/_sparse_coo_tensor_with_dims_compositeexplicitautograd_dispatch.h>
179
+ #include <ATen/ops/_sparse_coo_tensor_with_dims_and_tensors_compositeexplicitautograd_dispatch.h>
180
+ #include <ATen/ops/_sparse_csr_prod_compositeexplicitautograd_dispatch.h>
181
+ #include <ATen/ops/_sparse_csr_sum_compositeexplicitautograd_dispatch.h>
182
+ #include <ATen/ops/_sparse_log_softmax_compositeexplicitautograd_dispatch.h>
183
+ #include <ATen/ops/_sparse_log_softmax_backward_data_compositeexplicitautograd_dispatch.h>
184
+ #include <ATen/ops/_sparse_mask_projection_compositeexplicitautograd_dispatch.h>
185
+ #include <ATen/ops/_sparse_softmax_compositeexplicitautograd_dispatch.h>
186
+ #include <ATen/ops/_sparse_softmax_backward_data_compositeexplicitautograd_dispatch.h>
187
+ #include <ATen/ops/_sparse_sparse_matmul_compositeexplicitautograd_dispatch.h>
188
+ #include <ATen/ops/_sparse_sum_compositeexplicitautograd_dispatch.h>
189
+ #include <ATen/ops/_sparse_sum_backward_compositeexplicitautograd_dispatch.h>
190
+ #include <ATen/ops/_spdiags_compositeexplicitautograd_dispatch.h>
191
+ #include <ATen/ops/_stack_compositeexplicitautograd_dispatch.h>
192
+ #include <ATen/ops/_standard_gamma_compositeexplicitautograd_dispatch.h>
193
+ #include <ATen/ops/_standard_gamma_grad_compositeexplicitautograd_dispatch.h>
194
+ #include <ATen/ops/_test_autograd_multiple_dispatch_compositeexplicitautograd_dispatch.h>
195
+ #include <ATen/ops/_test_autograd_multiple_dispatch_view_compositeexplicitautograd_dispatch.h>
196
+ #include <ATen/ops/_test_autograd_multiple_dispatch_view_copy_compositeexplicitautograd_dispatch.h>
197
+ #include <ATen/ops/_test_functorch_fallback_compositeexplicitautograd_dispatch.h>
198
+ #include <ATen/ops/_test_optional_filled_intlist_compositeexplicitautograd_dispatch.h>
199
+ #include <ATen/ops/_test_optional_floatlist_compositeexplicitautograd_dispatch.h>
200
+ #include <ATen/ops/_test_optional_intlist_compositeexplicitautograd_dispatch.h>
201
+ #include <ATen/ops/_test_parallel_materialize_compositeexplicitautograd_dispatch.h>
202
+ #include <ATen/ops/_test_warn_in_autograd_compositeexplicitautograd_dispatch.h>
203
+ #include <ATen/ops/_thnn_fused_gru_cell_compositeexplicitautograd_dispatch.h>
204
+ #include <ATen/ops/_thnn_fused_gru_cell_backward_compositeexplicitautograd_dispatch.h>
205
+ #include <ATen/ops/_thnn_fused_lstm_cell_compositeexplicitautograd_dispatch.h>
206
+ #include <ATen/ops/_thnn_fused_lstm_cell_backward_impl_compositeexplicitautograd_dispatch.h>
207
+ #include <ATen/ops/_to_copy_compositeexplicitautograd_dispatch.h>
208
+ #include <ATen/ops/_to_dense_compositeexplicitautograd_dispatch.h>
209
+ #include <ATen/ops/_to_sparse_compositeexplicitautograd_dispatch.h>
210
+ #include <ATen/ops/_to_sparse_bsc_compositeexplicitautograd_dispatch.h>
211
+ #include <ATen/ops/_to_sparse_bsr_compositeexplicitautograd_dispatch.h>
212
+ #include <ATen/ops/_to_sparse_csc_compositeexplicitautograd_dispatch.h>
213
+ #include <ATen/ops/_to_sparse_csr_compositeexplicitautograd_dispatch.h>
214
+ #include <ATen/ops/_transform_bias_rescale_qkv_compositeexplicitautograd_dispatch.h>
215
+ #include <ATen/ops/_transformer_encoder_layer_fwd_compositeexplicitautograd_dispatch.h>
216
+ #include <ATen/ops/_trilinear_compositeexplicitautograd_dispatch.h>
217
+ #include <ATen/ops/_triton_multi_head_attention_compositeexplicitautograd_dispatch.h>
218
+ #include <ATen/ops/_triton_scaled_dot_attention_compositeexplicitautograd_dispatch.h>
219
+ #include <ATen/ops/_unique_compositeexplicitautograd_dispatch.h>
220
+ #include <ATen/ops/_unique2_compositeexplicitautograd_dispatch.h>
221
+ #include <ATen/ops/_unsafe_index_compositeexplicitautograd_dispatch.h>
222
+ #include <ATen/ops/_unsafe_index_put_compositeexplicitautograd_dispatch.h>
223
+ #include <ATen/ops/_unsafe_masked_index_compositeexplicitautograd_dispatch.h>
224
+ #include <ATen/ops/_unsafe_masked_index_put_accumulate_compositeexplicitautograd_dispatch.h>
225
+ #include <ATen/ops/_unsafe_view_compositeexplicitautograd_dispatch.h>
226
+ #include <ATen/ops/_values_copy_compositeexplicitautograd_dispatch.h>
227
+ #include <ATen/ops/_weight_norm_interface_compositeexplicitautograd_dispatch.h>
228
+ #include <ATen/ops/_weight_norm_interface_backward_compositeexplicitautograd_dispatch.h>
229
+ #include <ATen/ops/abs_compositeexplicitautograd_dispatch.h>
230
+ #include <ATen/ops/adaptive_avg_pool1d_compositeexplicitautograd_dispatch.h>
231
+ #include <ATen/ops/add_compositeexplicitautograd_dispatch.h>
232
+ #include <ATen/ops/addr_compositeexplicitautograd_dispatch.h>
233
+ #include <ATen/ops/affine_grid_generator_compositeexplicitautograd_dispatch.h>
234
+ #include <ATen/ops/alias_compositeexplicitautograd_dispatch.h>
235
+ #include <ATen/ops/alias_copy_compositeexplicitautograd_dispatch.h>
236
+ #include <ATen/ops/all_compositeexplicitautograd_dispatch.h>
237
+ #include <ATen/ops/allclose_compositeexplicitautograd_dispatch.h>
238
+ #include <ATen/ops/any_compositeexplicitautograd_dispatch.h>
239
+ #include <ATen/ops/arange_compositeexplicitautograd_dispatch.h>
240
+ #include <ATen/ops/as_strided_copy_compositeexplicitautograd_dispatch.h>
241
+ #include <ATen/ops/as_strided_scatter_compositeexplicitautograd_dispatch.h>
242
+ #include <ATen/ops/avg_pool1d_compositeexplicitautograd_dispatch.h>
243
+ #include <ATen/ops/bartlett_window_compositeexplicitautograd_dispatch.h>
244
+ #include <ATen/ops/batch_norm_backward_elemt_compositeexplicitautograd_dispatch.h>
245
+ #include <ATen/ops/batch_norm_backward_reduce_compositeexplicitautograd_dispatch.h>
246
+ #include <ATen/ops/batch_norm_gather_stats_compositeexplicitautograd_dispatch.h>
247
+ #include <ATen/ops/batch_norm_gather_stats_with_counts_compositeexplicitautograd_dispatch.h>
248
+ #include <ATen/ops/batch_norm_stats_compositeexplicitautograd_dispatch.h>
249
+ #include <ATen/ops/batch_norm_update_stats_compositeexplicitautograd_dispatch.h>
250
+ #include <ATen/ops/bernoulli_compositeexplicitautograd_dispatch.h>
251
+ #include <ATen/ops/binary_cross_entropy_with_logits_compositeexplicitautograd_dispatch.h>
252
+ #include <ATen/ops/bincount_compositeexplicitautograd_dispatch.h>
253
+ #include <ATen/ops/binomial_compositeexplicitautograd_dispatch.h>
254
+ #include <ATen/ops/bitwise_and_compositeexplicitautograd_dispatch.h>
255
+ #include <ATen/ops/bitwise_left_shift_compositeexplicitautograd_dispatch.h>
256
+ #include <ATen/ops/bitwise_or_compositeexplicitautograd_dispatch.h>
257
+ #include <ATen/ops/bitwise_right_shift_compositeexplicitautograd_dispatch.h>
258
+ #include <ATen/ops/bitwise_xor_compositeexplicitautograd_dispatch.h>
259
+ #include <ATen/ops/blackman_window_compositeexplicitautograd_dispatch.h>
260
+ #include <ATen/ops/block_diag_compositeexplicitautograd_dispatch.h>
261
+ #include <ATen/ops/bucketize_compositeexplicitautograd_dispatch.h>
262
+ #include <ATen/ops/cauchy_compositeexplicitautograd_dispatch.h>
263
+ #include <ATen/ops/ccol_indices_compositeexplicitautograd_dispatch.h>
264
+ #include <ATen/ops/ccol_indices_copy_compositeexplicitautograd_dispatch.h>
265
+ #include <ATen/ops/celu_compositeexplicitautograd_dispatch.h>
266
+ #include <ATen/ops/channel_shuffle_compositeexplicitautograd_dispatch.h>
267
+ #include <ATen/ops/cholesky_solve_compositeexplicitautograd_dispatch.h>
268
+ #include <ATen/ops/clone_compositeexplicitautograd_dispatch.h>
269
+ #include <ATen/ops/col_indices_compositeexplicitautograd_dispatch.h>
270
+ #include <ATen/ops/col_indices_copy_compositeexplicitautograd_dispatch.h>
271
+ #include <ATen/ops/complex_compositeexplicitautograd_dispatch.h>
272
+ #include <ATen/ops/conj_physical_compositeexplicitautograd_dispatch.h>
273
+ #include <ATen/ops/constant_pad_nd_compositeexplicitautograd_dispatch.h>
274
+ #include <ATen/ops/conv_depthwise3d_compositeexplicitautograd_dispatch.h>
275
+ #include <ATen/ops/conv_tbc_compositeexplicitautograd_dispatch.h>
276
+ #include <ATen/ops/convolution_compositeexplicitautograd_dispatch.h>
277
+ #include <ATen/ops/convolution_backward_compositeexplicitautograd_dispatch.h>
278
+ #include <ATen/ops/convolution_backward_overrideable_compositeexplicitautograd_dispatch.h>
279
+ #include <ATen/ops/convolution_overrideable_compositeexplicitautograd_dispatch.h>
280
+ #include <ATen/ops/copy_compositeexplicitautograd_dispatch.h>
281
+ #include <ATen/ops/copy_sparse_to_sparse_compositeexplicitautograd_dispatch.h>
282
+ #include <ATen/ops/copysign_compositeexplicitautograd_dispatch.h>
283
+ #include <ATen/ops/count_nonzero_compositeexplicitautograd_dispatch.h>
284
+ #include <ATen/ops/crow_indices_compositeexplicitautograd_dispatch.h>
285
+ #include <ATen/ops/crow_indices_copy_compositeexplicitautograd_dispatch.h>
286
+ #include <ATen/ops/cudnn_affine_grid_generator_compositeexplicitautograd_dispatch.h>
287
+ #include <ATen/ops/cudnn_affine_grid_generator_backward_compositeexplicitautograd_dispatch.h>
288
+ #include <ATen/ops/cudnn_batch_norm_backward_compositeexplicitautograd_dispatch.h>
289
+ #include <ATen/ops/cudnn_convolution_add_relu_compositeexplicitautograd_dispatch.h>
290
+ #include <ATen/ops/cudnn_convolution_relu_compositeexplicitautograd_dispatch.h>
291
+ #include <ATen/ops/cudnn_convolution_transpose_compositeexplicitautograd_dispatch.h>
292
+ #include <ATen/ops/cudnn_grid_sampler_compositeexplicitautograd_dispatch.h>
293
+ #include <ATen/ops/cudnn_grid_sampler_backward_compositeexplicitautograd_dispatch.h>
294
+ #include <ATen/ops/cummax_compositeexplicitautograd_dispatch.h>
295
+ #include <ATen/ops/cummin_compositeexplicitautograd_dispatch.h>
296
+ #include <ATen/ops/deg2rad_compositeexplicitautograd_dispatch.h>
297
+ #include <ATen/ops/dense_dim_compositeexplicitautograd_dispatch.h>
298
+ #include <ATen/ops/dequantize_compositeexplicitautograd_dispatch.h>
299
+ #include <ATen/ops/detach_compositeexplicitautograd_dispatch.h>
300
+ #include <ATen/ops/detach_copy_compositeexplicitautograd_dispatch.h>
301
+ #include <ATen/ops/diag_embed_compositeexplicitautograd_dispatch.h>
302
+ #include <ATen/ops/diagonal_compositeexplicitautograd_dispatch.h>
303
+ #include <ATen/ops/diagonal_backward_compositeexplicitautograd_dispatch.h>
304
+ #include <ATen/ops/diagonal_copy_compositeexplicitautograd_dispatch.h>
305
+ #include <ATen/ops/diagonal_scatter_compositeexplicitautograd_dispatch.h>
306
+ #include <ATen/ops/dist_compositeexplicitautograd_dispatch.h>
307
+ #include <ATen/ops/div_compositeexplicitautograd_dispatch.h>
308
+ #include <ATen/ops/dot_compositeexplicitautograd_dispatch.h>
309
+ #include <ATen/ops/embedding_compositeexplicitautograd_dispatch.h>
310
+ #include <ATen/ops/embedding_dense_backward_compositeexplicitautograd_dispatch.h>
311
+ #include <ATen/ops/embedding_renorm_compositeexplicitautograd_dispatch.h>
312
+ #include <ATen/ops/empty_compositeexplicitautograd_dispatch.h>
313
+ #include <ATen/ops/empty_like_compositeexplicitautograd_dispatch.h>
314
+ #include <ATen/ops/empty_permuted_compositeexplicitautograd_dispatch.h>
315
+ #include <ATen/ops/empty_quantized_compositeexplicitautograd_dispatch.h>
316
+ #include <ATen/ops/empty_strided_compositeexplicitautograd_dispatch.h>
317
+ #include <ATen/ops/expand_compositeexplicitautograd_dispatch.h>
318
+ #include <ATen/ops/expand_copy_compositeexplicitautograd_dispatch.h>
319
+ #include <ATen/ops/exponential_compositeexplicitautograd_dispatch.h>
320
+ #include <ATen/ops/eye_compositeexplicitautograd_dispatch.h>
321
+ #include <ATen/ops/fake_quantize_per_channel_affine_cachemask_compositeexplicitautograd_dispatch.h>
322
+ #include <ATen/ops/fake_quantize_per_tensor_affine_cachemask_compositeexplicitautograd_dispatch.h>
323
+ #include <ATen/ops/fft_fftfreq_compositeexplicitautograd_dispatch.h>
324
+ #include <ATen/ops/fft_rfftfreq_compositeexplicitautograd_dispatch.h>
325
+ #include <ATen/ops/fill_compositeexplicitautograd_dispatch.h>
326
+ #include <ATen/ops/flip_compositeexplicitautograd_dispatch.h>
327
+ #include <ATen/ops/floor_divide_compositeexplicitautograd_dispatch.h>
328
+ #include <ATen/ops/fmod_compositeexplicitautograd_dispatch.h>
329
+ #include <ATen/ops/frexp_compositeexplicitautograd_dispatch.h>
330
+ #include <ATen/ops/from_file_compositeexplicitautograd_dispatch.h>
331
+ #include <ATen/ops/full_compositeexplicitautograd_dispatch.h>
332
+ #include <ATen/ops/full_like_compositeexplicitautograd_dispatch.h>
333
+ #include <ATen/ops/geometric_compositeexplicitautograd_dispatch.h>
334
+ #include <ATen/ops/glu_backward_jvp_compositeexplicitautograd_dispatch.h>
335
+ #include <ATen/ops/glu_jvp_compositeexplicitautograd_dispatch.h>
336
+ #include <ATen/ops/grid_sampler_2d_compositeexplicitautograd_dispatch.h>
337
+ #include <ATen/ops/grid_sampler_2d_backward_compositeexplicitautograd_dispatch.h>
338
+ #include <ATen/ops/grid_sampler_3d_compositeexplicitautograd_dispatch.h>
339
+ #include <ATen/ops/grid_sampler_3d_backward_compositeexplicitautograd_dispatch.h>
340
+ #include <ATen/ops/hamming_window_compositeexplicitautograd_dispatch.h>
341
+ #include <ATen/ops/hann_window_compositeexplicitautograd_dispatch.h>
342
+ #include <ATen/ops/hardswish_backward_compositeexplicitautograd_dispatch.h>
343
+ #include <ATen/ops/huber_loss_backward_compositeexplicitautograd_dispatch.h>
344
+ #include <ATen/ops/index_fill_compositeexplicitautograd_dispatch.h>
345
+ #include <ATen/ops/index_put_compositeexplicitautograd_dispatch.h>
346
+ #include <ATen/ops/indices_compositeexplicitautograd_dispatch.h>
347
+ #include <ATen/ops/indices_copy_compositeexplicitautograd_dispatch.h>
348
+ #include <ATen/ops/int_repr_compositeexplicitautograd_dispatch.h>
349
+ #include <ATen/ops/is_coalesced_compositeexplicitautograd_dispatch.h>
350
+ #include <ATen/ops/is_pinned_compositeexplicitautograd_dispatch.h>
351
+ #include <ATen/ops/is_same_size_compositeexplicitautograd_dispatch.h>
352
+ #include <ATen/ops/isinf_compositeexplicitautograd_dispatch.h>
353
+ #include <ATen/ops/isnan_compositeexplicitautograd_dispatch.h>
354
+ #include <ATen/ops/kaiser_window_compositeexplicitautograd_dispatch.h>
355
+ #include <ATen/ops/kthvalue_compositeexplicitautograd_dispatch.h>
356
+ #include <ATen/ops/lift_compositeexplicitautograd_dispatch.h>
357
+ #include <ATen/ops/lift_fresh_compositeexplicitautograd_dispatch.h>
358
+ #include <ATen/ops/lift_fresh_copy_compositeexplicitautograd_dispatch.h>
359
+ #include <ATen/ops/linalg_lstsq_compositeexplicitautograd_dispatch.h>
360
+ #include <ATen/ops/linalg_matrix_exp_compositeexplicitautograd_dispatch.h>
361
+ #include <ATen/ops/linalg_pinv_compositeexplicitautograd_dispatch.h>
362
+ #include <ATen/ops/linear_compositeexplicitautograd_dispatch.h>
363
+ #include <ATen/ops/linear_backward_compositeexplicitautograd_dispatch.h>
364
+ #include <ATen/ops/linspace_compositeexplicitautograd_dispatch.h>
365
+ #include <ATen/ops/log_normal_compositeexplicitautograd_dispatch.h>
366
+ #include <ATen/ops/log_softmax_compositeexplicitautograd_dispatch.h>
367
+ #include <ATen/ops/logcumsumexp_compositeexplicitautograd_dispatch.h>
368
+ #include <ATen/ops/logical_and_compositeexplicitautograd_dispatch.h>
369
+ #include <ATen/ops/logical_not_compositeexplicitautograd_dispatch.h>
370
+ #include <ATen/ops/logical_or_compositeexplicitautograd_dispatch.h>
371
+ #include <ATen/ops/logical_xor_compositeexplicitautograd_dispatch.h>
372
+ #include <ATen/ops/logspace_compositeexplicitautograd_dispatch.h>
373
+ #include <ATen/ops/logsumexp_compositeexplicitautograd_dispatch.h>
374
+ #include <ATen/ops/lshift_compositeexplicitautograd_dispatch.h>
375
+ #include <ATen/ops/lstm_mps_backward_compositeexplicitautograd_dispatch.h>
376
+ #include <ATen/ops/masked_fill_compositeexplicitautograd_dispatch.h>
377
+ #include <ATen/ops/masked_scatter_compositeexplicitautograd_dispatch.h>
378
+ #include <ATen/ops/masked_scatter_backward_compositeexplicitautograd_dispatch.h>
379
+ #include <ATen/ops/matmul_backward_compositeexplicitautograd_dispatch.h>
380
+ #include <ATen/ops/max_pool2d_backward_compositeexplicitautograd_dispatch.h>
381
+ #include <ATen/ops/mean_compositeexplicitautograd_dispatch.h>
382
+ #include <ATen/ops/median_compositeexplicitautograd_dispatch.h>
383
+ #include <ATen/ops/miopen_batch_norm_compositeexplicitautograd_dispatch.h>
384
+ #include <ATen/ops/miopen_batch_norm_backward_compositeexplicitautograd_dispatch.h>
385
+ #include <ATen/ops/miopen_convolution_compositeexplicitautograd_dispatch.h>
386
+ #include <ATen/ops/miopen_convolution_transpose_compositeexplicitautograd_dispatch.h>
387
+ #include <ATen/ops/miopen_depthwise_convolution_compositeexplicitautograd_dispatch.h>
388
+ #include <ATen/ops/miopen_rnn_compositeexplicitautograd_dispatch.h>
389
+ #include <ATen/ops/miopen_rnn_backward_compositeexplicitautograd_dispatch.h>
390
+ #include <ATen/ops/mkldnn_adaptive_avg_pool2d_backward_compositeexplicitautograd_dispatch.h>
391
+ #include <ATen/ops/mkldnn_convolution_compositeexplicitautograd_dispatch.h>
392
+ #include <ATen/ops/mkldnn_linear_compositeexplicitautograd_dispatch.h>
393
+ #include <ATen/ops/mkldnn_linear_backward_compositeexplicitautograd_dispatch.h>
394
+ #include <ATen/ops/mkldnn_linear_backward_input_compositeexplicitautograd_dispatch.h>
395
+ #include <ATen/ops/mkldnn_linear_backward_weights_compositeexplicitautograd_dispatch.h>
396
+ #include <ATen/ops/mkldnn_max_pool2d_compositeexplicitautograd_dispatch.h>
397
+ #include <ATen/ops/mkldnn_max_pool2d_backward_compositeexplicitautograd_dispatch.h>
398
+ #include <ATen/ops/mkldnn_max_pool3d_compositeexplicitautograd_dispatch.h>
399
+ #include <ATen/ops/mkldnn_max_pool3d_backward_compositeexplicitautograd_dispatch.h>
400
+ #include <ATen/ops/mkldnn_reorder_conv2d_weight_compositeexplicitautograd_dispatch.h>
401
+ #include <ATen/ops/mkldnn_reorder_conv3d_weight_compositeexplicitautograd_dispatch.h>
402
+ #include <ATen/ops/mkldnn_rnn_layer_compositeexplicitautograd_dispatch.h>
403
+ #include <ATen/ops/mkldnn_rnn_layer_backward_compositeexplicitautograd_dispatch.h>
404
+ #include <ATen/ops/mode_compositeexplicitautograd_dispatch.h>
405
+ #include <ATen/ops/mps_convolution_backward_compositeexplicitautograd_dispatch.h>
406
+ #include <ATen/ops/mps_convolution_transpose_backward_compositeexplicitautograd_dispatch.h>
407
+ #include <ATen/ops/mul_compositeexplicitautograd_dispatch.h>
408
+ #include <ATen/ops/mv_compositeexplicitautograd_dispatch.h>
409
+ #include <ATen/ops/mvlgamma_compositeexplicitautograd_dispatch.h>
410
+ #include <ATen/ops/nan_to_num_compositeexplicitautograd_dispatch.h>
411
+ #include <ATen/ops/nanmedian_compositeexplicitautograd_dispatch.h>
412
+ #include <ATen/ops/native_batch_norm_backward_compositeexplicitautograd_dispatch.h>
413
+ #include <ATen/ops/native_dropout_compositeexplicitautograd_dispatch.h>
414
+ #include <ATen/ops/native_dropout_backward_compositeexplicitautograd_dispatch.h>
415
+ #include <ATen/ops/native_group_norm_compositeexplicitautograd_dispatch.h>
416
+ #include <ATen/ops/native_group_norm_backward_compositeexplicitautograd_dispatch.h>
417
+ #include <ATen/ops/native_layer_norm_compositeexplicitautograd_dispatch.h>
418
+ #include <ATen/ops/native_layer_norm_backward_compositeexplicitautograd_dispatch.h>
419
+ #include <ATen/ops/native_norm_compositeexplicitautograd_dispatch.h>
420
+ #include <ATen/ops/new_empty_compositeexplicitautograd_dispatch.h>
421
+ #include <ATen/ops/new_empty_strided_compositeexplicitautograd_dispatch.h>
422
+ #include <ATen/ops/new_full_compositeexplicitautograd_dispatch.h>
423
+ #include <ATen/ops/new_ones_compositeexplicitautograd_dispatch.h>
424
+ #include <ATen/ops/new_zeros_compositeexplicitautograd_dispatch.h>
425
+ #include <ATen/ops/norm_compositeexplicitautograd_dispatch.h>
426
+ #include <ATen/ops/normal_compositeexplicitautograd_dispatch.h>
427
+ #include <ATen/ops/ones_compositeexplicitautograd_dispatch.h>
428
+ #include <ATen/ops/ones_like_compositeexplicitautograd_dispatch.h>
429
+ #include <ATen/ops/permute_compositeexplicitautograd_dispatch.h>
430
+ #include <ATen/ops/permute_copy_compositeexplicitautograd_dispatch.h>
431
+ #include <ATen/ops/pixel_shuffle_compositeexplicitautograd_dispatch.h>
432
+ #include <ATen/ops/pixel_unshuffle_compositeexplicitautograd_dispatch.h>
433
+ #include <ATen/ops/poisson_compositeexplicitautograd_dispatch.h>
434
+ #include <ATen/ops/polar_compositeexplicitautograd_dispatch.h>
435
+ #include <ATen/ops/polygamma_compositeexplicitautograd_dispatch.h>
436
+ #include <ATen/ops/prod_compositeexplicitautograd_dispatch.h>
437
+ #include <ATen/ops/put_compositeexplicitautograd_dispatch.h>
438
+ #include <ATen/ops/q_per_channel_scales_compositeexplicitautograd_dispatch.h>
439
+ #include <ATen/ops/q_per_channel_zero_points_compositeexplicitautograd_dispatch.h>
440
+ #include <ATen/ops/quantize_per_channel_compositeexplicitautograd_dispatch.h>
441
+ #include <ATen/ops/quantize_per_tensor_compositeexplicitautograd_dispatch.h>
442
+ #include <ATen/ops/quantize_per_tensor_dynamic_compositeexplicitautograd_dispatch.h>
443
+ #include <ATen/ops/quantized_batch_norm_compositeexplicitautograd_dispatch.h>
444
+ #include <ATen/ops/quantized_max_pool1d_compositeexplicitautograd_dispatch.h>
445
+ #include <ATen/ops/quantized_max_pool2d_compositeexplicitautograd_dispatch.h>
446
+ #include <ATen/ops/quantized_max_pool3d_compositeexplicitautograd_dispatch.h>
447
+ #include <ATen/ops/rad2deg_compositeexplicitautograd_dispatch.h>
448
+ #include <ATen/ops/rand_compositeexplicitautograd_dispatch.h>
449
+ #include <ATen/ops/rand_like_compositeexplicitautograd_dispatch.h>
450
+ #include <ATen/ops/randint_compositeexplicitautograd_dispatch.h>
451
+ #include <ATen/ops/randint_like_compositeexplicitautograd_dispatch.h>
452
+ #include <ATen/ops/randn_compositeexplicitautograd_dispatch.h>
453
+ #include <ATen/ops/randn_like_compositeexplicitautograd_dispatch.h>
454
+ #include <ATen/ops/random_compositeexplicitautograd_dispatch.h>
455
+ #include <ATen/ops/randperm_compositeexplicitautograd_dispatch.h>
456
+ #include <ATen/ops/range_compositeexplicitautograd_dispatch.h>
457
+ #include <ATen/ops/relu_compositeexplicitautograd_dispatch.h>
458
+ #include <ATen/ops/remainder_compositeexplicitautograd_dispatch.h>
459
+ #include <ATen/ops/repeat_compositeexplicitautograd_dispatch.h>
460
+ #include <ATen/ops/repeat_interleave_compositeexplicitautograd_dispatch.h>
461
+ #include <ATen/ops/resize_compositeexplicitautograd_dispatch.h>
462
+ #include <ATen/ops/resize_as_compositeexplicitautograd_dispatch.h>
463
+ #include <ATen/ops/resize_as_sparse_compositeexplicitautograd_dispatch.h>
464
+ #include <ATen/ops/roll_compositeexplicitautograd_dispatch.h>
465
+ #include <ATen/ops/rot90_compositeexplicitautograd_dispatch.h>
466
+ #include <ATen/ops/row_indices_compositeexplicitautograd_dispatch.h>
467
+ #include <ATen/ops/row_indices_copy_compositeexplicitautograd_dispatch.h>
468
+ #include <ATen/ops/rrelu_with_noise_compositeexplicitautograd_dispatch.h>
469
+ #include <ATen/ops/rrelu_with_noise_backward_compositeexplicitautograd_dispatch.h>
470
+ #include <ATen/ops/rshift_compositeexplicitautograd_dispatch.h>
471
+ #include <ATen/ops/rsub_compositeexplicitautograd_dispatch.h>
472
+ #include <ATen/ops/scalar_tensor_compositeexplicitautograd_dispatch.h>
473
+ #include <ATen/ops/segment_reduce_compositeexplicitautograd_dispatch.h>
474
+ #include <ATen/ops/select_compositeexplicitautograd_dispatch.h>
475
+ #include <ATen/ops/select_backward_compositeexplicitautograd_dispatch.h>
476
+ #include <ATen/ops/select_copy_compositeexplicitautograd_dispatch.h>
477
+ #include <ATen/ops/select_scatter_compositeexplicitautograd_dispatch.h>
478
+ #include <ATen/ops/set_compositeexplicitautograd_dispatch.h>
479
+ #include <ATen/ops/slice_compositeexplicitautograd_dispatch.h>
480
+ #include <ATen/ops/slice_backward_compositeexplicitautograd_dispatch.h>
481
+ #include <ATen/ops/slice_copy_compositeexplicitautograd_dispatch.h>
482
+ #include <ATen/ops/slice_inverse_compositeexplicitautograd_dispatch.h>
483
+ #include <ATen/ops/slice_scatter_compositeexplicitautograd_dispatch.h>
484
+ #include <ATen/ops/slow_conv_dilated2d_compositeexplicitautograd_dispatch.h>
485
+ #include <ATen/ops/slow_conv_dilated3d_compositeexplicitautograd_dispatch.h>
486
+ #include <ATen/ops/smooth_l1_loss_backward_compositeexplicitautograd_dispatch.h>
487
+ #include <ATen/ops/soft_margin_loss_compositeexplicitautograd_dispatch.h>
488
+ #include <ATen/ops/soft_margin_loss_backward_compositeexplicitautograd_dispatch.h>
489
+ #include <ATen/ops/softmax_compositeexplicitautograd_dispatch.h>
490
+ #include <ATen/ops/sort_compositeexplicitautograd_dispatch.h>
491
+ #include <ATen/ops/sparse_compressed_tensor_compositeexplicitautograd_dispatch.h>
492
+ #include <ATen/ops/sparse_coo_tensor_compositeexplicitautograd_dispatch.h>
493
+ #include <ATen/ops/sparse_dim_compositeexplicitautograd_dispatch.h>
494
+ #include <ATen/ops/sparse_mask_compositeexplicitautograd_dispatch.h>
495
+ #include <ATen/ops/sparse_resize_compositeexplicitautograd_dispatch.h>
496
+ #include <ATen/ops/sparse_resize_and_clear_compositeexplicitautograd_dispatch.h>
497
+ #include <ATen/ops/special_chebyshev_polynomial_t_compositeexplicitautograd_dispatch.h>
498
+ #include <ATen/ops/special_chebyshev_polynomial_u_compositeexplicitautograd_dispatch.h>
499
+ #include <ATen/ops/special_chebyshev_polynomial_v_compositeexplicitautograd_dispatch.h>
500
+ #include <ATen/ops/special_chebyshev_polynomial_w_compositeexplicitautograd_dispatch.h>
501
+ #include <ATen/ops/special_hermite_polynomial_h_compositeexplicitautograd_dispatch.h>
502
+ #include <ATen/ops/special_hermite_polynomial_he_compositeexplicitautograd_dispatch.h>
503
+ #include <ATen/ops/special_laguerre_polynomial_l_compositeexplicitautograd_dispatch.h>
504
+ #include <ATen/ops/special_legendre_polynomial_p_compositeexplicitautograd_dispatch.h>
505
+ #include <ATen/ops/special_shifted_chebyshev_polynomial_t_compositeexplicitautograd_dispatch.h>
506
+ #include <ATen/ops/special_shifted_chebyshev_polynomial_u_compositeexplicitautograd_dispatch.h>
507
+ #include <ATen/ops/special_shifted_chebyshev_polynomial_v_compositeexplicitautograd_dispatch.h>
508
+ #include <ATen/ops/special_shifted_chebyshev_polynomial_w_compositeexplicitautograd_dispatch.h>
509
+ #include <ATen/ops/special_xlog1py_compositeexplicitautograd_dispatch.h>
510
+ #include <ATen/ops/special_zeta_compositeexplicitautograd_dispatch.h>
511
+ #include <ATen/ops/split_compositeexplicitautograd_dispatch.h>
512
+ #include <ATen/ops/split_copy_compositeexplicitautograd_dispatch.h>
513
+ #include <ATen/ops/split_with_sizes_compositeexplicitautograd_dispatch.h>
514
+ #include <ATen/ops/split_with_sizes_copy_compositeexplicitautograd_dispatch.h>
515
+ #include <ATen/ops/squeeze_compositeexplicitautograd_dispatch.h>
516
+ #include <ATen/ops/squeeze_copy_compositeexplicitautograd_dispatch.h>
517
+ #include <ATen/ops/stack_compositeexplicitautograd_dispatch.h>
518
+ #include <ATen/ops/std_mean_compositeexplicitautograd_dispatch.h>
519
+ #include <ATen/ops/sub_compositeexplicitautograd_dispatch.h>
520
+ #include <ATen/ops/sum_compositeexplicitautograd_dispatch.h>
521
+ #include <ATen/ops/sym_constrain_range_compositeexplicitautograd_dispatch.h>
522
+ #include <ATen/ops/sym_constrain_range_for_size_compositeexplicitautograd_dispatch.h>
523
+ #include <ATen/ops/t_compositeexplicitautograd_dispatch.h>
524
+ #include <ATen/ops/t_copy_compositeexplicitautograd_dispatch.h>
525
+ #include <ATen/ops/to_mkldnn_compositeexplicitautograd_dispatch.h>
526
+ #include <ATen/ops/to_padded_tensor_compositeexplicitautograd_dispatch.h>
527
+ #include <ATen/ops/trace_compositeexplicitautograd_dispatch.h>
528
+ #include <ATen/ops/transpose_compositeexplicitautograd_dispatch.h>
529
+ #include <ATen/ops/transpose_copy_compositeexplicitautograd_dispatch.h>
530
+ #include <ATen/ops/tril_indices_compositeexplicitautograd_dispatch.h>
531
+ #include <ATen/ops/triu_indices_compositeexplicitautograd_dispatch.h>
532
+ #include <ATen/ops/unbind_compositeexplicitautograd_dispatch.h>
533
+ #include <ATen/ops/unbind_copy_compositeexplicitautograd_dispatch.h>
534
+ #include <ATen/ops/unfold_backward_compositeexplicitautograd_dispatch.h>
535
+ #include <ATen/ops/unfold_copy_compositeexplicitautograd_dispatch.h>
536
+ #include <ATen/ops/uniform_compositeexplicitautograd_dispatch.h>
537
+ #include <ATen/ops/unique_consecutive_compositeexplicitautograd_dispatch.h>
538
+ #include <ATen/ops/unique_dim_compositeexplicitautograd_dispatch.h>
539
+ #include <ATen/ops/unique_dim_consecutive_compositeexplicitautograd_dispatch.h>
540
+ #include <ATen/ops/unsafe_split_compositeexplicitautograd_dispatch.h>
541
+ #include <ATen/ops/unsafe_split_with_sizes_compositeexplicitautograd_dispatch.h>
542
+ #include <ATen/ops/unsqueeze_compositeexplicitautograd_dispatch.h>
543
+ #include <ATen/ops/unsqueeze_copy_compositeexplicitautograd_dispatch.h>
544
+ #include <ATen/ops/upsample_bilinear2d_compositeexplicitautograd_dispatch.h>
545
+ #include <ATen/ops/upsample_nearest2d_compositeexplicitautograd_dispatch.h>
546
+ #include <ATen/ops/values_compositeexplicitautograd_dispatch.h>
547
+ #include <ATen/ops/values_copy_compositeexplicitautograd_dispatch.h>
548
+ #include <ATen/ops/var_mean_compositeexplicitautograd_dispatch.h>
549
+ #include <ATen/ops/vdot_compositeexplicitautograd_dispatch.h>
550
+ #include <ATen/ops/view_compositeexplicitautograd_dispatch.h>
551
+ #include <ATen/ops/view_as_complex_copy_compositeexplicitautograd_dispatch.h>
552
+ #include <ATen/ops/view_as_real_copy_compositeexplicitautograd_dispatch.h>
553
+ #include <ATen/ops/view_copy_compositeexplicitautograd_dispatch.h>
554
+ #include <ATen/ops/xlogy_compositeexplicitautograd_dispatch.h>
555
+ #include <ATen/ops/zero_compositeexplicitautograd_dispatch.h>
556
+ #include <ATen/ops/zeros_compositeexplicitautograd_dispatch.h>
557
+ #include <ATen/ops/zeros_like_compositeexplicitautograd_dispatch.h>
558
+
559
+
560
+
lib/python3.12/site-packages/torch/include/ATen/CompositeExplicitAutogradNonFunctionalFunctions.h ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <ATen/core/TensorBody.h>
2
+
3
+ // TODO Undo all logic introduced for Note [Avoiding Include Cycles In Static Dispatch]
4
+ // Code introduced to avoid cyclic dependency in static dispatch is no longer
5
+ // needed as static dispatch logic is moved from TensorBody.h, which caused cycles in the first place,
6
+ // to Operators.cpp for supporting multiple backends with multiple kernels.
7
+ //
8
+ // Note [Avoiding Include Cycles In Static Dispatch]
9
+ // In order to avoid #include cycles in the static dispatch build, we've carefully split out
10
+ // the static function definition files into {DispatchKey}Functions.h and {DispatchKey}Functions_inl.h.
11
+ //
12
+ // Without this split, the include cycle looks like TensorBody.h -> CPUFunctions.h -> TensorBody.h.
13
+ // - TensorBody.h #includes CPUFunctions.h in the static dispatch build, because the tensor methods
14
+ // all need to call into the fastpath C++ API defined in CPUFunctions.h. The methods are also all
15
+ // directly inlined into TensorBody.h.
16
+ // - CPUFunctions.h #includes TensorBody.h because it contains function declarations for the entire C++ API,
17
+ // which include functions that have defaultable std::optional<Tensor> arguments.
18
+ // That requires knowing the full Tensor class definition.
19
+ //
20
+ // We break the cycle by doing the following:
21
+ // - Split out CPUFunction.h into two files: CPUFunctions.h and CPUFunctions_inl.h
22
+ // - CPUFunction.h is a dummy file that just includes the Tensor class and includes CPUFunctions_inl.,
23
+ // - CPUFunctions_inl.h includes everything else
24
+ // - (only in the static dispatch build) TensorBody.h makes sure to finish defining the Tensor class,
25
+ // and then it includes CPUFunctions_inl.h.
26
+ // - All other files that want the cpu fastpath functions can include CPUFunctions.h directly.
27
+ // - This also means that static dispatch build, CPUFunctions.h only needs to
28
+ // #include TensorBody.h, and it will automatically bring in CPUFunctions_inl.h.
29
+ #include <ATen/CompositeExplicitAutogradNonFunctionalFunctions_inl.h>
lib/python3.12/site-packages/torch/include/ATen/CompositeExplicitAutogradNonFunctionalFunctions_inl.h ADDED
@@ -0,0 +1,324 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+ // @generated by torchgen/gen.py from DispatchKeyFunctions_inl.h
3
+
4
+ // NB: The implementing C++ file is RegisterDispatchKey.cpp
5
+
6
+ // The only #includes we need are for custom classes that have defaults in the C++ API
7
+ #include <c10/core/MemoryFormat.h>
8
+ #include <c10/core/Scalar.h>
9
+ #include <ATen/core/Reduction.h>
10
+
11
+ #if defined(AT_PER_OPERATOR_HEADERS) && defined(TORCH_ASSERT_ONLY_METHOD_OPERATORS)
12
+ #error This change adds a dependency on all pytorch operators, meaning the \
13
+ file will need to be re-compiled every time an operator is changed or added. \
14
+ Consider including a specific operator from \
15
+ <ATen/ops/{my_operator}_compositeexplicitautogradnonfunctional_dispatch.h>. \
16
+ See NOTE [TORCH_ASSERT_ONLY_METHOD_OPERATORS].
17
+ #endif
18
+
19
+ #include <ATen/ops/_addmm_activation_compositeexplicitautogradnonfunctional_dispatch.h>
20
+ #include <ATen/ops/_conj_copy_compositeexplicitautogradnonfunctional_dispatch.h>
21
+ #include <ATen/ops/_convert_indices_from_coo_to_csr_compositeexplicitautogradnonfunctional_dispatch.h>
22
+ #include <ATen/ops/_convert_indices_from_csr_to_coo_compositeexplicitautogradnonfunctional_dispatch.h>
23
+ #include <ATen/ops/_fw_primal_copy_compositeexplicitautogradnonfunctional_dispatch.h>
24
+ #include <ATen/ops/_indices_copy_compositeexplicitautogradnonfunctional_dispatch.h>
25
+ #include <ATen/ops/_linalg_det_compositeexplicitautogradnonfunctional_dispatch.h>
26
+ #include <ATen/ops/_linalg_eigh_compositeexplicitautogradnonfunctional_dispatch.h>
27
+ #include <ATen/ops/_linalg_slogdet_compositeexplicitautogradnonfunctional_dispatch.h>
28
+ #include <ATen/ops/_linalg_solve_ex_compositeexplicitautogradnonfunctional_dispatch.h>
29
+ #include <ATen/ops/_linalg_svd_compositeexplicitautogradnonfunctional_dispatch.h>
30
+ #include <ATen/ops/_log_softmax_compositeexplicitautogradnonfunctional_dispatch.h>
31
+ #include <ATen/ops/_log_softmax_backward_data_compositeexplicitautogradnonfunctional_dispatch.h>
32
+ #include <ATen/ops/_make_dual_copy_compositeexplicitautogradnonfunctional_dispatch.h>
33
+ #include <ATen/ops/_neg_view_copy_compositeexplicitautogradnonfunctional_dispatch.h>
34
+ #include <ATen/ops/_nested_get_values_copy_compositeexplicitautogradnonfunctional_dispatch.h>
35
+ #include <ATen/ops/_nested_view_from_buffer_copy_compositeexplicitautogradnonfunctional_dispatch.h>
36
+ #include <ATen/ops/_nested_view_from_jagged_copy_compositeexplicitautogradnonfunctional_dispatch.h>
37
+ #include <ATen/ops/_reshape_alias_copy_compositeexplicitautogradnonfunctional_dispatch.h>
38
+ #include <ATen/ops/_softmax_compositeexplicitautogradnonfunctional_dispatch.h>
39
+ #include <ATen/ops/_softmax_backward_data_compositeexplicitautogradnonfunctional_dispatch.h>
40
+ #include <ATen/ops/_sparse_broadcast_to_copy_compositeexplicitautogradnonfunctional_dispatch.h>
41
+ #include <ATen/ops/_test_autograd_multiple_dispatch_view_copy_compositeexplicitautogradnonfunctional_dispatch.h>
42
+ #include <ATen/ops/_trilinear_compositeexplicitautogradnonfunctional_dispatch.h>
43
+ #include <ATen/ops/_upsample_bicubic2d_aa_compositeexplicitautogradnonfunctional_dispatch.h>
44
+ #include <ATen/ops/_upsample_bicubic2d_aa_backward_compositeexplicitautogradnonfunctional_dispatch.h>
45
+ #include <ATen/ops/_upsample_bilinear2d_aa_compositeexplicitautogradnonfunctional_dispatch.h>
46
+ #include <ATen/ops/_upsample_bilinear2d_aa_backward_compositeexplicitautogradnonfunctional_dispatch.h>
47
+ #include <ATen/ops/_upsample_nearest_exact1d_compositeexplicitautogradnonfunctional_dispatch.h>
48
+ #include <ATen/ops/_upsample_nearest_exact1d_backward_compositeexplicitautogradnonfunctional_dispatch.h>
49
+ #include <ATen/ops/_upsample_nearest_exact2d_compositeexplicitautogradnonfunctional_dispatch.h>
50
+ #include <ATen/ops/_upsample_nearest_exact2d_backward_compositeexplicitautogradnonfunctional_dispatch.h>
51
+ #include <ATen/ops/_upsample_nearest_exact3d_compositeexplicitautogradnonfunctional_dispatch.h>
52
+ #include <ATen/ops/_upsample_nearest_exact3d_backward_compositeexplicitautogradnonfunctional_dispatch.h>
53
+ #include <ATen/ops/_values_copy_compositeexplicitautogradnonfunctional_dispatch.h>
54
+ #include <ATen/ops/acos_compositeexplicitautogradnonfunctional_dispatch.h>
55
+ #include <ATen/ops/acosh_compositeexplicitautogradnonfunctional_dispatch.h>
56
+ #include <ATen/ops/adaptive_max_pool2d_compositeexplicitautogradnonfunctional_dispatch.h>
57
+ #include <ATen/ops/adaptive_max_pool2d_backward_compositeexplicitautogradnonfunctional_dispatch.h>
58
+ #include <ATen/ops/adaptive_max_pool3d_compositeexplicitautogradnonfunctional_dispatch.h>
59
+ #include <ATen/ops/adaptive_max_pool3d_backward_compositeexplicitautogradnonfunctional_dispatch.h>
60
+ #include <ATen/ops/add_compositeexplicitautogradnonfunctional_dispatch.h>
61
+ #include <ATen/ops/addcdiv_compositeexplicitautogradnonfunctional_dispatch.h>
62
+ #include <ATen/ops/addcmul_compositeexplicitautogradnonfunctional_dispatch.h>
63
+ #include <ATen/ops/addmm_compositeexplicitautogradnonfunctional_dispatch.h>
64
+ #include <ATen/ops/addmv_compositeexplicitautogradnonfunctional_dispatch.h>
65
+ #include <ATen/ops/alias_copy_compositeexplicitautogradnonfunctional_dispatch.h>
66
+ #include <ATen/ops/all_compositeexplicitautogradnonfunctional_dispatch.h>
67
+ #include <ATen/ops/amax_compositeexplicitautogradnonfunctional_dispatch.h>
68
+ #include <ATen/ops/amin_compositeexplicitautogradnonfunctional_dispatch.h>
69
+ #include <ATen/ops/aminmax_compositeexplicitautogradnonfunctional_dispatch.h>
70
+ #include <ATen/ops/any_compositeexplicitautogradnonfunctional_dispatch.h>
71
+ #include <ATen/ops/argmax_compositeexplicitautogradnonfunctional_dispatch.h>
72
+ #include <ATen/ops/argmin_compositeexplicitautogradnonfunctional_dispatch.h>
73
+ #include <ATen/ops/as_strided_compositeexplicitautogradnonfunctional_dispatch.h>
74
+ #include <ATen/ops/as_strided_copy_compositeexplicitautogradnonfunctional_dispatch.h>
75
+ #include <ATen/ops/as_strided_scatter_compositeexplicitautogradnonfunctional_dispatch.h>
76
+ #include <ATen/ops/asin_compositeexplicitautogradnonfunctional_dispatch.h>
77
+ #include <ATen/ops/asinh_compositeexplicitautogradnonfunctional_dispatch.h>
78
+ #include <ATen/ops/atan_compositeexplicitautogradnonfunctional_dispatch.h>
79
+ #include <ATen/ops/atan2_compositeexplicitautogradnonfunctional_dispatch.h>
80
+ #include <ATen/ops/atanh_compositeexplicitautogradnonfunctional_dispatch.h>
81
+ #include <ATen/ops/avg_pool2d_compositeexplicitautogradnonfunctional_dispatch.h>
82
+ #include <ATen/ops/avg_pool2d_backward_compositeexplicitautogradnonfunctional_dispatch.h>
83
+ #include <ATen/ops/avg_pool3d_compositeexplicitautogradnonfunctional_dispatch.h>
84
+ #include <ATen/ops/avg_pool3d_backward_compositeexplicitautogradnonfunctional_dispatch.h>
85
+ #include <ATen/ops/baddbmm_compositeexplicitautogradnonfunctional_dispatch.h>
86
+ #include <ATen/ops/bernoulli_compositeexplicitautogradnonfunctional_dispatch.h>
87
+ #include <ATen/ops/bitwise_and_compositeexplicitautogradnonfunctional_dispatch.h>
88
+ #include <ATen/ops/bitwise_left_shift_compositeexplicitautogradnonfunctional_dispatch.h>
89
+ #include <ATen/ops/bitwise_not_compositeexplicitautogradnonfunctional_dispatch.h>
90
+ #include <ATen/ops/bitwise_or_compositeexplicitautogradnonfunctional_dispatch.h>
91
+ #include <ATen/ops/bitwise_right_shift_compositeexplicitautogradnonfunctional_dispatch.h>
92
+ #include <ATen/ops/bitwise_xor_compositeexplicitautogradnonfunctional_dispatch.h>
93
+ #include <ATen/ops/bmm_compositeexplicitautogradnonfunctional_dispatch.h>
94
+ #include <ATen/ops/cat_compositeexplicitautogradnonfunctional_dispatch.h>
95
+ #include <ATen/ops/ccol_indices_copy_compositeexplicitautogradnonfunctional_dispatch.h>
96
+ #include <ATen/ops/ceil_compositeexplicitautogradnonfunctional_dispatch.h>
97
+ #include <ATen/ops/clamp_compositeexplicitautogradnonfunctional_dispatch.h>
98
+ #include <ATen/ops/clamp_max_compositeexplicitautogradnonfunctional_dispatch.h>
99
+ #include <ATen/ops/clamp_min_compositeexplicitautogradnonfunctional_dispatch.h>
100
+ #include <ATen/ops/col_indices_copy_compositeexplicitautogradnonfunctional_dispatch.h>
101
+ #include <ATen/ops/copy_compositeexplicitautogradnonfunctional_dispatch.h>
102
+ #include <ATen/ops/copysign_compositeexplicitautogradnonfunctional_dispatch.h>
103
+ #include <ATen/ops/cos_compositeexplicitautogradnonfunctional_dispatch.h>
104
+ #include <ATen/ops/cosh_compositeexplicitautogradnonfunctional_dispatch.h>
105
+ #include <ATen/ops/crow_indices_copy_compositeexplicitautogradnonfunctional_dispatch.h>
106
+ #include <ATen/ops/cumprod_compositeexplicitautogradnonfunctional_dispatch.h>
107
+ #include <ATen/ops/cumsum_compositeexplicitautogradnonfunctional_dispatch.h>
108
+ #include <ATen/ops/detach_copy_compositeexplicitautogradnonfunctional_dispatch.h>
109
+ #include <ATen/ops/diag_embed_compositeexplicitautogradnonfunctional_dispatch.h>
110
+ #include <ATen/ops/diagonal_copy_compositeexplicitautogradnonfunctional_dispatch.h>
111
+ #include <ATen/ops/diagonal_scatter_compositeexplicitautogradnonfunctional_dispatch.h>
112
+ #include <ATen/ops/digamma_compositeexplicitautogradnonfunctional_dispatch.h>
113
+ #include <ATen/ops/div_compositeexplicitautogradnonfunctional_dispatch.h>
114
+ #include <ATen/ops/elu_compositeexplicitautogradnonfunctional_dispatch.h>
115
+ #include <ATen/ops/elu_backward_compositeexplicitautogradnonfunctional_dispatch.h>
116
+ #include <ATen/ops/eq_compositeexplicitautogradnonfunctional_dispatch.h>
117
+ #include <ATen/ops/erf_compositeexplicitautogradnonfunctional_dispatch.h>
118
+ #include <ATen/ops/erfc_compositeexplicitautogradnonfunctional_dispatch.h>
119
+ #include <ATen/ops/erfinv_compositeexplicitautogradnonfunctional_dispatch.h>
120
+ #include <ATen/ops/exp_compositeexplicitautogradnonfunctional_dispatch.h>
121
+ #include <ATen/ops/exp2_compositeexplicitautogradnonfunctional_dispatch.h>
122
+ #include <ATen/ops/expand_copy_compositeexplicitautogradnonfunctional_dispatch.h>
123
+ #include <ATen/ops/expm1_compositeexplicitautogradnonfunctional_dispatch.h>
124
+ #include <ATen/ops/floor_compositeexplicitautogradnonfunctional_dispatch.h>
125
+ #include <ATen/ops/fmax_compositeexplicitautogradnonfunctional_dispatch.h>
126
+ #include <ATen/ops/fmin_compositeexplicitautogradnonfunctional_dispatch.h>
127
+ #include <ATen/ops/fmod_compositeexplicitautogradnonfunctional_dispatch.h>
128
+ #include <ATen/ops/frac_compositeexplicitautogradnonfunctional_dispatch.h>
129
+ #include <ATen/ops/fractional_max_pool2d_compositeexplicitautogradnonfunctional_dispatch.h>
130
+ #include <ATen/ops/fractional_max_pool2d_backward_compositeexplicitautogradnonfunctional_dispatch.h>
131
+ #include <ATen/ops/fractional_max_pool3d_compositeexplicitautogradnonfunctional_dispatch.h>
132
+ #include <ATen/ops/gather_compositeexplicitautogradnonfunctional_dispatch.h>
133
+ #include <ATen/ops/gcd_compositeexplicitautogradnonfunctional_dispatch.h>
134
+ #include <ATen/ops/ge_compositeexplicitautogradnonfunctional_dispatch.h>
135
+ #include <ATen/ops/gelu_compositeexplicitautogradnonfunctional_dispatch.h>
136
+ #include <ATen/ops/gelu_backward_compositeexplicitautogradnonfunctional_dispatch.h>
137
+ #include <ATen/ops/glu_compositeexplicitautogradnonfunctional_dispatch.h>
138
+ #include <ATen/ops/gt_compositeexplicitautogradnonfunctional_dispatch.h>
139
+ #include <ATen/ops/hardshrink_compositeexplicitautogradnonfunctional_dispatch.h>
140
+ #include <ATen/ops/hardshrink_backward_compositeexplicitautogradnonfunctional_dispatch.h>
141
+ #include <ATen/ops/hardsigmoid_compositeexplicitautogradnonfunctional_dispatch.h>
142
+ #include <ATen/ops/hardsigmoid_backward_compositeexplicitautogradnonfunctional_dispatch.h>
143
+ #include <ATen/ops/hash_tensor_compositeexplicitautogradnonfunctional_dispatch.h>
144
+ #include <ATen/ops/heaviside_compositeexplicitautogradnonfunctional_dispatch.h>
145
+ #include <ATen/ops/hypot_compositeexplicitautogradnonfunctional_dispatch.h>
146
+ #include <ATen/ops/i0_compositeexplicitautogradnonfunctional_dispatch.h>
147
+ #include <ATen/ops/igamma_compositeexplicitautogradnonfunctional_dispatch.h>
148
+ #include <ATen/ops/igammac_compositeexplicitautogradnonfunctional_dispatch.h>
149
+ #include <ATen/ops/index_compositeexplicitautogradnonfunctional_dispatch.h>
150
+ #include <ATen/ops/index_add_compositeexplicitautogradnonfunctional_dispatch.h>
151
+ #include <ATen/ops/index_copy_compositeexplicitautogradnonfunctional_dispatch.h>
152
+ #include <ATen/ops/index_reduce_compositeexplicitautogradnonfunctional_dispatch.h>
153
+ #include <ATen/ops/indices_copy_compositeexplicitautogradnonfunctional_dispatch.h>
154
+ #include <ATen/ops/isin_compositeexplicitautogradnonfunctional_dispatch.h>
155
+ #include <ATen/ops/isneginf_compositeexplicitautogradnonfunctional_dispatch.h>
156
+ #include <ATen/ops/isposinf_compositeexplicitautogradnonfunctional_dispatch.h>
157
+ #include <ATen/ops/lcm_compositeexplicitautogradnonfunctional_dispatch.h>
158
+ #include <ATen/ops/le_compositeexplicitautogradnonfunctional_dispatch.h>
159
+ #include <ATen/ops/leaky_relu_compositeexplicitautogradnonfunctional_dispatch.h>
160
+ #include <ATen/ops/leaky_relu_backward_compositeexplicitautogradnonfunctional_dispatch.h>
161
+ #include <ATen/ops/lerp_compositeexplicitautogradnonfunctional_dispatch.h>
162
+ #include <ATen/ops/lgamma_compositeexplicitautogradnonfunctional_dispatch.h>
163
+ #include <ATen/ops/lift_fresh_copy_compositeexplicitautogradnonfunctional_dispatch.h>
164
+ #include <ATen/ops/linalg_cholesky_ex_compositeexplicitautogradnonfunctional_dispatch.h>
165
+ #include <ATen/ops/linalg_cross_compositeexplicitautogradnonfunctional_dispatch.h>
166
+ #include <ATen/ops/linalg_inv_ex_compositeexplicitautogradnonfunctional_dispatch.h>
167
+ #include <ATen/ops/linalg_ldl_factor_ex_compositeexplicitautogradnonfunctional_dispatch.h>
168
+ #include <ATen/ops/linalg_ldl_solve_compositeexplicitautogradnonfunctional_dispatch.h>
169
+ #include <ATen/ops/linalg_lu_compositeexplicitautogradnonfunctional_dispatch.h>
170
+ #include <ATen/ops/linalg_lu_factor_ex_compositeexplicitautogradnonfunctional_dispatch.h>
171
+ #include <ATen/ops/linalg_lu_solve_compositeexplicitautogradnonfunctional_dispatch.h>
172
+ #include <ATen/ops/linalg_pinv_compositeexplicitautogradnonfunctional_dispatch.h>
173
+ #include <ATen/ops/linalg_qr_compositeexplicitautogradnonfunctional_dispatch.h>
174
+ #include <ATen/ops/linalg_vector_norm_compositeexplicitautogradnonfunctional_dispatch.h>
175
+ #include <ATen/ops/log_compositeexplicitautogradnonfunctional_dispatch.h>
176
+ #include <ATen/ops/log10_compositeexplicitautogradnonfunctional_dispatch.h>
177
+ #include <ATen/ops/log1p_compositeexplicitautogradnonfunctional_dispatch.h>
178
+ #include <ATen/ops/log2_compositeexplicitautogradnonfunctional_dispatch.h>
179
+ #include <ATen/ops/logaddexp_compositeexplicitautogradnonfunctional_dispatch.h>
180
+ #include <ATen/ops/logaddexp2_compositeexplicitautogradnonfunctional_dispatch.h>
181
+ #include <ATen/ops/logit_backward_compositeexplicitautogradnonfunctional_dispatch.h>
182
+ #include <ATen/ops/logsumexp_compositeexplicitautogradnonfunctional_dispatch.h>
183
+ #include <ATen/ops/lt_compositeexplicitautogradnonfunctional_dispatch.h>
184
+ #include <ATen/ops/lu_unpack_compositeexplicitautogradnonfunctional_dispatch.h>
185
+ #include <ATen/ops/max_compositeexplicitautogradnonfunctional_dispatch.h>
186
+ #include <ATen/ops/max_pool2d_with_indices_compositeexplicitautogradnonfunctional_dispatch.h>
187
+ #include <ATen/ops/max_pool2d_with_indices_backward_compositeexplicitautogradnonfunctional_dispatch.h>
188
+ #include <ATen/ops/maximum_compositeexplicitautogradnonfunctional_dispatch.h>
189
+ #include <ATen/ops/mean_compositeexplicitautogradnonfunctional_dispatch.h>
190
+ #include <ATen/ops/min_compositeexplicitautogradnonfunctional_dispatch.h>
191
+ #include <ATen/ops/minimum_compositeexplicitautogradnonfunctional_dispatch.h>
192
+ #include <ATen/ops/mish_compositeexplicitautogradnonfunctional_dispatch.h>
193
+ #include <ATen/ops/mm_compositeexplicitautogradnonfunctional_dispatch.h>
194
+ #include <ATen/ops/mse_loss_compositeexplicitautogradnonfunctional_dispatch.h>
195
+ #include <ATen/ops/mul_compositeexplicitautogradnonfunctional_dispatch.h>
196
+ #include <ATen/ops/narrow_copy_compositeexplicitautogradnonfunctional_dispatch.h>
197
+ #include <ATen/ops/ne_compositeexplicitautogradnonfunctional_dispatch.h>
198
+ #include <ATen/ops/neg_compositeexplicitautogradnonfunctional_dispatch.h>
199
+ #include <ATen/ops/new_empty_strided_compositeexplicitautogradnonfunctional_dispatch.h>
200
+ #include <ATen/ops/nextafter_compositeexplicitautogradnonfunctional_dispatch.h>
201
+ #include <ATen/ops/nll_loss_backward_compositeexplicitautogradnonfunctional_dispatch.h>
202
+ #include <ATen/ops/nll_loss_forward_compositeexplicitautogradnonfunctional_dispatch.h>
203
+ #include <ATen/ops/norm_compositeexplicitautogradnonfunctional_dispatch.h>
204
+ #include <ATen/ops/permute_copy_compositeexplicitautogradnonfunctional_dispatch.h>
205
+ #include <ATen/ops/pixel_shuffle_compositeexplicitautogradnonfunctional_dispatch.h>
206
+ #include <ATen/ops/pixel_unshuffle_compositeexplicitautogradnonfunctional_dispatch.h>
207
+ #include <ATen/ops/polygamma_compositeexplicitautogradnonfunctional_dispatch.h>
208
+ #include <ATen/ops/pow_compositeexplicitautogradnonfunctional_dispatch.h>
209
+ #include <ATen/ops/prod_compositeexplicitautogradnonfunctional_dispatch.h>
210
+ #include <ATen/ops/reciprocal_compositeexplicitautogradnonfunctional_dispatch.h>
211
+ #include <ATen/ops/reflection_pad1d_compositeexplicitautogradnonfunctional_dispatch.h>
212
+ #include <ATen/ops/reflection_pad1d_backward_compositeexplicitautogradnonfunctional_dispatch.h>
213
+ #include <ATen/ops/reflection_pad3d_compositeexplicitautogradnonfunctional_dispatch.h>
214
+ #include <ATen/ops/reflection_pad3d_backward_compositeexplicitautogradnonfunctional_dispatch.h>
215
+ #include <ATen/ops/remainder_compositeexplicitautogradnonfunctional_dispatch.h>
216
+ #include <ATen/ops/renorm_compositeexplicitautogradnonfunctional_dispatch.h>
217
+ #include <ATen/ops/replication_pad1d_compositeexplicitautogradnonfunctional_dispatch.h>
218
+ #include <ATen/ops/replication_pad1d_backward_compositeexplicitautogradnonfunctional_dispatch.h>
219
+ #include <ATen/ops/replication_pad2d_compositeexplicitautogradnonfunctional_dispatch.h>
220
+ #include <ATen/ops/replication_pad3d_compositeexplicitautogradnonfunctional_dispatch.h>
221
+ #include <ATen/ops/round_compositeexplicitautogradnonfunctional_dispatch.h>
222
+ #include <ATen/ops/row_indices_copy_compositeexplicitautogradnonfunctional_dispatch.h>
223
+ #include <ATen/ops/rsqrt_compositeexplicitautogradnonfunctional_dispatch.h>
224
+ #include <ATen/ops/scatter_compositeexplicitautogradnonfunctional_dispatch.h>
225
+ #include <ATen/ops/scatter_add_compositeexplicitautogradnonfunctional_dispatch.h>
226
+ #include <ATen/ops/scatter_reduce_compositeexplicitautogradnonfunctional_dispatch.h>
227
+ #include <ATen/ops/select_backward_compositeexplicitautogradnonfunctional_dispatch.h>
228
+ #include <ATen/ops/select_copy_compositeexplicitautogradnonfunctional_dispatch.h>
229
+ #include <ATen/ops/select_scatter_compositeexplicitautogradnonfunctional_dispatch.h>
230
+ #include <ATen/ops/sgn_compositeexplicitautogradnonfunctional_dispatch.h>
231
+ #include <ATen/ops/sigmoid_compositeexplicitautogradnonfunctional_dispatch.h>
232
+ #include <ATen/ops/sigmoid_backward_compositeexplicitautogradnonfunctional_dispatch.h>
233
+ #include <ATen/ops/sign_compositeexplicitautogradnonfunctional_dispatch.h>
234
+ #include <ATen/ops/signbit_compositeexplicitautogradnonfunctional_dispatch.h>
235
+ #include <ATen/ops/silu_compositeexplicitautogradnonfunctional_dispatch.h>
236
+ #include <ATen/ops/silu_backward_compositeexplicitautogradnonfunctional_dispatch.h>
237
+ #include <ATen/ops/sin_compositeexplicitautogradnonfunctional_dispatch.h>
238
+ #include <ATen/ops/sinc_compositeexplicitautogradnonfunctional_dispatch.h>
239
+ #include <ATen/ops/sinh_compositeexplicitautogradnonfunctional_dispatch.h>
240
+ #include <ATen/ops/slice_copy_compositeexplicitautogradnonfunctional_dispatch.h>
241
+ #include <ATen/ops/slice_scatter_compositeexplicitautogradnonfunctional_dispatch.h>
242
+ #include <ATen/ops/slow_conv_transpose2d_compositeexplicitautogradnonfunctional_dispatch.h>
243
+ #include <ATen/ops/smooth_l1_loss_compositeexplicitautogradnonfunctional_dispatch.h>
244
+ #include <ATen/ops/softplus_compositeexplicitautogradnonfunctional_dispatch.h>
245
+ #include <ATen/ops/softplus_backward_compositeexplicitautogradnonfunctional_dispatch.h>
246
+ #include <ATen/ops/softshrink_compositeexplicitautogradnonfunctional_dispatch.h>
247
+ #include <ATen/ops/softshrink_backward_compositeexplicitautogradnonfunctional_dispatch.h>
248
+ #include <ATen/ops/sort_compositeexplicitautogradnonfunctional_dispatch.h>
249
+ #include <ATen/ops/special_airy_ai_compositeexplicitautogradnonfunctional_dispatch.h>
250
+ #include <ATen/ops/special_bessel_j0_compositeexplicitautogradnonfunctional_dispatch.h>
251
+ #include <ATen/ops/special_bessel_j1_compositeexplicitautogradnonfunctional_dispatch.h>
252
+ #include <ATen/ops/special_bessel_y0_compositeexplicitautogradnonfunctional_dispatch.h>
253
+ #include <ATen/ops/special_bessel_y1_compositeexplicitautogradnonfunctional_dispatch.h>
254
+ #include <ATen/ops/special_chebyshev_polynomial_t_compositeexplicitautogradnonfunctional_dispatch.h>
255
+ #include <ATen/ops/special_chebyshev_polynomial_u_compositeexplicitautogradnonfunctional_dispatch.h>
256
+ #include <ATen/ops/special_chebyshev_polynomial_v_compositeexplicitautogradnonfunctional_dispatch.h>
257
+ #include <ATen/ops/special_chebyshev_polynomial_w_compositeexplicitautogradnonfunctional_dispatch.h>
258
+ #include <ATen/ops/special_entr_compositeexplicitautogradnonfunctional_dispatch.h>
259
+ #include <ATen/ops/special_erfcx_compositeexplicitautogradnonfunctional_dispatch.h>
260
+ #include <ATen/ops/special_hermite_polynomial_h_compositeexplicitautogradnonfunctional_dispatch.h>
261
+ #include <ATen/ops/special_hermite_polynomial_he_compositeexplicitautogradnonfunctional_dispatch.h>
262
+ #include <ATen/ops/special_i0e_compositeexplicitautogradnonfunctional_dispatch.h>
263
+ #include <ATen/ops/special_i1_compositeexplicitautogradnonfunctional_dispatch.h>
264
+ #include <ATen/ops/special_i1e_compositeexplicitautogradnonfunctional_dispatch.h>
265
+ #include <ATen/ops/special_laguerre_polynomial_l_compositeexplicitautogradnonfunctional_dispatch.h>
266
+ #include <ATen/ops/special_legendre_polynomial_p_compositeexplicitautogradnonfunctional_dispatch.h>
267
+ #include <ATen/ops/special_log_ndtr_compositeexplicitautogradnonfunctional_dispatch.h>
268
+ #include <ATen/ops/special_modified_bessel_i0_compositeexplicitautogradnonfunctional_dispatch.h>
269
+ #include <ATen/ops/special_modified_bessel_i1_compositeexplicitautogradnonfunctional_dispatch.h>
270
+ #include <ATen/ops/special_modified_bessel_k0_compositeexplicitautogradnonfunctional_dispatch.h>
271
+ #include <ATen/ops/special_modified_bessel_k1_compositeexplicitautogradnonfunctional_dispatch.h>
272
+ #include <ATen/ops/special_ndtri_compositeexplicitautogradnonfunctional_dispatch.h>
273
+ #include <ATen/ops/special_scaled_modified_bessel_k0_compositeexplicitautogradnonfunctional_dispatch.h>
274
+ #include <ATen/ops/special_scaled_modified_bessel_k1_compositeexplicitautogradnonfunctional_dispatch.h>
275
+ #include <ATen/ops/special_shifted_chebyshev_polynomial_t_compositeexplicitautogradnonfunctional_dispatch.h>
276
+ #include <ATen/ops/special_shifted_chebyshev_polynomial_u_compositeexplicitautogradnonfunctional_dispatch.h>
277
+ #include <ATen/ops/special_shifted_chebyshev_polynomial_v_compositeexplicitautogradnonfunctional_dispatch.h>
278
+ #include <ATen/ops/special_shifted_chebyshev_polynomial_w_compositeexplicitautogradnonfunctional_dispatch.h>
279
+ #include <ATen/ops/special_spherical_bessel_j0_compositeexplicitautogradnonfunctional_dispatch.h>
280
+ #include <ATen/ops/special_xlog1py_compositeexplicitautogradnonfunctional_dispatch.h>
281
+ #include <ATen/ops/special_zeta_compositeexplicitautogradnonfunctional_dispatch.h>
282
+ #include <ATen/ops/split_copy_compositeexplicitautogradnonfunctional_dispatch.h>
283
+ #include <ATen/ops/split_with_sizes_copy_compositeexplicitautogradnonfunctional_dispatch.h>
284
+ #include <ATen/ops/sqrt_compositeexplicitautogradnonfunctional_dispatch.h>
285
+ #include <ATen/ops/squeeze_copy_compositeexplicitautogradnonfunctional_dispatch.h>
286
+ #include <ATen/ops/sub_compositeexplicitautogradnonfunctional_dispatch.h>
287
+ #include <ATen/ops/sum_compositeexplicitautogradnonfunctional_dispatch.h>
288
+ #include <ATen/ops/t_copy_compositeexplicitautogradnonfunctional_dispatch.h>
289
+ #include <ATen/ops/tan_compositeexplicitautogradnonfunctional_dispatch.h>
290
+ #include <ATen/ops/tanh_compositeexplicitautogradnonfunctional_dispatch.h>
291
+ #include <ATen/ops/tanh_backward_compositeexplicitautogradnonfunctional_dispatch.h>
292
+ #include <ATen/ops/threshold_compositeexplicitautogradnonfunctional_dispatch.h>
293
+ #include <ATen/ops/threshold_backward_compositeexplicitautogradnonfunctional_dispatch.h>
294
+ #include <ATen/ops/topk_compositeexplicitautogradnonfunctional_dispatch.h>
295
+ #include <ATen/ops/transpose_copy_compositeexplicitautogradnonfunctional_dispatch.h>
296
+ #include <ATen/ops/triangular_solve_compositeexplicitautogradnonfunctional_dispatch.h>
297
+ #include <ATen/ops/tril_compositeexplicitautogradnonfunctional_dispatch.h>
298
+ #include <ATen/ops/triu_compositeexplicitautogradnonfunctional_dispatch.h>
299
+ #include <ATen/ops/trunc_compositeexplicitautogradnonfunctional_dispatch.h>
300
+ #include <ATen/ops/unbind_copy_compositeexplicitautogradnonfunctional_dispatch.h>
301
+ #include <ATen/ops/unfold_copy_compositeexplicitautogradnonfunctional_dispatch.h>
302
+ #include <ATen/ops/unsqueeze_copy_compositeexplicitautogradnonfunctional_dispatch.h>
303
+ #include <ATen/ops/upsample_bicubic2d_compositeexplicitautogradnonfunctional_dispatch.h>
304
+ #include <ATen/ops/upsample_bicubic2d_backward_compositeexplicitautogradnonfunctional_dispatch.h>
305
+ #include <ATen/ops/upsample_bilinear2d_compositeexplicitautogradnonfunctional_dispatch.h>
306
+ #include <ATen/ops/upsample_bilinear2d_backward_compositeexplicitautogradnonfunctional_dispatch.h>
307
+ #include <ATen/ops/upsample_linear1d_compositeexplicitautogradnonfunctional_dispatch.h>
308
+ #include <ATen/ops/upsample_linear1d_backward_compositeexplicitautogradnonfunctional_dispatch.h>
309
+ #include <ATen/ops/upsample_nearest1d_compositeexplicitautogradnonfunctional_dispatch.h>
310
+ #include <ATen/ops/upsample_nearest1d_backward_compositeexplicitautogradnonfunctional_dispatch.h>
311
+ #include <ATen/ops/upsample_nearest2d_compositeexplicitautogradnonfunctional_dispatch.h>
312
+ #include <ATen/ops/upsample_nearest2d_backward_compositeexplicitautogradnonfunctional_dispatch.h>
313
+ #include <ATen/ops/upsample_nearest3d_compositeexplicitautogradnonfunctional_dispatch.h>
314
+ #include <ATen/ops/upsample_nearest3d_backward_compositeexplicitautogradnonfunctional_dispatch.h>
315
+ #include <ATen/ops/upsample_trilinear3d_compositeexplicitautogradnonfunctional_dispatch.h>
316
+ #include <ATen/ops/upsample_trilinear3d_backward_compositeexplicitautogradnonfunctional_dispatch.h>
317
+ #include <ATen/ops/values_copy_compositeexplicitautogradnonfunctional_dispatch.h>
318
+ #include <ATen/ops/view_as_complex_copy_compositeexplicitautogradnonfunctional_dispatch.h>
319
+ #include <ATen/ops/view_as_real_copy_compositeexplicitautogradnonfunctional_dispatch.h>
320
+ #include <ATen/ops/view_copy_compositeexplicitautogradnonfunctional_dispatch.h>
321
+ #include <ATen/ops/xlogy_compositeexplicitautogradnonfunctional_dispatch.h>
322
+
323
+
324
+
lib/python3.12/site-packages/torch/include/ATen/CompositeImplicitAutogradFunctions.h ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <ATen/core/TensorBody.h>
2
+
3
+ // TODO Undo all logic introduced for Note [Avoiding Include Cycles In Static Dispatch]
4
+ // Code introduced to avoid cyclic dependency in static dispatch is no longer
5
+ // needed as static dispatch logic is moved from TensorBody.h, which caused cycles in the first place,
6
+ // to Operators.cpp for supporting multiple backends with multiple kernels.
7
+ //
8
+ // Note [Avoiding Include Cycles In Static Dispatch]
9
+ // In order to avoid #include cycles in the static dispatch build, we've carefully split out
10
+ // the static function definition files into {DispatchKey}Functions.h and {DispatchKey}Functions_inl.h.
11
+ //
12
+ // Without this split, the include cycle looks like TensorBody.h -> CPUFunctions.h -> TensorBody.h.
13
+ // - TensorBody.h #includes CPUFunctions.h in the static dispatch build, because the tensor methods
14
+ // all need to call into the fastpath C++ API defined in CPUFunctions.h. The methods are also all
15
+ // directly inlined into TensorBody.h.
16
+ // - CPUFunctions.h #includes TensorBody.h because it contains function declarations for the entire C++ API,
17
+ // which include functions that have defaultable std::optional<Tensor> arguments.
18
+ // That requires knowing the full Tensor class definition.
19
+ //
20
+ // We break the cycle by doing the following:
21
+ // - Split out CPUFunction.h into two files: CPUFunctions.h and CPUFunctions_inl.h
22
+ // - CPUFunction.h is a dummy file that just includes the Tensor class and includes CPUFunctions_inl.,
23
+ // - CPUFunctions_inl.h includes everything else
24
+ // - (only in the static dispatch build) TensorBody.h makes sure to finish defining the Tensor class,
25
+ // and then it includes CPUFunctions_inl.h.
26
+ // - All other files that want the cpu fastpath functions can include CPUFunctions.h directly.
27
+ // - This also means that static dispatch build, CPUFunctions.h only needs to
28
+ // #include TensorBody.h, and it will automatically bring in CPUFunctions_inl.h.
29
+ #include <ATen/CompositeImplicitAutogradFunctions_inl.h>
lib/python3.12/site-packages/torch/include/ATen/CompositeImplicitAutogradFunctions_inl.h ADDED
@@ -0,0 +1,503 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+ // @generated by torchgen/gen.py from DispatchKeyFunctions_inl.h
3
+
4
+ // NB: The implementing C++ file is RegisterDispatchKey.cpp
5
+
6
+ // The only #includes we need are for custom classes that have defaults in the C++ API
7
+ #include <c10/core/MemoryFormat.h>
8
+ #include <c10/core/Scalar.h>
9
+ #include <ATen/core/Reduction.h>
10
+
11
+ #if defined(AT_PER_OPERATOR_HEADERS) && defined(TORCH_ASSERT_ONLY_METHOD_OPERATORS)
12
+ #error This change adds a dependency on all pytorch operators, meaning the \
13
+ file will need to be re-compiled every time an operator is changed or added. \
14
+ Consider including a specific operator from \
15
+ <ATen/ops/{my_operator}_compositeimplicitautograd_dispatch.h>. \
16
+ See NOTE [TORCH_ASSERT_ONLY_METHOD_OPERATORS].
17
+ #endif
18
+
19
+ #include <ATen/ops/_add_batch_dim_compositeimplicitautograd_dispatch.h>
20
+ #include <ATen/ops/_autocast_to_full_precision_compositeimplicitautograd_dispatch.h>
21
+ #include <ATen/ops/_autocast_to_reduced_precision_compositeimplicitautograd_dispatch.h>
22
+ #include <ATen/ops/_backward_compositeimplicitautograd_dispatch.h>
23
+ #include <ATen/ops/_batch_norm_impl_index_compositeimplicitautograd_dispatch.h>
24
+ #include <ATen/ops/_batch_norm_impl_index_backward_compositeimplicitautograd_dispatch.h>
25
+ #include <ATen/ops/_cast_Byte_compositeimplicitautograd_dispatch.h>
26
+ #include <ATen/ops/_cast_Char_compositeimplicitautograd_dispatch.h>
27
+ #include <ATen/ops/_cast_Double_compositeimplicitautograd_dispatch.h>
28
+ #include <ATen/ops/_cast_Float_compositeimplicitautograd_dispatch.h>
29
+ #include <ATen/ops/_cast_Half_compositeimplicitautograd_dispatch.h>
30
+ #include <ATen/ops/_cast_Int_compositeimplicitautograd_dispatch.h>
31
+ #include <ATen/ops/_cast_Long_compositeimplicitautograd_dispatch.h>
32
+ #include <ATen/ops/_cast_Short_compositeimplicitautograd_dispatch.h>
33
+ #include <ATen/ops/_choose_qparams_per_tensor_compositeimplicitautograd_dispatch.h>
34
+ #include <ATen/ops/_convolution_compositeimplicitautograd_dispatch.h>
35
+ #include <ATen/ops/_convolution_double_backward_compositeimplicitautograd_dispatch.h>
36
+ #include <ATen/ops/_convolution_mode_compositeimplicitautograd_dispatch.h>
37
+ #include <ATen/ops/_cufft_clear_plan_cache_compositeimplicitautograd_dispatch.h>
38
+ #include <ATen/ops/_cufft_get_plan_cache_max_size_compositeimplicitautograd_dispatch.h>
39
+ #include <ATen/ops/_cufft_get_plan_cache_size_compositeimplicitautograd_dispatch.h>
40
+ #include <ATen/ops/_cufft_set_plan_cache_max_size_compositeimplicitautograd_dispatch.h>
41
+ #include <ATen/ops/_debug_has_internal_overlap_compositeimplicitautograd_dispatch.h>
42
+ #include <ATen/ops/_dim_arange_compositeimplicitautograd_dispatch.h>
43
+ #include <ATen/ops/_embedding_bag_sparse_backward_compositeimplicitautograd_dispatch.h>
44
+ #include <ATen/ops/_fused_rms_norm_compositeimplicitautograd_dispatch.h>
45
+ #include <ATen/ops/_gather_sparse_backward_compositeimplicitautograd_dispatch.h>
46
+ #include <ATen/ops/_grid_sampler_2d_cpu_fallback_backward_compositeimplicitautograd_dispatch.h>
47
+ #include <ATen/ops/_has_compatible_shallow_copy_type_compositeimplicitautograd_dispatch.h>
48
+ #include <ATen/ops/_is_zerotensor_compositeimplicitautograd_dispatch.h>
49
+ #include <ATen/ops/_lu_with_info_compositeimplicitautograd_dispatch.h>
50
+ #include <ATen/ops/_nnpack_available_compositeimplicitautograd_dispatch.h>
51
+ #include <ATen/ops/_pack_padded_sequence_backward_compositeimplicitautograd_dispatch.h>
52
+ #include <ATen/ops/_pad_circular_compositeimplicitautograd_dispatch.h>
53
+ #include <ATen/ops/_pad_enum_compositeimplicitautograd_dispatch.h>
54
+ #include <ATen/ops/_pad_packed_sequence_compositeimplicitautograd_dispatch.h>
55
+ #include <ATen/ops/_propagate_xla_data_compositeimplicitautograd_dispatch.h>
56
+ #include <ATen/ops/_remove_batch_dim_compositeimplicitautograd_dispatch.h>
57
+ #include <ATen/ops/_reshape_from_tensor_compositeimplicitautograd_dispatch.h>
58
+ #include <ATen/ops/_rowwise_prune_compositeimplicitautograd_dispatch.h>
59
+ #include <ATen/ops/_saturate_weight_to_fp16_compositeimplicitautograd_dispatch.h>
60
+ #include <ATen/ops/_scaled_dot_product_attention_math_compositeimplicitautograd_dispatch.h>
61
+ #include <ATen/ops/_shape_as_tensor_compositeimplicitautograd_dispatch.h>
62
+ #include <ATen/ops/_sobol_engine_draw_compositeimplicitautograd_dispatch.h>
63
+ #include <ATen/ops/_sobol_engine_ff_compositeimplicitautograd_dispatch.h>
64
+ #include <ATen/ops/_sobol_engine_initialize_state_compositeimplicitautograd_dispatch.h>
65
+ #include <ATen/ops/_sobol_engine_scramble_compositeimplicitautograd_dispatch.h>
66
+ #include <ATen/ops/_sparse_bsc_tensor_unsafe_compositeimplicitautograd_dispatch.h>
67
+ #include <ATen/ops/_sparse_bsr_tensor_unsafe_compositeimplicitautograd_dispatch.h>
68
+ #include <ATen/ops/_sparse_compressed_tensor_unsafe_compositeimplicitautograd_dispatch.h>
69
+ #include <ATen/ops/_sparse_coo_tensor_unsafe_compositeimplicitautograd_dispatch.h>
70
+ #include <ATen/ops/_sparse_csc_tensor_unsafe_compositeimplicitautograd_dispatch.h>
71
+ #include <ATen/ops/_sparse_csr_tensor_unsafe_compositeimplicitautograd_dispatch.h>
72
+ #include <ATen/ops/_sparse_log_softmax_compositeimplicitautograd_dispatch.h>
73
+ #include <ATen/ops/_sparse_mm_compositeimplicitautograd_dispatch.h>
74
+ #include <ATen/ops/_sparse_softmax_compositeimplicitautograd_dispatch.h>
75
+ #include <ATen/ops/_sparse_sum_compositeimplicitautograd_dispatch.h>
76
+ #include <ATen/ops/_test_ambiguous_defaults_compositeimplicitautograd_dispatch.h>
77
+ #include <ATen/ops/_test_autograd_multiple_dispatch_compositeimplicitautograd_dispatch.h>
78
+ #include <ATen/ops/_test_check_tensor_compositeimplicitautograd_dispatch.h>
79
+ #include <ATen/ops/_test_serialization_subcmul_compositeimplicitautograd_dispatch.h>
80
+ #include <ATen/ops/_test_string_default_compositeimplicitautograd_dispatch.h>
81
+ #include <ATen/ops/_thnn_differentiable_gru_cell_backward_compositeimplicitautograd_dispatch.h>
82
+ #include <ATen/ops/_thnn_differentiable_lstm_cell_backward_compositeimplicitautograd_dispatch.h>
83
+ #include <ATen/ops/_thnn_fused_lstm_cell_backward_compositeimplicitautograd_dispatch.h>
84
+ #include <ATen/ops/_to_cpu_compositeimplicitautograd_dispatch.h>
85
+ #include <ATen/ops/_unpack_dual_compositeimplicitautograd_dispatch.h>
86
+ #include <ATen/ops/_upsample_bicubic2d_aa_compositeimplicitautograd_dispatch.h>
87
+ #include <ATen/ops/_upsample_bilinear2d_aa_compositeimplicitautograd_dispatch.h>
88
+ #include <ATen/ops/_upsample_nearest_exact1d_compositeimplicitautograd_dispatch.h>
89
+ #include <ATen/ops/_upsample_nearest_exact2d_compositeimplicitautograd_dispatch.h>
90
+ #include <ATen/ops/_upsample_nearest_exact3d_compositeimplicitautograd_dispatch.h>
91
+ #include <ATen/ops/_use_cudnn_rnn_flatten_weight_compositeimplicitautograd_dispatch.h>
92
+ #include <ATen/ops/_validate_sparse_bsc_tensor_args_compositeimplicitautograd_dispatch.h>
93
+ #include <ATen/ops/_validate_sparse_bsr_tensor_args_compositeimplicitautograd_dispatch.h>
94
+ #include <ATen/ops/_validate_sparse_compressed_tensor_args_compositeimplicitautograd_dispatch.h>
95
+ #include <ATen/ops/_validate_sparse_coo_tensor_args_compositeimplicitautograd_dispatch.h>
96
+ #include <ATen/ops/_validate_sparse_csc_tensor_args_compositeimplicitautograd_dispatch.h>
97
+ #include <ATen/ops/_validate_sparse_csr_tensor_args_compositeimplicitautograd_dispatch.h>
98
+ #include <ATen/ops/_version_compositeimplicitautograd_dispatch.h>
99
+ #include <ATen/ops/_weight_norm_compositeimplicitautograd_dispatch.h>
100
+ #include <ATen/ops/_weight_norm_differentiable_backward_compositeimplicitautograd_dispatch.h>
101
+ #include <ATen/ops/_wrapped_linear_prepack_compositeimplicitautograd_dispatch.h>
102
+ #include <ATen/ops/_wrapped_quantized_linear_prepacked_compositeimplicitautograd_dispatch.h>
103
+ #include <ATen/ops/absolute_compositeimplicitautograd_dispatch.h>
104
+ #include <ATen/ops/adaptive_avg_pool1d_compositeimplicitautograd_dispatch.h>
105
+ #include <ATen/ops/adaptive_avg_pool2d_compositeimplicitautograd_dispatch.h>
106
+ #include <ATen/ops/adaptive_avg_pool3d_compositeimplicitautograd_dispatch.h>
107
+ #include <ATen/ops/adaptive_max_pool1d_compositeimplicitautograd_dispatch.h>
108
+ #include <ATen/ops/adjoint_compositeimplicitautograd_dispatch.h>
109
+ #include <ATen/ops/affine_grid_generator_backward_compositeimplicitautograd_dispatch.h>
110
+ #include <ATen/ops/align_as_compositeimplicitautograd_dispatch.h>
111
+ #include <ATen/ops/align_tensors_compositeimplicitautograd_dispatch.h>
112
+ #include <ATen/ops/align_to_compositeimplicitautograd_dispatch.h>
113
+ #include <ATen/ops/all_compositeimplicitautograd_dispatch.h>
114
+ #include <ATen/ops/alpha_dropout_compositeimplicitautograd_dispatch.h>
115
+ #include <ATen/ops/and_compositeimplicitautograd_dispatch.h>
116
+ #include <ATen/ops/any_compositeimplicitautograd_dispatch.h>
117
+ #include <ATen/ops/arccos_compositeimplicitautograd_dispatch.h>
118
+ #include <ATen/ops/arccosh_compositeimplicitautograd_dispatch.h>
119
+ #include <ATen/ops/arcsin_compositeimplicitautograd_dispatch.h>
120
+ #include <ATen/ops/arcsinh_compositeimplicitautograd_dispatch.h>
121
+ #include <ATen/ops/arctan_compositeimplicitautograd_dispatch.h>
122
+ #include <ATen/ops/arctan2_compositeimplicitautograd_dispatch.h>
123
+ #include <ATen/ops/arctanh_compositeimplicitautograd_dispatch.h>
124
+ #include <ATen/ops/argsort_compositeimplicitautograd_dispatch.h>
125
+ #include <ATen/ops/argwhere_compositeimplicitautograd_dispatch.h>
126
+ #include <ATen/ops/atleast_1d_compositeimplicitautograd_dispatch.h>
127
+ #include <ATen/ops/atleast_2d_compositeimplicitautograd_dispatch.h>
128
+ #include <ATen/ops/atleast_3d_compositeimplicitautograd_dispatch.h>
129
+ #include <ATen/ops/avg_pool1d_compositeimplicitautograd_dispatch.h>
130
+ #include <ATen/ops/batch_norm_compositeimplicitautograd_dispatch.h>
131
+ #include <ATen/ops/bilinear_compositeimplicitautograd_dispatch.h>
132
+ #include <ATen/ops/broadcast_tensors_compositeimplicitautograd_dispatch.h>
133
+ #include <ATen/ops/broadcast_to_compositeimplicitautograd_dispatch.h>
134
+ #include <ATen/ops/can_cast_compositeimplicitautograd_dispatch.h>
135
+ #include <ATen/ops/cartesian_prod_compositeimplicitautograd_dispatch.h>
136
+ #include <ATen/ops/cat_compositeimplicitautograd_dispatch.h>
137
+ #include <ATen/ops/cdist_compositeimplicitautograd_dispatch.h>
138
+ #include <ATen/ops/chain_matmul_compositeimplicitautograd_dispatch.h>
139
+ #include <ATen/ops/chalf_compositeimplicitautograd_dispatch.h>
140
+ #include <ATen/ops/choose_qparams_optimized_compositeimplicitautograd_dispatch.h>
141
+ #include <ATen/ops/chunk_compositeimplicitautograd_dispatch.h>
142
+ #include <ATen/ops/clip_compositeimplicitautograd_dispatch.h>
143
+ #include <ATen/ops/coalesce_compositeimplicitautograd_dispatch.h>
144
+ #include <ATen/ops/column_stack_compositeimplicitautograd_dispatch.h>
145
+ #include <ATen/ops/combinations_compositeimplicitautograd_dispatch.h>
146
+ #include <ATen/ops/concat_compositeimplicitautograd_dispatch.h>
147
+ #include <ATen/ops/concatenate_compositeimplicitautograd_dispatch.h>
148
+ #include <ATen/ops/conj_compositeimplicitautograd_dispatch.h>
149
+ #include <ATen/ops/conj_physical_compositeimplicitautograd_dispatch.h>
150
+ #include <ATen/ops/contiguous_compositeimplicitautograd_dispatch.h>
151
+ #include <ATen/ops/conv1d_compositeimplicitautograd_dispatch.h>
152
+ #include <ATen/ops/conv2d_compositeimplicitautograd_dispatch.h>
153
+ #include <ATen/ops/conv3d_compositeimplicitautograd_dispatch.h>
154
+ #include <ATen/ops/conv_tbc_backward_compositeimplicitautograd_dispatch.h>
155
+ #include <ATen/ops/conv_transpose1d_compositeimplicitautograd_dispatch.h>
156
+ #include <ATen/ops/conv_transpose2d_compositeimplicitautograd_dispatch.h>
157
+ #include <ATen/ops/conv_transpose3d_compositeimplicitautograd_dispatch.h>
158
+ #include <ATen/ops/corrcoef_compositeimplicitautograd_dispatch.h>
159
+ #include <ATen/ops/cosine_embedding_loss_compositeimplicitautograd_dispatch.h>
160
+ #include <ATen/ops/cosine_similarity_compositeimplicitautograd_dispatch.h>
161
+ #include <ATen/ops/cov_compositeimplicitautograd_dispatch.h>
162
+ #include <ATen/ops/cross_compositeimplicitautograd_dispatch.h>
163
+ #include <ATen/ops/cross_entropy_loss_compositeimplicitautograd_dispatch.h>
164
+ #include <ATen/ops/ctc_loss_compositeimplicitautograd_dispatch.h>
165
+ #include <ATen/ops/cudnn_is_acceptable_compositeimplicitautograd_dispatch.h>
166
+ #include <ATen/ops/cummax_compositeimplicitautograd_dispatch.h>
167
+ #include <ATen/ops/cummaxmin_backward_compositeimplicitautograd_dispatch.h>
168
+ #include <ATen/ops/cummin_compositeimplicitautograd_dispatch.h>
169
+ #include <ATen/ops/cumprod_compositeimplicitautograd_dispatch.h>
170
+ #include <ATen/ops/cumprod_backward_compositeimplicitautograd_dispatch.h>
171
+ #include <ATen/ops/cumsum_compositeimplicitautograd_dispatch.h>
172
+ #include <ATen/ops/cumulative_trapezoid_compositeimplicitautograd_dispatch.h>
173
+ #include <ATen/ops/data_compositeimplicitautograd_dispatch.h>
174
+ #include <ATen/ops/det_compositeimplicitautograd_dispatch.h>
175
+ #include <ATen/ops/diag_compositeimplicitautograd_dispatch.h>
176
+ #include <ATen/ops/diagflat_compositeimplicitautograd_dispatch.h>
177
+ #include <ATen/ops/diagonal_compositeimplicitautograd_dispatch.h>
178
+ #include <ATen/ops/diff_compositeimplicitautograd_dispatch.h>
179
+ #include <ATen/ops/divide_compositeimplicitautograd_dispatch.h>
180
+ #include <ATen/ops/dropout_compositeimplicitautograd_dispatch.h>
181
+ #include <ATen/ops/dsplit_compositeimplicitautograd_dispatch.h>
182
+ #include <ATen/ops/dstack_compositeimplicitautograd_dispatch.h>
183
+ #include <ATen/ops/einsum_compositeimplicitautograd_dispatch.h>
184
+ #include <ATen/ops/embedding_backward_compositeimplicitautograd_dispatch.h>
185
+ #include <ATen/ops/embedding_bag_compositeimplicitautograd_dispatch.h>
186
+ #include <ATen/ops/embedding_sparse_backward_compositeimplicitautograd_dispatch.h>
187
+ #include <ATen/ops/empty_compositeimplicitautograd_dispatch.h>
188
+ #include <ATen/ops/expand_as_compositeimplicitautograd_dispatch.h>
189
+ #include <ATen/ops/fake_quantize_per_channel_affine_compositeimplicitautograd_dispatch.h>
190
+ #include <ATen/ops/fake_quantize_per_channel_affine_cachemask_backward_compositeimplicitautograd_dispatch.h>
191
+ #include <ATen/ops/fake_quantize_per_tensor_affine_compositeimplicitautograd_dispatch.h>
192
+ #include <ATen/ops/fake_quantize_per_tensor_affine_cachemask_backward_compositeimplicitautograd_dispatch.h>
193
+ #include <ATen/ops/fbgemm_linear_fp16_weight_compositeimplicitautograd_dispatch.h>
194
+ #include <ATen/ops/fbgemm_linear_fp16_weight_fp32_activation_compositeimplicitautograd_dispatch.h>
195
+ #include <ATen/ops/fbgemm_linear_int8_weight_compositeimplicitautograd_dispatch.h>
196
+ #include <ATen/ops/fbgemm_linear_int8_weight_fp32_activation_compositeimplicitautograd_dispatch.h>
197
+ #include <ATen/ops/fbgemm_linear_quantize_weight_compositeimplicitautograd_dispatch.h>
198
+ #include <ATen/ops/fbgemm_pack_gemm_matrix_fp16_compositeimplicitautograd_dispatch.h>
199
+ #include <ATen/ops/fbgemm_pack_quantized_matrix_compositeimplicitautograd_dispatch.h>
200
+ #include <ATen/ops/feature_alpha_dropout_compositeimplicitautograd_dispatch.h>
201
+ #include <ATen/ops/feature_dropout_compositeimplicitautograd_dispatch.h>
202
+ #include <ATen/ops/fft_fft_compositeimplicitautograd_dispatch.h>
203
+ #include <ATen/ops/fft_fft2_compositeimplicitautograd_dispatch.h>
204
+ #include <ATen/ops/fft_fftn_compositeimplicitautograd_dispatch.h>
205
+ #include <ATen/ops/fft_fftshift_compositeimplicitautograd_dispatch.h>
206
+ #include <ATen/ops/fft_hfft_compositeimplicitautograd_dispatch.h>
207
+ #include <ATen/ops/fft_hfft2_compositeimplicitautograd_dispatch.h>
208
+ #include <ATen/ops/fft_hfftn_compositeimplicitautograd_dispatch.h>
209
+ #include <ATen/ops/fft_ifft_compositeimplicitautograd_dispatch.h>
210
+ #include <ATen/ops/fft_ifft2_compositeimplicitautograd_dispatch.h>
211
+ #include <ATen/ops/fft_ifftn_compositeimplicitautograd_dispatch.h>
212
+ #include <ATen/ops/fft_ifftshift_compositeimplicitautograd_dispatch.h>
213
+ #include <ATen/ops/fft_ihfft_compositeimplicitautograd_dispatch.h>
214
+ #include <ATen/ops/fft_ihfft2_compositeimplicitautograd_dispatch.h>
215
+ #include <ATen/ops/fft_ihfftn_compositeimplicitautograd_dispatch.h>
216
+ #include <ATen/ops/fft_irfft_compositeimplicitautograd_dispatch.h>
217
+ #include <ATen/ops/fft_irfft2_compositeimplicitautograd_dispatch.h>
218
+ #include <ATen/ops/fft_irfftn_compositeimplicitautograd_dispatch.h>
219
+ #include <ATen/ops/fft_rfft_compositeimplicitautograd_dispatch.h>
220
+ #include <ATen/ops/fft_rfft2_compositeimplicitautograd_dispatch.h>
221
+ #include <ATen/ops/fft_rfftn_compositeimplicitautograd_dispatch.h>
222
+ #include <ATen/ops/fill_diagonal_compositeimplicitautograd_dispatch.h>
223
+ #include <ATen/ops/fix_compositeimplicitautograd_dispatch.h>
224
+ #include <ATen/ops/flatten_compositeimplicitautograd_dispatch.h>
225
+ #include <ATen/ops/flatten_dense_tensors_compositeimplicitautograd_dispatch.h>
226
+ #include <ATen/ops/fliplr_compositeimplicitautograd_dispatch.h>
227
+ #include <ATen/ops/flipud_compositeimplicitautograd_dispatch.h>
228
+ #include <ATen/ops/float_power_compositeimplicitautograd_dispatch.h>
229
+ #include <ATen/ops/frobenius_norm_compositeimplicitautograd_dispatch.h>
230
+ #include <ATen/ops/fused_moving_avg_obs_fake_quant_compositeimplicitautograd_dispatch.h>
231
+ #include <ATen/ops/gather_compositeimplicitautograd_dispatch.h>
232
+ #include <ATen/ops/gather_backward_compositeimplicitautograd_dispatch.h>
233
+ #include <ATen/ops/ger_compositeimplicitautograd_dispatch.h>
234
+ #include <ATen/ops/gradient_compositeimplicitautograd_dispatch.h>
235
+ #include <ATen/ops/greater_compositeimplicitautograd_dispatch.h>
236
+ #include <ATen/ops/greater_equal_compositeimplicitautograd_dispatch.h>
237
+ #include <ATen/ops/grid_sampler_compositeimplicitautograd_dispatch.h>
238
+ #include <ATen/ops/group_norm_compositeimplicitautograd_dispatch.h>
239
+ #include <ATen/ops/gru_compositeimplicitautograd_dispatch.h>
240
+ #include <ATen/ops/gru_cell_compositeimplicitautograd_dispatch.h>
241
+ #include <ATen/ops/hinge_embedding_loss_compositeimplicitautograd_dispatch.h>
242
+ #include <ATen/ops/histogramdd_compositeimplicitautograd_dispatch.h>
243
+ #include <ATen/ops/hsplit_compositeimplicitautograd_dispatch.h>
244
+ #include <ATen/ops/hstack_compositeimplicitautograd_dispatch.h>
245
+ #include <ATen/ops/imag_compositeimplicitautograd_dispatch.h>
246
+ #include <ATen/ops/index_add_compositeimplicitautograd_dispatch.h>
247
+ #include <ATen/ops/index_copy_compositeimplicitautograd_dispatch.h>
248
+ #include <ATen/ops/index_fill_compositeimplicitautograd_dispatch.h>
249
+ #include <ATen/ops/index_select_compositeimplicitautograd_dispatch.h>
250
+ #include <ATen/ops/index_select_backward_compositeimplicitautograd_dispatch.h>
251
+ #include <ATen/ops/infinitely_differentiable_gelu_backward_compositeimplicitautograd_dispatch.h>
252
+ #include <ATen/ops/inner_compositeimplicitautograd_dispatch.h>
253
+ #include <ATen/ops/instance_norm_compositeimplicitautograd_dispatch.h>
254
+ #include <ATen/ops/inverse_compositeimplicitautograd_dispatch.h>
255
+ #include <ATen/ops/is_complex_compositeimplicitautograd_dispatch.h>
256
+ #include <ATen/ops/is_conj_compositeimplicitautograd_dispatch.h>
257
+ #include <ATen/ops/is_distributed_compositeimplicitautograd_dispatch.h>
258
+ #include <ATen/ops/is_floating_point_compositeimplicitautograd_dispatch.h>
259
+ #include <ATen/ops/is_inference_compositeimplicitautograd_dispatch.h>
260
+ #include <ATen/ops/is_leaf_compositeimplicitautograd_dispatch.h>
261
+ #include <ATen/ops/is_neg_compositeimplicitautograd_dispatch.h>
262
+ #include <ATen/ops/is_nonzero_compositeimplicitautograd_dispatch.h>
263
+ #include <ATen/ops/is_signed_compositeimplicitautograd_dispatch.h>
264
+ #include <ATen/ops/is_vulkan_available_compositeimplicitautograd_dispatch.h>
265
+ #include <ATen/ops/isclose_compositeimplicitautograd_dispatch.h>
266
+ #include <ATen/ops/isfinite_compositeimplicitautograd_dispatch.h>
267
+ #include <ATen/ops/isreal_compositeimplicitautograd_dispatch.h>
268
+ #include <ATen/ops/istft_compositeimplicitautograd_dispatch.h>
269
+ #include <ATen/ops/item_compositeimplicitautograd_dispatch.h>
270
+ #include <ATen/ops/kl_div_compositeimplicitautograd_dispatch.h>
271
+ #include <ATen/ops/kron_compositeimplicitautograd_dispatch.h>
272
+ #include <ATen/ops/kthvalue_compositeimplicitautograd_dispatch.h>
273
+ #include <ATen/ops/l1_loss_compositeimplicitautograd_dispatch.h>
274
+ #include <ATen/ops/layer_norm_compositeimplicitautograd_dispatch.h>
275
+ #include <ATen/ops/ldexp_compositeimplicitautograd_dispatch.h>
276
+ #include <ATen/ops/less_compositeimplicitautograd_dispatch.h>
277
+ #include <ATen/ops/less_equal_compositeimplicitautograd_dispatch.h>
278
+ #include <ATen/ops/linalg_cholesky_compositeimplicitautograd_dispatch.h>
279
+ #include <ATen/ops/linalg_cond_compositeimplicitautograd_dispatch.h>
280
+ #include <ATen/ops/linalg_det_compositeimplicitautograd_dispatch.h>
281
+ #include <ATen/ops/linalg_diagonal_compositeimplicitautograd_dispatch.h>
282
+ #include <ATen/ops/linalg_eigh_compositeimplicitautograd_dispatch.h>
283
+ #include <ATen/ops/linalg_eigvals_compositeimplicitautograd_dispatch.h>
284
+ #include <ATen/ops/linalg_eigvalsh_compositeimplicitautograd_dispatch.h>
285
+ #include <ATen/ops/linalg_inv_compositeimplicitautograd_dispatch.h>
286
+ #include <ATen/ops/linalg_ldl_factor_compositeimplicitautograd_dispatch.h>
287
+ #include <ATen/ops/linalg_lu_factor_compositeimplicitautograd_dispatch.h>
288
+ #include <ATen/ops/linalg_matmul_compositeimplicitautograd_dispatch.h>
289
+ #include <ATen/ops/linalg_matrix_norm_compositeimplicitautograd_dispatch.h>
290
+ #include <ATen/ops/linalg_matrix_power_compositeimplicitautograd_dispatch.h>
291
+ #include <ATen/ops/linalg_matrix_rank_compositeimplicitautograd_dispatch.h>
292
+ #include <ATen/ops/linalg_multi_dot_compositeimplicitautograd_dispatch.h>
293
+ #include <ATen/ops/linalg_norm_compositeimplicitautograd_dispatch.h>
294
+ #include <ATen/ops/linalg_pinv_compositeimplicitautograd_dispatch.h>
295
+ #include <ATen/ops/linalg_slogdet_compositeimplicitautograd_dispatch.h>
296
+ #include <ATen/ops/linalg_solve_compositeimplicitautograd_dispatch.h>
297
+ #include <ATen/ops/linalg_solve_ex_compositeimplicitautograd_dispatch.h>
298
+ #include <ATen/ops/linalg_svd_compositeimplicitautograd_dispatch.h>
299
+ #include <ATen/ops/linalg_svdvals_compositeimplicitautograd_dispatch.h>
300
+ #include <ATen/ops/linalg_tensorinv_compositeimplicitautograd_dispatch.h>
301
+ #include <ATen/ops/linalg_tensorsolve_compositeimplicitautograd_dispatch.h>
302
+ #include <ATen/ops/linalg_vander_compositeimplicitautograd_dispatch.h>
303
+ #include <ATen/ops/linalg_vecdot_compositeimplicitautograd_dispatch.h>
304
+ #include <ATen/ops/linear_compositeimplicitautograd_dispatch.h>
305
+ #include <ATen/ops/log_sigmoid_compositeimplicitautograd_dispatch.h>
306
+ #include <ATen/ops/log_softmax_compositeimplicitautograd_dispatch.h>
307
+ #include <ATen/ops/logcumsumexp_compositeimplicitautograd_dispatch.h>
308
+ #include <ATen/ops/logdet_compositeimplicitautograd_dispatch.h>
309
+ #include <ATen/ops/logsumexp_compositeimplicitautograd_dispatch.h>
310
+ #include <ATen/ops/lstm_compositeimplicitautograd_dispatch.h>
311
+ #include <ATen/ops/lstm_cell_compositeimplicitautograd_dispatch.h>
312
+ #include <ATen/ops/lu_solve_compositeimplicitautograd_dispatch.h>
313
+ #include <ATen/ops/mH_compositeimplicitautograd_dispatch.h>
314
+ #include <ATen/ops/mT_compositeimplicitautograd_dispatch.h>
315
+ #include <ATen/ops/margin_ranking_loss_compositeimplicitautograd_dispatch.h>
316
+ #include <ATen/ops/masked_select_backward_compositeimplicitautograd_dispatch.h>
317
+ #include <ATen/ops/matmul_compositeimplicitautograd_dispatch.h>
318
+ #include <ATen/ops/matrix_H_compositeimplicitautograd_dispatch.h>
319
+ #include <ATen/ops/matrix_exp_compositeimplicitautograd_dispatch.h>
320
+ #include <ATen/ops/matrix_exp_backward_compositeimplicitautograd_dispatch.h>
321
+ #include <ATen/ops/matrix_power_compositeimplicitautograd_dispatch.h>
322
+ #include <ATen/ops/max_compositeimplicitautograd_dispatch.h>
323
+ #include <ATen/ops/max_pool1d_compositeimplicitautograd_dispatch.h>
324
+ #include <ATen/ops/max_pool1d_with_indices_compositeimplicitautograd_dispatch.h>
325
+ #include <ATen/ops/max_pool2d_compositeimplicitautograd_dispatch.h>
326
+ #include <ATen/ops/max_pool3d_compositeimplicitautograd_dispatch.h>
327
+ #include <ATen/ops/mean_compositeimplicitautograd_dispatch.h>
328
+ #include <ATen/ops/median_compositeimplicitautograd_dispatch.h>
329
+ #include <ATen/ops/meshgrid_compositeimplicitautograd_dispatch.h>
330
+ #include <ATen/ops/min_compositeimplicitautograd_dispatch.h>
331
+ #include <ATen/ops/mish_backward_compositeimplicitautograd_dispatch.h>
332
+ #include <ATen/ops/mode_compositeimplicitautograd_dispatch.h>
333
+ #include <ATen/ops/moveaxis_compositeimplicitautograd_dispatch.h>
334
+ #include <ATen/ops/movedim_compositeimplicitautograd_dispatch.h>
335
+ #include <ATen/ops/msort_compositeimplicitautograd_dispatch.h>
336
+ #include <ATen/ops/multilabel_margin_loss_compositeimplicitautograd_dispatch.h>
337
+ #include <ATen/ops/multiply_compositeimplicitautograd_dispatch.h>
338
+ #include <ATen/ops/nanmean_compositeimplicitautograd_dispatch.h>
339
+ #include <ATen/ops/nanmedian_compositeimplicitautograd_dispatch.h>
340
+ #include <ATen/ops/nanquantile_compositeimplicitautograd_dispatch.h>
341
+ #include <ATen/ops/narrow_compositeimplicitautograd_dispatch.h>
342
+ #include <ATen/ops/native_channel_shuffle_compositeimplicitautograd_dispatch.h>
343
+ #include <ATen/ops/negative_compositeimplicitautograd_dispatch.h>
344
+ #include <ATen/ops/nested_to_padded_tensor_compositeimplicitautograd_dispatch.h>
345
+ #include <ATen/ops/nll_loss_compositeimplicitautograd_dispatch.h>
346
+ #include <ATen/ops/nll_loss2d_compositeimplicitautograd_dispatch.h>
347
+ #include <ATen/ops/nll_loss_nd_compositeimplicitautograd_dispatch.h>
348
+ #include <ATen/ops/nonzero_numpy_compositeimplicitautograd_dispatch.h>
349
+ #include <ATen/ops/norm_compositeimplicitautograd_dispatch.h>
350
+ #include <ATen/ops/norm_except_dim_compositeimplicitautograd_dispatch.h>
351
+ #include <ATen/ops/not_equal_compositeimplicitautograd_dispatch.h>
352
+ #include <ATen/ops/nuclear_norm_compositeimplicitautograd_dispatch.h>
353
+ #include <ATen/ops/numpy_T_compositeimplicitautograd_dispatch.h>
354
+ #include <ATen/ops/one_hot_compositeimplicitautograd_dispatch.h>
355
+ #include <ATen/ops/or_compositeimplicitautograd_dispatch.h>
356
+ #include <ATen/ops/orgqr_compositeimplicitautograd_dispatch.h>
357
+ #include <ATen/ops/outer_compositeimplicitautograd_dispatch.h>
358
+ #include <ATen/ops/output_nr_compositeimplicitautograd_dispatch.h>
359
+ #include <ATen/ops/pad_compositeimplicitautograd_dispatch.h>
360
+ #include <ATen/ops/pad_sequence_compositeimplicitautograd_dispatch.h>
361
+ #include <ATen/ops/pairwise_distance_compositeimplicitautograd_dispatch.h>
362
+ #include <ATen/ops/pdist_compositeimplicitautograd_dispatch.h>
363
+ #include <ATen/ops/pin_memory_compositeimplicitautograd_dispatch.h>
364
+ #include <ATen/ops/pinverse_compositeimplicitautograd_dispatch.h>
365
+ #include <ATen/ops/poisson_nll_loss_compositeimplicitautograd_dispatch.h>
366
+ #include <ATen/ops/positive_compositeimplicitautograd_dispatch.h>
367
+ #include <ATen/ops/prelu_compositeimplicitautograd_dispatch.h>
368
+ #include <ATen/ops/prod_compositeimplicitautograd_dispatch.h>
369
+ #include <ATen/ops/promote_types_compositeimplicitautograd_dispatch.h>
370
+ #include <ATen/ops/qr_compositeimplicitautograd_dispatch.h>
371
+ #include <ATen/ops/quantile_compositeimplicitautograd_dispatch.h>
372
+ #include <ATen/ops/quantized_gru_cell_compositeimplicitautograd_dispatch.h>
373
+ #include <ATen/ops/quantized_lstm_cell_compositeimplicitautograd_dispatch.h>
374
+ #include <ATen/ops/quantized_rnn_relu_cell_compositeimplicitautograd_dispatch.h>
375
+ #include <ATen/ops/quantized_rnn_tanh_cell_compositeimplicitautograd_dispatch.h>
376
+ #include <ATen/ops/rand_compositeimplicitautograd_dispatch.h>
377
+ #include <ATen/ops/randn_compositeimplicitautograd_dispatch.h>
378
+ #include <ATen/ops/ravel_compositeimplicitautograd_dispatch.h>
379
+ #include <ATen/ops/real_compositeimplicitautograd_dispatch.h>
380
+ #include <ATen/ops/refine_names_compositeimplicitautograd_dispatch.h>
381
+ #include <ATen/ops/relu6_compositeimplicitautograd_dispatch.h>
382
+ #include <ATen/ops/rename_compositeimplicitautograd_dispatch.h>
383
+ #include <ATen/ops/repeat_interleave_compositeimplicitautograd_dispatch.h>
384
+ #include <ATen/ops/requires_grad_compositeimplicitautograd_dispatch.h>
385
+ #include <ATen/ops/reshape_compositeimplicitautograd_dispatch.h>
386
+ #include <ATen/ops/reshape_as_compositeimplicitautograd_dispatch.h>
387
+ #include <ATen/ops/resolve_conj_compositeimplicitautograd_dispatch.h>
388
+ #include <ATen/ops/resolve_neg_compositeimplicitautograd_dispatch.h>
389
+ #include <ATen/ops/result_type_compositeimplicitautograd_dispatch.h>
390
+ #include <ATen/ops/retain_grad_compositeimplicitautograd_dispatch.h>
391
+ #include <ATen/ops/retains_grad_compositeimplicitautograd_dispatch.h>
392
+ #include <ATen/ops/rms_norm_compositeimplicitautograd_dispatch.h>
393
+ #include <ATen/ops/rnn_relu_compositeimplicitautograd_dispatch.h>
394
+ #include <ATen/ops/rnn_relu_cell_compositeimplicitautograd_dispatch.h>
395
+ #include <ATen/ops/rnn_tanh_compositeimplicitautograd_dispatch.h>
396
+ #include <ATen/ops/rnn_tanh_cell_compositeimplicitautograd_dispatch.h>
397
+ #include <ATen/ops/row_stack_compositeimplicitautograd_dispatch.h>
398
+ #include <ATen/ops/rrelu_compositeimplicitautograd_dispatch.h>
399
+ #include <ATen/ops/scaled_dot_product_attention_compositeimplicitautograd_dispatch.h>
400
+ #include <ATen/ops/scatter_compositeimplicitautograd_dispatch.h>
401
+ #include <ATen/ops/scatter_add_compositeimplicitautograd_dispatch.h>
402
+ #include <ATen/ops/select_compositeimplicitautograd_dispatch.h>
403
+ #include <ATen/ops/selu_compositeimplicitautograd_dispatch.h>
404
+ #include <ATen/ops/set_compositeimplicitautograd_dispatch.h>
405
+ #include <ATen/ops/set_data_compositeimplicitautograd_dispatch.h>
406
+ #include <ATen/ops/silu_backward_compositeimplicitautograd_dispatch.h>
407
+ #include <ATen/ops/size_compositeimplicitautograd_dispatch.h>
408
+ #include <ATen/ops/slogdet_compositeimplicitautograd_dispatch.h>
409
+ #include <ATen/ops/slow_conv3d_compositeimplicitautograd_dispatch.h>
410
+ #include <ATen/ops/smm_compositeimplicitautograd_dispatch.h>
411
+ #include <ATen/ops/softmax_compositeimplicitautograd_dispatch.h>
412
+ #include <ATen/ops/sort_compositeimplicitautograd_dispatch.h>
413
+ #include <ATen/ops/sparse_bsc_tensor_compositeimplicitautograd_dispatch.h>
414
+ #include <ATen/ops/sparse_bsr_tensor_compositeimplicitautograd_dispatch.h>
415
+ #include <ATen/ops/sparse_coo_tensor_compositeimplicitautograd_dispatch.h>
416
+ #include <ATen/ops/sparse_csc_tensor_compositeimplicitautograd_dispatch.h>
417
+ #include <ATen/ops/sparse_csr_tensor_compositeimplicitautograd_dispatch.h>
418
+ #include <ATen/ops/special_digamma_compositeimplicitautograd_dispatch.h>
419
+ #include <ATen/ops/special_erf_compositeimplicitautograd_dispatch.h>
420
+ #include <ATen/ops/special_erfc_compositeimplicitautograd_dispatch.h>
421
+ #include <ATen/ops/special_erfinv_compositeimplicitautograd_dispatch.h>
422
+ #include <ATen/ops/special_exp2_compositeimplicitautograd_dispatch.h>
423
+ #include <ATen/ops/special_expit_compositeimplicitautograd_dispatch.h>
424
+ #include <ATen/ops/special_expm1_compositeimplicitautograd_dispatch.h>
425
+ #include <ATen/ops/special_gammainc_compositeimplicitautograd_dispatch.h>
426
+ #include <ATen/ops/special_gammaincc_compositeimplicitautograd_dispatch.h>
427
+ #include <ATen/ops/special_gammaln_compositeimplicitautograd_dispatch.h>
428
+ #include <ATen/ops/special_i0_compositeimplicitautograd_dispatch.h>
429
+ #include <ATen/ops/special_log1p_compositeimplicitautograd_dispatch.h>
430
+ #include <ATen/ops/special_log_softmax_compositeimplicitautograd_dispatch.h>
431
+ #include <ATen/ops/special_logit_compositeimplicitautograd_dispatch.h>
432
+ #include <ATen/ops/special_logsumexp_compositeimplicitautograd_dispatch.h>
433
+ #include <ATen/ops/special_multigammaln_compositeimplicitautograd_dispatch.h>
434
+ #include <ATen/ops/special_ndtr_compositeimplicitautograd_dispatch.h>
435
+ #include <ATen/ops/special_polygamma_compositeimplicitautograd_dispatch.h>
436
+ #include <ATen/ops/special_psi_compositeimplicitautograd_dispatch.h>
437
+ #include <ATen/ops/special_round_compositeimplicitautograd_dispatch.h>
438
+ #include <ATen/ops/special_sinc_compositeimplicitautograd_dispatch.h>
439
+ #include <ATen/ops/special_softmax_compositeimplicitautograd_dispatch.h>
440
+ #include <ATen/ops/special_xlogy_compositeimplicitautograd_dispatch.h>
441
+ #include <ATen/ops/split_compositeimplicitautograd_dispatch.h>
442
+ #include <ATen/ops/square_compositeimplicitautograd_dispatch.h>
443
+ #include <ATen/ops/squeeze_compositeimplicitautograd_dispatch.h>
444
+ #include <ATen/ops/sspaddmm_compositeimplicitautograd_dispatch.h>
445
+ #include <ATen/ops/std_compositeimplicitautograd_dispatch.h>
446
+ #include <ATen/ops/std_mean_compositeimplicitautograd_dispatch.h>
447
+ #include <ATen/ops/stft_compositeimplicitautograd_dispatch.h>
448
+ #include <ATen/ops/stride_compositeimplicitautograd_dispatch.h>
449
+ #include <ATen/ops/subtract_compositeimplicitautograd_dispatch.h>
450
+ #include <ATen/ops/sum_compositeimplicitautograd_dispatch.h>
451
+ #include <ATen/ops/sum_to_size_compositeimplicitautograd_dispatch.h>
452
+ #include <ATen/ops/svd_compositeimplicitautograd_dispatch.h>
453
+ #include <ATen/ops/swapaxes_compositeimplicitautograd_dispatch.h>
454
+ #include <ATen/ops/swapdims_compositeimplicitautograd_dispatch.h>
455
+ #include <ATen/ops/sym_is_contiguous_compositeimplicitautograd_dispatch.h>
456
+ #include <ATen/ops/sym_numel_compositeimplicitautograd_dispatch.h>
457
+ #include <ATen/ops/sym_size_compositeimplicitautograd_dispatch.h>
458
+ #include <ATen/ops/sym_storage_offset_compositeimplicitautograd_dispatch.h>
459
+ #include <ATen/ops/sym_stride_compositeimplicitautograd_dispatch.h>
460
+ #include <ATen/ops/take_along_dim_compositeimplicitautograd_dispatch.h>
461
+ #include <ATen/ops/tensor_split_compositeimplicitautograd_dispatch.h>
462
+ #include <ATen/ops/tensordot_compositeimplicitautograd_dispatch.h>
463
+ #include <ATen/ops/thnn_conv2d_compositeimplicitautograd_dispatch.h>
464
+ #include <ATen/ops/tile_compositeimplicitautograd_dispatch.h>
465
+ #include <ATen/ops/to_compositeimplicitautograd_dispatch.h>
466
+ #include <ATen/ops/to_dense_compositeimplicitautograd_dispatch.h>
467
+ #include <ATen/ops/to_dense_backward_compositeimplicitautograd_dispatch.h>
468
+ #include <ATen/ops/to_mkldnn_backward_compositeimplicitautograd_dispatch.h>
469
+ #include <ATen/ops/to_sparse_compositeimplicitautograd_dispatch.h>
470
+ #include <ATen/ops/to_sparse_bsc_compositeimplicitautograd_dispatch.h>
471
+ #include <ATen/ops/to_sparse_bsr_compositeimplicitautograd_dispatch.h>
472
+ #include <ATen/ops/to_sparse_csc_compositeimplicitautograd_dispatch.h>
473
+ #include <ATen/ops/to_sparse_csr_compositeimplicitautograd_dispatch.h>
474
+ #include <ATen/ops/trace_backward_compositeimplicitautograd_dispatch.h>
475
+ #include <ATen/ops/transpose_compositeimplicitautograd_dispatch.h>
476
+ #include <ATen/ops/trapezoid_compositeimplicitautograd_dispatch.h>
477
+ #include <ATen/ops/trapz_compositeimplicitautograd_dispatch.h>
478
+ #include <ATen/ops/triplet_margin_loss_compositeimplicitautograd_dispatch.h>
479
+ #include <ATen/ops/true_divide_compositeimplicitautograd_dispatch.h>
480
+ #include <ATen/ops/type_as_compositeimplicitautograd_dispatch.h>
481
+ #include <ATen/ops/unbind_compositeimplicitautograd_dispatch.h>
482
+ #include <ATen/ops/unflatten_compositeimplicitautograd_dispatch.h>
483
+ #include <ATen/ops/unflatten_dense_tensors_compositeimplicitautograd_dispatch.h>
484
+ #include <ATen/ops/unsafe_chunk_compositeimplicitautograd_dispatch.h>
485
+ #include <ATen/ops/upsample_bicubic2d_compositeimplicitautograd_dispatch.h>
486
+ #include <ATen/ops/upsample_bilinear2d_compositeimplicitautograd_dispatch.h>
487
+ #include <ATen/ops/upsample_linear1d_compositeimplicitautograd_dispatch.h>
488
+ #include <ATen/ops/upsample_nearest1d_compositeimplicitautograd_dispatch.h>
489
+ #include <ATen/ops/upsample_nearest2d_compositeimplicitautograd_dispatch.h>
490
+ #include <ATen/ops/upsample_nearest3d_compositeimplicitautograd_dispatch.h>
491
+ #include <ATen/ops/upsample_trilinear3d_compositeimplicitautograd_dispatch.h>
492
+ #include <ATen/ops/value_selecting_reduction_backward_compositeimplicitautograd_dispatch.h>
493
+ #include <ATen/ops/vander_compositeimplicitautograd_dispatch.h>
494
+ #include <ATen/ops/var_compositeimplicitautograd_dispatch.h>
495
+ #include <ATen/ops/var_mean_compositeimplicitautograd_dispatch.h>
496
+ #include <ATen/ops/view_as_compositeimplicitautograd_dispatch.h>
497
+ #include <ATen/ops/vsplit_compositeimplicitautograd_dispatch.h>
498
+ #include <ATen/ops/vstack_compositeimplicitautograd_dispatch.h>
499
+ #include <ATen/ops/where_compositeimplicitautograd_dispatch.h>
500
+ #include <ATen/ops/xor_compositeimplicitautograd_dispatch.h>
501
+
502
+
503
+
lib/python3.12/site-packages/torch/include/ATen/CompositeImplicitAutogradNestedTensorFunctions.h ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <ATen/core/TensorBody.h>
2
+
3
+ // TODO Undo all logic introduced for Note [Avoiding Include Cycles In Static Dispatch]
4
+ // Code introduced to avoid cyclic dependency in static dispatch is no longer
5
+ // needed as static dispatch logic is moved from TensorBody.h, which caused cycles in the first place,
6
+ // to Operators.cpp for supporting multiple backends with multiple kernels.
7
+ //
8
+ // Note [Avoiding Include Cycles In Static Dispatch]
9
+ // In order to avoid #include cycles in the static dispatch build, we've carefully split out
10
+ // the static function definition files into {DispatchKey}Functions.h and {DispatchKey}Functions_inl.h.
11
+ //
12
+ // Without this split, the include cycle looks like TensorBody.h -> CPUFunctions.h -> TensorBody.h.
13
+ // - TensorBody.h #includes CPUFunctions.h in the static dispatch build, because the tensor methods
14
+ // all need to call into the fastpath C++ API defined in CPUFunctions.h. The methods are also all
15
+ // directly inlined into TensorBody.h.
16
+ // - CPUFunctions.h #includes TensorBody.h because it contains function declarations for the entire C++ API,
17
+ // which include functions that have defaultable std::optional<Tensor> arguments.
18
+ // That requires knowing the full Tensor class definition.
19
+ //
20
+ // We break the cycle by doing the following:
21
+ // - Split out CPUFunction.h into two files: CPUFunctions.h and CPUFunctions_inl.h
22
+ // - CPUFunction.h is a dummy file that just includes the Tensor class and includes CPUFunctions_inl.,
23
+ // - CPUFunctions_inl.h includes everything else
24
+ // - (only in the static dispatch build) TensorBody.h makes sure to finish defining the Tensor class,
25
+ // and then it includes CPUFunctions_inl.h.
26
+ // - All other files that want the cpu fastpath functions can include CPUFunctions.h directly.
27
+ // - This also means that static dispatch build, CPUFunctions.h only needs to
28
+ // #include TensorBody.h, and it will automatically bring in CPUFunctions_inl.h.
29
+ #include <ATen/CompositeImplicitAutogradNestedTensorFunctions_inl.h>
lib/python3.12/site-packages/torch/include/ATen/CompositeImplicitAutogradNestedTensorFunctions_inl.h ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+ // @generated by torchgen/gen.py from DispatchKeyFunctions_inl.h
3
+
4
+ // NB: The implementing C++ file is RegisterDispatchKey.cpp
5
+
6
+ // The only #includes we need are for custom classes that have defaults in the C++ API
7
+ #include <c10/core/MemoryFormat.h>
8
+ #include <c10/core/Scalar.h>
9
+ #include <ATen/core/Reduction.h>
10
+
11
+ #if defined(AT_PER_OPERATOR_HEADERS) && defined(TORCH_ASSERT_ONLY_METHOD_OPERATORS)
12
+ #error This change adds a dependency on all pytorch operators, meaning the \
13
+ file will need to be re-compiled every time an operator is changed or added. \
14
+ Consider including a specific operator from \
15
+ <ATen/ops/{my_operator}_compositeimplicitautogradnestedtensor_dispatch.h>. \
16
+ See NOTE [TORCH_ASSERT_ONLY_METHOD_OPERATORS].
17
+ #endif
18
+
19
+ #include <ATen/ops/randn_like_compositeimplicitautogradnestedtensor_dispatch.h>
20
+ #include <ATen/ops/reshape_compositeimplicitautogradnestedtensor_dispatch.h>
21
+ #include <ATen/ops/reshape_as_compositeimplicitautogradnestedtensor_dispatch.h>
22
+ #include <ATen/ops/zeros_like_compositeimplicitautogradnestedtensor_dispatch.h>
23
+
24
+
25
+
lib/python3.12/site-packages/torch/include/ATen/Config.h ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ // Test these using #if AT_MKL_ENABLED(), not #ifdef, so that it's
4
+ // obvious if you forgot to include Config.h
5
+ // c.f. https://stackoverflow.com/questions/33759787/generating-an-error-if-checked-boolean-macro-is-not-defined
6
+ //
7
+ // DO NOT put the macros for CUDA libraries in this file; they belong in cuda/CUDAConfig.h
8
+
9
+ #define AT_MKLDNN_ENABLED() 1
10
+ #define AT_MKLDNN_ACL_ENABLED() 0
11
+ #define AT_MKL_ENABLED() 1
12
+ #define AT_MKL_SEQUENTIAL() 0
13
+ #define AT_POCKETFFT_ENABLED() 0
14
+ #define AT_NNPACK_ENABLED() 1
15
+ #define CAFFE2_STATIC_LINK_CUDA() 0
16
+ #define AT_BUILD_WITH_BLAS() 1
17
+ #define AT_BUILD_WITH_LAPACK() 1
18
+ #define AT_PARALLEL_OPENMP 1
19
+ #define AT_PARALLEL_NATIVE 0
20
+ #define AT_BLAS_F2C() 0
21
+ #define AT_BLAS_USE_CBLAS_DOT() 0
22
+ #define AT_KLEIDIAI_ENABLED() 0
23
+ #define AT_USE_EIGEN_SPARSE() 0
lib/python3.12/site-packages/torch/include/ATen/Context.h ADDED
@@ -0,0 +1,688 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <ATen/BlasBackend.h>
4
+ #include <ATen/CPUGeneratorImpl.h>
5
+ #include <ATen/DeviceAccelerator.h>
6
+ #include <ATen/LinalgBackend.h>
7
+ #include <ATen/ROCmFABackend.h>
8
+ #include <ATen/SDPBackend.h>
9
+ #include <ATen/core/ATenGeneral.h>
10
+ #include <ATen/core/DeprecatedTypeProperties.h>
11
+ #include <ATen/core/Generator.h>
12
+ #include <ATen/core/LegacyTypeDispatch.h>
13
+ #include <ATen/detail/AcceleratorHooksInterface.h>
14
+ #include <ATen/detail/CUDAHooksInterface.h>
15
+ #include <ATen/detail/HIPHooksInterface.h>
16
+ #include <ATen/detail/HPUHooksInterface.h>
17
+ #include <ATen/detail/IPUHooksInterface.h>
18
+ #include <ATen/detail/MAIAHooksInterface.h>
19
+ #include <ATen/detail/MPSHooksInterface.h>
20
+ #include <ATen/detail/MTIAHooksInterface.h>
21
+ #include <ATen/detail/PrivateUse1HooksInterface.h>
22
+ #include <ATen/detail/XPUHooksInterface.h>
23
+ #include <c10/core/QEngine.h>
24
+ #include <c10/core/impl/DeviceGuardImplInterface.h>
25
+ #include <c10/util/CallOnce.h>
26
+ #include <c10/util/Exception.h>
27
+ #include <c10/util/env.h>
28
+ #include <c10/util/irange.h>
29
+
30
+ #include <cstdint>
31
+ #include <map>
32
+ #include <mutex>
33
+
34
+ namespace at {
35
+
36
+ class Tensor;
37
+
38
+ enum class TORCH_API Float32MatmulPrecision { HIGHEST, HIGH, MEDIUM };
39
+
40
+ class TORCH_API Context {
41
+ public:
42
+ Context();
43
+
44
+ const Generator& defaultGenerator(Device device) {
45
+ c10::DeviceType device_type = device.type();
46
+ lazyInitDevice(device_type);
47
+
48
+ if (device_type == at::kCPU) {
49
+ return at::detail::getDefaultCPUGenerator();
50
+ } else {
51
+ return getAcceleratorHooksInterface(device_type)
52
+ .getDefaultGenerator(device.index());
53
+ }
54
+ }
55
+
56
+ const AcceleratorHooksInterface& getAcceleratorHooksInterface(
57
+ std::optional<c10::DeviceType> opt_device_type = std::nullopt) {
58
+ if (!opt_device_type.has_value()) {
59
+ opt_device_type = at::getAccelerator(true);
60
+ }
61
+ if (opt_device_type == at::kCUDA) {
62
+ return at::detail::getCUDAHooks();
63
+ } else if (opt_device_type == at::kXPU) {
64
+ return at::detail::getXPUHooks();
65
+ } else if (opt_device_type == at::kMPS) {
66
+ return at::detail::getMPSHooks();
67
+ } else if (opt_device_type == at::kPrivateUse1) {
68
+ return at::detail::getPrivateUse1Hooks();
69
+ } else if (opt_device_type == at::kMTIA) {
70
+ return at::detail::getMTIAHooks();
71
+ } else if (opt_device_type == at::kHIP) {
72
+ return at::detail::getHIPHooks();
73
+ } else if (opt_device_type == at::kHPU) {
74
+ return at::detail::getHPUHooks();
75
+ } else {
76
+ TORCH_CHECK(
77
+ false,
78
+ opt_device_type.has_value()
79
+ ? c10::DeviceTypeName(opt_device_type.value())
80
+ : "None",
81
+ " device type not an accelerator.");
82
+ }
83
+ }
84
+
85
+ Device getDeviceFromPtr(void* data, c10::DeviceType device_type) {
86
+ lazyInitDevice(device_type);
87
+
88
+ if (device_type == at::kCPU) {
89
+ return c10::DeviceType::CPU;
90
+ } else {
91
+ return getAcceleratorHooksInterface(device_type).getDeviceFromPtr(data);
92
+ }
93
+ }
94
+
95
+ bool isPinnedPtr(
96
+ const void* data,
97
+ std::optional<c10::DeviceType> device_type = std::nullopt) {
98
+ auto opt_device_type =
99
+ device_type.has_value() ? device_type : at::getAccelerator();
100
+ if (!opt_device_type.has_value() || // there is no accelerator
101
+ !at::isAccelerator(
102
+ opt_device_type.value())) { // passed device not an accelerator
103
+ return false;
104
+ }
105
+ if (!init_[static_cast<int8_t>(opt_device_type.value())].test_once()) {
106
+ // If the device is not initialized, no pointer can be pinned for it
107
+ return false;
108
+ }
109
+ return getAcceleratorHooksInterface(opt_device_type).isPinnedPtr(data);
110
+ }
111
+
112
+ Allocator* getPinnedMemoryAllocator(
113
+ std::optional<c10::DeviceType> device_type = std::nullopt) {
114
+ auto opt_device_type =
115
+ device_type.has_value() ? device_type : at::getAccelerator();
116
+ if (opt_device_type) {
117
+ lazyInitDevice(opt_device_type.value());
118
+ }
119
+ return getAcceleratorHooksInterface(device_type).getPinnedMemoryAllocator();
120
+ }
121
+
122
+ void lazyInitDevice(c10::DeviceType device_type) {
123
+ if (device_type != at::kCPU) {
124
+ c10::call_once(init_[static_cast<int8_t>(device_type)], [&] {
125
+ getAcceleratorHooksInterface(device_type).init();
126
+ });
127
+ }
128
+ }
129
+
130
+ static bool hasOpenMP();
131
+ static bool hasMKL();
132
+ static bool hasKleidiAI();
133
+ static bool hasLAPACK();
134
+ static bool hasMKLDNN();
135
+ static bool ckSupported();
136
+ static bool hasEigenSparse();
137
+ static bool hasMAGMA() {
138
+ return detail::getCUDAHooks().hasMAGMA();
139
+ }
140
+ static bool hasCUDA() {
141
+ return detail::getCUDAHooks().hasCUDA();
142
+ }
143
+ static bool hasMTIA() {
144
+ return detail::getMTIAHooks().hasMTIA();
145
+ }
146
+ static bool hasCUDART() {
147
+ return detail::getCUDAHooks().hasCUDART();
148
+ }
149
+ static long versionCUDART() {
150
+ return detail::getCUDAHooks().versionCUDART();
151
+ }
152
+ static bool hasCuDNN() {
153
+ return detail::getCUDAHooks().hasCuDNN();
154
+ }
155
+ static long versionCuDNN() {
156
+ return detail::getCUDAHooks().versionCuDNN();
157
+ }
158
+ static bool hasCuSOLVER() {
159
+ return detail::getCUDAHooks().hasCuSOLVER();
160
+ }
161
+ static bool hasCuBLASLt() {
162
+ return detail::getCUDAHooks().hasCuBLASLt();
163
+ }
164
+ static bool hasROCM() {
165
+ return detail::getCUDAHooks().hasROCM();
166
+ }
167
+ static bool hasCKSDPA() {
168
+ return detail::getCUDAHooks().hasCKSDPA();
169
+ }
170
+ static bool hasCKGEMM() {
171
+ return detail::getCUDAHooks().hasCKGEMM();
172
+ }
173
+ static bool hasHIP() {
174
+ return detail::getHIPHooks().hasHIP();
175
+ }
176
+ static bool hasMPS() {
177
+ return detail::getMPSHooks().hasMPS();
178
+ }
179
+ static bool hasIPU() {
180
+ return c10::impl::hasDeviceGuardImpl(c10::DeviceType::IPU);
181
+ }
182
+ static bool hasXLA() {
183
+ return c10::impl::hasDeviceGuardImpl(c10::DeviceType::XLA);
184
+ }
185
+ static bool hasXPU() {
186
+ return detail::getXPUHooks().hasXPU();
187
+ }
188
+ static bool hasLazy() {
189
+ return c10::impl::hasDeviceGuardImpl(c10::DeviceType::Lazy);
190
+ }
191
+ static bool hasMAIA() {
192
+ return c10::impl::hasDeviceGuardImpl(c10::DeviceType::MAIA);
193
+ }
194
+ static bool hasHPU() {
195
+ return detail::getHPUHooks().hasHPU();
196
+ }
197
+
198
+ static const at::cuda::NVRTC& getNVRTC() {
199
+ return detail::getCUDAHooks().nvrtc();
200
+ }
201
+
202
+ static bool setFlushDenormal(bool on);
203
+
204
+ // NB: This method is *purely* whether or not a user requested
205
+ // that CuDNN was enabled, it doesn't actually say anything about
206
+ // whether or not CuDNN is actually usable. Use cudnn_is_acceptable
207
+ // to test this instead
208
+ bool userEnabledCuDNN() const;
209
+ void setUserEnabledCuDNN(bool e);
210
+ bool userEnabledMkldnn() const;
211
+ void setUserEnabledMkldnn(bool e);
212
+ bool benchmarkCuDNN() const;
213
+ void setBenchmarkCuDNN(bool);
214
+ int benchmarkLimitCuDNN() const;
215
+ void setBenchmarkLimitCuDNN(int);
216
+ bool immediateMiopen() const;
217
+ void setImmediateMiopen(bool);
218
+ bool deterministicCuDNN() const;
219
+ void setDeterministicCuDNN(bool);
220
+ bool deterministicMkldnn() const;
221
+ void setDeterministicMkldnn(bool);
222
+ bool userEnabledNNPACK() const;
223
+ void setUserEnabledNNPACK(bool e);
224
+
225
+ // Note [Disabling Fused SDP Kernels]
226
+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
227
+ // Flash and Memory Efficient SDP kernels are enabled by default.
228
+ // However, they can be disabled by setting
229
+ // at::globalContext().setUserEnabledFlashSDP(false) flag.
230
+ // This is useful for debugging purposes. For example, if you want to
231
+ // compare the performance of the flash SDP kernels with the unfused
232
+ // kernel, you can disable the flash SDP kernels. By disabling
233
+ // the math SDP kernel, you can force your code to use flash kernels.
234
+ // The math SDP kernel can be disabled by setting
235
+ // at::globalContext().setUserEnabledMathSDP(false) flag.
236
+ void setSDPPriorityOrder(const std::vector<int64_t>& order);
237
+ std::array<at::SDPBackend, at::num_sdp_backends> sDPPriorityOrder();
238
+
239
+ void setSDPUseFlash(bool);
240
+ bool userEnabledFlashSDP() const;
241
+
242
+ void setSDPUseMemEfficient(bool);
243
+ bool userEnabledMemEfficientSDP() const;
244
+
245
+ void setSDPUseMath(bool);
246
+ bool userEnabledMathSDP() const;
247
+
248
+ void setSDPUseCuDNN(bool);
249
+ bool userEnabledCuDNNSDP() const;
250
+
251
+ void setAllowFP16BF16ReductionMathSDP(bool);
252
+ bool allowFP16BF16ReductionMathSDP() const;
253
+
254
+ void setSDPUseOverrideable(bool);
255
+ bool userEnabledOverrideableSDP() const;
256
+
257
+ at::LinalgBackend linalgPreferredBackend() const;
258
+ void setLinalgPreferredBackend(at::LinalgBackend);
259
+
260
+ at::BlasBackend blasPreferredBackend();
261
+ void setBlasPreferredBackend(at::BlasBackend);
262
+
263
+ at::ROCmFABackend getROCmFAPreferredBackend();
264
+ void setROCmFAPreferredBackend(at::ROCmFABackend);
265
+
266
+ // Note [Enabling Deterministic Operations]
267
+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
268
+ // Operations in PyTorch that normally act nondeterministically, but have an
269
+ // alternate deterministic implementation, should satisfy the following
270
+ // requirements:
271
+ //
272
+ // * Include this comment: "See Note [Enabling Deterministic Operations]"
273
+ //
274
+ // * Check the value of `at::globalContext().deterministicAlgorithms()` to
275
+ // toggle
276
+ // between nondeterministic and deterministic implementations.
277
+ //
278
+ // * Have an entry in the list of PyTorch operations that toggle between
279
+ // nondeterministic
280
+ // and deterministic implementations, in the docstring of
281
+ // `use_deterministic_algorithms()` in torch/__init__.py
282
+ //
283
+ // `example_func()` below shows an example of toggling between
284
+ // nondeterministic and deterministic implementations:
285
+ //
286
+ // void example_func() {
287
+ // // See Note [Enabling Deterministic Operations]
288
+ // if (at::globalContext().deterministicAlgorithms()) {
289
+ // example_func_deterministic();
290
+ // } else {
291
+ // example_func_nondeterministic();
292
+ // }
293
+ // }
294
+
295
+ bool deterministicAlgorithms() const;
296
+ bool deterministicAlgorithmsWarnOnly() const;
297
+ void setDeterministicAlgorithms(bool, bool);
298
+ bool deterministicFillUninitializedMemory() const;
299
+ void setDeterministicFillUninitializedMemory(bool);
300
+
301
+ // Note [Writing Nondeterministic Operations]
302
+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
303
+ // Operations in PyTorch that act nondeterministically and do not have an
304
+ // alternate deterministic implementation should satisfy the following
305
+ // requirements:
306
+ //
307
+ // * Include this comment: "See Note [Writing Nondeterministic Operations]"
308
+ //
309
+ // * Include a comment explaining why the operation is nondeterministic.
310
+ //
311
+ // * Throw an error when `Context::deterministicAlgorithms()` is true. Most
312
+ // of the time, this should be accomplished by calling
313
+ // `at::globalContext().alertNotDeterminstic()`. However, if the
314
+ // nondeterministic behavior is caused by the CuBLAS workspace
315
+ // configuration in CUDA >= 10.2,
316
+ // `at::globalContext().alertCuBLASConfigNotDeterministic()` should be
317
+ // called instead (in this case, a comment explaining why the operation is
318
+ // nondeterministic is not necessary). See below for details on these
319
+ // methods.
320
+ //
321
+ // * Have an entry in the list of nondeterministic PyTorch operations in the
322
+ // docstring of `use_deterministic_algorithms()` in torch/__init__.py
323
+ //
324
+ // * Have a test function in `test/test_torch.py` whose name begins with
325
+ // `test_nondeterministic_alert_`. Alternatively, if CuBLAS workspace
326
+ // configuration is the reason for nondeterminism, the operation should be
327
+ // included in the `test_cublas_config_nondeterministic_alert` test. Any new
328
+ // tests should ideally follow a pattern similar to the existing ones.
329
+ //
330
+ // `example_func()` below shows an example of the comments and error-throwing
331
+ // code for a nondeterministic operation:
332
+ //
333
+ // void example_func() {
334
+ // // See Note [Writing Nondeterministic Operations]
335
+ // // Nondeterministic because <reason>
336
+ // at::globalContext().alertNondeterministic("example_func");
337
+ // ...
338
+ // }
339
+
340
+ // Throws an error if `Context::deterministicAlgorithms()` is true
341
+ static void alertNotDeterministic(std::string_view const& caller);
342
+
343
+ // Throws an error if `Context::deterministicAlgorithms()` is true, CUDA
344
+ // >= 10.2, and CUBLAS_WORKSPACE_CONFIG is not set to either ":16:8" or
345
+ // ":4096:8". For more details:
346
+ // https://docs.nvidia.com/cuda/cublas/index.html#results-reproducibility
347
+ void alertCuBLASConfigNotDeterministic() const;
348
+
349
+ void setFloat32MatmulPrecision(const std::string& s);
350
+ void setFloat32Precision(
351
+ const std::string& backend,
352
+ const std::string& op,
353
+ const std::string& s);
354
+ bool allowTF32CuDNN(const std::string& op = std::string()) const;
355
+ void setAllowTF32CuDNN(bool);
356
+ bool allowTF32OneDNN() const;
357
+ void setAllowTF32OneDNN(bool);
358
+ bool allowTF32CuBLAS() const;
359
+ void setAllowTF32CuBLAS(bool);
360
+ Float32MatmulPrecision float32MatmulPrecision() const;
361
+ std::string float32Precision(
362
+ const std::string& backend,
363
+ const std::string& op) const;
364
+ bool allowFP16ReductionCuBLAS() const;
365
+ void setAllowFP16ReductionCuBLAS(bool);
366
+ bool allowBF16ReductionCuBLAS() const;
367
+ void setAllowBF16ReductionCuBLAS(bool);
368
+ bool allowFP16AccumulationCuBLAS() const;
369
+ void setAllowFP16AccumulationCuBLAS(bool);
370
+
371
+ // Matmuls can use a so-called "persistent" kernel which launches one CUDA
372
+ // block for each SM on the GPU, and each block then iterates over multiple
373
+ // output tiles. This allows to use software pipelining to hide the begin/end
374
+ // latencies (e.g., epilogue), especially when only one tile fits per SM.
375
+ // However, if some SMs are busy (e.g., with a background NCCL kernel), the
376
+ // matmul's blocks will be scheduled in two waves and, in the absence of some
377
+ // smart load balancing, the kernel will take twice as long. This flag allows
378
+ // to make matmuls target only a subset of the SMs, so they can fully schedule
379
+ // even next to a comms kernel, and only be a few percent slower.
380
+ std::optional<int32_t> _SMCarveout_EXPERIMENTAL() const;
381
+ void _setSMCarveout_EXPERIMENTAL(std::optional<int32_t>);
382
+
383
+ at::QEngine qEngine() const;
384
+ void setQEngine(at::QEngine e);
385
+ static const std::vector<at::QEngine>& supportedQEngines();
386
+ static bool isXNNPACKAvailable();
387
+ void setCheckSparseTensorInvariants(bool e);
388
+ bool checkSparseTensorInvariants() const;
389
+ // This method is used to release the original weight after pre-packing.
390
+ // It should be called once before loading/running the model.
391
+ // NB: By default it is set to true for mobile builds.
392
+ void setReleaseWeightsWhenPrepacking(bool e);
393
+ bool releaseWeightsWhenPrepacking() const;
394
+
395
+ void setDisplayVmapFallbackWarnings(bool enabled);
396
+ bool areVmapFallbackWarningsEnabled() const;
397
+
398
+ bool isDefaultMobileCPUAllocatorSet();
399
+ void setDefaultMobileCPUAllocator();
400
+ void unsetDefaultMobileCPUAllocator();
401
+ bool allowFP16ReductionCPU() const;
402
+ void setAllowFP16ReductionCPU(bool);
403
+
404
+ // Preserved for BC
405
+ void lazyInitCUDA() {
406
+ TORCH_WARN_DEPRECATION(
407
+ "lazyInitCUDA is deprecated. Please use lazyInitDevice(at::kCUDA) instead.")
408
+ lazyInitDevice(at::kCUDA);
409
+ }
410
+ void lazyInitHIP() {
411
+ TORCH_WARN_DEPRECATION(
412
+ "lazyInitHIP is deprecated. Please use lazyInitDevice(at::kHIP) instead.")
413
+ lazyInitDevice(at::kHIP);
414
+ }
415
+ void lazyInitXPU() {
416
+ TORCH_WARN_DEPRECATION(
417
+ "lazyInitXPU is deprecated. Please use lazyInitDevice(at::kXPU) instead.")
418
+ lazyInitDevice(at::kXPU);
419
+ }
420
+ void lazyInitMTIA() {
421
+ TORCH_WARN_DEPRECATION(
422
+ "lazyInitMTIA is deprecated. Please use lazyInitDevice(at::kMTIA) instead.")
423
+ lazyInitDevice(at::kMTIA);
424
+ }
425
+ void lazyInitPrivateUse1() {
426
+ TORCH_WARN_DEPRECATION(
427
+ "lazyInitPrivateUse1 is deprecated. Please use lazyInitDevice(at::kPrivateUse1) instead.")
428
+ lazyInitDevice(at::kPrivateUse1);
429
+ }
430
+
431
+ private:
432
+ static bool checkCuBLASConfigDeterministic();
433
+ std::array<c10::once_flag, at::COMPILE_TIME_MAX_DEVICE_TYPES> init_;
434
+ bool enabled_cudnn = true;
435
+ bool deterministic_cudnn = false;
436
+ bool deterministic_mkldnn = false;
437
+ bool _deterministic_algorithms = false;
438
+ bool _deterministic_algorithms_warn_only = false;
439
+ bool _deterministic_fill_uninitialized_memory = true;
440
+ std::array<at::SDPBackend, at::num_sdp_backends> sdp_priority_order = {
441
+ at::SDPBackend::flash_attention,
442
+ at::SDPBackend::efficient_attention,
443
+ at::SDPBackend::math,
444
+ at::SDPBackend::cudnn_attention,
445
+ at::SDPBackend::overrideable};
446
+ bool enabled_flashSDP = true;
447
+ bool enabled_mem_efficientSDP = true;
448
+ bool enabled_mathSDP = true;
449
+ bool enabled_cudnnSDP = true;
450
+ bool enabled_overrideable = true;
451
+ bool allow_fp16_bf16_reduction_mathSDP = false;
452
+ bool benchmark_cudnn = false;
453
+ bool immediate_miopen = false;
454
+ Float32MatmulPrecision float32_matmul_precision =
455
+ c10::utils::check_env("TORCH_ALLOW_TF32_CUBLAS_OVERRIDE") == true
456
+ ? at::Float32MatmulPrecision::HIGH
457
+ : at::Float32MatmulPrecision::HIGHEST;
458
+ int benchmark_limit_cudnn = 10;
459
+ bool allow_tf32_cudnn = true;
460
+ bool allow_fp16_reduction_cublas = true;
461
+ bool allow_bf16_reduction_cublas = true;
462
+ bool allow_fp16_accumulation_cublas = false;
463
+ std::optional<int32_t> sm_carveout = std::nullopt;
464
+ bool enabled_mkldnn = true;
465
+ bool allow_tf32_onednn = false;
466
+ bool enabled_nnpack = true;
467
+ at::LinalgBackend linalg_preferred_backend =
468
+ (c10::utils::check_env("TORCH_LINALG_PREFER_CUSOLVER") == true ||
469
+ c10::utils::check_env("TORCH_LINALG_PREFER_HIPSOLVER") == true) // alias
470
+ ? at::LinalgBackend::Cusolver
471
+ : at::LinalgBackend::Default;
472
+ at::BlasBackend blas_preferred_backend =
473
+ (c10::utils::check_env("TORCH_BLAS_PREFER_CUBLASLT") == true ||
474
+ c10::utils::check_env("TORCH_BLAS_PREFER_HIPBLASLT") == true) // alias
475
+ ? at::BlasBackend::Cublaslt
476
+ : at::BlasBackend::Default;
477
+ at::ROCmFABackend rocm_fa_preferred_backend =
478
+ c10::utils::check_env("TORCH_ROCM_FA_PREFER_CK") == true
479
+ ? at::ROCmFABackend::Ck
480
+ : at::ROCmFABackend::Default;
481
+ #ifdef C10_MOBILE
482
+ bool release_original_weights = true;
483
+ #else
484
+ bool release_original_weights = false;
485
+ #endif
486
+ bool display_vmap_fallback_warnings_ = false;
487
+ std::atomic<at::QEngine> quantized_engine = at::QEngine::NoQEngine;
488
+ bool enable_sparse_tensor_invariant_checks = false;
489
+ bool allow_fp16_reduction_cpu = false;
490
+
491
+ std::map<std::string, std::map<std::string, std::string>> fp32_precision = {
492
+ {"generic", {{"all", "none"}}},
493
+ {"mkldnn",
494
+ {{"matmul", "none"},
495
+ {"conv", "none"},
496
+ {"rnn", "none"},
497
+ {"all", "none"}}},
498
+ {"cuda",
499
+ {{"matmul",
500
+ float32_matmul_precision == at::Float32MatmulPrecision::HIGHEST
501
+ ? "none"
502
+ : "tf32"},
503
+ {"conv", "tf32"},
504
+ {"rnn", "tf32"},
505
+ {"all", "none"}}},
506
+ };
507
+
508
+ Allocator* prev_allocator_ptr_{nullptr};
509
+ };
510
+
511
+ TORCH_API Context& globalContext();
512
+
513
+ inline void init() {
514
+ globalContext();
515
+ }
516
+
517
+ TORCH_API Allocator* getCPUAllocator();
518
+
519
+ inline DeprecatedTypeProperties& getDeprecatedTypeProperties(
520
+ Backend p,
521
+ ScalarType s) {
522
+ return globalDeprecatedTypePropertiesRegistry().getDeprecatedTypeProperties(
523
+ p, s);
524
+ }
525
+
526
+ inline DeprecatedTypeProperties& CPU(ScalarType s) {
527
+ return globalDeprecatedTypePropertiesRegistry().getDeprecatedTypeProperties(
528
+ Backend::CPU, s);
529
+ }
530
+
531
+ inline DeprecatedTypeProperties& CUDA(ScalarType s) {
532
+ return globalDeprecatedTypePropertiesRegistry().getDeprecatedTypeProperties(
533
+ Backend::CUDA, s);
534
+ }
535
+
536
+ inline DeprecatedTypeProperties& HIP(ScalarType s) {
537
+ return globalDeprecatedTypePropertiesRegistry().getDeprecatedTypeProperties(
538
+ Backend::HIP, s);
539
+ }
540
+
541
+ inline DeprecatedTypeProperties& MPS(ScalarType s) {
542
+ return globalDeprecatedTypePropertiesRegistry().getDeprecatedTypeProperties(
543
+ Backend::MPS, s);
544
+ }
545
+
546
+ inline bool hasCUDA() {
547
+ return globalContext().hasCUDA();
548
+ }
549
+
550
+ inline bool hasMTIA() {
551
+ return globalContext().hasMTIA();
552
+ }
553
+
554
+ inline bool hasHIP() {
555
+ return globalContext().hasHIP();
556
+ }
557
+
558
+ inline bool hasIPU() {
559
+ return globalContext().hasIPU();
560
+ }
561
+
562
+ inline bool hasXLA() {
563
+ return globalContext().hasXLA();
564
+ }
565
+
566
+ inline bool hasMPS() {
567
+ return globalContext().hasMPS();
568
+ }
569
+
570
+ inline bool hasMAIA() {
571
+ return globalContext().hasMAIA();
572
+ }
573
+
574
+ inline bool hasXPU() {
575
+ return globalContext().hasXPU();
576
+ }
577
+
578
+ inline bool hasHPU() {
579
+ return globalContext().hasHPU();
580
+ }
581
+
582
+ // Despite its name, this function returns the number of *CUDA* GPUs.
583
+ inline size_t getNumGPUs() {
584
+ // WARNING: DO NOT ADD LOGIC TO HANDLE OTHER DEVICE TYPES TO THIS
585
+ // FUNCTION. If you are interested in interrogating the number of
586
+ // devices for a specific device type, add that function to the
587
+ // relevant library (e.g., similar to at::cuda::device_count())
588
+ if (hasCUDA() && hasHIP()) {
589
+ TORCH_CHECK(
590
+ false,
591
+ "Enabling both CUDA and HIP in ATen is not supported, as HIP masquerades "
592
+ "to be CUDA (e.g., when you say CUDA, on a HIP build of ATen, this actually "
593
+ "means HIP. Rebuild PyTorch with one or the other disabled.");
594
+ } else if (hasCUDA()) {
595
+ return detail::getCUDAHooks().deviceCount();
596
+ } else if (hasHIP()) {
597
+ return detail::getHIPHooks().getNumGPUs();
598
+ } else {
599
+ return 0;
600
+ }
601
+ }
602
+
603
+ inline bool hasOpenMP() {
604
+ return globalContext().hasOpenMP();
605
+ }
606
+
607
+ inline bool hasMKL() {
608
+ return globalContext().hasMKL();
609
+ }
610
+
611
+ inline bool hasKleidiAI() {
612
+ return globalContext().hasKleidiAI();
613
+ }
614
+
615
+ inline bool hasLAPACK() {
616
+ return globalContext().hasLAPACK();
617
+ }
618
+
619
+ inline bool hasEigenSparse() {
620
+ return globalContext().hasEigenSparse();
621
+ }
622
+
623
+ inline bool hasMAGMA() {
624
+ return globalContext().hasMAGMA();
625
+ }
626
+
627
+ inline bool hasMKLDNN() {
628
+ return globalContext().hasMKLDNN();
629
+ }
630
+
631
+ inline void manual_seed(uint64_t seed) {
632
+ {
633
+ auto gen = globalContext().defaultGenerator(c10::DeviceType::CPU);
634
+ // See Note [Acquire lock when using random generators]
635
+ std::lock_guard<std::mutex> lock(gen.mutex());
636
+ gen.set_current_seed(seed);
637
+ }
638
+
639
+ const auto opt_device_type = at::getAccelerator();
640
+ if (!opt_device_type.has_value()) {
641
+ return;
642
+ }
643
+ const auto num_gpus = globalContext()
644
+ .getAcceleratorHooksInterface(opt_device_type)
645
+ .deviceCount();
646
+ for (const auto i : c10::irange(num_gpus)) {
647
+ auto gen = globalContext().defaultGenerator(
648
+ Device(opt_device_type.value(), static_cast<c10::DeviceIndex>(i)));
649
+ {
650
+ // See Note [Acquire lock when using random generators]
651
+ std::lock_guard<std::mutex> lock(gen.mutex());
652
+ gen.set_current_seed(seed);
653
+ }
654
+ }
655
+ }
656
+
657
+ // When the global flag `allow_tf32` is set to true, cuBLAS handles are
658
+ // automatically configured to use math mode CUBLAS_TF32_TENSOR_OP_MATH.
659
+ // For some operators, such as addmv, TF32 offers no performance improvement
660
+ // but causes precision loss. To help this case, this class implements
661
+ // a RAII guard that can be used to quickly disable TF32 within its scope.
662
+ //
663
+ // Usage:
664
+ // NoTF32Guard disable_tf32;
665
+ struct TORCH_API NoTF32Guard {
666
+ NoTF32Guard();
667
+ NoTF32Guard(NoTF32Guard&& other) = delete;
668
+ NoTF32Guard(const NoTF32Guard&) = delete;
669
+ NoTF32Guard& operator=(const NoTF32Guard&) = delete;
670
+ NoTF32Guard& operator=(NoTF32Guard&&) = delete;
671
+ ~NoTF32Guard();
672
+ static bool should_disable_tf32();
673
+
674
+ private:
675
+ bool changed = false;
676
+ };
677
+
678
+ struct TORCH_API ROCmBackwardPassGuard {
679
+ ROCmBackwardPassGuard();
680
+ ROCmBackwardPassGuard(ROCmBackwardPassGuard&& other) = delete;
681
+ ROCmBackwardPassGuard(const ROCmBackwardPassGuard&) = delete;
682
+ ROCmBackwardPassGuard& operator=(const ROCmBackwardPassGuard&) = delete;
683
+ ROCmBackwardPassGuard& operator=(ROCmBackwardPassGuard&&) = delete;
684
+ ~ROCmBackwardPassGuard();
685
+ static bool is_backward_pass();
686
+ };
687
+
688
+ } // namespace at
lib/python3.12/site-packages/torch/include/ATen/DLConvertor.h ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <ATen/ATen.h>
4
+ #include <ATen/Tensor.h>
5
+ #include <ATen/dlpack.h>
6
+
7
+ // this converter will:
8
+ // 1) take a Tensor object and wrap it in the DLPack tensor
9
+ // 2) take a dlpack tensor and convert it to the ATen Tensor
10
+
11
+ namespace at {
12
+
13
+ TORCH_API ScalarType toScalarType(const DLDataType& dtype);
14
+ TORCH_API DLManagedTensor* toDLPack(const Tensor& src);
15
+ TORCH_API struct DLManagedTensorVersioned* toDLPackVersioned(const Tensor& src);
16
+ TORCH_API Tensor
17
+ fromDLPack(DLManagedTensor* src, std::function<void(void*)> deleter = {});
18
+ TORCH_API Tensor fromDLPackVersioned(
19
+ DLManagedTensorVersioned* src,
20
+ std::function<void(void*)> deleter = {});
21
+ TORCH_API DLDataType getDLDataType(const Tensor& t);
22
+ TORCH_API DLDevice getDLContext(const Tensor& tensor, const int64_t& device_id);
23
+
24
+ // Copies the Tensor if there's a device mismatch or copy is forced.
25
+ // This should be used before actually creating the DLPack capsule.
26
+ TORCH_API Tensor maybeCopyTensor(
27
+ const Tensor& data,
28
+ std::optional<DLDevice> optional_dl_device,
29
+ std::optional<bool> copy);
30
+
31
+ // Converts the given at::Device into a DLDevice.
32
+ TORCH_API DLDevice torchDeviceToDLDevice(at::Device device);
33
+
34
+ // This trait class is used for retrieving different attributes, such as the
35
+ // PyCapsule names and conversion functions for both DLPack tensor classes:
36
+ // `DLManagedTensor` and `DLManagedTensorVersioned`.
37
+ //
38
+ // Each specialization should contain the following 2 traits:
39
+ // - `capsule`: actual name of the capsule
40
+ // - `used`: name of the capsule after using it
41
+ // - `toDLPack`: function for converting a tensor into a DLPack capsule
42
+ // - `fromDLPack`: function for creating a tensor from a DLPack capsule
43
+ //
44
+ // While `toDLPack` is the directly exposed to Python, `fromDLPack` is not.
45
+ // Although it contains the core implementation, it lacks the required book
46
+ // keeping logic contained in its caller `tensor_fromDLPack`.
47
+ //
48
+ // That said, `fromDLPack` is used directly in a few DLPack tests that live
49
+ // inside ATen (no Python available).
50
+ template <class T>
51
+ struct DLPackTraits {};
52
+
53
+ template <>
54
+ struct DLPackTraits<DLManagedTensor> {
55
+ inline static const char* capsule = "dltensor";
56
+ inline static const char* used = "used_dltensor";
57
+ inline static auto toDLPack = at::toDLPack;
58
+ inline static auto fromDLPack = at::fromDLPack;
59
+ };
60
+
61
+ template <>
62
+ struct DLPackTraits<DLManagedTensorVersioned> {
63
+ inline static const char* capsule = "dltensor_versioned";
64
+ inline static const char* used = "used_dltensor_versioned";
65
+ inline static auto toDLPack = at::toDLPackVersioned;
66
+ inline static auto fromDLPack = at::fromDLPackVersioned;
67
+ };
68
+
69
+ } // namespace at
lib/python3.12/site-packages/torch/include/ATen/DTensorState.h ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <c10/macros/Macros.h>
4
+
5
+ namespace at {
6
+
7
+ TORCH_API bool get_dtensor_allow_implicit_replication();
8
+ TORCH_API void set_dtensor_allow_implicit_replication(bool enabled);
9
+
10
+ struct DTensorAllowImplicitReplication {
11
+ DTensorAllowImplicitReplication()
12
+ : prev_dtensor_allow_implicit_replication_(
13
+ get_dtensor_allow_implicit_replication()) {
14
+ set_dtensor_allow_implicit_replication(true);
15
+ }
16
+
17
+ DTensorAllowImplicitReplication(const DTensorAllowImplicitReplication&) =
18
+ delete;
19
+ DTensorAllowImplicitReplication& operator=(
20
+ const DTensorAllowImplicitReplication&) = delete;
21
+ DTensorAllowImplicitReplication(DTensorAllowImplicitReplication&&) = delete;
22
+ DTensorAllowImplicitReplication& operator=(
23
+ DTensorAllowImplicitReplication&&) = delete;
24
+
25
+ ~DTensorAllowImplicitReplication() {
26
+ set_dtensor_allow_implicit_replication(
27
+ prev_dtensor_allow_implicit_replication_);
28
+ }
29
+
30
+ private:
31
+ bool prev_dtensor_allow_implicit_replication_;
32
+ };
33
+
34
+ } // namespace at
lib/python3.12/site-packages/torch/include/ATen/Device.h ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ #pragma once
2
+ #include <c10/core/Device.h>