feat(math): introduce matmul operator
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
#define CENET_MATH_EXPR_HPP
|
||||
|
||||
#include "ops.hpp"
|
||||
#include "type_traits.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
@@ -43,9 +44,6 @@ struct binary_expression : expression_tag {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <typename T>
|
||||
using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>;
|
||||
|
||||
template <typename T, typename = void>
|
||||
struct operand {
|
||||
using type = T;
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
#ifndef CENET_MATH_OPS_HPP
|
||||
#define CENET_MATH_OPS_HPP
|
||||
|
||||
#include "tensor_fwd.hpp"
|
||||
#include "tensor_shape.hpp"
|
||||
#include "type_traits.hpp"
|
||||
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
@@ -25,7 +27,7 @@ struct has_shape<T, std::void_t<decltype(std::declval<T>().shape())>> : std::tru
|
||||
template <typename Op>
|
||||
struct generic_binary_op {
|
||||
template <typename L, typename R>
|
||||
static bool compatible(const L& lhs, const R& rhs) {
|
||||
static constexpr bool compatible(const L& lhs, const R& rhs) {
|
||||
if constexpr (std::is_arithmetic_v<L> || std::is_arithmetic_v<R>) {
|
||||
return true;
|
||||
} else {
|
||||
@@ -71,6 +73,52 @@ struct minus : generic_binary_op<std::minus<>> {};
|
||||
struct multiply : generic_binary_op<std::multiplies<>> {};
|
||||
struct divide : generic_binary_op<std::divides<>> {};
|
||||
|
||||
struct matmul {
|
||||
template <typename L, typename R>
|
||||
static constexpr bool compatible(const L& lhs, const R& rhs) {
|
||||
using lhs_shape = remove_cvref_t<decltype(lhs.shape())>;
|
||||
using rhs_shape = remove_cvref_t<decltype(rhs.shape())>;
|
||||
|
||||
return lhs_shape::rank == 2 && rhs_shape::rank == 2 &&
|
||||
lhs.shape().template get<1>() == rhs.shape().template get<0>();
|
||||
}
|
||||
|
||||
template <typename L, typename R>
|
||||
static constexpr auto shape(const L& lhs, const R& rhs) {
|
||||
if (!compatible(lhs, rhs)) throw std::runtime_error("uncompatible operands");
|
||||
|
||||
using lhs_shape = remove_cvref_t<decltype(lhs.shape())>;
|
||||
using rhs_shape = remove_cvref_t<decltype(rhs.shape())>;
|
||||
|
||||
constexpr std::size_t lhs_rows = lhs_shape::template sget<0>();
|
||||
constexpr std::size_t rhs_cols = rhs_shape::template sget<1>();
|
||||
|
||||
if constexpr (lhs_rows == dynamic_extent || rhs_cols == dynamic_extent) {
|
||||
return dynamic_shape<lhs_rows, rhs_cols>{};
|
||||
} else {
|
||||
return static_shape<lhs_rows, rhs_cols>{};
|
||||
}
|
||||
}
|
||||
|
||||
template <typename L, typename R>
|
||||
constexpr auto operator()(std::size_t index, const L& lhs, const R& rhs) const {
|
||||
const std::size_t outCols = shape(lhs, rhs).template get<1>();
|
||||
const std::size_t row = index / outCols;
|
||||
const std::size_t col = index % outCols;
|
||||
|
||||
auto lhsRows = lhs.shape().template get<0>();
|
||||
auto lhsCols = lhs.shape().template get<1>();
|
||||
auto rhsCols = rhs.shape().template get<1>();
|
||||
|
||||
decltype(lhs.coeff(0) * rhs.coeff(0)) acc{};
|
||||
for (std::size_t i = 0; i < lhsCols; ++i) {
|
||||
acc += lhs.coeff((row * lhsCols) + i) * rhs.coeff((i * rhsCols) + col);
|
||||
}
|
||||
|
||||
return acc;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace ops
|
||||
} // namespace math
|
||||
} // namespace cenet
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
#ifndef CENET_MATH_TYPE_TRAITS_HPP
|
||||
#define CENET_MATH_TYPE_TRAITS_HPP
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
namespace cenet {
|
||||
namespace math {
|
||||
|
||||
template <typename T>
|
||||
using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>;
|
||||
|
||||
}
|
||||
} // namespace cenet
|
||||
|
||||
#endif // CENET_MATH_TYPE_TRAITS_HPP
|
||||
Reference in New Issue
Block a user