Skip to content

Commit e75bd19

Browse files
Merge pull request #26 from flagos-ai/cuda_update
Cuda update
2 parents 1254fc6 + 29fb2c7 commit e75bd19

22 files changed

Lines changed: 2499 additions & 2255 deletions

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ python tests/test_spmm_opt.py <dir/> --csv spmm_opt.csv # optional: --dtype flo
9696

9797
```bash
9898
python tests/test_spmm_coo.py <dir_or_file.mtx>
99-
python tests/test_spmm_coo.py --synthetic # optional: --route rowrun|atomic|compare, --skip-api-checks, --skip-coo-coverage
100-
python tests/test_spmm_coo.py <dir/> --csv out.csv # only --route rowrun or atomic (not compare)
101-
# same tuning flags as CSR SpMM where applicable: --dense-cols, --block-n, --block-nnz, --warmup, --iters, --no-cusparse
99+
python tests/test_spmm_coo.py --synthetic # optional: --op non|trans|conj|all, --route rowrun|atomic|compare
100+
python tests/test_spmm_coo.py <dir/> --csv out.csv # only --route rowrun or atomic (not compare); optional: --op all
101+
# same tuning flags as CSR SpMM where applicable: --op, --dense-cols, --block-n, --block-nnz, --warmup, --iters, --no-cusparse
102102
```
103103

104104
**test_sddmm.py** - CSR SDDMM (`.mtx` batch or `--csv`):

README_cn.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ python tests/test_spmm_opt.py <目录/> --csv spmm_opt.csv # 可选:--dtype f
9494

9595
```bash
9696
python tests/test_spmm_coo.py <目录或文件.mtx>
97-
python tests/test_spmm_coo.py --synthetic # 可选:--route rowrun|atomic|compare、--skip-api-checks、--skip-coo-coverage
98-
python tests/test_spmm_coo.py <目录/> --csv out.csv # 仅支持 --route rowrun 或 atomic(compare 不能配 --csv)
99-
# 与 CSR SpMM 类似的调参:--dense-cols、--block-n、--block-nnz、--warmup、--iters、--no-cusparse
97+
python tests/test_spmm_coo.py --synthetic # 可选:--op non|trans|conj|all、--route rowrun|atomic|compare
98+
python tests/test_spmm_coo.py <目录/> --csv out.csv # 仅支持 --route rowrun 或 atomic(compare 不能配 --csv);可选:--op all
99+
# 与 CSR SpMM 类似的调参:--op、--dense-cols、--block-n、--block-nnz、--warmup、--iters、--no-cusparse
100100
```
101101

102102
**test_sddmm.py** - CSR SDDMM(`.mtx` 批量或 `--csv`):

ops_support.csv

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,41 @@ spgemm,CSR,int32,float64,non,triton,SUPPORTED
3434
spgemm,CSR,int64,float32,non,triton,SUPPORTED
3535
spgemm,CSR,int64,float64,non,triton,SUPPORTED
3636
spmm,COO,int32,float16,non,triton,SUPPORTED
37+
spmm,COO,int32,float16,trans,triton,SUPPORTED
38+
spmm,COO,int32,float16,conj,triton,SUPPORTED
3739
spmm,COO,int32,bfloat16,non,triton,SUPPORTED
40+
spmm,COO,int32,bfloat16,trans,triton,SUPPORTED
41+
spmm,COO,int32,bfloat16,conj,triton,SUPPORTED
3842
spmm,COO,int32,float32,non,triton,SUPPORTED
43+
spmm,COO,int32,float32,trans,triton,SUPPORTED
44+
spmm,COO,int32,float32,conj,triton,SUPPORTED
3945
spmm,COO,int32,float64,non,triton,SUPPORTED
46+
spmm,COO,int32,float64,trans,triton,SUPPORTED
47+
spmm,COO,int32,float64,conj,triton,SUPPORTED
4048
spmm,COO,int32,complex64,non,triton,SUPPORTED
49+
spmm,COO,int32,complex64,trans,triton,SUPPORTED
50+
spmm,COO,int32,complex64,conj,triton,SUPPORTED
4151
spmm,COO,int32,complex128,non,triton,SUPPORTED
52+
spmm,COO,int32,complex128,trans,triton,SUPPORTED
53+
spmm,COO,int32,complex128,conj,triton,SUPPORTED
4254
spmm,COO,int64,float16,non,triton,SUPPORTED
55+
spmm,COO,int64,float16,trans,triton,SUPPORTED
56+
spmm,COO,int64,float16,conj,triton,SUPPORTED
4357
spmm,COO,int64,bfloat16,non,triton,SUPPORTED
58+
spmm,COO,int64,bfloat16,trans,triton,SUPPORTED
59+
spmm,COO,int64,bfloat16,conj,triton,SUPPORTED
4460
spmm,COO,int64,float32,non,triton,SUPPORTED
61+
spmm,COO,int64,float32,trans,triton,SUPPORTED
62+
spmm,COO,int64,float32,conj,triton,SUPPORTED
4563
spmm,COO,int64,float64,non,triton,SUPPORTED
64+
spmm,COO,int64,float64,trans,triton,SUPPORTED
65+
spmm,COO,int64,float64,conj,triton,SUPPORTED
4666
spmm,COO,int64,complex64,non,triton,SUPPORTED
67+
spmm,COO,int64,complex64,trans,triton,SUPPORTED
68+
spmm,COO,int64,complex64,conj,triton,SUPPORTED
4769
spmm,COO,int64,complex128,non,triton,SUPPORTED
70+
spmm,COO,int64,complex128,trans,triton,SUPPORTED
71+
spmm,COO,int64,complex128,conj,triton,SUPPORTED
4872
spmm,CSR,int32,float16,non,triton,SUPPORTED
4973
spmm,CSR,int32,float16,trans,triton,SUPPORTED
5074
spmm,CSR,int32,float16,conj,triton,SUPPORTED

