Repository navigation
Description
I'm working on a model(enc_dec model) where, for a beam size of 1, I got the exact results as on the PyTorch side, but when I use a beam size larger than 1, I encounter some illegal access errors in CUDA memory
I discovered that the issue lies with multi-head attention by using CUDA GDB. Below, you can find a detailed description of the issue. the model was built with a beam size of 5. I'm using the release branch as main branch changed a lot and It will take some time to adopt my model again for it
CUDA Exception: Warp Illegal Address
The exception was triggered at PC 0x7f50bd8dd0c0
Thread 1 "python3" received signal CUDA_EXCEPTION_14, Warp Illegal Address.
[Switching focus to CUDA kernel 0, grid 645, block (0,0,0), thread (256,0,0), device 0, sm 0, warp 10, lane 0]
0x00007f50bd8dd120 in void tensorrt_llm::kernels::mmha::masked_multihead_attention_kernel<unsigned short, unsigned short,
tensorrt_llm::kernels::KVLinearBuffer, 64u, 512u, true, true, false, 8u, 8u, 8u,
4u>(tensorrt_llm::kernels::Multihead_attention_params<unsigned short, true>,
tensorrt_llm::kernels::KVLinearBuffer)<<<(20,2,1),(512,1,1)>>> ()I also received the following messages from the compute sanitizer introduced in CUDA 12.0:
========= Invalid __global__ read of size 16 bytes
========= at 0x99c0 in void tensorrt_llm::kernels::mmha::masked_multihead_attention_kernel<unsigned short, unsigned short, tensorrt_llm::kernels::KVLinearBuffer, (unsigned int)64, (unsigned int)512, (bool)1, (bool)1, (bool)0, (unsigned int)8, (unsigned int)8, (unsigned int)8, (unsigned int)4>(tensorrt_llm::kernels::Multihead_attention_params<T1, T6>, T3)
========= by thread (377,0,0) in block (1,0,0)
========= Address 0x7f4a93c34590 is out of bounds
========= and is 138197616 bytes before the nearest allocation at 0x7f4a9c000000 of size 390255048 bytes
========= Saved host backtrace up to driver entry point at kernel launch time
So based on the following kernel in cuda:
Lines 988 to 1020 in 6837c81
| template < | |
| // The type of the inputs. Supported types: float, uint16_t, nv_bfloat16. | |
| typename T, | |
| // The type of the cache. | |
| typename Tcache, | |
| // Type of struct containing KV cache | |
| typename KVCacheBuffer, | |
| // The hidden dimension per head. | |
| unsigned Dh, | |
| // The number of threads in a threadblock. | |
| unsigned THREADS_PER_BLOCK, | |
| // Whether cross attention is enabled | |
| bool DO_CROSS_ATTENTION, | |
| // Whether has beams. | |
| bool HAS_BEAMS, | |
| // Whether enable multi-block mode for long-sequence-length. | |
| bool DO_MULTI_BLOCK = false, | |
| // The number of threads per key. | |
| unsigned THREADS_PER_KEY = threads_per_key<T, dh_max(Dh)>(), | |
| // The number of threads per value. | |
| unsigned THREADS_PER_VALUE = threads_per_value<T>(dh_max(Dh)), | |
| // The unroll factor for loading from K cache. | |
| unsigned K_LOOP_UNROLL = 8, | |
| // The unroll factor for loading from V cache. | |
| // Set it default to 4 for higher occupancy (by reducing registers usage). | |
| unsigned V_LOOP_UNROLL = 4> | |
| __global__ void masked_multihead_attention_kernel( | |
| Multihead_attention_params<T, DO_CROSS_ATTENTION> params, KVCacheBuffer kvCacheBuffer) | |
| { | |
| using Tk = typename kernel_type_t<T>::Type; | |
| // Use 8bit cache. | |
| static constexpr bool ENABLE_8BITS_CACHE = sizeof(Tcache) == 1; |
we can deduce following information:
- Input Type (T): unsigned short - The inputs to the kernel are of type unsigned short.
- Cache Type (Tcache): unsigned short - The cache within the kernel uses unsigned short for its data.
- KV Cache Buffer Type: tensorrt_llm::kernels::KVLinearBuffer - Specifies the structure used for key-value caching.
- Hidden Dimension per Head (Dh): 64u - Each head in the multi-head attention mechanism works on 64-dimensional vectors.
- Threads Per Block: 512u - Each block in the GPU grid uses 512 threads.
- Cross Attention Flag (DO_CROSS_ATTENTION): true - The kernel performs cross-attention operations.
- Beams Flag (HAS_BEAMS): true - Indicates the handling of multiple beams, often used in sequence generation tasks.
- Multi-Block Mode Flag (DO_MULTI_BLOCK): false - Not using multi-block mode for long sequences.
- Threads Per Key (THREADS_PER_KEY): 8u - Allocates 8 threads for each key in the attention mechanism.
- Threads Per Value (THREADS_PER_VALUE): 8u - Allocates 8 threads for each value.
- K Loop Unroll Factor (K_LOOP_UNROLL): 8u - Unrolls 8 iterations for loading from the K cache.
- V Loop Unroll Factor (V_LOOP_UNROLL): 4u - Unrolls 4 iterations for loading from the V cache.
I can tell that this is happening in my cross-attention layer. Do you have any idea why, with a beam size larger than 1, I'm encountering illegal memory access?
Activity
[-]Efficiently Debugging CUDA Errors[/-][+]Cross Attention layer issue with beam size larger that 1[/+][-]Cross Attention layer issue with beam size larger that 1[/-][+]Cross Attention layer issue with beam size larger than 1[/+]jdemouth-nvidia commentedon Nov 27, 2023
@robosina we are actively working on enc-dec models and the release branch contains a good number of bug fixes and feature updates. I’ll let @symphonylyh talk about beam-search but I believe you’ll have to update to the main branch to get the bug fixes it contains. Sorry for the painful rebase.
robosina commentedon Nov 27, 2023
@jdemouth-nvidia, Thanks for the support. It would be awesome if @symphonylyh could clarify this matter. Meanwhile, I plan to switch to the main branch. I believe the current trt-llm is built in release mode (
-O3). Switching to debug mode might yield a more detailed stack trace for errors, which I'm considering to better understand what's happening inside the kernel. I suspect that if there's a runtime error, it could be due to an issue with kernel execution, possibly involving out-of-bounds access to one of the variables in the forward section. Although I'm not certain, this change might help pinpoint where the problem in the kernel execution lies. Let me know if this approach would be helpful for you. Thanks again.robosina commentedon Nov 29, 2023
@jdemouth-nvidia @symphonylyh I've ported the code to the main branch and noticed that the kernel issue regarding beam search seems resolved. However, I'm encountering a different problem. While the first token generation works as expected, there's a discrepancy between the PyTorch values and TensorRT in the second token generation(I've used
beam size =1). I've set up some layer debugging by defining tensors as outputs, and I've observed the issue reoccurring again in the cross-attention layers. It's worth noting that the first token is generated correctly. Additionally, the kv caching appears almost identical to the PyTorch side. Do you have any idea what might be causing this issue? If not, could you please guide me on what might indirectly affect the cross-attention layers in such a scenario? It's puzzling because the first token's attention is fine (the logits are identical to PyTorch), but the second token generation is incorrect.I apologize if I can't provide something reproducible at the moment, but I assure you that I will offer feedback if I encounter any bugs based on any information you can possibly provide.
I'm passing three tokens at the start of the program, and I'm receiving the following information(these are the output
logitswhich you can see that cross-attention causing a discrepancy ):symphonylyh commentedon Dec 1, 2023
@robosina let's relay our discussion of the step N+1 token issue in #500, and I'll close this issue for now. Before that, I'd like to point out one known issue though: batched ragged input + beam size > 1 is not working at this point. Batched padded or equal length input + beam size > 1 is fine. FYI
trebladev commentedon Aug 30, 2024
@robosina Hi, may I ask you how to use cuda-gdb to debug the gpt_attention_layer? I just cannot make breakpoint in the decoderMaskedMultiheadAttentionTemplate.h(cuda-gdb error is cannot find source file). Can you tell me how to set cuda-gdb? or may other method?
thanks