Skip to content

CuTeDSL examples: stream per-batch input prep for huge SM107 blockscaled GEMM perf runs - #3932

Open
arvin-chou wants to merge 1 commit into
NVIDIA:mainfrom
arvin-chou:ajhou/blockscaled-gemm-streamed-input-prep
Open

arvin-chou wants to merge 1 commit into
NVIDIA:mainfrom
arvin-chou:ajhou/blockscaled-gemm-streamed-input-prep

Conversation

@arvin-chou

Copy link
Copy Markdown
Collaborator

No description provided.

@fengxie fengxie left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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*):

  1. 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_dtype allocates dst_torch as torch.uint8, but cutlass_torch.cute_tensor_like(...) views its returned dst_slice_torch back to the native PyTorch dtype (torch.float8_e4m3fn, torch.float8_e5m2, or torch.float4_e2m1fn_x2 — see cutlass/torch.py:325-330). Calling uint8.copy_(dst_slice_torch) therefore runs a numerical PyTorch cast instead of a raw byte copy:

    • Float4E2M1FN (PyTorch with float4_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 to uint8 [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 to uint8 [254, 255, 0, 1], which decodes back as [nan, nan, 0.0, 1.52587890625e-05] (50% NaN, 25% subnormal).
      Because stream_ab only activates when skip_ref_check=True, the FP8 corruption is completely silent.
  2. Sub-byte packing stride mismatch corrupts batches 1..l-1 for Float4E2M1FN (and Float6E*), even after .view(torch.uint8) (dense_blockscaled_gemm_persistent.py:2839-2888):
    cute.testing.convert packs sub-byte elements densely at cutlass_dtype.width bits/element from offset 0: for Float4E2M1FN, one (rows, cols) slice occupies only rows * cols // 2 bytes (the first half of dst_slice_torch; the second half is untouched zeros). When full_cute sets element_type = Float4E2M1FN on dst_torch (strides (cols, 1, rows * cols)), CuTe interprets the batch stride rows * cols in FP4 elements (rows * cols // 2 bytes), so batch li is read at byte offset li * rows * cols // 2 — whereas dst_torch[:, :, li].copy_(...) writes batch li at byte offset li * rows * cols. On a 4-batch test filled with [0.5, 1.0, 1.5, 2.0] (even with .view(torch.uint8) fixed), full_cute decodes 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")), build dst_torch = torch.as_strided(backing, (rows, cols, l), strides), and copy only the packed packed_slice_bytes = (rows * cols * cutlass_dtype.width) // 8 raw bytes of dst_slice_torch.view(torch.uint8) into backing.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-tensor cute_tensor_like across Float8E4M3FN, Float8E5M2, Float4E2M1FN, and Float6E2M3FN in both k-major and mn-major.

Additional non-blocking notes:

  • Redundant double conversion (dense_blockscaled_gemm_persistent.py:2875-2880): cutlass_torch.cute_tensor_like(src, ...) already runs convert_cute_tensor internally (cutlass/torch.py:313-320); calling convert_cute_tensor again 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() uses uint8 element size (1) instead of cutlass_dtype.width / 8, which overstates Float4E2M1FN slice bytes by 2× (a 16-element / 8-byte slice passes % 16 == 0). Use (rows * cols * cutlass_dtype.width) % 128 == 0, and ideally gate stream_ab on it so ineligible shapes fall back cleanly instead of hitting an un-bypassable RuntimeError (there is no CLI flag to disable stream_ab).
  • Minor: in streamed perf mode, outer-scope sfa_reordered/sfb_reordered are still allocated on GPU solely to query sfa_torch.numel() / sfb_torch.numel() for use_cold_l2; a_torch/b_torch on line 3142 are dead locals; and _storage_elem_bytes is duplicated across both files. Adding a small unit test comparing the streamed buffer against cute_tensor_like on FP8 + FP4 (with L > 1) would lock this down.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants