40 : backend(backend), device(device), size(numFloats)
42 data = backend->Allocate(numFloats);
43 backend->Zero(data, numFloats);
52 : backend(backend), device(device), size(numFloats)
54 data = backend->Allocate(numFloats);
55 backend->CopyFromHost(data, hostData, numFloats);
62 :
Tensor(backend, device, hostData.data(), hostData.size())
83 : backend(other.backend), device(other.device), data(other.data), size(other.size)
99 backend = other.backend;
100 device = other.device;
103 other.data =
nullptr;
114 float *
Data() noexcept {
return data; }
116 const float *
Data() const noexcept {
return data; }
119 size_t Size() const noexcept {
return size; }
131 backend->CopyToHost(hostDst, data, size);
137 hostDst.resize(size);
144 float *data =
nullptr;
Device-agnostic compute backend interface: every numerical primitive a layer/network needs,...
Definition IComputeBackend.h:14
size_t Size() const noexcept
Number of floats this Tensor holds.
Definition Tensor.h:119
Tensor & operator=(const Tensor &)=delete
Tensor(const Tensor &)=delete
~Tensor()
Frees the underlying device allocation, if any (not called on a moved-from Tensor – see the move cons...
Definition Tensor.h:68
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 Te...
Definition Tensor.h:129
float * Data() noexcept
Returns the raw device buffer. Only safe to dereference directly from code that already knows it's ru...
Definition Tensor.h:114
const float * Data() const noexcept
const overload of Data() above.
Definition Tensor.h:116
Tensor(Tensor &&other) noexcept
Move constructor: takes over other's allocation, leaving it empty (Free()-safe, zero size).
Definition Tensor.h:82
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()....
Definition Tensor.h:51
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...
Definition Tensor.h:61
IComputeBackend * Backend() const noexcept
The backend this Tensor was allocated through.
Definition Tensor.h:123
void CopyToHost(std::vector< float > &hostDst) const
Same as above, resizing hostDst to fit if needed.
Definition Tensor.h:135
Tensor & operator=(Tensor &&other) noexcept
Move assignment: frees this Tensor's current allocation (if any), then takes over other's,...
Definition Tensor.h:91
DeviceType Device() const noexcept
Which device this Tensor lives on.
Definition Tensor.h:121
Tensor(IComputeBackend *backend, DeviceType device, size_t numFloats)
Allocates numFloats on the given backend/device, zero-initialized.
Definition Tensor.h:39
This file exists to provide both Tensor.h and IComputeBackend.h with device types without introducing...
Definition Backend.cpp:10
DeviceType
Which physical device a Tensor/IComputeBackend pair runs on.
Definition DeviceType.h:8