hyper-connections 0.0.16__tar.gz → 0.0.17__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.
- {hyper_connections-0.0.16 → hyper_connections-0.0.17}/PKG-INFO +1 -1
- {hyper_connections-0.0.16 → hyper_connections-0.0.17}/hyper_connections/hyper_connections_with_multi_branch_inputs.py +32 -8
- {hyper_connections-0.0.16 → hyper_connections-0.0.17}/pyproject.toml +1 -1
- {hyper_connections-0.0.16 → hyper_connections-0.0.17}/.github/workflows/python-publish.yml +0 -0
- {hyper_connections-0.0.16 → hyper_connections-0.0.17}/.gitignore +0 -0
- {hyper_connections-0.0.16 → hyper_connections-0.0.17}/LICENSE +0 -0
- {hyper_connections-0.0.16 → hyper_connections-0.0.17}/README.md +0 -0
- {hyper_connections-0.0.16 → hyper_connections-0.0.17}/hyper-connections.png +0 -0
- {hyper_connections-0.0.16 → hyper_connections-0.0.17}/hyper_connections/__init__.py +0 -0
- {hyper_connections-0.0.16 → hyper_connections-0.0.17}/hyper_connections/hyper_connections.py +0 -0
|
@@ -6,8 +6,8 @@ from random import randrange
|
|
|
6
6
|
|
|
7
7
|
import torch
|
|
8
8
|
from torch import nn
|
|
9
|
-
from torch.nn import Module
|
|
10
9
|
import torch.nn.functional as F
|
|
10
|
+
from torch.nn import Module, ModuleList
|
|
11
11
|
from torch.utils._pytree import tree_flatten, tree_unflatten
|
|
12
12
|
|
|
13
13
|
from einops import rearrange, repeat, reduce, einsum
|
|
@@ -22,6 +22,9 @@ def exists(v):
|
|
|
22
22
|
def default(v, d):
|
|
23
23
|
return v if exists(v) else d
|
|
24
24
|
|
|
25
|
+
def divisible_by(num, den):
|
|
26
|
+
return (num % den) == 0
|
|
27
|
+
|
|
25
28
|
def identity(t):
|
|
26
29
|
return t
|
|
27
30
|
|
|
@@ -35,7 +38,7 @@ class HyperConnections(Module):
|
|
|
35
38
|
num_residual_streams,
|
|
36
39
|
*,
|
|
37
40
|
dim,
|
|
38
|
-
branch: Module | None = None,
|
|
41
|
+
branch: Module | tuple[Module, ...] | list[Module] | None = None,
|
|
39
42
|
layer_index = None,
|
|
40
43
|
tanh = True,
|
|
41
44
|
channel_first = False,
|
|
@@ -46,7 +49,15 @@ class HyperConnections(Module):
|
|
|
46
49
|
"""
|
|
47
50
|
super().__init__()
|
|
48
51
|
|
|
49
|
-
self.
|
|
52
|
+
self.branches = None
|
|
53
|
+
|
|
54
|
+
if isinstance(branch, Module):
|
|
55
|
+
branch = [branch]
|
|
56
|
+
|
|
57
|
+
if exists(branch):
|
|
58
|
+
assert divisible_by(num_branch_inputs, len(branch))
|
|
59
|
+
|
|
60
|
+
self.branches = ModuleList(branch)
|
|
50
61
|
|
|
51
62
|
# activation, seemingly results were wishy washy depending on using tanh or not
|
|
52
63
|
|
|
@@ -155,13 +166,22 @@ class HyperConnections(Module):
|
|
|
155
166
|
|
|
156
167
|
return output
|
|
157
168
|
|
|
158
|
-
def decorate_branch(self, branch: Callable):
|
|
159
|
-
assert not exists(self.
|
|
169
|
+
def decorate_branch(self, branch: Callable | tuple[Callable, ...] | list[Callable]):
|
|
170
|
+
assert not exists(self.branches), 'branch was already wrapped on init'
|
|
160
171
|
|
|
161
172
|
def forward_and_add_residual(residual, *args, **kwargs):
|
|
162
173
|
branch_input, add_residual = self.forward(residual)
|
|
163
174
|
|
|
164
|
-
|
|
175
|
+
if callable(branch):
|
|
176
|
+
branches = [branch]
|
|
177
|
+
else:
|
|
178
|
+
branches = branch
|
|
179
|
+
|
|
180
|
+
branch_inputs = rearrange(branch_input, '(br b) ... -> br b ...', br = len(branches))
|
|
181
|
+
|
|
182
|
+
branch_outputs = [fn(x, *args, **kwargs) for fn, x in zip(branches, branch_inputs)]
|
|
183
|
+
|
|
184
|
+
branch_output = torch.cat(branch_outputs)
|
|
165
185
|
|
|
166
186
|
residual = add_residual(branch_output)
|
|
167
187
|
|
|
@@ -180,9 +200,13 @@ class HyperConnections(Module):
|
|
|
180
200
|
|
|
181
201
|
return tree_unflatten((branch_out, *rest), tree_spec)
|
|
182
202
|
|
|
183
|
-
if not exists(self.
|
|
203
|
+
if not exists(self.branches):
|
|
184
204
|
return branch_input, add_residual_fn
|
|
185
205
|
|
|
186
|
-
|
|
206
|
+
branch_inputs = rearrange(branch_input, '(br b) ... -> br b ...', br = len(self.branches))
|
|
207
|
+
|
|
208
|
+
branch_outputs = [fn(x, *branch_args, **branch_kwargs) for fn, x in zip(self.branches, branch_inputs)]
|
|
209
|
+
|
|
210
|
+
branch_output = torch.cat(branch_outputs)
|
|
187
211
|
|
|
188
212
|
return add_residual_fn(branch_output)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{hyper_connections-0.0.16 → hyper_connections-0.0.17}/hyper_connections/hyper_connections.py
RENAMED
|
File without changes
|