flashdiffusion 0.1.1__tar.gz → 0.1.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. {flashdiffusion-0.1.1 → flashdiffusion-0.1.2}/PKG-INFO +1 -1
  2. {flashdiffusion-0.1.1 → flashdiffusion-0.1.2}/pyproject.toml +1 -1
  3. flashdiffusion-0.1.2/src/flashdiffusion/csrc/flash_diffusion_sm120.cu +328 -0
  4. flashdiffusion-0.1.2/src/flashdiffusion/csrc/flash_diffusion_sm80.cu +324 -0
  5. {flashdiffusion-0.1.1 → flashdiffusion-0.1.2}/src/flashdiffusion.egg-info/PKG-INFO +1 -1
  6. {flashdiffusion-0.1.1 → flashdiffusion-0.1.2}/src/flashdiffusion.egg-info/SOURCES.txt +2 -0
  7. {flashdiffusion-0.1.1 → flashdiffusion-0.1.2}/LICENSE +0 -0
  8. {flashdiffusion-0.1.1 → flashdiffusion-0.1.2}/README.md +0 -0
  9. {flashdiffusion-0.1.1 → flashdiffusion-0.1.2}/setup.cfg +0 -0
  10. {flashdiffusion-0.1.1 → flashdiffusion-0.1.2}/setup.py +0 -0
  11. {flashdiffusion-0.1.1 → flashdiffusion-0.1.2}/src/flashdiffusion/ROPE.py +0 -0
  12. {flashdiffusion-0.1.1 → flashdiffusion-0.1.2}/src/flashdiffusion/__init__.py +0 -0
  13. {flashdiffusion-0.1.1 → flashdiffusion-0.1.2}/src/flashdiffusion/cdc.py +0 -0
  14. /flashdiffusion-0.1.1/src/flashdiffusion/csrc/flash_diffusion_sm120.cu → /flashdiffusion-0.1.2/src/flashdiffusion/csrc/flash_diffusion_sm120X.cu +0 -0
  15. {flashdiffusion-0.1.1 → flashdiffusion-0.1.2}/src/flashdiffusion/csrc/flash_diffusion_sm120_.cu +0 -0
  16. /flashdiffusion-0.1.1/src/flashdiffusion/csrc/flash_diffusion_sm80.cu → /flashdiffusion-0.1.2/src/flashdiffusion/csrc/flash_diffusion_sm80X.cu +0 -0
  17. {flashdiffusion-0.1.1 → flashdiffusion-0.1.2}/src/flashdiffusion/csrc/flash_diffusion_sm80_.cu +0 -0
  18. {flashdiffusion-0.1.1 → flashdiffusion-0.1.2}/src/flashdiffusion/dmap.py +0 -0
  19. {flashdiffusion-0.1.1 → flashdiffusion-0.1.2}/src/flashdiffusion/eigensolver.py +0 -0
  20. {flashdiffusion-0.1.1 → flashdiffusion-0.1.2}/src/flashdiffusion/eigensolver_gpu.py +0 -0
  21. {flashdiffusion-0.1.1 → flashdiffusion-0.1.2}/src/flashdiffusion/kernel.py +0 -0
  22. {flashdiffusion-0.1.1 → flashdiffusion-0.1.2}/src/flashdiffusion/kernel_cuda.py +0 -0
  23. {flashdiffusion-0.1.1 → flashdiffusion-0.1.2}/src/flashdiffusion/kernel_cuda_sm120.py +0 -0
  24. {flashdiffusion-0.1.1 → flashdiffusion-0.1.2}/src/flashdiffusion/kernel_jit.py +0 -0
  25. {flashdiffusion-0.1.1 → flashdiffusion-0.1.2}/src/flashdiffusion/utils.py +0 -0
  26. {flashdiffusion-0.1.1 → flashdiffusion-0.1.2}/src/flashdiffusion.egg-info/dependency_links.txt +0 -0
  27. {flashdiffusion-0.1.1 → flashdiffusion-0.1.2}/src/flashdiffusion.egg-info/requires.txt +0 -0
  28. {flashdiffusion-0.1.1 → flashdiffusion-0.1.2}/src/flashdiffusion.egg-info/top_level.txt +0 -0
  29. {flashdiffusion-0.1.1 → flashdiffusion-0.1.2}/tests/test_kernel.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: flashdiffusion
3
- Version: 0.1.1
3
+ Version: 0.1.2
4
4
  Summary: GPU-accelerated diffusion maps with tensor core kernels
5
5
  Author: Julio Candanedo
6
6
  Author-email: Julio Candanedo <julio@sparsetrace.ai>
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "flashdiffusion"
7
- version = "0.1.1"
7
+ version = "0.1.2"
8
8
  description = "GPU-accelerated diffusion maps with tensor core kernels"
9
9
  readme = "README.md"
10
10
  license = {text = "MIT"}
