Skip to content

[FEAT](kernels): implement Prefix-Shared Fused Attention for GRPO logprob computation - #71

Merged
Flink-ddd merged 3 commits into
mainfrom
feat/prefix-shared-attention
Jun 3, 2026
Merged

Flink-ddd merged 3 commits into
mainfrom
feat/prefix-shared-attention

Conversation

@Flink-ddd

Copy link
Copy Markdown
Collaborator

Fixes #65

Purpose

In GRPO and PPO training pipelines, multiple generated responses (Group Size, $G$) share the exact same prompt prefix.
When using standard SDPA backends (like PyTorch Native FlashAttention), the KV cache for the shared prompt must be expanded and repeated across the $G$ dimension. This results in severe Memory-Bound bottlenecks and massive VRAM bloat (computing the same KV tiles $G$ times), which often leads to OOM during Long Context RL tasks.

This PR implements a hardware-aware Prefix-Shared Fused Attention CUDA kernel to solve this. By leveraging a 3D grid layout and L2 Cache broadcasting, the shared prompt KV tiles are loaded into SRAM once and naturally broadcasted across the $G$ queries in the registers. This reduces the global memory read complexity for the KV cache from $O(G \cdot L_{kv} \cdot D)$ down to $O(L_{kv} \cdot D)$, fundamentally breaking the memory wall.

Test Plan

  1. Compilation Validation: Compile the custom C++ extension (rl_engine._C) via setup.py build_ext --inplace on an Ampere architecture (SM8.6).
  2. Correctness Check: Verify the kernel outputs against torch.nn.functional.scaled_dot_product_attention acting as the source of truth.
  3. Performance Benchmarking: Use triton.testing.do_bench to evaluate Latency and TFLOPS across increasing prompt lengths (from 1024 to 16384), keeping $bs=1$, $G=64$, $L_q=512$, $D=128$ fixed.

Test Result

  • Compilation: Successfully compiled with nvcc and c++ without errors. Successfully linked to Python via pybind11
root@bf08593400b1:~/RL-Kernel# 
root@bf08593400b1:~/RL-Kernel# python setup.py build_ext --inplace
/usr/local/lib/python3.11/dist-packages/setuptools/config/_apply_pyprojecttoml.py:78: SetuptoolsWarning: `install_requires` overwritten in `pyproject.toml` (dependencies)
  corresp(dist, value, root_dir)
running build_ext
/usr/local/lib/python3.11/dist-packages/torch/utils/cpp_extension.py:424: UserWarning: There are no x86_64-linux-gnu-g++ version bounds defined for CUDA version 12.4
  warnings.warn(f'There are no {compiler_name} version bounds defined for CUDA version {cuda_str_version}')
