diff --git a/crates/cuda-driver/src/lib.rs b/crates/cuda-driver/src/lib.rs index 597d6e59..adba92fc 100644 --- a/crates/cuda-driver/src/lib.rs +++ b/crates/cuda-driver/src/lib.rs @@ -340,6 +340,61 @@ struct ProjectionShape { quant_grid: u32, projection_grid: u32, } + +// The Q4_K PTX forms signed 32-bit packed-byte offsets, including the +// three-row arms of its four-row CTA when N < 2048. Those offsets are +// calculated before per-row guards, so admission must cover the padded +// launch rows, not only the final caller-visible row. +fn q4_k_native_indices_fit(rows: usize, columns: usize) -> bool { + const Q4_K_BLOCK: usize = 256; + const Q4_K_BLOCK_BYTES: usize = 144; + const Q4_K_ROWS_PER_CTA_SMALL: usize = 4; + + if rows == 0 || columns == 0 || !columns.is_multiple_of(Q4_K_BLOCK) { + return false; + } + + let rows_per_cta = if columns < 2048 { + Q4_K_ROWS_PER_CTA_SMALL + } else { + 1 + }; + let last_cta_base_row = match rows + .div_ceil(rows_per_cta) + .checked_sub(1) + .and_then(|index| index.checked_mul(rows_per_cta)) + { + Some(value) => value, + None => return false, + }; + // PTX materializes row, row+1, row+2, and row+3 offsets even for the + // one-row large-N launch; the invalid output arms are only guarded later. + let last_computed_row = match last_cta_base_row.checked_add(3) { + Some(value) if value <= i32::MAX as usize => value, + _ => return false, + }; + let blocks_per_row = columns / Q4_K_BLOCK; + // Each lane group starts at r362=0..7 and advances by eight until its + // index is >= B. The largest final, subsequently unconsumed index is + // B+7; this also covers the single small-N path's common increment. + let loop_advance_blocks = match blocks_per_row.checked_add(7) { + Some(value) => value, + None => return false, + }; + let last_executed_block_offset = match last_computed_row + .checked_mul(blocks_per_row) + .and_then(|row_base| row_base.checked_add(loop_advance_blocks)) + { + Some(value) => value, + None => return false, + }; + let last_signed_byte_offset = match last_executed_block_offset.checked_mul(Q4_K_BLOCK_BYTES) { + Some(value) => value, + None => return false, + }; + last_signed_byte_offset <= i32::MAX as usize +} + fn projection_shape(spec: ProjectionSpec) -> Result { let invalid = Error::InvalidProjectionShape; let (_, block, bytes, small) = spec.kind.layout(); @@ -359,6 +414,9 @@ fn projection_shape(spec: ProjectionSpec) -> Result { Ok(value) }; let weight_bytes = bounded(m.checked_mul(n / block).and_then(|v| v.checked_mul(bytes)))?; + if spec.kind == ProjectionKind::Dense(DenseFormat::Q4_K) && !q4_k_native_indices_fit(m, n) { + return Err(invalid); + } let activation_bytes = bounded(n.checked_mul(4))?; let output_bytes = bounded(m.checked_mul(4))?; let scratch_bytes = bounded((n / 32).checked_mul(36))?; @@ -1856,6 +1914,118 @@ mod family_tests { Err(Error::InvalidProjectionShape) ); } + + #[test] + fn q4_k_admission_bounds_every_padded_cta_byte_offset() { + // At N=256 the kernel launches four output rows per CTA and computes + // all four row-arm offsets before guarding the final partial CTA. + // The largest admitted padded row count therefore stays on a four + // row boundary and its post-loop signed offset fits i32. + const BLOCK_BYTES: usize = 144; + const ROWS_PER_CTA: usize = 4; + let max_block_index = i32::MAX as usize / BLOCK_BYTES; + // Include the common final +8-block PTX increment after the last + // iteration, then round the small-N four-row CTA down to its boundary. + let max_padded_rows = + ((max_block_index.saturating_sub(7) / ROWS_PER_CTA) * ROWS_PER_CTA).max(ROWS_PER_CTA); + let at_limit_offset = (max_padded_rows + 7) * BLOCK_BYTES; + assert!(at_limit_offset <= i32::MAX as usize); + assert!(q4_k_native_indices_fit(max_padded_rows - ROWS_PER_CTA, 256)); + assert!(q4_k_native_indices_fit(max_padded_rows, 256)); + assert!(!q4_k_native_indices_fit(max_padded_rows + 1, 256)); + assert!( + projection_shape(ProjectionSpec { + kind: ProjectionKind::Dense(DenseFormat::Q4_K), + rows: max_padded_rows, + columns: 256, + }) + .is_ok() + ); + + // The large-N launch uses one CTA per row but still materializes + // row+1 through row+3 and the final offsets of all eight lane groups. + let blocks_per_row = 2048 / 256; + let max_large_n_rows = ((max_block_index - (blocks_per_row + 7)) / blocks_per_row) - 2; + assert!(q4_k_native_indices_fit(max_large_n_rows - 1, 2048)); + assert!(q4_k_native_indices_fit(max_large_n_rows, 2048)); + assert!(!q4_k_native_indices_fit(max_large_n_rows + 1, 2048)); + assert!( + projection_shape(ProjectionSpec { + kind: ProjectionKind::Dense(DenseFormat::Q4_K), + rows: max_large_n_rows, + columns: 2048, + }) + .is_ok() + ); + assert_eq!( + projection_shape(ProjectionSpec { + kind: ProjectionKind::Dense(DenseFormat::Q4_K), + rows: max_large_n_rows + 1, + columns: 2048, + }), + Err(Error::InvalidProjectionShape) + ); + + // N=2304 has nine blocks: lane group 0 visits 0 and 8 then reaches + // 16, the maximum final index across groups 0..7. + let blocks_per_row: usize = 2304 / 256; + let final_loop_index = blocks_per_row + 7; + assert_eq!(final_loop_index, 16); + let max_nonwrapping_rows = ((max_block_index - final_loop_index) / blocks_per_row) - 2; + assert!(q4_k_native_indices_fit(max_nonwrapping_rows, 2304)); + assert!(!q4_k_native_indices_fit(max_nonwrapping_rows + 1, 2304)); + + // For sixteen blocks, lane group 7 visits block 15 and increments to + // 23; checking only the block-zero loop would miss this signed offset. + let blocks_per_row: usize = 4096 / 256; + let final_loop_index = blocks_per_row + 7; + assert_eq!(final_loop_index, 23); + let max_nonwrapping_rows = ((max_block_index - final_loop_index) / blocks_per_row) - 2; + assert!(q4_k_native_indices_fit(max_nonwrapping_rows, 4096)); + assert!(!q4_k_native_indices_fit(max_nonwrapping_rows + 1, 4096)); + + // The independent QA counterexample and its adjacent valid-row + // boundary are rejected before any context, cache, or allocation work. + for rows in [14_913_081, 14_913_082] { + assert!(!q4_k_native_indices_fit(rows, 256)); + assert_eq!( + projection_shape(ProjectionSpec { + kind: ProjectionKind::Dense(DenseFormat::Q4_K), + rows, + columns: 256, + }), + Err(Error::InvalidProjectionShape) + ); + } + + // With one row, a large N may have a large total extent while every + // native signed packed-byte base remains representable. This case is + // structural only and deliberately allocates no host or device data. + let large_n = 256 * 3_000_000; + assert!(q4_k_native_indices_fit(1, large_n)); + assert!( + projection_shape(ProjectionSpec { + kind: ProjectionKind::Dense(DenseFormat::Q4_K), + rows: 1, + columns: large_n, + }) + .is_ok() + ); + + // Positive individually-valid M and N still refuse when their Q4_K + // row/block product would overflow the native signed byte offset. + let max_i32_n = (i32::MAX as usize / 256) * 256; + assert!(!q4_k_native_indices_fit(2, max_i32_n)); + assert_eq!( + projection_shape(ProjectionSpec { + kind: ProjectionKind::Dense(DenseFormat::Q4_K), + rows: 2, + columns: max_i32_n, + }), + Err(Error::InvalidProjectionShape) + ); + } + #[cfg(all( feature = "cuda-driver", target_os = "linux",