torch-einops-kit 0.1.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- torch_einops_kit/__init__.py +172 -0
- torch_einops_kit/_cat_and_stack.py +197 -0
- torch_einops_kit/_dimensions.py +523 -0
- torch_einops_kit/_helpers.py +441 -0
- torch_einops_kit/_masking.py +318 -0
- torch_einops_kit/_padding.py +654 -0
- torch_einops_kit/_slicing.py +300 -0
- torch_einops_kit/_theTypes.py +434 -0
- torch_einops_kit/device.py +326 -0
- torch_einops_kit/einops.py +234 -0
- torch_einops_kit/nn.py +189 -0
- torch_einops_kit/py.typed +0 -0
- torch_einops_kit/save_load.py +554 -0
- torch_einops_kit/scaleValues.py +391 -0
- torch_einops_kit/utils.py +170 -0
- torch_einops_kit-0.1.8.dist-info/METADATA +482 -0
- torch_einops_kit-0.1.8.dist-info/RECORD +20 -0
- torch_einops_kit-0.1.8.dist-info/WHEEL +4 -0
- torch_einops_kit-0.1.8.dist-info/licenses/LICENSE +407 -0
- torch_einops_kit-0.1.8.dist-info/licenses/LICENSE-WANG +23 -0
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
# ruff: noqa: E224 PLC2701
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from os import PathLike
|
|
5
|
+
from torch.nn import Module
|
|
6
|
+
from torch.utils._pytree import PyTree
|
|
7
|
+
from typing import Any, overload, ParamSpec, Protocol, TYPE_CHECKING, TypedDict, TypeVar
|
|
8
|
+
|
|
9
|
+
if TYPE_CHECKING:
|
|
10
|
+
from torch import Tensor
|
|
11
|
+
from typing import Literal, TypeAlias
|
|
12
|
+
|
|
13
|
+
ConfigArgsKwargs: TypeAlias = tuple[tuple[Any, ...], dict[Any, Any]]
|
|
14
|
+
"""Represent the positional arguments and keyword arguments of a decorated constructor.
|
|
15
|
+
|
|
16
|
+
`ConfigArgsKwargs` is a `TypeAlias` for a two-element `tuple` whose first element holds positional
|
|
17
|
+
arguments as a `tuple` and whose second element holds keyword arguments as a `dict`. The `save_load`
|
|
18
|
+
[1] decorator stores each decorated `torch.nn.Module` [2] instance's constructor arguments as a
|
|
19
|
+
`ConfigArgsKwargs` under a configurable attribute name. `dehydrate_config` [3] and `rehydrate_config`
|
|
20
|
+
[4] both accept and return `ConfigArgsKwargs` values when traversing checkpoint configuration
|
|
21
|
+
payloads.
|
|
22
|
+
|
|
23
|
+
See Also
|
|
24
|
+
--------
|
|
25
|
+
DehydratedCheckpoint : Represent the full serialized checkpoint, including a pickled
|
|
26
|
+
`ConfigArgsKwargs` as `config`.
|
|
27
|
+
DehydratedTorchNNModule : Embed a `ConfigArgsKwargs` as the `config` field of a reconstruction
|
|
28
|
+
record.
|
|
29
|
+
|
|
30
|
+
References
|
|
31
|
+
----------
|
|
32
|
+
[1] torch_einops_kit.save_load.save_load
|
|
33
|
+
|
|
34
|
+
[2] torch.nn.Module - PyTorch documentation
|
|
35
|
+
https://pytorch.org/docs/stable/generated/torch.nn.Module.html
|
|
36
|
+
[3] torch_einops_kit.save_load.dehydrate_config
|
|
37
|
+
|
|
38
|
+
[4] torch_einops_kit.save_load.rehydrate_config
|
|
39
|
+
"""
|
|
40
|
+
PSpec = ParamSpec("PSpec")
|
|
41
|
+
"""Capture the full parameter specification of a callable for higher-order type signatures.
|
|
42
|
+
|
|
43
|
+
`PSpec` is a `ParamSpec` [1] that preserves the complete parameter signature of a callable through
|
|
44
|
+
decorator transformations. `move_inputs_to_device` [2] and `move_inputs_to_module_device` [3] each
|
|
45
|
+
use `PSpec` so the wrapped callable retains its original parameter signature. `maybe` [4] uses
|
|
46
|
+
`PSpec` to forward all positional and keyword arguments beyond the first to the wrapped callable.
|
|
47
|
+
|
|
48
|
+
See Also
|
|
49
|
+
--------
|
|
50
|
+
RVar : Represent the return type paired with `PSpec` in higher-order callable signatures.
|
|
51
|
+
TVar : Represent the input or return type paired with `PSpec` in identity-preserving signatures.
|
|
52
|
+
|
|
53
|
+
References
|
|
54
|
+
----------
|
|
55
|
+
[1] ParamSpec - Python typing documentation
|
|
56
|
+
https://docs.python.org/3/library/typing.html#typing.ParamSpec
|
|
57
|
+
[2] torch_einops_kit.device.move_inputs_to_device
|
|
58
|
+
|
|
59
|
+
[3] torch_einops_kit.device.move_inputs_to_module_device
|
|
60
|
+
|
|
61
|
+
[4] torch_einops_kit.maybe
|
|
62
|
+
"""
|
|
63
|
+
RVar = TypeVar("RVar")
|
|
64
|
+
"""Represent a generic return type in higher-order callable signatures.
|
|
65
|
+
|
|
66
|
+
`RVar` is an unconstrained `TypeVar` [1] used to annotate the return type of an inner callable in
|
|
67
|
+
decorator and wrapper signatures. `maybe` [2] uses `RVar` to type the return value of the callable
|
|
68
|
+
being wrapped, keeping `RVar` distinct from `TVar` [3], which types the first argument.
|
|
69
|
+
|
|
70
|
+
See Also
|
|
71
|
+
--------
|
|
72
|
+
TVar : Represent the input type paired with `RVar` in wrapper signatures.
|
|
73
|
+
PSpec : Capture the parameter specification of the callable paired with `RVar`.
|
|
74
|
+
|
|
75
|
+
References
|
|
76
|
+
----------
|
|
77
|
+
[1] TypeVar - Python typing documentation
|
|
78
|
+
https://docs.python.org/3/library/typing.html#typing.TypeVar
|
|
79
|
+
[2] torch_einops_kit.maybe
|
|
80
|
+
|
|
81
|
+
[3] torch_einops_kit.TVar
|
|
82
|
+
"""
|
|
83
|
+
StrPath: TypeAlias = str | PathLike[str]
|
|
84
|
+
"""Accept either a `str` or a `PathLike[str]` filesystem path value.
|
|
85
|
+
|
|
86
|
+
`StrPath` is a `TypeAlias` for `str | PathLike[str]` [1], intentionally copying the definition of
|
|
87
|
+
`_typeshed.StrPath` [2] from typeshed and mirroring the annotation accepted by `pathlib.Path` [3].
|
|
88
|
+
`StrPath` provides a single annotation for parameters that accept either a plain string path or any
|
|
89
|
+
path-like object. The generated `save` and `load` instance methods and the generated `init_and_load`
|
|
90
|
+
classmethod produced by `save_load` [4] each annotate their `path` parameter with `StrPath`.
|
|
91
|
+
|
|
92
|
+
References
|
|
93
|
+
----------
|
|
94
|
+
[1] os.PathLike - Python documentation
|
|
95
|
+
https://docs.python.org/3/library/os.html#os.PathLike
|
|
96
|
+
[2] _typeshed.StrPath - typeshed
|
|
97
|
+
https://github.com/python/typeshed/blob/main/stdlib/_typeshed/__init__.pyi
|
|
98
|
+
[3] pathlib.Path - Python documentation
|
|
99
|
+
https://docs.python.org/3/library/pathlib.html#pathlib.Path
|
|
100
|
+
[4] torch_einops_kit.save_load.save_load
|
|
101
|
+
"""
|
|
102
|
+
T_co = TypeVar("T_co", covariant=True)
|
|
103
|
+
"""Represent a covariant element type in read-only container protocols.
|
|
104
|
+
|
|
105
|
+
`T_co` is a covariant `TypeVar` [1] used in container protocols and functions that produce but do not
|
|
106
|
+
consume elements of a given type. `SupportsIntIndex` [2] uses `T_co` to annotate the element type
|
|
107
|
+
returned by `__getitem__`. `compact` [3] uses `T_co` for the element type in its input iterable and
|
|
108
|
+
its output `list`, preserving the element type while filtering `None` values.
|
|
109
|
+
|
|
110
|
+
See Also
|
|
111
|
+
--------
|
|
112
|
+
SupportsIntIndex : Container protocol parametrized by `T_co`.
|
|
113
|
+
TVar : Unconstrained TypeVar for operations where covariance is not required.
|
|
114
|
+
|
|
115
|
+
References
|
|
116
|
+
----------
|
|
117
|
+
[1] TypeVar - Python typing documentation
|
|
118
|
+
https://docs.python.org/3/library/typing.html#typing.TypeVar
|
|
119
|
+
[2] torch_einops_kit.SupportsIntIndex
|
|
120
|
+
|
|
121
|
+
[3] torch_einops_kit.compact
|
|
122
|
+
"""
|
|
123
|
+
TorchNNModule = TypeVar("TorchNNModule", bound=Module)
|
|
124
|
+
"""Represent a concrete `torch.nn.Module` subclass in decorator and method signatures.
|
|
125
|
+
|
|
126
|
+
`TorchNNModule` is a `TypeVar` [1] bound to `torch.nn.Module` [2] that lets static analyzers track
|
|
127
|
+
which specific subclass passes through a generic decorator or generic method. The `save_load` [3]
|
|
128
|
+
decorator uses `TorchNNModule` so that decorating a `Module` subclass preserves the concrete class
|
|
129
|
+
type in the return annotation rather than widening it to `type[Module]`.
|
|
130
|
+
`move_inputs_to_module_device` [4] uses `TorchNNModule` to preserve the specific `Module` subclass
|
|
131
|
+
type through its decorator.
|
|
132
|
+
|
|
133
|
+
See Also
|
|
134
|
+
--------
|
|
135
|
+
PSpec : Paired with `TorchNNModule` in `move_inputs_to_module_device` to capture the remaining
|
|
136
|
+
parameters.
|
|
137
|
+
|
|
138
|
+
References
|
|
139
|
+
----------
|
|
140
|
+
[1] TypeVar - Python typing documentation
|
|
141
|
+
https://docs.python.org/3/library/typing.html#typing.TypeVar
|
|
142
|
+
[2] torch.nn.Module - PyTorch documentation
|
|
143
|
+
https://pytorch.org/docs/stable/generated/torch.nn.Module.html
|
|
144
|
+
[3] torch_einops_kit.save_load.save_load
|
|
145
|
+
|
|
146
|
+
[4] torch_einops_kit.device.move_inputs_to_module_device
|
|
147
|
+
"""
|
|
148
|
+
TVar = TypeVar("TVar")
|
|
149
|
+
"""Represent an unconstrained generic type for identity-preserving operations.
|
|
150
|
+
|
|
151
|
+
`TVar` is an unconstrained `TypeVar` [1] used to annotate functions and protocols that accept a value
|
|
152
|
+
of any type and return a value of the same type. `exists` [2], `default` [3], `identity` [4], and
|
|
153
|
+
`map_values` [5] all use `TVar` to express that their output type matches their input type.
|
|
154
|
+
`IdentityCallable` [6] uses `TVar` to annotate both the input and output of its `__call__` method.
|
|
155
|
+
`first` [7] uses `TVar` via `SupportsIntIndex[TVar]` to propagate the element type of its argument to
|
|
156
|
+
the return type.
|
|
157
|
+
|
|
158
|
+
See Also
|
|
159
|
+
--------
|
|
160
|
+
T_co : Covariant TypeVar for read-only container protocols requiring covariance.
|
|
161
|
+
RVar : Separate TypeVar for the return type in higher-order callable signatures where input and
|
|
162
|
+
return types differ.
|
|
163
|
+
IdentityCallable : Protocol using `TVar` to type identity-preserving callables.
|
|
164
|
+
|
|
165
|
+
References
|
|
166
|
+
----------
|
|
167
|
+
[1] TypeVar - Python typing documentation
|
|
168
|
+
https://docs.python.org/3/library/typing.html#typing.TypeVar
|
|
169
|
+
[2] torch_einops_kit.exists
|
|
170
|
+
|
|
171
|
+
[3] torch_einops_kit.default
|
|
172
|
+
|
|
173
|
+
[4] torch_einops_kit.identity
|
|
174
|
+
|
|
175
|
+
[5] torch_einops_kit.map_values
|
|
176
|
+
|
|
177
|
+
[6] torch_einops_kit.IdentityCallable
|
|
178
|
+
|
|
179
|
+
[7] torch_einops_kit.first
|
|
180
|
+
"""
|
|
181
|
+
木 = TypeVar('木', bound=PyTree)
|
|
182
|
+
|
|
183
|
+
class DehydratedCheckpoint(TypedDict):
|
|
184
|
+
"""Represent the checkpoint dictionary written and read by the `save_load` decorator.
|
|
185
|
+
|
|
186
|
+
`DehydratedCheckpoint` is a `TypedDict` [1] that defines the structure of the dictionary
|
|
187
|
+
serialized by `torch.save` [2] and deserialized by `torch.load` [3] during checkpoint operations.
|
|
188
|
+
The `save_load` [4] decorator generates instance methods and a classmethod that build and consume
|
|
189
|
+
`DehydratedCheckpoint` values. The generated classmethod reads `config` to reconstruct
|
|
190
|
+
constructor arguments before restoring model state.
|
|
191
|
+
|
|
192
|
+
Attributes
|
|
193
|
+
----------
|
|
194
|
+
model : dict[str, Tensor]
|
|
195
|
+
The model state dictionary produced by `torch.nn.Module.state_dict` [5], mapping parameter
|
|
196
|
+
and buffer names to their `Tensor` values.
|
|
197
|
+
config : bytes
|
|
198
|
+
The constructor arguments serialized with `pickle.dumps` [6]. The bytes encode a pickled
|
|
199
|
+
`ConfigArgsKwargs` [7] value after `dehydrate_config` [8] has replaced any nested decorated
|
|
200
|
+
`Module` instances with `DehydratedTorchNNModule` [9] reconstruction records.
|
|
201
|
+
version : str | None
|
|
202
|
+
An optional version string written at save time. When both the stored version and the
|
|
203
|
+
`save_load` `version` argument are different, the generated load method prints a notice but
|
|
204
|
+
still restores model state.
|
|
205
|
+
|
|
206
|
+
See Also
|
|
207
|
+
--------
|
|
208
|
+
DehydratedTorchNNModule : Reconstruction record that may appear as a leaf in the pickled
|
|
209
|
+
`config` bytes.
|
|
210
|
+
ConfigArgsKwargs : Type of the value unpickled from `config`.
|
|
211
|
+
|
|
212
|
+
References
|
|
213
|
+
----------
|
|
214
|
+
[1] TypedDict - Python typing documentation
|
|
215
|
+
https://docs.python.org/3/library/typing.html#typing.TypedDict
|
|
216
|
+
[2] torch.save - PyTorch documentation
|
|
217
|
+
https://pytorch.org/docs/stable/generated/torch.save.html
|
|
218
|
+
[3] torch.load - PyTorch documentation
|
|
219
|
+
https://pytorch.org/docs/stable/generated/torch.load.html
|
|
220
|
+
[4] torch_einops_kit.save_load.save_load
|
|
221
|
+
|
|
222
|
+
[5] torch.nn.Module.state_dict - PyTorch documentation
|
|
223
|
+
https://pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module.state_dict
|
|
224
|
+
[6] pickle.dumps - Python documentation
|
|
225
|
+
https://docs.python.org/3/library/pickle.html#pickle.dumps
|
|
226
|
+
[7] torch_einops_kit.ConfigArgsKwargs
|
|
227
|
+
|
|
228
|
+
[8] torch_einops_kit.save_load.dehydrate_config
|
|
229
|
+
|
|
230
|
+
[9] torch_einops_kit.DehydratedTorchNNModule
|
|
231
|
+
"""
|
|
232
|
+
model: dict[str, Tensor]
|
|
233
|
+
config: bytes
|
|
234
|
+
version: str | None
|
|
235
|
+
|
|
236
|
+
class DehydratedTorchNNModule(TypedDict):
|
|
237
|
+
"""Represent the reconstruction record for a single decorated `torch.nn.Module` instance.
|
|
238
|
+
|
|
239
|
+
`DehydratedTorchNNModule` is a `TypedDict` [1] that encodes everything needed to reconstruct a
|
|
240
|
+
decorated `torch.nn.Module` [2] instance from a stored checkpoint. `dehydrate_config` [3]
|
|
241
|
+
produces a `DehydratedTorchNNModule` for each nested module it encounters during traversal.
|
|
242
|
+
`rehydrate_config` [4] recognizes a `DehydratedTorchNNModule` by the `__save_load_module__`
|
|
243
|
+
sentinel and reconstructs the original module by calling `klass` with the unpacked `config`.
|
|
244
|
+
|
|
245
|
+
Attributes
|
|
246
|
+
----------
|
|
247
|
+
__save_load_module__ : Literal[True]
|
|
248
|
+
A sentinel flag with value `True` that `rehydrate_config` uses to distinguish a
|
|
249
|
+
`DehydratedTorchNNModule` record from other values during traversal.
|
|
250
|
+
klass : type[Module]
|
|
251
|
+
The decorated `torch.nn.Module` subclass to instantiate during rehydration.
|
|
252
|
+
config : ConfigArgsKwargs
|
|
253
|
+
The constructor arguments stored as a `ConfigArgsKwargs` value. `config` itself may contain
|
|
254
|
+
further nested `DehydratedTorchNNModule` records that `rehydrate_config` will recursively
|
|
255
|
+
reconstruct.
|
|
256
|
+
|
|
257
|
+
See Also
|
|
258
|
+
--------
|
|
259
|
+
DehydratedCheckpoint : The outer checkpoint structure whose `config` bytes may contain pickled
|
|
260
|
+
`DehydratedTorchNNModule` values.
|
|
261
|
+
ConfigArgsKwargs : Type of the `config` field.
|
|
262
|
+
|
|
263
|
+
References
|
|
264
|
+
----------
|
|
265
|
+
[1] TypedDict - Python typing documentation
|
|
266
|
+
https://docs.python.org/3/library/typing.html#typing.TypedDict
|
|
267
|
+
[2] torch.nn.Module - PyTorch documentation
|
|
268
|
+
https://pytorch.org/docs/stable/generated/torch.nn.Module.html
|
|
269
|
+
[3] torch_einops_kit.save_load.dehydrate_config
|
|
270
|
+
|
|
271
|
+
[4] torch_einops_kit.save_load.rehydrate_config
|
|
272
|
+
"""
|
|
273
|
+
__save_load_module__: Literal[True]
|
|
274
|
+
klass: type[Module]
|
|
275
|
+
config: ConfigArgsKwargs
|
|
276
|
+
|
|
277
|
+
class DimAndValue(TypedDict, total=False):
|
|
278
|
+
"""Carry the optional `dim` and `value` keyword arguments for padding operations.
|
|
279
|
+
|
|
280
|
+
`DimAndValue` is a `TypedDict` [1] with `total=False`, meaning both `dim` and `value` are
|
|
281
|
+
optional. Padding functions in this package accept `**kwargs: Unpack[DimAndValue]` [2] to forward
|
|
282
|
+
these two keyword arguments to `pad_at_dim` [3]. `pad_left_at_dim` [4] and `pad_right_at_dim` [5]
|
|
283
|
+
both accept and forward `DimAndValue` keyword arguments.
|
|
284
|
+
|
|
285
|
+
Attributes
|
|
286
|
+
----------
|
|
287
|
+
dim : int
|
|
288
|
+
The dimension along which padding is applied. When absent, `pad_at_dim` uses a default of
|
|
289
|
+
`-1`, meaning the last dimension.
|
|
290
|
+
value : float
|
|
291
|
+
The fill value used for the padded positions. When absent, `pad_at_dim` uses a default of
|
|
292
|
+
`0.0`.
|
|
293
|
+
|
|
294
|
+
See Also
|
|
295
|
+
--------
|
|
296
|
+
pad_left_at_dim : Pad the left side of a tensor, accepting `**kwargs: Unpack[DimAndValue]`.
|
|
297
|
+
pad_right_at_dim : Pad the right side of a tensor, accepting `**kwargs: Unpack[DimAndValue]`.
|
|
298
|
+
|
|
299
|
+
References
|
|
300
|
+
----------
|
|
301
|
+
[1] TypedDict - Python typing documentation
|
|
302
|
+
https://docs.python.org/3/library/typing.html#typing.TypedDict
|
|
303
|
+
[2] Unpack - Python typing documentation
|
|
304
|
+
https://docs.python.org/3/library/typing.html#typing.Unpack
|
|
305
|
+
[3] torch_einops_kit.pad_at_dim
|
|
306
|
+
|
|
307
|
+
[4] torch_einops_kit.pad_left_at_dim
|
|
308
|
+
|
|
309
|
+
[5] torch_einops_kit.pad_right_at_dim
|
|
310
|
+
"""
|
|
311
|
+
dim: int
|
|
312
|
+
value: float
|
|
313
|
+
|
|
314
|
+
class IdentityCallable(Protocol):
|
|
315
|
+
"""Match any callable that returns its first positional argument unchanged.
|
|
316
|
+
|
|
317
|
+
`IdentityCallable` is a `Protocol` [1] that any callable satisfies when its `__call__` method
|
|
318
|
+
accepts a positional argument `value` of type `TVar` and returns the same `TVar` value,
|
|
319
|
+
regardless of any additional positional or keyword arguments. `maybe` [2] returns an
|
|
320
|
+
`IdentityCallable` when its `fn` argument is `None`, and `identity` [3] is the concrete
|
|
321
|
+
implementation of `IdentityCallable` used throughout this package.
|
|
322
|
+
|
|
323
|
+
See Also
|
|
324
|
+
--------
|
|
325
|
+
identity : Concrete implementation of `IdentityCallable` used by `maybe` when `fn` is `None`.
|
|
326
|
+
maybe : Returns an `IdentityCallable` when called with `fn=None`. TVar : TypeVar parametrizing
|
|
327
|
+
the input and output type of `IdentityCallable.__call__`.
|
|
328
|
+
|
|
329
|
+
References
|
|
330
|
+
----------
|
|
331
|
+
[1] Protocol - Python typing documentation
|
|
332
|
+
https://docs.python.org/3/library/typing.html#typing.Protocol
|
|
333
|
+
[2] torch_einops_kit.maybe
|
|
334
|
+
|
|
335
|
+
[3] torch_einops_kit.identity
|
|
336
|
+
"""
|
|
337
|
+
def __call__(self, value: TVar, /, *args: object, **kwargs: object) -> TVar: ...
|
|
338
|
+
|
|
339
|
+
class InversePackListTensors(Protocol):
|
|
340
|
+
"""Annotate the inverse callable returned by `pack_with_inverse` for `list[Tensor]` input.
|
|
341
|
+
|
|
342
|
+
You can use `InversePackListTensors` as a `Protocol` [1] for the inverse callable paired with
|
|
343
|
+
`pack_with_inverse` [2] when `t` is a `list[Tensor]`. The callable accepts a packed or
|
|
344
|
+
transformed `Tensor` `out`, optionally accepts an override unpacking pattern `inv_pattern`, and
|
|
345
|
+
returns a `list[Tensor]` whose item count and per-item shape match the original pack step.
|
|
346
|
+
|
|
347
|
+
Call Signatures
|
|
348
|
+
---------------
|
|
349
|
+
default pattern : callable
|
|
350
|
+
`inverse(out)` reuses the original `pattern` captured by `pack_with_inverse` [2].
|
|
351
|
+
override pattern : callable
|
|
352
|
+
`inverse(out, inv_pattern)` forwards `inv_pattern` to `einops.unpack` [3] instead of reusing
|
|
353
|
+
the original `pattern`. The `inv_pattern` must stay compatible with the packed-shape metadata
|
|
354
|
+
captured during packing.
|
|
355
|
+
|
|
356
|
+
See Also
|
|
357
|
+
--------
|
|
358
|
+
InversePackTensor : Annotate the inverse callable returned by `pack_with_inverse` for one
|
|
359
|
+
`Tensor` input.
|
|
360
|
+
torch_einops_kit.einops.pack_with_inverse : Build an `InversePackListTensors` callable for
|
|
361
|
+
`list[Tensor]` input.
|
|
362
|
+
|
|
363
|
+
References
|
|
364
|
+
----------
|
|
365
|
+
[1] Protocol - Python typing documentation
|
|
366
|
+
https://docs.python.org/3/library/typing.html#typing.Protocol
|
|
367
|
+
[2] torch_einops_kit.einops.pack_with_inverse
|
|
368
|
+
|
|
369
|
+
[3] einops pack/unpack API
|
|
370
|
+
https://einops.rocks/api/pack_unpack/
|
|
371
|
+
"""
|
|
372
|
+
@overload
|
|
373
|
+
def __call__(self, out: Tensor) -> list[Tensor]: ...
|
|
374
|
+
@overload
|
|
375
|
+
def __call__(self, out: Tensor, inv_pattern: str | None) -> list[Tensor]: ...
|
|
376
|
+
|
|
377
|
+
class InversePackTensor(Protocol):
|
|
378
|
+
"""Annotate the inverse callable returned by `pack_with_inverse` for one `Tensor` input.
|
|
379
|
+
|
|
380
|
+
You can use `InversePackTensor` as a `Protocol` [1] for the inverse callable paired with
|
|
381
|
+
`pack_with_inverse` [2] when `t` is one `Tensor`. The callable accepts a packed or transformed
|
|
382
|
+
`Tensor` `out`, optionally accepts an override unpacking pattern `inv_pattern`, and returns one
|
|
383
|
+
reconstructed `Tensor` instead of `list[Tensor]`.
|
|
384
|
+
|
|
385
|
+
Call Signatures
|
|
386
|
+
---------------
|
|
387
|
+
default pattern : callable
|
|
388
|
+
`inverse(out)` reuses the original `pattern` captured by `pack_with_inverse` [2].
|
|
389
|
+
override pattern : callable
|
|
390
|
+
`inverse(out, inv_pattern)` forwards `inv_pattern` to `einops.unpack` [3] instead of reusing
|
|
391
|
+
the original `pattern`. The returned callable unwraps the single reconstructed `Tensor` from
|
|
392
|
+
the single-item unpack result.
|
|
393
|
+
|
|
394
|
+
See Also
|
|
395
|
+
--------
|
|
396
|
+
InversePackListTensors : Annotate the inverse callable returned by `pack_with_inverse` for
|
|
397
|
+
`list[Tensor]` input.
|
|
398
|
+
torch_einops_kit.einops.pack_with_inverse : Build an `InversePackTensor` callable for one
|
|
399
|
+
`Tensor` input.
|
|
400
|
+
|
|
401
|
+
References
|
|
402
|
+
----------
|
|
403
|
+
[1] Protocol - Python typing documentation
|
|
404
|
+
https://docs.python.org/3/library/typing.html#typing.Protocol
|
|
405
|
+
[2] torch_einops_kit.einops.pack_with_inverse
|
|
406
|
+
|
|
407
|
+
[3] einops pack/unpack API
|
|
408
|
+
https://einops.rocks/api/pack_unpack/
|
|
409
|
+
"""
|
|
410
|
+
@overload
|
|
411
|
+
def __call__(self, out: Tensor) -> Tensor: ...
|
|
412
|
+
@overload
|
|
413
|
+
def __call__(self, out: Tensor, inv_pattern: str | None) -> Tensor: ...
|
|
414
|
+
|
|
415
|
+
class SupportsIntIndex(Protocol[T_co]):
|
|
416
|
+
"""Match any object that supports integer indexing via `__getitem__`.
|
|
417
|
+
|
|
418
|
+
`SupportsIntIndex` is a `Protocol` [1] that any object satisfies when it implements `__getitem__`
|
|
419
|
+
with a single `int` index and returns a value of type `T_co`. `first` [2] uses `SupportsIntIndex`
|
|
420
|
+
to accept any sequence-like argument without requiring it to be a `Sequence` or `list`
|
|
421
|
+
specifically.
|
|
422
|
+
|
|
423
|
+
See Also
|
|
424
|
+
--------
|
|
425
|
+
first : Uses `SupportsIntIndex` to accept any object that supports integer indexing.
|
|
426
|
+
T_co : Covariant TypeVar that parametrizes the element type returned by `__getitem__`.
|
|
427
|
+
|
|
428
|
+
References
|
|
429
|
+
----------
|
|
430
|
+
[1] Protocol - Python typing documentation
|
|
431
|
+
https://docs.python.org/3/library/typing.html#typing.Protocol
|
|
432
|
+
[2] torch_einops_kit.first
|
|
433
|
+
"""
|
|
434
|
+
def __getitem__(self, index: int, /) -> T_co: ...
|