Skip to content

[CUDA] columnwise quantize with tma - #3157

Merged
angeloskath merged 22 commits into
ml-explore:mainfrom
nastya236:tma_load
Jul 29, 2026
Merged

[CUDA] columnwise quantize with tma#3157
angeloskath merged 22 commits into
ml-explore:mainfrom
nastya236:tma_load

Conversation

@nastya236

@nastya236 nastya236 commented Feb 23, 2026

Copy link
Copy Markdown
Collaborator

Columnwise quantization with tma (mxfp8), bfloat16:

Size With tma (ms) Without tma (ms)
4096×4096 68.48 77.21
4096×8192 78.74 102.58
8192×4096 80.73 100.61
8192×8192 100.67 145.16
4096×16384 97.08 144.45
16384×4096 99.50 137.13

This PR:

  • Adds PTX instructions for asynchronous copy with TMA
  • Adds fp_quantize_columnwise_mxfp8 kernel for columnwise MXFP8 quantization using TMA on SM100+
  • Splits fp_quantize.cu into fp_quantize.cu (dispatch) and fp_quantize.cuh (kernels) to reduce file size
  • Moves swizzle_scales constants and get_swizzle_launch_args into cu:: namespace for consistency

The diff seems bigger than it is: since fp_quantize.cu already contains all the logic for block quantization, I decided to split it into fp_quantize.cu and fp_quantize.cuh to separate dispatch from kernel implementation.

TODO:
nvfp4 requires a separate columnwise kernel due to TMA tile size constraints. In the proposed kernel each thread processing a tile of size (N, M) and store a transposed result. M is equal to group_size -- 32 bytes formcfp8, but only 8 bytes for nvfp4. Since TMA requires the innermost tile dimension to be at least 128 bits (16 bytes), for nvfp4 kernel would need to load a larger tile and iterate over multiple groups.

@nastya236
nastya236 requested a review from zcbenz March 17, 2026 15:42
@nastya236
nastya236 marked this pull request as ready for review March 17, 2026 15:43
@nastya236 nastya236 changed the title [WIP] columnwise quantize with tma [CUDA] columnwise quantize with tma Mar 17, 2026
Comment thread mlx/backend/cuda/ptx.cuh
#if (CUDART_VERSION >= 12080) && (__CUDA_ARCH__ >= 1000) && \
defined(__CUDA_ARCH_SPECIFIC__)

__device__ __forceinline__ void mbarrier_init(uint64_t* mbar, uint32_t count) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we use the cuda::ptx APIs like cuda::ptx::mbarrier_init API instead? They don't have good documentation and you would have to search https://github.com/NVIDIA/cccl to find out API names though.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Thanks, great question. My intention with raw PTX was to make sure I understand exactly what each instruction does. I felt that when wrapped in ccc up the details are hidden bit, so writing them in a raw ptx makes them explicit. I agree on docs point. That being said if you have a strong preference, I am happy to substitute it with cccl functions :)

Comment thread mlx/backend/cuda/quantized/qqmm_utils.cu Outdated
Comment thread mlx/backend/cuda/quantized/fp_quantize.cuh Outdated
Comment thread mlx/backend/cuda/quantized/fp_quantize.cu Outdated
Comment thread mlx/backend/cuda/quantized/fp_quantize.cuh Outdated
Comment thread mlx/backend/cuda/quantized/fp_quantize.cuh Outdated
Comment thread mlx/backend/cuda/quantized/fp_quantize.cu Outdated
@angeloskath

Copy link
Copy Markdown
Member

@nastya236 is there any reason not to merge this? Will do a quick pass but I think it should be good to go, isn't it?

@nastya236

nastya236 commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator Author

The reason we have not merged it is that i was too slow to respond to Cheng's comment. I think it is mergable, thank you!

@angeloskath angeloskath left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

👍

@angeloskath
angeloskath merged commit e6134a8 into ml-explore:main Jul 29, 2026
28 checks passed
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