-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLayer.hpp
More file actions
97 lines (87 loc) · 2.3 KB
/
Copy pathLayer.hpp
File metadata and controls
97 lines (87 loc) · 2.3 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#pragma once
#include <algorithm>
#include <execution>
#include <functional>
#include <initializer_list>
#include <iostream>
#include <numeric>
#include <stdexcept>
#include <string>
#include <vector>
#include "graph/runtime_options.hpp"
#include "layers/Shape.hpp"
#include "layers/Tensor.hpp"
#include "parallel/parallel.hpp"
namespace it_lab_ai {
enum LayerType : uint8_t {
kInput,
kPooling,
kNormalization,
kDropout,
kElementWise,
kConvolution,
kFullyConnected,
kFlatten,
kOutput,
kConcat,
kSplit,
kBinaryOp,
kReduce,
kTranspose,
kReshape,
kSoftmax,
kMatmul,
kBatchNormalization,
kConvRelu
};
enum ImplType : uint8_t { kDefault, kTBB, kSTL };
using ParBackend = parallel::Backend;
class Layer;
struct PostOperations {
std::vector<std::shared_ptr<Layer>> layers;
unsigned int count = 0;
};
class Layer {
public:
Layer() = default;
explicit Layer(LayerType type) : type_(type) {}
virtual ~Layer() = default;
PostOperations postops;
[[nodiscard]] int getID() const { return id_; }
void setID(int id) { id_ = id; }
[[nodiscard]] LayerType getName() const { return type_; }
virtual void run(const std::vector<Tensor>& input,
std::vector<Tensor>& output) = 0;
virtual void run(const std::vector<Tensor>& input,
std::vector<Tensor>& output,
[[maybe_unused]] const RuntimeOptions& options) {
run(input, output);
}
#ifdef ENABLE_STATISTIC_WEIGHTS
virtual Tensor get_weights() = 0;
#endif
protected:
int id_ = 0;
LayerType type_;
};
template <typename ValueType>
class LayerImpl {
public:
LayerImpl() = default;
LayerImpl(const Shape& inputShape, const Shape& outputShape)
: inputShape_(inputShape), outputShape_(outputShape) {}
LayerImpl(const LayerImpl& c) = default;
LayerImpl& operator=(const LayerImpl& c) = default;
[[nodiscard]] virtual std::vector<ValueType> run(
const std::vector<ValueType>& input) const = 0;
[[nodiscard]] Shape get_input_shape() const { return inputShape_; }
[[nodiscard]] Shape get_output_shape() const { return outputShape_; }
// weights width x height
[[nodiscard]] std::pair<Shape, Shape> get_dims() const {
return std::pair<Shape, Shape>(outputShape_, inputShape_);
}
protected:
Shape inputShape_;
Shape outputShape_;
};
} // namespace it_lab_ai