x-mlps-pytorch 0.0.7__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,14 @@
1
+ name: Build with Rye
2
+ on: push
3
+
4
+ jobs:
5
+ build:
6
+ runs-on: ubuntu-latest
7
+ steps:
8
+ - uses: actions/checkout@v4
9
+ - name: Install Python
10
+ uses: actions/setup-python@v4
11
+ - name: Install the latest version of rye
12
+ uses: eifinger/setup-rye@v2
13
+ - name: Build with Rye
14
+ run: rye build
@@ -0,0 +1,36 @@
1
+ # This workflow will upload a Python Package using Twine when a release is created
2
+ # For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries
3
+
4
+ # This workflow uses actions that are not certified by GitHub.
5
+ # They are provided by a third-party and are governed by
6
+ # separate terms of service, privacy policy, and support
7
+ # documentation.
8
+
9
+ name: Upload Python Package
10
+
11
+ on:
12
+ release:
13
+ types: [published]
14
+
15
+ jobs:
16
+ deploy:
17
+
18
+ runs-on: ubuntu-latest
19
+
20
+ steps:
21
+ - uses: actions/checkout@v2
22
+ - name: Set up Python
23
+ uses: actions/setup-python@v2
24
+ with:
25
+ python-version: '3.x'
26
+ - name: Install dependencies
27
+ run: |
28
+ python -m pip install --upgrade pip
29
+ pip install build
30
+ - name: Build package
31
+ run: python -m build
32
+ - name: Publish package
33
+ uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
34
+ with:
35
+ user: __token__
36
+ password: ${{ secrets.PYPI_API_TOKEN }}
@@ -0,0 +1,19 @@
1
+ name: Tests the examples in README
2
+ on: push
3
+
4
+ jobs:
5
+ test:
6
+ runs-on: ubuntu-latest
7
+ steps:
8
+ - uses: actions/checkout@v4
9
+ - name: Install Python
10
+ uses: actions/setup-python@v4
11
+ - name: Install the latest version of rye
12
+ uses: eifinger/setup-rye@v2
13
+ - name: Use UV instead of pip
14
+ run: rye config --set-bool behavior.use-uv=true
15
+ - name: Install dependencies
16
+ run: |
17
+ pip install .[test]
18
+ - name: Run pytest
19
+ run: pytest tests
@@ -0,0 +1,174 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
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
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # UV
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ #uv.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ #poetry.lock
109
+
110
+ # pdm
111
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
112
+ #pdm.lock
113
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
114
+ # in version control.
115
+ # https://pdm.fming.dev/latest/usage/project/#working-with-version-control
116
+ .pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
121
+ __pypackages__/
122
+
123
+ # Celery stuff
124
+ celerybeat-schedule
125
+ celerybeat.pid
126
+
127
+ # SageMath parsed files
128
+ *.sage.py
129
+
130
+ # Environments
131
+ .env
132
+ .venv
133
+ env/
134
+ venv/
135
+ ENV/
136
+ env.bak/
137
+ venv.bak/
138
+
139
+ # Spyder project settings
140
+ .spyderproject
141
+ .spyproject
142
+
143
+ # Rope project settings
144
+ .ropeproject
145
+
146
+ # mkdocs documentation
147
+ /site
148
+
149
+ # mypy
150
+ .mypy_cache/
151
+ .dmypy.json
152
+ dmypy.json
153
+
154
+ # Pyre type checker
155
+ .pyre/
156
+
157
+ # pytype static type analyzer
158
+ .pytype/
159
+
160
+ # Cython debug symbols
161
+ cython_debug/
162
+
163
+ # PyCharm
164
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
165
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
166
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
167
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
168
+ #.idea/
169
+
170
+ # Ruff stuff:
171
+ .ruff_cache/
172
+
173
+ # PyPI configuration file
174
+ .pypirc
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 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,71 @@
1
+ Metadata-Version: 2.4
2
+ Name: x-mlps-pytorch
3
+ Version: 0.0.7
4
+ Summary: A collection of MLPs / Feedforwards
5
+ Project-URL: Homepage, https://pypi.org/project/x-mlps/
6
+ Project-URL: Repository, https://github.com/lucidrains/x-mlps
7
+ Author-email: Phil Wang <lucidrains@gmail.com>
8
+ License: MIT License
9
+
10
+ Copyright (c) 2025 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,feedforwards,mlps
31
+ Classifier: Development Status :: 4 - Beta
32
+ Classifier: Intended Audience :: Developers
33
+ Classifier: License :: OSI Approved :: MIT License
34
+ Classifier: Programming Language :: Python :: 3.8
35
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
36
+ Requires-Python: >=3.8
37
+ Requires-Dist: einops>=0.8.0
38
+ Requires-Dist: torch>=2.4
39
+ Provides-Extra: test
40
+ Requires-Dist: pytest; extra == 'test'
41
+ Description-Content-Type: text/markdown
42
+
43
+ ## x-mlps
44
+
45
+ Just a repository that will house MLPs, from garden variety to the exotic, so as to avoid having to reimplement them again and again for different projects (especially RL)
46
+
47
+ Will also be the repository I use for testing out [Jules](https://jules.google.com/) and other AI assisted tools.
48
+
49
+
50
+ ## Install
51
+
52
+ ```bash
53
+ $ pip install x-mlps-pytorch
54
+ ```
55
+
56
+ ## Usage
57
+
58
+ ```python
59
+ import torch
60
+ from x_mlps import MLP
61
+
62
+ actor = MLP(10, 16, 5)
63
+
64
+ critic = MLP(10, 32, 16, 1)
65
+
66
+ state = torch.randn(10)
67
+
68
+ action_logits = actor(state) # (5,)
69
+
70
+ values = critic(state) # (1,)
71
+ ```
@@ -0,0 +1,29 @@
1
+ ## x-mlps
2
+
3
+ Just a repository that will house MLPs, from garden variety to the exotic, so as to avoid having to reimplement them again and again for different projects (especially RL)
4
+
5
+ Will also be the repository I use for testing out [Jules](https://jules.google.com/) and other AI assisted tools.
6
+
7
+
8
+ ## Install
9
+
10
+ ```bash
11
+ $ pip install x-mlps-pytorch
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ ```python
17
+ import torch
18
+ from x_mlps import MLP
19
+
20
+ actor = MLP(10, 16, 5)
21
+
22
+ critic = MLP(10, 32, 16, 1)
23
+
24
+ state = torch.randn(10)
25
+
26
+ action_logits = actor(state) # (5,)
27
+
28
+ values = critic(state) # (1,)
29
+ ```
@@ -0,0 +1,52 @@
1
+ [project]
2
+ name = "x-mlps-pytorch"
3
+ version = "0.0.7"
4
+ description = "A collection of MLPs / Feedforwards"
5
+ authors = [
6
+ { name = "Phil Wang", email = "lucidrains@gmail.com" }
7
+ ]
8
+ readme = "README.md"
9
+ requires-python = ">= 3.8"
10
+ license = { file = "LICENSE" }
11
+ keywords = [
12
+ 'artificial intelligence',
13
+ 'deep learning',
14
+ 'mlps',
15
+ 'feedforwards'
16
+ ]
17
+ classifiers=[
18
+ 'Development Status :: 4 - Beta',
19
+ 'Intended Audience :: Developers',
20
+ 'Topic :: Scientific/Engineering :: Artificial Intelligence',
21
+ 'License :: OSI Approved :: MIT License',
22
+ 'Programming Language :: Python :: 3.8',
23
+ ]
24
+
25
+ dependencies = [
26
+ 'einops>=0.8.0',
27
+ 'torch>=2.4',
28
+ ]
29
+
30
+ [project.urls]
31
+ Homepage = "https://pypi.org/project/x-mlps/"
32
+ Repository = "https://github.com/lucidrains/x-mlps"
33
+
34
+ [build-system]
35
+ requires = ["hatchling"]
36
+ build-backend = "hatchling.build"
37
+
38
+ [project.optional-dependencies]
39
+ test = [
40
+ "pytest",
41
+ ]
42
+
43
+ [tool.pytest.ini_options]
44
+ pythonpath = [
45
+ "."
46
+ ]
47
+
48
+ [tool.hatch.metadata]
49
+ allow-direct-references = true
50
+
51
+ [tool.hatch.build.targets.wheel]
52
+ packages = ["x_mlps"]
@@ -0,0 +1,11 @@
1
+ import pytest
2
+ import torch
3
+
4
+ def test_ff():
5
+ from x_mlps_pytorch.ff import Feedforwards
6
+
7
+ mlp = Feedforwards(256, 4)
8
+
9
+ x = torch.randn(7, 3, 256)
10
+
11
+ assert mlp(x).shape == x.shape
@@ -0,0 +1,29 @@
1
+ import pytest
2
+ import torch
3
+
4
+ def test_mlp():
5
+ from x_mlps_pytorch.mlp import MLP
6
+
7
+ mlp = MLP(256, 128, 64)
8
+
9
+ x = torch.randn(7, 3, 256)
10
+
11
+ assert mlp(x).shape == (7, 3, 64)
12
+
13
+ # with depth
14
+
15
+ def test_create_mlp():
16
+ from x_mlps_pytorch.mlp import create_mlp
17
+
18
+ mlp = create_mlp(
19
+ dim = 128,
20
+ dim_in = 256,
21
+ dim_out = 64,
22
+ depth = 4
23
+ )
24
+
25
+ # same as MLP(256, 128, 128, 128, 128, 64)
26
+
27
+ x = torch.randn(7, 3, 256)
28
+
29
+ assert mlp(x).shape == (7, 3, 64)
@@ -0,0 +1,7 @@
1
+ from x_mlps_pytorch.mlp import (
2
+ MLP
3
+ )
4
+
5
+ from x_mlps_pytorch.ff import (
6
+ Feedforwards
7
+ )
@@ -0,0 +1,47 @@
1
+ import torch
2
+ from torch import nn
3
+ from torch.nn import Module, ModuleList
4
+
5
+ # main class
6
+
7
+ class Feedforwards(Module):
8
+
9
+ def __init__(
10
+ self,
11
+ dim,
12
+ depth,
13
+ *,
14
+ activation = nn.GELU(),
15
+ bias = True,
16
+ expansion_factor = 4.,
17
+ ):
18
+ super().__init__()
19
+
20
+ layers = []
21
+
22
+ dim_hidden = int(dim * expansion_factor)
23
+
24
+ # layers
25
+
26
+ for _ in range(depth):
27
+
28
+ layer = nn.Sequential(
29
+ nn.RMSNorm(dim),
30
+ nn.Linear(dim, dim_hidden, bias = bias),
31
+ activation,
32
+ nn.Linear(dim_hidden, dim, bias = bias)
33
+ )
34
+
35
+ layers.append(layer)
36
+
37
+ self.layers = ModuleList(layers)
38
+
39
+ def forward(
40
+ self,
41
+ x
42
+ ):
43
+
44
+ for layer in self.layers:
45
+ x = layer(x) + x
46
+
47
+ return x
@@ -0,0 +1,73 @@
1
+ import torch
2
+ from torch import nn
3
+ from torch.nn import Module, ModuleList
4
+
5
+ # functions
6
+
7
+ def exists(v):
8
+ return v is not None
9
+
10
+ # main class
11
+
12
+ class MLP(Module):
13
+ def __init__(
14
+ self,
15
+ *dims,
16
+ activation = nn.ReLU(),
17
+ bias = True
18
+ ):
19
+ super().__init__()
20
+ assert len(dims) > 1, f'must have more than 1 layer'
21
+
22
+ layers = []
23
+
24
+ # input output dimension pairs
25
+
26
+ dim_in_out = tuple(zip(dims[:-1], dims[1:]))
27
+
28
+ # layers
29
+
30
+ for i, (dim_in, dim_out) in enumerate(dim_in_out, start = 1):
31
+ is_last = i == len(dim_in_out)
32
+
33
+ layer = nn.Linear(dim_in, dim_out, bias = bias)
34
+
35
+ # if not last, add an activation after each linear layer
36
+
37
+ if not is_last:
38
+ layer = nn.Sequential(layer, activation)
39
+
40
+ layers.append(layer)
41
+
42
+ self.layers = ModuleList(layers)
43
+
44
+ def forward(
45
+ self,
46
+ x
47
+ ):
48
+
49
+ for layer in self.layers:
50
+ x = layer(x)
51
+
52
+ return x
53
+
54
+ # factory function
55
+
56
+ def create_mlp(
57
+ dim,
58
+ depth,
59
+ *,
60
+ dim_in = None,
61
+ dim_out = None,
62
+ **mlp_kwargs
63
+ ):
64
+ dims = (dim,) * depth
65
+
66
+ if exists(dim_in):
67
+ dims = (dim_in, *dims)
68
+
69
+ if exists(dim_out):
70
+ dims = (*dims, dim_out)
71
+
72
+ return MLP(*dims, **mlp_kwargs)
73
+