@@ -0,0 +1,328 @@
1
+ /*
2
+ * flash_diffusion_sm120.cu — SM120 WMMA v2
3
+ * ==========================================
4
+ * SM120 (RTX 5090 / RTX PRO 6000 Blackwell consumer) WMMA kernel.
5
+ *
6
+ * v2 over v1:
7
+ * Fixed SMEM size via k-chunking — supports arbitrary d (including d=784).
8
+ * v1 allocated BM×d SMEM which exceeded 99KB limit for d>~350.
9
+ *
10
+ * SMEM layout (fixed, independent of d):
11
+ * sA = BM × KCHUNK × 2 = 128 × 32 × 2 = 8,192 bytes
12
+ * sB = BN × KCHUNK × 2 = 128 × 32 × 2 = 8,192 bytes
13
+ * sDot = BM × BN × 4 = 128 × 128 × 4 = 65,536 bytes
14
+ * Total = 81,920 bytes = 80KB ✓ (SM120 limit: 99KB)
15
+ *
16
+ * K-chunking: outer loop over d in steps of KCHUNK=32.
17
+ * Each chunk loads sA/sB (BM×32) and (BN×32), runs 2 WMMA calls per warp,
18
+ * accumulates into sDot with fill_fragment(fc, 0) only on first chunk.
19
+ *
20
+ * Build:
21
+ * -arch=sm_120 -std=c++17 -O3 --use_fast_math
22
+ * -U__CUDA_NO_HALF_OPERATORS__ -U__CUDA_NO_HALF_CONVERSIONS__
23
+ * --expt-relaxed-constexpr
24
+ */
25
+
26
+ #include <cuda.h>
27
+ #include <cuda_runtime.h>
28
+ #include <cuda_fp16.h>
29
+ #include <mma.h>
30
+
31
+ #include <torch/extension.h>
32
+ #include <ATen/cuda/CUDAContext.h>
33
+ #include <c10/cuda/CUDAException.h>
34
+
35
+ using namespace nvcuda;
36
+
37
+ // ---------------------------------------------------------------------------
38
+ // Constants
39
+ // ---------------------------------------------------------------------------
40
+ static constexpr int WMMA_M = 16;
41
+ static constexpr int WMMA_N = 16;
42
+ static constexpr int WMMA_K = 16;
43
+
44
+ static constexpr int BM = 128;
45
+ static constexpr int BN = 128;
46
+ static constexpr int KCHUNK = 32; // process d in 32-wide chunks (2 WMMA_K)
47
+ // keeps SMEM fixed at 80KB regardless of d
48
+
49
+ // 16 warps: each covers 32×32 sub-tile via 2×2 WMMA calls
50
+ static constexpr int WARPS_M = 4;
51
+ static constexpr int WARPS_N = 4;
52
+ static constexpr int NWARPS = WARPS_M * WARPS_N; // 16
53
+ static constexpr int NTHREADS = NWARPS * 32; // 512
54
+
55
+ // Fixed SMEM: sA + sB + sDot
56
+ static constexpr size_t SMEM_BYTES =
57
+ (size_t)BM * KCHUNK * sizeof(half) // sA
58
+ + (size_t)BN * KCHUNK * sizeof(half) // sB
59
+ + (size_t)BM * BN * sizeof(float); // sDot
60
+
61
+ static inline int ceil_div(int a, int b) { return (a + b - 1) / b; }
62
+ static inline int round_up(int a, int b) { return ceil_div(a, b) * b; }
63
+
64
+ // ---------------------------------------------------------------------------
65
+ // Squared norms
66
+ // ---------------------------------------------------------------------------
67
+ __global__ void sq_norm_kernel_sm120(
68
+ const __half* __restrict__ X, float* __restrict__ sq, int N, int d)
69
+ {
70
+ int i = blockIdx.x * blockDim.x + threadIdx.x;
71
+ if (i >= N) return;
72
+ float s = 0.f;
73
+ for (int k = 0; k < d; ++k) { float v = __half2float(X[i*d+k]); s += v*v; }
74
+ sq[i] = s;
75
+ }
76
+
77
+ // ---------------------------------------------------------------------------
78
+ // SMEM loaders for a k-chunk [k0, k0+klen)
79
+ // sA: [BM, KCHUNK] row-major — zero-pad for klen < KCHUNK
80
+ // sB: [BN, KCHUNK] col-major (stored as [col, k]) — zero-pad similarly
81
+ // ---------------------------------------------------------------------------
82
+ __device__ __forceinline__ void load_sA_chunk(
83
+ half* sA, const half* X, int i0, int bm, int d,
84
+ int k0, int klen, int tid)
85
+ {
86
+ for (int idx = tid; idx < BM * KCHUNK; idx += NTHREADS) {
87
+ int r = idx / KCHUNK, c = idx % KCHUNK;
88
+ sA[idx] = (r < bm && c < klen)
89
+ ? X[(long long)(i0+r)*d + k0+c]
90
+ : __float2half(0.f);
91
+ }
92
+ }
93
+
94
+ __device__ __forceinline__ void load_sB_chunk(
95
+ half* sB, const half* X, int j0, int bn, int d,
96
+ int k0, int klen, int tid)
97
+ {
98
+ for (int idx = tid; idx < BN * KCHUNK; idx += NTHREADS) {
99
+ int col = idx / KCHUNK, c = idx % KCHUNK;
100
+ sB[idx] = (col < bn && c < klen)
101
+ ? X[(long long)(j0+col)*d + k0+c]
102
+ : __float2half(0.f);
103
+ }
104
+ }
105
+
106
+ // ---------------------------------------------------------------------------
107
+ // WMMA dot tile accumulate
108
+ // sA (BM×KCHUNK, row) @ sB^T (KCHUNK×BN, col) → sDot (BM×BN)
109
+ // Each of 16 warps covers 32×32 sub-tile (2×2 WMMA calls).
110
+ // first_chunk=true: fill fc=0 before MMA
111
+ // first_chunk=false: load fc from sDot first (accumulate across k-chunks)
112
+ // ---------------------------------------------------------------------------
113
+ __device__ __forceinline__ void wmma_accumulate(
114
+ const half* sA, const half* sB, float* sDot,
115
+ bool first_chunk, int tid)
116
+ {
117
+ const int warp = tid >> 5;
118
+ const int wm = warp / WARPS_N;
119
+ const int wn = warp % WARPS_N;
120
+
121
+ for (int dm = 0; dm < 2; ++dm) {
122
+ for (int dn = 0; dn < 2; ++dn) {
123
+ const int row16 = wm * 32 + dm * 16;
124
+ const int col16 = wn * 32 + dn * 16;
125
+
126
+ wmma::fragment<wmma::matrix_a, WMMA_M, WMMA_N, WMMA_K,
127
+ half, wmma::row_major> fa;
128
+ wmma::fragment<wmma::matrix_b, WMMA_M, WMMA_N, WMMA_K,
129
+ half, wmma::col_major> fb;
130
+ wmma::fragment<wmma::accumulator, WMMA_M, WMMA_N, WMMA_K, float> fc;
131
+
132
+ if (first_chunk) {
133
+ wmma::fill_fragment(fc, 0.f);
134
+ } else {
135
+ wmma::load_matrix_sync(fc,
136
+ sDot + row16 * BN + col16, BN, wmma::mem_row_major);
137
+ }
138
+
139
+ for (int kk = 0; kk < KCHUNK; kk += WMMA_K) {
140
+ wmma::load_matrix_sync(fa, sA + row16 * KCHUNK + kk, KCHUNK);
141
+ wmma::load_matrix_sync(fb, sB + col16 * KCHUNK + kk, KCHUNK);
142
+ wmma::mma_sync(fc, fa, fb, fc);
143
+ }
144
+
145
+ wmma::store_matrix_sync(
146
+ sDot + row16 * BN + col16, fc, BN, wmma::mem_row_major);
147
+ }
148
+ }
149
+ }
150
+
151
+ // ---------------------------------------------------------------------------
152
+ // Prepass kernel: D_i = Σ_j exp(-β||xi-xj||²) * w_j
153
+ // ---------------------------------------------------------------------------
154
+ __global__ void __launch_bounds__(NTHREADS)
155
+ prepass_kernel_sm120(
156
+ const half* __restrict__ X,
157
+ const float* __restrict__ w,
158
+ float* __restrict__ D,
159
+ const float* __restrict__ sq_X,
160
+ int N, int d, float beta)
161
+ {
162
+ extern __shared__ unsigned char smem[];
163
+ half* sA = reinterpret_cast<half*>(smem);
164
+ half* sB = sA + BM * KCHUNK;
165
+ float* sDot = reinterpret_cast<float*>(sB + BN * KCHUNK);
166
+
167
+ const int tid = threadIdx.x;
168
+ const int i0 = blockIdx.x * BM;
169
+ const int bm = min(BM, N - i0);
170
+ float acc = 0.f;
171
+
172
+ for (int j0 = 0; j0 < N; j0 += BN) {
173
+ const int bn = min(BN, N - j0);
174
+
175
+ // k-chunk loop: accumulate dot products into sDot
176
+ for (int k0 = 0; k0 < d; k0 += KCHUNK) {
177
+ const int klen = min(KCHUNK, d - k0);
178
+ const bool first = (k0 == 0);
179
+
180
+ load_sA_chunk(sA, X, i0, bm, d, k0, klen, tid);
181
+ load_sB_chunk(sB, X, j0, bn, d, k0, klen, tid);
182
+ __syncthreads();
183
+
184
+ wmma_accumulate(sA, sB, sDot, first, tid);
185
+ __syncthreads();
186
+ }
187
+
188
+ // epilogue: dist2 → exp → accumulate
189
+ if (tid < bm) {
190
+ float sqi = sq_X[i0 + tid];
191
+ #pragma unroll 4
192
+ for (int j = 0; j < bn; ++j) {
193
+ float dist2 = fmaxf(sqi + sq_X[j0+j] - 2.f * sDot[tid*BN+j], 0.f);
194
+ acc += __expf(-beta * dist2) * w[j0+j];
195
+ }
196
+ }
197
+ __syncthreads();
198
+ }
199
+
200
+ if (tid < bm) D[i0 + tid] = acc;
201
+ }
202
+
203
+ // ---------------------------------------------------------------------------
204
+ // Matvec kernel: out_i = rscale_i * Σ_j exp(-β||xi-xj||²) * u_j
205
+ // ---------------------------------------------------------------------------
206
+ __global__ void __launch_bounds__(NTHREADS)
207
+ matvec_kernel_sm120(
208
+ const half* __restrict__ X,
209
+ const float* __restrict__ u,
210
+ float* __restrict__ out,
211
+ const float* __restrict__ sq_X,
212
+ const float* __restrict__ rscale,
213
+ int N, int d, float beta)
214
+ {
215
+ extern __shared__ unsigned char smem[];
216
+ half* sA = reinterpret_cast<half*>(smem);
217
+ half* sB = sA + BM * KCHUNK;
218
+ float* sDot = reinterpret_cast<float*>(sB + BN * KCHUNK);
219
+
220
+ const int tid = threadIdx.x;
221
+ const int i0 = blockIdx.x * BM;
222
+ const int bm = min(BM, N - i0);
223
+ float acc = 0.f;
224
+
225
+ for (int j0 = 0; j0 < N; j0 += BN) {
226
+ const int bn = min(BN, N - j0);
227
+
228
+ for (int k0 = 0; k0 < d; k0 += KCHUNK) {
229
+ const int klen = min(KCHUNK, d - k0);
230
+ const bool first = (k0 == 0);
231
+
232
+ load_sA_chunk(sA, X, i0, bm, d, k0, klen, tid);
233
+ load_sB_chunk(sB, X, j0, bn, d, k0, klen, tid);
234
+ __syncthreads();
235
+
236
+ wmma_accumulate(sA, sB, sDot, first, tid);
237
+ __syncthreads();
238
+ }
239
+
240
+ if (tid < bm) {
241
+ float sqi = sq_X[i0 + tid];
242
+ #pragma unroll 4
243
+ for (int j = 0; j < bn; ++j) {
244
+ float dist2 = fmaxf(sqi + sq_X[j0+j] - 2.f * sDot[tid*BN+j], 0.f);
245
+ acc += __expf(-beta * dist2) * u[j0+j];
246
+ }
247
+ }
248
+ __syncthreads();
249
+ }
250
+
251
+ if (tid < bm) out[i0 + tid] = rscale[i0 + tid] * acc;
252
+ }
253
+
254
+ // ---------------------------------------------------------------------------
255
+ // SMEM opt-in
256
+ // ---------------------------------------------------------------------------
257
+ static void set_smem(const void* fn) {
258
+ int dev; C10_CUDA_CHECK(cudaGetDevice(&dev));
259
+ int maxoptin;
260
+ C10_CUDA_CHECK(cudaDeviceGetAttribute(
261
+ &maxoptin, cudaDevAttrMaxSharedMemoryPerBlockOptin, dev));
262
+ TORCH_CHECK((int)SMEM_BYTES <= maxoptin,
263
+ "SMEM ", SMEM_BYTES, " > device max ", maxoptin);
264
+ C10_CUDA_CHECK(cudaFuncSetAttribute(
265
+ fn, cudaFuncAttributeMaxDynamicSharedMemorySize, (int)SMEM_BYTES));
266
+ }
267
+
268
+ // ---------------------------------------------------------------------------
269
+ // Python entry points
270
+ // ---------------------------------------------------------------------------
271
+
272
+ torch::Tensor compute_sq_norms(torch::Tensor X_fp16) {
273
+ int N = X_fp16.size(0), d = X_fp16.size(1);
274
+ auto sq = torch::empty({N}, X_fp16.options().dtype(torch::kFloat32));
275
+ sq_norm_kernel_sm120<<<ceil_div(N,256), 256, 0,
276
+ at::cuda::getCurrentCUDAStream()>>>(
277
+ reinterpret_cast<const __half*>(X_fp16.data_ptr<at::Half>()),
278
+ sq.data_ptr<float>(), N, d);
279
+ C10_CUDA_KERNEL_LAUNCH_CHECK();
280
+ return sq;
281
+ }
282
+
283
+ torch::Tensor prepass(
284
+ torch::Tensor X_fp16, torch::Tensor w,
285
+ torch::Tensor sq_X, float beta)
286
+ {
287
+ int N = X_fp16.size(0), d = X_fp16.size(1);
288
+ set_smem(reinterpret_cast<const void*>(prepass_kernel_sm120));
289
+
290
+ auto D = torch::zeros({N}, w.options());
291
+ prepass_kernel_sm120<<<ceil_div(N,BM), NTHREADS, SMEM_BYTES,
292
+ at::cuda::getCurrentCUDAStream()>>>(
293
+ reinterpret_cast<const half*>(X_fp16.data_ptr<at::Half>()),
294
+ w.data_ptr<float>(), D.data_ptr<float>(),
295
+ sq_X.data_ptr<float>(), N, d, beta);
296
+ C10_CUDA_KERNEL_LAUNCH_CHECK();
297
+ return D;
298
+ }
299
+
300
+ torch::Tensor matvec(
301
+ torch::Tensor X_fp16, torch::Tensor v,
302
+ torch::Tensor rscale, torch::Tensor sq_X, float beta)
303
+ {
304
+ int N = X_fp16.size(0), d = X_fp16.size(1);
305
+ set_smem(reinterpret_cast<const void*>(matvec_kernel_sm120));
306
+
307
+ auto u = (rscale * v).contiguous();
308
+ auto out = torch::empty({N}, v.options());
309
+ matvec_kernel_sm120<<<ceil_div(N,BM), NTHREADS, SMEM_BYTES,
310
+ at::cuda::getCurrentCUDAStream()>>>(
311
+ reinterpret_cast<const half*>(X_fp16.data_ptr<at::Half>()),
312
+ u.data_ptr<float>(), out.data_ptr<float>(),
313
+ sq_X.data_ptr<float>(), rscale.data_ptr<float>(),
314
+ N, d, beta);
315
+ C10_CUDA_KERNEL_LAUNCH_CHECK();
316
+ return out;
317
+ }
318
+
319
+ PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
320
+ m.doc() = "FlashDiffusion SM120 WMMA v2 — k-chunked, arbitrary d "
321
+ "(RTX 5090 / RTX PRO 6000 Blackwell consumer)";
322
+ m.def("compute_sq_norms", &compute_sq_norms, py::arg("X_fp16"));
323
+ m.def("prepass", &prepass,
324
+ py::arg("X_fp16"), py::arg("w"), py::arg("sq_X"), py::arg("beta"));
325
+ m.def("matvec", &matvec,
326
+ py::arg("X_fp16"), py::arg("v"), py::arg("rscale"),
327
+ py::arg("sq_X"), py::arg("beta"));
328
+ }
@@ -0,0 +1,324 @@
1
+ /*
2
+ * flash_diffusion_sm80.cu — WMMA v2
3
+ * ===================================
4
+ * FlashDiffusion WMMA tensor core kernel.
5
+ * Targets: SM80 (A100), SM86 (RTX 3090), SM89 (RTX 4090),
6
+ * SM120 (RTX 5090 / RTX PRO 6000) via fallback
7
+ *
8
+ * v2 over v1:
9
+ * Fixed SMEM size via k-chunking — supports arbitrary d (including d=784).
10
+ * v1 allocated BM×KPAD SMEM which exceeded 96KB A100 limit for d > ~400.
11
+ *
12
+ * SMEM layout (fixed, independent of d):
13
+ * sA = BM × KCHUNK × 2 = 64 × 32 × 2 = 4,096 bytes
14
+ * sB = BN × KCHUNK × 2 = 64 × 32 × 2 = 4,096 bytes
15
+ * sDot = BM × BN × 4 = 64 × 64 × 4 = 16,384 bytes
16
+ * Total = 24,576 bytes = 24KB ✓ (well under 96KB A100 limit)
17
+ *
18
+ * K-chunking: loop over d in steps of KCHUNK=32.
19
+ * Each chunk: load sA[BM×32], sB[BN×32], run 1 WMMA call per warp,
20
+ * accumulate into sDot (load fc from sDot on non-first chunks).
21
+ *
22
+ * Build:
23
+ * SM80/86/89: -arch=sm_80 -std=c++17 -O3 --use_fast_math
24
+ * -U__CUDA_NO_HALF_OPERATORS__ -U__CUDA_NO_HALF_CONVERSIONS__
25
+ * --expt-relaxed-constexpr
26
+ */
27
+
28
+ #include <cuda.h>
29
+ #include <cuda_runtime.h>
30
+ #include <cuda_fp16.h>
31
+ #include <mma.h>
32
+
33
+ #include <torch/extension.h>
34
+ #include <ATen/cuda/CUDAContext.h>
35
+ #include <c10/cuda/CUDAException.h>
36
+
37
+ using namespace nvcuda;
38
+
39
+ // ---------------------------------------------------------------------------
40
+ // Constants
41
+ // ---------------------------------------------------------------------------
42
+ static constexpr int WMMA_M = 16;
43
+ static constexpr int WMMA_N = 16;
44
+ static constexpr int WMMA_K = 16;
45
+
46
+ static constexpr int BM = 64;
47
+ static constexpr int BN = 64;
48
+ static constexpr int KCHUNK = 32; // process d in 32-wide chunks (2 WMMA_K)
49
+
50
+ // 16 warps: each covers one 16×16 WMMA tile
51
+ static constexpr int WARPS_M = BM / WMMA_M; // 4
52
+ static constexpr int WARPS_N = BN / WMMA_N; // 4
53
+ static constexpr int NWARPS = WARPS_M * WARPS_N; // 16
54
+ static constexpr int NTHREADS = NWARPS * 32; // 512
55
+
56
+ // Fixed SMEM — 24KB, works for any d
57
+ static constexpr size_t SMEM_BYTES =
58
+ (size_t)BM * KCHUNK * sizeof(half) // sA
59
+ + (size_t)BN * KCHUNK * sizeof(half) // sB
60
+ + (size_t)BM * BN * sizeof(float); // sDot
61
+
62
+ static inline int ceil_div(int a, int b) { return (a + b - 1) / b; }
63
+
64
+ // ---------------------------------------------------------------------------
65
+ // Squared norms
66
+ // ---------------------------------------------------------------------------
67
+ __global__ void sq_norm_kernel(
68
+ const half* __restrict__ X, float* __restrict__ sq, int N, int d)
69
+ {
70
+ int i = blockIdx.x * blockDim.x + threadIdx.x;
71
+ if (i >= N) return;
72
+ float s = 0.f;
73
+ for (int k = 0; k < d; ++k) { float v = __half2float(X[i*d+k]); s += v*v; }
74
+ sq[i] = s;
75
+ }
76
+
77
+ // ---------------------------------------------------------------------------
78
+ // SMEM loaders for a k-chunk [k0, k0+klen)
79
+ // sA: [BM, KCHUNK] row-major → matrix_a row_major
80
+ // sB: [BN, KCHUNK] col-major → matrix_b col_major (= B^T)
81
+ // ---------------------------------------------------------------------------
82
+ __device__ __forceinline__ void load_sA_chunk(
83
+ half* sA, const half* X, int i0, int bm, int d,
84
+ int k0, int klen, int tid)
85
+ {
86
+ for (int idx = tid; idx < BM * KCHUNK; idx += NTHREADS) {
87
+ int r = idx / KCHUNK, c = idx % KCHUNK;
88
+ sA[idx] = (r < bm && c < klen)
89
+ ? X[(long long)(i0+r)*d + k0+c]
90
+ : __float2half(0.f);
91
+ }
92
+ }
93
+
94
+ __device__ __forceinline__ void load_sB_chunk(
95
+ half* sB, const half* X, int j0, int bn, int d,
96
+ int k0, int klen, int tid)
97
+ {
98
+ for (int idx = tid; idx < BN * KCHUNK; idx += NTHREADS) {
99
+ int col = idx / KCHUNK, c = idx % KCHUNK;
100
+ sB[idx] = (col < bn && c < klen)
101
+ ? X[(long long)(j0+col)*d + k0+c]
102
+ : __float2half(0.f);
103
+ }
104
+ }
105
+
106
+ // ---------------------------------------------------------------------------
107
+ // WMMA accumulate: sA (BM×KCHUNK, row) @ sB^T (KCHUNK×BN, col) → sDot
108
+ // Each warp covers one 16×16 WMMA tile.
109
+ // first_chunk=true: initialize fc=0
110
+ // first_chunk=false: load fc from sDot first (accumulate across k-chunks)
111
+ // ---------------------------------------------------------------------------
112
+ __device__ __forceinline__ void wmma_accumulate(
113
+ const half* sA, const half* sB, float* sDot,
114
+ bool first_chunk, int tid)
115
+ {
116
+ const int warp = tid >> 5;
117
+ const int wm = warp / WARPS_N;
118
+ const int wn = warp % WARPS_N;
119
+ const int row16 = wm * WMMA_M;
120
+ const int col16 = wn * WMMA_N;
121
+
122
+ wmma::fragment<wmma::matrix_a, WMMA_M, WMMA_N, WMMA_K,
123
+ half, wmma::row_major> fa;
124
+ wmma::fragment<wmma::matrix_b, WMMA_M, WMMA_N, WMMA_K,
125
+ half, wmma::col_major> fb;
126
+ wmma::fragment<wmma::accumulator, WMMA_M, WMMA_N, WMMA_K, float> fc;
127
+
128
+ if (first_chunk) {
129
+ wmma::fill_fragment(fc, 0.f);
130
+ } else {
131
+ wmma::load_matrix_sync(fc,
132
+ sDot + row16 * BN + col16, BN, wmma::mem_row_major);
133
+ }
134
+
135
+ for (int kk = 0; kk < KCHUNK; kk += WMMA_K) {
136
+ wmma::load_matrix_sync(fa, sA + row16 * KCHUNK + kk, KCHUNK);
137
+ wmma::load_matrix_sync(fb, sB + col16 * KCHUNK + kk, KCHUNK);
138
+ wmma::mma_sync(fc, fa, fb, fc);
139
+ }
140
+
141
+ wmma::store_matrix_sync(
142
+ sDot + row16 * BN + col16, fc, BN, wmma::mem_row_major);
143
+ }
144
+
145
+ // ---------------------------------------------------------------------------
146
+ // Prepass kernel: D_i = Σ_j exp(-β||xi-xj||²) * w_j
147
+ // ---------------------------------------------------------------------------
148
+ __global__ void __launch_bounds__(NTHREADS)
149
+ prepass_kernel(
150
+ const half* __restrict__ X,
151
+ const float* __restrict__ w,
152
+ float* __restrict__ D,
153
+ const float* __restrict__ sq_X,
154
+ int N, int d, float beta)
155
+ {
156
+ extern __shared__ unsigned char smem[];
157
+ half* sA = reinterpret_cast<half*>(smem);
158
+ half* sB = sA + BM * KCHUNK;
159
+ float* sDot = reinterpret_cast<float*>(sB + BN * KCHUNK);
160
+
161
+ const int tid = threadIdx.x;
162
+ const int i0 = blockIdx.x * BM;
163
+ const int bm = min(BM, N - i0);
164
+ float acc = 0.f;
165
+
166
+ for (int j0 = 0; j0 < N; j0 += BN) {
167
+ const int bn = min(BN, N - j0);
168
+
169
+ // k-chunk loop: accumulate dot products into sDot
170
+ for (int k0 = 0; k0 < d; k0 += KCHUNK) {
171
+ const int klen = min(KCHUNK, d - k0);
172
+ const bool first = (k0 == 0);
173
+
174
+ load_sA_chunk(sA, X, i0, bm, d, k0, klen, tid);
175
+ load_sB_chunk(sB, X, j0, bn, d, k0, klen, tid);
176
+ __syncthreads();
177
+
178
+ wmma_accumulate(sA, sB, sDot, first, tid);
179
+ __syncthreads();
180
+ }
181
+
182
+ // epilogue: dist2 → exp → accumulate
183
+ if (tid < bm) {
184
+ float sqi = sq_X[i0 + tid];
185
+ #pragma unroll 4
186
+ for (int j = 0; j < bn; ++j) {
187
+ float dist2 = fmaxf(sqi + sq_X[j0+j] - 2.f * sDot[tid*BN+j], 0.f);
188
+ acc += __expf(-beta * dist2) * w[j0+j];
189
+ }
190
+ }
191
+ __syncthreads();
192
+ }
193
+
194
+ if (tid < bm) D[i0 + tid] = acc;
195
+ }
196
+
197
+ // ---------------------------------------------------------------------------
198
+ // Matvec kernel: out_i = rscale_i * Σ_j exp(-β||xi-xj||²) * u_j
199
+ // ---------------------------------------------------------------------------
200
+ __global__ void __launch_bounds__(NTHREADS)
201
+ matvec_kernel(
202
+ const half* __restrict__ X,
203
+ const float* __restrict__ u,
204
+ float* __restrict__ out,
205
+ const float* __restrict__ sq_X,
206
+ const float* __restrict__ rscale,
207
+ int N, int d, float beta)
208
+ {
209
+ extern __shared__ unsigned char smem[];
210
+ half* sA = reinterpret_cast<half*>(smem);
211
+ half* sB = sA + BM * KCHUNK;
212
+ float* sDot = reinterpret_cast<float*>(sB + BN * KCHUNK);
213
+
214
+ const int tid = threadIdx.x;
215
+ const int i0 = blockIdx.x * BM;
216
+ const int bm = min(BM, N - i0);
217
+ float acc = 0.f;
218
+
219
+ for (int j0 = 0; j0 < N; j0 += BN) {
220
+ const int bn = min(BN, N - j0);
221
+
222
+ for (int k0 = 0; k0 < d; k0 += KCHUNK) {
223
+ const int klen = min(KCHUNK, d - k0);
224
+ const bool first = (k0 == 0);
225
+
226
+ load_sA_chunk(sA, X, i0, bm, d, k0, klen, tid);
227
+ load_sB_chunk(sB, X, j0, bn, d, k0, klen, tid);
228
+ __syncthreads();
229
+
230
+ wmma_accumulate(sA, sB, sDot, first, tid);
231
+ __syncthreads();
232
+ }
233
+
234
+ if (tid < bm) {
235
+ float sqi = sq_X[i0 + tid];
236
+ #pragma unroll 4
237
+ for (int j = 0; j < bn; ++j) {
238
+ float dist2 = fmaxf(sqi + sq_X[j0+j] - 2.f * sDot[tid*BN+j], 0.f);
239
+ acc += __expf(-beta * dist2) * u[j0+j];
240
+ }
241
+ }
242
+ __syncthreads();
243
+ }
244
+
245
+ if (tid < bm) out[i0 + tid] = rscale[i0 + tid] * acc;
246
+ }
247
+
248
+ // ---------------------------------------------------------------------------
249
+ // SMEM opt-in
250
+ // ---------------------------------------------------------------------------
251
+ static void set_smem(const void* fn) {
252
+ int dev; C10_CUDA_CHECK(cudaGetDevice(&dev));
253
+ int maxoptin;
254
+ C10_CUDA_CHECK(cudaDeviceGetAttribute(
255
+ &maxoptin, cudaDevAttrMaxSharedMemoryPerBlockOptin, dev));
256
+ TORCH_CHECK((int)SMEM_BYTES <= maxoptin,
257
+ "SMEM ", SMEM_BYTES, " > device max ", maxoptin);
258
+ C10_CUDA_CHECK(cudaFuncSetAttribute(
259
+ fn, cudaFuncAttributeMaxDynamicSharedMemorySize, (int)SMEM_BYTES));
260
+ }
261
+
262
+ // ---------------------------------------------------------------------------
263
+ // Python entry points
264
+ // ---------------------------------------------------------------------------
265
+
266
+ torch::Tensor compute_sq_norms(torch::Tensor X_fp16) {
267
+ TORCH_CHECK(X_fp16.is_cuda() && X_fp16.scalar_type() == at::kHalf,
268
+ "X_fp16 must be a CUDA half tensor");
269
+ int N = X_fp16.size(0), d = X_fp16.size(1);
270
+ auto sq = torch::empty({N}, X_fp16.options().dtype(at::kFloat));
271
+ sq_norm_kernel<<<ceil_div(N,256), 256, 0,
272
+ at::cuda::getCurrentCUDAStream()>>>(
273
+ reinterpret_cast<const half*>(X_fp16.data_ptr<at::Half>()),
274
+ sq.data_ptr<float>(), N, d);
275
+ C10_CUDA_KERNEL_LAUNCH_CHECK();
276
+ return sq;
277
+ }
278
+
279
+ torch::Tensor prepass(
280
+ torch::Tensor X_fp16, torch::Tensor w,
281
+ torch::Tensor sq_X, float beta)
282
+ {
283
+ int N = X_fp16.size(0), d = X_fp16.size(1);
284
+ set_smem(reinterpret_cast<const void*>(prepass_kernel));
285
+
286
+ auto D = torch::zeros({N}, w.options());
287
+ prepass_kernel<<<ceil_div(N,BM), NTHREADS, SMEM_BYTES,
288
+ at::cuda::getCurrentCUDAStream()>>>(
289
+ reinterpret_cast<const half*>(X_fp16.data_ptr<at::Half>()),
290
+ w.data_ptr<float>(), D.data_ptr<float>(),
291
+ sq_X.data_ptr<float>(), N, d, beta);
292
+ C10_CUDA_KERNEL_LAUNCH_CHECK();
293
+ return D;
294
+ }
295
+
296
+ torch::Tensor matvec(
297
+ torch::Tensor X_fp16, torch::Tensor v,
298
+ torch::Tensor rscale, torch::Tensor sq_X, float beta)
299
+ {
300
+ int N = X_fp16.size(0), d = X_fp16.size(1);
301
+ set_smem(reinterpret_cast<const void*>(matvec_kernel));
302
+
303
+ auto u = (rscale * v).contiguous();
304
+ auto out = torch::empty({N}, v.options());
305
+ matvec_kernel<<<ceil_div(N,BM), NTHREADS, SMEM_BYTES,
306
+ at::cuda::getCurrentCUDAStream()>>>(
307
+ reinterpret_cast<const half*>(X_fp16.data_ptr<at::Half>()),
308
+ u.data_ptr<float>(), out.data_ptr<float>(),
309
+ sq_X.data_ptr<float>(), rscale.data_ptr<float>(),
310
+ N, d, beta);
311
+ C10_CUDA_KERNEL_LAUNCH_CHECK();
312
+ return out;
313
+ }
314
+
315
+ PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
316
+ m.doc() = "FlashDiffusion WMMA v2 — k-chunked, arbitrary d "
317
+ "(SM80/SM86/SM89 A100/RTX3090/RTX4090)";
318
+ m.def("compute_sq_norms", &compute_sq_norms, py::arg("X_fp16"));
319
+ m.def("prepass", &prepass,
320
+ py::arg("X_fp16"), py::arg("w"), py::arg("sq_X"), py::arg("beta"));
321
+ m.def("matvec", &matvec,
322
+ py::arg("X_fp16"), py::arg("v"), py::arg("rscale"),
323
+ py::arg("sq_X"), py::arg("beta"));
324
+ }
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: flashdiffusion
3
- Version: 0.1.1
3
+ Version: 0.1.2
4
4
  Summary: GPU-accelerated diffusion maps with tensor core kernels
5
5
  Author: Julio Candanedo
6
6
  Author-email: Julio Candanedo <julio@sparsetrace.ai>
@@ -19,7 +19,9 @@ src/flashdiffusion.egg-info/dependency_links.txt
19
19
  src/flashdiffusion.egg-info/requires.txt
20
20
  src/flashdiffusion.egg-info/top_level.txt
21
21
  src/flashdiffusion/csrc/flash_diffusion_sm120.cu
22
+ src/flashdiffusion/csrc/flash_diffusion_sm120X.cu
22
23
  src/flashdiffusion/csrc/flash_diffusion_sm120_.cu
23
24
  src/flashdiffusion/csrc/flash_diffusion_sm80.cu
25
+ src/flashdiffusion/csrc/flash_diffusion_sm80X.cu
24
26
  src/flashdiffusion/csrc/flash_diffusion_sm80_.cu
25
27
  tests/test_kernel.py
File without changes
File without changes
File without changes
File without changes