ops_support.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,11 @@ def registry(modules: dict[str, SourceModule]) -> tuple[ApiSpec, ...]:
238238
if "spmm_csr" in modules
239239
else ("non", "trans", "conj")
240240
)
241+
spmm_coo_ops = (
242+
op_names(modules["spmm_coo"], "SPMM_COO_OP_NAMES")
243+
if "spmm_coo" in modules
244+
else ("non", "trans", "conj")
245+
)
241246
return (
242247
ApiSpec(
243248
"gather",
@@ -263,7 +268,7 @@ def registry(modules: dict[str, SourceModule]) -> tuple[ApiSpec, ...]:
263268
ApiSpec("spmm", "flagsparse_spmm_csr", "spmm_csr", "CSR", "triton", value_const="SUPPORTED_SPMM_VALUE_DTYPES", index_const="SUPPORTED_INDEX_DTYPES", ops=spmm_ops, notes="op supports non/trans/conj; conj on real dtypes is transpose-equivalent"),
264269
ApiSpec("spmm", "flagsparse_spmm_csr_opt", "spmm_csr", "CSR", "triton_opt", values=("float32", "float64"), index_const="SUPPORTED_INDEX_DTYPES", ops=("non",), notes="bucketed opt path only supports float32/float64"),
265270
ApiSpec("spmm", "flagsparse_spmm_csr_opt_alg2", "spmm_csr_opt_alg2", "CSR", "triton_opt_alg2", value_const="SUPPORTED_SPMM_OPT_ALG2_DTYPES", index_const="SUPPORTED_INDEX_DTYPES", ops=("non",), notes="hardware-aware alg2 opt path only supports float32/float64"),
266-
ApiSpec("spmm", "flagsparse_spmm_coo", "spmm_coo", "COO", "triton", values=spmm_values, index_const="SUPPORTED_INDEX_DTYPES", ops=("non",), notes="COO SpMM reuses CSR SpMM dtype declaration"),
271+
ApiSpec("spmm", "flagsparse_spmm_coo", "spmm_coo", "COO", "triton", values=spmm_values, index_const="SUPPORTED_INDEX_DTYPES", ops=spmm_coo_ops, notes="COO SpMM reuses CSR SpMM dtype declaration; op supports non/trans/conj"),
267272
ApiSpec("spgemm", "flagsparse_spgemm_csr", "spgemm_csr", "CSR", "triton", value_const="SUPPORTED_SPGEMM_VALUE_DTYPES", index_const="SUPPORTED_INDEX_DTYPES", ops=("non",)),
268273
ApiSpec("sddmm", "flagsparse_sddmm_csr", "sddmm_csr", "CSR", "triton", value_const="SUPPORTED_SDDMM_VALUE_DTYPES", index_const="SUPPORTED_INDEX_DTYPES", ops=("non",)),
269274
ApiSpec("spsv", "flagsparse_spsv_csr", "spsv", "CSR", "triton", value_const="SUPPORTED_SPSV_VALUE_DTYPES", index_const="SUPPORTED_SPSV_INDEX_DTYPES", ops=("NON_TRANS", "TRANS"), notes="TRANS support is narrower than NON_TRANS; see combo constants"),

src/flagsparse/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"pytorch_index_scatter",
1010
"cusparse_spmv_gather",
1111
"cusparse_spmv_scatter",
12+
"cusparse_native_gather",
1213
"benchmark_gather_case",
1314
"benchmark_scatter_case",
1415
"benchmark_performance",
@@ -128,6 +129,7 @@
128129
"pytorch_index_scatter",
129130
"cusparse_spmv_gather",
130131
"cusparse_spmv_scatter",
132+
"cusparse_native_gather",
131133
"benchmark_gather_case",
132134
"benchmark_scatter_case",
133135
"benchmark_performance",

src/flagsparse/sparse_operations/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
prepare_alpha_spmm_alg1_tle_opt2,
2222
)
2323
from .gather_scatter import (
24+
cusparse_native_gather,
2425
cusparse_spmv_gather,
2526
cusparse_spmv_scatter,
2627
flagsparse_gather,
@@ -140,6 +141,7 @@
140141
"comprehensive_spsm_test",
141142
"cusparse_spmv_gather",
142143
"cusparse_spmv_scatter",
144+
"cusparse_native_gather",
143145
"flagsparse_gather",
144146
"flagsparse_alpha_spmm_alg1",
145147
"flagsparse_alpha_spmm_alg1_tle",

src/flagsparse/sparse_operations/benchmarks.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
from ._common import *
44

55
from .gather_scatter import (
6+
DEFAULT_GATHER_BLOCK_SIZE,
7+
_PreparedCusparseNativeGather,
68
SUPPORTED_SCATTER_VALUE_DTYPES,
79
_scatter_dtype_error_message,
810
_cusparse_spmv,
9-
_make_gather_selector_matrix,
1011
_make_scatter_selector_matrix,
1112
_pytorch_scatter_impl,
1213
_triton_gather_impl,
@@ -76,10 +77,10 @@ def benchmark_gather_case(
7677
index_dtype=torch.int32,
7778
warmup=20,
7879
iters=200,
79-
block_size=1024,
80+
block_size=DEFAULT_GATHER_BLOCK_SIZE,
8081
run_cusparse=True,
8182
):
82-
"""Benchmark Triton vs PyTorch indexing vs cuSPARSE-backed COO SpMV."""
83+
"""Benchmark Triton vs PyTorch indexing vs native cuSPARSE gather."""
8384
device = torch.device("cuda")
8485
dense_vector = _build_random_dense(dense_size, value_dtype, device)
8586
indices = _build_indices(nnz, dense_size, index_dtype, device, unique=False)
@@ -88,7 +89,9 @@ def benchmark_gather_case(
8889
expected = dense_vector[indices]
8990

9091
pytorch_op = lambda: dense_vector[indices]
91-
triton_op = lambda: _triton_gather_impl(dense_vector, kernel_indices, block_size=block_size)
92+
triton_op = lambda: _triton_gather_impl(
93+
dense_vector, kernel_indices, block_size=block_size
94+
)
9295

9396
pytorch_values, pytorch_ms = _benchmark_cuda_op(pytorch_op, warmup=warmup, iters=iters)
9497
triton_values, triton_ms = _benchmark_cuda_op(triton_op, warmup=warmup, iters=iters)
@@ -110,11 +113,13 @@ def benchmark_gather_case(
110113
if skip_reason:
111114
cusparse_reason = skip_reason
112115
else:
116+
cusparse_plan = None
113117
try:
114-
selector_matrix = _make_gather_selector_matrix(
115-
indices, dense_vector.numel(), dense_vector.dtype
118+
cusparse_out = torch.empty_like(expected)
119+
cusparse_plan = _PreparedCusparseNativeGather(
120+
dense_vector, indices, out=cusparse_out
116121
)
117-
cusparse_op = lambda: _cusparse_spmv(selector_matrix, dense_vector)
122+
cusparse_op = lambda: cusparse_plan.run()
118123
cusparse_values, cusparse_ms = _benchmark_cuda_op(
119124
cusparse_op, warmup=warmup, iters=iters
120125
)
@@ -128,6 +133,9 @@ def benchmark_gather_case(
128133
)
129134
except Exception as exc:
130135
cusparse_reason = str(exc)
136+
finally:
137+
if cusparse_plan is not None:
138+
cusparse_plan.close()
131139

132140
triton_speedup_vs_pytorch = (
133141
pytorch_ms / triton_ms if triton_ms > 0 else float("inf")

0 commit comments

Comments
 (0)