From c55ff3b07867ed5058fbb555d9109841d8f8d588 Mon Sep 17 00:00:00 2001 From: Nolan Ho Date: Tue, 17 Nov 2026 21:27:44 -0800 Subject: [PATCH] fix(nvfp4): derive the colwise scale window count from the block size MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `tid_Y_colwise` was computed with a bare `% 4`, which is only correct because `BUFF_IN_DIM_Y / SCALE_DIM` happens to be 4 at the current `SCALE_DIM = 16`. It is an unexplained literal standing in for a derived quantity, and any change to the block size makes the index run past the input buffer: on a 32-element block the count is 2, so `tid_Y_colwise` reaches 3, `thread_offset_Y_colwise` reaches 96 against a 64-row `sIn`, and the derived `out_tr_thread_offset_X` reaches 48 against a `BUFF_OUT_TR_DIM_X` of 32 — the 8-byte shared store then crosses the row and writes past the end of `sOut_tr`. Behaviour is unchanged at `SCALE_DIM = 16` (the derived value is 4), so this is a correctness-of-expression change with no numerical effect. The `static_assert` pins the divisibility the derivation depends on. --- .../specialized/quantize_transpose_nvfp4_tuned_1D.cuh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/transformer_engine/common/cast/nvfp4/specialized/quantize_transpose_nvfp4_tuned_1D.cuh b/transformer_engine/common/cast/nvfp4/specialized/quantize_transpose_nvfp4_tuned_1D.cuh index 3c24f9a4111..994a1aa4306 100644 --- a/transformer_engine/common/cast/nvfp4/specialized/quantize_transpose_nvfp4_tuned_1D.cuh +++ b/transformer_engine/common/cast/nvfp4/specialized/quantize_transpose_nvfp4_tuned_1D.cuh @@ -200,7 +200,13 @@ __device__ __forceinline__ void colwise_scaling( const int warp = threadIdx.x / THREADS_PER_WARP; const int thread_lane = threadIdx.x % THREADS_PER_WARP; - const int tid_Y_colwise = (thread_lane / 2 + warp) % 4; + // Number of SCALE_DIM-tall windows in the input buffer. This was a bare `% 4`, which is only + // correct because BUFF_IN_DIM_Y / SCALE_DIM happens to be 4 at SCALE_DIM = 16: with any other + // block size the index runs past the buffer and the resulting thread_offset_Y_colwise walks off + // the end of sOut_tr in the transpose store below. + static_assert(BUFF_IN_DIM_Y % SCALE_DIM == 0, "SCALE_DIM must divide the input buffer height"); + constexpr int WINDOWS_PER_BUFF_IN = BUFF_IN_DIM_Y / SCALE_DIM; + const int tid_Y_colwise = (thread_lane / 2 + warp) % WINDOWS_PER_BUFF_IN; const int tid_X_colwise = thread_lane; const int thread_offset_Y_colwise = tid_Y_colwise * SCALE_DIM;