#![cfg(feature = "nn-linear-4bit")]
use vyre::ir::{Node, Program};
use vyre_libs::nn::quant::{
int4_batched_matmul_f32_scaled, int4_batched_matmul_top1_f32_scaled,
int4_batched_matvec_f32_scaled, int4_dot_f32_scaled, int4_dot_i32, int4_matvec_f32_scaled,
};
fn assert_composition_chain(program: &Program, parent_id: &str, primitive_id: &str) {
let [Node::Region {
generator,
source_region: None,
body,
}] = program.entry()
else {
panic!("expected one Cat-A parent region");
};
assert_eq!(generator.as_ref(), parent_id);
let [Node::Region {
generator,
source_region: Some(parent),
..
}] = body.as_slice()
else {
panic!("expected one Tier-2.5 child region");
};
assert_eq!(generator.as_ref(), primitive_id);
assert_eq!(parent.name, parent_id);
}
#[test]
fn dot_wrappers_record_cat_a_parent_and_primitive_child() {
let dot = int4_dot_i32("lhs", "rhs", "out", 8);
assert_composition_chain(
&dot,
"vyre-libs::quant::int4_dot_i32",
"vyre-primitives::math::quantized::i4x8_dot_i32",
);
let scaled = int4_dot_f32_scaled("lhs", "rhs", "lhs_scale", "rhs_scale", "out", 8);
assert_composition_chain(
&scaled,
"vyre-libs::quant::int4_dot_f32_scaled",
"vyre-primitives::math::quantized::i4x8_dot_f32_scaled",
);
}
#[test]
fn matrix_wrappers_record_their_exact_primitive_children() {
let matvec = int4_matvec_f32_scaled("weights", "x", "row_scales", "out", 3, 9);
assert_composition_chain(
&matvec,
"vyre-libs::quant::int4_matvec_f32_scaled",
"vyre-primitives::math::quantized::i4x8_matvec_f32_scaled",
);
let batched_matvec =
int4_batched_matvec_f32_scaled("weights", "x", "row_scales", "out", 2, 3, 9);
assert_composition_chain(
&batched_matvec,
"vyre-libs::quant::int4_batched_matvec_f32_scaled",
"vyre-primitives::math::quantized::i4x8_batched_matvec_f32_scaled",
);
let batched_matmul = int4_batched_matmul_f32_scaled(
"weights",
"activations",
"row_scales",
"batch_scales",
"out",
2,
3,
9,
);
assert_composition_chain(
&batched_matmul,
"vyre-libs::quant::int4_batched_matmul_f32_scaled",
"vyre-primitives::math::quantized::i4x8_batched_matmul_f32_scaled",
);
let top1 = int4_batched_matmul_top1_f32_scaled(
"weights",
"activations",
"row_scales",
"batch_scales",
"out",
2,
3,
9,
);
assert_composition_chain(
&top1,
"vyre-libs::quant::int4_batched_matmul_top1_f32_scaled",
"vyre-primitives::math::quantized::i4x8_batched_matmul_top1_f32_scaled",
);
}
#[test]
fn zero_lane_dot_keeps_composition_chain_and_trap() {
let program = int4_dot_i32("lhs", "rhs", "out", 0);
assert_composition_chain(
&program,
"vyre-libs::quant::int4_dot_i32",
"vyre-primitives::math::quantized::i4x8_dot_i32",
);
assert!(program.stats().trap());
}