fix: compress prompts shorter than iterative_size (#196)#247
Open
ousamabenyounes wants to merge 1 commit intomicrosoft:mainfrom
Open
fix: compress prompts shorter than iterative_size (#196)#247ousamabenyounes wants to merge 1 commit intomicrosoft:mainfrom
ousamabenyounes wants to merge 1 commit intomicrosoft:mainfrom
Conversation
When LLMLingua/LongLLMLingua iteratively compresses a prompt shorter than iterative_size (default 200), get_compressed_input is called with end == prompt_len. The line `need_idx[: end - iterative_size] = 1` then uses a negative index that wraps around from the right, which silently overwrites the tail of need_idx with True — so every token gets kept regardless of the thresholding decision and the achieved compression rate collapses to 1.0x for small prompts. Clamp end to at least iterative_size at the top of get_compressed_input so the two masking writes become a no-op on the left slice and only keep the tail on the right slice, letting the threshold-based need_idx actually take effect. Generated by Claude Code Vibe coded by ousamabenyounes Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes #196
LLMLingua / LongLLMLingua's iterative token-level compression path calls `get_compressed_input(..., end=prompt_len, iterative_size=200)`. When the prompt is shorter than `iterative_size`, `end - iterative_size` goes negative and the line
```python
need_idx[: end - iterative_size] = 1
```
ends up overwriting the tail of `need_idx` from the right (Python negative indexing). Every token ends up marked "keep", the threshold-based decision gets thrown away, and the user sees 1.0x achieved compression on short prompts — the reporter traced this to prompt lengths below ~66 tokens with `iterative_size=200` and even graphed the resulting zig-zag behaviour out to 200 tokens.
Fix
Clamp `end` to at least `iterative_size` at the top of `get_compressed_input`:
```python
if end < iterative_size:
end = iterative_size
```
This turns the two masking writes into:
so the threshold-based `need_idx` actually takes effect. Long prompts (where `end >= iterative_size` already) are untouched. This is the exact fix suggested by the reporter.
Before submitting
Verification
Generated by Claude Code
Vibe coded by ousamabenyounes