[Feature]: Zero-initialize additionalOutput buffers to prevent stale data in streaming mode
🚀 The feature, motivation and pitch
Motivation
We are using TensorRT-LLM for our proprietary model in a text-to-speech (TTS) pipeline. Instead of using generation tokens directly, we retrieve generation hidden_states via the additionalOutput API to feed into our speech synthesis module.
While developing streaming inference, we discovered that the additionalOutput buffers (specifically mAdditionalGenerationOutputTensors) retain data from previous requests.
Problem scenario:
- Run inference Request A (e.g., 100 tokens generated)
- Run inference Request B (e.g., 50 tokens generated)
- Run inference Request C (e.g., 30 tokens generated)
- Issue: Request C's hidden_states buffer still contains leftover data from Request A at positions 30-99
Why this matters for streaming
- In non-streaming mode: token count and hidden_states length are always synchronized, so slicing by token count works fine
- In streaming mode: tokens and hidden_states are filled at different rates, making it impossible to reliably slice by token count
Root Cause
In cpp/include/tensorrt_llm/batch_manager/llmRequest.h, the allocAdditionalOutputs function uses pinnedPool which reuses memory from a MemoryPool without clearing it:
auto tensor = runtime::BufferManager::pinnedPool(shape, dataType);
// No zero-initialization here
outputTensor.second = std::move(tensor);
Feature Request
Add zero-initialization for mAdditionalGenerationOutputTensors in allocAdditionalOutputs:
auto tensor = runtime::BufferManager::pinnedPool(shape, dataType);
std::memset(tensor->data(), 0, tensor->getSizeInBytes()); // Add this line
outputTensor.second = std::move(tensor);
This has minimal performance impact since it runs only once per request and buffer sizes are typically small.
Alternatives
Currently, we manually zero the tensor after each inference on the Python side:
for output in response.additional_outputs:
output.tensor.zero_()
This works but requires users to be aware of this behavior and implement the workaround themselves.
Additional context
This issue affects anyone using additionalOutput API for:
- Streaming inference with hidden_states
- Speech synthesis pipelines
- Any use case where partial buffer reads occur
Environment:
- TensorRT-LLM version: v0.18.0 branch
- Use case: Text-to-Speech with streaming
Thank you for the excellent work on TensorRT-LLM! We hope this feature can help other teams using additional outputs in streaming scenarios.
Before submitting a new issue...
- [x] Make sure you already searched for relevant issues, and checked the documentation and examples for answers to frequently asked questions.
Alternatively, if streaming mode could guarantee that generation hidden_states are synchronized with the token count (i.e., only valid data up to the current token position), this zero-initialization would not be necessary.
However, providing such a guarantee may be fundamentally difficult or may require significant changes to the current implementation.
Hi @laikhtewari @pjs102793 — I would like to investigate and prepare a fix for this if the issue is still desired and unowned.
I checked current main at 890e1089428b53bf2d585e0bee1ae1712e32eac7. The lifecycle is still:
LlmRequest::allocAdditionalOutputsallocates generation tensors as[beam, maxNewTokens, ...]fromBufferManager::pinnedPoolwithout initialization.copyAdditionalOutputswrites only positions produced by the request.LlmRequest::createResultexposes the entire generation additional-output tensor, including its unwritten tail. This differs from streaminggenerationLogits, which is sliced to the current valid range.
Before implementing, could you confirm which API contract is intended?
- Preferred if variable shapes are acceptable: return only the valid generated extent in streaming responses, matching
generationLogits. - If the fixed
[beam, maxNewTokens, ...]shape is ABI-required: zero-initialize the generation buffer once at allocation so the unwritten tail cannot expose pooled data from a prior request.
I would keep the first PR narrow: generation additional outputs only, a pooled-buffer sentinel regression test plus streaming/non-streaming controls, and a representative initialization/slicing cost check. Context outputs, model code, and unrelated Executor APIs would remain unchanged. I would also stop if the fixed-shape semantics already require consumers to use a valid-length field that I have missed.
Is this scope current, and is either contract preferred by the maintainers?
Hi @laikhtewari @pjs102793 — I would like to investigate and prepare a fix for this if the issue is still desired and unowned.
I checked current
mainat890e1089428b53bf2d585e0bee1ae1712e32eac7. The lifecycle is still:
LlmRequest::allocAdditionalOutputsallocates generation tensors as[beam, maxNewTokens, ...]fromBufferManager::pinnedPoolwithout initialization.copyAdditionalOutputswrites only positions produced by the request.LlmRequest::createResultexposes the entire generation additional-output tensor, including its unwritten tail. This differs from streaminggenerationLogits, which is sliced to the current valid range.Before implementing, could you confirm which API contract is intended?
- Preferred if variable shapes are acceptable: return only the valid generated extent in streaming responses, matching
generationLogits.- If the fixed
[beam, maxNewTokens, ...]shape is ABI-required: zero-initialize the generation buffer once at allocation so the unwritten tail cannot expose pooled data from a prior request.I would keep the first PR narrow: generation additional outputs only, a pooled-buffer sentinel regression test plus streaming/non-streaming controls, and a representative initialization/slicing cost check. Context outputs, model code, and unrelated Executor APIs would remain unchanged. I would also stop if the fixed-shape semantics already require consumers to use a valid-length field that I have missed.
Is this scope current, and is either contract preferred by the maintainers?
@HOYALIM
As mentioned in https://github.com/NVIDIA/TensorRT-LLM/issues/6706#issuecomment-4890467902, the TensorRT backend is being removed.
The v1.3.0rc20 release note says that it is the last RC to support the TensorRT backend, and that the backend will be removed in the next version. Since this issue only concerns the TensorRT path, I would guess that it will end up being closed at some point.
On our side, we have mostly finished migrating to the PyTorch backend, and we no longer see this problem there. It appends the additional outputs per iteration instead of pre-allocating a fixed-size pinned buffer, so no stale data is left over. The zero_() workaround is no longer needed for us.