MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
TensorInsertion.cpp
Go to the documentation of this file.
1#include "TensorInsertion.hpp"
2
3namespace MayaFlux::Kakshya {
4
5namespace {
6
7 void scatter(std::byte* base, const TensorSpec& target, const std::byte* from)
8 {
9 const std::vector<int64_t> strides = target.resolved_strides();
10 const size_t element = target.dtype.element_bytes();
11 std::vector<uint64_t> index(target.shape.size(), 0);
12 const uint64_t count = target.element_count().value_or(0);
13
14 for (uint64_t n = 0; n < count; ++n) {
15 int64_t offset = 0;
16 for (size_t axis = 0; axis < index.size(); ++axis)
17 offset += strides[axis] * static_cast<int64_t>(index[axis]);
18 std::memcpy(base + offset * static_cast<int64_t>(element), from + n * element, element);
19 for (size_t axis = index.size(); axis-- > 0;) {
20 if (++index[axis] < target.shape[axis])
21 break;
22 index[axis] = 0;
23 }
24 }
25 }
26
27}
28
29bool TensorInsertion::write_bytes(std::span<const std::byte> bytes, uint64_t byte_offset)
30{
31 const auto& spec = m_tensor->m_spec;
32 const auto count = spec.element_count();
33 if (!count || !spec.is_contiguous() || !m_tensor->is_valid())
34 return false;
35
36 const auto total = spec.dtype.storage_bytes(*count);
37 if (!total || byte_offset > *total || bytes.size() > *total - byte_offset)
38 return false;
39
40 if (!bytes.empty())
41 std::memcpy(m_tensor->m_data.get() + spec.byte_offset + byte_offset, bytes.data(), bytes.size());
42 return true;
43}
44
46{
47 const auto& spec = m_tensor->m_spec;
48 if (view.spec().dtype != spec.dtype || view.spec().shape != spec.shape || !m_tensor->is_valid())
49 return false;
50
51 const auto copy = TensorData::copy_of(view);
52 if (!copy)
53 return false;
54
55 const auto source = copy->access();
56 if (!source)
57 return false;
58
59 if (spec.is_contiguous())
60 return write_bytes(source->bytes());
61
62 scatter(m_tensor->m_data.get() + spec.byte_offset, spec, source->element_data());
63 return true;
64}
65
66bool TensorInsertion::assign_window(const Region& region, const TensorAccess& view)
67{
68 const auto& spec = m_tensor->m_spec;
69 if (view.spec().dtype != spec.dtype || !m_tensor->is_valid())
70 return false;
71
72 const auto target = spec.window(region);
73 if (!target || target->shape != view.spec().shape)
74 return false;
75 if (spec.dtype.is_sub_byte())
76 return assign(view);
77
78 const auto range = target->byte_range();
79 if (!range || range->second > m_tensor->m_size)
80 return false;
81
82 const auto copy = TensorData::copy_of(view);
83 if (!copy)
84 return false;
85 const auto source = copy->access();
86 if (!source)
87 return false;
88
89 scatter(m_tensor->m_data.get() + target->byte_offset, *target, source->element_data());
90 return true;
91}
92
93bool TensorInsertion::reshape(std::vector<uint64_t> shape)
94{
95 auto& spec = m_tensor->m_spec;
96 if (!spec.is_contiguous())
97 return false;
98
99 TensorSpec candidate = spec;
100 candidate.shape = std::move(shape);
101 candidate.strides.clear();
102 candidate.axes.clear();
103 candidate.quantization.reset();
104
105 if (candidate.element_count() != spec.element_count() || !candidate.is_valid())
106 return false;
107
108 spec = std::move(candidate);
109 return true;
110}
111
112bool TensorInsertion::resize(std::vector<uint64_t> shape)
113{
114 TensorSpec candidate = m_tensor->m_spec;
115 candidate.shape = std::move(shape);
116 candidate.strides.clear();
117 candidate.axes.clear();
118 candidate.quantization.reset();
119 candidate.byte_offset = 0;
120
121 auto replacement = TensorData::allocate(std::move(candidate));
122 if (!replacement)
123 return false;
124
125 const auto kept = m_tensor->access();
126 if (kept && kept->element_data() && replacement->m_size > 0) {
127 const auto source = TensorData::copy_of(*kept);
128 if (source) {
129 const size_t count = std::min(source->m_size, replacement->m_size);
130 std::memcpy(replacement->m_data.get(), source->m_data.get(), count);
131 }
132 }
133
134 *m_tensor = std::move(*replacement);
135 return true;
136}
137
138bool TensorInsertion::set_axes(std::vector<DataDimension> axes)
139{
140 if (axes.size() != m_tensor->m_spec.shape.size())
141 return false;
142 m_tensor->m_spec.axes = std::move(axes);
143 return true;
144}
145
146bool TensorInsertion::set_quantization(std::optional<TensorQuantization> quantization)
147{
148 TensorSpec candidate = m_tensor->m_spec;
149 candidate.quantization = std::move(quantization);
150 if (!candidate.is_valid())
151 return false;
152 m_tensor->m_spec = std::move(candidate);
153 return true;
154}
155
156}
const TensorSpec & spec() const noexcept
Layout of the borrowed memory.
Validated, non-owning view of tensor memory and its layout.
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
bool reshape(std::vector< uint64_t > shape)
Change the shape without moving data.
bool resize(std::vector< uint64_t > shape)
Replace the shape and reallocate.
bool set_quantization(std::optional< TensorQuantization > quantization)
Attach or clear quantization; it must be consistent with the shape.
bool set_axes(std::vector< DataDimension > axes)
Attach per-axis roles and names; the count must equal the rank.
bool assign_window(const Region &region, const TensorAccess &view)
Overwrite the box a Region selects with a view of the box's shape.
bool assign(const TensorAccess &view)
Overwrite the tensor with a copy of a view of the same dtype and shape.
bool write_bytes(std::span< const std::byte > bytes, uint64_t byte_offset=0)
Write raw bytes into the elements of a dense tensor.
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
Represents a point or span in N-dimensional space.
Definition Region.hpp:74
std::vector< DataDimension > axes
std::optional< uint64_t > element_count() const noexcept
Product of the shape, or std::nullopt on overflow.
std::optional< TensorQuantization > quantization
std::vector< int64_t > strides
bool is_valid() const
Whether diagnose() reports no problem.
std::vector< uint64_t > shape
Complete description of a tensor: type, shape, layout, meaning.