Skip to content

feat(sampling): Add async ring buffer and Spyre-optimized samplers - #1046

Merged
dilipgb merged 5 commits into
torch-spyre:mainfrom
dmholtz:stage1
Sep 9, 2026
Merged

feat(sampling): Add async ring buffer and Spyre-optimized samplers#1046
dilipgb merged 5 commits into
torch-spyre:mainfrom
dmholtz:stage1

Conversation

@dmholtz

@dmholtz dmholtz commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Description

This PR optimizes the Spyre sampling path in three stages to remove the main latency bottlenecks from token generation while preserving correctness.
To ease the review, each stage is a separate commit.

Problem

The default sampling path spends too much time in the critical decoding loop on CPU-heavy work:

  • Exponential noise generation is computed synchronously in the sampling path.
  • Softmax normalization remains expensive for large vocabularies.
  • Tensor-parallel CPU ranks redundantly sample and diverge from one another.

Solution

  1. Stage 1: Async noise pre-sampling

    • Introduce an asynchronous ring buffer that pre-generates exponential noise on a background thread.
    • Replace synchronous exponential_() calls in the critical path with zero-copy buffer access.
    • Add SpyreTopKTopPSampler and SpyreSampler integration for the Spyre path.
  2. Stage 2: Single-rank sampling on TP

    • Only TP rank 0 performs sampling and broadcasts the selected token IDs to the remaining ranks.
    • This removes redundant CPU work and keeps all ranks aligned on the same tokens.
  3. Stage 3: Log-space Gumbel sampling

    • Reformulate sampling as argmax(logits - log_noise) instead of argmax(log(softmax(logits)) + log_noise).
    • This preserves sampling behavior while removing softmax from the critical path.

Key Insight

The three optimizations are complementary: async noise generation removes expensive background work from the hot loop, TP rank gating removes redundant computation and divergence, and log-space Gumbel sampling removes softmax entirely. Together they reduce sampling latency without changing output distribution.
Performance tests show that OTPS increase by >30% for Granite 4.1 while staying in a fixed, thight compute budget.

Related Issues

n/a

Test Plan

  • Unit tests to verify ring-buffer correctness and sampling correctnes
  • Performance tests

Checklist

  • I have read the contributing guidelines
  • My code follows the project's code style (bash format.sh)
  • I have added tests for my changes
  • I have updated the documentation where needed
  • My commits include a Signed-off-by: line (DCO compliance)

@github-actions

Copy link
Copy Markdown

👋 Hi! Thank you for contributing.
Just a reminder: Make sure that your code passes all the linting checks, otherwise your PR won't be able to be merged. To do so, run ./format.sh.
Now you are good to go 🚀.

We also recommend installing prek and configuring it to check your code before every local commit.

@dmholtz
dmholtz marked this pull request as ready for review August 17, 2026 12:21
@dmholtz
dmholtz force-pushed the stage1 branch 2 times, most recently from 3bdd387 to 91e99a1 Compare August 18, 2026 05:54
Comment thread tests/v1/sample/test_spyre_topk_topp_sampler.py
@dilipgb

dilipgb commented Aug 28, 2026

Copy link
Copy Markdown
Collaborator

Otherwise looks good to me. Pele tests passed fine. Maybe we wanted to run e2e tests on card once? @gkumbhat can you please take a look at this once?

@dmholtz

dmholtz commented Aug 28, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for your review @dilipgb, let's run another round of tests to get this PR green

@dilipgb

dilipgb commented Aug 31, 2026

Copy link
Copy Markdown
Collaborator

@joerunde would take look at this PR once?

@tdoublep

Copy link
Copy Markdown
Collaborator

Can we rebase the PR against the new version of vLLM to check there is no interaction?

dmholtz and others added 3 commits September 1, 2026 07:05
Implement asynchronous exponential noise pre-sampling to optimize the
latency-critical sampling path for Spyre hardware.

Signed-off-by: David Holtz <56723830+dmholtz@users.noreply.github.com>
…ll ranks.

Previously, the full sampling path was redundantly executed on every rank and the results of non-zero ranks were discarded. In the context of CPU bound sampling, this wastes CPU resources and can be streamlined by sampling only on TP rank 0, broadcasting results to all ranks to initialize the next step.

Signed-off-by: David Holtz <56723830+dmholtz@users.noreply.github.com>
…rick.

Currently, the softmax runs on CPU critical path, adding few milliseconds of runtime to every decode. This change
rewrites the score computation by transfering the computation to log-space without changing the relative order of tokens.

Co-authored-by: Jan Hofmeier <jan.hofmeier@de.ibm.com>
Signed-off-by: David Holtz <56723830+dmholtz@users.noreply.github.com>
@dmholtz

dmholtz commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author

@tdoublep I rebased this PR to main, it went smooth without any conflicts. Could you please re-run the tests to confirm the behavior with vLLM v0.27.1?

@tdoublep tdoublep left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM - thanks for the awesome work!

@tdoublep

tdoublep commented Sep 1, 2026

Copy link
Copy Markdown
Collaborator

@dilipgb Please take a final look (you requested changes, which is blocking merge). Tests are passing against latests main

@R3hankhan123 R3hankhan123 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

1-2 nits rest it looks good to me

Comment thread sendnn_inference/model_executor/model_loader/spyre.py Outdated
Comment thread sendnn_inference/v1/sample/spyre_topk_topp_sampler.py Outdated
Signed-off-by: dmholtz <56723830+dmholtz@users.noreply.github.com>
Signed-off-by: dmholtz <56723830+dmholtz@users.noreply.github.com>
@dmholtz

dmholtz commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

@R3hankhan123 Thanks for your review, I addressed both NITs.

@R3hankhan123 R3hankhan123 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@dilipgb dilipgb left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@dilipgb
dilipgb merged commit f488d54 into torch-spyre:main Sep 9, 2026
16 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

Development

Successfully merging this pull request may close these issues.

4 participants