Skip to main content

qualia_core_db/inference/moe/
nvfp4.rs

1//! Compatibility exports for the canonical Etalon numerical implementation.
2pub use qualia_inference_kernel::moe::nvfp4::*;
3
4#[cfg(test)]
5mod tests {
6    use super::*;
7
8    #[test]
9    fn test_fp8_e4m3_conversion() {
10        // Bias is 7.
11        // 0x38 = 0b00111000 -> sign 0, exp 7 (7-7=0, 2^0=1.0), mantissa 0 -> 1.0
12        assert_eq!(fp8_e4m3_to_f32(0x38), 1.0);
13        // 0x40 = 0b01000000 -> sign 0, exp 8 (8-7=1, 2^1=2.0), mantissa 0 -> 2.0
14        assert_eq!(fp8_e4m3_to_f32(0x40), 2.0);
15        // 0xB8 = 0b10111000 -> sign 1 -> -1.0
16        assert_eq!(fp8_e4m3_to_f32(0xB8), -1.0);
17    }
18
19    #[test]
20    fn test_dequantize_nvfp4_block() {
21        let mut packed = [0u8; 8];
22        // Byte 0: low nibble = 2 (1.0), high nibble = 4 (2.0)
23        packed[0] = 0x42;
24        // Byte 1: low nibble = 7 (6.0), high nibble = 10 (-1.0)
25        packed[1] = 0xA7;
26
27        let scale = 0x38; // 1.0
28        let global = 1.0f32;
29        let mut out = [0.0f32; 16];
30
31        dequantize_nvfp4_block(&packed, scale, global, &mut out);
32
33        assert_eq!(out[0], 1.0);
34        assert_eq!(out[1], 2.0);
35        assert_eq!(out[2], 6.0);
36        assert_eq!(out[3], -1.0);
37        assert_eq!(out[4], 0.0);
38    }
39
40    #[test]
41    fn test_nvfp4_gemv_zero_heap() {
42        // 2 rows, 16 cols (1 block per row)
43        let mut packed = [0u8; 16];
44        // Row 0, byte 0: low = 2 (1.0), high = 4 (2.0)
45        packed[0] = 0x42;
46        // Row 1, byte 0: low = 3 (1.5), high = 5 (3.0)
47        packed[8] = 0x53;
48
49        let scales = [0x38, 0x38]; // 1.0, 1.0
50        let mut input = [0.0f32; 16];
51        input[0] = 10.0;
52        input[1] = 5.0;
53
54        let mut out = [0.0f32; 2];
55        nvfp4_gemv_zero_heap(&input, &packed, &scales, 1.0, 2, 16, &mut out).unwrap();
56
57        // Row 0 dot product: 1.0 * 10.0 + 2.0 * 5.0 = 20.0
58        assert_eq!(out[0], 20.0);
59        // Row 1 dot product: 1.5 * 10.0 + 3.0 * 5.0 = 30.0
60        assert_eq!(out[1], 30.0);
61    }
62}