{"id":15600981,"url":"https://github.com/lucidrains/megabyte-pytorch","last_synced_at":"2025-05-14T20:04:58.640Z","repository":{"id":165387180,"uuid":"640747712","full_name":"lucidrains/MEGABYTE-pytorch","owner":"lucidrains","description":"Implementation of MEGABYTE, Predicting Million-byte Sequences with Multiscale Transformers, in Pytorch","archived":false,"fork":false,"pushed_at":"2024-12-27T16:35:43.000Z","size":36145,"stargazers_count":640,"open_issues_count":4,"forks_count":55,"subscribers_count":10,"default_branch":"main","last_synced_at":"2025-04-13T15:09:56.856Z","etag":null,"topics":["artificial-intelligence","attention-mechanisms","deep-learning","learned-tokenization","long-context","transformers"],"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":"2023-05-15T03:27:36.000Z","updated_at":"2025-03-27T14:50:22.000Z","dependencies_parsed_at":"2025-02-21T09:11:24.766Z","dependency_job_id":"1995a663-6493-40d7-9b2c-a13f23fe3a04","html_url":"https://github.com/lucidrains/MEGABYTE-pytorch","commit_stats":{"total_commits":32,"total_committers":2,"mean_commits":16.0,"dds":0.03125,"last_synced_commit":"befceb7f2ee2aef4256a121d973011558839c26c"},"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2FMEGABYTE-pytorch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2FMEGABYTE-pytorch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2FMEGABYTE-pytorch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2FMEGABYTE-pytorch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lucidrains","download_url":"https://codeload.github.com/lucidrains/MEGABYTE-pytorch/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248732486,"owners_count":21152852,"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","learned-tokenization","long-context","transformers"],"created_at":"2024-10-03T02:10:54.231Z","updated_at":"2025-04-13T15:10:07.075Z","avatar_url":"https://github.com/lucidrains.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cimg src=\"./MEGABYTE.png\" width=\"450px\"\u003e\u003c/img\u003e\n\n## MEGABYTE - Pytorch\n\nImplementation of \u003ca href=\"https://arxiv.org/abs/2305.07185\"\u003eMEGABYTE\u003c/a\u003e, Predicting Million-byte Sequences with Multiscale Transformers, in Pytorch. Took the liberty to generalize it even further so one can have multiple local models.\n\n\u003ca href=\"https://github.com/lucidrains/simple-hierarchical-transformer\"\u003eSimilar independent research that is a further generalization\u003c/a\u003e\n\n## Appreciation\n\n- \u003ca href=\"https://stability.ai/\"\u003eStability\u003c/a\u003e and \u003ca href=\"https://huggingface.co/\"\u003e🤗 Huggingface\u003c/a\u003e for the generous sponsorship to work on and open source cutting edge artificial intelligence research\n\n## Install\n\n```bash\n$ pip install MEGABYTE-pytorch\n```\n\n## Usage\n\n```python\nimport torch\nfrom MEGABYTE_pytorch import MEGABYTE\n\nmodel = MEGABYTE(\n    num_tokens = 16000,             # number of tokens\n    dim = (512, 256),               # transformer model dimension (512 for coarsest, 256 for fine in this example)\n    max_seq_len = (1024, 4),        # sequence length for global and then local. this can be more than 2\n    depth = (6, 4),                 # number of layers for global and then local. this can be more than 2, but length must match the max_seq_len's\n    dim_head = 64,                  # dimension per head\n    heads = 8,                      # number of attention heads\n    flash_attn = True               # use flash attention\n)\n\nx = torch.randint(0, 16000, (1, 1024, 4))\n\nloss = model(x, return_loss = True)\nloss.backward()\n\n# then after much training\n\nlogits = model(x)\n\n# and sample from the logits accordingly\n# or you can use the generate function\n\nsampled = model.generate(temperature = 0.9, filter_thres = 0.9) # (1, 1024, 4)\n```\n\n## Test\n\nTrain on character-level enwik8 with patches of size 4 - length 8192\n\n```bash\n$ python train.py\n```\n\n## Citations\n\n```bibtex\n@misc{yu2023megabyte,\n    title   = {MEGABYTE: Predicting Million-byte Sequences with Multiscale Transformers}, \n    author  = {Lili Yu and Dániel Simig and Colin Flaherty and Armen Aghajanyan and Luke Zettlemoyer and Mike Lewis},\n    year    = {2023},\n    eprint  = {2305.07185},\n    archivePrefix = {arXiv},\n    primaryClass = {cs.LG}\n}\n```\n\n```bibtex\n@misc{https://doi.org/10.48550/arxiv.2302.01327,\n    doi     = {10.48550/ARXIV.2302.01327},\n    url     = {https://arxiv.org/abs/2302.01327},\n    author  = {Kumar, Manoj and Dehghani, Mostafa and Houlsby, Neil},\n    title   = {Dual PatchNorm},\n    publisher = {arXiv},\n    year    = {2023},\n    copyright = {Creative Commons Attribution 4.0 International}\n}\n```\n\n```bibtex\n@inproceedings{dao2022flashattention,\n    title   = {Flash{A}ttention: Fast and Memory-Efficient Exact Attention with {IO}-Awareness},\n    author  = {Dao, Tri and Fu, Daniel Y. and Ermon, Stefano and Rudra, Atri and R{\\'e}, Christopher},\n    booktitle = {Advances in Neural Information Processing Systems},\n    year    = {2022}\n}\n```\n\n```bibtex\n@software{peng_bo_2021_5196578,\n    author    = {PENG Bo},\n    title     = {BlinkDL/RWKV-LM: 0.01},\n    month     = {aug},\n    year      = {2021},\n    publisher = {Zenodo},\n    version   = {0.01},\n    doi       = {10.5281/zenodo.5196578},\n    url       = {https://doi.org/10.5281/zenodo.5196578}\n}\n```\n\n```bibtex\n@article{Kazemnejad2023TheIO,\n    title   = {The Impact of Positional Encoding on Length Generalization in Transformers},\n    author  = {Amirhossein Kazemnejad and Inkit Padhi and Karthikeyan Natesan Ramamurthy and Payel Das and Siva Reddy},\n    journal = {ArXiv},\n    year    = {2023},\n    volume  = {abs/2305.19466}\n}\n```\n\n```bibtex\n@misc{su2021roformer,\n    title   = {RoFormer: Enhanced Transformer with Rotary Position Embedding},\n    author  = {Jianlin Su and Yu Lu and Shengfeng Pan and Bo Wen and Yunfeng Liu},\n    year    = {2021},\n    eprint  = {2104.09864},\n    archivePrefix = {arXiv},\n    primaryClass = {cs.CL}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucidrains%2Fmegabyte-pytorch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flucidrains%2Fmegabyte-pytorch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucidrains%2Fmegabyte-pytorch/lists"}