use burn::tensor::Tensor;
pub(crate) fn matmul(lhs: Tensor<3>, rhs: Tensor<3>) -> Tensor<3> {
#[cfg(feature = "flex")]
if lhs.device() == burn::tensor::Device::flex() {
return lhs.matmul(rhs);
}
let columns = rhs.dims()[2];
let rhs = rhs.transpose();
let outputs = (0..columns)
.map(|column| {
let vector = rhs.clone().slice_dim(1, column..column + 1);
(lhs.clone() * vector).sum_dim(2)
})
.collect();
Tensor::cat(outputs, 2)
}
pub(crate) fn matmul2(lhs: Tensor<2>, rhs: Tensor<2>) -> Tensor<2> {
let [m, k] = lhs.dims();
let [_, n] = rhs.dims();
matmul(lhs.reshape([1, m, k]), rhs.reshape([1, k, n])).reshape([m, n])
}