MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
TensorContainer.cpp
Go to the documentation of this file.
1#include "TensorContainer.hpp"
2
5
6namespace MayaFlux::Kakshya {
7
8namespace {
9
10 std::vector<int64_t> column_major_strides(const std::vector<uint64_t>& shape)
11 {
12 std::vector<int64_t> result(shape.size(), 1);
13 int64_t running = 1;
14 for (size_t axis = 0; axis < shape.size(); ++axis) {
15 result[axis] = running;
16 running *= static_cast<int64_t>(std::max<uint64_t>(shape[axis], 1));
17 }
18 return result;
19 }
20
21}
22
24 : m_data(std::move(data))
25{
26 if (!m_data.is_valid() || !m_data.spec().is_valid()) {
27 error<std::invalid_argument>(
28 Journal::Component::Kakshya, Journal::Context::Configuration,
29 std::source_location::current(),
30 "TensorContainer: the tensor holds no valid spec");
31 }
32}
33
35{
36 auto allocated = TensorData::allocate(std::move(spec));
37 if (!allocated) {
38 error<std::invalid_argument>(
40 std::source_location::current(),
41 "TensorContainer: the spec is inconsistent or not host memory");
42 }
43 m_data = std::move(*allocated);
44}
45
47
48std::optional<TensorAccess> TensorContainer::view(const Region& region) const
49{
50 const auto window = m_data.spec().window(region);
51 if (!window)
52 return std::nullopt;
53
54 return as_tensor_access(m_data.storage(), *window, false);
55}
56
57std::vector<DataDimension> TensorContainer::get_dimensions() const
58{
59 return m_data.spec().dimensions();
60}
61
63{
64 return m_data.spec().element_count().value_or(0);
65}
66
68{
69 const auto& spec = m_data.spec();
70 if (spec.rank() > 1 && !spec.strides.empty() && spec.strides == column_major_strides(spec.shape))
73}
74
76{
77 const auto& shape = m_data.spec().shape;
78 if (shape.empty())
79 return 0;
80 return std::accumulate(shape.begin() + 1, shape.end(), uint64_t { 1 }, std::multiplies<> {});
81}
82
84{
85 const auto& shape = m_data.spec().shape;
86 return shape.empty() ? 0 : shape.front();
87}
88
89std::vector<TensorData> TensorContainer::get_region_data(const Region& region) const
90{
91 const auto selected = view(region);
92 if (!selected)
93 return {};
94
95 auto copy = TensorData::copy_of(*selected);
96 if (!copy)
97 return {};
98
99 std::vector<TensorData> result;
100 result.push_back(std::move(*copy));
101 return result;
102}
103
104std::vector<TensorData> TensorContainer::get_region_group_data(const RegionGroup& group) const
105{
106 std::vector<TensorData> result;
107 result.reserve(group.regions.size());
108
109 for (const auto& region : group.regions) {
110 auto copied = get_region_data(region);
111 if (!copied.empty())
112 result.push_back(std::move(copied.front()));
113 }
114 return result;
115}
116
117std::vector<TensorData> TensorContainer::get_segments_data(const std::vector<RegionSegment>& segments) const
118{
119 std::vector<TensorData> result;
120 result.reserve(segments.size());
121
122 for (const auto& segment : segments) {
123 if (segment.is_cached) {
124 if (const TensorData* held = segment.cache.get(tensor_cache())) {
125 result.push_back(*held);
126 continue;
127 }
128 }
129
130 const auto& region = segment.source_region;
131 const size_t rank = region.start_coordinates.size();
132 if (rank == 0 || region.end_coordinates.size() != rank
133 || segment.offset_in_region.size() != rank || segment.segment_size.size() != rank)
134 continue;
135
136 Region box;
137 box.start_coordinates.resize(rank);
138 box.end_coordinates.resize(rank);
139 bool valid = true;
140 for (size_t axis = 0; axis < rank && valid; ++axis) {
141 const uint64_t source_start = region.start_coordinates[axis];
142 const uint64_t source_end = region.end_coordinates[axis];
143 const uint64_t offset = segment.offset_in_region[axis];
144 const uint64_t count = segment.segment_size[axis];
145
146 valid = source_start <= source_end && offset <= source_end - source_start && count != 0;
147 if (!valid)
148 break;
149 const uint64_t start = source_start + offset;
150 valid = count - 1 <= source_end - start;
151 box.start_coordinates[axis] = start;
152 box.end_coordinates[axis] = start + count - 1;
153 }
154 if (!valid)
155 continue;
156
157 auto copied = get_region_data(box);
158 if (!copied.empty())
159 result.push_back(std::move(copied.front()));
160 }
161 return result;
162}
163
164void TensorContainer::set_region_data(const Region& region, const std::vector<TensorData>& data)
165{
166 if (data.size() != 1)
167 return;
168
169 const auto source = data.front().access();
170 if (!source)
171 return;
172
173 TensorInsertion(m_data).assign_window(region, *source);
174}
175
177{
178 m_region_groups[group.name] = group;
179}
180
181RegionGroup TensorContainer::get_region_group(const std::string& name) const
182{
183 const auto it = m_region_groups.find(name);
184 return it == m_region_groups.end() ? RegionGroup {} : it->second;
185}
186
187void TensorContainer::remove_region_group(const std::string& name)
188{
189 m_region_groups.erase(name);
190}
191
193{
194 return view(region).has_value();
195}
196
197uint64_t TensorContainer::coordinates_to_linear_index(const std::vector<uint64_t>& coordinates) const
198{
199 const auto& shape = m_data.spec().shape;
200 if (coordinates.size() != shape.size())
201 return 0;
202
203 uint64_t index = 0;
204 for (size_t axis = 0; axis < shape.size(); ++axis) {
205 if (coordinates[axis] >= shape[axis])
206 return 0;
207 index = index * shape[axis] + coordinates[axis];
208 }
209 return index;
210}
211
212std::vector<uint64_t> TensorContainer::linear_index_to_coordinates(uint64_t linear_index) const
213{
214 const auto& shape = m_data.spec().shape;
215 if (linear_index >= get_total_elements())
216 return {};
217
218 std::vector<uint64_t> coordinates(shape.size());
219 for (size_t axis = shape.size(); axis-- > 0;) {
220 coordinates[axis] = linear_index % shape[axis];
221 linear_index /= shape[axis];
222 }
223 return coordinates;
224}
225
227{
228 auto emptied = TensorData::allocate(
229 TensorSpec::contiguous(m_data.spec().dtype, { 0 }, m_data.spec().name));
230 if (emptied)
231 m_data = std::move(*emptied);
232 m_region_groups.clear();
233}
234
236{
237 const auto access = m_data.access();
238 return access ? access->element_data() : nullptr;
239}
240
241}
std::vector< uint64_t > linear_index_to_coordinates(uint64_t linear_index) const override
Coordinate of a row-major logical index.
uint64_t get_num_frames() const override
Extent of the leading axis.
const void * get_raw_data() const override
Borrow element zero, or nullptr when empty.
void remove_region_group(const std::string &name) override
Remove a named region group if present.
void add_region_group(const RegionGroup &group) override
Store or replace a named region group.
std::unordered_map< std::string, RegionGroup > m_region_groups
std::vector< TensorData > get_region_data(const Region &region) const override
Copy the box a Region selects into an owning dense tensor.
void set_region_data(const Region &region, const std::vector< TensorData > &data) override
Overwrite the box a Region selects.
uint64_t get_total_elements() const override
Number of elements in the tensor.
RegionGroup get_region_group(const std::string &name) const override
Return a named region group, or an empty group if absent.
TensorContainer(TensorData data)
Take ownership of a tensor.
std::vector< TensorData > get_region_group_data(const RegionGroup &group) const override
Copy the boxes of a group into separate tensors.
MemoryLayout get_memory_layout() const override
Column-major when the strides are exactly Fortran order, otherwise row-major.
std::vector< TensorData > get_segments_data(const std::vector< RegionSegment > &segments) const override
Copy segments into separate tensors.
std::vector< DataDimension > get_dimensions() const override
Describe the tensor's axes with their sizes and stride magnitudes.
void clear() override
Become an empty tensor of the same dtype and name, and drop all region groups.
bool is_region_loaded(const Region &region) const override
Whether the region selects a valid box.
uint64_t coordinates_to_linear_index(const std::vector< uint64_t > &coordinates) const override
Row-major logical index of a coordinate, independent of strides.
std::optional< TensorAccess > view() const
Borrow the whole tensor.
uint64_t get_frame_size() const override
Elements in one slice along the leading axis.
static std::optional< TensorData > copy_of(const TensorAccess &view)
Dense C-ordered copy of any tensor view.
Definition Tensor.cpp:126
static std::optional< TensorData > allocate(TensorSpec spec)
Zero-filled tensor for a spec.
Definition Tensor.cpp:72
Owning host tensor: a TensorSpec and the bytes it describes.
Definition Tensor.hpp:42
bool assign_window(const Region &region, const TensorAccess &view)
Overwrite the box a Region selects with a view of the box's shape.
Mutable adapter for writing into a TensorData.
@ Configuration
Configuration and parameter updates.
@ Kakshya
Containers[Signalsource, Stream, File], Regions, DataProcessors.
const Facet< TensorData > & tensor_cache()
Key for a cached TensorData held by a RegionSegment.
MemoryLayout
Memory layout for multi-dimensional data.
Definition NDData.hpp:33
@ ROW_MAJOR
C/C++ style (last dimension varies fastest).
Definition NDData.hpp:34
@ COLUMN_MAJOR
Fortran/MATLAB style (first dimension varies fastest).
Definition NDData.hpp:35
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< Region > regions
Collection of regions belonging to this group.
std::string name
Descriptive name of the group.
Organizes related signal regions into a categorized collection.
std::vector< uint64_t > end_coordinates
Ending frame index (inclusive).
Definition Region.hpp:79
std::vector< uint64_t > start_coordinates
Starting frame index (inclusive).
Definition Region.hpp:76
Represents a point or span in N-dimensional space.
Definition Region.hpp:74
static TensorSpec contiguous(TensorDType dtype, std::vector< uint64_t > shape, std::string name={})
C-ordered spec with no offset, axes or quantization.
Complete description of a tensor: type, shape, layout, meaning.