{"id":15600944,"url":"https://github.com/lucidrains/ponder-transformer","last_synced_at":"2025-04-14T22:20:32.466Z","repository":{"id":57454101,"uuid":"399804008","full_name":"lucidrains/ponder-transformer","owner":"lucidrains","description":"Implementation of a Transformer that Ponders, using the scheme from the PonderNet paper","archived":false,"fork":false,"pushed_at":"2021-10-30T03:31:24.000Z","size":17,"stargazers_count":80,"open_issues_count":2,"forks_count":8,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-03-28T10:21:18.910Z","etag":null,"topics":["adaptive-computation-time","artificial-intelligence","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-08-25T12:06:59.000Z","updated_at":"2024-12-24T02:51:19.000Z","dependencies_parsed_at":"2022-08-29T11:12:05.881Z","dependency_job_id":null,"html_url":"https://github.com/lucidrains/ponder-transformer","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2Fponder-transformer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2Fponder-transformer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2Fponder-transformer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2Fponder-transformer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lucidrains","download_url":"https://codeload.github.com/lucidrains/ponder-transformer/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248969049,"owners_count":21191181,"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":["adaptive-computation-time","artificial-intelligence","deep-learning","transformers"],"created_at":"2024-10-03T02:09:58.409Z","updated_at":"2025-04-14T22:20:32.426Z","avatar_url":"https://github.com/lucidrains.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Ponder(ing) Transformer\n\nImplementation of a Transformer that learns to adapt the number of computational steps it takes depending on the difficulty of the input sequence, using the scheme from the \u003ca href=\"https://arxiv.org/abs/2107.05407\"\u003ePonderNet\u003c/a\u003e paper. Will also try to abstract out a pondering module that can be used with any block that returns an output with the halting probability.\n\nThis repository would not have been possible without repeated viewings of \u003ca href=\"https://www.youtube.com/watch?v=nQDZmf2Yb9k\"\u003eYannic's educational video\u003c/a\u003e\n\n## Install\n\n```bash\n$ pip install ponder-transformer\n```\n\n## Usage\n\n```python\nimport torch\nfrom ponder_transformer import PonderTransformer\n\nmodel = PonderTransformer(\n    num_tokens = 20000,\n    dim = 512,\n    max_seq_len = 512\n)\n\nmask = torch.ones(1, 512).bool()\n\nx = torch.randint(0, 20000, (1, 512))\ny = torch.randint(0, 20000, (1, 512))\n\nloss = model(x, labels = y, mask = mask)\nloss.backward()\n```\n\nNow you can set the model to `.eval()` mode and it will terminate early when all samples of the batch have emitted a halting signal\n\n```python\nimport torch\nfrom ponder_transformer import PonderTransformer\n\nmodel = PonderTransformer(\n    num_tokens = 20000,\n    dim = 512,\n    max_seq_len = 512,\n    causal = True\n)\n\nx = torch.randint(0, 20000, (2, 512))\nmask = torch.ones(2, 512).bool()\n\nmodel.eval() # setting to eval makes it return the logits as well as the halting indices\n\nlogits, layer_indices = model(x,  mask = mask) # (2, 512, 20000), (2)\n\n# layer indices will contain, for each batch element, which layer they exited\n```\n\n## Citations\n\n```bibtex\n@misc{banino2021pondernet,\n    title   = {PonderNet: Learning to Ponder}, \n    author  = {Andrea Banino and Jan Balaguer and Charles Blundell},\n    year    = {2021},\n    eprint  = {2107.05407},\n    archivePrefix = {arXiv},\n    primaryClass = {cs.LG}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucidrains%2Fponder-transformer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flucidrains%2Fponder-transformer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucidrains%2Fponder-transformer/lists"}