Skip to content

Fix int32 overflows in W4A4 kernels at high resolution - #944

Open
danielwoz wants to merge 1 commit into
nunchaku-ai:mainfrom
danielwoz:fix-int32-overflow-w4a4-large-m
Open

Fix int32 overflows in W4A4 kernels at high resolution#944
danielwoz wants to merge 1 commit into
nunchaku-ai:mainfrom
danielwoz:fix-int32-overflow-w4a4-large-m

Conversation

@danielwoz

@danielwoz danielwoz commented Jul 10, 2026

Copy link
Copy Markdown

Several W4A4 code paths compute row-major offsets or size checks in 32-bit int. At high enough resolution (for example a video DiT running at 3840x3840, where a single FFN call exceeds 2^31 elements), the oscales size asserts overflow and abort in debug builds. In release builds the asserts are compiled out and the device-side pointer offsets wrap instead, so the kernels silently read or write out of bounds.

This change promotes the affected products to int64: the two oscales asserts in quantize_w4a4_act_fuse_lora and the matching assert in quantize_w4a4_act, the input pointer offset in quantize_w4a4_fuse_lora_kernel, the fp16 output store offset in the GEMM epilogue (gemm_base.cuh), and the QKV projection epilogue output offset (epilogues.cuh). Remaining per-tile and packed-store offsets stay far below 2^31 at these sizes and are left unchanged.

@danielwoz
danielwoz force-pushed the fix-int32-overflow-w4a4-large-m branch from 40fd085 to 552f0b8 Compare July 10, 2026 02:50
@danielwoz danielwoz changed the title Fix int32 overflow in W4A4 activation quantization at high resolution Fix int32 overflows in W4A4 kernels at high resolution Jul 10, 2026
Several W4A4 code paths compute row-major offsets or size checks as
int * int, which overflows once a single call's padded token count times
feature width exceeds 2^31 (e.g. a video DiT FFN at 3840x3840 resolution).
In debug builds the oscales size asserts abort spuriously; in release
builds the device-side pointer offsets wrap and the kernels read or write
out of bounds.

Promote the affected products to int64:
- gemm_w4a4_launch_impl.cuh: the two oscales asserts in
  quantize_w4a4_act_fuse_lora and the matching assert in quantize_w4a4_act
- gemm_w4a4.cuh: the input offset in quantize_w4a4_fuse_lora_kernel
- gemm_base.cuh: the fp16 output store offset in the GEMM epilogue
- epilogues.cuh: the QKV projection epilogue output offset
@danielwoz
danielwoz force-pushed the fix-int32-overflow-w4a4-large-m branch from 4c1b404 to 20e7bf7 Compare July 10, 2026 03:02
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.

1 participant