1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//! Kernel and PTX module names.
//!
//! `kernel_names` holds the PTX module name constants; `dtype_suffix` and
//! `kernel_name` build the `{op}_{dtype}` names the `.cu` files instantiate.
use crate::dtype::DType;
/// Kernel operation categories for consistent naming.
pub mod kernel_names {
/// Binary operations (two tensor inputs)
pub const BINARY_MODULE: &str = "binary";
/// Unary operations (one tensor input)
pub const UNARY_MODULE: &str = "unary";
/// Adjacent IEEE encodings through integer bit stepping.
pub const NEXT_REPRESENTABLE_MODULE: &str = "next_representable";
/// Single-pass interval and expansion arithmetic with a shared status flag.
pub const INTERVAL_MODULE: &str = "interval";
/// Correctly rounded F64 `exp`, `log`, `log1p` and `pow`, compiled without fast math.
pub const CORRECTLY_ROUNDED_MODULE: &str = "correctly_rounded";
/// Integer unary operations, which wrap in the dtype instead of computing
/// in a float register
pub const UNARY_INT_MODULE: &str = "unary_int";
/// Scalar operations (tensor + scalar input)
pub const SCALAR_MODULE: &str = "scalar";
/// Reduction operations (sum, max, min)
pub const REDUCE_MODULE: &str = "reduce";
/// Integer reduction operations, which accumulate in `Numr128`
pub const REDUCE_INT_MODULE: &str = "reduce_int";
/// Comparison operations (eq, ne, lt, le, gt, ge)
pub const COMPARE_MODULE: &str = "compare";
/// Element-wise activation functions (relu, sigmoid, silu, gelu, leaky_relu, elu)
pub const ACTIVATION_MODULE: &str = "activation";
/// Softmax forward + backward kernels
pub const SOFTMAX_MODULE: &str = "softmax";
/// Snake activation forward + backward kernels
pub const SNAKE_MODULE: &str = "snake";
/// RMSNorm (two-pass and register-cached kernels)
pub const NORM_RMS_MODULE: &str = "norm_rms";
/// LayerNorm
pub const NORM_LAYER_MODULE: &str = "norm_layer";
/// GroupNorm
pub const NORM_GROUP_MODULE: &str = "norm_group";
/// Fused add + RMSNorm, forward and backward
pub const FUSED_ADD_RMS_NORM_MODULE: &str = "fused_add_rms_norm";
/// Fused add + LayerNorm, forward and backward
pub const FUSED_ADD_LAYER_NORM_MODULE: &str = "fused_add_layer_norm";
/// Type casting operations (cast between dtypes)
pub const CAST_MODULE: &str = "cast";
/// Utility operations (fill, arange, linspace, eye)
pub const UTILITY_MODULE: &str = "utility";
/// Random sampling operations (rand, randn, randint, multinomial)
pub const UTILITY_RANDOM_MODULE: &str = "utility_random";
/// Coordinate-addressed indexing (gather_nd, gather_2d, slice_assign)
pub const INDEX_ND_MODULE: &str = "index_nd";
/// Scatter with reduction (sum, prod, max, min, mean)
pub const SCATTER_REDUCE_MODULE: &str = "scatter_reduce";
/// Ternary operations (where)
pub const TERNARY_MODULE: &str = "ternary";
/// Prefix sum operations (exclusive scan)
#[cfg(feature = "sparse")]
pub const SCAN_MODULE: &str = "scan";
/// Sparse matrix operations (SpMV, SpMM)
#[cfg(feature = "sparse")]
pub const SPARSE_SPMV_MODULE: &str = "sparse_spmv";
/// Sparse matrix element-wise operations (add, sub, mul)
#[cfg(feature = "sparse")]
pub const SPARSE_MERGE_MODULE: &str = "sparse_merge";
/// Sparse format conversion operations (COO↔CSR↔CSC)
#[cfg(feature = "sparse")]
pub const SPARSE_CONVERT_MODULE: &str = "sparse_convert";
/// COO sparse element-wise operations with CUB sort
#[cfg(feature = "sparse")]
pub const SPARSE_COO_MODULE: &str = "sparse_coo";
/// Dense × Sparse matrix multiplication (DSMM / SpMM)
#[cfg(feature = "sparse")]
pub const DSMM_MODULE: &str = "dsmm";
/// Linear algebra basic operations (trace, diag, diagflat, identity, transpose)
pub const LINALG_BASIC_MODULE: &str = "linalg_basic";
/// Banded linear system solvers (Thomas, banded LU)
pub const LINALG_BANDED_MODULE: &str = "linalg_banded";
/// Linear algebra solvers (forward_sub, backward_sub, det_from_lu, apply_permutation)
pub const LINALG_SOLVERS_MODULE: &str = "linalg_solvers";
/// Matrix decompositions (LU, Cholesky, QR)
pub const LINALG_DECOMP_MODULE: &str = "linalg_decomp";
/// SVD decomposition (Jacobi algorithm)
pub const LINALG_SVD_MODULE: &str = "linalg_svd";
/// Symmetric eigenvalue decomposition (Jacobi algorithm)
pub const LINALG_EIGEN_MODULE: &str = "linalg_eigen";
/// Schur decomposition (Hessenberg + QR iteration)
pub const LINALG_SCHUR_MODULE: &str = "linalg_schur";
/// General eigenvalue decomposition
pub const LINALG_EIGEN_GENERAL_MODULE: &str = "linalg_eigen_general";
/// Advanced decompositions (rsf2csf)
pub const LINALG_ADVANCED_MODULE: &str = "linalg_advanced";
/// QZ decomposition (generalized Schur - double-shift algorithm)
pub const LINALG_QZ_MODULE: &str = "linalg_qz";
/// Matrix functions (exp, log, sqrt on quasi-triangular matrices)
pub const LINALG_MATRIX_FUNCS_MODULE: &str = "linalg_matrix_funcs";
/// Matrix multiplication operations (native tiled GEMM)
pub const MATMUL_MODULE: &str = "matmul";
/// Grouped FP32 GEMM with device-side group boundaries.
pub const GROUPED_MATMUL_MODULE: &str = "grouped_matmul";
/// Tensor-core WMMA GEMM for F16/BF16 (sm_70+)
pub const MATMUL_WMMA_MODULE: &str = "matmul_wmma";
/// Implicit-GEMM tensor-core conv2d for F16/BF16, NHWC or NCHW input, OHWI weight
pub const CONV_IMPLICIT_GEMM_MODULE: &str = "conv_implicit_gemm";
/// Half-precision GEMV against a transposed weight (small M)
pub const GEMV_MODULE: &str = "gemv";
/// Compile-time-tiled integer GEMM
pub const MATMUL_INT_MODULE: &str = "matmul_int";
/// Compile-time-tiled FP8 GEMM (FP8E4M3, FP8E5M2), F32 accumulation
pub const MATMUL_FP8_MODULE: &str = "matmul_fp8";
/// Cumulative operations (cumsum, cumprod, logsumexp)
pub const CUMULATIVE_MODULE: &str = "cumulative";
/// Integer cumulative operations, which accumulate in `Numr128`
pub const CUMULATIVE_INT_MODULE: &str = "cumulative_int";
/// Normalized Walsh-Hadamard transform (shared-memory butterfly)
pub const FWHT_MODULE: &str = "fwht";
/// Distribution sampling operations (bernoulli, beta, gamma, etc.)
pub const DISTRIBUTIONS_MODULE: &str = "distributions";
/// Quasi-random sequence generation (sobol, halton, latin_hypercube)
pub const QUASIRANDOM_MODULE: &str = "quasirandom";
/// Advanced PRNGs (philox, threefry, pcg64, xoshiro256)
pub const ADVANCED_RANDOM_MODULE: &str = "advanced_random";
/// Statistics operations (mode)
pub const STATISTICS_MODULE: &str = "statistics";
/// Semiring matrix multiplication operations
pub const SEMIRING_MATMUL_MODULE: &str = "semiring_matmul";
/// conv1d im2col gather (packs receptive fields into a GEMM operand)
pub const IM2COL_MODULE: &str = "im2col";
/// conv2d im2col gather (packs receptive fields into a GEMM operand)
pub const IM2COL2D_MODULE: &str = "im2col2d";
/// conv_transpose1d column gather (packs contributing samples for a GEMM)
pub const COL_TRANSPOSE1D_MODULE: &str = "col_transpose1d";
/// conv_transpose1d GEMM-first fold (sums each output position's taps)
pub const COL2IM_TRANSPOSE1D_MODULE: &str = "col2im_transpose1d";
/// Generate kernel name for reduction operations.
#[inline]
pub fn reduce_kernel(op: &str) -> String {
format!("reduce_{}", op)
}
/// Generate kernel name for dimension-wise reduction operations.
#[inline]
pub fn reduce_dim_kernel(op: &str) -> String {
format!("reduce_{}_dim", op)
}
}
/// Get the kernel name suffix for a given dtype.
pub fn dtype_suffix(dtype: DType) -> &'static str {
match dtype {
DType::F32 => "f32",
DType::F64 => "f64",
DType::F16 => "f16",
DType::BF16 => "bf16",
DType::FP8E4M3 => "fp8_e4m3",
DType::FP8E5M2 => "fp8_e5m2",
DType::I64 => "i64",
DType::I32 => "i32",
DType::I16 => "i16",
DType::I8 => "i8",
DType::U64 => "u64",
DType::U32 => "u32",
DType::U16 => "u16",
DType::U8 => "u8",
DType::Bool => "bool",
DType::Complex64 => "c64",
DType::Complex128 => "c128",
}
}
/// Generate a kernel name with dtype suffix.
///
/// # Example
///
/// ```ignore
/// let name = kernel_name("add", DType::F32); // "add_f32"
/// ```
#[inline]
pub fn kernel_name(base: &str, dtype: DType) -> String {
format!("{}_{}", base, dtype_suffix(dtype))
}