diff --git a/crates/quant-core/src/iq2_s.rs b/crates/quant-core/src/iq2_s.rs index ea9157d9..38ea081c 100644 --- a/crates/quant-core/src/iq2_s.rs +++ b/crates/quant-core/src/iq2_s.rs @@ -95,30 +95,13 @@ mod tests { assert_eq!(decode_block(&block).unwrap(), [5.375; BLOCK_ELEMENTS]); } - #[test] - fn raw_eight_bit_signs_signed_zero_and_nonfinite_scales_are_admitted() { - let mut block = [0u8; BLOCK_BYTES]; - block[..2].copy_from_slice(&0x3c00u16.to_le_bytes()); - for signs in 0..=u8::MAX { - block[34..66].fill(signs); - for (element, value) in decode_block(&block).unwrap().into_iter().enumerate() { - assert_eq!( - value, - if signs & (1 << (element % 8)) != 0 { - -1.0 - } else { - 1.0 - } - ); - } - } - block[34..66].fill(0x81); + fn assert_scale_policy(block: &mut [u8; BLOCK_BYTES]) { for scale in [ 0x0000u16, 0x8000, 0x7c00, 0xfc00, 0x7c01, 0xfc01, 0x7e00, 0xfe00, ] { block[..2].copy_from_slice(&scale.to_le_bytes()); let mut output = [0.0; BLOCK_ELEMENTS]; - decode_into(&block, &mut output).unwrap(); + decode_into(block, &mut output).unwrap(); for (element, value) in output.into_iter().enumerate() { let negative = (scale & 0x8000 != 0) ^ matches!(element % 8, 0 | 7); if scale & 0x7c00 == 0x7c00 { @@ -135,6 +118,23 @@ mod tests { } } + fn assert_signs(block: &[u8; BLOCK_BYTES], signs: u8) { + for (element, value) in decode_block(block).unwrap().into_iter().enumerate() { + assert_eq!(value, if signs & (1 << (element % 8)) != 0 { -1.0 } else { 1.0 }); + } + } + + #[test] + fn raw_eight_bit_signs_signed_zero_and_nonfinite_scales_are_admitted() { + let mut block = [0u8; BLOCK_BYTES]; + block[..2].copy_from_slice(&0x3c00u16.to_le_bytes()); + for signs in 0..=u8::MAX { + block[34..66].fill(signs); + assert_signs(&block, signs); + } + block[34..66].fill(0x81); + assert_scale_policy(&mut block); + } #[test] fn unaligned_bulk_and_bad_shapes_preserve_output_contract() { let mut block = [0u8; BLOCK_BYTES]; diff --git a/crates/quant-core/src/iq3_s.rs b/crates/quant-core/src/iq3_s.rs index 4b2e1bee..96e6903d 100644 --- a/crates/quant-core/src/iq3_s.rs +++ b/crates/quant-core/src/iq3_s.rs @@ -65,8 +65,7 @@ mod tests { use super::*; use sha2::{Digest, Sha256}; - #[test] - fn decodes_constant_and_pinned_mixed_block() { + fn assert_grid_digest() { let mut table_bytes = Vec::with_capacity(512 * 4); for value in IQ3S_GRID { table_bytes.extend_from_slice(&value.to_le_bytes()); @@ -75,6 +74,9 @@ mod tests { hex::encode(Sha256::digest(table_bytes)), "bd1af4945a1717c65610b0284e4628b9a1ba3b306fae3a06f6e5f597356e349f" ); + } + + fn assert_constant_and_bulk_blocks() { let mut block = [0u8; BLOCK_BYTES]; block[..2].copy_from_slice(&0x3c00u16.to_le_bytes()); assert_eq!(decode_block(&block).unwrap(), [1.0; BLOCK_ELEMENTS]); @@ -89,6 +91,9 @@ mod tests { decode_into(&blocks, &mut decoded).unwrap(); assert_eq!(decoded[0], -1.0); assert_eq!(decoded[BLOCK_ELEMENTS], 1.0); + } + + fn assert_mixed_block() { let mut mixed = [0u8; BLOCK_BYTES]; for (index, byte) in mixed.iter_mut().enumerate() { *byte = (index as u8).wrapping_mul(73).wrapping_add(19); @@ -108,4 +113,11 @@ mod tests { assert!(decode_block(&mixed).is_err()); assert!(decode_into(&mixed[..109], &mut [0.0; BLOCK_ELEMENTS]).is_err()); } + + #[test] + fn decodes_constant_and_pinned_mixed_block() { + assert_grid_digest(); + assert_constant_and_bulk_blocks(); + assert_mixed_block(); + } } diff --git a/crates/quant-core/src/iq3_xxs.rs b/crates/quant-core/src/iq3_xxs.rs index f3a9e2a8..9feab346 100644 --- a/crates/quant-core/src/iq3_xxs.rs +++ b/crates/quant-core/src/iq3_xxs.rs @@ -110,36 +110,13 @@ mod tests { ); } - #[test] - fn sign_codes_signed_zero_and_nonfinite_scales_are_admitted() { - let mut block = [0u8; BLOCK_BYTES]; - for code in 0..128u32 { - let word = code | (code << 7) | (code << 14) | (code << 21); - for chunk in block[66..].as_chunks_mut::<4>().0 { - chunk.copy_from_slice(&word.to_le_bytes()); - } - block[..2].copy_from_slice(&0x3c00u16.to_le_bytes()); - let expected_signs = code as u8 | (((code.count_ones() & 1) as u8) << 7); - for (element, value) in decode_block(&block).unwrap().into_iter().enumerate() { - assert_eq!( - value, - if expected_signs & (1 << (element % 8)) != 0 { - -1.0 - } else { - 1.0 - } - ); - } - } - for chunk in block[66..].as_chunks_mut::<4>().0 { - chunk.copy_from_slice(&(1u32 | (1 << 7) | (1 << 14) | (1 << 21)).to_le_bytes()); - } + fn assert_scale_policy(block: &mut [u8; BLOCK_BYTES]) { for scale in [ 0x0000u16, 0x8000, 0x7c00, 0xfc00, 0x7c01, 0xfc01, 0x7e00, 0xfe00, ] { block[..2].copy_from_slice(&scale.to_le_bytes()); let mut output = [0.0; BLOCK_ELEMENTS]; - decode_into(&block, &mut output).unwrap(); + decode_into(block, &mut output).unwrap(); for (element, value) in output.into_iter().enumerate() { let negative = (scale & 0x8000 != 0) ^ matches!(element % 8, 0 | 7); if scale & 0x7c00 == 0x7c00 { @@ -156,6 +133,32 @@ mod tests { } } + fn assert_signs(block: &[u8; BLOCK_BYTES], signs: u8) { + for (element, value) in decode_block(block).unwrap().into_iter().enumerate() { + assert_eq!(value, if signs & (1 << (element % 8)) != 0 { -1.0 } else { 1.0 }); + } + } + + fn assert_sign_code(block: &mut [u8; BLOCK_BYTES], code: u32) { + let word = code | (code << 7) | (code << 14) | (code << 21); + for chunk in block[66..].as_chunks_mut::<4>().0 { + chunk.copy_from_slice(&word.to_le_bytes()); + } + block[..2].copy_from_slice(&0x3c00u16.to_le_bytes()); + assert_signs(block, code as u8 | (((code.count_ones() & 1) as u8) << 7)); + } + + #[test] + fn sign_codes_signed_zero_and_nonfinite_scales_are_admitted() { + let mut block = [0u8; BLOCK_BYTES]; + for code in 0..128u32 { + assert_sign_code(&mut block, code); + } + for chunk in block[66..].as_chunks_mut::<4>().0 { + chunk.copy_from_slice(&(1u32 | (1 << 7) | (1 << 14) | (1 << 21)).to_le_bytes()); + } + assert_scale_policy(&mut block); + } #[test] fn unaligned_bulk_and_bad_shapes_preserve_output_contract() { let mut block = [0u8; BLOCK_BYTES]; diff --git a/crates/quant-core/src/iq4_xs.rs b/crates/quant-core/src/iq4_xs.rs index afbc0c76..ee510418 100644 --- a/crates/quant-core/src/iq4_xs.rs +++ b/crates/quant-core/src/iq4_xs.rs @@ -101,6 +101,25 @@ mod tests { assert_eq!(actual[240], 1751.5); } + fn assert_bad_shapes_rejected(source: &[u8], output: &mut [f32; 2 * BLOCK_ELEMENTS]) { + for length in [0, 1, BLOCK_BYTES - 1, BLOCK_BYTES + 1, source.len() - 1] { + output.fill(123.0); + assert!(decode_into(&source[..length], output).is_err()); + assert_eq!(*output, [123.0; 2 * BLOCK_ELEMENTS]); + } + for length in [ + 0, + BLOCK_ELEMENTS - 1, + BLOCK_ELEMENTS + 1, + 2 * BLOCK_ELEMENTS, + ] { + output.fill(123.0); + assert!(decode_into(&source[..BLOCK_BYTES], &mut output[..length]).is_err()); + assert_eq!(*output, [123.0; 2 * BLOCK_ELEMENTS]); + } + decode_into(&[], &mut []).unwrap(); + } + #[test] fn bulk_handles_unaligned_blocks_and_rejects_shapes_before_writes() { let blocks = [known_block(0x3800), known_block(0xbc00)]; @@ -121,24 +140,8 @@ mod tests { ); } } - for source_len in [0, 1, BLOCK_BYTES - 1, BLOCK_BYTES + 1, source.len() - 1] { - output.fill(123.0); - assert!(decode_into(&source[..source_len], &mut output).is_err()); - assert_eq!(output, [123.0; BLOCK_ELEMENTS * 2]); - } - for output_len in [ - 0, - BLOCK_ELEMENTS - 1, - BLOCK_ELEMENTS + 1, - BLOCK_ELEMENTS * 2, - ] { - output.fill(123.0); - assert!(decode_into(&source[..BLOCK_BYTES], &mut output[..output_len]).is_err()); - assert_eq!(output, [123.0; BLOCK_ELEMENTS * 2]); - } - decode_into(&[], &mut []).unwrap(); + assert_bad_shapes_rejected(source, &mut output); } - #[test] fn signed_zero_keeps_both_multiplication_signs() { for scale_bits in [0x0000u16, 0x8000] { diff --git a/crates/quant-core/src/q2_0.rs b/crates/quant-core/src/q2_0.rs index 370fed93..2e6076d6 100644 --- a/crates/quant-core/src/q2_0.rs +++ b/crates/quant-core/src/q2_0.rs @@ -59,6 +59,25 @@ mod tests { assert_eq!(&output[60..], &[1.0; 4]); } + fn assert_bad_shapes_rejected(source: &[u8], output: &mut [f32; 2 * BLOCK_ELEMENTS]) { + for length in [0, 1, BLOCK_BYTES - 1, BLOCK_BYTES + 1, source.len() - 1] { + output.fill(123.0); + assert!(decode_into(&source[..length], output).is_err()); + assert_eq!(*output, [123.0; 2 * BLOCK_ELEMENTS]); + } + for length in [ + 0, + BLOCK_ELEMENTS - 1, + BLOCK_ELEMENTS + 1, + 2 * BLOCK_ELEMENTS, + ] { + output.fill(123.0); + assert!(decode_into(&source[..BLOCK_BYTES], &mut output[..length]).is_err()); + assert_eq!(*output, [123.0; 2 * BLOCK_ELEMENTS]); + } + decode_into(&[], &mut []).unwrap(); + } + #[test] fn unaligned_bulk_and_invalid_shapes_preserve_contract() { let mut block = [0u8; BLOCK_BYTES]; @@ -75,43 +94,27 @@ mod tests { for quartet in output.as_chunks::<4>().0 { assert_eq!(quartet, &[-1.0, 0.0, 1.0, 2.0]); } - for length in [0, 1, BLOCK_BYTES - 1, BLOCK_BYTES + 1, source.len() - 1] { - output.fill(123.0); - assert!(decode_into(&source[..length], &mut output).is_err()); - assert_eq!(output, [123.0; 2 * BLOCK_ELEMENTS]); - } - for length in [ - 0, - BLOCK_ELEMENTS - 1, - BLOCK_ELEMENTS + 1, - 2 * BLOCK_ELEMENTS, - ] { - output.fill(123.0); - assert!(decode_into(&source[..BLOCK_BYTES], &mut output[..length]).is_err()); - assert_eq!(output, [123.0; 2 * BLOCK_ELEMENTS]); - } - decode_into(&[], &mut []).unwrap(); + assert_bad_shapes_rejected(source, &mut output); } - - #[test] - fn signed_zero_and_nonfinite_scales_are_admitted() { - let mut block = [0u8; BLOCK_BYTES]; - block[2..].fill(0xe4); + fn assert_signed_zero_scales(block: &mut [u8; BLOCK_BYTES]) { for (scale, expected) in [ (0x0000u16, [0x8000_0000u32, 0, 0, 0]), (0x8000, [0, 0x8000_0000, 0x8000_0000, 0x8000_0000]), ] { block[..2].copy_from_slice(&scale.to_le_bytes()); - for quartet in decode_block(&block).unwrap().as_chunks::<4>().0 { + for quartet in decode_block(block).unwrap().as_chunks::<4>().0 { for (value, bits) in quartet.iter().zip(expected) { assert_eq!(value.to_bits(), bits); } } } + } + + fn assert_nonfinite_scales(block: &mut [u8; BLOCK_BYTES]) { for scale in [0x7c00u16, 0xfc00, 0x7c01, 0xfc01, 0x7e00, 0xfe00] { block[..2].copy_from_slice(&scale.to_le_bytes()); let mut output = [0.0; BLOCK_ELEMENTS]; - decode_into(&block, &mut output).unwrap(); + decode_into(block, &mut output).unwrap(); for quartet in output.as_chunks::<4>().0 { if scale & 0x03ff != 0 { assert!(quartet.iter().all(|v| v.is_nan())); @@ -128,4 +131,12 @@ mod tests { } } } + + #[test] + fn signed_zero_and_nonfinite_scales_are_admitted() { + let mut block = [0u8; BLOCK_BYTES]; + block[2..].fill(0xe4); + assert_signed_zero_scales(&mut block); + assert_nonfinite_scales(&mut block); + } } diff --git a/crates/quant-core/src/q4_k.rs b/crates/quant-core/src/q4_k.rs index 2e9f6e14..c64b7145 100644 --- a/crates/quant-core/src/q4_k.rs +++ b/crates/quant-core/src/q4_k.rs @@ -125,61 +125,72 @@ mod tests { decode_into(&[], &mut []).unwrap(); } - #[test] - fn signed_zero_and_nonfinite_arithmetic_is_admitted() { - let mut block = [0u8; BLOCK_BYTES]; - block[4..16].copy_from_slice(&[1, 1, 1, 1, 1, 1, 1, 1, 0x11, 0x11, 0x11, 0x11]); - block[16..].fill(0x11); + fn set_scales(block: &mut [u8; BLOCK_BYTES], d: u16, min: u16) { + block[..2].copy_from_slice(&d.to_le_bytes()); + block[2..4].copy_from_slice(&min.to_le_bytes()); + } + + fn assert_signed_zero_products(block: &mut [u8; BLOCK_BYTES]) { for (d, min, expected) in [ (0u16, 0u16, 0u32), (0x8000, 0, 0x8000_0000), (0, 0x8000, 0), (0x8000, 0x8000, 0), ] { - block[..2].copy_from_slice(&d.to_le_bytes()); - block[2..4].copy_from_slice(&min.to_le_bytes()); - assert!( - decode_block(&block) - .unwrap() - .iter() - .all(|v| v.to_bits() == expected) - ); + set_scales(block, d, min); + assert!(decode_block(block).unwrap().iter().all(|v| v.to_bits() == expected)); } + } + + fn assert_infinite_scales(block: &mut [u8; BLOCK_BYTES]) { for (d, min, expected_sign) in [ (0x7c00u16, 0x3c00u16, false), (0xfc00, 0x3c00, true), (0x3c00, 0x7c00, true), (0x3c00, 0xfc00, false), ] { - block[..2].copy_from_slice(&d.to_le_bytes()); - block[2..4].copy_from_slice(&min.to_le_bytes()); + set_scales(block, d, min); let mut output = [0.0; BLOCK_ELEMENTS]; - decode_into(&block, &mut output).unwrap(); + decode_into(block, &mut output).unwrap(); assert!( output .iter() .all(|v| v.is_infinite() && v.is_sign_negative() == expected_sign) ); } + } + + fn assert_nan_scales(block: &mut [u8; BLOCK_BYTES]) { for (d, min) in [ (0x7c00u16, 0x7c00u16), (0xfc00, 0xfc00), (0x7c01, 0x3c00), (0x3c00, 0xfc01), ] { - block[..2].copy_from_slice(&d.to_le_bytes()); - block[2..4].copy_from_slice(&min.to_le_bytes()); - assert!(decode_block(&block).unwrap().iter().all(|v| v.is_nan())); + set_scales(block, d, min); + assert!(decode_block(block).unwrap().iter().all(|v| v.is_nan())); } - block[..2].copy_from_slice(&0x7c00u16.to_le_bytes()); - block[2..4].fill(0); + } + + fn assert_nan_from_zero_factors(block: &mut [u8; BLOCK_BYTES]) { + set_scales(block, 0x7c00, 0); block[16..].fill(0); // Infinity times quant zero. - assert!(decode_block(&block).unwrap().iter().all(|v| v.is_nan())); + assert!(decode_block(block).unwrap().iter().all(|v| v.is_nan())); block[16..].fill(0x11); block[4..16].fill(0); // Infinity times scale zero. - assert!(decode_block(&block).unwrap().iter().all(|v| v.is_nan())); - block[..2].copy_from_slice(&0x3c00u16.to_le_bytes()); - block[2..4].copy_from_slice(&0x7c00u16.to_le_bytes()); // Infinite minimum times zero. - assert!(decode_block(&block).unwrap().iter().all(|v| v.is_nan())); + assert!(decode_block(block).unwrap().iter().all(|v| v.is_nan())); + set_scales(block, 0x3c00, 0x7c00); // Infinite minimum times zero. + assert!(decode_block(block).unwrap().iter().all(|v| v.is_nan())); + } + + #[test] + fn signed_zero_and_nonfinite_arithmetic_is_admitted() { + let mut block = [0u8; BLOCK_BYTES]; + block[4..16].copy_from_slice(&[1, 1, 1, 1, 1, 1, 1, 1, 0x11, 0x11, 0x11, 0x11]); + block[16..].fill(0x11); + assert_signed_zero_products(&mut block); + assert_infinite_scales(&mut block); + assert_nan_scales(&mut block); + assert_nan_from_zero_factors(&mut block); } } diff --git a/crates/quant-core/src/q5_k.rs b/crates/quant-core/src/q5_k.rs index e0b117ee..1070ee6a 100644 --- a/crates/quant-core/src/q5_k.rs +++ b/crates/quant-core/src/q5_k.rs @@ -147,61 +147,72 @@ mod tests { decode_into(&[], &mut []).unwrap(); } - #[test] - fn signed_zero_and_nonfinite_arithmetic_is_admitted() { - let mut block = [0u8; BLOCK_BYTES]; - block[4..16].copy_from_slice(&[1, 1, 1, 1, 1, 1, 1, 1, 0x11, 0x11, 0x11, 0x11]); - block[48..].fill(0x11); + fn set_scales(block: &mut [u8; BLOCK_BYTES], d: u16, min: u16) { + block[..2].copy_from_slice(&d.to_le_bytes()); + block[2..4].copy_from_slice(&min.to_le_bytes()); + } + + fn assert_signed_zero_products(block: &mut [u8; BLOCK_BYTES]) { for (d, min, expected) in [ (0u16, 0u16, 0u32), (0x8000, 0, 0x8000_0000), (0, 0x8000, 0), (0x8000, 0x8000, 0), ] { - block[..2].copy_from_slice(&d.to_le_bytes()); - block[2..4].copy_from_slice(&min.to_le_bytes()); - assert!( - decode_block(&block) - .unwrap() - .iter() - .all(|v| v.to_bits() == expected) - ); + set_scales(block, d, min); + assert!(decode_block(block).unwrap().iter().all(|v| v.to_bits() == expected)); } + } + + fn assert_infinite_scales(block: &mut [u8; BLOCK_BYTES]) { for (d, min, expected_sign) in [ (0x7c00u16, 0x3c00u16, false), (0xfc00, 0x3c00, true), (0x3c00, 0x7c00, true), (0x3c00, 0xfc00, false), ] { - block[..2].copy_from_slice(&d.to_le_bytes()); - block[2..4].copy_from_slice(&min.to_le_bytes()); + set_scales(block, d, min); let mut output = [0.0; BLOCK_ELEMENTS]; - decode_into(&block, &mut output).unwrap(); + decode_into(block, &mut output).unwrap(); assert!( output .iter() .all(|v| v.is_infinite() && v.is_sign_negative() == expected_sign) ); } + } + + fn assert_nan_scales(block: &mut [u8; BLOCK_BYTES]) { for (d, min) in [ (0x7c00u16, 0x7c00u16), (0xfc00, 0xfc00), (0x7c01, 0x3c00), (0x3c00, 0xfc01), ] { - block[..2].copy_from_slice(&d.to_le_bytes()); - block[2..4].copy_from_slice(&min.to_le_bytes()); - assert!(decode_block(&block).unwrap().iter().all(|v| v.is_nan())); + set_scales(block, d, min); + assert!(decode_block(block).unwrap().iter().all(|v| v.is_nan())); } - block[..2].copy_from_slice(&0x7c00u16.to_le_bytes()); - block[2..4].fill(0); + } + + fn assert_nan_from_zero_factors(block: &mut [u8; BLOCK_BYTES]) { + set_scales(block, 0x7c00, 0); block[48..].fill(0); // Infinity times quant zero. - assert!(decode_block(&block).unwrap().iter().all(|v| v.is_nan())); + assert!(decode_block(block).unwrap().iter().all(|v| v.is_nan())); block[48..].fill(0x11); block[4..16].fill(0); // Infinity times scale zero. - assert!(decode_block(&block).unwrap().iter().all(|v| v.is_nan())); - block[..2].copy_from_slice(&0x3c00u16.to_le_bytes()); - block[2..4].copy_from_slice(&0x7c00u16.to_le_bytes()); // Infinite minimum times zero. - assert!(decode_block(&block).unwrap().iter().all(|v| v.is_nan())); + assert!(decode_block(block).unwrap().iter().all(|v| v.is_nan())); + set_scales(block, 0x3c00, 0x7c00); // Infinite minimum times zero. + assert!(decode_block(block).unwrap().iter().all(|v| v.is_nan())); + } + + #[test] + fn signed_zero_and_nonfinite_arithmetic_is_admitted() { + let mut block = [0u8; BLOCK_BYTES]; + block[4..16].copy_from_slice(&[1, 1, 1, 1, 1, 1, 1, 1, 0x11, 0x11, 0x11, 0x11]); + block[48..].fill(0x11); + assert_signed_zero_products(&mut block); + assert_infinite_scales(&mut block); + assert_nan_scales(&mut block); + assert_nan_from_zero_factors(&mut block); } } diff --git a/crates/quant-core/src/q6_k.rs b/crates/quant-core/src/q6_k.rs index 4ce7c12a..47ed8fa5 100644 --- a/crates/quant-core/src/q6_k.rs +++ b/crates/quant-core/src/q6_k.rs @@ -125,6 +125,25 @@ mod tests { } } + fn assert_bad_shapes_rejected(source: &[u8], output: &mut [f32; 2 * BLOCK_ELEMENTS]) { + for length in [0, 1, BLOCK_BYTES - 1, BLOCK_BYTES + 1, source.len() - 1] { + output.fill(123.0); + assert!(decode_into(&source[..length], output).is_err()); + assert_eq!(*output, [123.0; 2 * BLOCK_ELEMENTS]); + } + for length in [ + 0, + BLOCK_ELEMENTS - 1, + BLOCK_ELEMENTS + 1, + 2 * BLOCK_ELEMENTS, + ] { + output.fill(123.0); + assert!(decode_into(&source[..BLOCK_BYTES], &mut output[..length]).is_err()); + assert_eq!(*output, [123.0; 2 * BLOCK_ELEMENTS]); + } + decode_into(&[], &mut []).unwrap(); + } + #[test] fn unaligned_bulk_and_invalid_shapes_preserve_contract() { let block = known_block(0x3800); @@ -140,24 +159,8 @@ mod tests { for (value, expected) in output.iter().zip(expected.into_iter().cycle()) { assert_eq!(value.to_bits(), expected.to_bits()); } - for length in [0, 1, BLOCK_BYTES - 1, BLOCK_BYTES + 1, source.len() - 1] { - output.fill(123.0); - assert!(decode_into(&source[..length], &mut output).is_err()); - assert_eq!(output, [123.0; 2 * BLOCK_ELEMENTS]); - } - for length in [ - 0, - BLOCK_ELEMENTS - 1, - BLOCK_ELEMENTS + 1, - 2 * BLOCK_ELEMENTS, - ] { - output.fill(123.0); - assert!(decode_into(&source[..BLOCK_BYTES], &mut output[..length]).is_err()); - assert_eq!(output, [123.0; 2 * BLOCK_ELEMENTS]); - } - decode_into(&[], &mut []).unwrap(); + assert_bad_shapes_rejected(source, &mut output); } - #[test] fn signed_zero_preserves_signed_scale_and_quant_multiplications() { let signs = [ @@ -177,47 +180,50 @@ mod tests { } } + const GROUP_NEGATIVE: [bool; 16] = [ + false, false, false, true, false, false, false, false, true, true, true, false, true, true, + true, true, + ]; + + fn assert_nonfinite_groups(scale: u16, block: &[u8; BLOCK_BYTES]) { + let mut output = [0.0; BLOCK_ELEMENTS]; + decode_into(block, &mut output).unwrap(); + for (index, (group, negative)) in output + .as_chunks::<16>() + .0 + .iter() + .zip(GROUP_NEGATIVE) + .enumerate() + { + if scale & 0x03ff != 0 || index == 3 { + assert!(group.iter().all(|value| value.is_nan())); + } else { + let sign = negative ^ (scale & 0x8000 != 0); + assert!( + group + .iter() + .all(|value| value.is_infinite() && value.is_sign_negative() == sign) + ); + } + } + } + + fn assert_zero_factors_are_nan(block: [u8; BLOCK_BYTES]) { + let mut zero_quant = block; + zero_quant[..128].fill(0); + zero_quant[128..192].fill(0xaa); // Every six-bit code is 32, hence quant zero. + assert!(decode_block(&zero_quant).unwrap().iter().all(|value| value.is_nan())); + let mut zero_subscale = block; + zero_subscale[192..208].fill(0); + assert!(decode_block(&zero_subscale).unwrap().iter().all(|value| value.is_nan())); + } + #[test] fn nonfinite_scales_zero_subscales_and_zero_quants_are_admitted() { - let signs = [ - false, false, false, true, false, false, false, false, true, true, true, false, true, - true, true, true, - ]; for scale in [0x7c00u16, 0xfc00, 0x7c01, 0xfc01, 0x7e00, 0xfe00] { let block = known_block(scale); - let mut output = [0.0; BLOCK_ELEMENTS]; - decode_into(&block, &mut output).unwrap(); - for (index, (group, negative)) in - output.as_chunks::<16>().0.iter().zip(signs).enumerate() - { - if scale & 0x03ff != 0 || index == 3 { - assert!(group.iter().all(|value| value.is_nan())); - } else { - let sign = negative ^ (scale & 0x8000 != 0); - assert!( - group - .iter() - .all(|value| value.is_infinite() && value.is_sign_negative() == sign) - ); - } - } - let mut zero_quant = block; - zero_quant[..128].fill(0); - zero_quant[128..192].fill(0xaa); // Every six-bit code is 32, hence quant zero. - assert!( - decode_block(&zero_quant) - .unwrap() - .iter() - .all(|value| value.is_nan()) - ); - let mut zero_subscale = block; - zero_subscale[192..208].fill(0); - assert!( - decode_block(&zero_subscale) - .unwrap() - .iter() - .all(|value| value.is_nan()) - ); + assert_nonfinite_groups(scale, &block); + assert_zero_factors_are_nan(block); } } } diff --git a/crates/quant-core/src/q8_0.rs b/crates/quant-core/src/q8_0.rs index ba9f3fdd..efc6f5a9 100644 --- a/crates/quant-core/src/q8_0.rs +++ b/crates/quant-core/src/q8_0.rs @@ -80,45 +80,44 @@ mod tests { decode_into(&[], &mut []).unwrap(); } - #[test] - fn signed_zero_and_infinity_times_zero_preserve_value_policy() { - let mut block = [0u8; BLOCK_BYTES]; - block[3] = 255; - block[4] = 1; + fn assert_signed_zero_scales(block: &mut [u8; BLOCK_BYTES]) { for (scale, zero, negative, positive) in [ (0x0000u16, 0, 0x8000_0000, 0), (0x8000, 0x8000_0000, 0, 0x8000_0000), ] { block[..2].copy_from_slice(&scale.to_le_bytes()); - let output = decode_block(&block).unwrap(); + let output = decode_block(block).unwrap(); assert_eq!(output[0].to_bits(), zero); assert_eq!(output[1].to_bits(), negative); assert_eq!(output[2].to_bits(), positive); } + } + + fn assert_infinite_scale_edges(scale: u16, output: &[f32]) { + if scale & 0x03ff != 0 { + assert!(output.iter().all(|value| value.is_nan())); + return; + } + let (negative, positive) = if scale == 0x7c00 { + (0xff80_0000, 0x7f80_0000) + } else { + (0x7f80_0000, 0xff80_0000) + }; + assert_eq!(output[1].to_bits(), negative); + assert_eq!(output[2].to_bits(), positive); + } + + #[test] + fn signed_zero_and_infinity_times_zero_preserve_value_policy() { + let mut block = [0u8; BLOCK_BYTES]; + block[3] = 255; + block[4] = 1; + assert_signed_zero_scales(&mut block); for scale in [0x7c00u16, 0xfc00, 0x7c01, 0xfc01] { block[..2].copy_from_slice(&scale.to_le_bytes()); let output = decode_block(&block).unwrap(); assert!(output[0].is_nan()); - if scale & 0x03ff == 0 { - assert_eq!( - output[1].to_bits(), - if scale == 0x7c00 { - 0xff80_0000 - } else { - 0x7f80_0000 - } - ); - assert_eq!( - output[2].to_bits(), - if scale == 0x7c00 { - 0x7f80_0000 - } else { - 0xff80_0000 - } - ); - } else { - assert!(output.iter().all(|value| value.is_nan())); - } + assert_infinite_scale_edges(scale, &output); } } } diff --git a/crates/sampler-ref/src/coupled.rs b/crates/sampler-ref/src/coupled.rs index 8476af45..864c555d 100644 --- a/crates/sampler-ref/src/coupled.rs +++ b/crates/sampler-ref/src/coupled.rs @@ -128,14 +128,9 @@ pub fn coupled_penalize( let nv = logits.len(); let mut seen = vec![false; nv]; for &v in hrow { - if v < 0 || v as usize >= id_vocab { + let Some(s) = subset_slot(v, id_to_sub, id_vocab, nv) else { continue; - } - let s = id_to_sub.map_or(v, |m| m[v as usize]); - if s < 0 || s as usize >= nv { - continue; - } - let s = s as usize; + }; if std::mem::replace(&mut seen[s], true) { continue; } @@ -144,6 +139,15 @@ pub fn coupled_penalize( } } +/// Subset index of history token `v`, or `None` when it is outside the vocabulary or the subset. +fn subset_slot(v: i32, id_to_sub: Option<&[i32]>, id_vocab: usize, nv: usize) -> Option { + if v < 0 || v as usize >= id_vocab { + return None; + } + let s = id_to_sub.map_or(v, |m| m[v as usize]); + (s >= 0 && (s as usize) < nv).then_some(s as usize) +} + /// `coupled_draft_sample`: draft `j` of the chain from one row of `nv` logits. /// Penalises `logits` in place, selects the request's top_k, then runs the /// target's tail with the Philox counter `coupled_draft_counter(step_rec0)`. diff --git a/crates/sampler-ref/src/params.rs b/crates/sampler-ref/src/params.rs index bac6c101..175bbfaa 100644 --- a/crates/sampler-ref/src/params.rs +++ b/crates/sampler-ref/src/params.rs @@ -282,18 +282,15 @@ impl SamplingParams { } else { 0.0 }; - let rp = request.repetition_penalty.filter(|v| *v != 1.0); - let pf = request.frequency_penalty.filter(|v| *v != 0.0); - let pp = request.presence_penalty.filter(|v| *v != 0.0); let penalty_last_n = Self::resolve_last_n(request); Ok(Self { temperature, top_p, top_k, min_p, - penalty_repeat: rp.map_or(1.0, wire_f32), - penalty_freq: pf.map_or(0.0, wire_f32), - penalty_present: pp.map_or(0.0, wire_f32), + penalty_repeat: request.repetition_penalty.filter(|v| *v != 1.0).map_or(1.0, wire_f32), + penalty_freq: request.frequency_penalty.filter(|v| *v != 0.0).map_or(0.0, wire_f32), + penalty_present: request.presence_penalty.filter(|v| *v != 0.0).map_or(0.0, wire_f32), penalty_last_n, seed: request.seed.and_then(NonZeroU64::new), })