RR-layer 0.1.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.
@@ -0,0 +1,221 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[codz]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ *.pth
28
+ *.npz
29
+ *.png
30
+ MANIFEST
31
+
32
+ # PyInstaller
33
+ # Usually these files are written by a python script from a template
34
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
35
+ *.manifest
36
+ *.spec
37
+
38
+ # Installer logs
39
+ pip-log.txt
40
+ pip-delete-this-directory.txt
41
+
42
+ # Unit test / coverage reports
43
+ htmlcov/
44
+ .tox/
45
+ .nox/
46
+ .coverage
47
+ .coverage.*
48
+ .cache
49
+ nosetests.xml
50
+ coverage.xml
51
+ *.cover
52
+ *.py.cover
53
+ .hypothesis/
54
+ .pytest_cache/
55
+ cover/
56
+
57
+ # Translations
58
+ *.mo
59
+ *.pot
60
+
61
+ # Django stuff:
62
+ *.log
63
+ local_settings.py
64
+ db.sqlite3
65
+ db.sqlite3-journal
66
+
67
+ # Flask stuff:
68
+ instance/
69
+ .webassets-cache
70
+
71
+ # Scrapy stuff:
72
+ .scrapy
73
+
74
+ # Sphinx documentation
75
+ docs/_build/
76
+
77
+ # PyBuilder
78
+ .pybuilder/
79
+ target/
80
+
81
+ # Jupyter Notebook
82
+ .ipynb_checkpoints
83
+
84
+ # IPython
85
+ profile_default/
86
+ ipython_config.py
87
+
88
+ # pyenv
89
+ # For a library or package, you might want to ignore these files since the code is
90
+ # intended to run in multiple environments; otherwise, check them in:
91
+ # .python-version
92
+
93
+ # pipenv
94
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
95
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
96
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
97
+ # install all needed dependencies.
98
+ # Pipfile.lock
99
+
100
+ # UV
101
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
102
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
103
+ # commonly ignored for libraries.
104
+ # uv.lock
105
+
106
+ # poetry
107
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
108
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
109
+ # commonly ignored for libraries.
110
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
111
+ # poetry.lock
112
+ # poetry.toml
113
+
114
+ # pdm
115
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
116
+ # pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
117
+ # https://pdm-project.org/en/latest/usage/project/#working-with-version-control
118
+ # pdm.lock
119
+ # pdm.toml
120
+ .pdm-python
121
+ .pdm-build/
122
+
123
+ # pixi
124
+ # Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
125
+ # pixi.lock
126
+ # Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
127
+ # in the .venv directory. It is recommended not to include this directory in version control.
128
+ .pixi
129
+
130
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
131
+ __pypackages__/
132
+
133
+ # Celery stuff
134
+ celerybeat-schedule
135
+ celerybeat.pid
136
+
137
+ # Redis
138
+ *.rdb
139
+ *.aof
140
+ *.pid
141
+
142
+ # RabbitMQ
143
+ mnesia/
144
+ rabbitmq/
145
+ rabbitmq-data/
146
+
147
+ # ActiveMQ
148
+ activemq-data/
149
+
150
+ # SageMath parsed files
151
+ *.sage.py
152
+
153
+ # Environments
154
+ .env
155
+ .envrc
156
+ .venv
157
+ env/
158
+ venv/
159
+ ENV/
160
+ env.bak/
161
+ venv.bak/
162
+
163
+ # Spyder project settings
164
+ .spyderproject
165
+ .spyproject
166
+
167
+ # Rope project settings
168
+ .ropeproject
169
+
170
+ # mkdocs documentation
171
+ /site
172
+
173
+ # mypy
174
+ .mypy_cache/
175
+ .dmypy.json
176
+ dmypy.json
177
+
178
+ # Pyre type checker
179
+ .pyre/
180
+
181
+ # pytype static type analyzer
182
+ .pytype/
183
+
184
+ # Cython debug symbols
185
+ cython_debug/
186
+
187
+ # PyCharm
188
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
189
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
190
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
191
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
192
+ # .idea/
193
+
194
+ # Abstra
195
+ # Abstra is an AI-powered process automation framework.
196
+ # Ignore directories containing user credentials, local state, and settings.
197
+ # Learn more at https://abstra.io/docs
198
+ .abstra/
199
+
200
+ # Visual Studio Code
201
+ # Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
202
+ # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
203
+ # and can be added to the global gitignore or merged into this file. However, if you prefer,
204
+ # you could uncomment the following to ignore the entire vscode folder
205
+ # .vscode/
206
+ # Temporary file for partial code execution
207
+ tempCodeRunnerFile.py
208
+
209
+ # Ruff stuff:
210
+ .ruff_cache/
211
+
212
+ # PyPI configuration file
213
+ .pypirc
214
+
215
+ # Marimo
216
+ marimo/_static/
217
+ marimo/_lsp/
218
+ __marimo__/
219
+
220
+ # Streamlit
221
+ .streamlit/secrets.toml
rr_layer-0.1.1/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Jad Mounayer
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,100 @@
1
+ Metadata-Version: 2.4
2
+ Name: RR_layer
3
+ Version: 0.1.1
4
+ Summary: A repo for RR layer in Pytorch.
5
+ Author-email: Jad Mounayer <jad.mounayer@outlook.com>
6
+ License: MIT
7
+ License-File: LICENSE
8
+ Requires-Python: >=3.10
9
+ Requires-Dist: matplotlib
10
+ Requires-Dist: numpy
11
+ Requires-Dist: torch
12
+ Requires-Dist: torchvision
13
+ Description-Content-Type: text/markdown
14
+
15
+ # RR_layer
16
+
17
+ `RR_layer` is a JAX / Equinox library implementing a **Rank-Reduction (RR) layer**: a layer that compresses its input by projecting onto a low-rank SVD basis.
18
+
19
+ The mechanics are different between training and evaluation:
20
+
21
+ - **During training**, the SVD basis is **recomputed from scratch for every batch**. For each batch, the layer computes the SVD of the batch, truncates to the top `r` components, and uses that truncated basis to project — so the basis is always data-dependent and up to date with the current batch, and gradients flow through the SVD/projection step. The model also automatically saves the last `N=basis_history_size` bases to be later used in evaluation.
22
+ - **During evaluation**, when layer.eval() is called, the layer will find a common basis to the whole dataset `Uf`, based on the last `N=basis_history_size` that were saved. When the layer is called in evaluation mode, the SVD is replaced by a projection on that basis `Uf`.
23
+ - In both cases, the shape of the input should be (N x ...), where N is the number samples in a batch. Note that the output will have the same shape is the input. This is because the layer performs the SVD, truncates, but then returns the reconstructed latent space. So the compression happens inside eventhough the output still has the same shape.
24
+
25
+ ## What's inside
26
+
27
+ ```
28
+ RR_layer/
29
+ ├── RR_layer/
30
+ │ ├── __init__.py
31
+ │ └── rr_layer.py # the layer implementation(s)
32
+ ├── examples/
33
+ | ├──── autoencoder
34
+ │ | ├── generate_data.ipynb
35
+ │ | ├── train_baseline.ipynb
36
+ │ | ├── train_RRAE.ipynb
37
+ │ | └── compare.ipynb
38
+ └── tests/
39
+ └── tests.py
40
+ ```
41
+
42
+ ### `RR_layer/rr_layer.py`
43
+
44
+ The library code. Contains the `RR_layer` Equinox module: a layer with two modes of operation —
45
+
46
+ - **train mode**: computes the SVD of the current batch's activations, truncates to rank `r`, and projects onto that basis (gradients flow through this).
47
+ - **eval mode**: projects onto a basis that was saved from training, rather than computing a new SVD.
48
+
49
+ ### `Examples/`
50
+
51
+ The example notebooks are meant to be worked through **in order**:
52
+
53
+ 1. **`generate_data.ipynb`** — generates the dataset used in the other notebooks. Run this first; it should leave behind whatever data artifacts the later notebooks load.
54
+
55
+ 2. **`train_baseline.ipynb`** — trains a standard, non-RR autoencoder as a reference point. This gives you a baseline reconstruction quality and runtime to compare the RRAE against.
56
+
57
+ 3. **`train_RRAE.ipynb`** — trains the Rank-Reduction Auto-Encoder, built with one or more `RR_layer` in the latent space. During training this recomputes the SVD basis per batch as described above; at the end of training it should save the basis to be reused at evaluation time.
58
+
59
+ 4. **`compare.ipynb`** — loads the trained baseline and RRAE models (using the saved, frozen SVD basis for the RRAE) and compares them for tasks such as linear interpolation and random generation.
60
+
61
+ Run the notebooks in the order above — each later notebook generally depends on artifacts (data, images) produced by the earlier ones.
62
+
63
+ ### `tests/tests.py`
64
+
65
+ Unit tests for the `RR_layer` module. Useful both to verify correctness after installing, and as additional usage examples of the layer API beyond the notebooks (e.g. shape checks, gradient flow through the SVD step in train mode, etc.)
66
+
67
+ ## Running the tests
68
+
69
+ ```bash
70
+ python -m pytest tests/tests.py
71
+ ```
72
+
73
+ ## Installation
74
+
75
+ ```bash
76
+ pip install RR_layer
77
+ ```
78
+
79
+ ## Quick start
80
+
81
+ ```python
82
+ import jax
83
+ from RR_layer import RRLayer # replace with actual export
84
+
85
+ inp = torch.randn(32, 768)
86
+ rr = RRLayer(rank=8)
87
+
88
+ rr.train()
89
+ y = rr(inp) # will compute the SVD
90
+
91
+ rr.eval()
92
+ y = rr(inp) # will project onto a saved basis
93
+ ```
94
+
95
+ ## Background
96
+
97
+ For more information about Rank Reduction layers, please refer to:
98
+
99
+ - **Paper:** [Rank Reduction Autoencoders](https://arxiv.org/abs/2405.13980)
100
+ - **Related repo:** [JadM133/RRAEs](https://github.com/JadM133/RRAEs)
@@ -0,0 +1,86 @@
1
+ # RR_layer
2
+
3
+ `RR_layer` is a JAX / Equinox library implementing a **Rank-Reduction (RR) layer**: a layer that compresses its input by projecting onto a low-rank SVD basis.
4
+
5
+ The mechanics are different between training and evaluation:
6
+
7
+ - **During training**, the SVD basis is **recomputed from scratch for every batch**. For each batch, the layer computes the SVD of the batch, truncates to the top `r` components, and uses that truncated basis to project — so the basis is always data-dependent and up to date with the current batch, and gradients flow through the SVD/projection step. The model also automatically saves the last `N=basis_history_size` bases to be later used in evaluation.
8
+ - **During evaluation**, when layer.eval() is called, the layer will find a common basis to the whole dataset `Uf`, based on the last `N=basis_history_size` that were saved. When the layer is called in evaluation mode, the SVD is replaced by a projection on that basis `Uf`.
9
+ - In both cases, the shape of the input should be (N x ...), where N is the number samples in a batch. Note that the output will have the same shape is the input. This is because the layer performs the SVD, truncates, but then returns the reconstructed latent space. So the compression happens inside eventhough the output still has the same shape.
10
+
11
+ ## What's inside
12
+
13
+ ```
14
+ RR_layer/
15
+ ├── RR_layer/
16
+ │ ├── __init__.py
17
+ │ └── rr_layer.py # the layer implementation(s)
18
+ ├── examples/
19
+ | ├──── autoencoder
20
+ │ | ├── generate_data.ipynb
21
+ │ | ├── train_baseline.ipynb
22
+ │ | ├── train_RRAE.ipynb
23
+ │ | └── compare.ipynb
24
+ └── tests/
25
+ └── tests.py
26
+ ```
27
+
28
+ ### `RR_layer/rr_layer.py`
29
+
30
+ The library code. Contains the `RR_layer` Equinox module: a layer with two modes of operation —
31
+
32
+ - **train mode**: computes the SVD of the current batch's activations, truncates to rank `r`, and projects onto that basis (gradients flow through this).
33
+ - **eval mode**: projects onto a basis that was saved from training, rather than computing a new SVD.
34
+
35
+ ### `Examples/`
36
+
37
+ The example notebooks are meant to be worked through **in order**:
38
+
39
+ 1. **`generate_data.ipynb`** — generates the dataset used in the other notebooks. Run this first; it should leave behind whatever data artifacts the later notebooks load.
40
+
41
+ 2. **`train_baseline.ipynb`** — trains a standard, non-RR autoencoder as a reference point. This gives you a baseline reconstruction quality and runtime to compare the RRAE against.
42
+
43
+ 3. **`train_RRAE.ipynb`** — trains the Rank-Reduction Auto-Encoder, built with one or more `RR_layer` in the latent space. During training this recomputes the SVD basis per batch as described above; at the end of training it should save the basis to be reused at evaluation time.
44
+
45
+ 4. **`compare.ipynb`** — loads the trained baseline and RRAE models (using the saved, frozen SVD basis for the RRAE) and compares them for tasks such as linear interpolation and random generation.
46
+
47
+ Run the notebooks in the order above — each later notebook generally depends on artifacts (data, images) produced by the earlier ones.
48
+
49
+ ### `tests/tests.py`
50
+
51
+ Unit tests for the `RR_layer` module. Useful both to verify correctness after installing, and as additional usage examples of the layer API beyond the notebooks (e.g. shape checks, gradient flow through the SVD step in train mode, etc.)
52
+
53
+ ## Running the tests
54
+
55
+ ```bash
56
+ python -m pytest tests/tests.py
57
+ ```
58
+
59
+ ## Installation
60
+
61
+ ```bash
62
+ pip install RR_layer
63
+ ```
64
+
65
+ ## Quick start
66
+
67
+ ```python
68
+ import jax
69
+ from RR_layer import RRLayer # replace with actual export
70
+
71
+ inp = torch.randn(32, 768)
72
+ rr = RRLayer(rank=8)
73
+
74
+ rr.train()
75
+ y = rr(inp) # will compute the SVD
76
+
77
+ rr.eval()
78
+ y = rr(inp) # will project onto a saved basis
79
+ ```
80
+
81
+ ## Background
82
+
83
+ For more information about Rank Reduction layers, please refer to:
84
+
85
+ - **Paper:** [Rank Reduction Autoencoders](https://arxiv.org/abs/2405.13980)
86
+ - **Related repo:** [JadM133/RRAEs](https://github.com/JadM133/RRAEs)
@@ -0,0 +1 @@
1
+ from .rr_layer import RRLayer