onnxruntime/onnxruntime/test
Tianlei Wu 9f0fae29e8
[CUDA] Add SparseAttention operator for Phi-3-small (#20216)
### Description
Add CUDA implementation for block sparse attention for Phi-3-small.

Block sparse attention was proposed in [Sparse
Transformers](https://arxiv.org/pdf/1904.10509) by OpenAI, and also
adopted in [BigBird](https://arxiv.org/pdf/2007.14062) with different
sparse layout.

In Phi-3-small, the sparse layout is static, and works with
unidirectional (causal) attention.

Compared to dense attention, the benefit of block sparse is to speed up
both training and inference. It could save memory thus support longer
context length.

- [x] Add operator spec and shape inference
- [x] Symbolic shape inference
- [x] Refactor GroupQueryAttention to expose common kernels for kv cache
concatenation, q/k/v transpose etc.
- [x] Add cuda kernel to convert block mask to CSR format
- [x] Add cuda kernel to generate position ids
- [x] Add compile script and template files to convert triton kernel to
cubin and dispatcher.
- [x] Add triton kernel v1 for prompt
- [x] Add triton kernel v2 for token generation and support padding
- [x] Update IO Binding Helper to allow buffer sharing.
- [x] Test relevance
- [x] Test performance

### Performance
Test in A100-SXM4-80GB with `batch_size=4, num_heads=32,
max_seq_len=8192, head_size=128, sparse_block_size=64, local_blocks=16,
vert_stride=8, num_layout=8`

We compare sparse attention to corresponding GQA with local attention
windows size 1024, or GQA with dense causal.

Average latency in milliseconds (for fused attention kernel used in
prompt prefilling):

seq_len | GQA-Dense | GQA-Local | SparseAttention
-- | -- | -- | --
64 | 0.0465 | 0.0722 | 0.0641
128 | 0.0618 | 0.0787 | 0.0672
256 | 0.1086 | 0.1076 | 0.0943
512 | 0.2535 | 0.2487 | 0.1676
1024 | 0.7042 | 0.7050 | 0.3800
2048 | 2.4125 | 1.9316 | 0.8966
4096 | 8.9346 | 4.5699 | 2.1129
8192 | 40.5401 | 10.3508 | 5.1748

Average latency in milliseconds (for fused attention kernel used in
token generation:

past_seq_len | GQA-Dense | GQA-Local | SparseAttention
-- | -- | -- | --
64 | 0.0186 | 0.0186 | 0.0870
128 | 0.0408 | 0.0466 | 0.1165
256 | 0.0530  | 0.0592 | 0.0988
512 | 0.0445| 0.0447 | 0.1150
1024 | 0.0634  | 0.0640 | 0.1454
2048 | 0.1027 | 0.0637 | 0.1589
4096 | 0.1789 | 0.0631 | 0.1806
8192 | 0.3288 | 0.0655 | 0.2146

We can see that the kernel for token generation still have room to
improve.

#### Limitations
Only support right-side padding and unidirectional attention.

The following are not supported in the first version:
(1) Packed mode like PackedMultiHeadAttention where input has been
removed padding.
(2) paged attention.
(3) bidirectional attention.
(4) GPU compute capacity that is not 8.0, 8.6 and 8.9.
(5) Left side padding.

Some of these limitations will be removed in the future (may be in a new
operator).
2024-04-30 09:06:29 -07:00
..
common Fix build errors from date/date.h C++20 compatibility (#20139) 2024-04-02 22:10:25 -07:00
contrib_ops Mlas Gemm 4bit avx2, avx512, and avx512vnni kernels (#20163) 2024-04-25 21:30:50 -07:00
cuda_host Enable CUDA EP unit testing on Windows (#20039) 2024-03-27 13:32:36 -07:00
custom_op_registration
debug_node_inputs_outputs
flatbuffers Support >2GB of Tensor data in training checkpoint (#20077) 2024-04-22 15:17:43 -07:00
framework Avoid call to Node::ToProto on first Graph::Resolve to improve session creation performance. (#20296) 2024-04-17 10:07:12 +10:00
fuzzing
global_thread_pools Replace T4 to A10 in Linux GPU workflow (#19205) 2024-01-23 10:49:24 -08:00
ir Introduce memory efficient topological sort (#20258) 2024-04-23 08:00:23 +08:00
logging_apis Remove two tests from test_logging_apis.cc (#19100) 2024-01-12 09:26:28 -08:00
mlas Mlas Gemm 4bit avx2, avx512, and avx512vnni kernels (#20163) 2024-04-25 21:30:50 -07:00
onnx Enable provider option to let user provider the profiling file path (#20285) 2024-04-17 09:42:40 -07:00
opaque_api
optimizer [QNN EP] Support per-channel quantized weights (#20154) 2024-04-16 08:45:35 -07:00
perftest OVEP Bug fix 1.18 (#20408) 2024-04-22 18:31:05 -07:00
platform Add helper to get errno and error message (#20324) 2024-04-17 21:17:36 +10:00
proto
providers [QNN EP] Support prelu fp16 (#20428) 2024-04-29 13:26:51 -07:00
python [CUDA] Add SparseAttention operator for Phi-3-small (#20216) 2024-04-30 09:06:29 -07:00
quantization Disable CPU EP's allocator's arena when address sanitizer is enabled (#19485) 2024-02-12 09:39:49 -08:00
shared_lib Fix: Shared lib tests fail during build for CUDA,TRT,DML (#20453) 2024-04-26 20:25:24 -07:00
testdata Fix quantization tools for issue #19529 (#19591) 2024-04-24 19:16:27 +02:00
unittest_main Don't reduce warning level for CUDA build on Windows (#19663) 2024-03-06 15:03:55 +10:00
util [DML EP] Add graph capture (#20257) 2024-04-18 10:15:00 -07:00
wasm Bump follow-redirects from 1.15.4 to 1.15.6 in /onnxruntime/test/wasm (#19950) 2024-03-16 18:54:06 -07:00
win_getopt
xctest
run_benchmark.py Add onnxruntime/test/run_benchmark.py helper script. (#19234) 2024-04-05 07:02:01 -07:00
run_benchmark.readme.md Add onnxruntime/test/run_benchmark.py helper script. (#19234) 2024-04-05 07:02:01 -07:00