Deepity
Ultra-fast, SIMD-accelerated Predictive Coding
Loading...
Searching...
No Matches
Deep::Tensor Class Reference

A device-aware, RAII-managed, move-only buffer – see the file-level note above for the full ownership/device contract. More...

#include <Tensor.h>

Public Member Functions

 Tensor (IComputeBackend *backend, DeviceType device, size_t numFloats)
 Allocates numFloats on the given backend/device, zero-initialized.
 Tensor (IComputeBackend *backend, DeviceType device, const float *hostData, size_t numFloats)
 Allocates numFloats on the given backend/device, then copies hostData in via backend->CopyFromHost(). This is the "load weights saved on CPU, run on GPU" pattern – works identically for device=DEVICE_CPU too, where CopyFromHost() is just a memcpy.
 Tensor (IComputeBackend *backend, DeviceType device, const std::vector< float > &hostData)
 Same as above, taking a std::vector directly – the expected common case (read a saved model's weights into a std::vector, then hand it straight to this constructor).
 ~Tensor ()
 Frees the underlying device allocation, if any (not called on a moved-from Tensor – see the move constructor).
 Tensor (const Tensor &)=delete
Tensor & operator= (const Tensor &)=delete
 Tensor (Tensor &&other) noexcept
 Move constructor: takes over other's allocation, leaving it empty (Free()-safe, zero size).
Tensor & operator= (Tensor &&other) noexcept
 Move assignment: frees this Tensor's current allocation (if any), then takes over other's, leaving it empty.
float * Data () noexcept
 Returns the raw device buffer. Only safe to dereference directly from code that already knows it's running on the same device this Tensor lives on (e.g. CPUBackend's own methods for a DEVICE_CPU Tensor) – never dereference a DEVICE_GPU Tensor's Data() from host code directly.
const float * Data () const noexcept
 const overload of Data() above.
size_t Size () const noexcept
 Number of floats this Tensor holds.
DeviceType Device () const noexcept
 Which device this Tensor lives on.
IComputeBackend * Backend () const noexcept
 The backend this Tensor was allocated through.
void CopyToHost (float *hostDst) const noexcept
 Copies this Tensor's data out to a host buffer, e.g. for Save()/inspection. A no-op memcpy if this Tensor is already DEVICE_CPU.
void CopyToHost (std::vector< float > &hostDst) const
 Same as above, resizing hostDst to fit if needed.

Detailed Description

A device-aware, RAII-managed, move-only buffer – see the file-level note above for the full ownership/device contract.

Constructor & Destructor Documentation

◆ Tensor() [1/5]

Deep::Tensor::Tensor ( IComputeBackend * backend,
DeviceType device,
size_t numFloats )
inline

Allocates numFloats on the given backend/device, zero-initialized.

Parameters
backendNon-owning pointer to the backend that will perform the allocation; must outlive this Tensor.
deviceWhich device this Tensor lives on – must match what backend itself actually allocates on (a CPUBackend paired with DEVICE_GPU, or vice versa, is a caller error).
numFloatsNumber of floats to allocate.

◆ Tensor() [2/5]

Deep::Tensor::Tensor ( IComputeBackend * backend,
DeviceType device,
const float * hostData,
size_t numFloats )
inline

Allocates numFloats on the given backend/device, then copies hostData in via backend->CopyFromHost(). This is the "load weights saved on CPU, run on GPU" pattern – works identically for device=DEVICE_CPU too, where CopyFromHost() is just a memcpy.

◆ Tensor() [3/5]

Deep::Tensor::Tensor ( IComputeBackend * backend,
DeviceType device,
const std::vector< float > & hostData )
inline

Same as above, taking a std::vector directly – the expected common case (read a saved model's weights into a std::vector, then hand it straight to this constructor).

◆ ~Tensor()

Deep::Tensor::~Tensor ( )
inline

Frees the underlying device allocation, if any (not called on a moved-from Tensor – see the move constructor).

◆ Tensor() [4/5]

Deep::Tensor::Tensor ( const Tensor & )
delete

◆ Tensor() [5/5]

Deep::Tensor::Tensor ( Tensor && other)
inlinenoexcept

Move constructor: takes over other's allocation, leaving it empty (Free()-safe, zero size).

Member Function Documentation

◆ Backend()

IComputeBackend * Deep::Tensor::Backend ( ) const
inlinenoexcept

The backend this Tensor was allocated through.

◆ CopyToHost() [1/2]

void Deep::Tensor::CopyToHost ( float * hostDst) const
inlinenoexcept

Copies this Tensor's data out to a host buffer, e.g. for Save()/inspection. A no-op memcpy if this Tensor is already DEVICE_CPU.

Parameters
hostDstMust point to at least Size() floats.

◆ CopyToHost() [2/2]

void Deep::Tensor::CopyToHost ( std::vector< float > & hostDst) const
inline

Same as above, resizing hostDst to fit if needed.

◆ Data() [1/2]

const float * Deep::Tensor::Data ( ) const
inlinenoexcept

const overload of Data() above.

◆ Data() [2/2]

float * Deep::Tensor::Data ( )
inlinenoexcept

Returns the raw device buffer. Only safe to dereference directly from code that already knows it's running on the same device this Tensor lives on (e.g. CPUBackend's own methods for a DEVICE_CPU Tensor) – never dereference a DEVICE_GPU Tensor's Data() from host code directly.

◆ Device()

DeviceType Deep::Tensor::Device ( ) const
inlinenoexcept

Which device this Tensor lives on.

◆ operator=() [1/2]

Tensor & Deep::Tensor::operator= ( const Tensor & )
delete

◆ operator=() [2/2]

Tensor & Deep::Tensor::operator= ( Tensor && other)
inlinenoexcept

Move assignment: frees this Tensor's current allocation (if any), then takes over other's, leaving it empty.

◆ Size()

size_t Deep::Tensor::Size ( ) const
inlinenoexcept

Number of floats this Tensor holds.


The documentation for this class was generated from the following file: