hyper-connections 0.0.14__tar.gz → 0.0.16__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: hyper-connections
3
- Version: 0.0.14
3
+ Version: 0.0.16
4
4
  Summary: Hyper-Connections
5
5
  Project-URL: Homepage, https://pypi.org/project/hyper-connections/
6
6
  Project-URL: Repository, https://github.com/lucidrains/hyper-connections
@@ -121,7 +121,11 @@ class HyperConnections(Module):
121
121
  self.channel_first = channel_first
122
122
 
123
123
  @classmethod
124
- def get_expand_reduce_stream_functions(cls, num_streams):
124
+ def get_expand_reduce_stream_functions(cls, num_streams, disable = False):
125
+
126
+ if disable:
127
+ return (identity, identity)
128
+
125
129
  expand_fn = partial(repeat, pattern = 'b ... -> (b s) ...', s = num_streams)
126
130
  reduce_fn = partial(reduce, pattern = '(b s) ... -> b ...', reduction = 'sum', s = num_streams)
127
131
 
@@ -133,7 +137,7 @@ class HyperConnections(Module):
133
137
  hyper_conn_klass = cls if not disable else Residual
134
138
 
135
139
  init_hyper_conn_fn = partial(hyper_conn_klass, num_streams)
136
- expand_reduce_fns = cls.get_expand_reduce_stream_functions(num_streams) if not disable else (identity, identity)
140
+ expand_reduce_fns = cls.get_expand_reduce_stream_functions(num_streams, disable = disable)
137
141
 
138
142
  return (init_hyper_conn_fn, *expand_reduce_fns)
139
143
 
@@ -57,12 +57,19 @@ class HyperConnections(Module):
57
57
  self.num_residual_streams = num_residual_streams
58
58
  self.num_branch_inputs = num_branch_inputs
59
59
 
60
- init_residual_index = default(layer_index, randrange(num_residual_streams)) % num_residual_streams # just choose one random residual stream if layer index not given
61
-
62
60
  self.static_beta = nn.Parameter(torch.ones(num_residual_streams, num_branch_inputs))
63
61
 
64
- init_alpha0 = torch.zeros((num_residual_streams, num_branch_inputs))
65
- init_alpha0[init_residual_index, :] = 1.
62
+ # make sure each branch input receives from different residual stream on init
63
+
64
+ stream_branches = num_residual_streams * num_branch_inputs
65
+ layer_index = default(layer_index, randrange(stream_branches))
66
+ layer_offset = layer_index % stream_branches * num_branch_inputs
67
+
68
+ stream_seq = torch.arange(num_residual_streams)
69
+ branch_input_seq = torch.arange(num_branch_inputs)
70
+
71
+ init_alpha0 = rearrange(stream_seq, 's -> s 1') + rearrange(branch_input_seq, 'bi -> 1 bi') + layer_offset
72
+ init_alpha0 = ((init_alpha0 % num_residual_streams) == 0).float()
66
73
 
67
74
  self.static_alpha = nn.Parameter(torch.cat([init_alpha0, torch.eye(num_residual_streams)], dim = 1))
68
75
 
@@ -76,7 +83,10 @@ class HyperConnections(Module):
76
83
  self.channel_first = channel_first
77
84
 
78
85
  @classmethod
79
- def get_expand_reduce_stream_functions(cls, num_streams):
86
+ def get_expand_reduce_stream_functions(cls, num_streams, disable = False):
87
+ if disable:
88
+ return (identity, identity)
89
+
80
90
  expand_fn = partial(repeat, pattern = 'b ... -> (b s) ...', s = num_streams)
81
91
  reduce_fn = partial(reduce, pattern = '(b s) ... -> b ...', reduction = 'sum', s = num_streams)
82
92
 
@@ -88,7 +98,7 @@ class HyperConnections(Module):
88
98
  hyper_conn_klass = cls if not disable else Residual
89
99
 
90
100
  init_hyper_conn_fn = partial(hyper_conn_klass, num_streams)
91
- expand_reduce_fns = cls.get_expand_reduce_stream_functions(num_streams) if not disable else (identity, identity)
101
+ expand_reduce_fns = cls.get_expand_reduce_stream_functions(num_streams, disable = disable)
92
102
 
93
103
  return (init_hyper_conn_fn, *expand_reduce_fns)
94
104
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "hyper-connections"
3
- version = "0.0.14"
3
+ version = "0.0.16"
4
4
  description = "Hyper-Connections"
5
5
  authors = [
6
6
  { name = "Phil Wang", email = "lucidrains@gmail.com" }