{"id":15600966,"url":"https://github.com/lucidrains/long-short-transformer","last_synced_at":"2025-08-20T05:30:55.704Z","repository":{"id":48739070,"uuid":"383859079","full_name":"lucidrains/long-short-transformer","owner":"lucidrains","description":"Implementation of Long-Short Transformer, combining local and global inductive biases for attention over long sequences, in Pytorch","archived":false,"fork":false,"pushed_at":"2021-08-04T12:20:55.000Z","size":35872,"stargazers_count":116,"open_issues_count":1,"forks_count":12,"subscribers_count":6,"default_branch":"main","last_synced_at":"2024-12-10T00:42:01.274Z","etag":null,"topics":["artificial-intelligence","attention-mechanism","deep-learning","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}},"created_at":"2021-07-07T16:20:43.000Z","updated_at":"2024-09-02T13:58:15.000Z","dependencies_parsed_at":"2022-08-26T08:22:30.725Z","dependency_job_id":null,"html_url":"https://github.com/lucidrains/long-short-transformer","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2Flong-short-transformer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2Flong-short-transformer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2Flong-short-transformer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2Flong-short-transformer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lucidrains","download_url":"https://codeload.github.com/lucidrains/long-short-transformer/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230394228,"owners_count":18218707,"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-mechanism","deep-learning","transformers"],"created_at":"2024-10-03T02:10:31.685Z","updated_at":"2024-12-19T07:07:19.073Z","avatar_url":"https://github.com/lucidrains.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cimg src=\"./long-short.png\" width=\"400px\"\u003e\u003c/img\u003e\n\n## Long-Short Transformer\n\nImplementation of \u003ca href=\"https://arxiv.org/abs/2107.02192\"\u003eLong-Short Transformer\u003c/a\u003e, combining local and global inductive biases for attention over long sequences, in Pytorch\n\n## Install\n\n```bash\n$ pip install long-short-transformer\n```\n\n## Usage\n\n```python\nimport torch\nfrom long_short_transformer import LongShortTransformer\n\nmodel = LongShortTransformer(\n    num_tokens = 20000,\n    dim = 512,\n    depth = 6,             # how deep\n    heads = 8,             # number of heads\n    dim_head = 64,         # dimension per head\n    max_seq_len = 1024,    # maximum sequence length\n    window_size = 128,     # local attention window size\n    r = 256                # like linformer, the sequence length is projected down to this value to avoid the quadratic, where r \u003c\u003c n (seq len)\n)\n\nx = torch.randint(0, 20000, (1, 1024))\nmask = torch.ones(1, 1024).bool()\n\nlogits = model(x, mask = mask) # (1, 1024, 20000)\n```\n\nFor the autoregressive case, you will have to also supply the `segment_size` and set `causal` to `True`\n\n```python\nimport torch\nfrom long_short_transformer import LongShortTransformer\n\nmodel = LongShortTransformer(\n    num_tokens = 20000,\n    dim = 512,\n    depth = 6,             # how deep\n    heads = 8,             # number of heads\n    dim_head = 64,         # dimension per head\n    causal = True,         # autoregressive or not\n    max_seq_len = 1024,    # maximum sequence length\n    window_size = 128,     # local attention window size\n    segment_size = 16,     # sequence is divided into segments of this size, to be projected down to r\n    r = 1                  # paper claimed best results with segment to r of 16:1\n)\n\nx = torch.randint(0, 20000, (1, 1024))\nmask = torch.ones(1, 1024).bool()\n\nlogits = model(x, mask = mask) # (1, 1024, 20000)\n```\n\nYou can test the autoregressive on enwik8 with\n\n```bash\n$ python train.py\n```\n\n## Citations\n\n```bibtex\n@misc{zhu2021longshort,\n    title   = {Long-Short Transformer: Efficient Transformers for Language and Vision}, \n    author  = {Chen Zhu and Wei Ping and Chaowei Xiao and Mohammad Shoeybi and Tom Goldstein and Anima Anandkumar and Bryan Catanzaro},\n    year    = {2021},\n    eprint  = {2107.02192},\n    archivePrefix = {arXiv},\n    primaryClass = {cs.CV}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucidrains%2Flong-short-transformer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flucidrains%2Flong-short-transformer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucidrains%2Flong-short-transformer/lists"}