-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlinear.h
More file actions
57 lines (45 loc) · 1.47 KB
/
linear.h
File metadata and controls
57 lines (45 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// linear.h
// Linear layer module
#ifndef TINYTENSOR_NN_LINEAR_H_
#define TINYTENSOR_NN_LINEAR_H_
#include <tt/device.h>
#include <tt/export.h>
#include <tt/nn/module.h>
#include <tt/scalar.h>
#include <tt/tensor.h>
#include <memory>
#include <optional>
#include <ostream>
#include <string>
namespace tinytensor::nn {
// A linear layer
class TINYTENSOR_EXPORT Linear : public Module {
public:
/**
* Construct a linear layer
* @param in_features Number of input features
* @param out_features Number of output features
* @param bias Boolean flag if a bias should be used
* @param dtype The dtype of the weights
* @param device The device the weights should be initialized on
*/
Linear(int in_features, int out_features, bool bias = true, ScalarType dtype = kDefaultFloat, Device device = kCPU);
/**
* Forward pass for Linear layer
* Shape of input should be (*, in_features) (see torch documentation)
* @param input The input tensor
* @return Output tensor of shape (*, out_features)
*/
[[nodiscard]] auto forward(const Tensor &input) const -> Tensor;
void pretty_print(std::ostream &os) const override;
[[nodiscard]] auto name() const -> std::string override {
return "Linear";
}
std::shared_ptr<Tensor> weight;
std::optional<std::shared_ptr<Tensor>> bias;
private:
int in_features_;
int out_features_;
};
} // namespace tinytensor::nn
#endif // TINYTENSOR_NN_LINEAR_H_