#![allow(unused)]
use crate::{
graph::{Graph, Node},
kernel::{BOp, OpId},
shape::Dim,
};
mod matmul;
impl Graph {
fn const_shape(&self, cid: OpId) -> Option<Vec<Dim>> {
self.shape(cid)
.into_iter()
.map(|dim| match &self.nodes[dim].node {
Node::Const { value: c, .. } => c.as_dim(),
_ => None,
})
.collect()
}
fn reduce_add_last(&self, cid: OpId) -> Option<(OpId, Dim)> {
self.class_nodes(cid).find_map(|nid| match &self.nodes[nid].node {
Node::Reduce { x, rop: BOp::Add, axes } => {
let prod_shape = self.const_shape(*x)?;
if prod_shape.len() == 3 && axes.len() == 1 && axes[0] == prod_shape.len() - 1 {
Some((*x, prod_shape[2]))
} else {
None
}
}
_ => None,
})
}
fn mul_of(&self, cid: OpId) -> Option<(OpId, OpId)> {
if let Some((x, y)) = self.class_nodes(cid).find_map(|nid| match &self.nodes[nid].node {
Node::Binary { x, y, bop: BOp::Mul } => Some((*x, *y)),
_ => None,
}) {
return Some((x, y));
}
let x = self.class_nodes(cid).find_map(|nid| match &self.nodes[nid].node {
Node::Cast { x, .. } => Some(*x),
_ => None,
})?;
self.class_nodes(x).find_map(|nid| match &self.nodes[nid].node {
Node::Binary { x, y, bop: BOp::Mul } => Some((*x, *y)),
_ => None,
})
}
fn expand_src(&self, cid: OpId) -> Option<(OpId, Vec<Dim>)> {
let x = self.class_nodes(cid).find_map(|nid| match &self.nodes[nid].node {
Node::Expand { x, .. } => Some(*x),
_ => None,
})?;
Some((x, self.const_shape(x)?))
}
fn transpose_src(&self, cid: OpId) -> Option<OpId> {
self.class_nodes(cid).find_map(|nid| match &self.nodes[nid].node {
Node::Reshape { x, .. } => self.transpose_src(*x),
Node::Permute { x, axes } if axes.len() == 2 && axes[0] == 1 && axes[1] == 0 => Some(*x),
_ => None,
})
}
}