Skip to content

[Bug] ModelInputs.split()中对max_kv_seqlen和sum_kv_seqlen的计算可能存在问题 #4024

Closed

Description

@poorpool

Checklist

  • 1. I have searched related issues but cannot get the expected help.
  • 2. The bug has not been fixed in the latest version.
  • 3. Please note that if the bug-related issue you submitted lacks corresponding environment info and a minimal reproducible demo, it will be challenging for us to reproduce and resolve the issue, reducing the likelihood of receiving feedback.

Describe the bug

阅读LMdeploy最新源码时,我发现Engine.create_model_inputs()中对kv_seqlens字段的计算是将每个sequence的未计算部分(seq_length)和已计算部分(history_lengths)长度相加。

在 ModelInputs.split()中,对max_kv_seqlen的处理是 max_kv_seqlen = self.max_kv_seqlen,然后每步 max_kv_seqlen += max_q_seqlen。这使得ModelInputs需要split时,split出来的第0个inp的max_kv_seqlen即为“本段seq_length+本序列split前的prompt总长度”,而非该字段期望的“本段seq_length+本段开头是第几个词元”。 同时split出来的其他inp的max_kv_seqlen、sum_kv_seqlen也将是错误的。

也许,ModelInputs.split()中的max_kv_seqlen = self.max_kv_seqlen应该改成 max_kv_seqlen = self.history_lengths[0].item()?不知道这是否是一个bug还是我理解出现了偏差,希望跟各位大佬讨论,谢谢>_<

Reproduction

纯读代码,不涉及复现

Environment

最新分支(https://github.com/InternLM/lmdeploy/commit/83976c934ec6ec1fe0ead5822ae0f1edf9cb1579)

Error traceback

Activity

  1. grimoire commented on Oct 4, 2025

    @grimoire
    Collaborator

    感谢汇报
    这确实算是一个 bug。ModelInputs.split 是用来处理超长 prefill 的分块计算的,max_kv_seqlen 的初始值应该是历史长度+当前轮 q_seqlen。不过从 self.history_lengths 可能涉及到一次 host-device 同步,不算最好的解决方案,从原本的 max_kv_seqlen 和 max_q_seqlen 进行换算会比较好。我们会尽快修复,或者如果你有这个意向的话也欢迎提 PR。

    另外这个值目前主要的用处是 attention 的 grid 以及 kv cache 的一些资源分配,超量分配会造成少量的浪费,但是不会对精度和性能造成太多影响。

  2. added 2 commits that reference this issue on Jun 17, 2026
    df2a1b4
    3c61d26
  3. added a commit that references this issue on Jun 18, 2026
    00d5dc8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions