torch-einops-kit 0.1.7__tar.gz → 0.1.8__tar.gz

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.
Files changed (20) hide show
  1. {torch_einops_kit-0.1.7 → torch_einops_kit-0.1.8}/PKG-INFO +1 -1
  2. {torch_einops_kit-0.1.7 → torch_einops_kit-0.1.8}/pyproject.toml +1 -1
  3. {torch_einops_kit-0.1.7 → torch_einops_kit-0.1.8}/src/torch_einops_kit/__init__.py +1 -1
  4. {torch_einops_kit-0.1.7 → torch_einops_kit-0.1.8}/src/torch_einops_kit/_theTypes.py +5 -2
  5. {torch_einops_kit-0.1.7 → torch_einops_kit-0.1.8}/src/torch_einops_kit/nn.py +13 -2
  6. {torch_einops_kit-0.1.7 → torch_einops_kit-0.1.8}/src/torch_einops_kit/utils.py +2 -1
  7. {torch_einops_kit-0.1.7 → torch_einops_kit-0.1.8}/LICENSE +0 -0
  8. {torch_einops_kit-0.1.7 → torch_einops_kit-0.1.8}/LICENSE-WANG +0 -0
  9. {torch_einops_kit-0.1.7 → torch_einops_kit-0.1.8}/README.md +0 -0
  10. {torch_einops_kit-0.1.7 → torch_einops_kit-0.1.8}/src/torch_einops_kit/_cat_and_stack.py +0 -0
  11. {torch_einops_kit-0.1.7 → torch_einops_kit-0.1.8}/src/torch_einops_kit/_dimensions.py +0 -0
  12. {torch_einops_kit-0.1.7 → torch_einops_kit-0.1.8}/src/torch_einops_kit/_helpers.py +0 -0
  13. {torch_einops_kit-0.1.7 → torch_einops_kit-0.1.8}/src/torch_einops_kit/_masking.py +0 -0
  14. {torch_einops_kit-0.1.7 → torch_einops_kit-0.1.8}/src/torch_einops_kit/_padding.py +0 -0
  15. {torch_einops_kit-0.1.7 → torch_einops_kit-0.1.8}/src/torch_einops_kit/_slicing.py +0 -0
  16. {torch_einops_kit-0.1.7 → torch_einops_kit-0.1.8}/src/torch_einops_kit/device.py +0 -0
  17. {torch_einops_kit-0.1.7 → torch_einops_kit-0.1.8}/src/torch_einops_kit/einops.py +0 -0
  18. {torch_einops_kit-0.1.7 → torch_einops_kit-0.1.8}/src/torch_einops_kit/py.typed +0 -0
  19. {torch_einops_kit-0.1.7 → torch_einops_kit-0.1.8}/src/torch_einops_kit/save_load.py +0 -0
  20. {torch_einops_kit-0.1.7 → torch_einops_kit-0.1.8}/src/torch_einops_kit/scaleValues.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: torch-einops-kit
3
- Version: 0.1.7
3
+ Version: 0.1.8
4
4
  Summary: Typed tensor-shaping, masking, padding, device-routing, and checkpoint utilities for PyTorch and `einops`. A superset of `lucidrains/torch-einops-utils` with similar utilities from other lucidrains packages. Adds strict typing, extensive tests, and comprehensive docstrings.
5
5
  Keywords: artificial intelligence,einops,machine learning,pytorch,torch
6
6
  Author: Hunter Hogan
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "torch-einops-kit"
3
- version = "0.1.7"
3
+ version = "0.1.8"
4
4
  description = "Typed tensor-shaping, masking, padding, device-routing, and checkpoint utilities for PyTorch and `einops`. A superset of `lucidrains/torch-einops-utils` with similar utilities from other lucidrains packages. Adds strict typing, extensive tests, and comprehensive docstrings."
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.10"
@@ -129,7 +129,7 @@ from torch_einops_kit._theTypes import (
129
129
  ConfigArgsKwargs as ConfigArgsKwargs, DehydratedCheckpoint as DehydratedCheckpoint, DehydratedTorchNNModule as DehydratedTorchNNModule,
130
130
  DimAndValue as DimAndValue, IdentityCallable as IdentityCallable, InversePackListTensors as InversePackListTensors,
131
131
  InversePackTensor as InversePackTensor, PSpec as PSpec, RVar as RVar, StrPath as StrPath, SupportsIntIndex as SupportsIntIndex,
132
- T_co as T_co, TorchNNModule as TorchNNModule, TVar as TVar)
132
+ T_co as T_co, TorchNNModule as TorchNNModule, TVar as TVar, 木 as 木)
133
133
 
