10 std::vector<int64_t> column_major_strides(
const std::vector<uint64_t>& shape)
12 std::vector<int64_t> result(shape.size(), 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));
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");
38 error<std::invalid_argument>(
40 std::source_location::current(),
41 "TensorContainer: the spec is inconsistent or not host memory");
43 m_data = std::move(*allocated);
50 const auto window =
m_data.spec().window(region);
59 return m_data.spec().dimensions();
64 return m_data.spec().element_count().value_or(0);
69 const auto& spec =
m_data.spec();
70 if (spec.rank() > 1 && !spec.strides.empty() && spec.strides == column_major_strides(spec.shape))
77 const auto& shape =
m_data.spec().shape;
80 return std::accumulate(shape.begin() + 1, shape.end(), uint64_t { 1 }, std::multiplies<> {});
85 const auto& shape =
m_data.spec().shape;
86 return shape.empty() ? 0 : shape.front();
91 const auto selected =
view(region);
99 std::vector<TensorData> result;
100 result.push_back(std::move(*copy));
106 std::vector<TensorData> result;
107 result.reserve(group.
regions.size());
109 for (
const auto& region : group.
regions) {
112 result.push_back(std::move(copied.front()));
119 std::vector<TensorData> result;
120 result.reserve(segments.size());
122 for (
const auto& segment : segments) {
123 if (segment.is_cached) {
125 result.push_back(*held);
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)
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];
146 valid = source_start <= source_end && offset <= source_end - source_start && count != 0;
149 const uint64_t start = source_start + offset;
150 valid = count - 1 <= source_end - start;
159 result.push_back(std::move(copied.front()));
166 if (data.size() != 1)
169 const auto source = data.front().access();
194 return view(region).has_value();
199 const auto& shape =
m_data.spec().shape;
200 if (coordinates.size() != shape.size())
204 for (
size_t axis = 0; axis < shape.size(); ++axis) {
205 if (coordinates[axis] >= shape[axis])
207 index = index * shape[axis] + coordinates[axis];
214 const auto& shape =
m_data.spec().shape;
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];
231 m_data = std::move(*emptied);
237 const auto access =
m_data.access();
238 return access ? access->element_data() :
nullptr;
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 ®ion) const override
Copy the box a Region selects into an owning dense tensor.
void set_region_data(const Region ®ion, 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 ®ion) 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.
~TensorContainer() override
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.
static std::optional< TensorData > allocate(TensorSpec spec)
Zero-filled tensor for a spec.
Owning host tensor: a TensorSpec and the bytes it describes.
bool assign_window(const Region ®ion, 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.
@ ROW_MAJOR
C/C++ style (last dimension varies fastest).
@ COLUMN_MAJOR
Fortran/MATLAB style (first dimension varies fastest).
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).
std::vector< uint64_t > start_coordinates
Starting frame index (inclusive).
Represents a point or span in N-dimensional space.
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.