metacontroller-pytorch 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.
@@ -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,21 @@
1
+ name: Pytest
2
+ on: [push, pull_request]
3
+
4
+ jobs:
5
+ build:
6
+
7
+ runs-on: ubuntu-latest
8
+
9
+ steps:
10
+ - uses: actions/checkout@v4
11
+ - name: Set up Python 3.10
12
+ uses: actions/setup-python@v5
13
+ with:
14
+ python-version: "3.10"
15
+ - name: Install dependencies
16
+ run: |
17
+ python -m pip install --upgrade pip
18
+ python -m pip install -e .[test]
19
+ - name: Test with pytest
20
+ run: |
21
+ python -m pytest tests/
@@ -0,0 +1,207 @@
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
+ 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
+ #poetry.toml
110
+
111
+ # pdm
112
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
113
+ # pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
114
+ # https://pdm-project.org/en/latest/usage/project/#working-with-version-control
115
+ #pdm.lock
116
+ #pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # pixi
121
+ # Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
122
+ #pixi.lock
123
+ # Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
124
+ # in the .venv directory. It is recommended not to include this directory in version control.
125
+ .pixi
126
+
127
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
128
+ __pypackages__/
129
+
130
+ # Celery stuff
131
+ celerybeat-schedule
132
+ celerybeat.pid
133
+
134
+ # SageMath parsed files
135
+ *.sage.py
136
+
137
+ # Environments
138
+ .env
139
+ .envrc
140
+ .venv
141
+ env/
142
+ venv/
143
+ ENV/
144
+ env.bak/
145
+ venv.bak/
146
+
147
+ # Spyder project settings
148
+ .spyderproject
149
+ .spyproject
150
+
151
+ # Rope project settings
152
+ .ropeproject
153
+
154
+ # mkdocs documentation
155
+ /site
156
+
157
+ # mypy
158
+ .mypy_cache/
159
+ .dmypy.json
160
+ dmypy.json
161
+
162
+ # Pyre type checker
163
+ .pyre/
164
+
165
+ # pytype static type analyzer
166
+ .pytype/
167
+
168
+ # Cython debug symbols
169
+ cython_debug/
170
+
171
+ # PyCharm
172
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
173
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
174
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
175
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
176
+ #.idea/
177
+
178
+ # Abstra
179
+ # Abstra is an AI-powered process automation framework.
180
+ # Ignore directories containing user credentials, local state, and settings.
181
+ # Learn more at https://abstra.io/docs
182
+ .abstra/
183
+
184
+ # Visual Studio Code
185
+ # Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
186
+ # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
187
+ # and can be added to the global gitignore or merged into this file. However, if you prefer,
188
+ # you could uncomment the following to ignore the entire vscode folder
189
+ # .vscode/
190
+
191
+ # Ruff stuff:
192
+ .ruff_cache/
193
+
194
+ # PyPI configuration file
195
+ .pypirc
196
+
197
+ # Cursor
198
+ # Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
199
+ # exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
200
+ # refer to https://docs.cursor.com/context/ignore-files
201
+ .cursorignore
202
+ .cursorindexingignore
203
+
204
+ # Marimo
205
+ marimo/_static/
206
+ marimo/_lsp/
207
+ __marimo__/
@@ -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,79 @@
1
+ Metadata-Version: 2.4
2
+ Name: metacontroller-pytorch
3
+ Version: 0.0.1
4
+ Summary: Transformer Metacontroller
5
+ Project-URL: Homepage, https://pypi.org/project/metacontroller/
6
+ Project-URL: Repository, https://github.com/lucidrains/metacontroller
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,hierarchical reinforcement learning,latent steering
31
+ Classifier: Development Status :: 4 - Beta
32
+ Classifier: Intended Audience :: Developers
33
+ Classifier: License :: OSI Approved :: MIT License
34
+ Classifier: Programming Language :: Python :: 3.9
35
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
36
+ Requires-Python: >=3.9
37
+ Requires-Dist: assoc-scan
38
+ Requires-Dist: discrete-continuous-embed-readout>=0.1.11
39
+ Requires-Dist: einops>=0.8.1
40
+ Requires-Dist: einx>=0.3.0
41
+ Requires-Dist: torch>=2.5
42
+ Requires-Dist: x-evolution>=0.1.23
43
+ Requires-Dist: x-mlps-pytorch
44
+ Requires-Dist: x-transformers
45
+ Provides-Extra: examples
46
+ Provides-Extra: test
47
+ Requires-Dist: pytest; extra == 'test'
48
+ Description-Content-Type: text/markdown
49
+
50
+ <img src="./fig1.png" width="400px"></img>
51
+
52
+ ## metacontroller (wip)
53
+
54
+ Implementation of the MetaController proposed in [Emergent temporal abstractions in autoregressive models enable hierarchical reinforcement learning](https://arxiv.org/abs/2512.20605)
55
+
56
+ ## Citations
57
+
58
+ ```bibtex
59
+ @misc{kobayashi2025emergenttemporalabstractionsautoregressive,
60
+ title = {Emergent temporal abstractions in autoregressive models enable hierarchical reinforcement learning},
61
+ author = {Seijin Kobayashi and Yanick Schimpf and Maximilian Schlegel and Angelika Steger and Maciej Wolczyk and Johannes von Oswald and Nino Scherrer and Kaitlin Maile and Guillaume Lajoie and Blake A. Richards and Rif A. Saurous and James Manyika and Blaise Agüera y Arcas and Alexander Meulemans and João Sacramento},
62
+ year={2025},
63
+ eprint = {2512.20605},
64
+ archivePrefix = {arXiv},
65
+ primaryClass = {cs.LG},
66
+ url = {https://arxiv.org/abs/2512.20605},
67
+ }
68
+ ```
69
+
70
+ ```bibtex
71
+ @article{Wagenmaker2025SteeringYD,
72
+ title = {Steering Your Diffusion Policy with Latent Space Reinforcement Learning},
73
+ author = {Andrew Wagenmaker and Mitsuhiko Nakamoto and Yunchu Zhang and Seohong Park and Waleed Yagoub and Anusha Nagabandi and Abhishek Gupta and Sergey Levine},
74
+ journal = {ArXiv},
75
+ year = {2025},
76
+ volume = {abs/2506.15799},
77
+ url = {https://api.semanticscholar.org/CorpusID:279464702}
78
+ }
79
+ ```
@@ -0,0 +1,30 @@
1
+ <img src="./fig1.png" width="400px"></img>
2
+
3
+ ## metacontroller (wip)
4
+
5
+ Implementation of the MetaController proposed in [Emergent temporal abstractions in autoregressive models enable hierarchical reinforcement learning](https://arxiv.org/abs/2512.20605)
6
+
7
+ ## Citations
8
+
9
+ ```bibtex
10
+ @misc{kobayashi2025emergenttemporalabstractionsautoregressive,
11
+ title = {Emergent temporal abstractions in autoregressive models enable hierarchical reinforcement learning},
12
+ author = {Seijin Kobayashi and Yanick Schimpf and Maximilian Schlegel and Angelika Steger and Maciej Wolczyk and Johannes von Oswald and Nino Scherrer and Kaitlin Maile and Guillaume Lajoie and Blake A. Richards and Rif A. Saurous and James Manyika and Blaise Agüera y Arcas and Alexander Meulemans and João Sacramento},
13
+ year={2025},
14
+ eprint = {2512.20605},
15
+ archivePrefix = {arXiv},
16
+ primaryClass = {cs.LG},
17
+ url = {https://arxiv.org/abs/2512.20605},
18
+ }
19
+ ```
20
+
21
+ ```bibtex
22
+ @article{Wagenmaker2025SteeringYD,
23
+ title = {Steering Your Diffusion Policy with Latent Space Reinforcement Learning},
24
+ author = {Andrew Wagenmaker and Mitsuhiko Nakamoto and Yunchu Zhang and Seohong Park and Waleed Yagoub and Anusha Nagabandi and Abhishek Gupta and Sergey Levine},
25
+ journal = {ArXiv},
26
+ year = {2025},
27
+ volume = {abs/2506.15799},
28
+ url = {https://api.semanticscholar.org/CorpusID:279464702}
29
+ }
30
+ ```
Binary file
@@ -0,0 +1 @@
1
+ from metacontroller.metacontroller import MetaController
@@ -0,0 +1,257 @@
1
+ from __future__ import annotations
2
+ from functools import partial
3
+
4
+ import torch
5
+ from torch import nn, cat, stack, tensor
6
+ from torch.nn import Module, GRU, Linear, Identity
7
+ import torch.nn.functional as F
8
+
9
+ # einops
10
+
11
+ import einx
12
+ from einops import einsum, rearrange, repeat, reduce
13
+ from einops.layers.torch import Rearrange
14
+
15
+ # external modules
16
+
17
+ from x_transformers import Decoder
18
+ from x_mlps_pytorch import Feedforwards
19
+ from x_evolution import EvoStrategy
20
+
21
+ from discrete_continuous_embed_readout import Embed, Readout
22
+
23
+ from assoc_scan import AssocScan
24
+
25
+ # constants
26
+
27
+ LinearNoBias = partial(Linear, bias = False)
28
+
29
+ GRU = partial(GRU, batch_first = True)
30
+
31
+ # helper functions
32
+
33
+ def exists(v):
34
+ return v is not None
35
+
36
+ def identity(t):
37
+ return t
38
+
39
+ def default(*args):
40
+ for arg in args:
41
+ if exists(arg):
42
+ return arg
43
+ return None
44
+
45
+ # meta controller
46
+
47
+ class MetaController(Module):
48
+ def __init__(
49
+ self,
50
+ dim_latent,
51
+ *,
52
+ decoder_expansion_factor = 2.,
53
+ decoder_depth = 1,
54
+ hypernetwork_low_rank = 16,
55
+ assoc_scan_kwargs: dict = dict()
56
+ ):
57
+ super().__init__()
58
+
59
+ # there are two phases, the first (discovery ssl phase) uses acausal with some ssm i don't really believe in - let's just use a bidirectional GRU as placeholders
60
+
61
+ self.bidirectional_temporal_compressor = GRU(dim_latent, dim_latent, bidirectional = True) # revisit naming
62
+
63
+ self.emitter = GRU(dim_latent * 2, dim_latent * 2)
64
+ self.emitter_to_action_mean_log_var = LinearNoBias(dim_latent * 2, dim_latent * 2)
65
+
66
+ # internal rl phase substitutes the acausal + emitter with a causal ssm
67
+
68
+ self.action_proposer = GRU(dim_latent, dim_latent)
69
+ self.action_proposer_mean_log_var = LinearNoBias(dim_latent, dim_latent * 2)
70
+
71
+ # switching unit
72
+
73
+ self.switching_unit = GRU(dim_latent, dim_latent)
74
+ self.to_switching_unit_beta = nn.Linear(dim_latent, 1, bias = False)
75
+
76
+ self.switch_gating = AssocScan(**assoc_scan_kwargs)
77
+
78
+ # decoder
79
+
80
+ assert hypernetwork_low_rank < dim_latent
81
+
82
+ dim_decoder_hidden = int(dim_latent * decoder_expansion_factor)
83
+
84
+ self.decoder = Feedforwards(
85
+ dim_in = dim_latent,
86
+ dim = dim_decoder_hidden,
87
+ depth = decoder_depth,
88
+ dim_out = 2 * hypernetwork_low_rank * dim_latent
89
+ )
90
+
91
+ self.to_hyper_network_weights = Rearrange('... (two d r) -> two ... d r', two = 2, r = hypernetwork_low_rank)
92
+
93
+ self.register_buffer('zero', tensor(0.), persistent = False)
94
+
95
+ def discovery_parameters(self):
96
+ return [
97
+ *self.bidirectional_temporal_compressor.parameters(),
98
+ *self.emitter.parameters(),
99
+ *self.emitter_to_action_mean_log_var.parameters()
100
+ *self.decoder.parameters(),
101
+ *self.switch_gating
102
+ ]
103
+
104
+ def internal_rl_parameters(self):
105
+ return [
106
+ *self.action_proposer.parameters(),
107
+ *self.action_proposer_mean_log_var.parameters(),
108
+ *self.decoder.parameters(),
109
+ *self.switch_gating
110
+ ]
111
+
112
+ def forward(
113
+ self,
114
+ residual_stream,
115
+ discovery_phase = False
116
+ ):
117
+
118
+ if discovery_phase:
119
+ temporal_compressed, _ = self.bidirectional_temporal_compressor(residual_stream)
120
+ temporal_compressed = reduce(temporal_compressed, '... (two d) -> ... d', 'mean', two = 2)
121
+
122
+ proposed_action_hidden, _ = self.emitter(cat((temporal_compressed, residual_stream), dim = -1))
123
+ proposed_action = self.emitter_to_action_mean_log_var(proposed_action_hidden)
124
+
125
+ else: # else internal rl phase
126
+ proposed_action_hidden, _ = self.action_proposer(residual_stream)
127
+ proposed_action = self.action_proposer_mean_log_var(proposed_action_hidden)
128
+
129
+ # sample from the gaussian as the action from the meta controller
130
+
131
+ mean, log_var = proposed_action.chunk(2, dim = -1)
132
+
133
+ std = (0.5 * log_var).exp()
134
+ sampled_action_intents = mean + torch.randn_like(mean) * std
135
+
136
+ # need to encourage normal distribution
137
+
138
+ vae_kl_loss = self.zero
139
+
140
+ if discovery_phase:
141
+ vae_kl_loss = (0.5 * (
142
+ log_var.exp()
143
+ + mean.square()
144
+ - log_var
145
+ - 1.
146
+ )).sum(dim = -1).mean()
147
+
148
+ # switching unit timer
149
+
150
+ batch, _, dim = sampled_action_intents.shape
151
+
152
+ switching_unit_gru_out, switching_unit_gru_hidden = self.switching_unit(residual_stream)
153
+
154
+ switch_beta = self.to_switching_unit_beta(switching_unit_gru_out).sigmoid()
155
+
156
+ action_intent_for_gating = rearrange(sampled_action_intents, 'b n d -> (b d) n')
157
+ switch_beta = repeat(switch_beta, 'b n 1 -> (b d) n', d = dim)
158
+
159
+ forget = 1. - switch_beta
160
+ gated_action_intent = self.switch_gating(action_intent_for_gating * forget, switch_beta)
161
+
162
+ gated_action_intent = rearrange(gated_action_intent, '(b d) n -> b n d', b = batch)
163
+
164
+ # decoder
165
+
166
+ decoder_out = self.decoder(gated_action_intent)
167
+
168
+ w1, w2 = self.to_hyper_network_weights(decoder_out)
169
+ hypernetwork_weight = einsum(w1, w2, '... i r, ... j r -> ... i j')
170
+
171
+ # generating the residual stream controlling signal
172
+
173
+ control_signal = einsum(gated_action_intent, hypernetwork_weight, '... d1, ... d1 d2 -> ... d1')
174
+
175
+ modified_residual_stream = residual_stream + control_signal
176
+
177
+ return modified_residual_stream, vae_kl_loss
178
+
179
+ # main transformer, which is subsumed into the environment after behavioral cloning
180
+
181
+ class Transformer(Module):
182
+ def __init__(
183
+ self,
184
+ dim,
185
+ *,
186
+ embed: Embed | dict,
187
+ lower_body: Decoder | dict,
188
+ upper_body: Decoder | dict,
189
+ readout: Readout | dict,
190
+ meta_controller: MetaController | None = None
191
+ ):
192
+ super().__init__()
193
+
194
+ if isinstance(embed, dict):
195
+ embed = Embed(dim = dim, **embed)
196
+
197
+ if isinstance(lower_body, dict):
198
+ lower_body = Decoder(dim = dim, **lower_body)
199
+
200
+ if isinstance(upper_body, dict):
201
+ upper_body = Decoder(dim = dim, **upper_body)
202
+
203
+ if isinstance(readout, dict):
204
+ readout = Readout(dim = dim, **readout)
205
+
206
+ self.embed = embed
207
+ self.lower_body = lower_body
208
+ self.upper_body = upper_body
209
+ self.readout = readout
210
+
211
+ # meta controller
212
+
213
+ self.meta_controller = meta_controller
214
+
215
+ def evolve(
216
+ self,
217
+ environment,
218
+ **kwargs
219
+ ):
220
+ assert exists(self.meta_controller), '`meta_controller` must be defined on init for evolutionary strategies to be straightforwardly applied'
221
+
222
+ evo_strat = EvoStrategy(
223
+ self,
224
+ environment = environment,
225
+ params_to_optimize = self.meta_controller.internal_rl_parameters(),
226
+ **kwargs
227
+ )
228
+
229
+ evo_strat()
230
+
231
+ def forward(
232
+ self,
233
+ ids,
234
+ meta_controller: Module | None = None,
235
+ discovery_phase = False,
236
+ return_latents = False
237
+ ):
238
+ meta_controller = default(meta_controller, self.meta_controller, Identity())
239
+
240
+ embed = self.embed(ids)
241
+
242
+ residual_stream = self.lower_body(embed)
243
+
244
+ # meta controller acts on residual stream here
245
+
246
+ modified_residual_stream, vae_aux_loss = meta_controller(residual_stream, discovery_phase = discovery_phase)
247
+
248
+ # modified residual stream sent back
249
+
250
+ attended = self.upper_body(modified_residual_stream)
251
+
252
+ dist_params = self.readout(attended)
253
+
254
+ if not return_latents:
255
+ return dist_params
256
+
257
+ return dist_params, latents
@@ -0,0 +1,64 @@
1
+ [project]
2
+ name = "metacontroller-pytorch"
3
+ version = "0.0.1"
4
+ description = "Transformer Metacontroller"
5
+ authors = [
6
+ { name = "Phil Wang", email = "lucidrains@gmail.com" }
7
+ ]
8
+ readme = "README.md"
9
+ requires-python = ">= 3.9"
10
+ license = { file = "LICENSE" }
11
+ keywords = [
12
+ 'artificial intelligence',
13
+ 'deep learning',
14
+ 'latent steering',
15
+ 'hierarchical reinforcement learning'
16
+ ]
17
+
18
+ classifiers=[
19
+ 'Development Status :: 4 - Beta',
20
+ 'Intended Audience :: Developers',
21
+ 'Topic :: Scientific/Engineering :: Artificial Intelligence',
22
+ 'License :: OSI Approved :: MIT License',
23
+ 'Programming Language :: Python :: 3.9',
24
+ ]
25
+
26
+ dependencies = [
27
+ "assoc-scan",
28
+ "einx>=0.3.0",
29
+ "einops>=0.8.1",
30
+ "discrete-continuous-embed-readout>=0.1.11",
31
+ "torch>=2.5",
32
+ "x-evolution>=0.1.23",
33
+ "x-mlps-pytorch",
34
+ "x-transformers"
35
+ ]
36
+
37
+ [project.urls]
38
+ Homepage = "https://pypi.org/project/metacontroller/"
39
+ Repository = "https://github.com/lucidrains/metacontroller"
40
+
41
+ [project.optional-dependencies]
42
+ examples = []
43
+ test = [
44
+ "pytest"
45
+ ]
46
+
47
+ [tool.pytest.ini_options]
48
+ pythonpath = [
49
+ "."
50
+ ]
51
+
52
+ [build-system]
53
+ requires = ["hatchling"]
54
+ build-backend = "hatchling.build"
55
+
56
+ [tool.rye]
57
+ managed = true
58
+ dev-dependencies = []
59
+
60
+ [tool.hatch.metadata]
61
+ allow-direct-references = true
62
+
63
+ [tool.hatch.build.targets.wheel]
64
+ packages = ["metacontroller"]
@@ -0,0 +1,26 @@
1
+ import pytest
2
+ param = pytest.mark.parametrize
3
+
4
+ import torch
5
+ from metacontroller.metacontroller import Transformer, MetaController
6
+
7
+ @param('discovery_phase', (False, True))
8
+ def test_metacontroller(
9
+ discovery_phase
10
+ ):
11
+
12
+ ids = torch.randint(0, 256, (1, 1024))
13
+
14
+ model = Transformer(
15
+ 512,
16
+ embed = dict(num_discrete = 256),
17
+ lower_body = dict(depth = 2,),
18
+ upper_body = dict(depth = 2,),
19
+ readout = dict(num_discrete = 256)
20
+ )
21
+
22
+ meta_controller = MetaController(512)
23
+
24
+ logits = model(ids, meta_controller = meta_controller, discovery_phase = discovery_phase)
25
+
26
+ assert logits.shape == (1, 1024, 256)