feat(math): add support for dynamic tensor ops
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
#ifndef CENET_MATH_OPS_HPP
|
||||
#define CENET_MATH_OPS_HPP
|
||||
|
||||
#include "cenet/math/tensor_shape.hpp"
|
||||
#include "tensor_traits.hpp"
|
||||
|
||||
#include <cstddef>
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
|
||||
namespace cenet {
|
||||
namespace math {
|
||||
@@ -14,15 +17,34 @@ private:
|
||||
template <typename L, typename R>
|
||||
struct result_type_impl {};
|
||||
|
||||
template <typename LT, typename RT, std::size_t... Dims>
|
||||
struct result_type_impl<tensor_view<LT, Dims...>, tensor_view<RT, Dims...>> {
|
||||
using type =
|
||||
tensor<decltype(std::declval<detach_storage_t<LT>>() + std::declval<detach_storage_t<RT>>()), Dims...>;
|
||||
template <typename LT, std::size_t... LDims, typename RT, std::size_t... RDims>
|
||||
struct result_type_impl<tensor_view<LT, LDims...>, tensor_view<RT, RDims...>> {
|
||||
using lhs_view = tensor_view<LT, LDims...>;
|
||||
using rhs_view = tensor_view<RT, RDims...>;
|
||||
|
||||
using type = std::enable_if_t<
|
||||
detail::is_shape_equivalent<typename lhs_view::shape_type, typename rhs_view::shape_type>::value,
|
||||
tensor<decltype(std::declval<detach_storage_t<LT>>() + std::declval<detach_storage_t<RT>>()), LDims...>>;
|
||||
};
|
||||
public:
|
||||
template <typename L, typename R>
|
||||
using result_type = typename result_type_impl<L, R>::type;
|
||||
|
||||
template <typename L, typename R, typename = std::enable_if_t<(is_tensor_v<L> && is_tensor_v<R>)>>
|
||||
static bool compatible(const L& lhs, const R& rhs) {
|
||||
return lhs.shape() == rhs.shape();
|
||||
}
|
||||
|
||||
template <typename L, typename R>
|
||||
static constexpr auto shape(const L& lhs, const R& rhs) {
|
||||
if (lhs.shape() != rhs.shape()) throw std::runtime_error("unexpected tensor shape");
|
||||
if constexpr (detail::is_static_shape_v<decltype(lhs.shape())>) {
|
||||
return lhs.shape();
|
||||
} else {
|
||||
return rhs.shape();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename LT, typename R>
|
||||
constexpr auto operator()(std::size_t index, LT lhs, const R& rhs) const {
|
||||
return lhs + rhs.coeff(index);
|
||||
@@ -34,15 +56,34 @@ private:
|
||||
template <typename L, typename R>
|
||||
struct result_type_impl {};
|
||||
|
||||
template <typename LT, typename RT, std::size_t... Dims>
|
||||
struct result_type_impl<tensor_view<LT, Dims...>, tensor_view<RT, Dims...>> {
|
||||
using type =
|
||||
tensor<decltype(std::declval<detach_storage_t<LT>>() - std::declval<detach_storage_t<RT>>()), Dims...>;
|
||||
template <typename LT, std::size_t... LDims, typename RT, std::size_t... RDims>
|
||||
struct result_type_impl<tensor_view<LT, LDims...>, tensor_view<RT, RDims...>> {
|
||||
using lhs_view = tensor_view<LT, LDims...>;
|
||||
using rhs_view = tensor_view<RT, RDims...>;
|
||||
|
||||
using type = std::enable_if_t<
|
||||
detail::is_shape_equivalent<typename lhs_view::shape_type, typename rhs_view::shape_type>::value,
|
||||
tensor<decltype(std::declval<detach_storage_t<LT>>() - std::declval<detach_storage_t<RT>>()), LDims...>>;
|
||||
};
|
||||
public:
|
||||
template <typename L, typename R>
|
||||
using result_type = typename result_type_impl<L, R>::type;
|
||||
|
||||
template <typename L, typename R, typename = std::enable_if_t<(is_tensor_v<L> && is_tensor_v<R>)>>
|
||||
static bool compatible(const L& lhs, const R& rhs) {
|
||||
return lhs.shape() == rhs.shape();
|
||||
}
|
||||
|
||||
template <typename L, typename R>
|
||||
static constexpr auto shape(const L& lhs, const R& rhs) {
|
||||
if (lhs.shape() != rhs.shape()) throw std::runtime_error("unexpected tensor shape");
|
||||
if constexpr (detail::is_static_shape_v<decltype(lhs.shape())>) {
|
||||
return lhs.shape();
|
||||
} else {
|
||||
return rhs.shape();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename LT, typename R>
|
||||
constexpr auto operator()(std::size_t index, LT lhs, const R& rhs) const {
|
||||
return lhs - rhs.coeff(index);
|
||||
|
||||
Reference in New Issue
Block a user