7 void scatter(std::byte* base,
const TensorSpec& target,
const std::byte* from)
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);
14 for (uint64_t n = 0; n <
count; ++n) {
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])
32 const auto count = spec.element_count();
33 if (!count || !spec.is_contiguous() || !
m_tensor->is_valid())
36 const auto total = spec.dtype.storage_bytes(*count);
37 if (!total || byte_offset > *total || bytes.size() > *total - byte_offset)
41 std::memcpy(
m_tensor->m_data.get() + spec.byte_offset + byte_offset, bytes.data(), bytes.size());
55 const auto source = copy->access();
59 if (spec.is_contiguous())
62 scatter(
m_tensor->m_data.get() + spec.byte_offset, spec, source->element_data());
72 const auto target = spec.window(region);
73 if (!target || target->shape != view.
spec().
shape)
75 if (spec.dtype.is_sub_byte())
78 const auto range = target->byte_range();
79 if (!range || range->second >
m_tensor->m_size)
85 const auto source = copy->access();
89 scatter(
m_tensor->m_data.get() + target->byte_offset, *target, source->element_data());
96 if (!spec.is_contiguous())
100 candidate.
shape = std::move(shape);
102 candidate.
axes.clear();
108 spec = std::move(candidate);
115 candidate.
shape = std::move(shape);
117 candidate.
axes.clear();
125 const auto kept =
m_tensor->access();
126 if (kept && kept->element_data() && replacement->m_size > 0) {
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);
134 *
m_tensor = std::move(*replacement);
140 if (axes.size() !=
m_tensor->m_spec.shape.size())
142 m_tensor->m_spec.axes = std::move(axes);
152 m_tensor->m_spec = std::move(candidate);
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.
static std::optional< TensorData > allocate(TensorSpec spec)
Zero-filled tensor for a spec.
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 ®ion, 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).
Represents a point or span in N-dimensional space.
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.