fix(math): fix tensor.hpp
This commit is contained in:
@@ -36,7 +36,7 @@ public:
|
||||
tensor() {
|
||||
if constexpr (traits::dynamic) {
|
||||
m_items = nullptr;
|
||||
} else if constexpr (traits::inplace_storage) {
|
||||
} else if constexpr (traits::inline_storage) {
|
||||
std::memset(m_items, 0, sizeof(m_items));
|
||||
} else {
|
||||
m_items = new value_type[traits::size];
|
||||
@@ -52,24 +52,31 @@ public:
|
||||
|
||||
~tensor() { release(); }
|
||||
|
||||
tensor(tensor&& other) noexcept { *this = std::move(other); }
|
||||
tensor(tensor&& other) noexcept {
|
||||
if constexpr (!traits::inline_storage) m_items = nullptr;
|
||||
*this = std::move(other);
|
||||
}
|
||||
|
||||
tensor& operator=(tensor&& other) noexcept {
|
||||
if (this == &other) return *this;
|
||||
release();
|
||||
m_items = other.m_items;
|
||||
std::memcpy(m_dimensions, other.m_dimensions, sizeof(m_dimensions));
|
||||
std::memcpy(m_strides, other.m_strides, sizeof(m_strides));
|
||||
|
||||
other.m_items = nullptr;
|
||||
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));
|
||||
if constexpr (!traits::inline_storage) other.m_items = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
tensor(const tensor& other) { *this = other; }
|
||||
tensor(const tensor& other) {
|
||||
if constexpr (!traits::inline_storage) m_items = nullptr;
|
||||
*this = other;
|
||||
}
|
||||
|
||||
tensor& operator=(const tensor& other) {
|
||||
if (this == &other) return *this;
|
||||
release();
|
||||
std::memcpy(m_dimensions, other.m_dimensions, sizeof(m_dimensions));
|
||||
std::memcpy(m_strides, other.m_strides, sizeof(m_strides));
|
||||
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_copy(std::make_index_sequence<sizeof...(Dims)>{});
|
||||
|
||||
@@ -80,7 +87,7 @@ public:
|
||||
typename = std::enable_if_t<(
|
||||
traits::dynamic && sizeof...(RtDims) == sizeof...(Dims) && (std::is_integral_v<RtDims> && ...))>>
|
||||
void set_dimensions(RtDims... dimensions) {
|
||||
if constexpr (!valid_dims(std::make_index_sequence<sizeof...(Dims)>{}, dimensions...))
|
||||
if (!valid_dims(std::make_index_sequence<sizeof...(Dims)>{}, dimensions...))
|
||||
throw std::runtime_error("invalid dynamic tensor dimensions");
|
||||
store_dims(std::make_index_sequence<sizeof...(Dims)>{}, dimensions...);
|
||||
|
||||
@@ -100,30 +107,39 @@ private:
|
||||
}
|
||||
private:
|
||||
template <typename... RtDims, std::size_t... Is>
|
||||
constexpr void store_dims(std::index_sequence<Is...>, RtDims... dimensions) const {
|
||||
return ((m_dimensions[Is] = dimensions) && ...);
|
||||
constexpr void store_dims(std::index_sequence<Is...>, RtDims... dimensions) {
|
||||
((m_shape.dimensions[Is] = static_cast<std::size_t>(dimensions)), ...);
|
||||
}
|
||||
private:
|
||||
template <std::size_t... Is>
|
||||
void dyn_alloc(std::index_sequence<Is...>) {
|
||||
m_items = new value_type[(m_dimensions[Is] * ...)];
|
||||
m_items = new value_type[(m_shape.dimensions[Is] * ...)];
|
||||
}
|
||||
|
||||
template <std::size_t... Is>
|
||||
void dyn_copy(const value_type* src, std::index_sequence<Is...>) {
|
||||
std::memcpy(m_items, src, sizeof(*m_items) * (m_dimensions[Is] * ... * 1));
|
||||
std::memcpy(m_items, src, sizeof(*m_items) * (m_shape.dimensions[Is] * ... * 1));
|
||||
}
|
||||
|
||||
void release() {
|
||||
if constexpr (!traits::inline_storage) {
|
||||
delete[] m_items;
|
||||
m_items = nullptr;
|
||||
}
|
||||
}
|
||||
private:
|
||||
traits::storage_type m_items;
|
||||
struct empty_shape {};
|
||||
|
||||
std::enable_if_t<traits::dynamic, std::size_t[sizeof...(Dims)]> m_dimensions;
|
||||
std::enable_if_t<traits::dynamic, std::size_t[sizeof...(Dims)]> m_strides;
|
||||
template <std::size_t Rank>
|
||||
struct dynamic_shape {
|
||||
std::size_t dimensions[Rank];
|
||||
std::size_t strides[Rank];
|
||||
};
|
||||
|
||||
using shape_type = std::conditional_t<traits::dynamic, dynamic_shape<traits::rank>, empty_shape>;
|
||||
private:
|
||||
traits::storage_type m_items;
|
||||
shape_type m_shape;
|
||||
};
|
||||
|
||||
template <typename T, std::size_t M, std::size_t N>
|
||||
|
||||
Reference in New Issue
Block a user