Compare commits
8
Commits
22bf8db5e8
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
513413515e
|
||
|
|
2702167244
|
||
|
|
1b8db65330
|
||
|
|
a0ca8e572d
|
||
|
|
bdb0198640
|
||
|
|
e8edbe7b7e
|
||
|
|
5360b4f9b0
|
||
|
|
a4640c4835
|
+21
-1
@@ -2,7 +2,8 @@ cmake_minimum_required(VERSION 3.14)
|
|||||||
|
|
||||||
project(cenet CXX)
|
project(cenet CXX)
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 20)
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
add_library(cenet INTERFACE
|
add_library(cenet INTERFACE
|
||||||
include/cenet/mat.hpp
|
include/cenet/mat.hpp
|
||||||
@@ -20,3 +21,22 @@ target_link_libraries(mnist PRIVATE cenet)
|
|||||||
|
|
||||||
add_executable(tokenizer example/tokenizer.cpp)
|
add_executable(tokenizer example/tokenizer.cpp)
|
||||||
target_link_libraries(tokenizer PRIVATE cenet)
|
target_link_libraries(tokenizer PRIVATE cenet)
|
||||||
|
|
||||||
|
add_executable(gpt example/gpt.cpp)
|
||||||
|
target_link_libraries(gpt PRIVATE cenet)
|
||||||
|
|
||||||
|
include(FetchContent)
|
||||||
|
FetchContent_Declare(
|
||||||
|
googletest
|
||||||
|
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
|
||||||
|
)
|
||||||
|
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
|
||||||
|
FetchContent_MakeAvailable(googletest)
|
||||||
|
|
||||||
|
enable_testing()
|
||||||
|
|
||||||
|
add_executable(math_test test/tensor.cpp)
|
||||||
|
target_link_libraries(math_test GTest::gtest_main cenet)
|
||||||
|
|
||||||
|
include(GoogleTest)
|
||||||
|
gtest_discover_tests(math_test)
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
#include "cenet/network.hpp"
|
||||||
|
#include "tokenizer.hpp"
|
||||||
|
|
||||||
|
#include <cenet/cenet.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
static constexpr std::size_t VOCAB_SIZE = 2256;
|
||||||
|
|
||||||
|
using network_type = cenet::network<std::vector<std::uint32_t>, cenet::tok2vec_layer<2256, 512>>;
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
tokenizer<VOCAB_SIZE> bpe;
|
||||||
|
if (!bpe.load("./gpt/shakespeare.bpe")) return 1;
|
||||||
|
std::cout << "Vocabulary loaded!\n";
|
||||||
|
|
||||||
|
auto* gpt = new network_type();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -283,8 +283,8 @@ private:
|
|||||||
file.write(value.data(), static_cast<std::streamsize>(value.size()));
|
file.write(value.data(), static_cast<std::streamsize>(value.size()));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <std::size_t... Is>
|
template <typename T, std::size_t... Is>
|
||||||
void dump(std::ofstream& file, auto value, std::index_sequence<Is...>) const {
|
void dump(std::ofstream& file, T value, std::index_sequence<Is...>) const {
|
||||||
char buf[] = { static_cast<char>((value >> (Is << 3ULL)) & 0xFF)... };
|
char buf[] = { static_cast<char>((value >> (Is << 3ULL)) & 0xFF)... };
|
||||||
file.write(buf, sizeof(buf));
|
file.write(buf, sizeof(buf));
|
||||||
}
|
}
|
||||||
@@ -308,7 +308,7 @@ public:
|
|||||||
auto lhs = load_u32(file);
|
auto lhs = load_u32(file);
|
||||||
auto rhs = load_u32(file);
|
auto rhs = load_u32(file);
|
||||||
auto merge = load_u32(file);
|
auto merge = load_u32(file);
|
||||||
m_merges.insert(lhs | (rhs << 32ULL), merge);
|
m_merges.emplace(lhs | (static_cast<std::uint64_t>(rhs) << 32ULL), merge);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -326,7 +326,7 @@ private:
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T load(std::ifstream& file) const {
|
T load(std::ifstream& file) const {
|
||||||
return load(file, std::make_index_sequence<sizeof(T)>{});
|
return load<T>(file, std::make_index_sequence<sizeof(T)>{});
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, std::size_t... Is>
|
template <typename T, std::size_t... Is>
|
||||||
|
|||||||
+77
-8
@@ -9,6 +9,7 @@
|
|||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <istream>
|
#include <istream>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
#include <stdexcept>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@@ -577,31 +578,99 @@ private:
|
|||||||
using input_type = typename input_type_s<NN, I>::type;
|
using input_type = typename input_type_s<NN, I>::type;
|
||||||
static_assert(detail::is_tokens_v<input_type>);
|
static_assert(detail::is_tokens_v<input_type>);
|
||||||
|
|
||||||
using output_type = math::matrix<float, math::dynamic_extent, EmbedDims>;
|
static_assert(VocabSize > 0);
|
||||||
|
static_assert(EmbedDims > 0);
|
||||||
|
|
||||||
|
using output_type = math::matrix<float, math::dynamic_extent, EmbedDims>;
|
||||||
|
using embedding_type = math::matrix<float, VocabSize, EmbedDims>;
|
||||||
|
|
||||||
output_type output;
|
output_type output;
|
||||||
|
|
||||||
|
embedding_type embedding;
|
||||||
|
|
||||||
struct gradient final {
|
struct gradient final {
|
||||||
void reset() {}
|
void reset() { embedding.view().fill(0); }
|
||||||
|
|
||||||
|
embedding_type embedding;
|
||||||
|
embedding_type momentum;
|
||||||
|
embedding_type variance;
|
||||||
|
|
||||||
|
std::size_t time = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
void dump(std::ostream& os) const {}
|
void dump(std::ostream& os) const {
|
||||||
|
os.write(reinterpret_cast<const char*>(embedding.data()), sizeof(float) * embedding.size());
|
||||||
|
}
|
||||||
|
|
||||||
void load(std::istream& is) {}
|
void load(std::istream& is) {
|
||||||
|
is.read(reinterpret_cast<char*>(embedding.data()), sizeof(float) * embedding.size());
|
||||||
|
}
|
||||||
|
|
||||||
template <typename Dist>
|
template <typename Dist>
|
||||||
void fill_random(const Dist& dist) {}
|
void fill_random(const Dist& dist) {
|
||||||
|
static std::random_device s_rd;
|
||||||
|
static std::mt19937 s_rng(s_rd());
|
||||||
|
|
||||||
|
auto distribution = dist;
|
||||||
|
|
||||||
|
const float scale = 1.0F / std::sqrt(static_cast<float>(EmbedDims));
|
||||||
|
|
||||||
|
for (std::size_t i = 0; i < embedding.size(); ++i) {
|
||||||
|
embedding.data()[i] = static_cast<float>(distribution(s_rng)) * scale;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
auto& forward(const input_type& input) {
|
auto& forward(const input_type& input) {
|
||||||
output.set_dimensions(input.size(), EmbedDims);
|
output.set_dimensions(input.size(), EmbedDims);
|
||||||
|
for (std::size_t i = 0; i < input.size(); ++i) {
|
||||||
|
if (input[i] >= VocabSize) throw std::out_of_range("invalid token!");
|
||||||
|
output.view()[i].fill(embedding.view()[input[i]]);
|
||||||
|
}
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Error>
|
template <typename Error>
|
||||||
auto backward(const input_type& input, gradient& grad, Error prevErr) {
|
void backward(const input_type& input, gradient& grad, Error prevErr) {
|
||||||
return std::move(prevErr);
|
const auto errorShape = prevErr.shape();
|
||||||
|
if (errorShape.template get<0>() != input.size() || errorShape.template get<1>() != EmbedDims) {
|
||||||
|
throw std::invalid_argument("unexpected embedding gradient shape");
|
||||||
|
}
|
||||||
|
|
||||||
|
for (std::size_t i = 0; i < input.size(); ++i) {
|
||||||
|
grad.embedding.view()[input[i]] += prevErr.view()[i];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void apply_grad(gradient& grad, double divisor, double rate) {}
|
void apply_grad(gradient& grad, double divisor, double rate) {
|
||||||
|
if (divisor <= 0.0) return;
|
||||||
|
|
||||||
|
++grad.time;
|
||||||
|
|
||||||
|
constexpr double beta1 = 0.9;
|
||||||
|
constexpr double beta2 = 0.999;
|
||||||
|
constexpr double epsilon = 1e-8;
|
||||||
|
|
||||||
|
const double correction1 = 1.0 - std::pow(beta1, grad.time);
|
||||||
|
const double correction2 = 1.0 - std::pow(beta2, grad.time);
|
||||||
|
|
||||||
|
float* weights = embedding.data();
|
||||||
|
float* momentum = grad.momentum.data();
|
||||||
|
float* variance = grad.variance.data();
|
||||||
|
|
||||||
|
for (std::size_t i = 0; i < embedding.size(); ++i) {
|
||||||
|
const double avgGrad = static_cast<double>(grad.embedding.coeff(i)) / divisor;
|
||||||
|
|
||||||
|
grad.momentum.coeff(i) = static_cast<float>(beta1 * grad.momentum.coeff(i) + (1.0 - beta1) * avgGrad);
|
||||||
|
grad.variance.coeff(i) =
|
||||||
|
static_cast<float>(beta2 * grad.variance.coeff(i) + (1.0 - beta2) * avgGrad * avgGrad);
|
||||||
|
|
||||||
|
const double momentumCorrected = grad.momentum.coeff(i) / correction1;
|
||||||
|
const double varianceCorrected = grad.variance.coeff(i) / correction2;
|
||||||
|
|
||||||
|
embedding.coeff(i) -=
|
||||||
|
static_cast<float>(rate * momentumCorrected / (std::sqrt(varianceCorrected) + epsilon));
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,9 @@ template <typename E, typename Op>
|
|||||||
struct unary_expression : expression_tag {
|
struct unary_expression : expression_tag {
|
||||||
E expr;
|
E expr;
|
||||||
|
|
||||||
auto eval(std::size_t index) { return Op{}(expr.coeff(index)); }
|
constexpr auto shape() const { return expr.shape(); }
|
||||||
|
|
||||||
|
auto coeff(std::size_t index) const { return Op{}(expr.coeff(index)); }
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename L, typename R, typename Op>
|
template <typename L, typename R, typename Op>
|
||||||
@@ -28,7 +30,9 @@ struct binary_expression : expression_tag {
|
|||||||
binary_expression(LFwd&& lhs, RFwd&& rhs)
|
binary_expression(LFwd&& lhs, RFwd&& rhs)
|
||||||
: lhs(std::forward<LFwd>(lhs)), rhs(std::forward<RFwd>(rhs)) {}
|
: lhs(std::forward<LFwd>(lhs)), rhs(std::forward<RFwd>(rhs)) {}
|
||||||
|
|
||||||
auto eval(std::size_t index) { return Op{}(index, lhs.coeff(index), as_operand(rhs)); }
|
constexpr auto shape() const { return Op::shape(lhs, rhs); }
|
||||||
|
|
||||||
|
auto coeff(std::size_t index) const { return Op{}(index, lhs.coeff(index), as_operand(rhs)); }
|
||||||
private:
|
private:
|
||||||
template <typename T>
|
template <typename T>
|
||||||
static decltype(auto) as_operand(T&& value) {
|
static decltype(auto) as_operand(T&& value) {
|
||||||
@@ -95,6 +99,14 @@ binary_expression<L, R, Op> operator-(L&& lhs, R&& rhs) {
|
|||||||
return { std::forward<L>(lhs), std::forward<R>(rhs) };
|
return { std::forward<L>(lhs), std::forward<R>(rhs) };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename L,
|
||||||
|
typename R,
|
||||||
|
typename Op = ops::multiply,
|
||||||
|
typename = std::void_t<typename detail::expression_result<binary_expression<L, R, Op>>::type>>
|
||||||
|
binary_expression<L, R, Op> operator*(L&& lhs, R&& rhs) {
|
||||||
|
return { std::forward<L>(lhs), std::forward<R>(rhs) };
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace math
|
} // namespace math
|
||||||
} // namespace cenet
|
} // namespace cenet
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
#ifndef CENET_MATH_OPS_HPP
|
#ifndef CENET_MATH_OPS_HPP
|
||||||
#define CENET_MATH_OPS_HPP
|
#define CENET_MATH_OPS_HPP
|
||||||
|
|
||||||
|
#include "tensor_shape.hpp"
|
||||||
#include "tensor_traits.hpp"
|
#include "tensor_traits.hpp"
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
namespace cenet {
|
namespace cenet {
|
||||||
namespace math {
|
namespace math {
|
||||||
@@ -14,15 +17,34 @@ private:
|
|||||||
template <typename L, typename R>
|
template <typename L, typename R>
|
||||||
struct result_type_impl {};
|
struct result_type_impl {};
|
||||||
|
|
||||||
template <typename LT, typename RT, std::size_t... Dims>
|
template <typename LT, std::size_t... LDims, typename RT, std::size_t... RDims>
|
||||||
struct result_type_impl<tensor_view<LT, Dims...>, tensor_view<RT, Dims...>> {
|
struct result_type_impl<tensor_view<LT, LDims...>, tensor_view<RT, RDims...>> {
|
||||||
using type =
|
using lhs_view = tensor_view<LT, LDims...>;
|
||||||
tensor<decltype(std::declval<detach_storage_t<LT>>() + std::declval<detach_storage_t<RT>>()), Dims...>;
|
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:
|
public:
|
||||||
template <typename L, typename R>
|
template <typename L, typename R>
|
||||||
using result_type = typename result_type_impl<L, R>::type;
|
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>
|
template <typename LT, typename R>
|
||||||
constexpr auto operator()(std::size_t index, LT lhs, const R& rhs) const {
|
constexpr auto operator()(std::size_t index, LT lhs, const R& rhs) const {
|
||||||
return lhs + rhs.coeff(index);
|
return lhs + rhs.coeff(index);
|
||||||
@@ -34,21 +56,79 @@ private:
|
|||||||
template <typename L, typename R>
|
template <typename L, typename R>
|
||||||
struct result_type_impl {};
|
struct result_type_impl {};
|
||||||
|
|
||||||
template <typename LT, typename RT, std::size_t... Dims>
|
template <typename LT, std::size_t... LDims, typename RT, std::size_t... RDims>
|
||||||
struct result_type_impl<tensor_view<LT, Dims...>, tensor_view<RT, Dims...>> {
|
struct result_type_impl<tensor_view<LT, LDims...>, tensor_view<RT, RDims...>> {
|
||||||
using type =
|
using lhs_view = tensor_view<LT, LDims...>;
|
||||||
tensor<decltype(std::declval<detach_storage_t<LT>>() - std::declval<detach_storage_t<RT>>()), Dims...>;
|
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:
|
public:
|
||||||
template <typename L, typename R>
|
template <typename L, typename R>
|
||||||
using result_type = typename result_type_impl<L, R>::type;
|
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>
|
template <typename LT, typename R>
|
||||||
constexpr auto operator()(std::size_t index, LT lhs, const R& rhs) const {
|
constexpr auto operator()(std::size_t index, LT lhs, const R& rhs) const {
|
||||||
return lhs - rhs.coeff(index);
|
return lhs - rhs.coeff(index);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class multiply {
|
||||||
|
private:
|
||||||
|
template <typename L, typename R>
|
||||||
|
struct result_type_impl {};
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace ops
|
} // namespace ops
|
||||||
} // namespace math
|
} // namespace math
|
||||||
} // namespace cenet
|
} // namespace cenet
|
||||||
|
|||||||
+79
-181
@@ -3,7 +3,9 @@
|
|||||||
|
|
||||||
#include "expr.hpp"
|
#include "expr.hpp"
|
||||||
#include "tensor_fwd.hpp"
|
#include "tensor_fwd.hpp"
|
||||||
|
#include "tensor_shape.hpp"
|
||||||
#include "tensor_traits.hpp"
|
#include "tensor_traits.hpp"
|
||||||
|
#include "tensor_view.hpp"
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
@@ -14,170 +16,6 @@
|
|||||||
namespace cenet {
|
namespace cenet {
|
||||||
namespace math {
|
namespace math {
|
||||||
|
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
template <std::size_t... Dims>
|
|
||||||
struct static_shape {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
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... Dims>
|
|
||||||
struct dynamic_shape {
|
|
||||||
dynamic_shape() = default;
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
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> {};
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
template <typename T, std::size_t... Dims>
|
|
||||||
class tensor_view {
|
|
||||||
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 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 + calculate_offset(std::make_tuple(offs...));
|
|
||||||
if constexpr (sizeof...(Offs) == sizeof...(Dims)) return *ptr;
|
|
||||||
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 + calculate_offset(std::make_tuple(offs...));
|
|
||||||
if constexpr (sizeof...(Offs) == sizeof...(Dims)) return *ptr;
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
template <typename... Offs>
|
|
||||||
std::size_t calculate_offset(const std::tuple<Offs...>& offs) const {
|
|
||||||
return calculate_offset_impl<sizeof...(Offs) - 1, sizeof...(Dims) - sizeof...(Offs)>(offs);
|
|
||||||
}
|
|
||||||
|
|
||||||
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<I>(offs) * dimension<Start + I>();
|
|
||||||
else
|
|
||||||
return (calculate_offset_impl<I - 1, Start>(offs) + std::get<I>(offs)) * dimension<Start + I>();
|
|
||||||
}
|
|
||||||
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_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; }
|
|
||||||
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:
|
|
||||||
using shape_type =
|
|
||||||
std::conditional_t<traits::dynamic, detail::dynamic_shape<Dims...>, detail::static_shape<Dims...>>;
|
|
||||||
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename T, std::size_t... Dims>
|
template <typename T, std::size_t... Dims>
|
||||||
class tensor {
|
class tensor {
|
||||||
public:
|
public:
|
||||||
@@ -186,9 +24,7 @@ public:
|
|||||||
using constant_value_type = const value_type;
|
using constant_value_type = const value_type;
|
||||||
using view_type = tensor_view<value_type, Dims...>;
|
using view_type = tensor_view<value_type, Dims...>;
|
||||||
using constant_view_type = tensor_view<constant_value_type, Dims...>;
|
using constant_view_type = tensor_view<constant_value_type, Dims...>;
|
||||||
private:
|
using shape_type = std::conditional_t<traits::dynamic, dynamic_shape<Dims...>, static_shape<Dims...>>;
|
||||||
using shape_type =
|
|
||||||
std::conditional_t<traits::dynamic, detail::dynamic_shape<Dims...>, detail::static_shape<Dims...>>;
|
|
||||||
public:
|
public:
|
||||||
tensor() {
|
tensor() {
|
||||||
if constexpr (traits::dynamic) {
|
if constexpr (traits::dynamic) {
|
||||||
@@ -205,7 +41,7 @@ public:
|
|||||||
typename = std::enable_if_t<(
|
typename = std::enable_if_t<(
|
||||||
traits::dynamic && sizeof...(RtDims) == sizeof...(Dims) && (std::is_integral_v<RtDims> && ...))>>
|
traits::dynamic && sizeof...(RtDims) == sizeof...(Dims) && (std::is_integral_v<RtDims> && ...))>>
|
||||||
explicit tensor(RtDims... dimensions)
|
explicit tensor(RtDims... dimensions)
|
||||||
: m_shape(detail::dynamic_shape<Dims...>{}) {
|
: m_shape(dynamic_shape<Dims...>{}) {
|
||||||
set_dimensions(dimensions...);
|
set_dimensions(dimensions...);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -213,29 +49,35 @@ public:
|
|||||||
|
|
||||||
tensor(tensor&& other) noexcept
|
tensor(tensor&& other) noexcept
|
||||||
: m_shape(std::move(other.m_shape)) {
|
: m_shape(std::move(other.m_shape)) {
|
||||||
if constexpr (!traits::inline_storage) m_items = nullptr;
|
if constexpr (!traits::inline_storage) {
|
||||||
*this = std::move(other);
|
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 {
|
tensor& operator=(tensor&& other) noexcept {
|
||||||
if (this == &other) return *this;
|
if (this == &other) return *this;
|
||||||
release();
|
release();
|
||||||
m_items = other.m_items;
|
if constexpr (!traits::inline_storage) {
|
||||||
if constexpr (!traits::inline_storage) other.m_items = nullptr;
|
m_items = other.m_items;
|
||||||
|
other.m_items = nullptr;
|
||||||
|
} else {
|
||||||
|
std::memcpy(m_items, other.m_items, sizeof(m_items));
|
||||||
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
tensor(const tensor& other)
|
tensor(const tensor& other)
|
||||||
: m_shape(other.m_shape) {
|
: m_shape(other.m_shape) {
|
||||||
if constexpr (!traits::inline_storage) m_items = nullptr;
|
dyn_alloc(std::make_index_sequence<sizeof...(Dims)>{});
|
||||||
*this = other;
|
dyn_copy(std::make_index_sequence<sizeof...(Dims)>{});
|
||||||
}
|
}
|
||||||
|
|
||||||
tensor& operator=(const tensor& other) {
|
tensor& operator=(const tensor& other) {
|
||||||
if (this == &other) return *this;
|
if (this == &other) return *this;
|
||||||
release();
|
release();
|
||||||
std::memcpy(m_shape.dimensions, other.m_shape.dimensions, sizeof(m_shape.dimensions));
|
|
||||||
std::memcpy(m_shape.strides, other.m_shape.strides, sizeof(m_shape.strides));
|
|
||||||
dyn_alloc(std::make_index_sequence<sizeof...(Dims)>{});
|
dyn_alloc(std::make_index_sequence<sizeof...(Dims)>{});
|
||||||
dyn_copy(std::make_index_sequence<sizeof...(Dims)>{});
|
dyn_copy(std::make_index_sequence<sizeof...(Dims)>{});
|
||||||
|
|
||||||
@@ -247,12 +89,15 @@ public:
|
|||||||
shape_type::template compatible<typename detail::expression_result<E>::type>()>>
|
shape_type::template compatible<typename detail::expression_result<E>::type>()>>
|
||||||
tensor(E&& expr) { // NOLINT
|
tensor(E&& expr) { // NOLINT
|
||||||
if constexpr (!traits::inline_storage) {
|
if constexpr (!traits::inline_storage) {
|
||||||
static_assert(!traits::dynamic, "Dynamic tensors are not supported yet!");
|
if constexpr (traits::dynamic) {
|
||||||
m_items = new value_type[traits::size];
|
set_dimensions(expr.shape());
|
||||||
|
} else {
|
||||||
|
m_items = new value_type[traits::size];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (std::size_t i = 0; i < view().size(); ++i) {
|
for (std::size_t i = 0; i < view().size(); ++i) {
|
||||||
m_items[i] = std::forward<E>(expr).eval(i);
|
m_items[i] = std::forward<E>(expr).coeff(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -260,10 +105,51 @@ public:
|
|||||||
typename = std::enable_if_t<detail::is_expression_v<E> && !is_tensor_v<E> &&
|
typename = std::enable_if_t<detail::is_expression_v<E> && !is_tensor_v<E> &&
|
||||||
std::is_same_v<tensor, typename detail::expression_result<E>::type>>>
|
std::is_same_v<tensor, typename detail::expression_result<E>::type>>>
|
||||||
tensor& operator=(E&& expr) {
|
tensor& operator=(E&& expr) {
|
||||||
|
if (!expr.valid_for(m_shape)) throw std::runtime_error("unexpected tensor shape");
|
||||||
for (std::size_t i = 0; i < view().size(); ++i) {
|
for (std::size_t i = 0; i < view().size(); ++i) {
|
||||||
m_items[i] = std::forward<E>(expr);
|
m_items[i] = std::forward<E>(expr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename E,
|
||||||
|
typename = std::enable_if_t<detail::is_expression_v<E> && !is_tensor_v<E> &&
|
||||||
|
std::is_same_v<tensor, typename detail::expression_result<E>::type>>>
|
||||||
|
tensor& operator+=(E&& expr) {
|
||||||
|
if (!expr.valid_for(m_shape)) throw std::runtime_error("unexpected tensor shape");
|
||||||
|
for (std::size_t i = 0; i < view().size(); ++i) {
|
||||||
|
m_items[i] += std::forward<E>(expr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename E,
|
||||||
|
typename = std::enable_if_t<detail::is_expression_v<E> && !is_tensor_v<E> &&
|
||||||
|
std::is_same_v<tensor, typename detail::expression_result<E>::type>>>
|
||||||
|
tensor& operator-=(E&& expr) {
|
||||||
|
if (!expr.valid_for(m_shape)) throw std::runtime_error("unexpected tensor shape");
|
||||||
|
for (std::size_t i = 0; i < view().size(); ++i) {
|
||||||
|
m_items[i] -= std::forward<E>(expr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename E,
|
||||||
|
typename = std::enable_if_t<detail::is_expression_v<E> && !is_tensor_v<E> &&
|
||||||
|
std::is_same_v<tensor, typename detail::expression_result<E>::type>>>
|
||||||
|
tensor& operator*=(E&& expr) {
|
||||||
|
if (!expr.valid_for(m_shape)) throw std::runtime_error("unexpected tensor shape");
|
||||||
|
for (std::size_t i = 0; i < view().size(); ++i) {
|
||||||
|
m_items[i] *= std::forward<E>(expr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename E,
|
||||||
|
typename = std::enable_if_t<detail::is_expression_v<E> && !is_tensor_v<E> &&
|
||||||
|
std::is_same_v<tensor, typename detail::expression_result<E>::type>>>
|
||||||
|
tensor& operator/=(E&& expr) {
|
||||||
|
if (!expr.valid_for(m_shape)) throw std::runtime_error("unexpected tensor shape");
|
||||||
|
for (std::size_t i = 0; i < view().size(); ++i) {
|
||||||
|
m_items[i] /= std::forward<E>(expr);
|
||||||
|
}
|
||||||
|
}
|
||||||
public:
|
public:
|
||||||
view_type view() { return { m_items, m_shape }; }
|
view_type view() { return { m_items, m_shape }; }
|
||||||
constant_view_type view() const { return { m_items, m_shape }; }
|
constant_view_type view() const { return { m_items, m_shape }; }
|
||||||
@@ -283,6 +169,16 @@ public:
|
|||||||
m_items = new value_type[(dimensions * ... * 1)];
|
m_items = new value_type[(dimensions * ... * 1)];
|
||||||
std::memset(m_items, 0, sizeof(*m_items) * (dimensions * ... * 1));
|
std::memset(m_items, 0, sizeof(*m_items) * (dimensions * ... * 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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:
|
public:
|
||||||
constexpr std::size_t size() const { return size_impl(std::make_index_sequence<sizeof...(Dims)>{}); }
|
constexpr std::size_t size() const { return size_impl(std::make_index_sequence<sizeof...(Dims)>{}); }
|
||||||
|
|
||||||
@@ -303,10 +199,12 @@ public:
|
|||||||
|
|
||||||
value_type* data() { return m_items; }
|
value_type* data() { return m_items; }
|
||||||
const value_type* data() const { return m_items; }
|
const value_type* data() const { return m_items; }
|
||||||
|
|
||||||
|
const shape_type& shape() const { return m_shape; }
|
||||||
private:
|
private:
|
||||||
template <typename... RtDims, std::size_t... Is>
|
template <typename... RtDims, std::size_t... Is>
|
||||||
constexpr bool valid_dims(std::index_sequence<Is...>, RtDims... dimensions) const {
|
constexpr bool valid_dims(std::index_sequence<Is...>, RtDims... dimensions) const {
|
||||||
return (valid_dim<Is>(dimensions) && ...);
|
return (valid_dim<decltype(dimensions), Is>(dimensions) && ...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename RtDim, std::size_t I>
|
template <typename RtDim, std::size_t I>
|
||||||
@@ -345,7 +243,7 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
typename traits::storage_type m_items;
|
typename traits::storage_type m_items{};
|
||||||
shape_type m_shape;
|
shape_type m_shape;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,196 @@
|
|||||||
|
#ifndef CENET_MATH_TENSOR_SHAPE_HPP
|
||||||
|
#define CENET_MATH_TENSOR_SHAPE_HPP
|
||||||
|
|
||||||
|
#include "tensor_fwd.hpp"
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
namespace cenet {
|
||||||
|
namespace math {
|
||||||
|
|
||||||
|
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... RDims>
|
||||||
|
bool operator==(const static_shape<RDims...>& rhs) const {
|
||||||
|
return sizeof...(Dims) == sizeof...(RDims) && ((Dims == RDims) && ...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <std::size_t... RDims>
|
||||||
|
bool operator!=(const static_shape<RDims...>& rhs) const {
|
||||||
|
return sizeof...(Dims) != sizeof...(RDims) || ((Dims != RDims) || ...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <std::size_t I>
|
||||||
|
constexpr auto get() const {
|
||||||
|
return get_impl<I, Dims...>::value;
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
template <std::size_t... Dims>
|
||||||
|
struct dynamic_shape : shape<Dims...> {
|
||||||
|
dynamic_shape() = default;
|
||||||
|
|
||||||
|
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... RDims>
|
||||||
|
bool operator==(const static_shape<RDims...>& rhs) const {
|
||||||
|
return sizeof...(Dims) == sizeof...(RDims) &&
|
||||||
|
compare_impl<decltype(rhs), RDims...>(std::make_index_sequence<sizeof...(Dims)>{}, rhs);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <std::size_t... RDims>
|
||||||
|
bool operator==(const dynamic_shape<RDims...>& rhs) const {
|
||||||
|
return sizeof...(Dims) == sizeof...(RDims) &&
|
||||||
|
compare_impl<decltype(rhs), RDims...>(std::make_index_sequence<sizeof...(Dims)>{}, rhs);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <std::size_t I>
|
||||||
|
constexpr auto get() const {
|
||||||
|
return get_impl<I, Dims...>::value == dynamic_extent ? dimensions[I] : 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 L,
|
||||||
|
std::size_t... LDims,
|
||||||
|
template <std::size_t...> class R,
|
||||||
|
std::size_t... RDims,
|
||||||
|
std::size_t... Is>
|
||||||
|
static constexpr bool compare_impl(std::index_sequence<Is...>, const L<LDims...>& lhs, const R<RDims...>& rhs) {
|
||||||
|
return ((lhs.template 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;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
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 <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 {};
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
|
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 math
|
||||||
|
} // namespace cenet
|
||||||
|
|
||||||
|
#endif // CENET_MATH_TENSOR_SHAPE_HPP
|
||||||
@@ -0,0 +1,136 @@
|
|||||||
|
#ifndef CENET_MATH_TENSOR_VIEW_HPP
|
||||||
|
#define CENET_MATH_TENSOR_VIEW_HPP
|
||||||
|
|
||||||
|
#include "expr.hpp"
|
||||||
|
#include "tensor_fwd.hpp"
|
||||||
|
#include "tensor_shape.hpp"
|
||||||
|
#include "tensor_traits.hpp"
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstring>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
namespace cenet {
|
||||||
|
namespace math {
|
||||||
|
|
||||||
|
template <typename T, std::size_t... Dims>
|
||||||
|
class tensor_view {
|
||||||
|
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 + calculate_offset(std::make_tuple(offs...));
|
||||||
|
if constexpr (sizeof...(Offs) == sizeof...(Dims)) return *ptr;
|
||||||
|
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 + calculate_offset(std::make_tuple(offs...));
|
||||||
|
if constexpr (sizeof...(Offs) == sizeof...(Dims)) return *ptr;
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
template <typename... Offs>
|
||||||
|
std::size_t calculate_offset(const std::tuple<Offs...>& offs) const {
|
||||||
|
return calculate_offset_impl<sizeof...(Offs) - 1, sizeof...(Dims) - sizeof...(Offs)>(offs);
|
||||||
|
}
|
||||||
|
|
||||||
|
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<I>(offs) * dimension<Start + I>();
|
||||||
|
else
|
||||||
|
return (calculate_offset_impl<I - 1, Start>(offs) + std::get<I>(offs)) * dimension<Start + I>();
|
||||||
|
}
|
||||||
|
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::memcpy(m_view + offset, source, std::min(length, size() - offset));
|
||||||
|
}
|
||||||
|
|
||||||
|
void fill(const tensor_view& view) { std::memcpy(m_view, view.m_view, size()); }
|
||||||
|
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 math
|
||||||
|
} // namespace cenet
|
||||||
|
|
||||||
|
#endif // CENET_MATH_TENSOR_VIEW_HPP
|
||||||
Binary file not shown.
Reference in New Issue
Block a user