MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
TensorAccess.cpp
Go to the documentation of this file.
1#include "TensorAccess.hpp"
2
4
5namespace MayaFlux::Kakshya {
6
7std::optional<TensorAccess> as_tensor_access(
8 std::span<const std::byte> storage, const TensorSpec& spec, bool read_only)
9{
10 if (const auto reason = spec.diagnose(); !reason.empty()) {
12 "as_tensor_access: {}", reason);
13 return std::nullopt;
14 }
15 if (!spec.device.is_host_accessible()) {
17 "as_tensor_access: tensor '{}' lives on a device the host cannot read", spec.name);
18 return std::nullopt;
19 }
20
21 const auto range = spec.byte_range();
22 if (!range || range->second > storage.size()) {
24 "as_tensor_access: tensor '{}' needs {} bytes but {} are available",
25 spec.name, range ? range->second : 0, storage.size());
26 return std::nullopt;
27 }
28
30}
31
32uint64_t TensorAccess::element_count() const noexcept
33{
34 return m_spec.element_count().value_or(0);
35}
36
37const std::byte* TensorAccess::element_data() const noexcept
38{
39 if (element_count() == 0 || m_storage.empty())
40 return nullptr;
41 return m_storage.data() + m_spec.byte_offset;
42}
43
44std::span<const std::byte> TensorAccess::bytes() const noexcept
45{
46 const auto* first = element_data();
47 if (!first || !m_spec.is_contiguous())
48 return {};
49 const auto size = m_spec.dtype.storage_bytes(element_count());
50 return size ? std::span<const std::byte>(first, *size) : std::span<const std::byte> {};
51}
52
54{
55 const auto raw = bytes();
56 return { .data = raw.data(), .bytes = raw.size(), .format = m_spec.dtype.gpu_format() };
57}
58
59}
#define MF_ERROR(comp, ctx,...)
bool read_only() const noexcept
Whether writers must treat the memory as immutable.
TensorGpuView gpu_view() const noexcept
Upload description for a dense tensor.
TensorAccess(std::span< const std::byte > storage, TensorSpec spec, bool read_only)
std::span< const std::byte > bytes() const noexcept
Bytes of the elements when they are dense in C order.
uint64_t element_count() const noexcept
Number of elements.
std::span< const std::byte > m_storage
const TensorSpec & spec() const noexcept
Layout of the borrowed memory.
std::span< const std::byte > storage() const noexcept
The whole borrowed region.
const std::byte * element_data() const noexcept
Pointer to element zero, or nullptr for an empty tensor.
@ Runtime
General runtime operations (default fallback).
@ Kakshya
Containers[Signalsource, Stream, File], Regions, DataProcessors.
std::optional< TensorAccess > as_tensor_access(std::span< const std::byte > storage, const TensorSpec &spec, bool read_only)
Validate and borrow tensor memory.
Pointer, byte count and element format for uploading a tensor.
Complete description of a tensor: type, shape, layout, meaning.