use crate::{
graph::{ClassId, Graph, Node},
kernel::BOp,
runtime::ShapeId,
shape::Dim,
slab::Slab,
};
mod matmul;
impl Graph {
fn reduce_add_last(&self, cid: ClassId, shapes: &Slab<ShapeId, Vec<Dim>>) -> Option<(ClassId, Dim)> {
self.classes[cid].nodes.iter().find_map(|&nid| match &self.nodes[nid].node {
Node::Reduce { x, bop: BOp::Add, axes } => {
let prod_shape = &shapes[self.classes[*x].shape];
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: ClassId) -> Option<(ClassId, ClassId)> {
if let Some((x, y)) = self.classes[cid].nodes.iter().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.classes[cid].nodes.iter().find_map(|&nid| match &self.nodes[nid].node {
Node::Cast { x, .. } => Some(*x),
_ => None,
})?;
self.classes[x].nodes.iter().find_map(|&nid| match &self.nodes[nid].node {
Node::Binary { x, y, bop: BOp::Mul } => Some((*x, *y)),
_ => None,
})
}
fn expand_src(&self, cid: ClassId, shapes: &Slab<ShapeId, Vec<Dim>>) -> Option<(ClassId, Vec<Dim>)> {
let x = self.classes[cid].nodes.iter().find_map(|&nid| match &self.nodes[nid].node {
Node::Expand { x, .. } => Some(*x),
_ => None,
})?;
Some((x, shapes[self.classes[x].shape].clone()))
}
fn transpose_src(&self, cid: ClassId) -> Option<ClassId> {
self.classes[cid].nodes.iter().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,
})
}
}