13 auto* block =
static_cast<std::byte*
>(::operator
new(size, std::align_val_t { 256 }));
14 std::memset(block, 0, size);
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)
21 if (axis == shape.size()) {
22 std::memcpy(destination, source, element);
23 destination += element;
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);
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)
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);
40 shape.push_back(count);
45 shape.push_back(components);
67 *
this = std::move(copy);
74 if (
const auto reason =
spec.diagnose(); !reason.empty()) {
76 "TensorData::allocate: {}", reason);
79 if (!
spec.device.is_host_accessible()) {
81 "TensorData::allocate: tensor '{}' is not host memory",
spec.name);
85 const auto range =
spec.byte_range();
87 result.
m_size =
static_cast<size_t>(range->second);
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());
105 std::memcpy(result->m_data.get(), bytes.data(), bytes.size());
110 const DataVariant& variant, std::vector<uint64_t> shape, std::string name)
113 [&](
const auto& values) -> std::optional<TensorData> {
114 using V =
typename std::decay_t<
decltype(values)>::value_type;
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));
120 return from_vector_shaped(values,
sizeof(V) /
sizeof(
float),
false, std::move(shape), std::move(name));
129 spec.strides.clear();
130 spec.byte_offset = 0;
136 if (result->m_size == 0)
140 std::memcpy(result->m_data.get(), view.
element_data(), result->m_size);
144 std::byte* cursor = result->m_data.get();
169 if (
const auto v =
to_vector<std::complex<float>>())
171 if (
const auto v =
to_vector<std::complex<double>>())
178 for (
const auto& tensor :
tensors) {
179 if (tensor.spec().name == name)
188 if (tensor.spec().name == name)
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())
200 for (
size_t j = i + 1; j <
tensors.size(); ++j) {
201 if (
tensors[j].spec().name == name)
#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.
std::optional< DataVariant > to_variant() const
Copy into a flat DataVariant when the dtype has an alternative.
detail::AlignedBytes m_data
std::optional< std::vector< T > > to_vector() const
Copy into a vector of T.
static std::optional< TensorData > copy_of(const TensorAccess &view)
Dense C-ordered copy of any tensor view.
bool is_valid() const noexcept
Whether the tensor holds a consistent spec and its storage.
TensorData & operator=(const TensorData &other)
const TensorSpec & spec() const noexcept
Layout of the stored bytes.
std::span< const std::byte > storage() const noexcept
The whole stored block.
static std::optional< TensorData > from_bytes(TensorSpec spec, std::span< const std::byte > bytes)
Tensor holding a copy of bytes.
std::optional< TensorAccess > access() const
Borrow the tensor, or std::nullopt when it holds nothing valid.
static std::optional< TensorData > allocate(TensorSpec spec)
Zero-filled tensor for a spec.
static std::optional< TensorData > from_variant(const DataVariant &variant, std::vector< uint64_t > shape={}, std::string name={})
Tensor holding a copy of a DataVariant.
Owning host tensor: a TensorSpec and the bytes it describes.
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
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.
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).
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
const TensorData * find(std::string_view name) const
Tensor with this exact name, or nullptr.
bool is_consistent() const
Whether every tensor is valid and named, with no duplicate names.
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.