Deepity
Ultra-fast, SIMD-accelerated Predictive Coding
Loading...
Searching...
No Matches
Tensor.h
Go to the documentation of this file.
1#pragma once
2#include <cstddef>
3#include <vector>
6
23
24namespace Deep
25{
28 class Tensor
29 {
30 public:
39 Tensor(IComputeBackend *backend, DeviceType device, size_t numFloats)
40 : backend(backend), device(device), size(numFloats)
41 {
42 data = backend->Allocate(numFloats);
43 backend->Zero(data, numFloats);
44 }
45
51 Tensor(IComputeBackend *backend, DeviceType device, const float *hostData, size_t numFloats)
52 : backend(backend), device(device), size(numFloats)
53 {
54 data = backend->Allocate(numFloats);
55 backend->CopyFromHost(data, hostData, numFloats);
56 }
57
61 Tensor(IComputeBackend *backend, DeviceType device, const std::vector<float> &hostData)
62 : Tensor(backend, device, hostData.data(), hostData.size())
63 {
64 }
65
69 {
70 if (data != nullptr)
71 {
72 backend->Free(data);
73 }
74 }
75
76 // Move-only -- see file-level note.
77 Tensor(const Tensor &) = delete;
78 Tensor &operator=(const Tensor &) = delete;
79
82 Tensor(Tensor &&other) noexcept
83 : backend(other.backend), device(other.device), data(other.data), size(other.size)
84 {
85 other.data = nullptr;
86 other.size = 0;
87 }
88
91 Tensor &operator=(Tensor &&other) noexcept
92 {
93 if (this != &other)
94 {
95 if (data != nullptr)
96 {
97 backend->Free(data);
98 }
99 backend = other.backend;
100 device = other.device;
101 data = other.data;
102 size = other.size;
103 other.data = nullptr;
104 other.size = 0;
105 }
106 return *this;
107 }
108
114 float *Data() noexcept { return data; }
116 const float *Data() const noexcept { return data; }
117
119 size_t Size() const noexcept { return size; }
121 DeviceType Device() const noexcept { return device; }
123 IComputeBackend *Backend() const noexcept { return backend; }
124
129 void CopyToHost(float *hostDst) const noexcept
130 {
131 backend->CopyToHost(hostDst, data, size);
132 }
133
135 void CopyToHost(std::vector<float> &hostDst) const
136 {
137 hostDst.resize(size);
138 CopyToHost(hostDst.data());
139 }
140
141 private:
142 IComputeBackend *backend = nullptr; // non-owning; must outlive this Tensor
143 DeviceType device;
144 float *data = nullptr;
145 size_t size = 0;
146 };
147}
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