building 'rl_engine._C' extension
creating /root/RL-Kernel/build/temp.linux-x86_64-cpython-311/csrc/cuda/attention
/usr/local/lib/python3.11/dist-packages/torch/utils/cpp_extension.py:1965: UserWarning: TORCH_CUDA_ARCH_LIST is not set, all archs for visible cards are included for compilation. 
If this is not desired, please set os.environ['TORCH_CUDA_ARCH_LIST'].
  warnings.warn(
Emitting ninja build file /root/RL-Kernel/build/temp.linux-x86_64-cpython-311/build.ninja...
Compiling objects...
Allowing ninja to set a default number of workers... (overridable by setting the environment variable MAX_JOBS=N)
[1/3] /usr/local/cuda/bin/nvcc --generate-dependencies-with-compile --dependency-output /root/RL-Kernel/build/temp.linux-x86_64-cpython-311/csrc/cuda/attention/prefix_shared_attention.o.d -I/usr/local/lib/python3.11/dist-packages/torch/include -I/usr/local/lib/python3.11/dist-packages/torch/include/torch/csrc/api/include -I/usr/local/lib/python3.11/dist-packages/torch/include/TH -I/usr/local/lib/python3.11/dist-packages/torch/include/THC -I/usr/local/cuda/include -I/usr/include/python3.11 -c -c /root/RL-Kernel/csrc/cuda/attention/prefix_shared_attention.cu -o /root/RL-Kernel/build/temp.linux-x86_64-cpython-311/csrc/cuda/attention/prefix_shared_attention.o -D__CUDA_NO_HALF_OPERATORS__ -D__CUDA_NO_HALF_CONVERSIONS__ -D__CUDA_NO_BFLOAT16_CONVERSIONS__ -D__CUDA_NO_HALF2_OPERATORS__ --expt-relaxed-constexpr --compiler-options ''"'"'-fPIC'"'"'' -O3 --use_fast_math -Xfatbin -compress-all -DTORCH_API_INCLUDE_EXTENSION_H '-DPYBIND11_COMPILER_TYPE="_gcc"' '-DPYBIND11_STDLIB="_libstdcpp"' '-DPYBIND11_BUILD_ABI="_cxxabi1011"' -DTORCH_EXTENSION_NAME=_C -D_GLIBCXX_USE_CXX11_ABI=0 -gencode=arch=compute_86,code=compute_86 -gencode=arch=compute_86,code=sm_86 -std=c++17
[2/3] c++ -MMD -MF /root/RL-Kernel/build/temp.linux-x86_64-cpython-311/csrc/ops.o.d -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -fPIC -I/usr/local/lib/python3.11/dist-packages/torch/include -I/usr/local/lib/python3.11/dist-packages/torch/include/torch/csrc/api/include -I/usr/local/lib/python3.11/dist-packages/torch/include/TH -I/usr/local/lib/python3.11/dist-packages/torch/include/THC -I/usr/local/cuda/include -I/usr/include/python3.11 -c -c /root/RL-Kernel/csrc/ops.cpp -o /root/RL-Kernel/build/temp.linux-x86_64-cpython-311/csrc/ops.o -O3 -std=c++17 -DKERNEL_ALIGN_WITH_CUDA -DTORCH_API_INCLUDE_EXTENSION_H '-DPYBIND11_COMPILER_TYPE="_gcc"' '-DPYBIND11_STDLIB="_libstdcpp"' '-DPYBIND11_BUILD_ABI="_cxxabi1011"' -DTORCH_EXTENSION_NAME=_C -D_GLIBCXX_USE_CXX11_ABI=0
[3/3] /usr/local/cuda/bin/nvcc --generate-dependencies-with-compile --dependency-output /root/RL-Kernel/build/temp.linux-x86_64-cpython-311/csrc/fused_logp_kernel.o.d -I/usr/local/lib/python3.11/dist-packages/torch/include -I/usr/local/lib/python3.11/dist-packages/torch/include/torch/csrc/api/include -I/usr/local/lib/python3.11/dist-packages/torch/include/TH -I/usr/local/lib/python3.11/dist-packages/torch/include/THC -I/usr/local/cuda/include -I/usr/include/python3.11 -c -c /root/RL-Kernel/csrc/fused_logp_kernel.cu -o /root/RL-Kernel/build/temp.linux-x86_64-cpython-311/csrc/fused_logp_kernel.o -D__CUDA_NO_HALF_OPERATORS__ -D__CUDA_NO_HALF_CONVERSIONS__ -D__CUDA_NO_BFLOAT16_CONVERSIONS__ -D__CUDA_NO_HALF2_OPERATORS__ --expt-relaxed-constexpr --compiler-options ''"'"'-fPIC'"'"'' -O3 --use_fast_math -Xfatbin -compress-all -DTORCH_API_INCLUDE_EXTENSION_H '-DPYBIND11_COMPILER_TYPE="_gcc"' '-DPYBIND11_STDLIB="_libstdcpp"' '-DPYBIND11_BUILD_ABI="_cxxabi1011"' -DTORCH_EXTENSION_NAME=_C -D_GLIBCXX_USE_CXX11_ABI=0 -gencode=arch=compute_86,code=compute_86 -gencode=arch=compute_86,code=sm_86 -std=c++17
creating build/lib.linux-x86_64-cpython-311/rl_engine
x86_64-linux-gnu-g++ -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -shared -Wl,-O1 -Wl,-Bsymbolic-functions /root/RL-Kernel/build/temp.linux-x86_64-cpython-311/csrc/cuda/attention/prefix_shared_attention.o /root/RL-Kernel/build/temp.linux-x86_64-cpython-311/csrc/fused_logp_kernel.o /root/RL-Kernel/build/temp.linux-x86_64-cpython-311/csrc/ops.o -L/usr/local/lib/python3.11/dist-packages/torch/lib -L/usr/local/cuda/lib64 -L/usr/lib/x86_64-linux-gnu -lc10 -ltorch -ltorch_cpu -ltorch_python -lcudart -lc10_cuda -ltorch_cuda -o build/lib.linux-x86_64-cpython-311/rl_engine/_C.cpython-311-x86_64-linux-gnu.so
copying build/lib.linux-x86_64-cpython-311/rl_engine/_C.cpython-311-x86_64-linux-gnu.so -> rl_engine
root@bf08593400b1:~/RL-Kernel#
  • Performance: The custom Prefix-Shared kernel consistently outperforms PyTorch Native SDPA, achieving up to 1.10x speedup (9.4% time saved) at 16K context length. More importantly, it requires zero memory expansion for the KV cache, completely eliminating the $G \times$ VRAM bloat.

Benchmark Environment: NVIDIA RTX A4000 (16GB VRAM, PCIe Gen4)
Benchmark Data:

Prompt Len Native (ms) RL-Kernel (ms) Native TFLOPS RL-Kernel TFLOPS Speedup Time Saved
1024 0.339 0.321 50.7 53.6 1.06x 5.4%
2048 0.610 0.566 56.3 60.7 1.08x 7.2%
4096 1.249 1.219 55.0 56.4 1.03x 2.5%
8192 2.592 2.448 53.0 56.1 1.06x 5.5%
16384 5.478 4.963 50.2 55.4 1.10x 9.4%

(Note: TFLOPS represents hardware utilization. Native SDPA hits a memory-bound wall at higher len_kv due to redundant reads, whereas the Prefix-Shared kernel scales beautifully via L2 Cache)

@Flink-ddd
Flink-ddd merged commit 5f91944 into main Jun 3, 2026
4 checks passed
@Flink-ddd Flink-ddd added component: kernels Tasks involving the development of CUDA and Triton underlying operators platform: cuda Specific optimizations or bugs in NVIDIA graphics cards (such as FlashInfer, TMA optimizations) feature labels Jun 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: kernels Tasks involving the development of CUDA and Triton underlying operators feature platform: cuda Specific optimizations or bugs in NVIDIA graphics cards (such as FlashInfer, TMA optimizations)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEAT][kernels]: implement Prefix-Shared Fused Attention for GRPO LogProb computation

1 participant