Skip to content

Commit

Permalink
fix: WIP unit test migration to suite-unit
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienBalianSonos committed Feb 21, 2024
1 parent 819fbc8 commit 221f869
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 184 deletions.
2 changes: 0 additions & 2 deletions core/src/ops/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,6 @@ macro_rules! bin_to_super_type {
c_dt: &DatumType,
accumulator_dt: DatumType
) -> TractResult<Option<Tensor>> {
dbg!("generic call",a, b, c_dt, a.datum_type().qparams(), b.datum_type().qparams(), c_dt.qparams());
if let (Some(QParams::ZpScale {zero_point: a_zp, scale: a_scale}),
Some(QParams::ZpScale {zero_point: b_zp, scale: b_scale}),
Some(QParams::ZpScale {zero_point: c_zp, scale: c_scale})) =
Expand Down Expand Up @@ -625,7 +624,6 @@ macro_rules! bin_to_super_type {
if let Some(d) = generic_q_binary_as_float_op(a, b, c_dt, DatumType::F32)? {
return Ok(Some(d));
}
dbg!("HU nobodoy called me, smell bad ...");
)?
Ok(None)
}
Expand Down
176 changes: 0 additions & 176 deletions core/src/ops/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,182 +722,6 @@ mod tests {
Ok(())
}

struct TestOpWithQU8 {
operator: crate::ops::binary::TypedBinOp,
tensor_mul_input_a: [u8; 4],
scalar_mul_input_b: u8,
output_qparams: QParams,
expected_output: [u8; 4],
a_qparams: Option<QParams>,
b_qparams: Option<QParams>,
}
impl TestOpWithQU8 {
fn check(&self) -> TractResult<()> {
let mut model = TypedModel::default();

let a_dt = DatumType::QU8(if let Some(a_qp) = self.a_qparams {
a_qp
} else {
self.output_qparams
});

let b_dt = DatumType::QU8(if let Some(b_qp) = self.b_qparams {
b_qp
} else {
self.output_qparams
});

let a = model.add_source("a", TypedFact::dt_shape(a_dt, [2_usize, 2]))?;

let mut b_tensor = tensor0(self.scalar_mul_input_b).broadcast_into_rank(2)?;
unsafe { b_tensor.set_datum_type(b_dt) };
let b = model.add_const("b", b_tensor.into_arc_tensor())?;

// we need to wire correctly output to the mul {
let mut op = self.operator.clone();
op.1 = Some(DatumType::QU8(self.output_qparams));
// }
let c = model.wire_node("c", op, &[a, b])?[0];
model.set_output_outlets(&[c])?;

let mut a_data = Tensor::from_shape(&[2, 2], &self.tensor_mul_input_a)?;
unsafe { a_data.set_datum_type(a_dt) };

let result = SimplePlan::new(&model)?.run(tvec!(a_data.into()))?;
let arr = result[0].to_array_view::<u8>()?;
assert_eq!(
arr,
Tensor::from_shape(&[2, 2], &self.expected_output)?.to_array_view::<u8>()?
);
Ok(())
}
}

#[test]
fn mul_as_qu8_overflow_clamp() -> TractResult<()> {
// last value in output tensor overflow hence is clamped
TestOpWithQU8 {
operator: mul(),
tensor_mul_input_a: [1_u8, 2, 3, 128],
scalar_mul_input_b: 4_u8,
output_qparams: QParams::ZpScale { scale: 1., zero_point: 0 },
a_qparams: None, // aligned with output_qparams
b_qparams: None, // aligned with output_qparams
expected_output: [4_u8, 8, 12, 255],
}
.check()
}

#[test]
fn mul_as_qu8_non_neutral_scale_and_offset() -> TractResult<()> {
// attempt with non neutral scale and offset
TestOpWithQU8 {
operator: mul(),
tensor_mul_input_a: [1_u8, 2, 3, 128], // real: -3, 0, 3, 378
scalar_mul_input_b: 4_u8, // real: 6
output_qparams: QParams::ZpScale { scale: 3., zero_point: 2 },
// optima in non quantized output real: -18, 0, 18, 2268
a_qparams: None, // aligned with output_qparams
b_qparams: None, // aligned with output_qparams
expected_output: [0_u8, 2, 8, 255], // approx obtained real: -6, 0, 18, 759
}
.check()
}

