MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Tensor.cpp
Go to the documentation of this file.
1#include "Tensor.hpp"
2
4
5namespace MayaFlux::Kakshya {
6
7namespace {
8
9 detail::AlignedBytes allocate_block(size_t size)
10 {
11 if (size == 0)
12 return nullptr;
13 auto* block = static_cast<std::byte*>(::operator new(size, std::align_val_t { 256 }));
14 std::memset(block, 0, size);
15 return detail::AlignedBytes(block);
16 }
17
18 void gather(const std::byte* source, std::byte*& destination, size_t element,
19 const std::vector<uint64_t>& shape, const std::vector<int64_t>& strides, size_t axis)
20 {
21 if (axis == shape.size()) {
22 std::memcpy(destination, source, element);
23 destination += element;
24 return;
25 }
26 for (uint64_t i = 0; i < shape[axis]; ++i) {
27 const auto step = strides[axis] * static_cast<int64_t>(element) * static_cast<int64_t>(i);
28 gather(source + step, destination, element, shape, strides, axis + 1);
29 }
30 }
31
32 template <typename T>
33 std::optional<TensorData> from_vector_shaped(
34 const std::vector<T>& values, size_t components, bool matrix,
35 std::vector<uint64_t> shape, std::string name)
36 {
37 const std::span<const std::byte> bytes = std::as_bytes(std::span<const T>(values));
38 const size_t count = bytes.size() / sizeof(float) / std::max<size_t>(components, 1);
39 if (shape.empty()) {
40 shape.push_back(count);
41 if (matrix) {
42 shape.push_back(4);
43 shape.push_back(4);
44 } else {
45 shape.push_back(components);
46 }
47 }
49 TensorSpec::contiguous(TensorDType::of<float>(), std::move(shape), std::move(name)), bytes);
50 }
51
52}
53
55 : m_spec(other.m_spec)
56 , m_data(allocate_block(other.m_size))
57 , m_size(other.m_size)
58{
59 if (m_size > 0)
60 std::memcpy(m_data.get(), other.m_data.get(), m_size);
61}
62
64{
65 if (this != &other) {
66 TensorData copy(other);
67 *this = std::move(copy);
68 }
69 return *this;
70}
71
72std::optional<TensorData> TensorData::allocate(TensorSpec spec)
73{
74 if (const auto reason = spec.diagnose(); !reason.empty()) {
76 "TensorData::allocate: {}", reason);
77 return std::nullopt;
78 }
79 if (!spec.device.is_host_accessible()) {
81 "TensorData::allocate: tensor '{}' is not host memory", spec.name);
82 return std::nullopt;
83 }
84
85 const auto range = spec.byte_range();
86 TensorData result;
87 result.m_size = static_cast<size_t>(range->second);
88 result.m_data = allocate_block(result.m_size);
89 result.m_spec = std::move(spec);
90 return result;
91}
92
93std::optional<TensorData> TensorData::from_bytes(TensorSpec spec, std::span<const std::byte> bytes)
94{
95 auto result = allocate(std::move(spec));
96 if (!result)
97 return std::nullopt;
98 if (result->m_size != bytes.size()) {
100 "TensorData::from_bytes: tensor '{}' needs {} bytes, received {}",
101 result->m_spec.name, result->m_size, bytes.size());
102 return std::nullopt;
103 }
104 if (!bytes.empty())
105 std::memcpy(result->m_data.get(), bytes.data(), bytes.size());
106 return result;
107}
108
109std::optional<TensorData> TensorData::from_variant(
110 const DataVariant& variant, std::vector<uint64_t> shape, std::string name)
111{
112 return std::visit(
113 [&](const auto& values) -> std::optional<TensorData> {
114 using V = typename std::decay_t<decltype(values)>::value_type;
115 if constexpr (TensorElement<V>) {
116 return from_values<V>(std::span<const V>(values), std::move(shape), std::move(name));
117 } else if constexpr (std::same_as<V, glm::mat4>) {
118 return from_vector_shaped(values, 16, true, std::move(shape), std::move(name));
119 } else {
120 return from_vector_shaped(values, sizeof(V) / sizeof(float), false, std::move(shape), std::move(name));
121 }
122 },
123 variant);
124}
125
126std::optional<TensorData> TensorData::copy_of(const TensorAccess& view)
127{
128 TensorSpec spec = view.spec();
129 spec.strides.clear();
130 spec.byte_offset = 0;
131 spec.device = {};
132
133 auto result = allocate(spec);
134 if (!result)
135 return std::nullopt;
136 if (result->m_size == 0)
137 return result;
138
139 if (view.spec().dtype.is_sub_byte() || view.spec().is_contiguous()) {
140 std::memcpy(result->m_data.get(), view.element_data(), result->m_size);
141 return result;
142 }
143
144 std::byte* cursor = result->m_data.get();
145 gather(view.element_data(), cursor, view.spec().dtype.element_bytes(),
146 view.spec().shape, view.spec().resolved_strides(), 0);
147 return result;
148}
149
150std::optional<TensorAccess> TensorData::access() const
151{
152 if (!is_valid())
153 return std::nullopt;
154 return as_tensor_access(storage(), m_spec, false);
155}
156
157std::optional<DataVariant> TensorData::to_variant() const
158{
159 if (const auto v = to_vector<double>())
160 return DataVariant { *v };
161 if (const auto v = to_vector<float>())
162 return DataVariant { *v };
163 if (const auto v = to_vector<uint8_t>())
164 return DataVariant { *v };
165 if (const auto v = to_vector<uint16_t>())
166 return DataVariant { *v };
167 if (const auto v = to_vector<uint32_t>())
168 return DataVariant { *v };
169 if (const auto v = to_vector<std::complex<float>>())
170 return DataVariant { *v };
171 if (const auto v = to_vector<std::complex<double>>())
172 return DataVariant { *v };
173 return std::nullopt;
174}
175
176const TensorData* TensorSet::find(std::string_view name) const
177{
178 for (const auto& tensor : tensors) {
179 if (tensor.spec().name == name)
180 return &tensor;
181 }
182 return nullptr;
183}
184
185TensorData* TensorSet::find(std::string_view name)
186{
187 for (auto& tensor : tensors) {
188 if (tensor.spec().name == name)
189 return &tensor;
190 }
191 return nullptr;
192}
193
195{
196 for (size_t i = 0; i < tensors.size(); ++i) {
197 const auto& name = tensors[i].spec().name;
198 if (name.empty() || !tensors[i].is_valid() || !tensors[i].spec().is_valid())
199 return false;
200 for (size_t j = i + 1; j < tensors.size(); ++j) {
201 if (tensors[j].spec().name == name)
202 return false;
203 }
204 }
205 return true;
206}
207
208}
#define MF_ERROR(comp, ctx,...)
const TensorSpec & spec() const noexcept
Layout of the borrowed memory.
const std::byte * element_data() const noexcept
Pointer to element zero, or nullptr for an empty tensor.
Validated, non-owning view of tensor memory and its layout.
static std::optional< TensorData > from_values(std::span< const T > values, std::vector< uint64_t > shape={}, std::string name={})
Contiguous tensor holding a copy of typed values.
Definition Tensor.hpp:75
std::optional< DataVariant > to_variant() const
Copy into a flat DataVariant when the dtype has an alternative.
Definition Tensor.cpp:157
detail::AlignedBytes m_data
Definition Tensor.hpp:156
std::optional< std::vector< T > > to_vector() const
Copy into a vector of T.
Definition Tensor.hpp:132
static std::optional< TensorData > copy_of(const TensorAccess &view)
Dense C-ordered copy of any tensor view.
Definition Tensor.cpp:126
bool is_valid() const noexcept
Whether the tensor holds a consistent spec and its storage.
Definition Tensor.hpp:107
TensorData & operator=(const TensorData &other)
Definition Tensor.cpp:63
const TensorSpec & spec() const noexcept
Layout of the stored bytes.
Definition Tensor.hpp:105
std::span< const std::byte > storage() const noexcept
The whole stored block.
Definition Tensor.hpp:111
static std::optional< TensorData > from_bytes(TensorSpec spec, std::span< const std::byte > bytes)
Tensor holding a copy of bytes.
Definition Tensor.cpp:93
std::optional< TensorAccess > access() const
Borrow the tensor, or std::nullopt when it holds nothing valid.
Definition Tensor.cpp:150
static std::optional< TensorData > allocate(TensorSpec spec)
Zero-filled tensor for a spec.
Definition Tensor.cpp:72
static std::optional< TensorData > from_variant(const DataVariant &variant, std::vector< uint64_t > shape={}, std::string name={})
Tensor holding a copy of a DataVariant.
Definition Tensor.cpp:109
Owning host tensor: a TensorSpec and the bytes it describes.
Definition Tensor.hpp:42
A C++ scalar or std::complex that maps to a TensorDType.
@ Runtime
General runtime operations (default fallback).
@ Kakshya
Containers[Signalsource, Stream, File], Regions, DataProcessors.
std::unique_ptr< std::byte, AlignedDelete > AlignedBytes
Definition Tensor.hpp:16
std::variant< std::vector< double >, std::vector< float >, std::vector< uint8_t >, std::vector< uint16_t >, std::vector< uint32_t >, std::vector< std::complex< float > >, std::vector< std::complex< double > >, std::vector< glm::vec2 >, std::vector< glm::vec3 >, std::vector< glm::vec4 >, std::vector< glm::mat4 > > DataVariant
Multi-type data storage for different precision needs.
Definition NDData.hpp:58
std::optional< TensorAccess > as_tensor_access(std::span< const std::byte > storage, const TensorSpec &spec, bool read_only)
Validate and borrow tensor memory.
std::vector< double > count(std::span< const double > data, size_t n_windows, uint32_t hop_size, uint32_t window_size)
Sample count per window (as double for pipeline uniformity).
Definition Analysis.cpp:506
constexpr size_t element_bytes() const noexcept
Bytes of one element, zero for a sub-byte type.
constexpr bool is_sub_byte() const noexcept
Whether one element is not a whole number of bytes.
static constexpr TensorDType of() noexcept
Type for a C++ scalar or complex element.
std::vector< TensorData > tensors
Definition Tensor.hpp:168
const TensorData * find(std::string_view name) const
Tensor with this exact name, or nullptr.
Definition Tensor.cpp:176
bool is_consistent() const
Whether every tensor is valid and named, with no duplicate names.
Definition Tensor.cpp:194
bool is_contiguous() const noexcept
Whether elements are laid out densely in C order.
static TensorSpec contiguous(TensorDType dtype, std::vector< uint64_t > shape, std::string name={})
C-ordered spec with no offset, axes or quantization.
std::vector< uint64_t > shape
std::vector< int64_t > resolved_strides() const
Element strides, with C order filled in when none are stored.
Complete description of a tensor: type, shape, layout, meaning.