log-depth-recurrent-modeling 0.0.1__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.
- log_depth_recurrent_modeling/__init__.py +7 -0
- log_depth_recurrent_modeling/ar_grc.py +167 -0
- log_depth_recurrent_modeling-0.0.1.dist-info/METADATA +88 -0
- log_depth_recurrent_modeling-0.0.1.dist-info/RECORD +6 -0
- log_depth_recurrent_modeling-0.0.1.dist-info/WHEEL +4 -0
- log_depth_recurrent_modeling-0.0.1.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
import math
|
|
3
|
+
|
|
4
|
+
import torch
|
|
5
|
+
from torch import nn
|
|
6
|
+
from torch.nn import Module, Linear, RMSNorm, LayerNorm
|
|
7
|
+
import torch.nn.functional as F
|
|
8
|
+
|
|
9
|
+
from einops import einsum, rearrange, repeat
|
|
10
|
+
|
|
11
|
+
from x_mlps_pytorch import create_mlp
|
|
12
|
+
|
|
13
|
+
from torch_einops_utils import pad_at_dim_to_multiple, pack_with_inverse
|
|
14
|
+
|
|
15
|
+
# helper functions
|
|
16
|
+
|
|
17
|
+
def exists(v):
|
|
18
|
+
return v is not None
|
|
19
|
+
|
|
20
|
+
def default(*args):
|
|
21
|
+
for arg in args:
|
|
22
|
+
if exists(arg):
|
|
23
|
+
return arg
|
|
24
|
+
return None
|
|
25
|
+
|
|
26
|
+
# classes
|
|
27
|
+
|
|
28
|
+
class GatedRecursiveCell(Module):
|
|
29
|
+
def __init__(
|
|
30
|
+
self,
|
|
31
|
+
dim,
|
|
32
|
+
dim_hidden = None,
|
|
33
|
+
activation = nn.SiLU(),
|
|
34
|
+
depth = 1,
|
|
35
|
+
use_rmsnorm = True
|
|
36
|
+
):
|
|
37
|
+
super().__init__()
|
|
38
|
+
dim_out = dim * 4
|
|
39
|
+
dim_hidden = default(dim_hidden, dim_out)
|
|
40
|
+
|
|
41
|
+
self.mlp = create_mlp(
|
|
42
|
+
dim_in = 2 * dim,
|
|
43
|
+
dim = dim_hidden,
|
|
44
|
+
dim_out = dim_out,
|
|
45
|
+
activation = activation,
|
|
46
|
+
depth = depth
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
self.norm = RMSNorm(dim) if use_rmsnorm else LayerNorm(dim, bias = False)
|
|
50
|
+
|
|
51
|
+
def forward(self, x, y):
|
|
52
|
+
|
|
53
|
+
x_gate, y_gate, c_gate, c = self.mlp((x, y)).chunk(4, dim = -1)
|
|
54
|
+
|
|
55
|
+
return self.norm(x * x_gate.sigmoid() + y * y_gate.sigmoid() + c_gate.sigmoid() * c)
|
|
56
|
+
|
|
57
|
+
# main class
|
|
58
|
+
|
|
59
|
+
class AutoregressiveGatedRecursiveCell(Module):
|
|
60
|
+
def __init__(
|
|
61
|
+
self,
|
|
62
|
+
*,
|
|
63
|
+
num_tokens,
|
|
64
|
+
dim_embed,
|
|
65
|
+
dim,
|
|
66
|
+
window_size,
|
|
67
|
+
separate_grc = False,
|
|
68
|
+
grc_kwargs: dict = dict()
|
|
69
|
+
):
|
|
70
|
+
super().__init__()
|
|
71
|
+
|
|
72
|
+
tree_depth = math.log2(window_size)
|
|
73
|
+
assert tree_depth.is_integer()
|
|
74
|
+
|
|
75
|
+
self.dim = dim
|
|
76
|
+
|
|
77
|
+
# embed
|
|
78
|
+
|
|
79
|
+
self.token_embed = nn.Embedding(num_tokens, dim_embed)
|
|
80
|
+
|
|
81
|
+
# project to and from
|
|
82
|
+
|
|
83
|
+
self.embed_to_model = Linear(dim_embed, dim)
|
|
84
|
+
self.model_to_embed = Linear(dim, dim_embed)
|
|
85
|
+
|
|
86
|
+
# recursive tree, up and down (blelloch scan)
|
|
87
|
+
|
|
88
|
+
self.window_size = window_size
|
|
89
|
+
|
|
90
|
+
self.tree_depth = int(tree_depth)
|
|
91
|
+
|
|
92
|
+
self.root_hidden = nn.Parameter(torch.randn(dim) * 1e-2)
|
|
93
|
+
|
|
94
|
+
self.separate_grc = separate_grc
|
|
95
|
+
self.up_grc = GatedRecursiveCell(dim = dim, **grc_kwargs)
|
|
96
|
+
self.down_grc = GatedRecursiveCell(dim = dim, **grc_kwargs) if separate_grc else self.up_grc
|
|
97
|
+
|
|
98
|
+
def forward(
|
|
99
|
+
self,
|
|
100
|
+
ids,
|
|
101
|
+
return_loss = False
|
|
102
|
+
):
|
|
103
|
+
up_grc, down_grc, root_hidden = self.up_grc, self.down_grc, self.root_hidden
|
|
104
|
+
|
|
105
|
+
if return_loss:
|
|
106
|
+
ids, labels = ids[:, :-1], ids[:, 1:]
|
|
107
|
+
|
|
108
|
+
embeds = self.token_embed(ids)
|
|
109
|
+
|
|
110
|
+
x = self.embed_to_model(embeds)
|
|
111
|
+
|
|
112
|
+
x, remove_padding = pad_at_dim_to_multiple(x, multiple = self.window_size, dim = -2)
|
|
113
|
+
|
|
114
|
+
# divide into window size
|
|
115
|
+
|
|
116
|
+
x = rearrange(x, 'b (w n) d -> b w n d', n = self.window_size)
|
|
117
|
+
x, inverse_pack_window = pack_with_inverse(x, '* n d')
|
|
118
|
+
|
|
119
|
+
# up sweep
|
|
120
|
+
|
|
121
|
+
curr = x
|
|
122
|
+
up_hiddens = [x]
|
|
123
|
+
|
|
124
|
+
for _ in range(self.tree_depth - 1):
|
|
125
|
+
|
|
126
|
+
left, right = rearrange(curr, 'b (h two) d -> two b h d', two = 2)
|
|
127
|
+
curr = up_grc(left, right)
|
|
128
|
+
|
|
129
|
+
up_hiddens.append(curr)
|
|
130
|
+
|
|
131
|
+
# down sweep (blelloch)
|
|
132
|
+
|
|
133
|
+
curr = repeat(root_hidden, 'd -> b 1 d', b = x.shape[0])
|
|
134
|
+
|
|
135
|
+
for _ in range(self.tree_depth):
|
|
136
|
+
left_up, _ = rearrange(up_hiddens.pop(), 'b (h two) d -> two b h d', two = 2)
|
|
137
|
+
|
|
138
|
+
right_carry = down_grc(curr, left_up)
|
|
139
|
+
|
|
140
|
+
curr = rearrange([curr, right_carry], 'two b h d -> b (h two) d')
|
|
141
|
+
|
|
142
|
+
# include each leaf
|
|
143
|
+
|
|
144
|
+
x = down_grc(curr, x)
|
|
145
|
+
|
|
146
|
+
# restore window
|
|
147
|
+
|
|
148
|
+
x = inverse_pack_window(x)
|
|
149
|
+
x = rearrange(x, 'b w n d -> b (w n) d')
|
|
150
|
+
|
|
151
|
+
# slice out padding
|
|
152
|
+
|
|
153
|
+
x = remove_padding(x)
|
|
154
|
+
|
|
155
|
+
embeds = self.model_to_embed(x)
|
|
156
|
+
|
|
157
|
+
logits = einsum(embeds, self.token_embed.weight, 'b n d, l d -> b n l')
|
|
158
|
+
|
|
159
|
+
if not return_loss:
|
|
160
|
+
return logits
|
|
161
|
+
|
|
162
|
+
return F.cross_entropy(rearrange(logits, 'b n l -> b l n'), labels, ignore_index = -1)
|
|
163
|
+
|
|
164
|
+
# alias
|
|
165
|
+
|
|
166
|
+
GRC = GatedRecursiveCell
|
|
167
|
+
ARGRC = AutoregressiveGatedRecursiveCell
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: log-depth-recurrent-modeling
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Log Depth Recurrent Modeling
|
|
5
|
+
Project-URL: Homepage, https://pypi.org/project/log-depth-recurrent-modeling/
|
|
6
|
+
Project-URL: Repository, https://github.com/lucidrains/log-depth-recurrent-modeling
|
|
7
|
+
Author-email: Phil Wang <lucidrains@gmail.com>
|
|
8
|
+
License: MIT License
|
|
9
|
+
|
|
10
|
+
Copyright (c) 2026 Phil Wang
|
|
11
|
+
|
|
12
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
13
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
14
|
+
in the Software without restriction, including without limitation the rights
|
|
15
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
16
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
17
|
+
furnished to do so, subject to the following conditions:
|
|
18
|
+
|
|
19
|
+
The above copyright notice and this permission notice shall be included in all
|
|
20
|
+
copies or substantial portions of the Software.
|
|
21
|
+
|
|
22
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
23
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
24
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
25
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
26
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
27
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
28
|
+
SOFTWARE.
|
|
29
|
+
License-File: LICENSE
|
|
30
|
+
Keywords: artificial intelligence,deep learning
|
|
31
|
+
Classifier: Development Status :: 4 - Beta
|
|
32
|
+
Classifier: Intended Audience :: Developers
|
|
33
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
34
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
35
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
36
|
+
Requires-Python: >=3.10
|
|
37
|
+
Requires-Dist: einops>=0.8.1
|
|
38
|
+
Requires-Dist: torch-einops-utils>=0.1.30
|
|
39
|
+
Requires-Dist: torch>=2.5
|
|
40
|
+
Requires-Dist: x-mlps-pytorch>=0.6.2
|
|
41
|
+
Provides-Extra: examples
|
|
42
|
+
Provides-Extra: test
|
|
43
|
+
Requires-Dist: pytest; extra == 'test'
|
|
44
|
+
Description-Content-Type: text/markdown
|
|
45
|
+
|
|
46
|
+
<img src="./ldrlm-fig1.png" width="350px"></img>
|
|
47
|
+
|
|
48
|
+
## Log Depth Recurrent Modeling (wip)
|
|
49
|
+
|
|
50
|
+
Explorations into the [Log Depth Recurrent Modeling](https://arxiv.org/abs/2609.28212) proposed by Yiqin Wang of Imperial College London
|
|
51
|
+
|
|
52
|
+
## Citations
|
|
53
|
+
|
|
54
|
+
```bibtex
|
|
55
|
+
@misc{wang2026logdepthrecurrentlanguagemodeling,
|
|
56
|
+
title = {Log-Depth Recurrent Language Modeling},
|
|
57
|
+
author = {Yiqin Wang and Nuri Cingillioglu and Charles Pert},
|
|
58
|
+
year = {2026},
|
|
59
|
+
eprint = {2609.28212},
|
|
60
|
+
archivePrefix = {arXiv},
|
|
61
|
+
primaryClass = {cs.LG},
|
|
62
|
+
url = {https://arxiv.org/abs/2609.28212},
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
```bibtex
|
|
67
|
+
@misc{shen2019orderedmemory,
|
|
68
|
+
title = {Ordered Memory},
|
|
69
|
+
author = {Yikang Shen and Shawn Tan and Arian Hosseini and Zhouhan Lin and Alessandro Sordoni and Aaron Courville},
|
|
70
|
+
year = {2019},
|
|
71
|
+
eprint = {1910.13466},
|
|
72
|
+
archivePrefix = {arXiv},
|
|
73
|
+
primaryClass = {cs.LG},
|
|
74
|
+
url = {https://arxiv.org/abs/1910.13466},
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
```bibtex
|
|
79
|
+
@misc{pert2026lengthgeneralizationlogdepthrecurrent,
|
|
80
|
+
title = {Length Generalization with Log-Depth Recurrent Units},
|
|
81
|
+
author = {Charles Pert and Dalal Alrajeh and Alessandra Russo},
|
|
82
|
+
year = {2026},
|
|
83
|
+
eprint = {2605.26035},
|
|
84
|
+
archivePrefix = {arXiv},
|
|
85
|
+
primaryClass = {cs.LG},
|
|
86
|
+
url = {https://arxiv.org/abs/2605.26035},
|
|
87
|
+
}
|
|
88
|
+
```
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
log_depth_recurrent_modeling/__init__.py,sha256=2JG1vAchNKPZ3QMhlmL9dLkw61VFhGzai5w-SQQhH2M,143
|
|
2
|
+
log_depth_recurrent_modeling/ar_grc.py,sha256=CesL3qGlA9JIXb3BpixX7YDeFi6Q7ORzH5TFqh2srV0,4085
|
|
3
|
+
log_depth_recurrent_modeling-0.0.1.dist-info/METADATA,sha256=trKFJhn6lb7s6fEwdXDmv0MgBXV6IHzJUQX1_pNdtkw,3490
|
|
4
|
+
log_depth_recurrent_modeling-0.0.1.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
|
|
5
|
+
log_depth_recurrent_modeling-0.0.1.dist-info/licenses/LICENSE,sha256=e6AOF7Z8EFdK3IdcL0x0fLw4cY7Q0d0kNR0o0TmBewM,1066
|
|
6
|
+
log_depth_recurrent_modeling-0.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Phil Wang
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|