flashdiffusion 0.1.0__tar.gz → 0.1.1__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 (27) hide show
  1. {flashdiffusion-0.1.0 → flashdiffusion-0.1.1}/PKG-INFO +1 -1
  2. {flashdiffusion-0.1.0 → flashdiffusion-0.1.1}/pyproject.toml +4 -1
  3. {flashdiffusion-0.1.0 → flashdiffusion-0.1.1}/src/flashdiffusion/__init__.py +7 -4
  4. flashdiffusion-0.1.1/src/flashdiffusion/csrc/flash_diffusion_sm120.cu +312 -0
  5. flashdiffusion-0.1.1/src/flashdiffusion/csrc/flash_diffusion_sm120_.cu +204 -0
  6. flashdiffusion-0.1.1/src/flashdiffusion/csrc/flash_diffusion_sm80.cu +293 -0
  7. flashdiffusion-0.1.1/src/flashdiffusion/csrc/flash_diffusion_sm80_.cu +238 -0
  8. {flashdiffusion-0.1.0 → flashdiffusion-0.1.1}/src/flashdiffusion/kernel_cuda.py +11 -9
  9. {flashdiffusion-0.1.0 → flashdiffusion-0.1.1}/src/flashdiffusion/kernel_cuda_sm120.py +12 -10
  10. {flashdiffusion-0.1.0 → flashdiffusion-0.1.1}/src/flashdiffusion/kernel_jit.py +58 -23
  11. {flashdiffusion-0.1.0 → flashdiffusion-0.1.1}/src/flashdiffusion.egg-info/PKG-INFO +1 -1
  12. {flashdiffusion-0.1.0 → flashdiffusion-0.1.1}/src/flashdiffusion.egg-info/SOURCES.txt +4 -0
  13. {flashdiffusion-0.1.0 → flashdiffusion-0.1.1}/LICENSE +0 -0
  14. {flashdiffusion-0.1.0 → flashdiffusion-0.1.1}/README.md +0 -0
  15. {flashdiffusion-0.1.0 → flashdiffusion-0.1.1}/setup.cfg +0 -0
  16. {flashdiffusion-0.1.0 → flashdiffusion-0.1.1}/setup.py +0 -0
  17. {flashdiffusion-0.1.0 → flashdiffusion-0.1.1}/src/flashdiffusion/ROPE.py +0 -0
  18. {flashdiffusion-0.1.0 → flashdiffusion-0.1.1}/src/flashdiffusion/cdc.py +0 -0
  19. {flashdiffusion-0.1.0 → flashdiffusion-0.1.1}/src/flashdiffusion/dmap.py +0 -0
  20. {flashdiffusion-0.1.0 → flashdiffusion-0.1.1}/src/flashdiffusion/eigensolver.py +0 -0
  21. {flashdiffusion-0.1.0 → flashdiffusion-0.1.1}/src/flashdiffusion/eigensolver_gpu.py +0 -0
  22. {flashdiffusion-0.1.0 → flashdiffusion-0.1.1}/src/flashdiffusion/kernel.py +0 -0
  23. {flashdiffusion-0.1.0 → flashdiffusion-0.1.1}/src/flashdiffusion/utils.py +0 -0
  24. {flashdiffusion-0.1.0 → flashdiffusion-0.1.1}/src/flashdiffusion.egg-info/dependency_links.txt +0 -0
  25. {flashdiffusion-0.1.0 → flashdiffusion-0.1.1}/src/flashdiffusion.egg-info/requires.txt +0 -0
  26. {flashdiffusion-0.1.0 → flashdiffusion-0.1.1}/src/flashdiffusion.egg-info/top_level.txt +0 -0
  27. {flashdiffusion-0.1.0 → flashdiffusion-0.1.1}/tests/test_kernel.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: flashdiffusion
3
- Version: 0.1.0
3
+ Version: 0.1.1
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.0"
7
+ version = "0.1.1"
8
8
  description = "GPU-accelerated diffusion maps with tensor core kernels"
