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
d366af69-9e71-516e-86af-ce5d347cc77c
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc3
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_120rc3.md
266bb6b4-b114-50d1-a011-856bd3813c12
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc3
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_120rc3.md
f550d948-f7f3-54e8-9809-1fa085a7ffe2
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc3
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_120rc3.md
0559abe3-48f1-5054-af7d-fa597e52ad14
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc3
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_120rc3.md
66318c7e-eafb-5f66-afc5-43f013208f6f
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc3
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_120rc3.md
338e3589-db6b-5d1b-9270-1216257ab192
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc3
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_120rc3.md
c25f75fc-9889-5707-a9ac-481dd9457832
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc3
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_120rc3.md
24e1e37e-108f-558a-a720-b699f8a30ca9
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc3
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_120rc3.md
fc320c7d-0e2b-5836-8dcf-edfa7d41b192
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc3
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_120rc3.md
102004ac-7613-55c6-97b7-156c62dc2e69
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc3
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_120rc3.md
3efc420d-5d06-54f9-8f62-fa16f6ab9652
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc3
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_120rc3.md
8195d52f-132e-5a73-87a1-d092ea361526
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc3
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_120rc3.md
237e8d97-2f0a-5231-b608-843d45cb9490
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc3
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_120rc3.md
6ce24ae6-d674-5158-a705-7f488504d98b
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc3
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/2128f73/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_120rc3.md
7f5f80bc-18bb-5154-a11a-cde418e66b28
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc3
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_120rc3.md
fec8ec46-ebfb-5b98-b45e-5becea9b121f
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc3
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_120rc3.md
0f6148e9-23d1-5b2b-8a1c-3f8e6be6c9a0
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc3
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_120rc3.md
1e419b0e-3149-5b85-8228-27c13bf89acc
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc3
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_120rc3.md
4e1dddce-f4c6-530f-bef9-4b5a7a65c28f
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc3
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_120rc3.md
d5a01748-b216-5dcc-aaad-053a7745f4e1
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc3
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_120rc3.md
73efeefe-0823-5c0b-bcbf-016c49f103d6
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc4
0
# Multi-Head, Multi-Query, and Group-Query Attention# Source: https://nvidia.github.io/TensorRT-LLM/1.2.0rc4/features/attention.html Version: 1.2.0rc4 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_120rc4.md
b428a2c8-ce77-5800-929f-f6eee5e390cc
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc4
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_120rc4.md
9faf6034-7168-551b-8060-0ad9a24e0241
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc4
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_120rc4.md
a5dc15ee-5c06-5e7b-9041-af212e916a58
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc4
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_120rc4.md
16432fbf-c13f-5954-8096-0377e46ff04f
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc4
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_120rc4.md
70364f6d-29c7-5af3-ac0c-182e8bbadbf2
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc4
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_120rc4.md
b0e4dc08-6c7e-5352-8f4b-10e43100cacf
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc4
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_120rc4.md
9a377cfb-334f-59df-9a38-62e2a263e4cf
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc4
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_120rc4.md
980c83a0-6e81-59d0-bf78-c80cdf961338
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc4
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_120rc4.md
87abc0e2-3e41-57ca-a47d-bf7a26a78aa6
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc4
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_120rc4.md
8c38b5cb-6366-52f6-8fea-668d8ad3816e
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc4
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_120rc4.md
1d00a2a6-a6ca-541a-bca9-b8410e4cf3db
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc4
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_120rc4.md
4f71fa31-b7b6-5e75-81f6-af264cda2211
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc4
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_120rc4.md
5cf6e16a-d257-5fbb-8dd5-2a9ea039750c
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc4
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_120rc4.md
f60d72c3-5062-577f-ac8b-d97220bd685f
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc4
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_120rc4.md
da58bf27-d23d-53fa-ac5d-dcb559446133
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc4
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_120rc4.md
795fb6dd-10a8-5e9a-b44d-c4c25e76618a
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc4
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_120rc4.md
def5119b-1998-518a-ba80-d481babcabdc
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc4
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/a761585/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_120rc4.md
54687cb1-1e19-5d27-8ab5-128a272a7c6f
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc4
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_120rc4.md
7f7728d6-cbbf-57ef-a569-1ebf8ff39ff2
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc4
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_120rc4.md
32d8b35b-fae8-5db9-a963-e636da2fe56c
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc4
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_120rc4.md
61763288-45f8-591e-ad00-154909806af0
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc4
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_120rc4.md
24278a2f-6ff8-599c-abcd-0f5bccae7216
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc4
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_120rc4.md
91132bba-548b-5226-aaab-8db13f660c4e
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc4
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_120rc4.md
45c91edf-c6bd-507e-af52-6a9aded24d33
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc5
0
# Multi-Head, Multi-Query, and Group-Query Attention# Source: https://nvidia.github.io/TensorRT-LLM/1.2.0rc5/features/attention.html Version: 1.2.0rc5 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_120rc5.md
7c481eaa-e0cd-5980-a463-29c3e79dcbe0
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc5
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_120rc5.md
44b1aeea-7c50-5dd7-8e30-0ea77d8c0f02
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc5
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_120rc5.md
32ad637b-e47e-5f54-8c32-c599fa87c3c2
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc5
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_120rc5.md
82f8387e-b45b-54b2-a239-ecdcc54abdde
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc5
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_120rc5.md
62744b92-6b2b-5742-bbd1-9741ec63589a
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc5
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_120rc5.md
bc29c632-b504-5909-bdda-b5ca1db9346f
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc5
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_120rc5.md
e3bd1e96-f523-599b-b01e-fe771976b939
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc5
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_120rc5.md
e309e78e-da52-5263-9c23-63588f5dc3f5
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc5
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_120rc5.md
e2f5e125-fb51-5307-9d89-eec9f53757e3
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc5
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_120rc5.md
d4fe7af6-6817-5b3d-b381-7eeb3866fd38
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc5
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_120rc5.md
b11e953e-7b18-5f88-8b5a-efb6376b45e4
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc5
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_120rc5.md
ddcb6d4c-41a4-5348-9a45-a42fb2a59cc6
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc5
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_120rc5.md
7b4715b4-5f91-5e51-8988-978619589c43
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc5
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_120rc5.md
1a88c1ac-2106-585c-84d0-694173b617d8
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc5
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_120rc5.md
eea87005-be42-5cb2-be55-b7a5beffa310
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc5
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_120rc5.md
c8bf8915-4df4-5e67-8cc0-05f80471981e
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc5
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_120rc5.md
97179be2-5a90-504a-aac2-7c541c3d4975
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc5
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/e4c7078/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_120rc5.md
4663a91a-c87e-5ee6-acf2-df8ae4e645a7
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc5
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_120rc5.md
43eb25c0-a3cf-505e-b8b3-3b602a6ecc97
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc5
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_120rc5.md
5f936c05-5df7-55ab-b39d-c634d6a56d95
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc5
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_120rc5.md
2ad1e959-7eec-57b2-baa3-ca0dfe0abdab
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc5
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_120rc5.md
3afd4f01-1570-5a9c-911c-0a43365311e0
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc5
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_120rc5.md
1d0efa5c-2141-5cb5-a15b-c825e914b7ae
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc5
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_120rc5.md
e3eed5a1-e566-50e9-a593-3715f11cc4a5
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc6
0
# Multi-Head, Multi-Query, and Group-Query Attention# Source: https://nvidia.github.io/TensorRT-LLM/1.2.0rc6/features/attention.html Version: 1.2.0rc6 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_120rc6.md
96136be0-4e7b-5e16-b2b8-ed413525e37c
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc6
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_120rc6.md
13ca7fec-2be5-51b0-8c5b-2a4b3aba4a60
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc6
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_120rc6.md
afb5b486-7a0f-5bab-8e2e-bc2eec1baa2f
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc6
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_120rc6.md
0616ffd4-756e-5eb0-80f2-3fcdc16e3d1e
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc6
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_120rc6.md
e92e22a2-88c6-5a56-9b01-60511d8704e1
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc6
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_120rc6.md
8f15b0c3-ce44-504c-b267-1609f65e1da4
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc6
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_120rc6.md
9b7fc298-2012-5182-b6c3-d8cd569420cd
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc6
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_120rc6.md
66859694-58a2-5e90-95a9-81ad50cc712d
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc6
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_120rc6.md
865bb0e5-3abe-5f30-876d-d6a0bdb229aa
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc6
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_120rc6.md
0c3f04b3-edfa-5af7-b7fb-346f2873aee5
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc6
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_120rc6.md
6d1be344-1535-52c6-bea4-4219a7ce5786
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc6
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_120rc6.md
dcee371e-8135-5ef1-b351-7bfb87fe5b67
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc6
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_120rc6.md
4c7dc030-c80e-5a4d-9524-f3a66d60969f
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc6
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_120rc6.md
92d8fd13-e0ca-5c33-a535-d4ef0335e69e
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc6
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_120rc6.md
3eefe684-c584-5329-a00b-d570009e9af8
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc6
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_120rc6.md
2093e21c-8354-5bd8-bc3b-a79486c2c7e1
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc6
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_120rc6.md
96bbafbf-5249-5bb6-a0fd-58c78551ea7d
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc6
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/9ba1426/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_120rc6.md
f7958cbc-86a5-5971-9dab-ed4affe06a95
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc6
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_120rc6.md
856ba85d-aefc-5f64-a4c6-9d877fe79ed3
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc6
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_120rc6.md
27579021-584e-57b1-afab-688a82f12679
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc6
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_120rc6.md
ab78e515-d21e-5c56-830c-31cf5939b957
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc6
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_120rc6.md
f53f7bcc-254b-50c2-95c9-13b822ce4f04
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc6
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_120rc6.md
1a87496e-a784-55f4-a0dc-a6ec3373914b
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc6
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_120rc6.md
396cbb43-4543-5a0f-bfea-27fbb69ec962
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc7
0
# Multi-Head, Multi-Query, and Group-Query Attention# Source: https://nvidia.github.io/TensorRT-LLM/1.2.0rc7/features/attention.html Version: 1.2.0rc7 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_120rc7.md
375af731-7f04-5cb8-93ac-ec8c7f273ea5
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc7
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_120rc7.md
d91fb2c5-6742-5cd7-98ad-c2e84705bdb3
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc7
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_120rc7.md
fab3a1cd-aff8-54fe-aebf-8ebdef065aaf
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc7
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_120rc7.md
3188c84f-271a-5970-b0f8-97951e803773
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc7
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_120rc7.md
41c49514-c1b9-56e4-a93a-016b6cecfbeb
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc7
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_120rc7.md
1a90eba5-7acf-59a5-b256-e5542d1f5f64
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc7
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_120rc7.md
64e4a230-425b-5088-b971-6cbec4752e03
docs
nvidia_tensorrt-llm
nvidia_tensorrt-llm_attention
120rc7
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_120rc7.md