Skip to content

Fix double-subtraction of pos_ in TextLLMRunner::generate() (#18727)#18727

Merged
meta-codesync[bot] merged 1 commit intomainfrom
export-D99742232
Apr 7, 2026
Merged

Fix double-subtraction of pos_ in TextLLMRunner::generate() (#18727)#18727
meta-codesync[bot] merged 1 commit intomainfrom
export-D99742232

Conversation

@kirklandsign
Copy link
Copy Markdown
Contributor

@kirklandsign kirklandsign commented Apr 6, 2026

Summary:

When seq_len is set and pos_ > 0 (multi-turn conversations),
max_context_len was pre-adjusted by subtracting pos_, but
resolve_max_new_tokens then only subtracted num_prompt_tokens
instead of the full occupied position count. This caused
min(seq_len, max_context_len) to use a too-large max_context_len,
producing more tokens than allowed by seq_len.

Fix: use raw metadata value for max_context_len and pass pos_
(which includes prompt tokens after prefill) to
resolve_max_new_tokens, matching multimodal_runner's behavior.

Differential Revision: D99742232

@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented Apr 6, 2026

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/18727

Note: Links to docs will display an error until the docs builds have been completed.

❌ 3 New Failures, 3 Pending, 3 Unrelated Failures

As of commit 2332004 with merge base 19f7ff2 (image):

NEW FAILURES - The following jobs have failed:

FLAKY - The following jobs failed but were likely due to flakiness present on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

Copilot AI review requested due to automatic review settings April 6, 2026 23:26
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 6, 2026
@meta-codesync
Copy link
Copy Markdown
Contributor

meta-codesync Bot commented Apr 6, 2026

@kirklandsign has exported this pull request. If you are a Meta employee, you can view the originating Diff in D99742232.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 6, 2026

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes token budget resolution in TextLLMRunner::generate() for multi-turn conversations when seq_len is set, aligning behavior with multimodal_runner so occupied KV-cache positions are correctly accounted for.

Changes:

  • Stop pre-adjusting max_context_len by pos_; use the raw metadata value instead.
  • Resolve max_new_tokens using the full occupied position count (pos_ after prefill), and tighten the max-context prefill guard accordingly.
  • Add a regression test covering the multi-turn + seq_len case.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
extension/llm/runner/text_llm_runner.cpp Corrects max token resolution by using raw max_context_len and passing occupied positions (pos_) into resolve_max_new_tokens().
extension/llm/runner/test/test_text_llm_runner.cpp Adds regression coverage to ensure seq_len limits respect prior-turn pos_ occupancy.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +171 to 175
// Resolve max_new_tokens. pos_ now reflects all occupied positions
// (including prompt tokens just prefilled).
int max_new_tokens =
config.resolve_max_new_tokens(max_context_len, num_prompt_tokens);
config.resolve_max_new_tokens(max_context_len, pos_);

Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GenerationConfig::resolve_max_new_tokens() takes int32_t parameters documented as num_prompt_tokens, but this call passes pos_ (an int64_t occupied-position count). This relies on implicit narrowing conversions and on a broader interpretation of the parameter than the API/docstring (and the pybinding arg name) suggests. Consider updating resolve_max_new_tokens to accept an int64_t occupied token count (or adding a new helper with clearer naming) and adjusting the documentation/bindings to avoid truncation risk and confusion.

Copilot uses AI. Check for mistakes.
@meta-codesync meta-codesync Bot changed the title Fix double-subtraction of pos_ in TextLLMRunner::generate() Fix double-subtraction of pos_ in TextLLMRunner::generate() (#18727) Apr 7, 2026
meta-codesync Bot pushed a commit that referenced this pull request Apr 7, 2026
Summary:

When seq_len is set and pos_ > 0 (multi-turn conversations),
max_context_len was pre-adjusted by subtracting pos_, but
resolve_max_new_tokens then only subtracted num_prompt_tokens
instead of the full occupied position count. This caused
min(seq_len, max_context_len) to use a too-large max_context_len,
producing more tokens than allowed by seq_len.

Fix: use raw metadata value for max_context_len and pass pos_
(which includes prompt tokens after prefill) to
resolve_max_new_tokens, matching multimodal_runner's behavior.

Differential Revision: D99742232
@meta-codesync meta-codesync Bot force-pushed the export-D99742232 branch from 6b8cca8 to 6deda58 Compare April 7, 2026 06:15
meta-codesync Bot pushed a commit that referenced this pull request Apr 7, 2026
Summary:

When seq_len is set and pos_ > 0 (multi-turn conversations),
max_context_len was pre-adjusted by subtracting pos_, but
resolve_max_new_tokens then only subtracted num_prompt_tokens
instead of the full occupied position count. This caused
min(seq_len, max_context_len) to use a too-large max_context_len,
producing more tokens than allowed by seq_len.

Fix: use raw metadata value for max_context_len and pass pos_
(which includes prompt tokens after prefill) to
resolve_max_new_tokens, matching multimodal_runner's behavior.

Differential Revision: D99742232
Copilot AI review requested due to automatic review settings April 7, 2026 18:15
@meta-codesync meta-codesync Bot force-pushed the export-D99742232 branch from 6deda58 to 009b11d Compare April 7, 2026 18:15
@kirklandsign kirklandsign review requested due to automatic review settings April 7, 2026 18:15
meta-codesync Bot pushed a commit that referenced this pull request Apr 7, 2026
Summary:

When seq_len is set and pos_ > 0 (multi-turn conversations),
max_context_len was pre-adjusted by subtracting pos_, but
resolve_max_new_tokens then only subtracted num_prompt_tokens
instead of the full occupied position count. This caused
min(seq_len, max_context_len) to use a too-large max_context_len,
producing more tokens than allowed by seq_len.

Fix: use raw metadata value for max_context_len and pass pos_
(which includes prompt tokens after prefill) to
resolve_max_new_tokens, matching multimodal_runner's behavior.

Differential Revision: D99742232
@meta-codesync meta-codesync Bot force-pushed the export-D99742232 branch from 009b11d to 784d607 Compare April 7, 2026 18:16
Summary:
Pull Request resolved: #18727

When seq_len is set and pos_ > 0 (multi-turn conversations),
max_context_len was pre-adjusted by subtracting pos_, but
resolve_max_new_tokens then only subtracted num_prompt_tokens
instead of the full occupied position count. This caused
min(seq_len, max_context_len) to use a too-large max_context_len,
producing more tokens than allowed by seq_len.

Fix: use raw metadata value for max_context_len and pass pos_
(which includes prompt tokens after prefill) to
resolve_max_new_tokens, matching multimodal_runner's behavior.

Differential Revision: D99742232
Copy link
Copy Markdown
Contributor

@larryliu0820 larryliu0820 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review automatically exported from Phabricator review in Meta.

@meta-codesync meta-codesync Bot merged commit 5ba654f into main Apr 7, 2026
161 of 170 checks passed
@meta-codesync meta-codesync Bot deleted the export-D99742232 branch April 7, 2026 20:15
jpiat pushed a commit to jpiat/executorch that referenced this pull request Apr 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported meta-exported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants