Commit 1b021e8
committed
feat(path-c): wire fused v32 sparse-MLA to zero-copy DLPack on gb10 (kernel inf blocks e2e)
Wire `_sparse_mla_v32_fused.py` (already env-gated into the differentiable
`sparse_mla_fp8_path_c_apply_prepared_float` fwd+VJP) to feed MLX-CUDA q/kv/indices
to the torch-backend v32 kernel interfaces ZERO-COPY via DLPack, and forward the
`gb10` arch flag so the kernel emits the 99 KiB sm_121 variant.
Wiring (correct + verified):
- `_cuda_zerocopy.mlx_cuda_array_to_torch_tensor`: MLX-CUDA -> torch CUDA tensor
zero-copy. Prefers the native MLX kDLCUDA `__dlpack__` export (mlx 6da6a0e4,
`torch.from_dlpack(mx.array)`); falls back to the `data_ptr()` capsule builder
only when native export is absent. Both are genuine device-view zero-copy
(NOT a silent host-copy degrade). RAISES on failure (RULE #1).
- `_sparse_mla_v32_fused._mlx_to_torch_cuda`: under CPPMEGA_TILELANG_CUDA_ZEROCOPY=1
uses the zero-copy bridge instead of the numpy-host-roundtrip eager bridge.
- forward `gb10=` to `sparse_mla_fwd_interface` / `sparse_mla_bwd` (auto-detect by
default; `CPPMEGA_SPARSE_MLA_V32_GB10` overrides).
- fix a torch-tensor bug: dkv cast used MLX/numpy `.astype` -> `.to(torch.bfloat16)`.
- scripts/verify_sparse_mla_v32_pathc_e2e.py: traps both bridges to PROVE the
fused kernel ran via zero-copy (15 zc-calls / 0 eager), surfaces a kernel inf
blowup loudly with the exact non-finite rows.
PROVEN on gb10 (sm_121a, CUDA 13.3, tilelang 823c807c):
- zero-copy input bridge: 15 `mlx_cuda_array_to_torch_tensor` calls, 0 eager
numpy-host calls, for the fwd+bwd path_c op. `torch.from_dlpack(mx.array)`
returns a CUDA view (is_cuda, bit-identical) for fp32/bf16/int32.
- the real fused fwd+bwd kernels compiled + LAUNCHED on sm_121a (the prior
>48 KB dynamic-smem opt-in wall is GONE after the 823c807c re-tile; 93.0 KiB
fwd fit). atomic_addx4 dKV scatter reached + ran.
BLOCKER (kernel, not wiring): `sparse_mla_fwd_interface(..., gb10=True)` on this
gb10 HEAD produces `inf` on a sparse subset of query rows (e.g. 4864 inf over
19/65536 rows at S=512/topk=256; cos=0.9955 on the FINITE elements). Reproducible
with ZERO cppmega code via the upstream example's own `test_sparse_mla_fwd`
(`diff=nan`) at both S=512/topk=256 and the model S=4096/topk=2048 shape; persists
with dense-valid indices and a guard KV row, so it is a fwd LSE/online-softmax
overflow inside the kernel, not the gather/mask or the wiring. A single inf
poisons full-tensor cos -> nan, so e2e fwd+bwd parity-vs-reference cannot be
asserted until the tilelang `sparse_mla_fwd.py` inf is fixed. Reported honestly,
no silent reference fallback (RULE #1).1 parent 4cce5ca commit 1b021e8
4 files changed
Lines changed: 430 additions & 21 deletions
File tree
- cppmega_mlx/nn/_tilelang
- docs
- scripts
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
287 | 287 | | |
288 | 288 | | |
289 | 289 | | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
38 | 47 | | |
39 | 48 | | |
40 | 49 | | |
| |||
77 | 86 | | |
78 | 87 | | |
79 | 88 | | |
80 | | - | |
81 | | - | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
82 | 100 | | |
83 | 101 | | |
84 | | - | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
85 | 106 | | |
86 | | - | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
87 | 112 | | |
88 | 113 | | |
89 | 114 | | |
90 | 115 | | |
91 | 116 | | |
| 117 | + | |
| 118 | + | |
92 | 119 | | |
93 | 120 | | |
94 | 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 | + | |
95 | 154 | | |
96 | 155 | | |
97 | 156 | | |
| |||
204 | 263 | | |
205 | 264 | | |
206 | 265 | | |
| 266 | + | |
207 | 267 | | |
208 | 268 | | |
209 | 269 | | |
| |||
256 | 316 | | |
257 | 317 | | |
258 | 318 | | |
| 319 | + | |
259 | 320 | | |
260 | 321 | | |
261 | 322 | | |
| |||
268 | 329 | | |
269 | 330 | | |
270 | 331 | | |
271 | | - | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
272 | 335 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
123 | 123 | | |
124 | 124 | | |
125 | 125 | | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
135 | 135 | | |
136 | 136 | | |
137 | 137 | | |
| |||
0 commit comments