9
9
  readme = "README.md"
10
10
  license = {text = "MIT"}
@@ -28,3 +28,6 @@ Homepage = "https://github.com/sparsetrace/FlashDiffusion"
28
28
 
29
29
  [tool.setuptools.packages.find]
30
30
  where = ["src"]
31
+
32
+ [tool.setuptools.package-data]
33
+ flashdiffusion = ["csrc/*.cu", "csrc/*.cuh"]
@@ -1,11 +1,14 @@
1
1
  # src/flashdiffusion/__init__.py
2
2
 
3
3
  import sys, os
4
+
5
+ # ensure src/ is on path for editable installs
4
6
  _src = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
5
7
  if _src not in sys.path:
6
8
  sys.path.insert(0, _src)
7
9
 
8
- import os, torch
10
+ import torch
11
+
9
12
  def _jit_init():
10
13
  """On first import: detect GPU, JIT-build kernel, inject into sys.modules."""
11
14
  if not torch.cuda.is_available():
@@ -13,12 +16,12 @@ def _jit_init():
13
16
  major, minor = torch.cuda.get_device_capability()
14
17
  sm = major * 10 + minor
15
18
 
16
- repo_root = os.path.dirname(os.path.dirname(os.path.dirname(
17
- os.path.abspath(__file__)))) # src/flashdiffusion → repo root
19
+ # package_dir = .../flashdiffusion/ (works for both pip and editable)
20
+ package_dir = os.path.dirname(os.path.abspath(__file__))
18
21
 
19
22
  from .kernel_jit import _build_kernel
20
23
  try:
21
- _build_kernel(sm, repo_root)
24
+ _build_kernel(sm, package_dir)
22
25
  except Exception as e:
23
26
  print(f'[FlashDiffusion] JIT build failed ({e}), CPU fallback only')
24
27
 
