[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"similar-datamllab--LongLM":3,"tool-datamllab--LongLM":61},[4,18,26,36,44,52],{"id":5,"name":6,"github_repo":7,"description_zh":8,"stars":9,"difficulty_score":10,"last_commit_at":11,"category_tags":12,"status":17},4358,"openclaw","openclaw\u002Fopenclaw","OpenClaw 是一款专为个人打造的本地化 AI 助手，旨在让你在自己的设备上拥有完全可控的智能伙伴。它打破了传统 AI 助手局限于特定网页或应用的束缚，能够直接接入你日常使用的各类通讯渠道，包括微信、WhatsApp、Telegram、Discord、iMessage 等数十种平台。无论你在哪个聊天软件中发送消息，OpenClaw 都能即时响应，甚至支持在 macOS、iOS 和 Android 设备上进行语音交互，并提供实时的画布渲染功能供你操控。\n\n这款工具主要解决了用户对数据隐私、响应速度以及“始终在线”体验的需求。通过将 AI 部署在本地，用户无需依赖云端服务即可享受快速、私密的智能辅助，真正实现了“你的数据，你做主”。其独特的技术亮点在于强大的网关架构，将控制平面与核心助手分离，确保跨平台通信的流畅性与扩展性。\n\nOpenClaw 非常适合希望构建个性化工作流的技术爱好者、开发者，以及注重隐私保护且不愿被单一生态绑定的普通用户。只要具备基础的终端操作能力（支持 macOS、Linux 及 Windows WSL2），即可通过简单的命令行引导完成部署。如果你渴望拥有一个懂你",349277,3,"2026-04-06T06:32:30",[13,14,15,16],"Agent","开发框架","图像","数据工具","ready",{"id":19,"name":20,"github_repo":21,"description_zh":22,"stars":23,"difficulty_score":10,"last_commit_at":24,"category_tags":25,"status":17},3808,"stable-diffusion-webui","AUTOMATIC1111\u002Fstable-diffusion-webui","stable-diffusion-webui 是一个基于 Gradio 构建的网页版操作界面，旨在让用户能够轻松地在本地运行和使用强大的 Stable Diffusion 图像生成模型。它解决了原始模型依赖命令行、操作门槛高且功能分散的痛点，将复杂的 AI 绘图流程整合进一个直观易用的图形化平台。\n\n无论是希望快速上手的普通创作者、需要精细控制画面细节的设计师，还是想要深入探索模型潜力的开发者与研究人员，都能从中获益。其核心亮点在于极高的功能丰富度：不仅支持文生图、图生图、局部重绘（Inpainting）和外绘（Outpainting）等基础模式，还独创了注意力机制调整、提示词矩阵、负向提示词以及“高清修复”等高级功能。此外，它内置了 GFPGAN 和 CodeFormer 等人脸修复工具，支持多种神经网络放大算法，并允许用户通过插件系统无限扩展能力。即使是显存有限的设备，stable-diffusion-webui 也提供了相应的优化选项，让高质量的 AI 艺术创作变得触手可及。",162132,"2026-04-05T11:01:52",[14,15,13],{"id":27,"name":28,"github_repo":29,"description_zh":30,"stars":31,"difficulty_score":32,"last_commit_at":33,"category_tags":34,"status":17},1381,"everything-claude-code","affaan-m\u002Feverything-claude-code","everything-claude-code 是一套专为 AI 编程助手（如 Claude Code、Codex、Cursor 等）打造的高性能优化系统。它不仅仅是一组配置文件，而是一个经过长期实战打磨的完整框架，旨在解决 AI 代理在实际开发中面临的效率低下、记忆丢失、安全隐患及缺乏持续学习能力等核心痛点。\n\n通过引入技能模块化、直觉增强、记忆持久化机制以及内置的安全扫描功能，everything-claude-code 能显著提升 AI 在复杂任务中的表现，帮助开发者构建更稳定、更智能的生产级 AI 代理。其独特的“研究优先”开发理念和针对 Token 消耗的优化策略，使得模型响应更快、成本更低，同时有效防御潜在的攻击向量。\n\n这套工具特别适合软件开发者、AI 研究人员以及希望深度定制 AI 工作流的技术团队使用。无论您是在构建大型代码库，还是需要 AI 协助进行安全审计与自动化测试，everything-claude-code 都能提供强大的底层支持。作为一个曾荣获 Anthropic 黑客大奖的开源项目，它融合了多语言支持与丰富的实战钩子（hooks），让 AI 真正成长为懂上",141543,2,"2026-04-06T11:32:54",[14,13,35],"语言模型",{"id":37,"name":38,"github_repo":39,"description_zh":40,"stars":41,"difficulty_score":32,"last_commit_at":42,"category_tags":43,"status":17},2271,"ComfyUI","Comfy-Org\u002FComfyUI","ComfyUI 是一款功能强大且高度模块化的视觉 AI 引擎，专为设计和执行复杂的 Stable Diffusion 图像生成流程而打造。它摒弃了传统的代码编写模式，采用直观的节点式流程图界面，让用户通过连接不同的功能模块即可构建个性化的生成管线。\n\n这一设计巧妙解决了高级 AI 绘图工作流配置复杂、灵活性不足的痛点。用户无需具备编程背景，也能自由组合模型、调整参数并实时预览效果，轻松实现从基础文生图到多步骤高清修复等各类复杂任务。ComfyUI 拥有极佳的兼容性，不仅支持 Windows、macOS 和 Linux 全平台，还广泛适配 NVIDIA、AMD、Intel 及苹果 Silicon 等多种硬件架构，并率先支持 SDXL、Flux、SD3 等前沿模型。\n\n无论是希望深入探索算法潜力的研究人员和开发者，还是追求极致创作自由度的设计师与资深 AI 绘画爱好者，ComfyUI 都能提供强大的支持。其独特的模块化架构允许社区不断扩展新功能，使其成为当前最灵活、生态最丰富的开源扩散模型工具之一，帮助用户将创意高效转化为现实。",107888,"2026-04-06T11:32:50",[14,15,13],{"id":45,"name":46,"github_repo":47,"description_zh":48,"stars":49,"difficulty_score":10,"last_commit_at":50,"category_tags":51,"status":17},4487,"LLMs-from-scratch","rasbt\u002FLLMs-from-scratch","LLMs-from-scratch 是一个基于 PyTorch 的开源教育项目，旨在引导用户从零开始一步步构建一个类似 ChatGPT 的大型语言模型（LLM）。它不仅是同名技术著作的官方代码库，更提供了一套完整的实践方案，涵盖模型开发、预训练及微调的全过程。\n\n该项目主要解决了大模型领域“黑盒化”的学习痛点。许多开发者虽能调用现成模型，却难以深入理解其内部架构与训练机制。通过亲手编写每一行核心代码，用户能够透彻掌握 Transformer 架构、注意力机制等关键原理，从而真正理解大模型是如何“思考”的。此外，项目还包含了加载大型预训练权重进行微调的代码，帮助用户将理论知识延伸至实际应用。\n\nLLMs-from-scratch 特别适合希望深入底层原理的 AI 开发者、研究人员以及计算机专业的学生。对于不满足于仅使用 API，而是渴望探究模型构建细节的技术人员而言，这是极佳的学习资源。其独特的技术亮点在于“循序渐进”的教学设计：将复杂的系统工程拆解为清晰的步骤，配合详细的图表与示例，让构建一个虽小但功能完备的大模型变得触手可及。无论你是想夯实理论基础，还是为未来研发更大规模的模型做准备",90106,"2026-04-06T11:19:32",[35,15,13,14],{"id":53,"name":54,"github_repo":55,"description_zh":56,"stars":57,"difficulty_score":10,"last_commit_at":58,"category_tags":59,"status":17},4292,"Deep-Live-Cam","hacksider\u002FDeep-Live-Cam","Deep-Live-Cam 是一款专注于实时换脸与视频生成的开源工具，用户仅需一张静态照片，即可通过“一键操作”实现摄像头画面的即时变脸或制作深度伪造视频。它有效解决了传统换脸技术流程繁琐、对硬件配置要求极高以及难以实时预览的痛点，让高质量的数字内容创作变得触手可及。\n\n这款工具不仅适合开发者和技术研究人员探索算法边界，更因其极简的操作逻辑（仅需三步：选脸、选摄像头、启动），广泛适用于普通用户、内容创作者、设计师及直播主播。无论是为了动画角色定制、服装展示模特替换，还是制作趣味短视频和直播互动，Deep-Live-Cam 都能提供流畅的支持。\n\n其核心技术亮点在于强大的实时处理能力，支持口型遮罩（Mouth Mask）以保留使用者原始的嘴部动作，确保表情自然精准；同时具备“人脸映射”功能，可同时对画面中的多个主体应用不同面孔。此外，项目内置了严格的内容安全过滤机制，自动拦截涉及裸露、暴力等不当素材，并倡导用户在获得授权及明确标注的前提下合规使用，体现了技术发展与伦理责任的平衡。",88924,"2026-04-06T03:28:53",[14,15,13,60],"视频",{"id":62,"github_repo":63,"name":64,"description_en":65,"description_zh":66,"ai_summary_zh":66,"readme_en":67,"readme_zh":68,"quickstart_zh":69,"use_case_zh":70,"hero_image_url":71,"owner_login":72,"owner_name":73,"owner_avatar_url":74,"owner_bio":75,"owner_company":76,"owner_location":76,"owner_email":76,"owner_twitter":76,"owner_website":77,"owner_url":78,"languages":79,"stars":84,"forks":85,"last_commit_at":86,"license":87,"difficulty_score":10,"env_os":88,"env_gpu":89,"env_ram":90,"env_deps":91,"category_tags":97,"github_topics":98,"view_count":32,"oss_zip_url":76,"oss_zip_packed_at":76,"status":17,"created_at":105,"updated_at":106,"faqs":107,"releases":137},4584,"datamllab\u002FLongLM","LongLM","[ICML'24 Spotlight] LLM Maybe LongLM: Self-Extend LLM Context Window Without Tuning","LongLM 是一项荣获 ICML 2024 Spotlight 肯定的开源技术，旨在让大型语言模型（LLM）无需重新训练即可突破原有的上下文长度限制。它核心解决了模型在处理超长文本时因位置编码限制而性能下降的难题，通过独特的“自扩展”（Self-Extend）机制，动态调整注意力机制中的位置信息，使模型能够轻松处理数万字甚至更长的文档、代码或对话历史。\n\n这项技术特别适合 AI 研究人员、开发者以及需要处理长文本场景的应用构建者。无论是进行长上下文能力评估的研究团队，还是希望在不增加算力成本的前提下提升现有模型（如 Llama-3、Gemma、Qwen1.5 等）实用性的工程师，都能从中受益。LongLM 的独特亮点在于其“免微调”特性：用户只需调用简单的 API 并设置分组与窗口大小参数，即可即时激活长文本处理能力。此外，项目还集成了 FlashAttention 和 Triton 加速实现，显著提升了推理效率。目前，该技术已在 Google I\u002FO 大会上作为 Gemma 模型长上下文能力的演示案例亮相，证明了其在业界的认可度与实用性。","# LLM Maybe LongLM: Self-Extend LLM Context Window Without Tuning\n\n\nImplementation of the proposed Self-Extend in [LLM Maybe LongLM: Self-Extend LLM Context Window Without Tuning](https:\u002F\u002Farxiv.org\u002Fpdf\u002F2401.01325.pdf). \n\n\n## Updates\n- [05\u002F31\u002F2024]:🎉 SelfExtend was highlighted in a Google I\u002FO session at [YouTube](https:\u002F\u002Fyoutu.be\u002FTV7qCk1dBWA?t=2025) to demonstrate the long-context ability of Gemma!!!\n- [05\u002F01\u002F2024]:🎉 SelfExtend has been accepted by ICML 2024! See you in Vienna!\n- [04\u002F19\u002F2024]:💡 We added the support for LLama-3 with transformers==4.40. To use it with transformers==4.40, you may change the file name of [Llama_4_40.py](.\u002Fself_extend_patch\u002FLlama_4_40.py) to ``Llama.py`` to replace the [existing patch file](.\u002Fself_extend_patch\u002FLlama.py). \n  - (Although, Llama-3's offical model hub recommends using transformers==4.40, we find that with transformers==4.38.2, Llama-3 can also work well)\n\n- [04\u002F06\u002F2024]: We added some hyperparameters searching results with SelfExtend, you may check [here](#selfextend-on-needle-in-a-haystack)\n- [03\u002F24\u002F2024]: We added [Triton](https:\u002F\u002Fgithub.com\u002Fopenai\u002Ftriton) implemented flash self-extend. Now, you can use our [Triton implemented FlashSelfExtend](.\u002Fself_extend_patch\u002Fselfextend_flash_attn_triton.py) to enjoy self-extend!\n- [03\u002F20\u002F2024]: We do many updates:\n  - We added the [FlashAttention](.\u002Fself_extend_patch\u002Fselfextend_flash_attn.py) implementation of self-extend, credits to [Qingquan Song](https:\u002F\u002Fqingquansong.github.io\u002F)! This implementation uses the original flash_attn from Tri Dao. \n  - We also tried to implement [FlashAttention](.\u002Fself_extend_patch\u002Ftriton_self_extend_flash_attn.py) for self-extend using Triton. But currently, **it only works for the prefill stage, and cannot work well for the decoding stage.** We are eagerly debugging this. Any suggestions are very welcome!  \n  - We added the support to Qwen1.5. Check the [codes](.\u002Fself_extend_patch\u002FQwen2.py) for more details.\n  - We reorganized this repo and refactored several files. Now, all codes run with **transformers==4.38.2 and flash_attn==2.5.6**. Legacy codes\u002Fexamples\u002FREADME are packed into [legacy_patch_before_4_38](.\u002Flegacy_patch_before_4_38\u002F). We recommend using our docker: [hoytjin\u002Fselfextend_docker:v0.1](https:\u002F\u002Fhub.docker.com\u002Fr\u002Fhoytjin\u002Fselfextend_docker\u002Ftags) to avoid any environmental issues. \n  - We updated the api, now you can simply call `SelfExtend.apply(loaded model, group size, window size)` to enjoy our self-extend! Check and run the provided [example](.\u002Fexample.py) for more details! \n  - We add a new passkey example with 32k context length and a more challenging 10-digit passkey. \n  - Please join our [Discord](https:\u002F\u002Fdiscord.gg\u002FhGZSeH8h) for discussion! 🔥🔥 \n- [02\u002F22\u002F2024]: We added the [Implementation for Google New LLM Gemma](https:\u002F\u002Fgithub.com\u002Fdatamllab\u002FLongLM\u002Fblob\u002Fmaster\u002Fgemma_self_extend_patch.py)!!! Welcome to try and test it out!!\n- [01\u002F19\u002F2024]: We've added the [implementation for Llama with transformers 4.36.2](https:\u002F\u002Fgithub.com\u002Fdatamllab\u002FLongLM\u002Fblob\u002Fmaster\u002Fllama_self_extend_patch_4_36.py) and the [implementation for Microsoft's official phi-2 with transformers 4.37](https:\u002F\u002Fgithub.com\u002Fdatamllab\u002FLongLM\u002Fblob\u002Fmaster\u002Fphi_self_extend_patch_4_37.py). Another good news: the flash attention version will come in days!💥\n- [01\u002F11\u002F2024]: We've tested the implementation for phi-2. [It works](.\u002Fimg\u002Fphi2_long_bench.jpg). You may find some results on this [Reddit post](https:\u002F\u002Fwww.reddit.com\u002Fr\u002FLocalLLaMA\u002Fcomments\u002F194mmki\u002Fselfextend_works_for_phi2_now_looks_good\u002F?utm_source=share&utm_medium=web2x&context=3) and details on this [X post](https:\u002F\u002Fx.com\u002Fserendip410\u002Fstatus\u002F1745668085711790553?s=20)\n- [01\u002F08\u002F2024]: Add third-party implementations section\n- [01\u002F07\u002F2024]: Add Implementation for Mistral\n- [01\u002F05\u002F2024]: Our proposed method is discussed on this [Reddit post](https:\u002F\u002Fwww.reddit.com\u002Fr\u002FLocalLLaMA\u002Fs\u002FIFOnL7yGNK) \n\n\n\n## Possible issues unrelated to Self-Extend:\n- Gemma-7b has to be loaded in bfloat16. But Gemma-2b still works well with float16.\n- If using transformers 4.36, the default attention used by Llama is `LlamaSpdaAttention` rather than `LlamaSpdaAttention`. Be careful about this and make sure you replace the forward method with the correct class.\n- Mistral's sliding window should be disabled to use Self-Extend. The reason of why we should not use SWA can be found in our paper.  \n\n## Third-party Implementations\n\n**Llama.cpp** [https:\u002F\u002Fgithub.com\u002Fggerganov\u002Fllama.cpp](https:\u002F\u002Fgithub.com\u002Fggerganov\u002Fllama.cpp\u002Fblob\u002F1fc2f265ff9377a37fd2c61eae9cd813a3491bea\u002Fexamples\u002Fmain\u002Fmain.cpp#L552)\n\nLlama.cpp has a great implementation and integration for self-extend! Have a try! 😄\n\n\n## 1. Overview \nThis work elicits LLMs' inherent ability to handle long contexts without fine-tuning. The limited length of the training sequence during training may limit the application of Large Language Models (LLMs) on long input sequences for inference. In this work, we argue that existing LLMs themselves have inherent capabilities for handling long contexts. Based on this argument, we suggest extending LLMs' context window by themselves to fully utilize their inherent ability. We propose Self-Extend to stimulate LLMs' long context handling potential. The basic idea is to construct bi-level attention information: the group level and the neighbor level. The two levels are computed by the original model's self-attention, which means the proposed does not require any training.\n\n\u003Cp align=\"center\">\n\u003Cimg width=\"600\" src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdatamllab_LongLM_readme_6f142def6397.jpg\">\n\n\n## 2. How to Use SelfExtend\n\n### 2.1 Setup\n\nFor current Llama Implementation, the python packages used are:\n```bash\ntransformers==4.38.2\nflash_attn==2.5.6 \n```\n\nWe recommend to use this docker: [hoytjin\u002Fselfextend_docker:v0.1](https:\u002F\u002Fhub.docker.com\u002Fr\u002Fhoytjin\u002Fselfextend_docker\u002Ftags)\n\nWe provided patches for several models before. You may check [legacy_patch_before_4_38](.\u002Flegacy_patch_before_4_38\u002F). It contains legacy patches (llama, mistral, phi..etc) and README.\n\n### Installation\n\nClone the repository to your machine and copy your modeling files into the cloned repo directory.\n\n### 2.2 Run\n```python\nimport SelfExtend\n\n# Load your model, e.g., loaded_model = AutoModelForCausalLM.from_pretrained(model_path) \n\n# group size, neighbor window. \n\nSelfExtend.apply(loaded_model, group_size, window_size, enable_flash_attention=False)\n\n# Inference, e.g., loaded_model.generate(...)\n\n```\nenable_flash_attention=False by default, you may set enable_flash_attention=True, if the model is loaed with FlashAttention enabled. \n\nWe use passkeyretrieval as an example to show how to use self-extend. You may check [example.py](.\u002Fexample.py):\n\n```bash\npython example.py\n\n```\n## 3.How to choose the group_size and neighbor_window\n\nThe following thoughts are based on our experience:\n\n- With Llama-2 as the base model, **2\\~64** are reasonable for group_size; **512\\~1536** are feasible for neighbor_window. But larger group_size and smaller neighbor_window are also good in many cases. \n\n- The general rule of choosing group_size and neighbor_window is: ensure the input sequence lenght is within the maximum extended window size (For llama-2, it would be (4096 - neighbor_window) * group_size + neighbor_window ). \n\n- We didn't choose the group size carefully. For the same sequence, smaller group should be better. But we found this does not strictly hold in some experiments: \n  > Sometimes, a larger group size can be beneficial. This may be due to the fact that larger positions are not well-trained. A larger group size can utilize smaller positions, which have received more training, to facilitate extension. However, smaller group sizes tend to have better precision. Thus, there is a trade-off. For more details, refer to the ablation study section. \u003Cbr>\u003Cbr>For example:\u003Cbr>If the input length for a QA task is 15,800, with a neighbor window set to 1,024, the group size can be set to 5. This is because 5 * (4,096 - 1,024) + 1,024 equals 16,384, which is greater than 15,800. However, setting the group size to 6, or even larger, such as 8 or 16, might improve the model's performance. With a group size of 5, Self-Extend uses positions 1,025 to 3,979 to extend the context window. If the group size is set to 8, Self-Extend uses positions 1,025 to 2,871 for extension. Although a group size of 8 is less precise than a group size of 5, the positions 2,872 to 3,979, utilized by a group size of 5, are less trained during pretraining, which may affect the effectiveness of the extension.\n\n- Maybe, for a sequence of length L, you can try the smallest group size first [calculated by: G * (L- w_n) + w_n] , and then test whether larger group can be better.\n\n#### SelfExtend on 'Needle in a Haystack'\n\u003Cp align=\"center\">\n\u003Cimg width=\"600\" src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdatamllab_LongLM_readme_de5f16773c4c.jpg\">\n\u003Cp align=\"center\">\n\u003Cimg width=\"600\" src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdatamllab_LongLM_readme_27aa58efa603.jpg\">\n\n#### Emperical Rule:\nDenoting the pretraining context window as $L$, the target extension length as $N$, the neighbor window as $W$, and the group size as $G$, the empirical rule for selecting hyperparameters is to ensure that the following inequality holds: $(\\frac{1}{2} \\sim \\frac{2}{3}) \\times L > W + \\frac{N-W}{G}$ This is empirical, we believe it's due the fact that: large relative positions are not well trained. Empirically, only a portion($\\frac{1}{2} \\sim \\frac{2}{3}$) of positions are well trained and SelfExtend should only leverage these well-trained relative position for extension. This finding explains:\nExcessively small group sizes can degrade performance, as they provide precise position information but require SelfExtend to utilize less well-trained relative positions for extension.\nExcessively large neighbor window sizes can also degrade performance, as they provide more neighbor information but necessitate the use of less well-trained relative positions for extension.\nThe experimental results indicate that SelfExtend is not overly sensitive to hyperparameter selection. Predefined, heuristic values for group size and neighbor window size are often sufficient to achieve satisfactory performance.\n\n**[TLDR]**\n SelfExtend is not overly sensitive to hyperparameter selection. One could use a representative task to find proper hyperparameters. Or direcly follow our empirical inequality: $(\\frac{1}{2} \\sim \\frac{2}{3}) \\times L > W + \\frac{N-W}{G}$\n\n\n\n------\n\n\nIf you find our method useful, please kindly cite our paper.\n```bibtex\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\n## 4. Contributing\nWe welcome contributions from the research community to improve the effeicency of SelfExtend. If you have any idea or would like to report a bug, please open an issue or submit a pull request.\n\n## 5. License\nThe code is released under the MIT License.\n\n","# LLM 也许 LongLM：无需微调即可自扩展 LLM 上下文窗口\n\n\n在 [LLM 也许 LongLM：无需微调即可自扩展 LLM 上下文窗口](https:\u002F\u002Farxiv.org\u002Fpdf\u002F2401.01325.pdf) 中提出的 Self-Extend 的实现。\n\n\n## 更新\n- [2024年5月31日]:🎉 SelfExtend 在 Google I\u002FO 演示中被重点介绍，地点为 [YouTube](https:\u002F\u002Fyoutu.be\u002FTV7qCk1dBWA?t=2025)，展示了 Gemma 的长上下文能力！！！\n- [2024年5月1日]:🎉 SelfExtend 已被 ICML 2024 接受！维也纳见！\n- [2024年4月19日]:💡 我们增加了对 LLama-3 和 transformers==4.40 的支持。若要使用 transformers==4.40，可将 [Llama_4_40.py](.\u002Fself_extend_patch\u002FLlama_4_40.py) 文件名改为 ``Llama.py``，以替换 [现有补丁文件](.\u002Fself_extend_patch\u002FLlama.py)。\n  - （尽管 Llama-3 官方模型库推荐使用 transformers==4.40，但我们发现使用 transformers==4.38.2 时，Llama-3 同样可以良好运行）\n\n- [2024年4月6日]: 我们添加了使用 SelfExtend 进行超参数搜索的结果，您可在此查看 [这里](#selfextend-on-needle-in-a-haystack)\n- [2024年3月24日]: 我们添加了 [Triton](https:\u002F\u002Fgithub.com\u002Fopenai\u002Ftriton) 实现的闪速自扩展功能。现在，您可以使用我们 [Triton 实现的 FlashSelfExtend](.\u002Fself_extend_patch\u002Fselfextend_flash_attn_triton.py) 来体验自扩展功能！\n- [2024年3月20日]: 我们进行了多项更新：\n  - 我们添加了 SelfExtend 的 [FlashAttention](.\u002Fself_extend_patch\u002Fselfextend_flash_attn.py) 实现，特别感谢 [Qingquan Song](https:\u002F\u002Fqingquansong.github.io\u002F)！该实现使用了 Tri Dao 提供的原生 flash_attn。\n  - 我们还尝试使用 Triton 为 SelfExtend 实现 [FlashAttention](.\u002Fself_extend_patch\u002Ftriton_self_extend_flash_attn.py)。但目前，**它仅适用于预填充阶段，无法在解码阶段良好工作。** 我们正在积极调试中。欢迎提出任何建议！  \n  - 我们增加了对 Qwen1.5 的支持。更多详情请参阅 [代码](.\u002Fself_extend_patch\u002FQwen2.py)。\n  - 我们重新整理了此仓库，并重构了几处文件。现在，所有代码均在 **transformers==4.38.2 和 flash_attn==2.5.6** 环境下运行。旧版代码\u002F示例\u002FREADME 已打包至 [legacy_patch_before_4_38](.\u002Flegacy_patch_before_4_38\u002F)。我们建议使用我们的 Docker 镜像：[hoytjin\u002Fselfextend_docker:v0.1](https:\u002F\u002Fhub.docker.com\u002Fr\u002Fhoytjin\u002Fselfextend_docker\u002Ftags)，以避免任何环境问题。\n  - 我们更新了 API，现在只需简单调用 `SelfExtend.apply(加载的模型, 组大小, 窗口大小)` 即可享受我们的自扩展功能！请查看并运行提供的 [示例](.\u002Fexample.py) 以获取更多细节！\n  - 我们新增了一个包含 32k 上下文长度的密码示例，以及一个更具挑战性的 10 位密码示例。\n  - 欢迎加入我们的 [Discord](https:\u002F\u002Fdiscord.gg\u002FhGZSeH8h) 讨论群！🔥🔥 \n- [2024年2月22日]: 我们添加了 [Google 新型 LLM Gemma 的实现](https:\u002F\u002Fgithub.com\u002Fdatamllab\u002FLongLM\u002Fblob\u002Fmaster\u002Fgemma_self_extend_patch.py)！！！欢迎大家试用和测试！！\n- [2024年1月19日]: 我们增加了 [基于 transformers 4.36.2 的 Llama 实现](https:\u002F\u002Fgithub.com\u002Fdatamllab\u002FLongLM\u002Fblob\u002Fmaster\u002Fllama_self_extend_patch_4_36.py)以及 [基于 transformers 4.37 的 Microsoft 官方 phi-2 实现](https:\u002F\u002Fgithub.com\u002Fdatamllab\u002FLongLM\u002Fblob\u002Fmaster\u002Fphi_self_extend_patch_4_37.py)。还有一个好消息：闪速注意力版本将在几天内推出！💥\n- [2024年1月11日]: 我们已测试了 phi-2 的实现。[效果很好](.\u002Fimg\u002Fphi2_long_bench.jpg)。您可在 [Reddit 帖子](https:\u002F\u002Fwww.reddit.com\u002Fr\u002FLocalLLaMA\u002Fcomments\u002F194mmki\u002Fselfextend_works_for_phi2_now_looks_good\u002F?utm_source=share&utm_medium=web2x&context=3) 中找到相关结果，详细信息也可见 [X 平台帖子](https:\u002F\u002Fx.com\u002Fserendip410\u002Fstatus\u002F1745668085711790553?s=20)。\n- [2024年1月8日]: 添加第三方实现部分\n- [2024年1月7日]: 添加 Mistral 的实现\n- [2024年1月5日]: 我们的提案方法已在 [Reddit 帖子](https:\u002F\u002Fwww.reddit.com\u002Fr\u002FLocalLLaMA\u002Fs\u002FIFOnL7yGNK) 中讨论。\n\n\n\n## 可能与 Self-Extend 无关的问题：\n- Gemma-7b 必须以 bfloat16 格式加载。而 Gemma-2b 使用 float16 格式仍可正常运行。\n- 如果使用 transformers 4.36，Llama 默认使用的注意力机制是 `LlamaSpdaAttention`，而非 `LlamaSpdaAttention`。请注意这一点，并确保将前向传播方法替换为正确的类。\n- 使用 Self-Extend 时，应禁用 Mistral 的滑动窗口功能。为何不应使用 SWA 的原因可在我们的论文中找到。\n\n## 第三方实现\n\n**Llama.cpp** [https:\u002F\u002Fgithub.com\u002Fggerganov\u002Fllama.cpp](https:\u002F\u002Fgithub.com\u002Fggerganov\u002Fllama.cpp\u002Fblob\u002F1fc2f265ff9377a37fd2c61eae9cd813a3491bea\u002Fexamples\u002Fmain\u002Fmain.cpp#L552)\n\nLlama.cpp 对 Self-Extend 的实现和集成非常出色！不妨一试！😄\n\n\n## 1. 概述\n本研究旨在激发 LLM 在无需微调的情况下处理长上下文的能力。由于训练过程中序列长度受限，大型语言模型 (LLMs) 在推理阶段往往难以应对长输入序列。在本工作中，我们认为现有的 LLM 本身具备处理长上下文的内在能力。基于这一观点，我们提出通过自我扩展 LLM 的上下文窗口来充分发挥其内在潜力。我们提出了 Self-Extend 方法，以激发 LLM 处理长上下文的潜能。其基本思想是构建双层注意力信息：组级和邻域级。这两层均由模型原有的自注意力机制计算得出，因此该方法无需任何训练。\n\n\u003Cp align=\"center\">\n\u003Cimg width=\"600\" src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdatamllab_LongLM_readme_6f142def6397.jpg\">\n\n\n## 2. 如何使用 SelfExtend\n\n### 2.1 设置\n\n对于当前的 Llama 实现，所使用的 Python 包如下：\n```bash\ntransformers==4.38.2\nflash_attn==2.5.6 \n```\n\n我们建议使用此 Docker 镜像：[hoytjin\u002Fselfextend_docker:v0.1](https:\u002F\u002Fhub.docker.com\u002Fr\u002Fhoytjin\u002Fselfextend_docker\u002Ftags)\n\n我们此前已为多个模型提供了补丁。您可查看 [legacy_patch_before_4_38](.\u002Flegacy_patch_before_4_38\u002F)。其中包含旧版补丁（Llama、Mistral、Phi 等）及 README。\n\n### 安装\n\n将仓库克隆到您的机器上，并将您的模型文件复制到克隆后的目录中。\n\n### 2.2 运行\n```python\nimport SelfExtend\n\n# 加载您的模型，例如：loaded_model = AutoModelForCausalLM.from_pretrained(model_path) \n\n# 组大小、邻域窗口。 \n\nSelfExtend.apply(loaded_model, 组大小, 窗口大小, enable_flash_attention=False)\n\n# 推理，例如：loaded_model.generate(...)\n\n```\n默认情况下，`enable_flash_attention=False`。如果模型已启用 FlashAttention，则可将其设置为 `True`。\n\n我们以密码检索为例说明如何使用 SelfExtend。您可查看 [example.py](.\u002Fexample.py)：\n\n```bash\npython example.py\n\n```\n\n## 3. 如何选择 group_size 和 neighbor_window\n\n以下建议基于我们的经验：\n\n- 以 Llama-2 为基模型时，group_size 的合理范围是 **2~64**；neighbor_window 的可行范围是 **512~1536**。不过，在许多情况下，较大的 group_size 配合较小的 neighbor_window 也能取得不错的效果。\n\n- 选择 group_size 和 neighbor_window 的一般原则是：确保输入序列长度不超过最大扩展窗口大小（对于 Llama-2 而言，即 (4096 - neighbor_window) * group_size + neighbor_window）。\n\n- 我们在实验中并未对 group_size 进行细致调优。通常认为，对于同一序列，较小的 group 应该表现更好。然而，在某些实验中我们发现这一规律并不完全成立：\n  > 有时，较大的 group_size 反而更有利。这可能是因为相对位置较大的部分在预训练阶段未得到充分训练，而较大的 group_size 能够利用那些训练更充分的较小位置来辅助上下文窗口的扩展。不过，较小的 group_size 往往具有更高的精度。因此，这里存在一个权衡。更多细节请参阅消融实验部分。\u003Cbr>\u003Cbr>例如：\u003Cbr>若 QA 任务的输入长度为 15,800，且 neighbor window 设置为 1,024，则 group_size 可设为 5，因为 5 * (4,096 - 1,024) + 1,024 = 16,384，大于 15,800。然而，将 group_size 设为 6，甚至更大如 8 或 16，可能会进一步提升模型性能。当 group_size 为 5 时，Self-Extend 会使用位置 1,025 至 3,979 来扩展上下文窗口；而若 group_size 设为 8，则 Self-Extend 仅使用位置 1,025 至 2,871 进行扩展。尽管 group_size 为 8 的精度不如 5，但 group_size 为 5 所使用的 2,872 至 3,979 位置在预训练过程中训练程度较低，这可能会影响扩展效果。\n\n- 对于长度为 L 的序列，可以先尝试最小的 group_size [计算公式为：G * (L - w_n) + w_n]，然后再测试更大的 group 是否能带来更好的效果。\n\n#### SelfExtend 在“大海捞针”任务上的表现\n\u003Cp align=\"center\">\n\u003Cimg width=\"600\" src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdatamllab_LongLM_readme_de5f16773c4c.jpg\">\n\u003Cp align=\"center\">\n\u003Cimg width=\"600\" src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdatamllab_LongLM_readme_27aa58efa603.jpg\">\n\n#### 经验法则：\n设预训练上下文窗口为 $L$，目标扩展长度为 $N$，邻域窗口为 $W$，分组大小为 $G$，则超参数选择的经验法则应满足以下不等式：$(\\frac{1}{2} \\sim \\frac{2}{3}) \\times L > W + \\frac{N-W}{G}$。这一规则是基于经验得出的，我们认为其背后的原因在于：相对位置较大的部分在预训练中并未得到充分训练。因此，只有约一半到三分之二的位置训练得较好，Self-Extend 应当仅利用这些训练较好的相对位置来进行扩展。这一发现也解释了：\n过小的 group_size 会导致性能下降，因为虽然它提供了精确的位置信息，却要求 Self-Extend 使用训练不足的相对位置进行扩展。\n过大的 neighbor window 也会降低性能，因为它虽然提供了更多的邻近信息，但也需要依赖训练较差的相对位置来完成扩展。\n实验结果表明，Self-Extend 对超参数的选择并不敏感。采用预先设定的启发式值作为 group_size 和 neighbor window 的取值，通常即可获得令人满意的效果。\n\n**[简要总结]**\n Self-Extend 对超参数的选择并不敏感。可以通过一个代表性任务来确定合适的超参数，或者直接遵循我们的经验不等式：$(\\frac{1}{2} \\sim \\frac{2}{3}) \\times L > W + \\frac{N-W}{G}$。\n\n\n\n------\n\n\n如果您认为我们的方法有用，请引用我们的论文：\n```bibtex\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\n## 4. 贡献\n我们欢迎研究社区为提升 Self-Extend 的效率做出贡献。如果您有任何想法或想报告 bug，请提交 issue 或 pull request。\n\n## 5. 许可证\n代码采用 MIT 许可证发布。","# LongLM (Self-Extend) 快速上手指南\n\nLongLM (Self-Extend) 是一种无需微调即可扩展大语言模型（LLM）上下文窗口的技术。它通过构建“组级别”和“邻居级别”的双层注意力机制，激发模型固有的长文本处理能力。\n\n## 1. 环境准备\n\n### 系统要求\n- **Python**: 建议 Python 3.8+\n- **GPU**: 支持 CUDA 的 NVIDIA GPU（推荐使用 FlashAttention 加速）\n- **Docker (推荐)**: 为避免环境依赖冲突，强烈建议使用官方提供的 Docker 镜像。\n  - 镜像地址：`hoytjin\u002Fselfextend_docker:v0.1`\n\n### 前置依赖\n当前主要实现基于 **Llama** 系列模型，核心依赖版本如下：\n- `transformers==4.38.2`\n- `flash_attn==2.5.6`\n\n> **注意**：若使用其他版本（如 4.40 支持 Llama-3），需替换对应的补丁文件，详见项目更新日志。\n\n## 2. 安装步骤\n\n### 方案 A：使用 Docker（推荐）\n直接拉取预配置好的环境镜像，避免手动安装复杂的 CUDA 和 FlashAttention 依赖。\n\n```bash\ndocker pull hoytjin\u002Fselfextend_docker:v0.1\ndocker run -it --gpus all -v $(pwd):\u002Fworkspace hoytjin\u002Fselfextend_docker:v0.1\n```\n\n### 方案 B：手动安装\n克隆仓库并安装依赖：\n\n```bash\n# 1. 克隆仓库\ngit clone https:\u002F\u002Fgithub.com\u002Fdatamllab\u002FLongLM.git\ncd LongLM\n\n# 2. 安装核心依赖 (确保 torch 已正确安装且匹配 CUDA 版本)\npip install transformers==4.38.2\npip install flash-attn==2.5.6 --no-build-isolation\n\n# 3. (可选) 国内加速源\n# pip install -i https:\u002F\u002Fpypi.tuna.tsinghua.edu.cn\u002Fsimple transformers==4.38.2\n# pip install -i https:\u002F\u002Fpypi.tuna.tsinghua.edu.cn\u002Fsimple flash-attn==2.5.6 --no-build-isolation\n```\n\n## 3. 基本使用\n\n### 核心代码示例\n只需导入 `SelfExtend` 模块，加载模型后调用 `apply` 方法即可启用长上下文扩展。\n\n```python\nimport SelfExtend\nfrom transformers import AutoModelForCausalLM, AutoTokenizer\n\n# 1. 加载模型 (以 Llama-2 为例)\nmodel_path = \"meta-llama\u002FLlama-2-7b-hf\" # 替换为你的本地模型路径\nloaded_model = AutoModelForCausalLM.from_pretrained(model_path, device_map=\"auto\")\ntokenizer = AutoTokenizer.from_pretrained(model_path)\n\n# 2. 应用 Self-Extend\n# 参数说明:\n# group_size: 组大小 (经验值 2~64)\n# window_size: 邻居窗口大小 (经验值 512~1536)\n# enable_flash_attention: 若模型加载时启用了 FlashAttention 则设为 True\ngroup_size = 8\nwindow_size = 1024\nSelfExtend.apply(loaded_model, group_size, window_size, enable_flash_attention=False)\n\n# 3. 进行长文本推理\ninput_text = \"你的超长输入文本...\" \ninputs = tokenizer(input_text, return_tensors=\"pt\").to(loaded_model.device)\n\n# 生成响应\noutputs = loaded_model.generate(**inputs, max_new_tokens=512)\nprint(tokenizer.decode(outputs[0], skip_special_tokens=True))\n```\n\n### 运行官方示例\n项目提供了基于 Passkey Retrieval 任务的完整示例脚本，可直接运行测试：\n\n```bash\npython example.py\n```\n\n### 参数选择建议\n- **group_size (G)**: 建议范围 `2 ~ 64`。较小的值精度更高，但可能利用到训练不足的位置；较大的值容错性更好。\n- **window_size (W)**: 建议范围 `512 ~ 1536`。\n- **经验法则**: 确保目标长度 $N$ 满足不等式 $(\\frac{1}{2} \\sim \\frac{2}{3}) \\times L > W + \\frac{N-W}{G}$，其中 $L$ 为模型原始上下文长度（如 4096）。通常无需精细搜索，使用启发式默认值即可获得良好效果。","某法律科技团队需要让本地部署的 Llama-3 模型直接分析长达 10 万字的复杂并购合同，以提取关键风险条款，但受限于模型原生的上下文窗口。\n\n### 没有 LongLM 时\n- **被迫截断信息**：由于模型原生支持长度有限，必须将合同强行切割成多个片段处理，导致跨段落的逻辑关联（如“定义”章节与“违约责任”章节的呼应）完全丢失。\n- **微调成本高昂**：若要扩展上下文，需收集大量长文本数据进行全量微调或位置插值训练，耗费数天 GPU 算力且极易破坏模型原有的语言理解能力。\n- **推理速度缓慢**：采用传统的滑动窗口或外部检索增强生成（RAG）方案，不仅增加了系统架构复杂度，还因多次重复调用模型导致响应延迟极高。\n- **关键细节遗漏**：在分割处理中，位于文档中间或边缘的关键数字（如赔偿上限、特定日期）极易被忽略，造成“大海捞针”测试失败。\n\n### 使用 LongLM 后\n- **无损超长上下文**：通过 Self-Extend 技术，无需任何微调即可将 Llama-3 的上下文窗口平滑扩展至 100k+，整份合同可一次性输入，完整保留全局逻辑链条。\n- **零训练成本部署**：仅需调用 `SelfExtend.apply` 接口并设置分组大小参数，几分钟内即可完成配置，省去了昂贵的数据准备和模型重训环节。\n- **高效即时推理**：结合 FlashAttention 优化，即使在处理超长序列时也能保持流畅的解码速度，显著降低了首字延迟和整体耗时。\n- **精准信息定位**：模型在长文本中的“大海捞针”能力大幅提升，能准确捕捉分散在文档各处的 10 位以上密码或隐蔽条款，准确率接近短文本水平。\n\nLongLM 让开发者无需牺牲性能或投入巨额算力，即可低成本赋予现有开源模型原生的超长文本理解能力。","https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdatamllab_LongLM_a86f9856.png","datamllab","Data Analytics Lab at Rice University","https:\u002F\u002Foss.gittoolsai.com\u002Favatars\u002Fdatamllab_e8922862.png","We develop automated and interpretable machine learning algorithms\u002Fsystems with understanding of their theoretical properties.",null,"https:\u002F\u002Fcs.rice.edu\u002F~xh37\u002Findex.html","https:\u002F\u002Fgithub.com\u002Fdatamllab",[80],{"name":81,"color":82,"percentage":83},"Python","#3572A5",100,664,61,"2026-04-02T23:32:31","MIT","Linux","需要支持 FlashAttention 的 NVIDIA GPU（显存需求取决于模型大小及上下文长度，未明确具体数值），需安装 flash_attn","未说明",{"notes":92,"python":90,"dependencies":93},"官方强烈建议使用提供的 Docker 镜像 (hoytjin\u002Fselfextend_docker:v0.1) 以避免环境问题。Gemma-7b 模型必须以 bfloat16 精度加载。Mistral 模型使用此方法时需禁用滑动窗口 (Sliding Window)。支持 Llama-3 (需 transformers 4.40 或 4.38.2)、Qwen1.5、Phi-2 等模型，部分旧版本模型补丁位于 legacy_patch_before_4_38 目录中。",[94,95,96],"transformers==4.38.2","flash_attn==2.5.6","triton (可选，用于加速)",[35,14],[99,100,101,102,103,104],"context-window","large-language-models","llm","longlm","self-extend","selfextend","2026-03-27T02:49:30.150509","2026-04-07T03:51:34.186893",[108,113,118,123,128,133],{"id":109,"question_zh":110,"answer_zh":111,"source_url":112},20846,"为什么在 Transformers 4.36+ 版本中使用 LongLM\u002FSelfExtend 会报错或无法工作？","这是因为 Transformers 4.36 及更高版本默认将注意力模块从 `LlamaAttention` 更改为 `LlamaSdpaAttention`（Gemma 模型同理变为 `GemmaSdpaAttention`），而旧版补丁代码仍尝试修改旧的类名。\n\n解决方案：\n1. 检查你的 Transformers 版本。\n2. 如果是 4.36+ 版本，需要修改 `SelfExtend.py` 中的 `modify_method_of_instance` 调用，将目标类名从 `\"LlamaAttention\"` 替换为 `\"LlamaSdpaAttention\"`（或 `\"GemmaAttention\"` 替换为 `\"GemmaSdpaAttention\"`）。\n3. 参考 Issue #46 中的补丁示例，确保代码适配当前版本的默认注意力机制。","https:\u002F\u002Fgithub.com\u002Fdatamllab\u002FLongLM\u002Fissues\u002F46",{"id":114,"question_zh":115,"answer_zh":116,"source_url":117},20847,"输入序列过长导致显存溢出（OOM）怎么办？","当输入长度增加时，显存消耗迅速增长并导致 OOM 通常是因为未正确启用 Flash Attention 或参数配置不当。\n\n建议步骤：\n1. 确认环境中安装了兼容的 `flash-attn` 库。\n2. 在调用 `SelfExtend.apply` 时，确保根据需求正确设置 `enable_flash_attention` 参数。\n3. 维护者提到他们正在忙于 Flash Attention 的实现和调试，请确保使用最新版本的代码库，因为旧版本可能存在内存优化不足的问题。\n4. 如果问题依旧，尝试调整 `group_size` 和 `window_size` 参数以平衡性能与显存占用。","https:\u002F\u002Fgithub.com\u002Fdatamllab\u002FLongLM\u002Fissues\u002F10",{"id":119,"question_zh":120,"answer_zh":121,"source_url":122},20848,"是否支持 Phi-2 模型？如何使用？","是的，项目支持 Phi-2 模型。\n\n使用方法：\n1. 对于 Transformers 4.37.1 版本，请使用专门的补丁文件 `phi_self_extend_patch_4_37.py`。\n2. 导入该补丁并应用到模型上，类似于 Llama 模型的操作流程。\n3. 注意调整 `group_size` 等参数以适应具体任务需求。如果在测试中遇到输出异常，请检查参数配置（如 `group_size_1` 等）是否与模型架构匹配。","https:\u002F\u002Fgithub.com\u002Fdatamllab\u002FLongLM\u002Fissues\u002F22",{"id":124,"question_zh":125,"answer_zh":126,"source_url":127},20849,"如何在 Transformers 4.36 版本中实现 Flash Attention 支持？","Transformers 4.36 与 4.38.2 的主要区别在于 RoPE 应用于 KV 的方式，但自注意力计算部分几乎相同。\n\n实现方案：\n1. 可以参考 4.38.2 版本的实现逻辑，进行少量修改即可适配 4.36。\n2. 如果遇到 `flash_attn` 版本兼容性问题，可以使用项目提供的 Triton Flash Attention 实现作为替代方案。\n3. 注意：Triton 实现的速度比原生 `flash_attn` 慢约 10%~20%，但能保证功能可用性。","https:\u002F\u002Fgithub.com\u002Fdatamllab\u002FLongLM\u002Fissues\u002F23",{"id":129,"question_zh":130,"answer_zh":131,"source_url":132},20850,"如何对 safetensors 格式的模型（如 Yi-1.5）启用 SelfExtend？","SelfExtend 方法原则上适用于任意采用类似架构的 LLM，包括 safetensors 格式的模型。\n\n针对 Yi 和 Yi-1.5 系列：\n1. Yi 和 Llama2 架构极其相似（主要差异在 GQA 和 RoPE），Yi-1.5 是在 Yi 基础上继续训练得到的。\n2. 你可以直接参考 Llama 的实现代码进行修改。\n3. 历史上 Yi 曾直接使用过 Llama 的实现逻辑，因此只需微调注意力相关的补丁代码即可适配 Yi 系列模型，无需等待官方专门发布针对 safetensors 格式的独立脚本。","https:\u002F\u002Fgithub.com\u002Fdatamllab\u002FLongLM\u002Fissues\u002F41",{"id":134,"question_zh":135,"answer_zh":136,"source_url":112},20851,"Gemma 模型加载失败或报错怎么办？","Gemma 模型加载失败通常是因为注意力模块类名不匹配（见常见问题 1）。\n\n具体修复步骤：\n1. 错误信息通常指向 `SelfExtend.apply` 函数内部。\n2. 打开 `SelfExtend.py` 文件，找到修改注意力机制的部分。\n3. 将代码中硬编码的 `\"LlamaAttention\"` 或 `\"GemmaAttention\"` 替换为当前 Transformers 版本实际使用的类名，通常是 `\"LlamaSdpaAttention\"` 或 `\"GemmaSdpaAttention\"`。\n4. 保存后重新运行脚本即可解决。",[]]