NForge
Tensor library
Loading...
Searching...
No Matches
tensor_view.h
1#ifndef TENSOR_VIEW_H
2#define TENSOR_VIEW_H
3
4#include "nforge/core/tensor.h"
5#include "nforge/core/tensor_layout.h"
6#include "nforge/core/tensor_shape.h"
7
13public:
15 View(Tensor& parent);
16
18 View(Tensor& parent, const std::vector<size_t>& position);
19
21 View(Tensor& parent, const std::vector<size_t>& position, const TensorLayout& layout);
22
24 View(const Tensor& parent);
25
28 static Tensor::View broadcast(Tensor& source, const Tensor::Shape& shape);
29
32 static Tensor::View subsample(const View& src, const std::vector<size_t>& factors);
33
35 void print() const;
36
38 inline Tensor& getParent() const { return m_parent; }
39
41 inline std::vector<size_t> getPosition() const { return m_position; }
42
44 inline size_t getOffset() const { return m_layout.offset; }
45
47 inline Tensor::Shape getShape() const { return Tensor::Shape(m_layout); }
48
50 std::vector<size_t> getStride() const;
51
53 const TensorLayout& getLayout() const { return m_layout; }
54
56 std::string getBackendString() const { return m_parent.getBackendString(); }
57
59 inline Backend getBackend() const { return m_parent.getBackend(); }
60
62 Tensor copy() const;
63
65 std::vector<float> toVector() const;
66
68 Tensor operator+(const Tensor::View& rhs) const;
69
71 Tensor operator-(const Tensor::View& rhs) const;
72
74 Tensor operator*(const Tensor::View& rhs) const;
75
77 Tensor operator/(const Tensor::View& rhs) const;
78
80 void operator+=(const Tensor::View& rhs);
81
83 void operator-=(const Tensor::View& rhs);
84
86 void operator*=(const Tensor::View& rhs);
87
89 void operator/=(const Tensor::View& rhs);
90
92 Tensor mean(size_t dim = 0) const;
93
95 Tensor sum(size_t dim = 0) const;
96
98 Tensor min(size_t dim = 0) const;
99
101 Tensor max(size_t dim = 0) const;
102
104 Tensor prod(size_t dim = 0) const;
105
107 Tensor norm() const;
108
110 Tensor::View operator=(const Tensor& rhs);
111
114
117 Tensor::View operator=(float scalar);
118
120 Tensor::View operator[](size_t idx) const;
121
128 Tensor matmul(const Tensor::View& rhs) const;
129
131 Tensor::View subsample(std::vector<size_t> strides) const;
132
135 bool operator==(const Tensor& rhs) const;
136
139 bool operator==(const Tensor::View& rhs) const;
140
143 bool operator!=(const Tensor& rhs) const;
144
147 bool operator!=(const Tensor::View& rhs) const;
148
150 Tensor operator<(const Tensor::View& rhs) const;
151
153 Tensor operator<=(const Tensor::View& rhs) const;
154
156 Tensor operator>(const Tensor::View& rhs) const;
157
159 Tensor operator>=(const Tensor::View& rhs) const;
160
163 Tensor isClose(const Tensor::View& rhs, float tolerance = 1e-5f) const;
164
165private:
166 // Differentiates the broadcast constructor from public constructors.
167 struct BroadcastTag {};
168
169 // Constructs a view with explicit stride and shape. Used by broadcast().
170 View(Tensor& parent, const std::vector<size_t>& stride, const Tensor::Shape& shape,
171 BroadcastTag);
172
173 Tensor& m_parent;
174 std::vector<size_t> m_position;
175 TensorLayout m_layout;
176};
177
178#endif // TENSOR_VIEW_H
Definition tensor_shape.h:15
Definition tensor_view.h:12
Tensor::View operator[](size_t idx) const
Indexes into the first dimension of this view.
Definition tensor_view.cpp:211
Tensor & getParent() const
Returns the referenced tensor.
Definition tensor_view.h:38
Tensor sum(size_t dim=0) const
Reduces dimensions [dim, rank) by summation. Result shape is shape[0:dim].
Definition tensor_view.cpp:154
std::vector< float > toVector() const
Copies the viewd elements into a flat vector.
Definition tensor_view.cpp:102
Tensor prod(size_t dim=0) const
Reduces dimensions [dim, rank) by taking the product. Result shape is shape[0:dim].
Definition tensor_view.cpp:169
Tensor operator<(const Tensor::View &rhs) const
Elementwise less than. Returns a tensor of 0.0 / 1.0.
Definition tensor_view.cpp:290
Tensor copy() const
Deep copies the viewed region into a new tensor.
Definition tensor_view.cpp:90
Tensor operator>=(const Tensor::View &rhs) const
Elementwise greater or equal. Returns a tensor of 0.0 / 1.0.
Definition tensor_view.cpp:305
bool operator!=(const Tensor &rhs) const
Definition tensor_view.cpp:285
Tensor mean(size_t dim=0) const
Reduces dimensions [dim, rank) by averaging. Result shape is shape[0:dim].
Definition tensor_view.cpp:149
Tensor isClose(const Tensor::View &rhs, float tolerance=1e-5f) const
Definition tensor_view.cpp:310
Backend getBackend() const
Returns the backend enum.
Definition tensor_view.h:59
Tensor matmul(const Tensor::View &rhs) const
Definition tensor_view.cpp:221
static Tensor::View subsample(const View &src, const std::vector< size_t > &factors)
Definition tensor_view.cpp:226
void print() const
Prints the view to stdout.
Definition tensor_view.cpp:65
void operator*=(const Tensor::View &rhs)
In-place elementwise multiplication with a tensor or view. Modifies the parent tensor.
Definition tensor_view.cpp:136
Tensor operator<=(const Tensor::View &rhs) const
Elementwise less or equal. Returns a tensor of 0.0 / 1.0.
Definition tensor_view.cpp:295
Tensor norm() const
L2 norm (scalar tensor equal to sqrt(sum(x^2))).
Definition tensor_view.cpp:174
const TensorLayout & getLayout() const
Returns the underlying physical layout.
Definition tensor_view.h:53
Tensor min(size_t dim=0) const
Reduces dimensions [dim, rank) by taking the minimum. Result shape is shape[0:dim].
Definition tensor_view.cpp:159
size_t getOffset() const
Returns the element offset from the parent's data start.
Definition tensor_view.h:44
void operator+=(const Tensor::View &rhs)
In-place elementwise addition with a tensor or view. Modifies the parent tensor.
Definition tensor_view.cpp:124
Tensor::Shape getShape() const
Returns the shape of the viewed region.
Definition tensor_view.h:47
void operator-=(const Tensor::View &rhs)
In-place elementwise subtraction with a tensor or view. Modifies the parent tensor.
Definition tensor_view.cpp:130
std::string getBackendString() const
Returns "CPU" or "CUDA".
Definition tensor_view.h:56
std::vector< size_t > getPosition() const
Returns the origin position within the parent tensor.
Definition tensor_view.h:41
static Tensor::View broadcast(Tensor &source, const Tensor::Shape &shape)
Definition tensor_view.cpp:30
bool operator==(const Tensor &rhs) const
Definition tensor_view.cpp:270
Tensor::View operator=(const Tensor &rhs)
Copies data from a tensor into the referenced position of this view.
Definition tensor_view.cpp:179
Tensor operator>(const Tensor::View &rhs) const
Elementwise greater than. Returns a tensor of 0.0 / 1.0.
Definition tensor_view.cpp:300
std::vector< size_t > getStride() const
Returns the logical stride per dimension, normalized by the parent's base stride.
Definition tensor_view.cpp:75
Tensor max(size_t dim=0) const
Reduces dimensions [dim, rank) by taking the maximum. Result shape is shape[0:dim].
Definition tensor_view.cpp:164
void operator/=(const Tensor::View &rhs)
In-place elementwise division by a tensor or view. Modifies the parent tensor.
Definition tensor_view.cpp:142
Definition tensor.h:18
friend Tensor operator-(float scalar, const Tensor &rhs)
Elementwise subtraction of a tensor from a pure float.
Definition tensor.cpp:176
friend Tensor operator+(float scalar, const Tensor &rhs)
Elementwise addition of a pure float and a tensor.
Definition tensor.cpp:174
friend Tensor operator/(float scalar, const Tensor &rhs)
Elementwise division of a pure float by a tensor.
Definition tensor.cpp:180
Backend getBackend() const
Returns the backend enum.
Definition tensor.h:75
friend Tensor operator*(float scalar, const Tensor &rhs)
Elementwise multiplication of a pure float and a tensor.
Definition tensor.cpp:178
std::string getBackendString() const
Returns "CPU" or "CUDA".
Definition tensor.cpp:89
Definition tensor_layout.h:15
size_t offset
Storage offset before indexing begins.
Definition tensor_layout.h:23