12 ::operator
delete(pointer, std::align_val_t { 256 });
76 std::span<const T> values,
std::vector<uint64_t> shape = {}, std::string name = {})
79 shape.push_back(values.size());
81 TensorSpec::contiguous(TensorDType::of<T>(), std::move(shape), std::move(name)),
82 std::as_bytes(values));
93 [[nodiscard]]
static std::optional<TensorData> from_variant(
94 const DataVariant& variant, std::vector<uint64_t> shape = {}, std::string name = {});
102 [[nodiscard]]
static std::optional<TensorData> copy_of(
const TensorAccess& view);
116 [[nodiscard]] std::optional<TensorAccess> access()
const;
123 [[nodiscard]] std::optional<DataVariant> to_variant()
const;
131 template <DataVariantElement T>
132 [[nodiscard]] std::optional<std::vector<T>>
to_vector()
const
134 const auto view =
access();
137 const auto raw = view->bytes();
139 if (!
m_spec.dtype.is<T>())
142 if (!
m_spec.dtype.is<
float>() || raw.size() %
sizeof(T) != 0)
146 return view->element_count() == 0 ? std::optional<std::vector<T>>(std::vector<T> {}) : std::nullopt;
147 std::vector<T> result(raw.size() /
sizeof(T));
148 std::memcpy(result.data(), raw.data(), result.size() *
sizeof(T));
172 [[nodiscard]]
const TensorData*
find(std::string_view name)
const;
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.
detail::AlignedBytes m_data
std::optional< std::vector< T > > to_vector() const
Copy into a vector of T.
bool is_valid() const noexcept
Whether the tensor holds a consistent spec and its storage.
TensorData & operator=(const TensorData &other)
TensorData(TensorData &&) noexcept=default
const TensorSpec & spec() const noexcept
Layout of the stored bytes.
std::span< const std::byte > storage() const noexcept
The whole stored block.
size_t byte_size() const noexcept
Storage size in bytes.
static std::optional< TensorData > from_bytes(TensorSpec spec, std::span< const std::byte > bytes)
Tensor holding a copy of bytes.
friend class TensorInsertion
std::optional< TensorAccess > access() const
Borrow the tensor, or std::nullopt when it holds nothing valid.
std::span< std::byte > storage() noexcept
The whole stored block, writable.
static std::optional< TensorData > allocate(TensorSpec spec)
Zero-filled tensor for a spec.
Owning host tensor: a TensorSpec and the bytes it describes.
A C++ scalar or std::complex that maps to a TensorDType.
std::unique_ptr< std::byte, AlignedDelete > AlignedBytes
std::vector< TensorData > tensors
const TensorData * find(std::string_view name) const
Tensor with this exact name, or nullptr.
std::map< std::string, std::string > metadata
bool is_consistent() const
Whether every tensor is valid and named, with no duplicate names.
Named tensors that belong together, such as a model's weights or I/O.
Complete description of a tensor: type, shape, layout, meaning.
void operator()(std::byte *pointer) const noexcept