
|博客|文档|松弛|讨论论坛|
FlashInfer是用于大型语言模型的库和内核生成器,可提供LLM GPU内核的高性能实现,例如FlashTattiention,SparSeateention,PageTtention,PageTention,采样等。 FlashInfer专注于LLM服务和推理,并在各种情况下提供最先进的表现。
查看我们的V0.2发行博客以获取新功能!
FlashInfer的核心功能包括:
plan / run Comput Computitation,我们安排在plan阶段的可变长度输入计算以减轻负载不足问题。FlashInfer支持Pytorch,TVM和C ++(仅标题)API,并且可以轻松地集成到现有项目中。
使用我们的Pytorch API是最简单的入门方法:
我们为Linux提供了预构建的车轮。您可以使用以下命令安装FlashInfer:
# For CUDA 12.4 & torch 2.4
pip install flashinfer -i https://flashinfer.ai/whl/cu124/torch2.4
# For other CUDA & torch versions, please check https://docs.flashinfer.ai/installation.html我们还提供夜间制造的车轮,以尝试主要分支的最新功能:
pip install flashinfer -i https://flashinfer.ai/whl/nightly/cu124/torch2.4另外,您可以从来源构建FlashInfer:
git clone https://github.com/flashinfer-ai/flashinfer.git --recursive
cd flashinfer
pip install -e . -v默认情况下,FlashInfer将其内核使用Just-time(JIT)汇编。要预编译本质内核,请在运行安装命令之前设置环境变量FLASHINFER_ENABLE_AOT=1 :
FLASHINFER_ENABLE_AOT=1 pip install -e . -v有关更多详细信息,请参阅源文档中的安装。
以下是使用FlashInfer的单重点解码/附录/预填充注意内核的最小示例:
import torch
import flashinfer
kv_len = 2048
num_kv_heads = 32
head_dim = 128
k = torch . randn ( kv_len , num_kv_heads , head_dim ). half (). to ( 0 )
v = torch . randn ( kv_len , num_kv_heads , head_dim ). half (). to ( 0 )
# decode attention
num_qo_heads = 32
q = torch . randn ( num_qo_heads , head_dim ). half (). to ( 0 )
o = flashinfer . single_decode_with_kv_cache ( q , k , v ) # decode attention without RoPE on-the-fly
o_rope_on_the_fly = flashinfer . single_decode_with_kv_cache ( q , k , v , pos_encoding_mode = "ROPE_LLAMA" ) # decode with LLaMA style RoPE on-the-fly
# append attention
append_qo_len = 128
q = torch . randn ( append_qo_len , num_qo_heads , head_dim ). half (). to ( 0 ) # append attention, the last 128 tokens in the KV-Cache are the new tokens
o = flashinfer . single_prefill_with_kv_cache ( q , k , v , causal = True ) # append attention without RoPE on-the-fly, apply causal mask
o_rope_on_the_fly = flashinfer . single_prefill_with_kv_cache ( q , k , v , causal = True , pos_encoding_mode = "ROPE_LLAMA" ) # append attention with LLaMA style RoPE on-the-fly, apply causal mask
# prefill attention
qo_len = 2048
q = torch . randn ( qo_len , num_qo_heads , head_dim ). half (). to ( 0 ) # prefill attention
o = flashinfer . single_prefill_with_kv_cache ( q , k , v , causal = False ) # prefill attention without RoPE on-the-fly, do not apply causal mask查看文档,以了解批处理解码/附录/预填充内核和共享式级联内核的文档。
我们使用NVBENCH介绍FlashInfer内核性能,您可以使用以下命令编译和运行基准:
mkdir build
cp cmake/config.cmake build # you can modify the config.cmake to enable/disable benchmarks and change CUDA architectures
cd build
cmake ..
make -j12您可以运行./bench_{single/batch}_{prefill/decode}来基于性能(例如,单次重复预填充注意力,例如./bench_single_prefill )。 ./bench_{single/batch}_{prefill/decode} --help将向您展示可用的选项。
FlashInfer还提供C ++ API和TVM绑定,请参阅文档以获取更多详细信息。
我们很高兴分享FlashInfer被许多尖端项目所采用的,包括但不限于:
FlashInfer的灵感来自FlashAttention 1和2,VLLM,Stream-K,Cutlass和Aitemplate项目。