{"id":15600973,"url":"https://github.com/lucidrains/mega-pytorch","last_synced_at":"2025-04-07T05:10:10.841Z","repository":{"id":60025527,"uuid":"540610272","full_name":"lucidrains/Mega-pytorch","owner":"lucidrains","description":"Implementation of Mega, the Single-head Attention with Multi-headed EMA architecture that currently holds SOTA on Long Range Arena","archived":false,"fork":false,"pushed_at":"2023-08-26T17:59:00.000Z","size":35814,"stargazers_count":204,"open_issues_count":1,"forks_count":11,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-03-31T04:06:50.982Z","etag":null,"topics":["artificial-intelligence","attention-mechanisms","deep-learning","exponential-moving-average","long-range-arena"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lucidrains.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-09-23T20:40:57.000Z","updated_at":"2024-12-26T03:09:43.000Z","dependencies_parsed_at":"2024-10-23T01:34:11.966Z","dependency_job_id":null,"html_url":"https://github.com/lucidrains/Mega-pytorch","commit_stats":{"total_commits":26,"total_committers":1,"mean_commits":26.0,"dds":0.0,"last_synced_commit":"dc765fd5313bf02419a473dfc819b2ab33e046e8"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2FMega-pytorch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2FMega-pytorch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2FMega-pytorch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2FMega-pytorch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lucidrains","download_url":"https://codeload.github.com/lucidrains/Mega-pytorch/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247595334,"owners_count":20963943,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["artificial-intelligence","attention-mechanisms","deep-learning","exponential-moving-average","long-range-arena"],"created_at":"2024-10-03T02:10:40.367Z","updated_at":"2025-04-07T05:10:10.821Z","avatar_url":"https://github.com/lucidrains.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cimg src=\"./mega.png\" width=\"450px\"\u003e\u003c/img\u003e\n\n## Mega - Moving Average Equipped Gated Attention - Pytorch\n\nImplementation of the \u003ca href=\"https://arxiv.org/abs/2209.10655\"\u003eMega\u003c/a\u003e layer, the Single-head Attention with Multi-headed EMA layer that exists in the architecture that currently holds SOTA on Long Range Arena, beating S4 on Pathfinder-X and all the other tasks save for audio.\n\n## Install\n\n```bash\n$ pip install mega-pytorch\n```\n\n## Usage\n\nThe Mega Layer with combination of attention and learned EMA\n\n```python\nimport torch\nfrom mega_pytorch import MegaLayer\n\nlayer = MegaLayer(\n    dim = 128,                   # model dimensions\n    ema_heads = 16,              # number of EMA heads\n    attn_dim_qk = 64,            # dimension of queries / keys in attention\n    attn_dim_value = 256,        # dimension of values in attention\n    laplacian_attn_fn = False,   # whether to use softmax (false) or laplacian attention activation fn (true)\n)\n\nx = torch.randn(1, 1024, 128)     # (batch, seq, dim)\n\nout = layer(x) # (1, 1024, 128)\n```\n\nFull Mega (with layernorm for now)\n\n```python\nimport torch\nfrom mega_pytorch import Mega\n\nmega = Mega(\n    num_tokens = 256,            # number of tokens\n    dim = 128,                   # model dimensions\n    depth = 6,                   # depth\n    ema_heads = 16,              # number of EMA heads\n    attn_dim_qk = 64,            # dimension of queries / keys in attention\n    attn_dim_value = 256,        # dimensino of values in attention\n    laplacian_attn_fn = True,    # whether to use softmax (false) or laplacian attention activation fn (true)\n)\n\nx = torch.randint(0, 256, (1, 1024))\n\nlogits = mega(x) # (1, 1024, 256)\n```\n\n## Todo\n\n- [ ] add dynamic positional bias for best length extrapolation arch\n\n## Citations\n\n```bibtex\n@inproceedings{Ma2022MegaMA,\n    title   = {Mega: Moving Average Equipped Gated Attention},\n    author  = {Xuezhe Ma and Chunting Zhou and Xiang Kong and Junxian He and Liangke Gui and Graham Neubig and Jonathan May and Luke Zettlemoyer},\n    year    = {2022}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucidrains%2Fmega-pytorch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flucidrains%2Fmega-pytorch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucidrains%2Fmega-pytorch/lists"}