@@ -0,0 +1,312 @@
1
+ /*
2
+ * flash_diffusion_sm120.cu
3
+ * ========================
4
+ * SM120 (RTX 5090 / RTX PRO 6000 Blackwell consumer) WMMA kernel.
5
+ *
6
+ * Upgraded from scalar v1 to WMMA tensor cores.
7
+ * SM120 uses nvcuda::wmma (same mma.sync family as SM80/SM86/SM89).
8
+ * No wgmma, no TMEM, no UMMA — those are SM90/SM100 only.
9
+ *
10
+ * Key differences from SM80 WMMA kernel:
11
+ * - BM=128, BN=128 (vs 64×64) — larger tiles exploit SM120's bigger SMEM
12
+ * - 16 warps = 512 threads, each warp covers 32×32 via 2×2 WMMA calls
13
+ * - KPAD = ceil(d/16)*16 as usual
14
+ *
15
+ * SMEM per block (SM120 has 100KB):
16
+ * sA = BM × KPAD × 2 = 128 × KPAD × 2
17
+ * sB = BN × KPAD × 2 = 128 × KPAD × 2
18
+ * sDot = BM × BN × 4 = 128 × 128 × 4 = 64KB
19
+ * For d≤16 (KPAD=16): 64 + 4 + 4 = 72KB ✓
20
+ * For d≤64 (KPAD=64): 64 + 16 + 16 = 96KB ✓
21
+ * For d≤128 (KPAD=128): 64 + 32 + 32 = 128KB — needs opt-in
22
+ *
23
+ * Build:
24
+ * -arch=sm_120 -std=c++17 -O3 --use_fast_math
25
+ * -U__CUDA_NO_HALF_OPERATORS__ -U__CUDA_NO_HALF_CONVERSIONS__
26
+ * --expt-relaxed-constexpr
27
+ */
28
+
29
+ #include <cuda.h>
30
+ #include <cuda_runtime.h>
31
+ #include <cuda_fp16.h>
32
+ #include <mma.h>
33
+
34
+ #include <torch/extension.h>
35
+ #include <ATen/cuda/CUDAContext.h>
36
+ #include <c10/cuda/CUDAException.h>
37
+
38
+ using namespace nvcuda;
39
+
40
+ // ---------------------------------------------------------------------------
41
+ // Constants
42
+ // ---------------------------------------------------------------------------
43
+ static constexpr int WMMA_M = 16;
44
+ static constexpr int WMMA_N = 16;
45
+ static constexpr int WMMA_K = 16;
46
+
47
+ static constexpr int BM = 128;
48
+ static constexpr int BN = 128;
49
+
50
+ // 16 warps = 512 threads
51
+ // Each warp covers a 32×32 sub-tile via 2×2 WMMA calls
52
+ static constexpr int WARPS_M = 4; // 4 warps in M → 4×32 = 128 rows ✓
53
+ static constexpr int WARPS_N = 4; // 4 warps in N → 4×32 = 128 cols ✓
54
+ static constexpr int NWARPS = WARPS_M * WARPS_N; // 16
55
+ static constexpr int NTHREADS = NWARPS * 32; // 512
56
+
57
+ static inline int ceil_div(int a, int b) { return (a + b - 1) / b; }
58
+ static inline int round_up(int a, int b) { return ceil_div(a, b) * b; }
59
+
60
+ static size_t smem_size(int d) {
61
+ int KPAD = round_up(d, WMMA_K);
62
+ return (size_t)BM * KPAD * sizeof(half)
63
+ + (size_t)BN * KPAD * sizeof(half)
64
+ + (size_t)BM * BN * sizeof(float);
65
+ }
66
+
67
+ // ---------------------------------------------------------------------------
68
+ // Squared norms
69
+ // ---------------------------------------------------------------------------
70
+ __global__ void sq_norm_kernel_sm120(
71
+ const __half* __restrict__ X, float* __restrict__ sq, int N, int d)
72
+ {
73
+ int i = blockIdx.x * blockDim.x + threadIdx.x;
74
+ if (i >= N) return;
75
+ float s = 0.f;
76
+ for (int k = 0; k < d; ++k) { float v = __half2float(X[i*d+k]); s += v*v; }
77
+ sq[i] = s;
78
+ }
79
+
80
+ // ---------------------------------------------------------------------------
81
+ // SMEM loaders
82
+ // sA: [BM, KPAD] row-major → matrix_a row_major
83
+ // sB: [BN, KPAD] stored [col,k] → matrix_b col_major (B^T)
84
+ // ---------------------------------------------------------------------------
85
+ __device__ __forceinline__ void load_sA(
86
+ half* sA, const half* X, int i0, int bm, int d, int KPAD, int tid)
87
+ {
88
+ for (int idx = tid; idx < BM * KPAD; idx += NTHREADS) {
89
+ int r = idx / KPAD, c = idx % KPAD;
90
+ sA[idx] = (r < bm && c < d) ? X[(long long)(i0+r)*d + c] : __float2half(0.f);
91
+ }
92
+ }
93
+
94
+ __device__ __forceinline__ void load_sB(
95
+ half* sB, const half* X, int j0, int bn, int d, int KPAD, int tid)
96
+ {
97
+ for (int idx = tid; idx < BN * KPAD; idx += NTHREADS) {
98
+ int col = idx / KPAD, k = idx % KPAD;
99
+ sB[idx] = (col < bn && k < d) ? X[(long long)(j0+col)*d + k] : __float2half(0.f);
100
+ }
101
+ }
102
+
103
+ // ---------------------------------------------------------------------------
104
+ // WMMA dot tile: sA (BM×KPAD, row) @ sB^T (KPAD×BN, col) → sDot (BM×BN)
105
+ //
106
+ // Each of 16 warps covers a 32×32 sub-tile using 2×2 WMMA fragments.
107
+ // warp (wm, wn): output rows [wm*32, wm*32+32), cols [wn*32, wn*32+32)
108
+ // ---------------------------------------------------------------------------
109
+ __device__ __forceinline__ void wmma_dot_tile(
110
+ const half* sA, const half* sB, float* sDot, int KPAD, int tid)
111
+ {
112
+ const int warp = tid >> 5;
113
+ const int wm = warp / WARPS_N; // 0..3
114
+ const int wn = warp % WARPS_N; // 0..3
115
+
116
+ // 2×2 WMMA tiles per warp → 32×32 sub-tile
117
+ for (int dm = 0; dm < 2; ++dm) {
118
+ for (int dn = 0; dn < 2; ++dn) {
119
+ const int row16 = wm * 32 + dm * 16;
120
+ const int col16 = wn * 32 + dn * 16;
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
+ wmma::fill_fragment(fc, 0.f);
128
+
129
+ for (int kk = 0; kk < KPAD; kk += WMMA_K) {
130
+ wmma::load_matrix_sync(fa, sA + row16 * KPAD + kk, KPAD);
131
+ wmma::load_matrix_sync(fb, sB + col16 * KPAD + kk, KPAD);
132
+ wmma::mma_sync(fc, fa, fb, fc);
133
+ }
134
+
135
+ wmma::store_matrix_sync(
136
+ sDot + row16 * BN + col16, fc, BN, wmma::mem_row_major);
137
+ }
138
+ }
139
+ }
140
+
141
+ // ---------------------------------------------------------------------------
142
+ // Prepass kernel
143
+ // ---------------------------------------------------------------------------
144
+ __global__ void prepass_kernel_sm120(
145
+ const half* __restrict__ X,
146
+ const float* __restrict__ w,
147
+ float* __restrict__ D,
148
+ const float* __restrict__ sq_X,
149
+ int N, int d, int KPAD, float beta)
150
+ {
151
+ extern __shared__ unsigned char smem[];
152
+ half* sA = reinterpret_cast<half*>(smem);
153
+ half* sB = sA + BM * KPAD;
154
+ float* sDot = reinterpret_cast<float*>(sB + BN * KPAD);
155
+
156
+ const int tid = threadIdx.x;
157
+ const int i0 = blockIdx.x * BM;
158
+ const int bm = min(BM, N - i0);
159
+ float acc = 0.f;
160
+
161
+ load_sA(sA, X, i0, bm, d, KPAD, tid);
162
+ __syncthreads();
163
+
164
+ for (int j0 = 0; j0 < N; j0 += BN) {
165
+ const int bn = min(BN, N - j0);
166
+
167
+ load_sB(sB, X, j0, bn, d, KPAD, tid);
168
+ __syncthreads();
169
+
170
+ wmma_dot_tile(sA, sB, sDot, KPAD, tid);
171
+ __syncthreads();
172
+
173
+ if (tid < bm) {
174
+ float sqi = sq_X[i0 + tid];
175
+ #pragma unroll 4
176
+ for (int j = 0; j < bn; ++j) {
177
+ float dist2 = fmaxf(sqi + sq_X[j0+j] - 2.f * sDot[tid*BN+j], 0.f);
178
+ acc += __expf(-beta * dist2) * w[j0+j];
179
+ }
180
+ }
181
+ __syncthreads();
182
+ }
183
+
184
+ if (tid < bm) D[i0 + tid] = acc;
185
+ }
186
+
187
+ // ---------------------------------------------------------------------------
188
+ // Matvec kernel
189
+ // ---------------------------------------------------------------------------
190
+ __global__ void matvec_kernel_sm120(
191
+ const half* __restrict__ X,
192
+ const float* __restrict__ u,
193
+ float* __restrict__ out,
194
+ const float* __restrict__ sq_X,
195
+ const float* __restrict__ rscale,
196
+ int N, int d, int KPAD, float beta)
197
+ {
198
+ extern __shared__ unsigned char smem[];
199
+ half* sA = reinterpret_cast<half*>(smem);
200
+ half* sB = sA + BM * KPAD;
201
+ float* sDot = reinterpret_cast<float*>(sB + BN * KPAD);
202
+
203
+ const int tid = threadIdx.x;
204
+ const int i0 = blockIdx.x * BM;
205
+ const int bm = min(BM, N - i0);
206
+ float acc = 0.f;
207
+
208
+ load_sA(sA, X, i0, bm, d, KPAD, tid);
209
+ __syncthreads();
210
+
211
+ for (int j0 = 0; j0 < N; j0 += BN) {
212
+ const int bn = min(BN, N - j0);
213
+
214
+ load_sB(sB, X, j0, bn, d, KPAD, tid);
215
+ __syncthreads();
216
+
217
+ wmma_dot_tile(sA, sB, sDot, KPAD, tid);
218
+ __syncthreads();
219
+
220
+ if (tid < bm) {
221
+ float sqi = sq_X[i0 + tid];
222
+ #pragma unroll 4
223
+ for (int j = 0; j < bn; ++j) {
224
+ float dist2 = fmaxf(sqi + sq_X[j0+j] - 2.f * sDot[tid*BN+j], 0.f);
225
+ acc += __expf(-beta * dist2) * u[j0+j];
226
+ }
227
+ }
228
+ __syncthreads();
229
+ }
230
+
231
+ if (tid < bm) out[i0 + tid] = rscale[i0 + tid] * acc;
232
+ }
233
+
234
+ // ---------------------------------------------------------------------------
235
+ // SMEM opt-in
236
+ // ---------------------------------------------------------------------------
237
+ static void set_smem(const void* fn, size_t bytes) {
238
+ int dev; C10_CUDA_CHECK(cudaGetDevice(&dev));
239
+ int maxoptin;
240
+ C10_CUDA_CHECK(cudaDeviceGetAttribute(
241
+ &maxoptin, cudaDevAttrMaxSharedMemoryPerBlockOptin, dev));
242
+ TORCH_CHECK((int)bytes <= maxoptin,
243
+ "SMEM ", bytes, " > device max ", maxoptin);
244
+ C10_CUDA_CHECK(cudaFuncSetAttribute(
245
+ fn, cudaFuncAttributeMaxDynamicSharedMemorySize, (int)bytes));
246
+ }
247
+
248
+ // ---------------------------------------------------------------------------
249
+ // Python entry points
250
+ // ---------------------------------------------------------------------------
251
+
252
+ torch::Tensor compute_sq_norms(torch::Tensor X_fp16) {
253
+ int N = X_fp16.size(0), d = X_fp16.size(1);
254
+ auto sq = torch::empty({N}, X_fp16.options().dtype(torch::kFloat32));
255
+ sq_norm_kernel_sm120<<<ceil_div(N,256), 256, 0,
256
+ at::cuda::getCurrentCUDAStream()>>>(
257
+ reinterpret_cast<const __half*>(X_fp16.data_ptr<at::Half>()),
258
+ sq.data_ptr<float>(), N, d);
259
+ C10_CUDA_KERNEL_LAUNCH_CHECK();
260
+ return sq;
261
+ }
262
+
263
+ torch::Tensor prepass(
264
+ torch::Tensor X_fp16, torch::Tensor w,
265
+ torch::Tensor sq_X, float beta)
266
+ {
267
+ int N = X_fp16.size(0), d = X_fp16.size(1);
268
+ int KPAD = round_up(d, WMMA_K);
269
+ size_t sm = smem_size(d);
270
+ set_smem(reinterpret_cast<const void*>(prepass_kernel_sm120), sm);
271
+
272
+ auto D = torch::zeros({N}, w.options());
273
+ prepass_kernel_sm120<<<ceil_div(N,BM), NTHREADS, sm,
274
+ at::cuda::getCurrentCUDAStream()>>>(
275
+ reinterpret_cast<const half*>(X_fp16.data_ptr<at::Half>()),
276
+ w.data_ptr<float>(), D.data_ptr<float>(),
277
+ sq_X.data_ptr<float>(), N, d, KPAD, beta);
278
+ C10_CUDA_KERNEL_LAUNCH_CHECK();
279
+ return D;
280
+ }
281
+
282
+ torch::Tensor matvec(
283
+ torch::Tensor X_fp16, torch::Tensor v,
284
+ torch::Tensor rscale, torch::Tensor sq_X, float beta)
285
+ {
286
+ int N = X_fp16.size(0), d = X_fp16.size(1);
287
+ int KPAD = round_up(d, WMMA_K);
288
+ size_t sm = smem_size(d);
289
+ set_smem(reinterpret_cast<const void*>(matvec_kernel_sm120), sm);
290
+
291
+ auto u = (rscale * v).contiguous();
292
+ auto out = torch::empty({N}, v.options());
293
+ matvec_kernel_sm120<<<ceil_div(N,BM), NTHREADS, sm,
294
+ at::cuda::getCurrentCUDAStream()>>>(
295
+ reinterpret_cast<const half*>(X_fp16.data_ptr<at::Half>()),
296
+ u.data_ptr<float>(), out.data_ptr<float>(),
297
+ sq_X.data_ptr<float>(), rscale.data_ptr<float>(),
298
+ N, d, KPAD, beta);
299
+ C10_CUDA_KERNEL_LAUNCH_CHECK();
300
+ return out;
301
+ }
302
+
303
+ PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
304
+ m.doc() = "FlashDiffusion SM120 WMMA tensor cores "
305
+ "(RTX 5090 / RTX PRO 6000 Blackwell consumer)";
306
+ m.def("compute_sq_norms", &compute_sq_norms, py::arg("X_fp16"));
307
+ m.def("prepass", &prepass,
308
+ py::arg("X_fp16"), py::arg("w"), py::arg("sq_X"), py::arg("beta"));
309
+ m.def("matvec", &matvec,
310
+ py::arg("X_fp16"), py::arg("v"), py::arg("rscale"),
311
+ py::arg("sq_X"), py::arg("beta"));
312
+ }
@@ -0,0 +1,204 @@
1
+ /*
2
+ * flash_diffusion_sm120.cu
3
+ * ========================
4
+ * SM120 (RTX 5090 / RTX PRO 6000 Blackwell) kernel for FlashDiffusion.
5
+ *
6
+ * Validated working. No cp.async pipeline (v1 — synchronous loads).
7
+ * cp.async pipeline to be added in v2 once correctness is confirmed.
8
+ *
9
+ * SM120 vs SM80 differences used here:
10
+ * - Larger tiles: BM=128, BN=128 (vs BM=64, BN=64 on SM80)
11
+ * - More threads: NTHREADS=256 (8 warps vs 4 warps)
12
+ * - Same mma.sync instruction family (no wgmma, no TMEM)
13
+ * - No multicast, cluster shape 1x1x1
14
+ *
15
+ * No online softmax. No gradients. rscale precomputed.
16
+ *
17
+ * Build:
18
+ * -arch=sm_120 -std=c++17 --use_fast_math
19
+ * -U__CUDA_NO_HALF_OPERATORS__ --expt-relaxed-constexpr
20
+ */
21
+
22
+ #include <cuda.h>
23
+ #include <cuda_runtime.h>
24
+ #include <cuda_fp16.h>
25
+ #include <torch/extension.h>
26
+ #include <ATen/cuda/CUDAContext.h>
27
+
28
+ static constexpr int BM = 128;
29
+ static constexpr int BN = 128;
30
+ static constexpr int NTHREADS = 256;
31
+
32
+ __device__ __forceinline__ float fast_exp(float x) { return __expf(x); }
33
+
34
+ __global__ void sq_norm_kernel_sm120(
35
+ const __half* __restrict__ X,
36
+ float* __restrict__ sq,
37
+ int N, int d)
38
+ {
39
+ int i = blockIdx.x * blockDim.x + threadIdx.x;
40
+ if (i >= N) return;
41
+ float s = 0.f;
42
+ for (int k = 0; k < d; ++k) {
43
+ float v = __half2float(X[i * d + k]);
44
+ s += v * v;
45
+ }
46
+ sq[i] = s;
47
+ }
48
+
49
+ __global__ void prepass_kernel_sm120(
50
+ const __half* __restrict__ X,
51
+ const float* __restrict__ w,
52
+ float* __restrict__ D,
53
+ const float* __restrict__ sq_X,
54
+ int N, int d, float beta)
55
+ {
56
+ extern __shared__ __half smem[];
57
+ __half* sXi = smem;
58
+ __half* sXj = sXi + BM * d;
59
+
60
+ int tid = threadIdx.x;
61
+ int i0 = blockIdx.x * BM;
62
+ int i1 = min(i0 + BM, N);
63
+ int bm = i1 - i0;
64
+
65
+ float acc = 0.f;
66
+
67
+ for (int idx = tid; idx < bm * d; idx += NTHREADS) {
68
+ int r = idx / d, c = idx % d;
69
+ sXi[r * d + c] = X[(i0 + r) * d + c];
70
+ }
71
+ __syncthreads();
72
+
73
+ for (int j0 = 0; j0 < N; j0 += BN) {
74
+ int j1 = min(j0 + BN, N);
75
+ int bn = j1 - j0;
76
+
77
+ for (int idx = tid; idx < bn * d; idx += NTHREADS) {
78
+ int r = idx / d, c = idx % d;
79
+ sXj[r * d + c] = X[(j0 + r) * d + c];
80
+ }
81
+ __syncthreads();
82
+
83
+ if (tid < bm) {
84
+ float sqi = sq_X[i0 + tid];
85
+ for (int j = 0; j < bn; ++j) {
86
+ float dot = 0.f;
87
+ for (int k = 0; k < d; ++k)
88
+ dot += __half2float(sXi[tid * d + k])
89
+ * __half2float(sXj[j * d + k]);
90
+ float dist2 = fmaxf(sqi + sq_X[j0 + j] - 2.f * dot, 0.f);
91
+ acc += fast_exp(-beta * dist2) * w[j0 + j];
92
+ }
93
+ }
94
+ __syncthreads();
95
+ }
96
+
97
+ if (tid < bm) D[i0 + tid] = acc;
98
+ }
99
+
100
+ __global__ void matvec_kernel_sm120(
101
+ const __half* __restrict__ X,
102
+ const float* __restrict__ u,
103
+ float* __restrict__ out,
104
+ const float* __restrict__ sq_X,
105
+ const float* __restrict__ rscale,
106
+ int N, int d, float beta)
107
+ {
108
+ extern __shared__ __half smem[];
109
+ __half* sXi = smem;
110
+ __half* sXj = sXi + BM * d;
111
+
112
+ int tid = threadIdx.x;
113
+ int i0 = blockIdx.x * BM;
114
+ int i1 = min(i0 + BM, N);
115
+ int bm = i1 - i0;
116
+
117
+ float acc = 0.f;
118
+
119
+ for (int idx = tid; idx < bm * d; idx += NTHREADS) {
120
+ int r = idx / d, c = idx % d;
121
+ sXi[r * d + c] = X[(i0 + r) * d + c];
122
+ }
123
+ __syncthreads();
124
+
125
+ for (int j0 = 0; j0 < N; j0 += BN) {
126
+ int j1 = min(j0 + BN, N);
127
+ int bn = j1 - j0;
128
+
129
+ for (int idx = tid; idx < bn * d; idx += NTHREADS) {
130
+ int r = idx / d, c = idx % d;
131
+ sXj[r * d + c] = X[(j0 + r) * d + c];
132
+ }
133
+ __syncthreads();
134
+
135
+ if (tid < bm) {
136
+ float sqi = sq_X[i0 + tid];
137
+ for (int j = 0; j < bn; ++j) {
138
+ float dot = 0.f;
139
+ for (int k = 0; k < d; ++k)
140
+ dot += __half2float(sXi[tid * d + k])
141
+ * __half2float(sXj[j * d + k]);
142
+ float dist2 = fmaxf(sqi + sq_X[j0 + j] - 2.f * dot, 0.f);
143
+ acc += fast_exp(-beta * dist2) * u[j0 + j];
144
+ }
145
+ }
146
+ __syncthreads();
147
+ }
148
+
149
+ if (tid < bm) out[i0 + tid] = rscale[i0 + tid] * acc;
150
+ }
151
+
152
+ torch::Tensor compute_sq_norms(torch::Tensor X_fp16) {
153
+ int N = X_fp16.size(0), d = X_fp16.size(1);
154
+ auto sq = torch::empty({N}, X_fp16.options().dtype(torch::kFloat32));
155
+ sq_norm_kernel_sm120<<<(N + 255) / 256, 256, 0,
156
+ at::cuda::getCurrentCUDAStream()>>>(
157
+ reinterpret_cast<const __half*>(X_fp16.data_ptr<at::Half>()),
158
+ sq.data_ptr<float>(), N, d);
159
+ return sq;
160
+ }
161
+
162
+ torch::Tensor prepass(
163
+ torch::Tensor X_fp16, torch::Tensor w,
164
+ torch::Tensor sq_X, float beta)
165
+ {
166
+ int N = X_fp16.size(0), d = X_fp16.size(1);
167
+ auto D = torch::zeros({N}, w.options());
168
+ int grid = (N + BM - 1) / BM;
169
+ size_t smem = (size_t)(BM + BN) * d * sizeof(__half);
170
+ prepass_kernel_sm120<<<grid, NTHREADS, smem,
171
+ at::cuda::getCurrentCUDAStream()>>>(
172
+ reinterpret_cast<const __half*>(X_fp16.data_ptr<at::Half>()),
173
+ w.data_ptr<float>(), D.data_ptr<float>(),
174
+ sq_X.data_ptr<float>(), N, d, beta);
175
+ return D;
176
+ }
177
+
178
+ torch::Tensor matvec(
179
+ torch::Tensor X_fp16, torch::Tensor v,
180
+ torch::Tensor rscale, torch::Tensor sq_X, float beta)
181
+ {
182
+ int N = X_fp16.size(0), d = X_fp16.size(1);
183
+ auto u = rscale * v;
184
+ auto out = torch::empty({N}, v.options());
185
+ int grid = (N + BM - 1) / BM;
186
+ size_t smem = (size_t)(BM + BN) * d * sizeof(__half);
187
+ matvec_kernel_sm120<<<grid, NTHREADS, smem,
188
+ at::cuda::getCurrentCUDAStream()>>>(
189
+ reinterpret_cast<const __half*>(X_fp16.data_ptr<at::Half>()),
190
+ u.data_ptr<float>(), out.data_ptr<float>(),
191
+ sq_X.data_ptr<float>(), rscale.data_ptr<float>(), N, d, beta);
192
+ return out;
193
+ }
194
+
195
+ PYBIND11_MODULE(flash_diffusion_sm120, m) {
196
+ m.doc() = "FlashDiffusion SM120 — 128x128 tiles v1 validated "
197
+ "(RTX 5090 / RTX PRO 6000 Blackwell)";
198
+ m.def("compute_sq_norms", &compute_sq_norms, py::arg("X_fp16"));
199
+ m.def("prepass", &prepass,
200
+ py::arg("X_fp16"), py::arg("w"), py::arg("sq_X"), py::arg("beta"));
201
+ m.def("matvec", &matvec,
202
+ py::arg("X_fp16"), py::arg("v"), py::arg("rscale"),
203
+ py::arg("sq_X"), py::arg("beta"));
204
+ }