mmdit


Namemmdit JSON
Version 0.2.1 PyPI version JSON
download
home_pageNone
SummaryMMDiT
upload_time2024-12-27 15:25:09
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseMIT License Copyright (c) 2024 Phil Wang Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords artificial intelligence attention mechanism deep learning multi-modal transformer
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <img src="./mmdit.png" width="300px"></img>

## MMDiT

Implementation of a single layer of the MMDiT, proposed by Esser et al. in <a href="https://arxiv.org/abs/2403.03206">Stable Diffusion 3</a>, in Pytorch

Besides a straight reproduction, will also generalize to > 2 modalities, as I can envision an MMDiT for images, audio, and text.

Will also offer an <a href="https://github.com/lucidrains/mmdit/blob/main/mmdit/adaptive_attention.py">improvised variant of self attention</a> that adaptively selects the weights to use through learned gating. This idea came from adaptive convolutions applied by Kang et al. for GigaGAN.

## Install

```bash
$ pip install mmdit
```

## Usage

```python
import torch
from mmdit import MMDiTBlock

# define mm dit block

block = MMDiTBlock(
    dim_joint_attn = 512,
    dim_cond = 256,
    dim_text = 768,
    dim_image = 512,
    qk_rmsnorm = True
)

# mock inputs

time_cond = torch.randn(2, 256)

text_tokens = torch.randn(2, 512, 768)
text_mask = torch.ones((2, 512)).bool()

image_tokens = torch.randn(2, 1024, 512)

# single block forward

text_tokens_next, image_tokens_next = block(
    time_cond = time_cond,
    text_tokens = text_tokens,
    text_mask = text_mask,
    image_tokens = image_tokens
)
```

A generalized version can be used as so

```python
import torch
from mmdit.mmdit_generalized_pytorch import MMDiT

mmdit = MMDiT(
    depth = 2, 
    dim_modalities = (768, 512, 384),
    dim_joint_attn = 512,
    dim_cond = 256,
    qk_rmsnorm = True
)

# mock inputs

time_cond = torch.randn(2, 256)

text_tokens = torch.randn(2, 512, 768)
text_mask = torch.ones((2, 512)).bool()

video_tokens = torch.randn(2, 1024, 512)

audio_tokens = torch.randn(2, 256, 384)

# forward

text_tokens, video_tokens, audio_tokens = mmdit(
    modality_tokens = (text_tokens, video_tokens, audio_tokens),
    modality_masks = (text_mask, None, None),
    time_cond = time_cond,
)
```

## Citations

```bibtex
@article{Esser2024ScalingRF,
    title   = {Scaling Rectified Flow Transformers for High-Resolution Image Synthesis},
    author  = {Patrick Esser and Sumith Kulal and A. Blattmann and Rahim Entezari and Jonas Muller and Harry Saini and Yam Levi and Dominik Lorenz and Axel Sauer and Frederic Boesel and Dustin Podell and Tim Dockhorn and Zion English and Kyle Lacey and Alex Goodwin and Yannik Marek and Robin Rombach},
    journal = {ArXiv},
    year    = {2024},
    volume  = {abs/2403.03206},
    url     = {https://api.semanticscholar.org/CorpusID:268247980}
}
```

```bibtex
@inproceedings{Darcet2023VisionTN,
    title   = {Vision Transformers Need Registers},
    author  = {Timoth'ee Darcet and Maxime Oquab and Julien Mairal and Piotr Bojanowski},
    year    = {2023},
    url     = {https://api.semanticscholar.org/CorpusID:263134283}
}
```

