chunk_id large_stringlengths 36 36 | doc_type large_stringclasses 2
values | category large_stringclasses 17
values | document large_stringclasses 50
values | version_raw large_stringclasses 590
values | chunk_index int64 0 262 | text large_stringlengths 11 5.69k | file_path large_stringclasses 882
values |
|---|---|---|---|---|---|---|---|
5fddadc5-120c-5424-91e5-9e991ef028cf | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc7 | 8 | For example, the Flashinfer backend calls `append_paged_kv_cache` and then `wrapper.run` to perform the attention operation here.
## The Features of the `TrtllmAttention` Backend[#](#the-features-of-the-trtllmattention-backend "Link to this heading")
The following sections introduce some features of the default `Trtl... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc7.md |
d1fe2633-b22c-5ea6-b837-8ce4f59fb4dc | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc7 | 9 | with a 1D tensor containing the lengths of the different sequences.
### Context and Generation Phases[#](#context-and-generation-phases "Link to this heading")
The `TrtllmAttention` backend encapsulates different implementations for both
context and generation phases into a single custom torch op.
#### Context Phase... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc7.md |
f623c4de-2659-5d09-9c59-05dc9b720146 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc7 | 10 | [FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness](https://arxiv.org/abs/2205.14135)
and
[FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning](https://arxiv.org/abs/2307.08691).
Currently, the implementation triggers extra kernels that apply pre-processing
to the... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc7.md |
8c1a8501-8f9d-5f71-aefe-be284456b282 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc7 | 11 | #### Generation Phase[#](#generation-phase "Link to this heading")
The generation phase is implemented using a single kernel called the masked
multi-head attention in TensorRT LLM. That kernel is able to apply
pre-processing on the Q, K, and V elements on-the-fly: it adds the QKV bias, applies
RoPE, and performs dequa... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc7.md |
c2992d9f-8fec-5563-8ce4-1db146a20dae | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc7 | 12 | However, NVIDIA currently recommends testing that mode when `batch_size * num_heads` is less than the number of multi-processors on the GPU.
This guidance may be subject to change in the future.
Note that even if the multi-block mode is enabled, the attention operator will
not immediately trigger the multi-block versi... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc7.md |
da1ae976-152a-5fe0-bcd8-31b150d503ac | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc7 | 13 | #### XQA Optimization[#](#xqa-optimization "Link to this heading")
XQA optimization is another optimization for MQA/GQA in the generation phase.
It currently only supports a limited number of model configurations, such as the LLAMA2 70B model.
Support matrix of the XQA optimization:
* FP16 / BF16 compute data type.
... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc7.md |
b1cb0e88-69be-55df-9147-4abf17d8205c | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc7 | 14 | ### In-flight Batching[#](#in-flight-batching "Link to this heading")
TensorRT LLM supports in-flight batching of requests (also known as continuous
batching or iteration-level batching) for higher serving throughput. With this feature,
sequences in the context phase can be processed together with sequences in
the gen... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc7.md |
67af93be-c436-56c9-99f7-f7c014814652 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc7 | 15 | the length of the maximum input sequence is inefficient use of resources*.
### Chunked Context[#](#chunked-context "Link to this heading")
In the original state, the common behavior was to process all context tokens at
once. This feature splits the context into several chunks. In this way, the
context chunks can be b... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc7.md |
5f72c830-820d-5e6e-a4b2-c044eb7117da | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc7 | 16 | that technique to accelerate its generation phase. In TensorRT LLM, there is
one KV cache per Transformer layer, which means that there are as many KV
caches as layers in a model. The current version of TensorRT LLM supports two
different types of KV caches: **contiguous** and **paged** KV caches.
#### Contiguous KV C... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc7.md |
987359ed-5efd-57ea-bca4-0c1b6725bbe8 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc7 | 17 | keeps track of the sequences, allocates new blocks from a pool and recycles those
blocks when required. See the implementation of
[`KVCacheManager`](https://github.com/NVIDIA/TensorRT-LLM/tree/a65b0d4/tensorrt_llm/_torch/pyexecutor/resource_manager.py).
#### INT8/FP8 KV Caches[#](#int8-fp8-kv-caches "Link to this head... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc7.md |
c2a3c729-9744-5d99-bbf2-dff0ed29d19c | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc7 | 18 | since it does multiply as `fp_value * (1.0 / kv_cache_scaling_factor)` in plugin.
During generation, the values read from the cache are dequantized on-the-fly in
the MHA/MQA kernel. Dequantization is defined as
`quantized_value * kv_cache_scaling_factor`.
### Sliding Window Attention, Cyclic (Rolling Buffer) KV Cache... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc7.md |
5f2294d0-4a5e-5594-9453-b2dfb26fd342 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc7 | 19 | dealing with very long sequences.
\_Note that the cyclic kv cache feature doesn’t work with beam searching currently as
the context kv cache are shared across beams.
### StreamingLLM[#](#streamingllm "Link to this heading")
The StreamingLLM feature uses a window attention to perform efficient and stable LLM
on long ... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc7.md |
15f940a0-0250-5720-b1a7-3b471e5455f0 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc7 | 20 | When determining the relative distance and adding positional information to tokens,
StreamingLLM use the positions within the cache rather than those in the original text.
`sink_token_length` is also used to enable this feature.
### Beam-Search[#](#beam-search "Link to this heading")
The attention operator supports ... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc7.md |
b13a91ae-1a47-511d-a5d4-c24adcc53bfc | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc7 | 21 | ### Input QKV tensor[#](#input-qkv-tensor "Link to this heading")
The input QKV tensor packs the Q, K and V tensors (concatenated along the last
dimension) after the projection of the hidden states. It is a 3D tensor. RoPE
and quantization to INT8 or FP8 (when needed) are performed by the GPT
attention operator.
In p... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc7.md |
ad5565e6-e0e9-58a9-a0d8-e9dfa687464d | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc7 | 22 | for seq in generation_phase:
num_tokens += seq.beam_width
```
### Rotary Positional Embedding (RoPE)[#](#rotary-positional-embedding-rope "Link to this heading")
The attention operator can perform the computation of the Rotary
Positional Embedding (RoPE). When that operation is enabled,
`rotary_embedding_dim` is ... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc7.md |
8e86bc99-8834-5e29-bb33-10782811817d | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc7 | 23 | ### Cross Attention[#](#cross-attention "Link to this heading")
On top of the MHA as self attention needed by GPT-style decoder-only models, the attention operator also supports cross attention.
This enables the attention operator to be more broadly used as a generic decoder component. For example, the Encoder-Decode... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc7.md |
95788a77-7aa0-523a-9385-db86bc53b48d | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc8 | 0 | # Multi-Head, Multi-Query, and Group-Query Attention#
Source: https://nvidia.github.io/TensorRT-LLM/1.2.0rc8/features/attention.html
Version: 1.2.0rc8
This document details the implementation of multi-head attention (MHA),
multi-query attention (MQA), and group-query attention (GQA) for autoregressive
models in Tenso... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc8.md |
a602495d-76c0-539f-ac9c-1d0324a9bb31 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc8 | 1 | ## Attention Backends[#](#attention-backends "Link to this heading")
There are currently three available attention backends: the vanilla backend, the TRT-LLM backend, and the Flashinfer backend.
You can specify the desired attention backend using `PyTorchConfig.attn_backend`. For instance, to utilize the Flashinfer ba... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc8.md |
d82d1349-5ee9-5826-a721-e584dd619421 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc8 | 2 | 1. **FP8 Quantization**: This feature enables the quantization of inputs and KV cache into FP8 format, significantly reducing memory usage and improving computational throughput.
2. **RoPE Fusion**: By integrating rotary position embedding (RoPE) directly into the attention computation, this feature enhances efficiency... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc8.md |
0445cffb-cdaf-5b87-9786-458d7c08aaf8 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc8 | 3 | ## Implement a New Attention Backend[#](#implement-a-new-attention-backend "Link to this heading")
You can implement a new attention backend to integrate other attention libraries.
An attention backend consists of an `AttentionBackend` class and an `AttentionMetadata` class.
There are three stages in the PyTorch that ... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc8.md |
f74d417d-04ef-5e96-bed3-9bba5828ee31 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc8 | 4 | 3. To prepare parameters of the input and KV cache, call `AttentionMetadata.prepare` to convert from existing metadata and KV cache manager.
3. Single step forward: During the forward pass of each attention layer, call `AttentionBackend.forward` to perform the attention operation. The `AttentionMetadata` will be pro... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc8.md |
472af37f-d762-55dc-bf9c-0e317660561d | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc8 | 5 | | num\_tokens | int | Number of tokens in the batch. |
| num\_ctx\_tokens | int | Number of tokens in sequences in the context phase. |
| kv\_cache\_manager | KVCacheManager | The KV cache manager. |
| is\_cuda\_graph | bool | Whether CUDA graph is enabled. |
| seq\_lens | Tensor | The length of each sequence in the ba... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc8.md |
dedcaf61-7662-5890-9fdf-4edeb881c660 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc8 | 6 | During `AttentionMetadata.__init__`, you can initialize additional fields for the new attention metadata.
For example, the Flashinfer metadata initializes `decode_wrapper` here.
During `AttentionMetadata.prepare`, the runtime will fill all predefined fields, and you can fill your customized fields according to these pr... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc8.md |
a712cb4d-3c13-58b8-8398-75b8222d43b2 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc8 | 7 | | quant\_config | QuantConfig | Optional quantization configuration. If None, no quantization is applied. |
| pos\_embd\_params | PositionalEmbeddingParams | Optional parameters defining how positional embedding should be applied. If None, positional embedding should be applied by the model before calling the backend. ... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc8.md |
a03648f0-bd43-54fa-83f5-7f8705f90227 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc8 | 8 | For example, the Flashinfer backend calls `append_paged_kv_cache` and then `wrapper.run` to perform the attention operation here.
## The Features of the `TrtllmAttention` Backend[#](#the-features-of-the-trtllmattention-backend "Link to this heading")
The following sections introduce some features of the default `Trtl... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc8.md |
b00f790c-61e8-5724-9c94-e8a38c7d0740 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc8 | 9 | with a 1D tensor containing the lengths of the different sequences.
### Context and Generation Phases[#](#context-and-generation-phases "Link to this heading")
The `TrtllmAttention` backend encapsulates different implementations for both
context and generation phases into a single custom torch op.
#### Context Phase... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc8.md |
09e2e9da-3e10-5be6-a28e-ac83d2a3fb46 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc8 | 10 | [FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness](https://arxiv.org/abs/2205.14135)
and
[FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning](https://arxiv.org/abs/2307.08691).
Currently, the implementation triggers extra kernels that apply pre-processing
to the... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc8.md |
89787558-c881-5485-acaf-c76f9519cf8d | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc8 | 11 | #### Generation Phase[#](#generation-phase "Link to this heading")
The generation phase is implemented using a single kernel called the masked
multi-head attention in TensorRT LLM. That kernel is able to apply
pre-processing on the Q, K, and V elements on-the-fly: it adds the QKV bias, applies
RoPE, and performs dequa... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc8.md |
d8d752e2-b5ac-5e47-a99d-ff07751c7761 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc8 | 12 | However, NVIDIA currently recommends testing that mode when `batch_size * num_heads` is less than the number of multi-processors on the GPU.
This guidance may be subject to change in the future.
Note that even if the multi-block mode is enabled, the attention operator will
not immediately trigger the multi-block versi... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc8.md |
e5b3eab4-72a4-5772-868c-c43701775c1b | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc8 | 13 | #### XQA Optimization[#](#xqa-optimization "Link to this heading")
XQA optimization is another optimization for MQA/GQA in the generation phase.
It currently only supports a limited number of model configurations, such as the LLAMA2 70B model.
Support matrix of the XQA optimization:
* FP16 / BF16 compute data type.
... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc8.md |
23d73b56-2c9f-583d-8fbb-20b4815fbced | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc8 | 14 | ### In-flight Batching[#](#in-flight-batching "Link to this heading")
TensorRT LLM supports in-flight batching of requests (also known as continuous
batching or iteration-level batching) for higher serving throughput. With this feature,
sequences in the context phase can be processed together with sequences in
the gen... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc8.md |
4910a4df-732c-50bd-893b-f500714daa0d | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc8 | 15 | the length of the maximum input sequence is inefficient use of resources*.
### Chunked Context[#](#chunked-context "Link to this heading")
In the original state, the common behavior was to process all context tokens at
once. This feature splits the context into several chunks. In this way, the
context chunks can be b... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc8.md |
6b80224f-57d6-5901-97ee-28d8da15e6cf | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc8 | 16 | that technique to accelerate its generation phase. In TensorRT LLM, there is
one KV cache per Transformer layer, which means that there are as many KV
caches as layers in a model. The current version of TensorRT LLM supports two
different types of KV caches: **contiguous** and **paged** KV caches.
#### Contiguous KV C... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc8.md |
62f0987d-76d3-55c3-93ff-4fe77681c00d | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc8 | 17 | keeps track of the sequences, allocates new blocks from a pool and recycles those
blocks when required. See the implementation of
[`KVCacheManager`](https://github.com/NVIDIA/TensorRT-LLM/tree/80649a8/tensorrt_llm/_torch/pyexecutor/resource_manager.py).
#### INT8/FP8 KV Caches[#](#int8-fp8-kv-caches "Link to this head... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc8.md |
a3b060e0-7d36-5569-9b52-29d15cbcda99 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc8 | 18 | since it does multiply as `fp_value * (1.0 / kv_cache_scaling_factor)` in plugin.
During generation, the values read from the cache are dequantized on-the-fly in
the MHA/MQA kernel. Dequantization is defined as
`quantized_value * kv_cache_scaling_factor`.
### Sliding Window Attention, Cyclic (Rolling Buffer) KV Cache... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc8.md |
3747fb27-7d19-5e83-aade-d7913dac7a19 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc8 | 19 | dealing with very long sequences.
\_Note that the cyclic kv cache feature doesn’t work with beam searching currently as
the context kv cache are shared across beams.
### StreamingLLM[#](#streamingllm "Link to this heading")
The StreamingLLM feature uses a window attention to perform efficient and stable LLM
on long ... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc8.md |
94b20fab-863c-5ab1-acba-6a37db1eae1a | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc8 | 20 | When determining the relative distance and adding positional information to tokens,
StreamingLLM use the positions within the cache rather than those in the original text.
`sink_token_length` is also used to enable this feature.
### Beam-Search[#](#beam-search "Link to this heading")
The attention operator supports ... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc8.md |
dd9cf71f-d72b-5215-9e68-2f9e94b84f46 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc8 | 21 | ### Input QKV tensor[#](#input-qkv-tensor "Link to this heading")
The input QKV tensor packs the Q, K and V tensors (concatenated along the last
dimension) after the projection of the hidden states. It is a 3D tensor. RoPE
and quantization to INT8 or FP8 (when needed) are performed by the GPT
attention operator.
In p... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc8.md |
a91e4bf7-4cdb-5818-a17f-b12ef00ac3a5 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc8 | 22 | for seq in generation_phase:
num_tokens += seq.beam_width
```
### Rotary Positional Embedding (RoPE)[#](#rotary-positional-embedding-rope "Link to this heading")
The attention operator can perform the computation of the Rotary
Positional Embedding (RoPE). When that operation is enabled,
`rotary_embedding_dim` is ... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc8.md |
b7cf8b44-0e49-5aa7-b440-49a42bc74e41 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 120rc8 | 23 | ### Cross Attention[#](#cross-attention "Link to this heading")
On top of the MHA as self attention needed by GPT-style decoder-only models, the attention operator also supports cross attention.
This enables the attention operator to be more broadly used as a generic decoder component. For example, the Encoder-Decode... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_120rc8.md |
e58e1c58-e565-5750-ac5a-d57b68628099 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc0 | 0 | # Multi-Head, Multi-Query, and Group-Query Attention#
Source: https://nvidia.github.io/TensorRT-LLM/1.3.0rc0/features/attention.html
Version: 1.3.0rc0
This document details the implementation of multi-head attention (MHA),
multi-query attention (MQA), and group-query attention (GQA) for autoregressive
models in Tenso... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc0.md |
5190eba9-18c8-5132-9277-378c6ef9cd25 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc0 | 1 | ## Attention Backends[#](#attention-backends "Link to this heading")
There are currently three available attention backends: the vanilla backend, the TRT-LLM backend, and the Flashinfer backend.
You can specify the desired attention backend using `PyTorchConfig.attn_backend`. For instance, to utilize the Flashinfer ba... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc0.md |
0303ab5e-9592-577a-81e1-0c99cca9813c | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc0 | 2 | 1. **FP8 Quantization**: This feature enables the quantization of inputs and KV cache into FP8 format, significantly reducing memory usage and improving computational throughput.
2. **RoPE Fusion**: By integrating rotary position embedding (RoPE) directly into the attention computation, this feature enhances efficiency... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc0.md |
d6c3e10e-48d0-578f-a3b5-33e194d0d94a | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc0 | 3 | ## Implement a New Attention Backend[#](#implement-a-new-attention-backend "Link to this heading")
You can implement a new attention backend to integrate other attention libraries.
An attention backend consists of an `AttentionBackend` class and an `AttentionMetadata` class.
There are three stages in the PyTorch that ... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc0.md |
ca1b5ede-75a2-5d82-8dbe-6b7800b98afa | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc0 | 4 | 3. To prepare parameters of the input and KV cache, call `AttentionMetadata.prepare` to convert from existing metadata and KV cache manager.
3. Single step forward: During the forward pass of each attention layer, call `AttentionBackend.forward` to perform the attention operation. The `AttentionMetadata` will be pro... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc0.md |
134b7418-549a-5781-a12a-dea8c3e19b42 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc0 | 5 | | num\_tokens | int | Number of tokens in the batch. |
| num\_ctx\_tokens | int | Number of tokens in sequences in the context phase. |
| kv\_cache\_manager | KVCacheManager | The KV cache manager. |
| is\_cuda\_graph | bool | Whether CUDA graph is enabled. |
| seq\_lens | Tensor | The length of each sequence in the ba... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc0.md |
237671a4-b258-5f50-8bba-4bf10d57836c | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc0 | 6 | During `AttentionMetadata.__init__`, you can initialize additional fields for the new attention metadata.
For example, the Flashinfer metadata initializes `decode_wrapper` here.
During `AttentionMetadata.prepare`, the runtime will fill all predefined fields, and you can fill your customized fields according to these pr... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc0.md |
6be0ea2b-a5bf-5ef6-b74f-e88dfb90e0a2 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc0 | 7 | | quant\_config | QuantConfig | Optional quantization configuration. If None, no quantization is applied. |
| pos\_embd\_params | PositionalEmbeddingParams | Optional parameters defining how positional embedding should be applied. If None, positional embedding should be applied by the model before calling the backend. ... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc0.md |
7636e572-7abf-5379-9bf2-be8b2e74d2fa | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc0 | 8 | For example, the Flashinfer backend calls `append_paged_kv_cache` and then `wrapper.run` to perform the attention operation here.
## The Features of the `TrtllmAttention` Backend[#](#the-features-of-the-trtllmattention-backend "Link to this heading")
The following sections introduce some features of the default `Trtl... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc0.md |
c5d64de2-65d3-5877-8575-00f6aa45d960 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc0 | 9 | with a 1D tensor containing the lengths of the different sequences.
### Context and Generation Phases[#](#context-and-generation-phases "Link to this heading")
The `TrtllmAttention` backend encapsulates different implementations for both
context and generation phases into a single custom torch op.
#### Context Phase... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc0.md |
8fd598fb-4c46-5c6c-a1a7-0df973db1230 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc0 | 10 | [FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness](https://arxiv.org/abs/2205.14135)
and
[FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning](https://arxiv.org/abs/2307.08691).
Currently, the implementation triggers extra kernels that apply pre-processing
to the... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc0.md |
583d154d-94e9-54ac-b7ff-8354cff29d1d | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc0 | 11 | #### Generation Phase[#](#generation-phase "Link to this heading")
The generation phase is implemented using a single kernel called the masked
multi-head attention in TensorRT LLM. That kernel is able to apply
pre-processing on the Q, K, and V elements on-the-fly: it adds the QKV bias, applies
RoPE, and performs dequa... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc0.md |
3f482c86-7705-5d0b-970e-bac9a7adc823 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc0 | 12 | However, NVIDIA currently recommends testing that mode when `batch_size * num_heads` is less than the number of multi-processors on the GPU.
This guidance may be subject to change in the future.
Note that even if the multi-block mode is enabled, the attention operator will
not immediately trigger the multi-block versi... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc0.md |
9be0c8fc-2113-5fd0-87cb-7a32d0dcadd8 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc0 | 13 | #### XQA Optimization[#](#xqa-optimization "Link to this heading")
XQA optimization is another optimization for MQA/GQA in the generation phase.
It currently only supports a limited number of model configurations, such as the LLAMA2 70B model.
Support matrix of the XQA optimization:
* FP16 / BF16 compute data type.
... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc0.md |
8884b47a-8588-547d-bd69-308c38d9c4a7 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc0 | 14 | ### In-flight Batching[#](#in-flight-batching "Link to this heading")
TensorRT LLM supports in-flight batching of requests (also known as continuous
batching or iteration-level batching) for higher serving throughput. With this feature,
sequences in the context phase can be processed together with sequences in
the gen... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc0.md |
797babc2-7d49-5be2-8627-6da3fa96bece | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc0 | 15 | the length of the maximum input sequence is inefficient use of resources*.
### Chunked Context[#](#chunked-context "Link to this heading")
In the original state, the common behavior was to process all context tokens at
once. This feature splits the context into several chunks. In this way, the
context chunks can be b... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc0.md |
06edbd0f-d4ed-5f99-9f41-4f8b8f56a54c | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc0 | 16 | that technique to accelerate its generation phase. In TensorRT LLM, there is
one KV cache per Transformer layer, which means that there are as many KV
caches as layers in a model. The current version of TensorRT LLM supports two
different types of KV caches: **contiguous** and **paged** KV caches.
#### Contiguous KV C... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc0.md |
4e1ded6f-bbb8-5e49-aff8-7b4bbf6aeccc | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc0 | 17 | keeps track of the sequences, allocates new blocks from a pool and recycles those
blocks when required. See the implementation of
[`KVCacheManager`](https://github.com/NVIDIA/TensorRT-LLM/tree/0af1a0e/tensorrt_llm/_torch/pyexecutor/resource_manager.py).
#### INT8/FP8 KV Caches[#](#int8-fp8-kv-caches "Link to this head... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc0.md |
1db1fa01-4a0a-5216-823e-2ed6bf2b5bbb | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc0 | 18 | since it does multiply as `fp_value * (1.0 / kv_cache_scaling_factor)` in plugin.
During generation, the values read from the cache are dequantized on-the-fly in
the MHA/MQA kernel. Dequantization is defined as
`quantized_value * kv_cache_scaling_factor`.
### Sliding Window Attention, Cyclic (Rolling Buffer) KV Cache... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc0.md |
f8ad3914-aeb1-5ac0-9c2d-2e1e29c8776b | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc0 | 19 | dealing with very long sequences.
\_Note that the cyclic kv cache feature doesn’t work with beam searching currently as
the context kv cache are shared across beams.
### StreamingLLM[#](#streamingllm "Link to this heading")
The StreamingLLM feature uses a window attention to perform efficient and stable LLM
on long ... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc0.md |
44afa12f-f643-521a-9549-a75df28037bb | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc0 | 20 | When determining the relative distance and adding positional information to tokens,
StreamingLLM use the positions within the cache rather than those in the original text.
`sink_token_length` is also used to enable this feature.
### Beam-Search[#](#beam-search "Link to this heading")
The attention operator supports ... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc0.md |
eb81b530-489b-5bab-bf6b-5acd42dc7225 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc0 | 21 | ### Input QKV tensor[#](#input-qkv-tensor "Link to this heading")
The input QKV tensor packs the Q, K and V tensors (concatenated along the last
dimension) after the projection of the hidden states. It is a 3D tensor. RoPE
and quantization to INT8 or FP8 (when needed) are performed by the GPT
attention operator.
In p... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc0.md |
994523ed-adf5-5ff8-8ef5-1604b4a26322 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc0 | 22 | for seq in generation_phase:
num_tokens += seq.beam_width
```
### Rotary Positional Embedding (RoPE)[#](#rotary-positional-embedding-rope "Link to this heading")
The attention operator can perform the computation of the Rotary
Positional Embedding (RoPE). When that operation is enabled,
`rotary_embedding_dim` is ... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc0.md |
0bc1fe60-d000-5720-96a8-d50f9bc71e2c | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc0 | 23 | ### Cross Attention[#](#cross-attention "Link to this heading")
On top of the MHA as self attention needed by GPT-style decoder-only models, the attention operator also supports cross attention.
This enables the attention operator to be more broadly used as a generic decoder component. For example, the Encoder-Decode... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc0.md |
4100a16e-f64d-5aa5-986d-a624425cd92e | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc1 | 0 | # Multi-Head, Multi-Query, and Group-Query Attention#
Source: https://nvidia.github.io/TensorRT-LLM/1.3.0rc1/features/attention.html
Version: 1.3.0rc1
This document details the implementation of multi-head attention (MHA),
multi-query attention (MQA), and group-query attention (GQA) for autoregressive
models in Tenso... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc1.md |
fda05511-f553-5e4f-86aa-9c7f0efd155f | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc1 | 1 | ## Attention Backends[#](#attention-backends "Link to this heading")
There are currently three available attention backends: the vanilla backend, the TRT-LLM backend, and the Flashinfer backend.
You can specify the desired attention backend using `PyTorchConfig.attn_backend`. For instance, to utilize the Flashinfer ba... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc1.md |
7669c07c-2c71-55b3-9087-dc0e8199c674 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc1 | 2 | 1. **FP8 Quantization**: This feature enables the quantization of inputs and KV cache into FP8 format, significantly reducing memory usage and improving computational throughput.
2. **RoPE Fusion**: By integrating rotary position embedding (RoPE) directly into the attention computation, this feature enhances efficiency... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc1.md |
cf554200-bbc6-528f-99cb-07de21c8c9c6 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc1 | 3 | ## Implement a New Attention Backend[#](#implement-a-new-attention-backend "Link to this heading")
You can implement a new attention backend to integrate other attention libraries.
An attention backend consists of an `AttentionBackend` class and an `AttentionMetadata` class.
There are three stages in the PyTorch that ... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc1.md |
429c2bee-841f-51b9-952f-20dbd110126b | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc1 | 4 | 3. To prepare parameters of the input and KV cache, call `AttentionMetadata.prepare` to convert from existing metadata and KV cache manager.
3. Single step forward: During the forward pass of each attention layer, call `AttentionBackend.forward` to perform the attention operation. The `AttentionMetadata` will be pro... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc1.md |
f961289d-6d4a-5543-bb70-a465e182cc2f | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc1 | 5 | | num\_tokens | int | Number of tokens in the batch. |
| num\_ctx\_tokens | int | Number of tokens in sequences in the context phase. |
| kv\_cache\_manager | KVCacheManager | The KV cache manager. |
| is\_cuda\_graph | bool | Whether CUDA graph is enabled. |
| seq\_lens | Tensor | The length of each sequence in the ba... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc1.md |
5bb01564-90f5-5e73-8062-f00b374945a4 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc1 | 6 | During `AttentionMetadata.__init__`, you can initialize additional fields for the new attention metadata.
For example, the Flashinfer metadata initializes `decode_wrapper` here.
During `AttentionMetadata.prepare`, the runtime will fill all predefined fields, and you can fill your customized fields according to these pr... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc1.md |
e99fc872-20b9-56c0-a1de-a1fb7b991c11 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc1 | 7 | | quant\_config | QuantConfig | Optional quantization configuration. If None, no quantization is applied. |
| pos\_embd\_params | PositionalEmbeddingParams | Optional parameters defining how positional embedding should be applied. If None, positional embedding should be applied by the model before calling the backend. ... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc1.md |
25ca1351-c50b-55b9-903e-809d97a8553b | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc1 | 8 | For example, the Flashinfer backend calls `append_paged_kv_cache` and then `wrapper.run` to perform the attention operation here.
## The Features of the `TrtllmAttention` Backend[#](#the-features-of-the-trtllmattention-backend "Link to this heading")
The following sections introduce some features of the default `Trtl... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc1.md |
7ef3e5b6-9d9c-5ed0-8623-9fa61c570c85 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc1 | 9 | with a 1D tensor containing the lengths of the different sequences.
### Context and Generation Phases[#](#context-and-generation-phases "Link to this heading")
The `TrtllmAttention` backend encapsulates different implementations for both
context and generation phases into a single custom torch op.
#### Context Phase... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc1.md |
734f47a1-e61d-50b0-9d5e-e3decef9e30e | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc1 | 10 | [FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness](https://arxiv.org/abs/2205.14135)
and
[FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning](https://arxiv.org/abs/2307.08691).
Currently, the implementation triggers extra kernels that apply pre-processing
to the... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc1.md |
841a8b6b-d045-543e-b0e9-388ac084aea4 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc1 | 11 | #### Generation Phase[#](#generation-phase "Link to this heading")
The generation phase is implemented using a single kernel called the masked
multi-head attention in TensorRT LLM. That kernel is able to apply
pre-processing on the Q, K, and V elements on-the-fly: it adds the QKV bias, applies
RoPE, and performs dequa... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc1.md |
b21eb0c4-fc57-5b4a-bced-f4db5bb23e2f | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc1 | 12 | However, NVIDIA currently recommends testing that mode when `batch_size * num_heads` is less than the number of multi-processors on the GPU.
This guidance may be subject to change in the future.
Note that even if the multi-block mode is enabled, the attention operator will
not immediately trigger the multi-block versi... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc1.md |
446cc4a8-cd9a-5d6f-ad04-6f5a7e94fc8a | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc1 | 13 | #### XQA Optimization[#](#xqa-optimization "Link to this heading")
XQA optimization is another optimization for MQA/GQA in the generation phase.
It currently only supports a limited number of model configurations, such as the LLAMA2 70B model.
Support matrix of the XQA optimization:
* FP16 / BF16 compute data type.
... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc1.md |
ddf4e323-7544-58bb-8b7d-394cd777167b | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc1 | 14 | ### In-flight Batching[#](#in-flight-batching "Link to this heading")
TensorRT LLM supports in-flight batching of requests (also known as continuous
batching or iteration-level batching) for higher serving throughput. With this feature,
sequences in the context phase can be processed together with sequences in
the gen... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc1.md |
e166d96e-5e7d-52eb-9298-fe0dd51863ff | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc1 | 15 | the length of the maximum input sequence is inefficient use of resources*.
### Chunked Context[#](#chunked-context "Link to this heading")
In the original state, the common behavior was to process all context tokens at
once. This feature splits the context into several chunks. In this way, the
context chunks can be b... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc1.md |
35b3d3c2-a9ed-5479-bea1-72c2a772db4b | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc1 | 16 | that technique to accelerate its generation phase. In TensorRT LLM, there is
one KV cache per Transformer layer, which means that there are as many KV
caches as layers in a model. The current version of TensorRT LLM supports two
different types of KV caches: **contiguous** and **paged** KV caches.
#### Contiguous KV C... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc1.md |
f5511f6d-0553-57c0-bb94-6bcf74018cc3 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc1 | 17 | keeps track of the sequences, allocates new blocks from a pool and recycles those
blocks when required. See the implementation of
[`KVCacheManager`](https://github.com/NVIDIA/TensorRT-LLM/tree/45d7022/tensorrt_llm/_torch/pyexecutor/resource_manager.py).
#### INT8/FP8 KV Caches[#](#int8-fp8-kv-caches "Link to this head... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc1.md |
ebe9eed2-3341-5673-a711-e4f9fdfc90fa | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc1 | 18 | since it does multiply as `fp_value * (1.0 / kv_cache_scaling_factor)` in plugin.
During generation, the values read from the cache are dequantized on-the-fly in
the MHA/MQA kernel. Dequantization is defined as
`quantized_value * kv_cache_scaling_factor`.
### Sliding Window Attention, Cyclic (Rolling Buffer) KV Cache... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc1.md |
586b8e2f-a862-5753-9925-ee71edb2aa27 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc1 | 19 | dealing with very long sequences.
\_Note that the cyclic kv cache feature doesn’t work with beam searching currently as
the context kv cache are shared across beams.
### StreamingLLM[#](#streamingllm "Link to this heading")
The StreamingLLM feature uses a window attention to perform efficient and stable LLM
on long ... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc1.md |
6c92173e-d8f4-5d52-8555-7a06a3334ea7 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc1 | 20 | When determining the relative distance and adding positional information to tokens,
StreamingLLM use the positions within the cache rather than those in the original text.
`sink_token_length` is also used to enable this feature.
### Beam-Search[#](#beam-search "Link to this heading")
The attention operator supports ... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc1.md |
4d0fa092-5929-5b18-8271-6593d4898dd8 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc1 | 21 | ### Input QKV tensor[#](#input-qkv-tensor "Link to this heading")
The input QKV tensor packs the Q, K and V tensors (concatenated along the last
dimension) after the projection of the hidden states. It is a 3D tensor. RoPE
and quantization to INT8 or FP8 (when needed) are performed by the GPT
attention operator.
In p... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc1.md |
13245051-56fe-5c4b-a6f4-5d40ad907045 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc1 | 22 | for seq in generation_phase:
num_tokens += seq.beam_width
```
### Rotary Positional Embedding (RoPE)[#](#rotary-positional-embedding-rope "Link to this heading")
The attention operator can perform the computation of the Rotary
Positional Embedding (RoPE). When that operation is enabled,
`rotary_embedding_dim` is ... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc1.md |
31c1eefd-88ce-5d18-8271-bac0bbc3cc50 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc1 | 23 | ### Cross Attention[#](#cross-attention "Link to this heading")
On top of the MHA as self attention needed by GPT-style decoder-only models, the attention operator also supports cross attention.
This enables the attention operator to be more broadly used as a generic decoder component. For example, the Encoder-Decode... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc1.md |
f9ebcc0c-6afb-5183-b678-e6de202d08e2 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc2 | 0 | # Multi-Head, Multi-Query, and Group-Query Attention#
Source: https://nvidia.github.io/TensorRT-LLM/1.3.0rc2/features/attention.html
Version: 1.3.0rc2
This document details the implementation of multi-head attention (MHA),
multi-query attention (MQA), and group-query attention (GQA) for autoregressive
models in Tenso... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc2.md |
c9a2c5c7-3164-5664-80c0-c9ee85958adc | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc2 | 1 | ## Attention Backends[#](#attention-backends "Link to this heading")
There are currently three available attention backends: the vanilla backend, the TRT-LLM backend, and the Flashinfer backend.
You can specify the desired attention backend using `PyTorchConfig.attn_backend`. For instance, to utilize the Flashinfer ba... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc2.md |
f239c0fa-7668-578e-ab42-140822c20bda | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc2 | 2 | 1. **FP8 Quantization**: This feature enables the quantization of inputs and KV cache into FP8 format, significantly reducing memory usage and improving computational throughput.
2. **RoPE Fusion**: By integrating rotary position embedding (RoPE) directly into the attention computation, this feature enhances efficiency... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc2.md |
f039ddd9-2613-56fc-9fa4-b6a02050a311 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc2 | 3 | ## Implement a New Attention Backend[#](#implement-a-new-attention-backend "Link to this heading")
You can implement a new attention backend to integrate other attention libraries.
An attention backend consists of an `AttentionBackend` class and an `AttentionMetadata` class.
There are three stages in the PyTorch that ... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc2.md |
14adf1ac-f9be-5e84-a83c-fda5839a911f | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc2 | 4 | 3. To prepare parameters of the input and KV cache, call `AttentionMetadata.prepare` to convert from existing metadata and KV cache manager.
3. Single step forward: During the forward pass of each attention layer, call `AttentionBackend.forward` to perform the attention operation. The `AttentionMetadata` will be pro... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc2.md |
991a6148-9e72-511e-a170-2612df85754e | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc2 | 5 | | num\_tokens | int | Number of tokens in the batch. |
| num\_ctx\_tokens | int | Number of tokens in sequences in the context phase. |
| kv\_cache\_manager | KVCacheManager | The KV cache manager. |
| is\_cuda\_graph | bool | Whether CUDA graph is enabled. |
| seq\_lens | Tensor | The length of each sequence in the ba... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc2.md |
62a2fbd7-55a1-5bf1-bd7c-675adad93b8d | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc2 | 6 | During `AttentionMetadata.__init__`, you can initialize additional fields for the new attention metadata.
For example, the Flashinfer metadata initializes `decode_wrapper` here.
During `AttentionMetadata.prepare`, the runtime will fill all predefined fields, and you can fill your customized fields according to these pr... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc2.md |
8554f1d5-e7dd-5a4f-9e1a-6691eaea61e8 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc2 | 7 | | quant\_config | QuantConfig | Optional quantization configuration. If None, no quantization is applied. |
| pos\_embd\_params | PositionalEmbeddingParams | Optional parameters defining how positional embedding should be applied. If None, positional embedding should be applied by the model before calling the backend. ... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc2.md |
60ccf0f2-dd2e-5042-82c7-a4c8f298ebb2 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc2 | 8 | For example, the Flashinfer backend calls `append_paged_kv_cache` and then `wrapper.run` to perform the attention operation here.
## The Features of the `TrtllmAttention` Backend[#](#the-features-of-the-trtllmattention-backend "Link to this heading")
The following sections introduce some features of the default `Trtl... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc2.md |
bd065877-0d50-5c24-8527-96eb314263a7 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc2 | 9 | with a 1D tensor containing the lengths of the different sequences.
### Context and Generation Phases[#](#context-and-generation-phases "Link to this heading")
The `TrtllmAttention` backend encapsulates different implementations for both
context and generation phases into a single custom torch op.
#### Context Phase... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc2.md |
1ac15a12-e786-5204-beca-76aa07d5c995 | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc2 | 10 | [FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness](https://arxiv.org/abs/2205.14135)
and
[FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning](https://arxiv.org/abs/2307.08691).
Currently, the implementation triggers extra kernels that apply pre-processing
to the... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc2.md |
fac0cdf4-90ff-5da4-b8f0-908cefac947d | docs | nvidia_tensorrt-llm | nvidia_tensorrt-llm_attention | 130rc2 | 11 | #### Generation Phase[#](#generation-phase "Link to this heading")
The generation phase is implemented using a single kernel called the masked
multi-head attention in TensorRT LLM. That kernel is able to apply
pre-processing on the Q, K, and V elements on-the-fly: it adds the QKV bias, applies
RoPE, and performs dequa... | large_dataset/raw/docs/nvidia_tensorrt-llm/attention/attention_130rc2.md |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.