Skip to content

inference truncate causes “output_ids” to be incorrectly sliced #1912

Description

@lyy-zz

in inference.py

  1. convert input_ids to list, assign to output_ids
    output_ids = list(input_ids)
  2. truncate input_ids, and get length assign to input_echo_len, but output_ids not truncate, input_echo_len length is not equal to len(output_ids)
    input_ids = input_ids[-max_src_len:]
  3. use input_echo_len slice output_ids causes output contain part of the token in question, like***** ASSISTANT: 。
    tmp_output_ids = output_ids[input_echo_len:]

    Example:

max_new_tokens = 2048

Q:请介绍清华大学
truncate input_ids: an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions. USER: 请介绍清华大学 ASSISTANT:
output after slice: 华大学 ASSISTANT: 清华大学是中国著名的综合性研究型大学之一,位于北京市。该校成立于1911年,最初是清政府建立的留美预备学校,后来发展成为国立清华大学。清华大学是中国最早的高等学府之一,也是中国现代教育的重要奠基者之一。\n\n清华大学在多个学科领域都拥有很高的学术水平和知名度,其中工程、计算机、经济、管理等专业尤为著名。该校拥有一流的教学和研究设施,包括国家实验室、国家工程实验室、国家工程研究中心等。此外,清华大学还拥有多个国家级科研机构,如清华大学国家技术创新研究院、清华大学国家金融研究院等。\n\n清华大学的校园占地面积广阔,环境优美,是中国最美丽的大学校园之一。该校拥有多个校区,其中最著名的是清华大学本部,位于北京市海淀区清华路。清华大学的校园文化氛围浓厚,学生生活丰富多彩,学校也非常重视学生的全面发展和社会责任感的培养。\n\n清华大学是中国高等教育的重要标志之一,也是国际知名的高等学府之一。该校的毕业生在中国乃至世界各地都享有很高的声誉,许多毕业生成为了各个领域的领军人物。</s>

  • Should consideration be given to moving output_ids = list(input_ids) after input_ids = input_ids[-max_src_len:]
  • or drop - 8, What does -8 mean?
    max_src_len = context_len - max_new_tokens - 8

Activity

  1. merrymercy commented on Jul 13, 2023

    @merrymercy
    Member

    Could you contribute a PR?
    I think "-8" was a mistake.

  2. merrymercy commented on Jul 13, 2023

    @merrymercy
    Member

    Please check whether #1935 works for you

  3. lyy-zz commented on Jul 25, 2023

    @lyy-zz
    Author

    Thank you for your reply,is works for me。
    But when context_len=max_new_tokens, -1 still drop the first token, for example: <s>

  4. merrymercy commented on Jul 25, 2023

    @merrymercy
    Member

    @lyy-zz please send a pull request to help us fix it

  5. lyy-zz commented on Aug 28, 2023

    @lyy-zz
    Author

    just remove -1, like

        else:  # truncate
            max_src_len = context_len - max_new_tokens
    

    when context_len=max_new_tokens,max_src_len = 0 not -1, so input_ids = input_ids[-max_src_len:] is input_ids[-0:] not input_ids[1:]
    Consider max_new_tokens > context_len, is input_ids = input_ids[(max_new_token - context_len):] instead of input_ids = input_ids[(max_new_token - context_len + 1):], like 2K model max_new_tokens=2049, exceeds only 1 token, so truncated slice should be input_ids[-(-1):] instead of input_ids[-(-2):]

    Please check if this understanding is correct, thank you

  6. surak commented on Nov 3, 2023

    @surak
    Collaborator

    Hey @lyy-zz is this still an issue?

  7. chessing234 commented on Jun 24, 2026

    @chessing234

    Opened a PR — dropped the stray - 1 in max_src_len so truncation no longer eats an extra prompt token when the context budget is tight.

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