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

[v1] Support allowed_token_ids in v1 Sampler #13210

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

houseroad
Copy link
Contributor

@houseroad houseroad commented Feb 13, 2025

Follow the implementation in vllm/entrypoints/openai/logits_processors.py.

The idea is straightforward, adding a [batch_size x vocab_size] mask tensor, and leverage a list of bools to determine whether to do the inplace masked fill.

  • add test
  • move some verification in _get_allowed_token_ids_logits_processor to SamplingParam.

Test with

  • pytest tests/v1/sample/test_sampler.py
  • pytest tests/v1/worker/test_gpu_input_batch.py

Copy link

👋 Hi! Thank you for contributing to the vLLM project.

💬 Join our developer Slack at https://slack.vllm.ai to discuss your PR in #pr-reviews, coordinate on features in #feat- channels, or join special interest groups in #sig- channels.

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 either: Add ready label to the PR or enable auto-merge.

🚀

@mergify mergify bot added the v1 label Feb 13, 2025
@houseroad houseroad force-pushed the v1_allowed_token_ids branch from 9b044a4 to ee1d1a0 Compare February 14, 2025 22:16
@houseroad houseroad changed the title Support allowed_token_ids in v1 Sampler [v1] Support allowed_token_ids in v1 Sampler Feb 14, 2025
Copy link

mergify bot commented Feb 16, 2025

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

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

@WoosukKwon
Copy link
Collaborator

cc @njhill Could you please take a look?

Copy link
Member

@njhill njhill left a comment

Choose a reason for hiding this comment

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

Thanks @houseroad!

I have a few comments, mainly to keep the tensor manipulation on cpu and apply mask to the batch rather than individual rows.

Also you might want to take a look at #13360, I'm hoping to get that into shape soon and have all the sampling params like this encapsulated as vectorized logits processors.

Comment on lines 63 to 65
logprobs_tensors = (None
if num_logprobs is None else self.gather_logprobs(
raw_logprobs, num_logprobs, token_ids=sampled))
Copy link
Member

Choose a reason for hiding this comment

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

Please revert unrelated formatting changes

Comment on lines 56 to 57
# Apply temperature.
logits = self.apply_temperature(logits, sampling_metadata.temperature)
Copy link
Member

Choose a reason for hiding this comment

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

This shouldn't be here (guessing it was a merge mistake)

Comment on lines 199 to 200
self.allowed_token_ids_mask: torch.Tensor = torch.zeros(
max_num_reqs, self.vocab_size, dtype=torch.bool, device=device)
Copy link
Member

Choose a reason for hiding this comment

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

This could actually get quite large (e.g. 128 MB), we may want to consider a different (pre)allocation strategy.

Also, consistent with the other tensors, we should keep this in pinned CPU memory and only copy to GPU in an async manner when the batch makeup changes (and has_allowed_token_ids is True)

Comment on lines 248 to 249
for i, allowed_token_ids in enumerate(
sampling_metadata.has_allowed_token_ids):
if allowed_token_ids:
logits[i].masked_fill_(
sampling_metadata.allowed_token_ids_mask[i], float("-inf"))
Copy link
Member

Choose a reason for hiding this comment

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

Can we do a single operation here rather than a loop (make has_allowed_token_ids a single bool rather than list).

@pytest.mark.parametrize("device", CUDA_DEVICES)
@pytest.mark.parametrize("batch_size", [1, 2, 32])
@pytest.mark.parametrize("num_allowed_token_ids", [0, 1, 2])
def test_sampler_allowed_token_ids(device: str, batch_size: int,
Copy link
Member

Choose a reason for hiding this comment

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

Thank you for adding this test!!

Copy link

mergify bot commented Feb 19, 2025

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

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 19, 2025
@houseroad houseroad force-pushed the v1_allowed_token_ids branch from 1793e7a to 77c8db6 Compare February 19, 2025 05:39
@mergify mergify bot removed the needs-rebase label Feb 19, 2025
@houseroad houseroad force-pushed the v1_allowed_token_ids branch 2 times, most recently from 14b0933 to 9c0d832 Compare February 19, 2025 06:12
Signed-off-by: Lu Fang <[email protected]>

Complete the main logic for allowed_token_ids in v1 sampler

Signed-off-by: Lu Fang <[email protected]>

lint the code

Signed-off-by: Lu Fang <[email protected]>

Add boundary checks for allowed_token_ids

Signed-off-by: Lu Fang <[email protected]>

address the comments

Signed-off-by: Lu Fang <[email protected]>
@houseroad houseroad force-pushed the v1_allowed_token_ids branch from 9c0d832 to 2bdd559 Compare February 19, 2025 06:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants