{"id":19156227,"url":"https://github.com/kyegomez/selfextend","last_synced_at":"2025-10-08T17:24:30.804Z","repository":{"id":215346566,"uuid":"738703559","full_name":"kyegomez/SelfExtend","owner":"kyegomez","description":"Implementation of SelfExtend from the paper \"LLM Maybe LongLM: Self-Extend LLM Context Window Without Tuning\" from Pytorch and Zeta","archived":false,"fork":false,"pushed_at":"2024-11-11T03:27:09.000Z","size":2272,"stargazers_count":13,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-19T20:16:55.926Z","etag":null,"topics":["ai","artificial-intelligence","attention-is-all-you-need","attention-mechanism","attention-model","gpt4","machine-learning","ml","pytorch","torch"],"latest_commit_sha":null,"homepage":"https://discord.gg/GYbXvDGevY","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/kyegomez.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","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},"funding":{"github":["kyegomez"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"lfx_crowdfunding":null,"custom":null}},"created_at":"2024-01-03T21:08:57.000Z","updated_at":"2024-09-05T23:11:46.000Z","dependencies_parsed_at":"2024-01-07T04:25:38.039Z","dependency_job_id":"4b3c8862-dd97-40ad-ad2b-e18cbbb9f542","html_url":"https://github.com/kyegomez/SelfExtend","commit_stats":null,"previous_names":["kyegomez/selfextend"],"tags_count":0,"template":false,"template_full_name":"kyegomez/Python-Package-Template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kyegomez%2FSelfExtend","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kyegomez%2FSelfExtend/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kyegomez%2FSelfExtend/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kyegomez%2FSelfExtend/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kyegomez","download_url":"https://codeload.github.com/kyegomez/SelfExtend/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252834239,"owners_count":21811342,"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":["ai","artificial-intelligence","attention-is-all-you-need","attention-mechanism","attention-model","gpt4","machine-learning","ml","pytorch","torch"],"created_at":"2024-11-09T08:33:40.440Z","updated_at":"2025-10-08T17:24:25.786Z","avatar_url":"https://github.com/kyegomez.png","language":"Python","funding_links":["https://github.com/sponsors/kyegomez"],"categories":[],"sub_categories":[],"readme":"[![Multi-Modality](agorabanner.png)](https://discord.gg/qUtxnK2NMf)\n\n# SelfExtendAttn\nImplementation of SelfExtendAttn from the paper \"LLM Maybe LongLM: Self-Extend LLM Context Window Without Tuning\" from Pytorch and Zeta. This implementation is based mostly on the pseudocode listed in Algorithm 1 in page 4\n\n\n# Install\n`pip install selfextend`\n\n\n## Usage\n```python\nimport torch\nfrom se_attn import SelfExtendAttn\n\n# Example usage\ndim = 512  # Dimension of model\ng_size = 2  # Group size\nw_size = 4  # Window size for neighbor tokens\nself_extend = SelfExtendAttn(dim, g_size, w_size, qk_norm=True)\n\n# Example tensors for q, k, v, and pos\nq = torch.randn(1, 10, dim)\nk = torch.randn(1, 10, dim)\nv = torch.randn(1, 10, dim)\npos = torch.arange(0, 10).unsqueeze(0)  # Example positional indices\n\noutput = self_extend(q, k, v, pos)\nprint(output)\n```\n\n---\n\n## Technical Architecture\n\n### Key Concepts\n\n- **Grouped Attention**: This mechanism divides the input sequence into groups and applies the attention operation within each group. It uses a floor operation to adjust the positions within the groups, enabling efficient handling of longer sequences.\n  \n- **Normal Attention**: Standard self-attention used in transformers, focusing on nearby tokens within a specified window.\n\n### Attention Mechanism\n\nThe `SelfExtendAttn` module integrates these two attention strategies:\n\n1. **Normal Attention** is applied to tokens within a neighborhood window, maintaining precise positional information for closely related tokens.\n   \n2. **Grouped Attention** is used for tokens outside this neighborhood window. It reduces the granularity of positional information for distant tokens, which is less critical but still contributes to the overall context understanding.\n\n### Merge Strategy\n\nThe attention values outside the neighborhood window are replaced by those obtained from the grouped attention. This merging strategy ensures a smooth transition and efficient processing of longer sequences while preserving the essential context captured by the normal attention within the neighborhood window.\n\n### Positional Encoding\n\nSine and cosine functions generate positional encodings, ensuring that the model retains an understanding of token order and position.\n\n## Implementation Details\n\n- **Module Class**: `SelfExtendAttn` is implemented as a subclass of `nn.Module` in PyTorch.\n- **Configurability**: Key parameters such as group size and neighbor window size are configurable.\n- **Causal Masking**: Ensures that the attention mechanism respects the autoregressive property of language models.\n\n\n\n# Citation\n```bibtext\n@misc{jin2024llm,\n    title={LLM Maybe LongLM: Self-Extend LLM Context Window Without Tuning}, \n    author={Hongye Jin and Xiaotian Han and Jingfeng Yang and Zhimeng Jiang and Zirui Liu and Chia-Yuan Chang and Huiyuan Chen and Xia Hu},\n    year={2024},\n    eprint={2401.01325},\n    archivePrefix={arXiv},\n    primaryClass={cs.CL}\n}\n```\n\n# License\nMIT","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkyegomez%2Fselfextend","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkyegomez%2Fselfextend","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkyegomez%2Fselfextend/lists"}