use crate::dtype::DType;
use crate::error::Result;
use crate::runtime::cpu::kernels::{matmul_i8_to_i32_bias_kernel, matmul_i8_to_i32_kernel};
use crate::runtime::cpu::{CpuClient, CpuRuntime};
use crate::tensor::Tensor;
#[allow(clippy::too_many_arguments)]
pub(super) fn matmul_i8_i32(
client: &CpuClient,
a: &Tensor<CpuRuntime>,
b: &Tensor<CpuRuntime>,
bias: Option<&Tensor<CpuRuntime>>,
out_shape: &[usize],
a_batch_idx: &[usize],
b_batch_idx: &[usize],
m: usize,
n: usize,
k: usize,
) -> Result<Tensor<CpuRuntime>> {
let out = Tensor::<CpuRuntime>::empty(out_shape, DType::I32, &client.device)?;
let a_addr = a.ptr();
let b_addr = b.ptr();
let out_addr = out.ptr();
let bias_addr = bias.map(|t| t.ptr());
let (lda, ldb, ldc) = (k, n, n);
let batch_size = a_batch_idx.len();
let run_batch = |batch: usize| unsafe {
let a_ptr = a_addr as *const i8;
let b_ptr = b_addr as *const i8;
let out_ptr = out_addr as *mut i32;
let a_off = a_batch_idx[batch] * m * k;
let b_off = b_batch_idx[batch] * k * n;
let out_off = batch * m * n;
match bias_addr {
Some(bias_addr) => matmul_i8_to_i32_bias_kernel(
a_ptr.add(a_off),
b_ptr.add(b_off),
bias_addr as *const i32,
out_ptr.add(out_off),
m,
n,
k,
lda,
ldb,
ldc,
),
None => matmul_i8_to_i32_kernel(
a_ptr.add(a_off),
b_ptr.add(b_off),
out_ptr.add(out_off),
m,
n,
k,
lda,
ldb,
ldc,
),
}
};
#[cfg(feature = "rayon")]
{
use rayon::prelude::*;
if batch_size > 1 {
let min_len = client.rayon_min_len();
client.install_parallelism(|| {
(0..batch_size)
.into_par_iter()
.with_min_len(min_len)
.for_each(run_batch);
});
} else {
for batch in 0..batch_size {
run_batch(batch);
}
}
}
#[cfg(not(feature = "rayon"))]
{
for batch in 0..batch_size {
run_batch(batch);
}
}
Ok(out)
}
#[cfg(test)]
mod tests {
use crate::dtype::DType;
use crate::error::Error;
use crate::ops::MatmulOps;
use crate::runtime::Runtime;
use crate::runtime::cpu::{CpuClient, CpuDevice, CpuRuntime};
use crate::tensor::Tensor;
fn client() -> (CpuClient, CpuDevice) {
let device = CpuDevice::new();
let client = CpuRuntime::default_client(&device);
(client, device)
}
#[test]
fn test_matmul_bias_i8_returns_i32() {
let (c, dev) = client();
let a = Tensor::<CpuRuntime>::from_slice(&[1i8, 2, 3, 4], &[2, 2], &dev).expect("A");
let b = Tensor::<CpuRuntime>::from_slice(&[5i8, 6, 7, 8], &[2, 2], &dev).expect("B");
let bias = Tensor::<CpuRuntime>::from_slice(&[1i32, -2], &[2], &dev).expect("bias");
let out = c.matmul_bias(&a, &b, &bias).expect("matmul_bias");
assert_eq!(out.dtype(), DType::I32);
assert_eq!(out.to_vec::<i32>(), vec![20i32, 20, 44, 48]);
}
#[test]
fn test_matmul_bias_i8_accumulates_past_i8_range() {
let (c, dev) = client();
let a = Tensor::<CpuRuntime>::from_slice(&[127i8; 4], &[1, 4], &dev).expect("A");
let b = Tensor::<CpuRuntime>::from_slice(&[127i8; 4], &[4, 1], &dev).expect("B");
let bias = Tensor::<CpuRuntime>::from_slice(&[1_000i32], &[1], &dev).expect("bias");
let out = c.matmul_bias(&a, &b, &bias).expect("matmul_bias");
assert_eq!(out.dtype(), DType::I32);
assert_eq!(out.to_vec::<i32>(), vec![65_516i32]);
}
#[test]
fn test_matmul_bias_i8_batched_returns_i32() {
let (c, dev) = client();
let a = Tensor::<CpuRuntime>::from_slice(&[1i8, 2, 3, 4, 5, 6, 7, 8], &[2, 2, 2], &dev)
.expect("A");
let b = Tensor::<CpuRuntime>::from_slice(&[1i8, 0, 0, 1, 2, 0, 0, 2], &[2, 2, 2], &dev)
.expect("B");
let bias = Tensor::<CpuRuntime>::from_slice(&[1i32, -2], &[2], &dev).expect("bias");
let out = c.matmul_bias(&a, &b, &bias).expect("matmul_bias");
assert_eq!(out.dtype(), DType::I32);
assert_eq!(out.to_vec::<i32>(), vec![2i32, 0, 4, 2, 11, 10, 15, 14]);
}
#[test]
fn test_matmul_bias_i8_rejects_i8_bias() {
let (c, dev) = client();
let a = Tensor::<CpuRuntime>::from_slice(&[1i8, 2, 3, 4], &[2, 2], &dev).expect("A");
let b = Tensor::<CpuRuntime>::from_slice(&[5i8, 6, 7, 8], &[2, 2], &dev).expect("B");
let bias = Tensor::<CpuRuntime>::from_slice(&[1i8, -2], &[2], &dev).expect("bias");
match c.matmul_bias(&a, &b, &bias) {
Err(Error::InvalidArgument { arg, reason }) => {
assert_eq!(arg, "bias");
assert!(reason.contains("matmul_bias"), "reason: {reason}");
assert!(reason.contains("I32"), "reason: {reason}");
}
other => panic!("expected an I8 bias to be rejected, got {other:?}"),
}
}
}