refactor(tulip): move cenet/math/ to tulip/
This commit is contained in:
@@ -0,0 +1,258 @@
|
||||
#ifndef TULIP_EXPR_HPP
|
||||
#define TULIP_EXPR_HPP
|
||||
|
||||
#include "./ops.hpp"
|
||||
#include "./tensor_traits.hpp"
|
||||
#include "./type_traits.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace tulip {
|
||||
|
||||
struct expression_tag {};
|
||||
struct expression_node_tag : expression_tag {};
|
||||
|
||||
template <typename E, typename Op>
|
||||
struct unary_expression;
|
||||
|
||||
template <typename L, typename R, typename Op>
|
||||
struct binary_expression;
|
||||
|
||||
template <typename CRTP>
|
||||
struct ops_provider {
|
||||
template <typename R>
|
||||
binary_expression<CRTP&, R, ops::matmul> matmul(R&& rhs) & {
|
||||
if (!ops::matmul::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self(), std::forward<R>(rhs) };
|
||||
}
|
||||
|
||||
template <typename R>
|
||||
binary_expression<const CRTP&, R, ops::matmul> matmul(R&& rhs) const& {
|
||||
if (!ops::matmul::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self(), std::forward<R>(rhs) };
|
||||
}
|
||||
|
||||
template <typename R>
|
||||
binary_expression<CRTP&&, R, ops::matmul> matmul(R&& rhs) && {
|
||||
if (!ops::matmul::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self(), std::forward<R>(rhs) };
|
||||
}
|
||||
|
||||
template <typename R>
|
||||
binary_expression<const CRTP&&, R, ops::matmul> matmul(R&& rhs) const&& {
|
||||
if (!ops::matmul::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self(), std::forward<R>(rhs) };
|
||||
}
|
||||
|
||||
template <typename Func>
|
||||
unary_expression<CRTP&, ops::apply<Func>> apply(Func&& func) & {
|
||||
if (!ops::apply<Func>::compatible(self())) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self(), std::forward<Func>(func) };
|
||||
}
|
||||
|
||||
template <typename Func>
|
||||
unary_expression<const CRTP&, ops::apply<Func>> apply(Func&& func) const& {
|
||||
if (!ops::apply<Func>::compatible(self())) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self(), std::forward<Func>(func) };
|
||||
}
|
||||
|
||||
template <typename Func>
|
||||
unary_expression<CRTP&&, ops::apply<Func>> apply(Func&& func) && {
|
||||
if (!ops::apply<Func>::compatible(self())) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self(), std::forward<Func>(func) };
|
||||
}
|
||||
|
||||
template <typename Func>
|
||||
unary_expression<const CRTP&&, ops::apply<Func>> apply(Func&& func) const&& {
|
||||
if (!ops::apply<Func>::compatible(self())) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self(), std::forward<Func>(func) };
|
||||
}
|
||||
|
||||
template <typename R, typename Func>
|
||||
binary_expression<CRTP&, R, ops::apply<Func>> apply(R&& rhs, Func&& func) & {
|
||||
if (!ops::apply<Func>::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self(), std::forward<R>(rhs), std::forward<Func>(func) };
|
||||
}
|
||||
|
||||
template <typename R, typename Func>
|
||||
binary_expression<const CRTP&, R, ops::apply<Func>> apply(R&& rhs, Func&& func) const& {
|
||||
if (!ops::apply<Func>::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self(), std::forward<R>(rhs), std::forward<Func>(func) };
|
||||
}
|
||||
|
||||
template <typename R, typename Func>
|
||||
binary_expression<CRTP&&, R, ops::apply<Func>> apply(R&& rhs, Func&& func) && {
|
||||
if (!ops::apply<Func>::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self(), std::forward<R>(rhs), std::forward<Func>(func) };
|
||||
}
|
||||
|
||||
template <typename R, typename Func>
|
||||
binary_expression<const CRTP&&, R, ops::apply<Func>> apply(R&& rhs, Func&& func) const&& {
|
||||
if (!ops::apply<Func>::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self(), std::forward<R>(rhs), std::forward<Func>(func) };
|
||||
}
|
||||
|
||||
unary_expression<CRTP&, ops::transpose> transpose() & {
|
||||
if (!ops::transpose::compatible(self())) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self() };
|
||||
}
|
||||
|
||||
unary_expression<const CRTP&, ops::transpose> transpose() const& {
|
||||
if (!ops::transpose::compatible(self())) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self() };
|
||||
}
|
||||
|
||||
unary_expression<CRTP&&, ops::transpose> transpose() && {
|
||||
if (!ops::transpose::compatible(self())) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self() };
|
||||
}
|
||||
|
||||
unary_expression<const CRTP&&, ops::transpose> transpose() const&& {
|
||||
if (!ops::transpose::compatible(self())) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self() };
|
||||
}
|
||||
|
||||
template <typename S>
|
||||
unary_expression<CRTP&, ops::reshape<S>> reshape() & {
|
||||
if (!ops::reshape<S>::compatible(self())) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self() };
|
||||
}
|
||||
|
||||
template <typename S>
|
||||
unary_expression<const CRTP&, ops::reshape<S>> reshape() const& {
|
||||
if (!ops::reshape<S>::compatible(self())) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self() };
|
||||
}
|
||||
|
||||
template <typename S>
|
||||
unary_expression<CRTP&&, ops::reshape<S>> reshape() && {
|
||||
if (!ops::reshape<S>::compatible(self())) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self() };
|
||||
}
|
||||
|
||||
template <typename S>
|
||||
unary_expression<const CRTP&&, ops::reshape<S>> reshape() const&& {
|
||||
if (!ops::reshape<S>::compatible(self())) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self() };
|
||||
}
|
||||
private:
|
||||
decltype(auto) self() & { return static_cast<CRTP&>(*this); }
|
||||
decltype(auto) self() && { return static_cast<CRTP&&>(*this); }
|
||||
|
||||
decltype(auto) self() const& { return static_cast<const CRTP&>(*this); }
|
||||
decltype(auto) self() const&& { return static_cast<const CRTP&&>(*this); }
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <typename T>
|
||||
struct operand_storage {
|
||||
using raw_type = remove_cvref_t<T>;
|
||||
using type = std::conditional_t<std::is_base_of_v<expression_node_tag, raw_type>,
|
||||
raw_type,
|
||||
std::conditional_t<std::is_lvalue_reference_v<T>, T, raw_type>>;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
using operand_storage_t = typename operand_storage<T>::type;
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template <typename E, typename Op>
|
||||
struct unary_expression : expression_node_tag, ops_provider<unary_expression<E, Op>> {
|
||||
using expr_type = detail::operand_storage_t<E>;
|
||||
|
||||
expr_type expr;
|
||||
Op op;
|
||||
|
||||
template <typename EFwd = E,
|
||||
typename OpFwd = Op,
|
||||
typename = std::enable_if_t<std::is_constructible_v<expr_type, EFwd&&> &&
|
||||
!std::is_same_v<std::decay_t<EFwd>, unary_expression>>>
|
||||
unary_expression(EFwd&& expr, OpFwd&& op = Op{}) // NOLINT
|
||||
: expr(std::forward<EFwd>(expr)), op(std::forward<OpFwd>(op)) {}
|
||||
|
||||
constexpr auto shape() const { return Op::shape(expr); }
|
||||
|
||||
auto coeff(std::size_t index) const { return op(index, std::as_const(expr)); }
|
||||
};
|
||||
|
||||
template <typename L, typename R, typename Op>
|
||||
struct binary_expression : expression_node_tag, ops_provider<binary_expression<L, R, Op>> {
|
||||
using lhs_type = detail::operand_storage_t<L>;
|
||||
using rhs_type = detail::operand_storage_t<R>;
|
||||
|
||||
lhs_type lhs;
|
||||
rhs_type rhs;
|
||||
Op op;
|
||||
|
||||
template <typename LFwd = L, typename RFwd = R, typename OpFwd = Op>
|
||||
binary_expression(LFwd&& lhs, RFwd&& rhs, OpFwd&& op = Op{})
|
||||
: lhs(std::forward<LFwd>(lhs)), rhs(std::forward<RFwd>(rhs)), op(std::forward<OpFwd>(op)) {}
|
||||
|
||||
constexpr auto shape() const { return Op::shape(lhs, rhs); }
|
||||
|
||||
auto coeff(std::size_t index) const { return op(index, std::as_const(lhs), std::as_const(rhs)); }
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <typename T, typename = void>
|
||||
struct operand {
|
||||
using type = T;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct operand<T, std::void_t<typename remove_cvref_t<T>::view_type>> {
|
||||
using type = typename remove_cvref_t<T>::view_type;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
using operand_t = typename operand<T>::type;
|
||||
|
||||
template <typename T>
|
||||
static constexpr bool is_expression_v = std::is_base_of_v<expression_tag, remove_cvref_t<T>>;
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template <typename L, typename R, typename Op = ops::plus>
|
||||
binary_expression<L, R, Op> operator+(L&& lhs, R&& rhs) {
|
||||
// TODO: introduce a better exception class
|
||||
if (!Op::compatible(lhs, rhs)) throw std::runtime_error("tensor shape mismatch");
|
||||
return { std::forward<L>(lhs), std::forward<R>(rhs) };
|
||||
}
|
||||
|
||||
template <typename L, typename R, typename Op = ops::minus>
|
||||
binary_expression<L, R, Op> operator-(L&& lhs, R&& rhs) {
|
||||
if (!Op::compatible(lhs, rhs)) throw std::runtime_error("tensor shape mismatch");
|
||||
return { std::forward<L>(lhs), std::forward<R>(rhs) };
|
||||
}
|
||||
|
||||
template <typename L, typename R, typename Op = ops::multiply>
|
||||
binary_expression<L, R, Op> operator*(L&& lhs, R&& rhs) {
|
||||
if (!Op::compatible(lhs, rhs)) throw std::runtime_error("tensor shape mismatch");
|
||||
return { std::forward<L>(lhs), std::forward<R>(rhs) };
|
||||
}
|
||||
|
||||
template <typename L, typename R, typename Op = ops::divide>
|
||||
binary_expression<L, R, Op> operator/(L&& lhs, R&& rhs) {
|
||||
if (!Op::compatible(lhs, rhs)) throw std::runtime_error("tensor shape mismatch");
|
||||
return { std::forward<L>(lhs), std::forward<R>(rhs) };
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr decltype(auto) eval(T&& fwd) {
|
||||
if constexpr (detail::is_expression_v<T>) {
|
||||
auto tensor = tensor_from_shape<decltype(fwd.coeff(std::declval<std::size_t>()))>(fwd.shape());
|
||||
tensor = std::forward<T>(fwd);
|
||||
return tensor;
|
||||
} else {
|
||||
return std::forward<T>(fwd);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace tulip
|
||||
|
||||
#endif // TULIP_EXPR_HPP
|
||||
@@ -0,0 +1,220 @@
|
||||
#ifndef TULIP_OPS_HPP
|
||||
#define TULIP_OPS_HPP
|
||||
|
||||
#include "./tensor_fwd.hpp"
|
||||
#include "./tensor_shape.hpp"
|
||||
#include "./type_traits.hpp"
|
||||
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
|
||||
namespace tulip {
|
||||
namespace ops {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <typename T, typename = void>
|
||||
struct has_shape : std::false_type {};
|
||||
|
||||
template <typename T>
|
||||
struct has_shape<T, std::void_t<decltype(std::declval<T>().shape())>> : std::true_type {};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template <typename Op>
|
||||
struct generic_binary_op {
|
||||
template <typename L, typename R>
|
||||
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 {
|
||||
return lhs.shape() == rhs.shape();
|
||||
}
|
||||
}
|
||||
|
||||
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 best_shape = tulip::detail::best_shape<decltype(shape_wrapper(lhs)), decltype(shape_wrapper(rhs))>;
|
||||
static_assert(best_shape::valid, "uncompatible operands");
|
||||
if constexpr (best_shape::left) {
|
||||
return lhs.shape();
|
||||
} else {
|
||||
return rhs.shape();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename L, typename R>
|
||||
constexpr auto operator()(std::size_t index, const L& lhs, const R& rhs) const {
|
||||
if constexpr (std::is_arithmetic_v<L>) {
|
||||
return Op{}(lhs, rhs.coeff(index));
|
||||
} else if constexpr (std::is_arithmetic_v<R>) {
|
||||
return Op{}(lhs.coeff(index), rhs);
|
||||
} else {
|
||||
return Op{}(lhs.coeff(index), rhs.coeff(index));
|
||||
}
|
||||
}
|
||||
private:
|
||||
template <typename T>
|
||||
static constexpr auto shape_wrapper(T&& fwd) {
|
||||
if constexpr (detail::has_shape<T>::value) {
|
||||
return std::forward<T>(fwd).shape();
|
||||
} else {
|
||||
return std::forward<T>(fwd);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct plus : generic_binary_op<std::plus<>> {};
|
||||
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>{ lhs_shape::template get<0>(), rhs_shape::template get<1>() };
|
||||
} 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;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Func>
|
||||
struct apply {
|
||||
Func func;
|
||||
|
||||
template <typename Fwd = Func, typename = std::enable_if_t<std::is_constructible_v<Func, Fwd>>>
|
||||
apply(Fwd&& fwd) // NOLINT
|
||||
: func(std::forward<Fwd>(fwd)) {}
|
||||
|
||||
template <typename E>
|
||||
static constexpr bool compatible(const E& expr) {
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename L, typename R>
|
||||
static constexpr bool compatible(const L& lhs, const R& rhs) {
|
||||
return lhs.shape() == rhs.shape();
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static constexpr auto shape(const E& expr) {
|
||||
if (!compatible(expr)) throw std::runtime_error("uncompatible operands");
|
||||
return expr.shape();
|
||||
}
|
||||
|
||||
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");
|
||||
return lhs.shape();
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
constexpr auto operator()(std::size_t index, const E& expr) const {
|
||||
return func(expr.coeff(index));
|
||||
}
|
||||
|
||||
template <typename L, typename R>
|
||||
constexpr auto operator()(std::size_t index, const L& lhs, const R& rhs) const {
|
||||
return func(lhs.coeff(index), rhs.coeff(index));
|
||||
}
|
||||
};
|
||||
|
||||
struct transpose {
|
||||
template <typename E>
|
||||
static constexpr bool compatible(const E& expr) {
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static constexpr auto shape(const E& expr) {
|
||||
if (!compatible(expr)) throw std::runtime_error("uncompatible operands");
|
||||
|
||||
using shape_type = remove_cvref_t<decltype(expr.shape())>;
|
||||
|
||||
constexpr std::size_t rows = shape_type::template sget<0>();
|
||||
constexpr std::size_t cols = shape_type::template sget<1>();
|
||||
|
||||
if constexpr (rows == dynamic_extent || cols == dynamic_extent) {
|
||||
return dynamic_shape<cols, rows>{ shape_type::template get<1>(), shape_type::template get<0>() };
|
||||
} else {
|
||||
return static_shape<cols, rows>{};
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
constexpr auto operator()(std::size_t index, const E& expr) const {
|
||||
const std::size_t rows = expr.shape().template get<0>();
|
||||
const std::size_t cols = expr.shape().template get<1>();
|
||||
return expr.coeff(((index % rows) * cols) + (index / rows));
|
||||
}
|
||||
};
|
||||
|
||||
// TODO: introduce dynamic reshape
|
||||
template <typename S, typename = std::enable_if_t<::tulip::detail::is_static_shape_v<S>>>
|
||||
struct reshape {
|
||||
template <typename E>
|
||||
static constexpr bool compatible(const E& expr) {
|
||||
using shape_type = remove_cvref_t<decltype(expr.shape())>;
|
||||
if constexpr (!::tulip::detail::is_static_shape<shape_type>::value) {
|
||||
return false;
|
||||
} else {
|
||||
return shape_type::size == S::size;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static constexpr auto shape(const E& expr) {
|
||||
if (!compatible(expr)) throw std::runtime_error("uncompatible operands");
|
||||
return S{};
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
constexpr auto operator()(std::size_t index, const E& expr) const {
|
||||
return expr.coeff(index);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace ops
|
||||
} // namespace tulip
|
||||
|
||||
#endif // TULIP_OPS_HPP
|
||||
@@ -0,0 +1,277 @@
|
||||
#ifndef TULIP_TENSOR_HPP
|
||||
#define TULIP_TENSOR_HPP
|
||||
|
||||
#include "./expr.hpp"
|
||||
#include "./tensor_fwd.hpp"
|
||||
#include "./tensor_shape.hpp"
|
||||
#include "./tensor_traits.hpp"
|
||||
#include "./tensor_view.hpp"
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace tulip {
|
||||
|
||||
template <typename T, std::size_t... Dims>
|
||||
class tensor : public expression_tag, public ops_provider<tensor<T, Dims...>> {
|
||||
public:
|
||||
using traits = tensor_traits<tensor>;
|
||||
using value_type = typename traits::value_type;
|
||||
using constant_value_type = const value_type;
|
||||
using view_type = tensor_view<value_type, Dims...>;
|
||||
using constant_view_type = tensor_view<constant_value_type, Dims...>;
|
||||
using shape_type = std::conditional_t<traits::dynamic, dynamic_shape<Dims...>, static_shape<Dims...>>;
|
||||
public:
|
||||
template <typename ST>
|
||||
using with_storage = typename traits::template with_storage_t<ST>;
|
||||
public:
|
||||
tensor() {
|
||||
if constexpr (traits::dynamic) {
|
||||
m_items = nullptr;
|
||||
m_shape = {};
|
||||
} else if constexpr (traits::inline_storage) {
|
||||
view().fill();
|
||||
} else {
|
||||
m_items = new value_type[traits::size];
|
||||
view().fill();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename... RtDims,
|
||||
typename = std::enable_if_t<(
|
||||
traits::dynamic && sizeof...(RtDims) == sizeof...(Dims) && (std::is_integral_v<RtDims> && ...))>>
|
||||
explicit tensor(RtDims... dimensions) {
|
||||
set_dimensions(dimensions...);
|
||||
}
|
||||
|
||||
~tensor() { release(); }
|
||||
|
||||
tensor(tensor&& other) noexcept
|
||||
: m_shape(std::move(other.m_shape)) {
|
||||
if constexpr (!traits::inline_storage) {
|
||||
m_items = other.m_items;
|
||||
other.m_items = nullptr;
|
||||
} else {
|
||||
std::memcpy(m_items, other.m_items, sizeof(m_items));
|
||||
}
|
||||
}
|
||||
|
||||
tensor& operator=(tensor&& other) noexcept {
|
||||
if (this == &other) return *this;
|
||||
release();
|
||||
m_shape = std::move(other.m_shape);
|
||||
if constexpr (!traits::inline_storage) {
|
||||
m_items = other.m_items;
|
||||
other.m_items = nullptr;
|
||||
} else {
|
||||
std::memcpy(m_items, other.m_items, sizeof(m_items));
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
tensor(const tensor& other)
|
||||
: m_shape(other.m_shape) {
|
||||
set_dimensions(other.m_shape);
|
||||
dyn_copy(other.m_items);
|
||||
}
|
||||
|
||||
tensor& operator=(const tensor& other) {
|
||||
if (this == &other) return *this;
|
||||
release();
|
||||
set_dimensions(other.m_shape);
|
||||
dyn_copy(other.m_items);
|
||||
|
||||
return *this;
|
||||
}
|
||||
public:
|
||||
// TODO: improve aliasing semantics
|
||||
template <typename E,
|
||||
typename = std::enable_if_t<detail::is_expression_v<E> && !std::is_same_v<std::decay_t<E>, tensor>>>
|
||||
tensor(E&& exprFwd) { // NOLINT
|
||||
set_dimensions(exprFwd.shape());
|
||||
|
||||
E expr = std::forward<E>(exprFwd);
|
||||
for (std::size_t i = 0; i < size(); ++i) {
|
||||
m_items[i] = expr.coeff(i);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E, typename = std::enable_if_t<detail::is_expression_v<E>>>
|
||||
tensor& operator=(E&& exprFwd) {
|
||||
set_dimensions(exprFwd.shape());
|
||||
|
||||
E expr = std::forward<E>(exprFwd);
|
||||
for (std::size_t i = 0; i < view().size(); ++i) {
|
||||
m_items[i] = expr.coeff(i);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename E, typename = std::enable_if_t<detail::is_expression_v<E>>>
|
||||
tensor& operator+=(E&& exprFwd) {
|
||||
if (m_shape != exprFwd.shape()) throw std::runtime_error("tensor shape mismatch");
|
||||
|
||||
E expr = std::forward<E>(exprFwd);
|
||||
for (std::size_t i = 0; i < view().size(); ++i) {
|
||||
m_items[i] += expr.coeff(i);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename E, typename = std::enable_if_t<detail::is_expression_v<E>>>
|
||||
tensor& operator-=(E&& exprFwd) {
|
||||
if (m_shape != exprFwd.shape()) throw std::runtime_error("tensor shape mismatch");
|
||||
|
||||
E expr = std::forward<E>(exprFwd);
|
||||
for (std::size_t i = 0; i < view().size(); ++i) {
|
||||
m_items[i] -= expr.coeff(i);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename E, typename = std::enable_if_t<detail::is_expression_v<E>>>
|
||||
tensor& operator*=(E&& exprFwd) {
|
||||
if (m_shape != exprFwd.shape()) throw std::runtime_error("tensor shape mismatch");
|
||||
|
||||
E expr = std::forward<E>(exprFwd);
|
||||
for (std::size_t i = 0; i < view().size(); ++i) {
|
||||
m_items[i] *= expr.coeff(i);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename E, typename = std::enable_if_t<detail::is_expression_v<E>>>
|
||||
tensor& operator/=(E&& exprFwd) {
|
||||
if (m_shape != exprFwd.shape()) throw std::runtime_error("tensor shape mismatch");
|
||||
|
||||
E expr = std::forward<E>(exprFwd);
|
||||
for (std::size_t i = 0; i < view().size(); ++i) {
|
||||
m_items[i] /= expr.coeff(i);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
public:
|
||||
view_type view() & { return { m_items, m_shape }; }
|
||||
constant_view_type view() const& { return { m_items, m_shape }; }
|
||||
|
||||
view_type view() && = delete;
|
||||
constant_view_type view() const&& = delete;
|
||||
|
||||
explicit operator view_type() { return view(); }
|
||||
explicit operator constant_view_type() const { return view(); }
|
||||
public:
|
||||
template <typename Off, typename = std::enable_if_t<std::is_integral_v<Off>>>
|
||||
decltype(auto) operator[](Off off) {
|
||||
return view()[off];
|
||||
}
|
||||
|
||||
template <typename Off, typename = std::enable_if_t<std::is_integral_v<Off>>>
|
||||
decltype(auto) operator[](Off off) const {
|
||||
return view()[off];
|
||||
}
|
||||
public:
|
||||
template <typename... RtDims,
|
||||
typename = std::enable_if_t<(sizeof...(RtDims) == sizeof...(Dims) && (std::is_integral_v<RtDims> && ...))>>
|
||||
void set_dimensions(RtDims... dimensions) {
|
||||
if (!valid_dims(std::make_index_sequence<sizeof...(Dims)>{}, dimensions...))
|
||||
throw std::runtime_error("unexpected shape");
|
||||
if constexpr (!traits::inline_storage) {
|
||||
store_dims(std::make_index_sequence<sizeof...(Dims)>{}, dimensions...);
|
||||
|
||||
release();
|
||||
m_items = new value_type[(dimensions * ... * 1)];
|
||||
std::memset(m_items, 0, sizeof(*m_items) * (dimensions * ... * 1));
|
||||
} else {
|
||||
store_dims(std::make_index_sequence<sizeof...(Dims)>{}, dimensions...);
|
||||
}
|
||||
}
|
||||
|
||||
template <template <std::size_t...> class Shape, std::size_t... ShapeDims>
|
||||
void set_dimensions(const Shape<ShapeDims...>& shape) {
|
||||
set_dimensions_impl(shape, std::make_index_sequence<sizeof...(ShapeDims)>{});
|
||||
}
|
||||
private:
|
||||
template <template <std::size_t...> class Shape, std::size_t... ShapeDims, std::size_t... Is>
|
||||
void set_dimensions_impl(const Shape<ShapeDims...>& shape, std::index_sequence<Is...>) {
|
||||
set_dimensions(shape.template get<Is>()...);
|
||||
}
|
||||
public:
|
||||
constexpr std::size_t size() const { return size_impl(std::make_index_sequence<sizeof...(Dims)>{}); }
|
||||
|
||||
auto dimensions() const { return dimensions_impl(std::make_index_sequence<sizeof...(Dims)>{}); }
|
||||
private:
|
||||
template <std::size_t... Is>
|
||||
constexpr std::size_t size_impl(std::index_sequence<Is...>) const {
|
||||
return (dimension<Is>() * ... * 1);
|
||||
}
|
||||
|
||||
template <std::size_t... Is>
|
||||
constexpr auto dimensions_impl(std::index_sequence<Is...>) const {
|
||||
return std::make_tuple(dimension<Is>()...);
|
||||
}
|
||||
public:
|
||||
value_type& coeff(std::size_t index) { return m_items[index]; }
|
||||
const value_type& coeff(std::size_t index) const { return m_items[index]; }
|
||||
|
||||
value_type* data() { return m_items; }
|
||||
const value_type* data() const { return m_items; }
|
||||
|
||||
const shape_type& shape() const { return m_shape; }
|
||||
private:
|
||||
template <typename... RtDims, std::size_t... Is>
|
||||
constexpr bool valid_dims(std::index_sequence<Is...>, RtDims... dimensions) const {
|
||||
return (valid_dim<decltype(dimensions), Is>(dimensions) && ...);
|
||||
}
|
||||
|
||||
template <typename RtDim, std::size_t I>
|
||||
constexpr bool valid_dim(RtDim dim) const {
|
||||
return traits::dimensions[I] == dynamic_extent || traits::dimensions[I] == dim;
|
||||
}
|
||||
private:
|
||||
template <typename... RtDims, std::size_t... Is>
|
||||
constexpr void store_dims(std::index_sequence<Is...>, RtDims... dimensions) {
|
||||
if constexpr (!detail::is_static_shape_v<shape_type>) {
|
||||
((m_shape.dimensions[Is] = static_cast<std::size_t>(dimensions)), ...);
|
||||
}
|
||||
}
|
||||
private:
|
||||
template <std::size_t I>
|
||||
constexpr std::size_t dimension() const {
|
||||
if constexpr (traits::dynamic && traits::dimensions[I] == dynamic_extent) {
|
||||
return m_shape.dimensions[I];
|
||||
} else {
|
||||
return traits::dimensions[I];
|
||||
}
|
||||
}
|
||||
private:
|
||||
template <std::size_t... Is>
|
||||
void dyn_alloc(std::index_sequence<Is...>) {
|
||||
if constexpr (!traits::inline_storage) {
|
||||
m_items = new value_type[size()];
|
||||
}
|
||||
}
|
||||
|
||||
void dyn_copy(const value_type* src) { std::memcpy(m_items, src, sizeof(*m_items) * size()); }
|
||||
|
||||
void release() {
|
||||
if constexpr (!traits::inline_storage) {
|
||||
delete[] m_items;
|
||||
m_items = nullptr;
|
||||
}
|
||||
}
|
||||
private:
|
||||
typename traits::storage_type m_items{};
|
||||
shape_type m_shape;
|
||||
};
|
||||
|
||||
} // namespace tulip
|
||||
|
||||
#endif // TULIP_TENSOR_HPP
|
||||
@@ -0,0 +1,30 @@
|
||||
#ifndef TULIP_FWD_HPP
|
||||
#define TULIP_FWD_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <limits>
|
||||
|
||||
namespace tulip {
|
||||
|
||||
inline constexpr std::size_t dynamic_extent = std::numeric_limits<std::size_t>::max();
|
||||
|
||||
template <typename T>
|
||||
struct inline_storage {
|
||||
using value_type = T;
|
||||
};
|
||||
|
||||
struct heap_storage_tag {};
|
||||
struct inline_storage_tag {};
|
||||
|
||||
template <typename T, std::size_t... Dims>
|
||||
class tensor_view;
|
||||
|
||||
template <typename T, std::size_t... Dims>
|
||||
class tensor;
|
||||
|
||||
template <typename T, std::size_t M, std::size_t N>
|
||||
using matrix = tensor<T, M, N>;
|
||||
|
||||
} // namespace tulip
|
||||
|
||||
#endif // TULIP_FWD_HPP
|
||||
@@ -0,0 +1,319 @@
|
||||
#ifndef TULIP_TENSOR_SHAPE_HPP
|
||||
#define TULIP_TENSOR_SHAPE_HPP
|
||||
|
||||
#include "./tensor_fwd.hpp"
|
||||
|
||||
#include <cstddef>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace tulip {
|
||||
|
||||
template <std::size_t... Dims>
|
||||
struct shape {};
|
||||
|
||||
template <std::size_t... Dims>
|
||||
struct static_shape;
|
||||
|
||||
template <std::size_t... Dims>
|
||||
struct dynamic_shape;
|
||||
|
||||
template <std::size_t... Dims>
|
||||
struct static_shape : shape<Dims...> {
|
||||
static_shape() = default;
|
||||
|
||||
template <std::size_t... ExtDims, typename = std::enable_if_t<(sizeof...(ExtDims) >= sizeof...(Dims))>>
|
||||
explicit static_shape(const static_shape<ExtDims...>& oth) {}
|
||||
|
||||
template <std::size_t... OthDims>
|
||||
static constexpr bool compatible() {
|
||||
return ((Dims == OthDims) && ...);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static constexpr bool compatible() {
|
||||
return compatible_impl<T>::value;
|
||||
}
|
||||
|
||||
template <std::size_t I>
|
||||
constexpr auto get() const {
|
||||
return get_impl<I, Dims...>::value;
|
||||
}
|
||||
|
||||
template <std::size_t I>
|
||||
static constexpr auto sget() {
|
||||
return get_impl<I, Dims...>::value;
|
||||
}
|
||||
|
||||
static constexpr auto rank = sizeof...(Dims);
|
||||
static constexpr auto score = sizeof...(Dims);
|
||||
private:
|
||||
template <typename T>
|
||||
struct compatible_impl : std::false_type {};
|
||||
|
||||
template <typename T, std::size_t... OthDims>
|
||||
struct compatible_impl<tensor<T, OthDims...>>
|
||||
: std::conditional_t<((Dims == OthDims) && ...), std::true_type, std::false_type> {};
|
||||
|
||||
template <std::size_t I, std::size_t First, std::size_t... Rest>
|
||||
struct get_impl : get_impl<I - 1, Rest...> {};
|
||||
|
||||
template <std::size_t First, std::size_t... Rest>
|
||||
struct get_impl<0, First, Rest...> {
|
||||
static constexpr auto value = First;
|
||||
};
|
||||
private:
|
||||
template <std::size_t S, std::size_t...>
|
||||
struct size_impl {
|
||||
static constexpr auto value = S;
|
||||
};
|
||||
|
||||
template <std::size_t S, std::size_t First, std::size_t... Rest>
|
||||
struct size_impl<S, First, Rest...> : size_impl<First * S, Rest...> {};
|
||||
public:
|
||||
static constexpr auto size = size_impl<1, Dims...>::value;
|
||||
|
||||
template <std::size_t Start = 0, typename... Offs>
|
||||
std::size_t calculate_offset(const std::tuple<Offs...>& offs) const {
|
||||
constexpr std::size_t off_count = sizeof...(Offs);
|
||||
|
||||
static_assert(off_count > 0);
|
||||
static_assert(Start + off_count <= sizeof...(Dims));
|
||||
|
||||
return calculate_offset_impl<off_count - 1, Start>(offs) * trailing_product<Start + off_count>();
|
||||
}
|
||||
private:
|
||||
template <std::size_t I, std::size_t Start, typename... Offs>
|
||||
std::size_t calculate_offset_impl(const std::tuple<Offs...>& offs) const {
|
||||
if constexpr (I == 0) {
|
||||
return std::get<0>(offs);
|
||||
} else {
|
||||
return (calculate_offset_impl<I - 1, Start>(offs) * get<Start + I>()) + std::get<I>(offs);
|
||||
}
|
||||
}
|
||||
|
||||
template <std::size_t I>
|
||||
std::size_t trailing_product() const {
|
||||
if constexpr (I == sizeof...(Dims)) {
|
||||
return 1;
|
||||
} else {
|
||||
return get<I>() * trailing_product<I + 1>();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template <std::size_t... Dims>
|
||||
struct dynamic_shape : shape<Dims...> {
|
||||
dynamic_shape() = default;
|
||||
|
||||
template <typename... RDims,
|
||||
typename = std::enable_if_t<(sizeof...(RDims) == sizeof...(Dims)) && (std::is_integral_v<RDims> && ...)>>
|
||||
explicit dynamic_shape(RDims... rtDimensions) {
|
||||
store_impl<0>(rtDimensions...);
|
||||
}
|
||||
|
||||
template <std::size_t ExtRank, typename = std::enable_if_t<(ExtRank >= sizeof...(Dims))>>
|
||||
explicit dynamic_shape(const dynamic_shape<ExtRank>& oth) {
|
||||
constexpr std::size_t ext_offset = ExtRank - sizeof...(Dims);
|
||||
for (std::size_t i = 0; i < sizeof...(Dims); ++i) {
|
||||
dimensions[i] = oth.dimensions[ext_offset + i];
|
||||
}
|
||||
}
|
||||
|
||||
template <std::size_t... OthDims>
|
||||
static constexpr bool compatible() {
|
||||
return ((Dims == dynamic_extent || Dims == OthDims) && ...);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static constexpr bool compatible() {
|
||||
return compatible_impl<T>::value;
|
||||
}
|
||||
|
||||
template <std::size_t I>
|
||||
constexpr auto get() const {
|
||||
return get_impl<I, Dims...>::value == dynamic_extent ? dimensions[I] : get_impl<I, Dims...>::value;
|
||||
}
|
||||
|
||||
template <std::size_t I>
|
||||
static constexpr auto sget() {
|
||||
return get_impl<I, Dims...>::value;
|
||||
}
|
||||
|
||||
std::size_t dimensions[sizeof...(Dims)]{};
|
||||
private:
|
||||
template <typename T>
|
||||
struct compatible_impl : std::false_type {};
|
||||
|
||||
template <typename T, std::size_t... OthDims>
|
||||
struct compatible_impl<tensor<T, OthDims...>>
|
||||
: std::conditional_t<((Dims == dynamic_extent || Dims == OthDims) && ...), std::true_type, std::false_type> {};
|
||||
|
||||
template <template <std::size_t...> class R, std::size_t... RDims, std::size_t... Is>
|
||||
constexpr bool compare_impl(std::index_sequence<Is...>, const R<RDims...>& rhs) const {
|
||||
return ((get<Is>() == rhs.template get<Is>()) && ...);
|
||||
}
|
||||
|
||||
template <std::size_t I, std::size_t First, std::size_t... Rest>
|
||||
struct get_impl : get_impl<I - 1, Rest...> {};
|
||||
|
||||
template <std::size_t First, std::size_t... Rest>
|
||||
struct get_impl<0, First, Rest...> {
|
||||
static constexpr auto value = First;
|
||||
};
|
||||
|
||||
template <std::size_t Score, std::size_t...>
|
||||
struct score_impl {
|
||||
static constexpr auto value = Score;
|
||||
};
|
||||
|
||||
template <std::size_t Score, std::size_t First, std::size_t... Rest>
|
||||
struct score_impl<Score, First, Rest...> : score_impl<Score + ((First != dynamic_extent) ? 1 : 0), Rest...> {};
|
||||
|
||||
template <std::size_t I, typename RDim, typename... RDims, typename = std::enable_if_t<(I < sizeof...(Dims))>>
|
||||
void store_impl(RDim first, RDims... rest) {
|
||||
dimensions[I] = first;
|
||||
store_impl<I + 1>(rest...);
|
||||
}
|
||||
|
||||
template <std::size_t I, typename RDim>
|
||||
void store_impl(RDim last) {
|
||||
dimensions[I] = last;
|
||||
}
|
||||
public:
|
||||
static constexpr auto rank = sizeof...(Dims);
|
||||
static constexpr auto score = score_impl<0, Dims...>::value;
|
||||
|
||||
template <std::size_t Start = 0, typename... Offs>
|
||||
std::size_t calculate_offset(const std::tuple<Offs...>& offs) const {
|
||||
constexpr std::size_t off_count = sizeof...(Offs);
|
||||
|
||||
static_assert(off_count > 0);
|
||||
static_assert(Start + off_count <= sizeof...(Dims));
|
||||
|
||||
return calculate_offset_impl<off_count - 1, Start>(offs) * trailing_product<Start + off_count>();
|
||||
}
|
||||
private:
|
||||
template <std::size_t I, std::size_t Start, typename... Offs>
|
||||
std::size_t calculate_offset_impl(const std::tuple<Offs...>& offs) const {
|
||||
if constexpr (I == 0) {
|
||||
return std::get<0>(offs);
|
||||
} else {
|
||||
return (calculate_offset_impl<I - 1, Start>(offs) * get<Start + I>()) + std::get<I>(offs);
|
||||
}
|
||||
}
|
||||
|
||||
template <std::size_t I>
|
||||
std::size_t trailing_product() const {
|
||||
if constexpr (I == sizeof...(Dims)) {
|
||||
return 1;
|
||||
} else {
|
||||
return get<I>() * trailing_product<I + 1>();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <typename T>
|
||||
struct is_static_shape : std::false_type {};
|
||||
|
||||
template <std::size_t... Dims>
|
||||
struct is_static_shape<static_shape<Dims...>> : std::true_type {};
|
||||
|
||||
template <std::size_t... Dims>
|
||||
struct is_static_shape_var : std::true_type {};
|
||||
|
||||
template <std::size_t First, std::size_t... Rest>
|
||||
struct is_static_shape_var<First, Rest...>
|
||||
: std::conditional_t<(First == dynamic_extent), std::false_type, is_static_shape_var<Rest...>> {};
|
||||
|
||||
template <typename T>
|
||||
inline constexpr auto is_static_shape_v = is_static_shape<T>::value;
|
||||
|
||||
template <template <std::size_t...> class L,
|
||||
std::size_t... LDims,
|
||||
template <std::size_t...> class R,
|
||||
std::size_t... RDims,
|
||||
std::size_t... Is>
|
||||
static constexpr bool shape_compare_impl(std::index_sequence<Is...>, const L<LDims...>& lhs, const R<RDims...>& rhs) {
|
||||
return ((lhs.template get<Is>() == rhs.template get<Is>()) && ...);
|
||||
}
|
||||
|
||||
template <typename L, typename R>
|
||||
struct is_shape_equivalent : std::false_type {};
|
||||
|
||||
template <template <std::size_t...> class L,
|
||||
std::size_t LFirst,
|
||||
std::size_t... LRest,
|
||||
template <std::size_t...> class R,
|
||||
std::size_t RFirst,
|
||||
std::size_t... RRest>
|
||||
struct is_shape_equivalent<L<LFirst, LRest...>, R<RFirst, RRest...>>
|
||||
: std::conditional_t<(LFirst == RFirst || LFirst == dynamic_extent || RFirst == dynamic_extent),
|
||||
is_shape_equivalent<L<LRest...>, R<RRest...>>,
|
||||
std::false_type> {};
|
||||
|
||||
template <template <std::size_t...> class L, template <std::size_t...> class R>
|
||||
struct is_shape_equivalent<L<>, R<>> : std::true_type {};
|
||||
|
||||
template <typename L, typename R, typename = void>
|
||||
struct best_shape {};
|
||||
|
||||
template <template <std::size_t...> class T, std::size_t... Dims, typename O>
|
||||
struct best_shape<T<Dims...>, O> {
|
||||
using type = T<Dims...>;
|
||||
|
||||
static constexpr bool valid = true;
|
||||
static constexpr bool left = true;
|
||||
};
|
||||
|
||||
template <template <std::size_t...> class T, std::size_t... Dims, typename O>
|
||||
struct best_shape<O, T<Dims...>> {
|
||||
using type = T<Dims...>;
|
||||
|
||||
static constexpr bool valid = true;
|
||||
static constexpr bool left = false;
|
||||
};
|
||||
|
||||
template <template <std::size_t...> class L,
|
||||
std::size_t... LDims,
|
||||
template <std::size_t...> class R,
|
||||
std::size_t... RDims>
|
||||
struct best_shape<L<LDims...>, R<RDims...>> {
|
||||
static constexpr bool valid = sizeof...(LDims) == sizeof...(RDims);
|
||||
static constexpr bool left = L<RDims...>::score >= R<LDims...>::score;
|
||||
using type = std::enable_if_t<valid, std::conditional_t<left, L<LDims...>, R<RDims...>>>;
|
||||
};
|
||||
|
||||
template <std::size_t... Dims>
|
||||
struct shape_of_impl {
|
||||
using type = std::conditional_t<is_static_shape_var<Dims...>::value, static_shape<Dims...>, dynamic_shape<Dims...>>;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template <std::size_t... Dims>
|
||||
using shape_of = typename detail::shape_of_impl<Dims...>::type;
|
||||
|
||||
template <template <std::size_t...> class L,
|
||||
std::size_t... LDims,
|
||||
template <std::size_t...> class R,
|
||||
std::size_t... RDims>
|
||||
bool operator==(const L<LDims...>& lhs, const R<RDims...>& rhs) {
|
||||
return sizeof...(LDims) == sizeof...(RDims) &&
|
||||
detail::shape_compare_impl(std::make_index_sequence<sizeof...(LDims)>{}, lhs, rhs);
|
||||
}
|
||||
|
||||
template <template <std::size_t...> class L,
|
||||
std::size_t... LDims,
|
||||
template <std::size_t...> class R,
|
||||
std::size_t... RDims>
|
||||
bool operator!=(const L<LDims...>& lhs, const R<RDims...>& rhs) {
|
||||
return sizeof...(LDims) != sizeof...(RDims) ||
|
||||
!detail::shape_compare_impl(std::make_index_sequence<sizeof...(LDims)>{}, lhs, rhs);
|
||||
}
|
||||
|
||||
} // namespace tulip
|
||||
|
||||
#endif // TULIP_TENSOR_SHAPE_HPP
|
||||
@@ -0,0 +1,143 @@
|
||||
#ifndef TULIP_TENSOR_TRAITS_HPP
|
||||
#define TULIP_TENSOR_TRAITS_HPP
|
||||
|
||||
#include "./tensor_fwd.hpp"
|
||||
#include "./tensor_shape.hpp"
|
||||
#include "./type_traits.hpp"
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
namespace tulip {
|
||||
|
||||
template <typename T>
|
||||
struct tensor_storage {};
|
||||
|
||||
template <typename T, std::size_t... Dims>
|
||||
struct tensor_storage<tensor<T, Dims...>> {
|
||||
static_assert(std::is_arithmetic_v<T>);
|
||||
|
||||
using value_type = T;
|
||||
using storage_type = T*;
|
||||
};
|
||||
|
||||
template <typename T, std::size_t... Dims>
|
||||
struct tensor_storage<tensor<inline_storage<T>, Dims...>> {
|
||||
static_assert(std::is_arithmetic_v<T>);
|
||||
|
||||
using value_type = T;
|
||||
using storage_type = T[(Dims * ... * 1)];
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct is_storage_inline : std::false_type {};
|
||||
|
||||
template <typename T>
|
||||
struct is_storage_inline<inline_storage<T>> : std::true_type {};
|
||||
|
||||
template <typename T>
|
||||
static constexpr auto is_storage_inline_v = is_storage_inline<T>::value;
|
||||
|
||||
template <typename T>
|
||||
struct detach_storage {
|
||||
using type = T;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct detach_storage<inline_storage<T>> {
|
||||
using type = T;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
using detach_storage_t = typename detach_storage<T>::type;
|
||||
|
||||
template <typename T>
|
||||
struct is_tensor : std::false_type {};
|
||||
|
||||
template <typename T, std::size_t... Dims>
|
||||
struct is_tensor<tensor<T, Dims...>> : std::true_type {};
|
||||
|
||||
template <typename T>
|
||||
static constexpr auto is_tensor_v = is_tensor<T>::value;
|
||||
|
||||
template <typename T>
|
||||
struct is_tensor_dynamic : std::false_type {};
|
||||
|
||||
template <typename T>
|
||||
struct is_tensor_dynamic<tensor<T>> : std::false_type {};
|
||||
|
||||
template <typename T, std::size_t First, std::size_t... Rest>
|
||||
struct is_tensor_dynamic<tensor<T, First, Rest...>> : is_tensor_dynamic<tensor<T, Rest...>> {};
|
||||
|
||||
template <typename T, std::size_t... Rest>
|
||||
struct is_tensor_dynamic<tensor<T, dynamic_extent, Rest...>> : std::true_type {};
|
||||
|
||||
template <typename T>
|
||||
static constexpr auto is_tensor_dynamic_v = is_tensor_dynamic<T>::value;
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <typename>
|
||||
struct tensor_with_storage {};
|
||||
|
||||
template <>
|
||||
struct tensor_with_storage<heap_storage_tag> {
|
||||
template <typename T, std::size_t... Dims>
|
||||
using type = tensor<detach_storage_t<T>, Dims...>;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct tensor_with_storage<inline_storage_tag> {
|
||||
template <typename T, std::size_t... Dims>
|
||||
using type = tensor<inline_storage<detach_storage_t<T>>, Dims...>;
|
||||
};
|
||||
|
||||
template <typename T, typename Shape>
|
||||
struct tensor_from_shape {};
|
||||
|
||||
template <typename T, std::size_t... Dims>
|
||||
struct tensor_from_shape<T, static_shape<Dims...>> {
|
||||
using type = tensor<T, Dims...>;
|
||||
};
|
||||
|
||||
template <typename T, std::size_t... Dims>
|
||||
struct tensor_from_shape<T, dynamic_shape<Dims...>> {
|
||||
using type = tensor<T, Dims...>;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template <typename T>
|
||||
struct tensor_traits {};
|
||||
|
||||
template <typename T, std::size_t... Dims>
|
||||
struct tensor_traits<tensor<T, Dims...>> {
|
||||
using tensor_type = tensor<T, Dims...>;
|
||||
using value_type = typename tensor_storage<tensor_type>::value_type;
|
||||
using storage_type = typename tensor_storage<tensor_type>::storage_type;
|
||||
|
||||
static constexpr auto rank = sizeof...(Dims);
|
||||
static constexpr auto dynamic = is_tensor_dynamic_v<tensor_type>;
|
||||
|
||||
static constexpr auto inline_storage = is_storage_inline_v<T>;
|
||||
static_assert(!inline_storage || !dynamic, "Tensor storage set to inline with dynamic extents!");
|
||||
|
||||
static constexpr std::enable_if_t<(sizeof...(Dims) > 0), std::size_t[sizeof...(Dims)]> dimensions = { Dims... };
|
||||
|
||||
static constexpr std::size_t size = dynamic ? dynamic_extent : (Dims * ... * 1);
|
||||
|
||||
template <typename ST>
|
||||
using with_storage_t = typename detail::tensor_with_storage<ST>::template type<T, Dims...>;
|
||||
};
|
||||
|
||||
template <typename T, typename Shape>
|
||||
constexpr decltype(auto) tensor_from_shape(const Shape& shape) {
|
||||
typename detail::tensor_from_shape<T, remove_cvref_t<Shape>>::type tensor;
|
||||
if constexpr (!detail::is_static_shape_v<Shape>) {
|
||||
tensor.set_dimensions(shape);
|
||||
}
|
||||
return tensor;
|
||||
}
|
||||
|
||||
} // namespace tulip
|
||||
|
||||
#endif // TULIP_TENSOR_TRAITS_HPP
|
||||
@@ -0,0 +1,136 @@
|
||||
#ifndef TULIP_TENSOR_VIEW_HPP
|
||||
#define TULIP_TENSOR_VIEW_HPP
|
||||
|
||||
#include "./expr.hpp"
|
||||
#include "./tensor_fwd.hpp"
|
||||
#include "./tensor_shape.hpp"
|
||||
#include "./tensor_traits.hpp"
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <random>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace tulip {
|
||||
|
||||
template <typename T, std::size_t... Dims>
|
||||
class tensor_view : public expression_tag, public ops_provider<tensor_view<T, Dims...>> {
|
||||
static_assert(std::is_arithmetic_v<T>);
|
||||
|
||||
template <typename, std::size_t...>
|
||||
friend class tensor;
|
||||
|
||||
template <typename, std::size_t...>
|
||||
friend class tensor_view;
|
||||
public:
|
||||
using traits = tensor_traits<tensor<T, Dims...>>;
|
||||
using shape_type = std::conditional_t<traits::dynamic, dynamic_shape<Dims...>, static_shape<Dims...>>;
|
||||
using value_type = T;
|
||||
public:
|
||||
template <typename... Offs,
|
||||
typename = std::enable_if_t<(
|
||||
sizeof...(Offs) > 0 && sizeof...(Offs) <= sizeof...(Dims) && (std::is_integral_v<Offs> && ...))>>
|
||||
decltype(auto) slice(Offs... offs) {
|
||||
auto* ptr = m_view + m_shape.calculate_offset(std::make_tuple(offs...));
|
||||
if constexpr (sizeof...(Offs) == sizeof...(Dims))
|
||||
return *ptr;
|
||||
else
|
||||
return inner_view<T, sizeof...(Offs)>(ptr, std::index_sequence<Dims...>{});
|
||||
}
|
||||
|
||||
template <typename... Offs,
|
||||
typename = std::enable_if_t<(
|
||||
sizeof...(Offs) > 0 && sizeof...(Offs) <= sizeof...(Dims) && (std::is_integral_v<Offs> && ...))>>
|
||||
decltype(auto) slice(Offs... offs) const {
|
||||
const auto* ptr = m_view + m_shape.calculate_offset(std::make_tuple(offs...));
|
||||
if constexpr (sizeof...(Offs) == sizeof...(Dims))
|
||||
return *ptr;
|
||||
else
|
||||
return inner_view<const T, sizeof...(Offs)>(ptr, std::index_sequence<Dims...>{});
|
||||
}
|
||||
public:
|
||||
template <typename Off, typename = std::enable_if_t<std::is_integral_v<Off>>>
|
||||
decltype(auto) operator[](Off off) {
|
||||
return slice(off);
|
||||
}
|
||||
|
||||
template <typename Off, typename = std::enable_if_t<std::is_integral_v<Off>>>
|
||||
decltype(auto) operator[](Off off) const {
|
||||
return slice(off);
|
||||
}
|
||||
public:
|
||||
constexpr std::size_t size() const { return size_impl(std::make_index_sequence<sizeof...(Dims)>{}); }
|
||||
|
||||
auto dimensions() const { return dimensions_impl(std::make_index_sequence<sizeof...(Dims)>{}); }
|
||||
private:
|
||||
template <std::size_t... Is>
|
||||
constexpr std::size_t size_impl(std::index_sequence<Is...>) const {
|
||||
return (dimension<Is>() * ... * 1);
|
||||
}
|
||||
|
||||
template <std::size_t... Is>
|
||||
constexpr auto dimensions_impl(std::index_sequence<Is...>) const {
|
||||
return std::make_tuple(dimension<Is>()...);
|
||||
}
|
||||
public:
|
||||
void fill(value_type fill = {}) {
|
||||
for (std::size_t i = 0; i < size(); ++i) {
|
||||
m_view[i] = fill;
|
||||
}
|
||||
}
|
||||
|
||||
void fill(const value_type* source, std::size_t offset = 0, std::size_t length = dynamic_extent) {
|
||||
offset = std::min(offset, size());
|
||||
std::memmove(m_view + offset, source, std::min(length, size() - offset) * sizeof(*m_view));
|
||||
}
|
||||
|
||||
void fill(const tensor_view& view) { fill(view.m_view, 0, view.size()); }
|
||||
|
||||
template <typename Dist>
|
||||
void fill_random(Dist dist) {
|
||||
static std::random_device s_rd;
|
||||
static std::mt19937 s_mt(s_rd());
|
||||
|
||||
for (std::size_t i = 0; i < size(); ++i) {
|
||||
m_view[i] = dist(s_mt);
|
||||
}
|
||||
}
|
||||
public:
|
||||
value_type& coeff(std::size_t index) { return m_view[index]; }
|
||||
const value_type& coeff(std::size_t index) const { return m_view[index]; }
|
||||
|
||||
value_type* data() { return m_view; }
|
||||
const value_type* data() const { return m_view; }
|
||||
|
||||
const shape_type& shape() const { return m_shape; }
|
||||
private:
|
||||
template <typename InT, std::size_t Off, std::size_t First, std::size_t... Rest>
|
||||
auto inner_view(value_type* view, std::index_sequence<First, Rest...>) {
|
||||
if constexpr (Off == 0)
|
||||
return tensor_view<InT, First, Rest...>{ view, m_shape };
|
||||
else
|
||||
return inner_view<InT, Off - 1>(view, std::index_sequence<Rest...>{});
|
||||
}
|
||||
private:
|
||||
template <std::size_t I>
|
||||
constexpr std::size_t dimension() const {
|
||||
if constexpr (traits::dynamic && traits::dimensions[I] == dynamic_extent) {
|
||||
return m_shape.dimensions[I];
|
||||
} else {
|
||||
return traits::dimensions[I];
|
||||
}
|
||||
}
|
||||
private:
|
||||
template <typename Fwd>
|
||||
tensor_view(value_type* view, Fwd&& shape)
|
||||
: m_view(view), m_shape(std::forward<Fwd>(shape)) {}
|
||||
private:
|
||||
value_type* m_view;
|
||||
|
||||
shape_type m_shape;
|
||||
};
|
||||
|
||||
} // namespace tulip
|
||||
|
||||
#endif // TULIP_TENSOR_VIEW_HPP
|
||||
@@ -0,0 +1,13 @@
|
||||
#ifndef TULIP_TYPE_TRAITS_HPP
|
||||
#define TULIP_TYPE_TRAITS_HPP
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
namespace tulip {
|
||||
|
||||
template <typename T>
|
||||
using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>;
|
||||
|
||||
} // namespace tulip
|
||||
|
||||
#endif // TULIP_TYPE_TRAITS_HPP
|
||||
Reference in New Issue
Block a user