```bibtex
@article{Zhu2024HyperConnections,
    title   = {Hyper-Connections},
    author  = {Defa Zhu and Hongzhi Huang and Zihao Huang and Yutao Zeng and Yunyao Mao and Banggu Wu and Qiyang Min and Xun Zhou},
    journal = {ArXiv},
    year    = {2024},
    volume  = {abs/2409.19606},
    url     = {https://api.semanticscholar.org/CorpusID:272987528}
}
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "mmdit",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "artificial intelligence, attention mechanism, deep learning, multi-modal transformer",
    "author": null,
    "author_email": "Phil Wang <lucidrains@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/89/5f/20819402e9167c45729d499158843061c549fd6748adcecd2fe232417fb7/mmdit-0.2.1.tar.gz",
    "platform": null,
    "description": "<img src=\"./mmdit.png\" width=\"300px\"></img>\n\n## MMDiT\n\nImplementation of a single layer of the MMDiT, proposed by Esser et al. in <a href=\"https://arxiv.org/abs/2403.03206\">Stable Diffusion 3</a>, in Pytorch\n\nBesides a straight reproduction, will also generalize to > 2 modalities, as I can envision an MMDiT for images, audio, and text.\n\nWill also offer an <a href=\"https://github.com/lucidrains/mmdit/blob/main/mmdit/adaptive_attention.py\">improvised variant of self attention</a> that adaptively selects the weights to use through learned gating. This idea came from adaptive convolutions applied by Kang et al. for GigaGAN.\n\n## Install\n\n```bash\n$ pip install mmdit\n```\n\n## Usage\n\n```python\nimport torch\nfrom mmdit import MMDiTBlock\n\n# define mm dit block\n\nblock = MMDiTBlock(\n    dim_joint_attn = 512,\n    dim_cond = 256,\n    dim_text = 768,\n    dim_image = 512,\n    qk_rmsnorm = True\n)\n\n# mock inputs\n\ntime_cond = torch.randn(2, 256)\n\ntext_tokens = torch.randn(2, 512, 768)\ntext_mask = torch.ones((2, 512)).bool()\n\nimage_tokens = torch.randn(2, 1024, 512)\n\n# single block forward\n\ntext_tokens_next, image_tokens_next = block(\n    time_cond = time_cond,\n    text_tokens = text_tokens,\n    text_mask = text_mask,\n    image_tokens = image_tokens\n)\n```\n\nA generalized version can be used as so\n\n```python\nimport torch\nfrom mmdit.mmdit_generalized_pytorch import MMDiT\n\nmmdit = MMDiT(\n    depth = 2, \n    dim_modalities = (768, 512, 384),\n    dim_joint_attn = 512,\n    dim_cond = 256,\n    qk_rmsnorm = True\n)\n\n# mock inputs\n\ntime_cond = torch.randn(2, 256)\n\ntext_tokens = torch.randn(2, 512, 768)\ntext_mask = torch.ones((2, 512)).bool()\n\nvideo_tokens = torch.randn(2, 1024, 512)\n\naudio_tokens = torch.randn(2, 256, 384)\n\n# forward\n\ntext_tokens, video_tokens, audio_tokens = mmdit(\n    modality_tokens = (text_tokens, video_tokens, audio_tokens),\n    modality_masks = (text_mask, None, None),\n    time_cond = time_cond,\n)\n```\n\n## Citations\n\n```bibtex\n@article{Esser2024ScalingRF,\n    title   = {Scaling Rectified Flow Transformers for High-Resolution Image Synthesis},\n    author  = {Patrick Esser and Sumith Kulal and A. Blattmann and Rahim Entezari and Jonas Muller and Harry Saini and Yam Levi and Dominik Lorenz and Axel Sauer and Frederic Boesel and Dustin Podell and Tim Dockhorn and Zion English and Kyle Lacey and Alex Goodwin and Yannik Marek and Robin Rombach},\n    journal = {ArXiv},\n    year    = {2024},\n    volume  = {abs/2403.03206},\n    url     = {https://api.semanticscholar.org/CorpusID:268247980}\n}\n```\n\n```bibtex\n@inproceedings{Darcet2023VisionTN,\n    title   = {Vision Transformers Need Registers},\n    author  = {Timoth'ee Darcet and Maxime Oquab and Julien Mairal and Piotr Bojanowski},\n    year    = {2023},\n    url     = {https://api.semanticscholar.org/CorpusID:263134283}\n}\n```\n\n```bibtex\n@article{Zhu2024HyperConnections,\n    title   = {Hyper-Connections},\n    author  = {Defa Zhu and Hongzhi Huang and Zihao Huang and Yutao Zeng and Yunyao Mao and Banggu Wu and Qiyang Min and Xun Zhou},\n    journal = {ArXiv},\n    year    = {2024},\n    volume  = {abs/2409.19606},\n    url     = {https://api.semanticscholar.org/CorpusID:272987528}\n}\n```\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2024 Phil Wang  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "MMDiT",
    "version": "0.2.1",
    "project_urls": {
        "Homepage": "https://pypi.org/project/mmdit/",
        "Repository": "https://github.com/lucidrains/mmdit"
    },
    "split_keywords": [
        "artificial intelligence",
        " attention mechanism",
        " deep learning",
        " multi-modal transformer"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b72e0ca2323e8b056068c689f9f8a5422dd109fc8cafcdaba399b4d6d676946d",
                "md5": "6380b4c6b952667ea5679f457f20c564",
                "sha256": "aba24234da57f0f371e9caba1a287d93bdb483450b496ab84f8792886aee7413"
            },
            "downloads": -1,
            "filename": "mmdit-0.2.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6380b4c6b952667ea5679f457f20c564",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 10510,
            "upload_time": "2024-12-27T15:25:06",
            "upload_time_iso_8601": "2024-12-27T15:25:06.747370Z",
            "url": "https://files.pythonhosted.org/packages/b7/2e/0ca2323e8b056068c689f9f8a5422dd109fc8cafcdaba399b4d6d676946d/mmdit-0.2.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "895f20819402e9167c45729d499158843061c549fd6748adcecd2fe232417fb7",
                "md5": "5c60f68fded18cdd4c2f09df057dca61",
                "sha256": "1b91277a8cb64ebce080c555d01f6f1ca81fde67bba090bfa3f01def9c8f0049"
            },
            "downloads": -1,
            "filename": "mmdit-0.2.1.tar.gz",
            "has_sig": false,
            "md5_digest": "5c60f68fded18cdd4c2f09df057dca61",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 148985,
            "upload_time": "2024-12-27T15:25:09",
            "upload_time_iso_8601": "2024-12-27T15:25:09.291782Z",
            "url": "https://files.pythonhosted.org/packages/89/5f/20819402e9167c45729d499158843061c549fd6748adcecd2fe232417fb7/mmdit-0.2.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-27 15:25:09",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "lucidrains",
    "github_project": "mmdit",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "mmdit"
}
        
Elapsed time: 0.49853s