Repository navigation
CuTeDSL examples: stream per-batch input prep for huge SM107 blockscaled GEMM perf runs - #3932
arvin-chou wants to merge 1 commit into
Conversation
…led GEMM perf runs
fengxie
left a comment
There was a problem hiding this comment.
The motivation and memory-budgeting idea are solid, and skipping the dense ref_f32_torch_tensor_cpu in create_scale_factor_tensor when skip_ref_check=True is a great win. However, construct_ab_cute_pointers_emulated_streamed has two blocking data-correctness / crash bugs that affect every supported block-scaled input dtype (Float4E2M1FN, Float8E4M3FN, Float8E5M2, and Float6E*):
-
Dtype mismatch in
dst_torch[:, :, li].copy_(dst_slice_torch)crashes on FP4 and silently corrupts FP8 into NaNs/subnormals (dense_blockscaled_gemm_persistent.py:2881):
_storage_dtypeallocatesdst_torchastorch.uint8, butcutlass_torch.cute_tensor_like(...)views its returneddst_slice_torchback to the native PyTorch dtype (torch.float8_e4m3fn,torch.float8_e5m2, ortorch.float4_e2m1fn_x2— seecutlass/torch.py:325-330). Callinguint8.copy_(dst_slice_torch)therefore runs a numerical PyTorch cast instead of a raw byte copy:Float4E2M1FN(PyTorch withfloat4_e2m1fn_x2): hits a hard GPU device-side assertion (c10/core/DynamicCast.h:79: fetch_and_cast: Assertion 'false' failed→cudaErrorAssert).Float8E4M3FN:[-2.0, -1.0, 0.0, 1.0](uint8[192, 184, 0, 56]) is cast touint8[254, 255, 0, 1], which decodes back as[-448.0, nan, 0.0, 0.001953125](25% NaN, 25% subnormal).Float8E5M2:[-2.0, -1.0, 0.0, 1.0](uint8[192, 188, 0, 60]) is cast touint8[254, 255, 0, 1], which decodes back as[nan, nan, 0.0, 1.52587890625e-05](50% NaN, 25% subnormal).
Becausestream_abonly activates whenskip_ref_check=True, the FP8 corruption is completely silent.
-
Sub-byte packing stride mismatch corrupts batches
1..l-1forFloat4E2M1FN(andFloat6E*), even after.view(torch.uint8)(dense_blockscaled_gemm_persistent.py:2839-2888):
cute.testing.convertpacks sub-byte elements densely atcutlass_dtype.widthbits/element from offset 0: forFloat4E2M1FN, one(rows, cols)slice occupies onlyrows * cols // 2bytes (the first half ofdst_slice_torch; the second half is untouched zeros). Whenfull_cutesetselement_type = Float4E2M1FNondst_torch(strides(cols, 1, rows * cols)), CuTe interprets the batch striderows * colsin FP4 elements (rows * cols // 2bytes), so batchliis read at byte offsetli * rows * cols // 2— whereasdst_torch[:, :, li].copy_(...)writes batchliat byte offsetli * rows * cols. On a 4-batch test filled with[0.5, 1.0, 1.5, 2.0](even with.view(torch.uint8)fixed),full_cutedecodes batch 0 as[0.5], batch 1 as[0.0], batch 2 as[1.0], and batch 3 as[-6.0, 0.0, 1.5].Suggested fix: allocate a flat 1D backing tensor (
backing = torch.empty(rows * cols * l, dtype=_storage_dtype(cutlass_dtype), device="cuda")), builddst_torch = torch.as_strided(backing, (rows, cols, l), strides), and copy only the packedpacked_slice_bytes = (rows * cols * cutlass_dtype.width) // 8raw bytes ofdst_slice_torch.view(torch.uint8)intobacking.view(torch.uint8)[li * packed_slice_bytes : (li + 1) * packed_slice_bytes]. I verified on B200 that this produces byte-for-byte identical buffers to full-tensorcute_tensor_likeacrossFloat8E4M3FN,Float8E5M2,Float4E2M1FN, andFloat6E2M3FNin bothk-major andmn-major.
Additional non-blocking notes:
- Redundant double conversion (
dense_blockscaled_gemm_persistent.py:2875-2880):cutlass_torch.cute_tensor_like(src, ...)already runsconvert_cute_tensorinternally (cutlass/torch.py:313-320); callingconvert_cute_tensoragain right after runs the conversion kernel twice per batch slice. - Packed alignment check (
dense_blockscaled_gemm_persistent.py:2849-2855):batch_bytes = rows * cols * dst_torch.element_size()usesuint8element size (1) instead ofcutlass_dtype.width / 8, which overstatesFloat4E2M1FNslice bytes by 2× (a 16-element / 8-byte slice passes% 16 == 0). Use(rows * cols * cutlass_dtype.width) % 128 == 0, and ideally gatestream_abon it so ineligible shapes fall back cleanly instead of hitting an un-bypassableRuntimeError(there is no CLI flag to disablestream_ab). - Minor: in streamed perf mode, outer-scope
sfa_reordered/sfb_reorderedare still allocated on GPU solely to querysfa_torch.numel()/sfb_torch.numel()foruse_cold_l2;a_torch/b_torchon line 3142 are dead locals; and_storage_elem_bytesis duplicated across both files. Adding a small unit test comparing the streamed buffer againstcute_tensor_likeon FP8 + FP4 (withL > 1) would lock this down.
No description provided.