Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Frontend][Misc] Don't Repeat Yourself (DRY) Sampling #11368

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

AlpinDale
Copy link
Contributor

@AlpinDale AlpinDale commented Dec 20, 2024

FIX #8581

This PR adds support for the DRY sampler, a modern repetition penalty to prevent repetitive outputs at a sequence-level, and performs much better than older penalty methods.

Showcase:

Prompt:

All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work

No DRY output:

 and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.\n\nSomewhere between the repetition and the mundane task of George Orwell’s 1984 and the evocative yet disturbing image of a mess of typewriter keys

dry_multiplier=1.0

... (more)\nIn a remote part of town there once lived a lonely young boy named Danny who wrote the phrase \"All work and no p(l)ay makes Jack a d(u)ll boy.\" on a typewriter for what he would shortly come to realize was the remainder of his life. Danny, who was 8 years old at the time (1984), had not yet learned how to turn off his own typed phrase.\nAs I, a good six years older (1990), like many others who experienced The Shining, witnessed the two year and two month documented torment that befell this isolated young soul. A remote"

Currently, using DRY can incur an overhead of up to ~25%. We can probably optimize this.

Copy link

👋 Hi! Thank you for contributing to the vLLM project.
Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run fastcheck CI which starts running only a small and essential subset of CI tests to quickly catch errors. You can run other CI tests on top of those by going to your fastcheck build on Buildkite UI (linked in the PR checks section) and unblock them. If you do not have permission to unblock, ping simon-mo or khluu to add you in our Buildkite org.

Once the PR is approved and ready to go, your PR reviewer(s) can run CI to test the changes comprehensively before merging.

To run CI, PR reviewers can do one of these:

  • Add ready label to the PR
  • Enable auto-merge.

🚀

@mergify mergify bot added the frontend label Dec 20, 2024
@AlpinDale
Copy link
Contributor Author

/ready

@simon-mo simon-mo added the ready ONLY add when PR is ready to merge/full CI is needed label Dec 27, 2024
Copy link

mergify bot commented Dec 27, 2024

This pull request has merge conflicts that must be resolved before it can be
merged. Please rebase the PR, @AlpinDale.

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@mergify mergify bot added the needs-rebase label Dec 27, 2024
@mergify mergify bot removed the needs-rebase label Dec 27, 2024
"""
Apply Don't Repeat Yourself (DRY) sampling to the logits.

Reference: https://github.com/PygmalionAI/aphrodite-engine/blob/a3c03db7355b33c0dfd670b084e827d0aa7442d1/aphrodite/modeling/layers/sampler.py#L621-L702
Copy link

Choose a reason for hiding this comment

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

Probably makes more sense to link to the original PR (oobabooga/text-generation-webui#5677), as that contains a detailed description and discussion, whereas this is just code similar to the code below.

"""

VOCAB_SIZE = logits.size(-1)
MAX_NGRAM = 100
Copy link

Choose a reason for hiding this comment

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

50 is more than enough. In practice, top logits tend to be on the order of 10^2, and 1.75^50 is already 10^12. This will double performance for pathological inputs.

# Find all instances of the last token- potential ngrams!
endpoint_indexes = torch.nonzero(token_seq == last_token,
as_tuple=True)[0].tolist()
# NOTE(alpin): This seems like the slow part.
Copy link

Choose a reason for hiding this comment

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

Here's an idea: Instead of matching a single token above with nonzero, try to match the last allowed_length tokens. Shorter matches don't need to be penalized by definition, and this should drastically cut down on the number of matches that must be looped over. I'm not sure how to perform subsequence matching in PyTorch, but there should be a way to do this as a vectorized operation.


# Convert ngram lengths to penalty exponents
penalty_mask = ngram_lens > 0
scales = bases[irow]**(ngram_lens[penalty_mask] - min_ngram)
Copy link

Choose a reason for hiding this comment

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

What if ngram_lens[penalty_mask] - min_ngram < 0? Perhaps I'm misunderstanding the implementation, but that case doesn't appear to be accounted for.

Choose a reason for hiding this comment

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

+1 on confirming this.
If I understand correctly, we want something like the following to filer out the cases when ngram_lens[penalty_mask] - min_ngram < 0:

# Apply penalties
penalty_mask = ngram_lens > 0
exponent = ngram_lens[penalty_mask] - min_ngram

# Ensure that when exponent < 0, scales is set to 0
valid_exponent_mask = exponent >= 0
scales = torch.zeros_like(exponent, dtype=torch.float32)
scales[valid_exponent_mask] = bases[irow] ** exponent[valid_exponent_mask]

# Apply penalties
logits[irow][penalty_mask] -= multipliers[irow] * scales

Copy link

mergify bot commented Feb 11, 2025

This pull request has merge conflicts that must be resolved before it can be
merged. Please rebase the PR, @AlpinDale.

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@mergify mergify bot added the needs-rebase label Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
frontend needs-rebase ready ONLY add when PR is ready to merge/full CI is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature]: DRY Sampling
4 participants