transfusion-pytorch 0.9.5__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,24 @@
1
+ name: Tests the examples in README
2
+ on: [push, pull_request]
3
+
4
+ env:
5
+ TYPECHECK: True
6
+
7
+ jobs:
8
+ test:
9
+ runs-on: ubuntu-latest
10
+ steps:
11
+ - uses: actions/checkout@v4
12
+ - name: Install Python
13
+ uses: actions/setup-python@v5
14
+ with:
15
+ python-version: "3.11"
16
+ - name: Install dependencies
17
+ run: |
18
+ python -m pip install uv
19
+ python -m uv pip install --upgrade pip
20
+ python -m uv pip install torch --index-url https://download.pytorch.org/whl/nightly/cpu
21
+ python -m uv pip install -e .[test]
22
+ - name: Test with pytest
23
+ run: |
24
+ python -m pytest tests/
@@ -0,0 +1,165 @@
1
+ data/mnist/
2
+ results/
3
+
4
+ # Byte-compiled / optimized / DLL files
5
+ __pycache__/
6
+ *.py[cod]
7
+ *$py.class
8
+
9
+ # C extensions
10
+ *.so
11
+
12
+ # Distribution / packaging
13
+ .Python
14
+ build/
15
+ develop-eggs/
16
+ dist/
17
+ downloads/
18
+ eggs/
19
+ .eggs/
20
+ lib/
21
+ lib64/
22
+ parts/
23
+ sdist/
24
+ var/
25
+ wheels/
26
+ share/python-wheels/
27
+ *.egg-info/
28
+ .installed.cfg
29
+ *.egg
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
+ # poetry
101
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
102
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
103
+ # commonly ignored for libraries.
104
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
105
+ #poetry.lock
106
+
107
+ # pdm
108
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
109
+ #pdm.lock
110
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
111
+ # in version control.
112
+ # https://pdm.fming.dev/latest/usage/project/#working-with-version-control
113
+ .pdm.toml
114
+ .pdm-python
115
+ .pdm-build/
116
+
117
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
118
+ __pypackages__/
119
+
120
+ # Celery stuff
121
+ celerybeat-schedule
122
+ celerybeat.pid
123
+
124
+ # SageMath parsed files
125
+ *.sage.py
126
+
127
+ # Environments
128
+ .env
129
+ .venv
130
+ env/
131
+ venv/
132
+ ENV/
133
+ env.bak/
134
+ venv.bak/
135
+
136
+ # Spyder project settings
137
+ .spyderproject
138
+ .spyproject
139
+
140
+ # Rope project settings
141
+ .ropeproject
142
+
143
+ # mkdocs documentation
144
+ /site
145
+
146
+ # mypy
147
+ .mypy_cache/
148
+ .dmypy.json
149
+ dmypy.json
150
+
151
+ # Pyre type checker
152
+ .pyre/
153
+
154
+ # pytype static type analyzer
155
+ .pytype/
156
+
157
+ # Cython debug symbols
158
+ cython_debug/
159
+
160
+ # PyCharm
161
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
162
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
163
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
164
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
165
+ #.idea/
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 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,308 @@
1
+ Metadata-Version: 2.4
2
+ Name: transfusion-pytorch
3
+ Version: 0.9.5
4
+ Summary: Transfusion in Pytorch
5
+ Project-URL: Homepage, https://pypi.org/project/transfusion-pytorch/
6
+ Project-URL: Repository, https://github.com/lucidrains/transfusion-pytorch
7
+ Author-email: Phil Wang <lucidrains@gmail.com>
8
+ License: MIT License
9
+
10
+ Copyright (c) 2024 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,attention mechanism,deep learning,rectified flow,transformers
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: axial-positional-embedding>=0.3.5
38
+ Requires-Dist: beartype
39
+ Requires-Dist: einops>=0.8.0
40
+ Requires-Dist: einx>=0.3.0
41
+ Requires-Dist: ema-pytorch
42
+ Requires-Dist: hyper-connections>=0.0.10
43
+ Requires-Dist: jaxtyping
44
+ Requires-Dist: loguru
45
+ Requires-Dist: rotary-embedding-torch>=0.8.4
46
+ Requires-Dist: torch>=2.0
47
+ Requires-Dist: torchdiffeq
48
+ Requires-Dist: tqdm
49
+ Provides-Extra: examples
50
+ Requires-Dist: datasets; extra == 'examples'
51
+ Requires-Dist: diffusers; extra == 'examples'
52
+ Provides-Extra: test
53
+ Requires-Dist: pytest; extra == 'test'
54
+ Description-Content-Type: text/markdown
55
+
56
+ <img src="./transfusion.png" width="400px"></img>
57
+
58
+ ## Transfusion - Pytorch
59
+
60
+ Pytorch implementation of [Transfusion](https://www.arxiv.org/abs/2408.11039), "Predict the Next Token and Diffuse Images with One Multi-Modal Model", from MetaAI.
61
+
62
+ In this repo, we will substitute diffusion with flow matching given the success of Flux from Black Forest Labs (but will keep the original paper title given Transflow does not have the same ring). This repository will also attempt to extend to any number of modalities.
63
+
64
+ ## Install
65
+
66
+ ```bash
67
+ $ pip install transfusion-pytorch
68
+ ```
69
+
70
+ ## Usage
71
+
72
+ One modality, say images
73
+
74
+ ```python
75
+ from torch import randint, randn
76
+ from transfusion_pytorch import Transfusion
77
+
78
+ model = Transfusion(
79
+ num_text_tokens = 256,
80
+ dim_latent = 384,
81
+ modality_default_shape = (4,), # fallback, in the case the language model did not produce a valid modality shape
82
+ transformer = dict(
83
+ dim = 512,
84
+ depth = 8
85
+ )
86
+ )
87
+
88
+ # any torch.long is text, torch.float is modalities
89
+
90
+ text_and_images = [
91
+ [randint(0, 256, (16,)), randn(4, 384), randint(0, 256, (8,)), randn(6, 384)],
92
+ [randint(0, 256, (16,)), randn(7, 384), randint(0, 256, (5,)), randn(2, 384), randint(0, 256, (9,))]
93
+ ]
94
+
95
+ loss = model(text_and_images)
96
+
97
+ loss.backward()
98
+
99
+ # after much training
100
+
101
+ one_multimodal_sample = model.sample()
102
+ ```
103
+
104
+ Multiple different modalities
105
+
106
+ ```python
107
+ from torch import randint, randn
108
+ from transfusion_pytorch import Transfusion
109
+
110
+ model = Transfusion(
111
+ num_text_tokens = 256,
112
+ dim_latent = (384, 192), # specify multiple latent dimensions
113
+ modality_default_shape = ((4,), (2,)), # default shapes for first and second modality
114
+ transformer = dict(
115
+ dim = 512,
116
+ depth = 8
117
+ )
118
+ )
119
+
120
+ # then for the Tensors of type float, you can pass a tuple[int, Tensor] and specify the modality index in the first position
121
+
122
+ # any torch.long is text, torch.float is modalities
123
+
124
+ text_images_and_audio = [
125
+ [randint(0, 256, (16,)), (0, randn(4, 384)), randint(0, 256, (8,)), (1, randn(6, 192))],
126
+ [randint(0, 256, (16,)), randn(7, 384), randint(0, 256, (5,)), (1, randn(2, 192)), randint(0, 256, (9,))]
127
+ ]
128
+
129
+ loss = model(text_images_and_audio)
130
+
131
+ loss.backward()
132
+
133
+ # after much training
134
+
135
+ one_multimodal_sample = model.sample()
136
+ ```
137
+
138
+ Automatically taking care of encoding and decoding of images
139
+
140
+ ```python
141
+ import torch
142
+ from torch import nn, randint, randn
143
+ from transfusion_pytorch import Transfusion, print_modality_sample
144
+
145
+ mock_encoder = nn.Conv2d(3, 384, 3, padding = 1)
146
+ mock_decoder = nn.Conv2d(384, 3, 3, padding = 1)
147
+
148
+ model = Transfusion(
149
+ num_text_tokens = 12,
150
+ dim_latent = 384,
151
+ channel_first_latent = True,
152
+ modality_default_shape = (4, 4),
153
+ modality_encoder = mock_encoder,
154
+ modality_decoder = mock_decoder,
155
+ transformer = dict(
156
+ dim = 512,
157
+ depth = 8
158
+ )
159
+ )
160
+
161
+ text_and_images = [
162
+ [
163
+ randint(0, 12, (16,)), # 16 text tokens
164
+ randn(3, 8, 8), # (8 x 8) 3 channeled image
165
+ randint(0, 12, (8,)), # 8 text tokens
166
+ randn(3, 7, 7) # (7 x 7) 3 channeled image
167
+ ],
168
+ [
169
+ randint(0, 12, (16,)), # 16 text tokens
170
+ randn(3, 8, 5), # (8 x 5) 3 channeled image
171
+ randint(0, 12, (5,)), # 5 text tokens
172
+ randn(3, 2, 16), # (2 x 16) 3 channeled image
173
+ randint(0, 12, (9,)) # 9 text tokens
174
+ ]
175
+ ]
176
+
177
+ loss = model(text_and_images)
178
+
179
+ loss.backward()
180
+
181
+ # after much training
182
+
183
+ one_multimodal_sample = model.sample()
184
+
185
+ print_modality_sample(one_multimodal_sample)
186
+ ```
187
+
188
+ To pretrain on language first, just pass in your text as type `Int['batch seq']`
189
+
190
+ ```python
191
+ import torch
192
+ from transfusion_pytorch import Transfusion
193
+
194
+ model = Transfusion(
195
+ num_text_tokens = 256,
196
+ dim_latent = 384,
197
+ transformer = dict(
198
+ dim = 512,
199
+ depth = 8,
200
+ )
201
+ ).cuda()
202
+
203
+ text = torch.randint(0, 256, (2, 1024)).cuda()
204
+
205
+ loss = model(text)
206
+ loss.backward()
207
+
208
+ # after much training
209
+
210
+ sampled = model.generate_text_only(text[:, :1], 1024)
211
+ ```
212
+
213
+ ## Citations
214
+
215
+ ```bibtex
216
+ @inproceedings{Zhou2024TransfusionPT,
217
+ title = {Transfusion: Predict the Next Token and Diffuse Images with One Multi-Modal Model},
218
+ author = {Chunting Zhou and Lili Yu and Arun Babu and Kushal Tirumala and Michihiro Yasunaga and Leonid Shamis and Jacob Kahn and Xuezhe Ma and Luke Zettlemoyer and Omer Levy},
219
+ year = {2024},
220
+ url = {https://api.semanticscholar.org/CorpusID:271909855}
221
+ }
222
+ ```
223
+
224
+ ```bibtex
225
+ @misc{Rubin2024,
226
+ author = {Ohad Rubin},
227
+ url = {https://medium.com/@ohadrubin/exploring-weight-decay-in-layer-normalization-challenges-and-a-reparameterization-solution-ad4d12c24950}
228
+ }
229
+ ```
230
+
231
+ ```bibtex
232
+ @article{Nguyen2024MinPS,
233
+ title = {Min P Sampling: Balancing Creativity and Coherence at High Temperature},
234
+ author = {Minh Nguyen and Andrew Baker and Andreas Kirsch and Clement Neo},
235
+ journal = {ArXiv},
236
+ year = {2024},
237
+ volume = {abs/2407.01082},
238
+ url = {https://api.semanticscholar.org/CorpusID:270870613}
239
+ }
240
+ ```
241
+
242
+ ```bibtex
243
+ @article{Bao2022AllAW,
244
+ title = {All are Worth Words: A ViT Backbone for Diffusion Models},
245
+ author = {Fan Bao and Shen Nie and Kaiwen Xue and Yue Cao and Chongxuan Li and Hang Su and Jun Zhu},
246
+ journal = {2023 IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR)},
247
+ year = {2022},
248
+ pages = {22669-22679},
249
+ url = {https://api.semanticscholar.org/CorpusID:253581703}
250
+ }
251
+ ```
252
+
253
+ ```bibtex
254
+ @inproceedings{Zhao2024MonoFormerOT,
255
+ title = {MonoFormer: One Transformer for Both Diffusion and Autoregression},
256
+ author = {Chuyang Zhao and Yuxing Song and Wenhao Wang and Haocheng Feng and Errui Ding and Yifan Sun and Xinyan Xiao and Jingdong Wang},
257
+ year = {2024},
258
+ url = {https://api.semanticscholar.org/CorpusID:272832492}
259
+ }
260
+ ```
261
+
262
+ ```bibtex
263
+ @article{Yang2024ConsistencyFM,
264
+ title = {Consistency Flow Matching: Defining Straight Flows with Velocity Consistency},
265
+ author = {Ling Yang and Zixiang Zhang and Zhilong Zhang and Xingchao Liu and Minkai Xu and Wentao Zhang and Chenlin Meng and Stefano Ermon and Bin Cui},
266
+ journal = {ArXiv},
267
+ year = {2024},
268
+ volume = {abs/2407.02398},
269
+ url = {https://api.semanticscholar.org/CorpusID:270878436}
270
+ }
271
+ ```
272
+
273
+ ```bibtex
274
+ @inproceedings{Zhou2024ValueRL,
275
+ title = {Value Residual Learning For Alleviating Attention Concentration In Transformers},
276
+ author = {Zhanchao Zhou and Tianyi Wu and Zhiyun Jiang and Zhenzhong Lan},
277
+ year = {2024},
278
+ url = {https://api.semanticscholar.org/CorpusID:273532030}
279
+ }
280
+ ```
281
+
282
+ ```bibtex
283
+ @inproceedings{Duvvuri2024LASERAW,
284
+ title = {LASER: Attention with Exponential Transformation},
285
+ author = {Sai Surya Duvvuri and Inderjit S. Dhillon},
286
+ year = {2024},
287
+ url = {https://api.semanticscholar.org/CorpusID:273849947}
288
+ }
289
+ ```
290
+
291
+ ```bibtex
292
+ @inproceedings{Dong2024HymbaAH,
293
+ title = {Hymba: A Hybrid-head Architecture for Small Language Models},
294
+ author = {Xin Dong and Y. Fu and Shizhe Diao and Wonmin Byeon and Zijia Chen and Ameya Mahabaleshwarkar and Shih-Yang Liu and Matthijs Van Keirsbilck and Min-Hung Chen and Yoshi Suhara and Yingyan Lin and Jan Kautz and Pavlo Molchanov},
295
+ year = {2024},
296
+ url = {https://api.semanticscholar.org/CorpusID:274166163}
297
+ ```
298
+
299
+ ```bibtex
300
+ @article{Zhu2024HyperConnections,
301
+ title = {Hyper-Connections},
302
+ author = {Defa Zhu and Hongzhi Huang and Zihao Huang and Yutao Zeng and Yunyao Mao and Banggu Wu and Qiyang Min and Xun Zhou},
303
+ journal = {ArXiv},
304
+ year = {2024},
305
+ volume = {abs/2409.19606},
306
+ url = {https://api.semanticscholar.org/CorpusID:272987528}
307
+ }
308
+ ```