134
134
  # isort: split
135
135
  from torch_einops_kit._helpers import (
@@ -1,12 +1,14 @@
1
- # ruff: noqa: E224
1
+ # ruff: noqa: E224 PLC2701
2
2
  from __future__ import annotations
3
3
 
4
4
  from os import PathLike
5
5
  from torch.nn import Module
6
- from typing import Any, Literal, overload, ParamSpec, Protocol, TYPE_CHECKING, TypeAlias, TypedDict, TypeVar
6
+ from torch.utils._pytree import PyTree
7
+ from typing import Any, overload, ParamSpec, Protocol, TYPE_CHECKING, TypedDict, TypeVar
7
8
 
8
9
  if TYPE_CHECKING:
9
10
  from torch import Tensor
11
+ from typing import Literal, TypeAlias
10
12
 
11
13
  ConfigArgsKwargs: TypeAlias = tuple[tuple[Any, ...], dict[Any, Any]]
12
14
  """Represent the positional arguments and keyword arguments of a decorated constructor.
@@ -176,6 +178,7 @@ References
176
178
 
177
179
  [7] torch_einops_kit.first
178
180
  """
181
+ 木 = TypeVar('木', bound=PyTree)
179
182
 
180
183
  class DehydratedCheckpoint(TypedDict):
181
184
  """Represent the checkpoint dictionary written and read by the `save_load` decorator.
@@ -16,8 +16,8 @@ Classes
16
16
  from __future__ import annotations
17
17
 
18
18
  from torch import nn
19
- from torch_einops_kit import compact, identity, PSpec, RVar
20
- from typing import Generic, TYPE_CHECKING
19
+ from torch_einops_kit import compact, identity, PSpec, RVar, tree_flatten_with_inverse, TVar, 木
20
+ from typing import Concatenate, Generic, TYPE_CHECKING
21
21
 
22
22
  if TYPE_CHECKING:
23
23
  from collections.abc import Callable
@@ -151,6 +151,17 @@ class Lambda(nn.Module, Generic[PSpec, RVar]):
151
151
  """
152
152
  return self.fn(*args, **kwargs)
153
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
+
154
165
  """
155
166
  Some of the logic in this module may be protected by the following.
156
167
 
@@ -20,6 +20,7 @@ from typing import Any, TYPE_CHECKING
20
20
 
21
21
  if TYPE_CHECKING:
22
22
  from collections.abc import Callable, Iterable
23
+ from torch_einops_kit import 木
23
24
 
24
25
  def tree_map_tensor(fn: Callable[[Tensor], Tensor], tree: PyTree) -> PyTree:
25
26
  """Apply `fn` to every `torch.Tensor` leaf in `tree`, leaving non-tensor leaves unchanged.
@@ -88,7 +89,7 @@ def tree_map_tensor(fn: Callable[[Tensor], Tensor], tree: PyTree) -> PyTree:
88
89
 
89
90
  return tree_map(func, tree)
90
91
 
91
- def tree_flatten_with_inverse(tree: PyTree) -> tuple[list[Any], Callable[[Iterable[Any]], PyTree]]:
92
+ def tree_flatten_with_inverse(tree: 木) -> tuple[list[Any], Callable[[Iterable[Any]], 木]]:
92
93
  """Flatten `tree` into a list of leaves and return a paired inverse function.
93
94
 
94
95
  You can use this function to decompose a nested PyTree [1] structure into a flat list of leaves