From 6fbec8ed473f418a9c20d5b6a4d56486544139da Mon Sep 17 00:00:00 2001 From: Jakub Zakrzewski Date: Tue, 21 Apr 2026 21:06:09 +0200 Subject: [PATCH] [Bugfix][Kernel] nvfp4 cutlass MoE: fix nvfp4 experts quant out-of-bounds read for expert counts not divisible by 4 or 16 (#40351) Signed-off-by: Jakub Zakrzewski --- .../quantization/fp4/nvfp4_experts_quant.cu | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/csrc/libtorch_stable/quantization/fp4/nvfp4_experts_quant.cu b/csrc/libtorch_stable/quantization/fp4/nvfp4_experts_quant.cu index f90bd543ab..744ae4f731 100644 --- a/csrc/libtorch_stable/quantization/fp4/nvfp4_experts_quant.cu +++ b/csrc/libtorch_stable/quantization/fp4/nvfp4_experts_quant.cu @@ -277,7 +277,9 @@ void quant_impl(void* output, void* output_scale, void* input, (totalWorkSize + block.x * grid.x - 1) / (block.x * grid.x); if (blockRepeat > 1) { size_t shared_mem_size = (n_experts + 1) * sizeof(uint32_t); - if (n_experts >= 4) { + // The shared-memory vectorized offset load only handles full 4-expert + // chunks. Use the scalar specialization for the remainder cases. + if (n_experts >= 4 && n_experts % 4 == 0) { cvt_fp16_to_fp4 <<>>( m_topk, k, reinterpret_cast(input), @@ -299,7 +301,9 @@ void quant_impl(void* output, void* output_scale, void* input, n_experts); } } else { - if (n_experts >= 16) { + // The low-latency vectorized expert lookup only handles full 16-expert + // chunks. Fall back to the scalar lookup path for the remainder cases. + if (n_experts >= 16 && n_experts % 16 == 0) { cvt_fp16_to_fp4 <<>>( m_topk, k, reinterpret_cast(input),