#![allow(unused)]
use crate::{DType, graph::Graph, kernel::OpId, shape::Dim};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct MatMul {
pub(crate) a: OpId,
pub(crate) b: OpId,
pub(crate) out: OpId,
pub(crate) m: Dim,
pub(crate) n: Dim,
pub(crate) k: Dim,
pub(crate) acc_dtype: DType,
pub(crate) in_dtype: DType,
}
impl Graph {
pub(crate) fn match_matmul(&self, cid: OpId) -> Option<MatMul> {
let out_shape = self.const_shape(cid)?;
if out_shape.len() != 2 {
return None;
}
let [m, n] = [out_shape[0], out_shape[1]];
let (prod, k) = self.reduce_add_last(cid)?;
let (ea, eb) = self.mul_of(prod)?;
let (a, a3) = self.expand_src(ea)?;
let (bt, b3) = self.expand_src(eb)?;
if a3 != [m, 1, k] || b3 != [1, n, k] {
return None;
}
let b = self.transpose_src(bt)?;
if self.const_shape(b).as_deref() != Some(&[k, n][..]) {
return None;
}
let in_dtype = self.dtype(b);
if self.dtype(a) != in_dtype {
return None;
}
Some(MatMul { a, b, out: cid, m, n, k, acc_dtype: self.dtype(cid), in_dtype })
}
}