Skip to content

[Performance]: Avoid dense Python conversion for sparse TorchSampler logprobs #21980

Open

Description

@Hang-get

Proposal to improve performance

TorchSampler.update_requests converts LogProbsState tensors to nested Python lists after the sampler completion event. Today, if one request asks for logprobs, LogProbsStateList.from_logprobs_state calls .tolist() for every preallocated sequence slot. Mixed batches therefore materialize thousands of unused rows.

Pass the active logprobs sequence slots into the conversion and compact the host tensors first when fewer than half of the slots need logprobs. Dense batches keep the existing direct .tolist() path. The request-to-row mapping preserves the existing result format.

This work is on the serialized host path after sampler_event.synchronize(), before requests can be updated or responses returned.

Report of performance regression

CPU-only benchmark on current main 9d32b9506, using the real LogProbsStateList.from_logprobs_state implementation. Inputs used CPU tensors shaped like the sampler's host buffers with max_topk_logprobs=20. The process was pinned to CPU 0, with 15 warmups, seven timed groups, and the median of 30-100 calls per group.

Preallocated slots Steps Requests needing logprobs Before Proposed Speedup
256 1 1 357.8 us 25.5 us 14.0x
256 1 32 324.9 us 66.1 us 4.9x
2048 1 1 4515.5 us 25.4 us 177.5x
2048 1 64 4496.2 us 109.5 us 41.1x

The 2048-slot size matches the shipped max_batch_size default. A separate equivalence test compares dense and compact conversion for sampled-only, simple-format, and top-k logprobs; all four cases pass.

Misc discussion on performance

The device-to-host copies are unchanged. This proposal removes only the subsequent Python-object materialization for unused slots, so it does not add GPU work or synchronization.

Your current environment

  • OS: Linux 4.18.0 x86_64
  • Python: 3.11.18
  • PyTorch: 2.15.0+cpu
  • TensorRT-LLM: main at 9d32b9506
  • GPU: not used

Before submitting a new issue...

  • I searched open and closed issues and pull requests, including recent sampler and logprobs performance work.

AI assistance (Cursor) was used to investigate and benchmark this proposal.

Activity

  1. added
    General perf
    on Nov 10, 2026
  2. added a commit that references this issue on Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    General perf

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions