Skip to content

fix(infer): reject prompts without generation space - #9851

Merged
hjh0119 merged 1 commit into
modelscope:mainfrom
RerankerGuo:fix/reject-overlong-prompts
Aug 4, 2026
Merged

hjh0119 merged 1 commit into
modelscope:mainfrom
RerankerGuo:fix/reject-overlong-prompts

Conversation

@RerankerGuo

Copy link
Copy Markdown
Contributor

PR type

  • Bug Fix
  • New Feature
  • Document Updates
  • More Models or Datasets Support

PR information

Fixes #6441.

Background

When an encoded prompt consumes all or more than the model context,
set_default_max_tokens() currently calculates a zero or negative
generation limit and stores it in the request configuration.

The invalid value then reaches a backend-specific configuration class,
which raises a secondary error such as:

ValueError: `max_new_tokens` must be greater than 0, but is -5923.

This obscures the actual problem: the prompt itself leaves no room for
generation.

Changes

  • Reject requests when the available generation space is zero or
    negative.
  • Report the input token count, model context limit, and configured
    offset in the error.
  • Preserve the existing behavior that caps valid requests to the
    remaining context.
  • Add CPU boundary tests for overlong, exactly-full, and valid prompts.

Verification

  • PYTHONPATH=. .venv/bin/python -m unittest tests.infer.test_infer_engine_limits
  • PYTHONPATH=. .venv/bin/python tests/run.py --pattern test_infer_engine_limits.py
  • .venv/bin/pre-commit run --all-files
  • git diff --check

Impact

Valid inference requests are unchanged. Requests that previously
propagated zero or negative generation limits now fail earlier with an
actionable error. The change applies consistently to inference backends
that use set_default_max_tokens().

Experiment results

Before the fix, prompts of length 5 and 4 with max_model_len=4
produced generation limits of -1 and 0 without a direct error. After
the fix, both raise the input-length error, while a valid prompt still
caps the requested generation length from 4 to 2.

Raise a clear input-length error before zero or negative generation limits reach backend-specific configuration validation.

Fixes #6441

Test: python -m unittest tests.infer.test_infer_engine_limits
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@hjh0119
hjh0119 merged commit 2390f43 into modelscope:main Aug 4, 2026
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants