mmdit


Namemmdit JSON
Version 0.1.0 PyPI version JSON
download
home_pageNone
SummaryMMDiT
upload_time2024-05-10 15:36:25
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 improvised variant of the attention that adaptively selects the weights to use through learned gating.

## 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(1, 256)

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

image_tokens = torch.randn(1, 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(1, 256)

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

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

audio_tokens = torch.randn(1, 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}
}
```

            

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/95/05/54ca19c0d43a845a755543cebc538260b6aa41a0995db33adf6dd8ebdfdb/mmdit-0.1.0.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 improvised variant of the attention that adaptively selects the weights to use through learned gating.\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(1, 256)\n\ntext_tokens = torch.randn(1, 512, 768)\ntext_mask = torch.ones((1, 512)).bool()\n\nimage_tokens = torch.randn(1, 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(1, 256)\n\ntext_tokens = torch.randn(1, 512, 768)\ntext_mask = torch.ones((1, 512)).bool()\n\nvideo_tokens = torch.randn(1, 1024, 512)\n\naudio_tokens = torch.randn(1, 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",
    "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.1.0",
    "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": "e54d33e357493ccd921063e25192623f72b6bf9d6465ea995667dcfde4520d82",
                "md5": "8617b3ad4a6491433396d5a81ea83a86",
                "sha256": "5a5f9939d9e9ae395bfd973f2356ab3b4024b8246b917127abb5092e5d3ad5a3"
            },
            "downloads": -1,
            "filename": "mmdit-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "8617b3ad4a6491433396d5a81ea83a86",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 9611,
            "upload_time": "2024-05-10T15:36:23",
            "upload_time_iso_8601": "2024-05-10T15:36:23.339514Z",
            "url": "https://files.pythonhosted.org/packages/e5/4d/33e357493ccd921063e25192623f72b6bf9d6465ea995667dcfde4520d82/mmdit-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "950554ca19c0d43a845a755543cebc538260b6aa41a0995db33adf6dd8ebdfdb",
                "md5": "68322c3ef0e9e13ab66eb8259f5bfd72",
                "sha256": "bd23bcc83eced0b362135851ea5b1aca1678a8edfc91f34e0d2a3ae96bb46fa1"
            },
            "downloads": -1,
            "filename": "mmdit-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "68322c3ef0e9e13ab66eb8259f5bfd72",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 148208,
            "upload_time": "2024-05-10T15:36:25",
            "upload_time_iso_8601": "2024-05-10T15:36:25.378185Z",
            "url": "https://files.pythonhosted.org/packages/95/05/54ca19c0d43a845a755543cebc538260b6aa41a0995db33adf6dd8ebdfdb/mmdit-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-10 15:36:25",
    "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.36020s