log-depth-recurrent-modeling 0.0.1__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.
- log_depth_recurrent_modeling-0.0.1/.gitignore +220 -0
- log_depth_recurrent_modeling-0.0.1/LICENSE +21 -0
- log_depth_recurrent_modeling-0.0.1/PKG-INFO +88 -0
- log_depth_recurrent_modeling-0.0.1/README.md +43 -0
- log_depth_recurrent_modeling-0.0.1/log_depth_recurrent_modeling/__init__.py +7 -0
- log_depth_recurrent_modeling-0.0.1/log_depth_recurrent_modeling/ar_grc.py +167 -0
- log_depth_recurrent_modeling-0.0.1/pyproject.toml +66 -0
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
*.sh
|
|
2
|
+
|
|
3
|
+
# Byte-compiled / optimized / DLL files
|
|
4
|
+
__pycache__/
|
|
5
|
+
*.py[codz]
|
|
6
|
+
*$py.class
|
|
7
|
+
|
|
8
|
+
# C extensions
|
|
9
|
+
*.so
|
|
10
|
+
|
|
11
|
+
# Distribution / packaging
|
|
12
|
+
.Python
|
|
13
|
+
build/
|
|
14
|
+
develop-eggs/
|
|
15
|
+
dist/
|
|
16
|
+
downloads/
|
|
17
|
+
eggs/
|
|
18
|
+
.eggs/
|
|
19
|
+
lib/
|
|
20
|
+
lib64/
|
|
21
|
+
parts/
|
|
22
|
+
sdist/
|
|
23
|
+
var/
|
|
24
|
+
wheels/
|
|
25
|
+
share/python-wheels/
|
|
26
|
+
*.egg-info/
|
|
27
|
+
.installed.cfg
|
|
28
|
+
*.egg
|
|
29
|
+
MANIFEST
|
|
30
|
+
|
|
31
|
+
# PyInstaller
|
|
32
|
+
# Usually these files are written by a python script from a template
|
|
33
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
34
|
+
*.manifest
|
|
35
|
+
*.spec
|
|
36
|
+
|
|
37
|
+
# Installer logs
|
|
38
|
+
pip-log.txt
|
|
39
|
+
pip-delete-this-directory.txt
|
|
40
|
+
|
|
41
|
+
# Unit test / coverage reports
|
|
42
|
+
htmlcov/
|
|
43
|
+
.tox/
|
|
44
|
+
.nox/
|
|
45
|
+
.coverage
|
|
46
|
+
.coverage.*
|
|
47
|
+
.cache
|
|
48
|
+
nosetests.xml
|
|
49
|
+
coverage.xml
|
|
50
|
+
*.cover
|
|
51
|
+
*.py.cover
|
|
52
|
+
.hypothesis/
|
|
53
|
+
.pytest_cache/
|
|
54
|
+
cover/
|
|
55
|
+
|
|
56
|
+
# Translations
|
|
57
|
+
*.mo
|
|
58
|
+
*.pot
|
|
59
|
+
|
|
60
|
+
# Django stuff:
|
|
61
|
+
*.log
|
|
62
|
+
local_settings.py
|
|
63
|
+
db.sqlite3
|
|
64
|
+
db.sqlite3-journal
|
|
65
|
+
|
|
66
|
+
# Flask stuff:
|
|
67
|
+
instance/
|
|
68
|
+
.webassets-cache
|
|
69
|
+
|
|
70
|
+
# Scrapy stuff:
|
|
71
|
+
.scrapy
|
|
72
|
+
|
|
73
|
+
# Sphinx documentation
|
|
74
|
+
docs/_build/
|
|
75
|
+
|
|
76
|
+
# PyBuilder
|
|
77
|
+
.pybuilder/
|
|
78
|
+
target/
|
|
79
|
+
|
|
80
|
+
# Jupyter Notebook
|
|
81
|
+
.ipynb_checkpoints
|
|
82
|
+
|
|
83
|
+
# IPython
|
|
84
|
+
profile_default/
|
|
85
|
+
ipython_config.py
|
|
86
|
+
|
|
87
|
+
# pyenv
|
|
88
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
89
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
90
|
+
# .python-version
|
|
91
|
+
|
|
92
|
+
# pipenv
|
|
93
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
94
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
95
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
96
|
+
# install all needed dependencies.
|
|
97
|
+
# Pipfile.lock
|
|
98
|
+
|
|
99
|
+
# UV
|
|
100
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
101
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
102
|
+
# commonly ignored for libraries.
|
|
103
|
+
# uv.lock
|
|
104
|
+
|
|
105
|
+
# poetry
|
|
106
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
107
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
108
|
+
# commonly ignored for libraries.
|
|
109
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
110
|
+
# poetry.lock
|
|
111
|
+
# poetry.toml
|
|
112
|
+
|
|
113
|
+
# pdm
|
|
114
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
115
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
116
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
117
|
+
# pdm.lock
|
|
118
|
+
# pdm.toml
|
|
119
|
+
.pdm-python
|
|
120
|
+
.pdm-build/
|
|
121
|
+
|
|
122
|
+
# pixi
|
|
123
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
124
|
+
# pixi.lock
|
|
125
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
126
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
127
|
+
.pixi
|
|
128
|
+
|
|
129
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
130
|
+
__pypackages__/
|
|
131
|
+
|
|
132
|
+
# Celery stuff
|
|
133
|
+
celerybeat-schedule
|
|
134
|
+
celerybeat.pid
|
|
135
|
+
|
|
136
|
+
# Redis
|
|
137
|
+
*.rdb
|
|
138
|
+
*.aof
|
|
139
|
+
*.pid
|
|
140
|
+
|
|
141
|
+
# RabbitMQ
|
|
142
|
+
mnesia/
|
|
143
|
+
rabbitmq/
|
|
144
|
+
rabbitmq-data/
|
|
145
|
+
|
|
146
|
+
# ActiveMQ
|
|
147
|
+
activemq-data/
|
|
148
|
+
|
|
149
|
+
# SageMath parsed files
|
|
150
|
+
*.sage.py
|
|
151
|
+
|
|
152
|
+
# Environments
|
|
153
|
+
.env
|
|
154
|
+
.envrc
|
|
155
|
+
.venv
|
|
156
|
+
env/
|
|
157
|
+
venv/
|
|
158
|
+
ENV/
|
|
159
|
+
env.bak/
|
|
160
|
+
venv.bak/
|
|
161
|
+
|
|
162
|
+
# Spyder project settings
|
|
163
|
+
.spyderproject
|
|
164
|
+
.spyproject
|
|
165
|
+
|
|
166
|
+
# Rope project settings
|
|
167
|
+
.ropeproject
|
|
168
|
+
|
|
169
|
+
# mkdocs documentation
|
|
170
|
+
/site
|
|
171
|
+
|
|
172
|
+
# mypy
|
|
173
|
+
.mypy_cache/
|
|
174
|
+
.dmypy.json
|
|
175
|
+
dmypy.json
|
|
176
|
+
|
|
177
|
+
# Pyre type checker
|
|
178
|
+
.pyre/
|
|
179
|
+
|
|
180
|
+
# pytype static type analyzer
|
|
181
|
+
.pytype/
|
|
182
|
+
|
|
183
|
+
# Cython debug symbols
|
|
184
|
+
cython_debug/
|
|
185
|
+
|
|
186
|
+
# PyCharm
|
|
187
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
188
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
189
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
190
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
191
|
+
# .idea/
|
|
192
|
+
|
|
193
|
+
# Abstra
|
|
194
|
+
# Abstra is an AI-powered process automation framework.
|
|
195
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
196
|
+
# Learn more at https://abstra.io/docs
|
|
197
|
+
.abstra/
|
|
198
|
+
|
|
199
|
+
# Visual Studio Code
|
|
200
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
201
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
202
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
203
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
204
|
+
# .vscode/
|
|
205
|
+
# Temporary file for partial code execution
|
|
206
|
+
tempCodeRunnerFile.py
|
|
207
|
+
|
|
208
|
+
# Ruff stuff:
|
|
209
|
+
.ruff_cache/
|
|
210
|
+
|
|
211
|
+
# PyPI configuration file
|
|
212
|
+
.pypirc
|
|
213
|
+
|
|
214
|
+
# Marimo
|
|
215
|
+
marimo/_static/
|
|
216
|
+
marimo/_lsp/
|
|
217
|
+
__marimo__/
|
|
218
|
+
|
|
219
|
+
# Streamlit
|
|
220
|
+
.streamlit/secrets.toml
|
|
@@ -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.
|
|
@@ -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,43 @@
|
|
|
1
|
+
<img src="./ldrlm-fig1.png" width="350px"></img>
|
|
2
|
+
|
|
3
|
+
## Log Depth Recurrent Modeling (wip)
|
|
4
|
+
|
|
5
|
+
Explorations into the [Log Depth Recurrent Modeling](https://arxiv.org/abs/2609.28212) proposed by Yiqin Wang of Imperial College London
|
|
6
|
+
|
|
7
|
+
## Citations
|
|
8
|
+
|
|
9
|
+
```bibtex
|
|
10
|
+
@misc{wang2026logdepthrecurrentlanguagemodeling,
|
|
11
|
+
title = {Log-Depth Recurrent Language Modeling},
|
|
12
|
+
author = {Yiqin Wang and Nuri Cingillioglu and Charles Pert},
|
|
13
|
+
year = {2026},
|
|
14
|
+
eprint = {2609.28212},
|
|
15
|
+
archivePrefix = {arXiv},
|
|
16
|
+
primaryClass = {cs.LG},
|
|
17
|
+
url = {https://arxiv.org/abs/2609.28212},
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```bibtex
|
|
22
|
+
@misc{shen2019orderedmemory,
|
|
23
|
+
title = {Ordered Memory},
|
|
24
|
+
author = {Yikang Shen and Shawn Tan and Arian Hosseini and Zhouhan Lin and Alessandro Sordoni and Aaron Courville},
|
|
25
|
+
year = {2019},
|
|
26
|
+
eprint = {1910.13466},
|
|
27
|
+
archivePrefix = {arXiv},
|
|
28
|
+
primaryClass = {cs.LG},
|
|
29
|
+
url = {https://arxiv.org/abs/1910.13466},
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
```bibtex
|
|
34
|
+
@misc{pert2026lengthgeneralizationlogdepthrecurrent,
|
|
35
|
+
title = {Length Generalization with Log-Depth Recurrent Units},
|
|
36
|
+
author = {Charles Pert and Dalal Alrajeh and Alessandra Russo},
|
|
37
|
+
year = {2026},
|
|
38
|
+
eprint = {2605.26035},
|
|
39
|
+
archivePrefix = {arXiv},
|
|
40
|
+
primaryClass = {cs.LG},
|
|
41
|
+
url = {https://arxiv.org/abs/2605.26035},
|
|
42
|
+
}
|
|
43
|
+
```
|
|
@@ -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,66 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "log-depth-recurrent-modeling"
|
|
3
|
+
version = "0.0.1"
|
|
4
|
+
description = "Log Depth Recurrent Modeling"
|
|
5
|
+
authors = [
|
|
6
|
+
{ name = "Phil Wang", email = "lucidrains@gmail.com" }
|
|
7
|
+
]
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
requires-python = ">= 3.10"
|
|
10
|
+
license = { file = "LICENSE" }
|
|
11
|
+
keywords = [
|
|
12
|
+
'artificial intelligence',
|
|
13
|
+
'deep learning'
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
classifiers = [
|
|
17
|
+
'Development Status :: 4 - Beta',
|
|
18
|
+
'Intended Audience :: Developers',
|
|
19
|
+
'Topic :: Scientific/Engineering :: Artificial Intelligence',
|
|
20
|
+
'License :: OSI Approved :: MIT License',
|
|
21
|
+
'Programming Language :: Python :: 3.10',
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
dependencies = [
|
|
25
|
+
"einops>=0.8.1",
|
|
26
|
+
"torch>=2.5",
|
|
27
|
+
"torch-einops-utils>=0.1.30",
|
|
28
|
+
"x-mlps-pytorch>=0.6.2"
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
[project.urls]
|
|
32
|
+
Homepage = "https://pypi.org/project/log-depth-recurrent-modeling/"
|
|
33
|
+
Repository = "https://github.com/lucidrains/log-depth-recurrent-modeling"
|
|
34
|
+
|
|
35
|
+
[project.optional-dependencies]
|
|
36
|
+
examples = []
|
|
37
|
+
test = [
|
|
38
|
+
"pytest"
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
[tool.pytest.ini_options]
|
|
42
|
+
pythonpath = [
|
|
43
|
+
"."
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
[build-system]
|
|
47
|
+
requires = ["hatchling"]
|
|
48
|
+
build-backend = "hatchling.build"
|
|
49
|
+
|
|
50
|
+
[tool.rye]
|
|
51
|
+
managed = true
|
|
52
|
+
dev-dependencies = []
|
|
53
|
+
|
|
54
|
+
[tool.hatch.metadata]
|
|
55
|
+
allow-direct-references = true
|
|
56
|
+
|
|
57
|
+
[tool.hatch.build]
|
|
58
|
+
include = [
|
|
59
|
+
"log_depth_recurrent_modeling",
|
|
60
|
+
"pyproject.toml",
|
|
61
|
+
"README.md",
|
|
62
|
+
"LICENSE"
|
|
63
|
+
]
|
|
64
|
+
|
|
65
|
+
[tool.hatch.build.targets.wheel]
|
|
66
|
+
packages = ["log_depth_recurrent_modeling"]
|