nnlib
GPU-accelerated, C/C++ neural network library.
tensor.h
Go to the documentation of this file.
1 
8 #ifndef NNLIB_TENSOR_H
9 #define NNLIB_TENSOR_H
10 
11 #include "allocation.h"
12 #include "cache.h"
13 #include "session.cuh"
14 #include <cstdlib>
15 #include <iostream>
16 #include <memory>
17 #include <utility>
18 #include <vector>
19 
20 class BackwardFunction; // forward declaration to solve a circular dependency
21 
22 
26 class Tensor {
27 
31 public:
32  std::vector<size_t> shape;
33 
40  size_t size;
41 
48 
52  float* data;
53 
58 
59  bool requiresGrad;
60  std::shared_ptr<BackwardFunction> gradFunction;
61  std::shared_ptr<Tensor> grad;
62 
66  Tensor();
67 
73  Tensor(const Tensor& other);
74 
80  explicit Tensor(std::vector<size_t> shape);
81 
82  Tensor(std::vector<size_t> shape, DataLocation location);
83 
91  template<typename... Args>
92  explicit Tensor(Args... args) : Tensor(std::vector<size_t>({static_cast<size_t>(args)...})) {
93  }
94 
103  Tensor& operator=(const Tensor& other);
104 
105  [[nodiscard]] std::shared_ptr<Tensor> copy() const;
106 
114  void move(DataLocation target);
115 
116  void useGrad();
117  void backward();
118 
125  static Tensor construct1d(const std::vector<float>& data);
126 
133  static Tensor construct2d(const std::vector<std::vector<float>>& data);
134 
144  template<typename... Args>
145  float& operator()(Args... args) {
146  std::vector<size_t> index = std::vector<size_t>({static_cast<size_t>(args)...});
147  // Make sure the indexes are within acceptable range and throw SizeMismatchException if not.
148  verifyIndex(index);
149  // Recursively figure out the index in the flattened array (the effective index)
150  size_t effectiveIndex = findEffectiveAddress(index, shape.size() - 1);
151  return data[effectiveIndex];
152  }
153 
157  ~Tensor();
158 
162 private:
163  void computeSize();
164 
175  [[nodiscard]] size_t findEffectiveAddress(const std::vector<size_t>& index, size_t depth) const;
176 
182  void verifyIndex(const std::vector<size_t>& index) const;
183 };
184 
185 typedef std::shared_ptr<Tensor> sTensor;
186 
194 std::ostream& operator<<(std::ostream& stream, const Tensor& tensor);
195 
196 sTensor sum(const sTensor& a);
197 
198 void fill(float value, sTensor& tensor);
199 void fill(const sTensor& value, sTensor& tensor);
200 
201 sTensor add(const sTensor& a, const sTensor& b);
202 
203 sTensor subtract(const sTensor& a, const sTensor& b);
204 
205 sTensor hadamard(const sTensor& a, const sTensor& b);
206 
207 sTensor divide(const sTensor& a, const sTensor& b);
208 
209 sTensor log(const sTensor& a);
210 
211 sTensor multiply(const sTensor& a, float constant);
212 
213 sTensor multiply(const sTensor& a, const sTensor& b);
214 
215 sTensor transpose(const sTensor& a);
216 
217 sTensor relu(const sTensor& a);
218 
219 sTensor sigmoid(const sTensor& a);
220 
221 namespace no_grad {
222  sTensor sum(const sTensor& a);
223 
224  sTensor add(const sTensor& a, const sTensor& b);
225 
226  sTensor subtract(const sTensor& a, const sTensor& b);
227 
228  sTensor hadamard(const sTensor& a, const sTensor& b);
229 
230  sTensor divide(const sTensor& a, const sTensor& b);
231 
232  sTensor log(const sTensor& a);
233 
234  sTensor multiply(const sTensor& a, float constant);
235 
236  sTensor multiply(const sTensor& a, const sTensor& b);
237 
238  sTensor transpose(const sTensor& a);
239 
240  sTensor relu(const sTensor& a);
241 
242  sTensor sigmoid(const sTensor& a);
243 } // namespace no_grad
244 
245 #endif //NNLIB_TENSOR_H
Header file to declare common functions regarding memory allocation on host.
DataLocation
Enumerate to specify where data is located.
Definition: cache.h:24
Definition: functions.h:20
Contains information about the current session.
Definition: session.cuh:19
Class to represent multidimensional arrays.
Definition: tensor.h:26
std::vector< size_t > shape
Store the shape of the tensor.
Definition: tensor.h:32
void verifyIndex(const std::vector< size_t > &index) const
Verify that an index of an element is within the shape of the tensor.
Definition: tensor.cpp:158
Tensor()
Initialize an empty tensor.
Definition: tensor.cpp:23
size_t findEffectiveAddress(const std::vector< size_t > &index, size_t depth) const
Finds the address of an element in the flattened data array given its index in non-flattened tensor.
Definition: tensor.cpp:150
~Tensor()
The destructor.
Definition: tensor.cpp:107
void move(DataLocation target)
Move the tensor to the designated destination.
Definition: tensor.cpp:87
static Tensor construct1d(const std::vector< float > &data)
Static method to easily initialize a 1D tensor with given data.
Definition: tensor.cpp:122
DataLocation location
The location of the tensor.
Definition: tensor.h:47
size_t size
Store the total size of the tensor.
Definition: tensor.h:40
Tensor(Args... args)
Construct a tensor based on the passed shape.
Definition: tensor.h:92
Tensor & operator=(const Tensor &other)
The assignment operator.
Definition: tensor.cpp:64
Session session
Session object containing information about current session.
Definition: tensor.h:57
void computeSize()
Compute the total size of the tensor based on its shape.
Definition: tensor.cpp:115
static Tensor construct2d(const std::vector< std::vector< float >> &data)
Static method to easily initialize a 2D tensor with given data.
Definition: tensor.cpp:131
float * data
The data stored by the tensor.
Definition: tensor.h:52
float & operator()(Args... args)
Method to access an element of the tensor at a specific method.
Definition: tensor.h:145
Header file declaring the Session class.
std::ostream & operator<<(std::ostream &stream, const Tensor &tensor)
Enables the tensor to be printed using std::cout.
Definition: tensor.cpp:253