#[test]
fn mul_as_qu8_non_aligned_scale_and_offset() -> TractResult<()> {
// attempt with all scale and offset not aligned
TestOpWithQU8 {
operator: mul(),
tensor_mul_input_a: [3_u8, 4, 10, 25], // real: 0, 4.5, 31.5, 99
scalar_mul_input_b: 6_u8, // real: 5
output_qparams: QParams::ZpScale { scale: 1., zero_point: 0 },
a_qparams: Some(QParams::ZpScale { scale: 4.5, zero_point: 3 }),
b_qparams: Some(QParams::ZpScale { scale: 2.5, zero_point: 4 }),
// optima in non quantized output real: 0, 22.5, 157.5, 495
expected_output: [0_u8, 22, 158, 255],
}
.check()
}

#[test]
fn add_as_qu8_non_aligned_scale_and_offset() -> TractResult<()> {
// attempt with all scale and offset not aligned
TestOpWithQU8 {
operator: add(),
tensor_mul_input_a: [3_u8, 4, 10, 25], // real: 0, 4.5, 31.5, 99
scalar_mul_input_b: 6_u8, // real: 5
output_qparams: QParams::ZpScale { scale: 1., zero_point: 0 },
a_qparams: Some(QParams::ZpScale { scale: 4.5, zero_point: 3 }),
b_qparams: Some(QParams::ZpScale { scale: 2.5, zero_point: 4 }),
// optima in non quantized output real: 5, 9.5, 36.5, 104
expected_output: [5_u8, 9, 37, 104],
}
.check()
}

#[test]
fn div_as_qu8_non_aligned_scale_and_offset() -> TractResult<()> {
// attempt with all scale and offset not aligned
TestOpWithQU8 {
operator: div(),
tensor_mul_input_a: [3_u8, 5, 10, 25], // real: 0, 9, 31.5, 99
scalar_mul_input_b: 6_u8, // real: 5
output_qparams: QParams::ZpScale { scale: 1., zero_point: 0 },
a_qparams: Some(QParams::ZpScale { scale: 4.5, zero_point: 3 }),
b_qparams: Some(QParams::ZpScale { scale: 2.5, zero_point: 4 }),
// optima in non quantized output real: 0, 1.8, 6.3, 19.8
expected_output: [0_u8, 2, 5, 20],
}
.check()
}

#[test]
fn max_0_as_qu8_non_aligned_scale_and_offset() -> TractResult<()> {
// relu in qu8
TestOpWithQU8 {
operator: max(),
tensor_mul_input_a: [100_u8, 5, 110, 99], // real: 0, −427.5, 45, -4.5
scalar_mul_input_b: 100_u8, // real: 0
output_qparams: QParams::ZpScale { scale: 1., zero_point: 0 },
a_qparams: Some(QParams::ZpScale { scale: 4.5, zero_point: 100 }),
b_qparams: Some(QParams::ZpScale { scale: 4.5, zero_point: 100 }),
// optima in non quantized output real: 0, 0, 45, 0
expected_output: [0_u8, 0, 45, 0],
}
.check()
}

#[test]
fn max_15_as_qu8_non_aligned_scale_and_offset() -> TractResult<()> {
TestOpWithQU8 {
operator: max(),
tensor_mul_input_a: [5_u8, 9, 8, 20], // real: 0, 16, 12, 60
scalar_mul_input_b: 15_u8, // real: 15
output_qparams: QParams::ZpScale { scale: 1., zero_point: 0 },
a_qparams: Some(QParams::ZpScale { scale: 4., zero_point: 5 }),
b_qparams: Some(QParams::ZpScale { scale: 3., zero_point: 10 }),
// optima in non quantized output real: 15, 16, 15, 60
expected_output: [15_u8, 16, 15, 60],
}
.check()
}

#[test]
fn min_15_as_qu8_non_aligned_scale_and_offset() -> TractResult<()> {
TestOpWithQU8 {
operator: min(),
tensor_mul_input_a: [5_u8, 9, 8, 20], // real: 0, 16, 12, 60
scalar_mul_input_b: 15_u8, // real: 15
output_qparams: QParams::ZpScale { scale: 1., zero_point: 0 },
a_qparams: Some(QParams::ZpScale { scale: 4., zero_point: 5 }),
b_qparams: Some(QParams::ZpScale { scale: 3., zero_point: 10 }),
// optima in non quantized output real: 0, 15, 12, 15
expected_output: [0_u8, 15, 12, 15],
}
.check()
}

#[test]
fn div_as_shift() -> TractResult<()> {
let mut model = TypedModel::default();
Expand Down
Loading

0 comments on commit 221f869

Please sign in to comment.