Skip to content

Conversation

kashif
Copy link
Contributor

@kashif kashif commented Sep 4, 2025

This pull request introduces improvements to sampling support in continuous batching generation, enhances tensor shape handling for logit processors, and expands test coverage for batch generation with sampling. The main changes are grouped below:

Sampling and Generation Behavior:

  • Enabled sampling in batch_generate by setting do_sample=True and configuring temperature and top_p parameters, allowing for more diverse outputs during batch generation.

Tensor Shape Handling and Sampling Logic:

  • Updated the logit processor in continuous_api.py to handle 3D logits produced by continuous batching, reshaping them as needed for compatibility with logit processors and restoring original shapes after processing.
  • Improved sampling logic in _sample to ensure correct tensor dimensions when sampling tokens, making the method robust for both sampling and greedy decoding.

Paged Attention and Integration:

  • Added a check for the presence of flash_attn_varlen_func in the attention implementation to ensure compatibility and prevent errors during paged attention forward passes.

Testing and Validation:

  • Refactored batch output decoding in tests to use generated_tokens instead of static_outputs for accuracy.
  • Added a new parameterized test test_generate_batch_with_sampling to verify that batch generation with sampling enabled works correctly across different attention implementations and configurations. The test checks for successful completion, non-empty outputs, and actual sampling behavior.

@kashif kashif requested a review from McPatate September 4, 2025 12:59
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Member

@McPatate McPatate left a comment

Choose a reason for hiding this comment

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

lgtm, ty!

Copy link
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

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

thanks! perfect 🤗

@ArthurZucker ArthurZucker merged commit 3f7bda4 into main Sep 8, 2025
24 checks passed
@ArthurZucker ArthurZucker deleted the cp-do-sample branch September 8, 2025 08:30
vijayabhaskar-ev pushed a commit to vijayabhaskar-ev/transformers that referenced this pull request Oct 2, 2025
…ngface#40692)

* fix do_Sample=True in continous batching

* added test

* fix top_p

* test

* Update examples/pytorch/continuous_batching.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants