Compare commits

...
4 Commits
6 changed files with 119 additions and 29 deletions
+38
View File
@@ -160,6 +160,44 @@ private:
};
};
class relu final : public layer_info {
template <typename Input, typename... Layers>
friend class ::cenet::network;
private:
template <class NN, std::size_t I>
struct inner final {
using input_type = typename input_type_s<NN, I>::type;
using output_type = input_type;
typename input_type::template with_storage<math::inline_storage_tag> retained;
struct gradient {
void reset() {}
};
void dump(std::ostream& os) const {}
void load(std::istream& is) {}
template <typename Dist>
void fill_random(const Dist& dist) {}
template <typename Fwd>
auto feed(Fwd&& fwd) {
retained = std::forward<Fwd>(fwd);
return retained.apply([](auto in) { return in > 0.0 ? in : 0.0; });
}
template <typename Error>
auto backward(gradient& grad, Error&& prevErr) const {
return std::forward<Error>(prevErr).apply(retained,
[](auto in, auto check) { return check > 0.0 ? in : 0.0; });
}
void apply_grad(gradient&, double, double) {}
};
};
} // namespace layers
} // namespace cenet
+6
View File
@@ -44,6 +44,12 @@ struct ops_provider {
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() };
}
private:
decltype(auto) self() & { return reinterpret_cast<CRTP&>(*this); }
decltype(auto) self() && { return reinterpret_cast<CRTP&&>(*this); }
+26 -12
View File
@@ -146,18 +146,7 @@ struct apply {
template <typename L, typename R>
static constexpr auto shape(const L& lhs, const R& rhs) {
if (!compatible(lhs, rhs)) throw std::runtime_error("uncompatible operands");
using lhs_shape = remove_cvref_t<decltype(lhs.shape())>;
using rhs_shape = remove_cvref_t<decltype(rhs.shape())>;
constexpr std::size_t lhs_rows = lhs_shape::template sget<0>();
constexpr std::size_t rhs_cols = rhs_shape::template sget<1>();
if constexpr (lhs_rows == dynamic_extent || rhs_cols == dynamic_extent) {
return dynamic_shape<lhs_rows, rhs_cols>{};
} else {
return static_shape<lhs_rows, rhs_cols>{};
}
return lhs.shape();
}
template <typename E>
@@ -201,6 +190,31 @@ struct transpose {
}
};
// TODO: introduce dynamic reshape
template <typename S, typename = std::enable_if_t<::cenet::math::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 (!::cenet::math::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 math
} // namespace cenet
+3 -2
View File
@@ -34,9 +34,10 @@ public:
m_items = nullptr;
m_shape = {};
} else if constexpr (traits::inline_storage) {
std::memset(m_items, 0, sizeof(m_items));
view().fill();
} else {
m_items = new value_type[traits::size];
view().fill();
}
}
@@ -261,7 +262,7 @@ private:
private:
template <std::size_t... Is>
void dyn_alloc(std::index_sequence<Is...>) {
if constexpr (traits::dynamic) {
if constexpr (!traits::inline_storage) {
m_items = new value_type[size()];
}
}
+44
View File
@@ -63,6 +63,33 @@ private:
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 {
static_assert(sizeof...(Offs) > 0);
static_assert(sizeof...(Offs) <= sizeof...(Dims));
return calculate_offset_impl<Start, sizeof...(Dims) - sizeof...(Offs)>(offs);
}
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... Dims>
@@ -141,6 +168,23 @@ private:
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 {
static_assert(sizeof...(Offs) > 0);
static_assert(sizeof...(Offs) <= sizeof...(Dims));
return calculate_offset_impl<Start, sizeof...(Dims) - sizeof...(Offs)>(offs);
}
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);
}
}
};
namespace detail {
+2 -15
View File
@@ -33,7 +33,7 @@ public:
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...));
auto* ptr = m_view + m_shape.calculate_offset(std::make_tuple(offs...));
if constexpr (sizeof...(Offs) == sizeof...(Dims))
return *ptr;
else
@@ -44,7 +44,7 @@ public:
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...));
const auto* ptr = m_view + m_shape.calculate_offset(std::make_tuple(offs...));
if constexpr (sizeof...(Offs) == sizeof...(Dims))
return *ptr;
else
@@ -60,19 +60,6 @@ public:
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)>{}); }