{"id":15600929,"url":"https://github.com/lucidrains/feedback-transformer-pytorch","last_synced_at":"2025-08-22T00:31:34.557Z","repository":{"id":57429003,"uuid":"335391302","full_name":"lucidrains/feedback-transformer-pytorch","owner":"lucidrains","description":"Implementation of Feedback Transformer in Pytorch","archived":false,"fork":false,"pushed_at":"2021-03-02T15:18:04.000Z","size":52,"stargazers_count":104,"open_issues_count":3,"forks_count":2,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-11-27T13:21:55.356Z","etag":null,"topics":["artifiical-intelligence","attention-mechanism","deep-learning","memory","transformer"],"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-02-02T18:51:31.000Z","updated_at":"2024-09-29T07:26:09.000Z","dependencies_parsed_at":"2022-09-08T23:40:44.852Z","dependency_job_id":null,"html_url":"https://github.com/lucidrains/feedback-transformer-pytorch","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2Ffeedback-transformer-pytorch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2Ffeedback-transformer-pytorch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2Ffeedback-transformer-pytorch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2Ffeedback-transformer-pytorch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lucidrains","download_url":"https://codeload.github.com/lucidrains/feedback-transformer-pytorch/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230542288,"owners_count":18242332,"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":["artifiical-intelligence","attention-mechanism","deep-learning","memory","transformer"],"created_at":"2024-10-03T02:08:48.726Z","updated_at":"2024-12-20T06:06:49.101Z","avatar_url":"https://github.com/lucidrains.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Feedback Transformer - Pytorch\n\nSimple implementation of \u003ca href=\"https://arxiv.org/abs/2002.09402\"\u003eFeedback Transformer\u003c/a\u003e in Pytorch. They improve on Transformer-XL by having each token have access to the representations of all previous layers through time. This is achieved by aggregating the outputs of all layers into a shared memory, which each token across layers can attend to at each time step.\n\nThe main drawback is longer training time, due to its non-parallel nature. But I thought I'd build it to further exploration and research into this line of work.\n\n\u003ca href=\"https://www.youtube.com/watch?v=zdb8MM94A5c\"\u003eYannic Kilcher video\u003c/a\u003e\n\nI also took the liberty to add some various enhancements, including pre-normalization, GLU gated feedforwards, as well as simplified T5 relative positional embeddings.\n\n## Install\n\n```bash\n$ pip install feedback-transformer-pytorch\n```\n\n## Usage\n\n```python\nimport torch\nfrom feedback_transformer_pytorch import FeedbackTransformer\n\nmodel = FeedbackTransformer(\n    num_tokens = 20000,           # number of tokens\n    dim = 512,                    # dimension\n    depth = 6,                    # depth\n    seq_len = 2,                  # the sequence length of each segment or window\n    mem_len = 256,                # length of the memory buffer\n    dim_head = 64,                # dimension of each head\n    heads = 8,                    # number of heads\n    attn_dropout = 0.1,           # attention dropout\n    ff_dropout = 0.1              # feedforward dropout\n).cuda()\n\nx = torch.randint(0, 20000, (2, 64)).cuda()\nmodel(x)  # (2, 64, 20000)\n```\n\nIf you would like to have fine control over the memory (when to detach, etc), you can do it with some extra keyword arguments on `.forward`\n\n```python\nimport torch\nfrom feedback_transformer_pytorch import FeedbackTransformer\n\nmodel = FeedbackTransformer(\n    num_tokens = 20000,\n    dim = 512,\n    depth = 6,\n    seq_len = 32,\n    mem_len = 256\n).cuda()\n\nx1 = torch.randint(0, 20000, (2, 32)).cuda()\nx2 = torch.randint(0, 20000, (2, 32)).cuda()\nx3 = torch.randint(0, 20000, (2, 32)).cuda()\n\nout1, mem1 = model(x1, return_memory = True)\nout2, mem2 = model(x2, memory = mem1, return_memory = True)\nout3, mem3 = model(x3, memory = mem2, return_memory = True)  # (2, 32, 20000)\n```\n\n## Citations\n\n```bibtex\n@misc{fan2021addressing,\n    title   = {Addressing Some Limitations of Transformers with Feedback Memory}, \n    author  = {Angela Fan and Thibaut Lavril and Edouard Grave and Armand Joulin and Sainbayar Sukhbaatar},\n    year    = {2021},\n    eprint  = {2002.09402},\n    archivePrefix = {arXiv},\n    primaryClass = {cs.LG}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucidrains%2Ffeedback-transformer-pytorch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flucidrains%2Ffeedback-transformer-pytorch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucidrains%2Ffeedback-transformer-pytorch/lists"}