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/nn.py ADDED
@@ -0,0 +1,189 @@
1
+ """Construct `nn.Sequential` instances and adapt callables to the `nn.Module` interface.
2
+
3
+ Contents
4
+ --------
5
+ Functions
6
+ Sequential
7
+ Construct an `nn.Sequential` instance from `modules`, ignoring each value that is `None`.
8
+
9
+ Classes
10
+ Identity
11
+ Adapt `identity` to the `nn.Module` interface.
12
+ Lambda
13
+ Adapt `fn` to the `nn.Module` interface.
14
+ """
15
+
16
+ from __future__ import annotations
17
+
18
+ from torch import nn
19
+ from torch_einops_kit import compact, identity, PSpec, RVar, tree_flatten_with_inverse, TVar, 木
20
+ from typing import Concatenate, Generic, TYPE_CHECKING
21
+
22
+ if TYPE_CHECKING:
23
+ from collections.abc import Callable
24
+
25
+ def Sequential(*modules: nn.Module | None) -> nn.Sequential:
26
+ """Construct an `nn.Sequential` instance from `modules`, ignoring each value that is `None`.
27
+
28
+ You can call `Sequential` in place of `nn.Sequential` [1] when `modules` may contain `None`.
29
+ `Sequential` passes `modules` to `compact` [2], then passes the resulting `list` of `nn.Module`
30
+ instances to `nn.Sequential` [1].
31
+
32
+ Parameters
33
+ ----------
34
+ *modules : nn.Module | None
35
+ Each `nn.Module` instance to place in the returned `nn.Sequential` instance. Each value that
36
+ is `None` is discarded before construction.
37
+
38
+ Returns
39
+ -------
40
+ sequential : nn.Sequential
41
+ `nn.Sequential` instance that contains each value from `modules` that is not `None`, in
42
+ argument order.
43
+
44
+ See Also
45
+ --------
46
+ compact : Filter `None` values from `arr` and return the remaining elements as a `list`.
47
+
48
+ References
49
+ ----------
50
+ [1] torch.nn.Sequential - PyTorch documentation
51
+ https://pytorch.org/docs/stable/generated/torch.nn.Sequential.html
52
+ [2] torch_einops_kit.compact
53
+ """
54
+ return nn.Sequential(*compact(modules))
55
+
56
+ class Identity(nn.Module):
57
+ """Adapt `identity` to the `nn.Module` interface.
58
+
59
+ You can instantiate `Identity` when code requires an `nn.Module` [1] instance but the required
60
+ computation is `identity` [2]. `Identity` binds `forward` to `identity`, so `Identity.forward`
61
+ accepts the same arguments as `identity` and returns the first positional argument unchanged.
62
+ Unlike `torch.nn.Identity` [3], `Identity.forward` accepts additional positional arguments and
63
+ keyword arguments.
64
+
65
+ Attributes
66
+ ----------
67
+ forward : staticmethod
68
+ Class attribute that delegates `Identity.forward` to `identity` [2].
69
+
70
+ See Also
71
+ --------
72
+ identity : Return `t` unchanged, ignoring all other arguments.
73
+ torch.nn.Identity : Return the input unchanged.
74
+
75
+ References
76
+ ----------
77
+ [1] torch.nn.Module - PyTorch documentation
78
+ https://pytorch.org/docs/stable/generated/torch.nn.Module.html
79
+ [2] torch_einops_kit.identity
80
+
81
+ [3] torch.nn.Identity - PyTorch documentation
82
+ https://pytorch.org/docs/stable/generated/torch.nn.Identity.html
83
+ """
84
+
85
+ forward = staticmethod(identity)
86
+ """Return the first positional argument unchanged and ignore each additional argument.
87
+
88
+ You can call `Identity.forward` when `Identity` must satisfy the `nn.Module` interface and the
89
+ required computation is `identity` [1]. `Identity.forward` delegates to `identity`, so
90
+ `Identity.forward` returns the first positional argument unchanged and discards each additional
91
+ positional argument and keyword argument.
92
+
93
+ See Also
94
+ --------
95
+ identity : Return `t` unchanged, ignoring all other arguments.
96
+
97
+ References
98
+ ----------
99
+ [1] torch_einops_kit.identity
100
+ """
101
+
102
+ class Lambda(nn.Module, Generic[PSpec, RVar]):
103
+ """Adapt `fn` to the `nn.Module` interface.
104
+
105
+ You can instantiate `Lambda` when code requires an `nn.Module` [1] instance but the required
106
+ computation already exists as `fn`. `Lambda` stores `fn` on `self.fn`, and `Lambda.forward` passes
107
+ each positional argument and keyword argument to `fn` without changing the argument structure.
108
+
109
+ Attributes
110
+ ----------
111
+ fn : Callable[PSpec, RVar]
112
+ Callable object called by `Lambda.forward`.
113
+
114
+ References
115
+ ----------
116
+ [1] torch.nn.Module - PyTorch documentation
117
+ https://pytorch.org/docs/stable/generated/torch.nn.Module.html
118
+ """
119
+
120
+ def __init__(self, fn: Callable[PSpec, RVar]) -> None:
121
+ """Store `fn` on `self.fn`.
122
+
123
+ You can instantiate `Lambda` with `fn` when `fn` already implements the required computation.
124
+ `Lambda.__init__` stores `fn` on `self.fn` without modifying `fn`.
125
+
126
+ Parameters
127
+ ----------
128
+ fn : Callable[PSpec, RVar]
129
+ Callable object stored on `self.fn`.
130
+ """
131
+ super().__init__()
132
+ self.fn: Callable[PSpec, RVar] = fn
133
+
134
+ def forward(self, *args: PSpec.args, **kwargs: PSpec.kwargs) -> RVar:
135
+ """Call `self.fn` with `args` and `kwargs`.
136
+
137
+ You can call `Lambda.forward` to delegate `args` and `kwargs` to `self.fn`. `Lambda.forward`
138
+ returns the value returned by `self.fn` without modifying `args` or `kwargs`.
139
+
140
+ Parameters
141
+ ----------
142
+ *args : PSpec.args
143
+ Positional arguments passed to `self.fn`.
144
+ **kwargs : PSpec.kwargs
145
+ Keyword arguments passed to `self.fn`.
146
+
147
+ Returns
148
+ -------
149
+ result : RVar
150
+ Value returned by `self.fn`.
151
+ """
152
+ return self.fn(*args, **kwargs)
153
+
154
+ class Residual(nn.Module, Generic[TVar, PSpec, 木]): # noqa: D101
155
+ def __init__(self, fn: Callable[Concatenate[TVar, PSpec], 木]) -> None:
156
+ super().__init__()
157
+ self.fn: Callable[Concatenate[TVar, PSpec], 木] = fn
158
+
159
+ def forward(self, x: TVar, *args: PSpec.args, **kwargs: PSpec.kwargs) -> 木: # noqa: D102
160
+ out: 木 = self.fn(x, *args, **kwargs)
161
+
162
+ (first, *rest), inverse = tree_flatten_with_inverse(out)
163
+ return inverse((first + x, *rest))
164
+
165
+ """
166
+ Some of the logic in this module may be protected by the following.
167
+
168
+ MIT License
169
+
170
+ Copyright (c) 2026 Phil Wang
171
+
172
+ Permission is hereby granted, free of charge, to any person obtaining a copy
173
+ of this software and associated documentation files (the "Software"), to deal
174
+ in the Software without restriction, including without limitation the rights
175
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
176
+ copies of the Software, and to permit persons to whom the Software is
177
+ furnished to do so, subject to the following conditions:
178
+
179
+ The above copyright notice and this permission notice shall be included in all
180
+ copies or substantial portions of the Software.
181
+
182
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
183
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
184
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
185
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
186
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
187
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
188
+ SOFTWARE.
189
+ """
File without changes