Skip to content

Commit 3e8a111

Browse files
authored
Implement function to find subgraph and change LayerType assignment (#209)
1 parent ccaf84d commit 3e8a111

32 files changed

Lines changed: 536 additions & 112 deletions

app/Converters/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tensorflow==2.19.0
1+
tensorflow>=2.19.0
22
onnx>=1.15.0
33
ultralytics>=8.0.0
44
numpy>=1.21.0

app/Graph/build.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,12 @@ void build_graph(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
7373
it_lab_ai::Tensor tmp_bias = it_lab_ai::make_tensor(tensor.get_bias());
7474
auto conv_layer = std::make_shared<it_lab_ai::ConvolutionalLayer>(
7575
1, pads, 1, tmp_values, tmp_bias, impl2);
76-
conv_layer->setName(it_lab_ai::kConvolution);
7776
layers.push_back(conv_layer);
7877
layerpostop.push_back(false);
7978
if (comments) std::cout << "ConvLayer added to layers." << std::endl;
8079
}
8180
if (layer_type.find("relu") != std::string::npos) {
8281
auto ew_layer = std::make_shared<it_lab_ai::EWLayer>("relu");
83-
ew_layer->setName(it_lab_ai::kElementWise);
8482
layers.push_back(ew_layer);
8583
layerpostop.push_back(true);
8684
if (comments)
@@ -101,7 +99,6 @@ void build_graph(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
10199
//
102100
tensor = tmp_tensor;
103101
auto fc_layer = std::make_shared<it_lab_ai::FCLayer>(tensor, tmp_bias);
104-
fc_layer->setName(it_lab_ai::kFullyConnected);
105102
layers.push_back(fc_layer);
106103
layerpostop.push_back(false);
107104
if (comments) std::cout << "DenseLayer added to layers." << std::endl;
@@ -120,7 +117,6 @@ void build_graph(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
120117
<< std::endl;
121118
auto pool_layer =
122119
std::make_shared<it_lab_ai::PoolingLayer>(shape, pooltype, impl1);
123-
pool_layer->setName(it_lab_ai::kPooling);
124120
layers.push_back(pool_layer);
125121
layerpostop.push_back(false);
126122
if (comments) std::cout << "PoolingLayer added to layers." << std::endl;
@@ -129,15 +125,13 @@ void build_graph(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
129125
if (layer_type.find("Flatten") != std::string::npos) {
130126
auto flatten_layer = std::make_shared<it_lab_ai::FlattenLayer>(
131127
std::vector<size_t>({0, 3, 2, 1}));
132-
flatten_layer->setName(it_lab_ai::kFlatten);
133128
layers.push_back(flatten_layer);
134129
layerpostop.push_back(false);
135130
if (comments) std::cout << "FlattenLayer added to layers." << std::endl;
136131
}
137132

138133
if (layer_type.find("Dropout") != std::string::npos) {
139134
auto dropout_layer = std::make_shared<it_lab_ai::DropOutLayer>(0.0);
140-
dropout_layer->setName(it_lab_ai::kDropout);
141135
layers.push_back(dropout_layer);
142136
layerpostop.push_back(false);
143137
if (comments)
@@ -151,7 +145,6 @@ void build_graph(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
151145
std::cout << "number of layers - " << layers.size() + 1 << std::endl;
152146
it_lab_ai::Graph graph(static_cast<int>(layers.size()));
153147
it_lab_ai::InputLayer a1(it_lab_ai::kNchw, it_lab_ai::kNchw);
154-
a1.setName(it_lab_ai::kInput);
155148

156149
if (comments) std::cout << "InputLayer created." << std::endl;
157150

include/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
file(GLOB_RECURSE graph_headers graph/*.h graph/*.hpp)
22
set(GRAPH_HEADERS "${graph_headers}" PARENT_SCOPE)
33

4+
file(GLOB_RECURSE graphT_headers graph_transformations/*.h graph_transformations/*.hpp)
5+
set(GRAPHT_HEADERS "${graphT_headers}" PARENT_SCOPE)
6+
47
file(GLOB_RECURSE layers_headers layers/*.h layers/*.hpp)
58
set(LAYERS_HEADERS "${layers_headers}" PARENT_SCOPE)
69

include/graph/graph.hpp

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ struct BranchState {
2323

2424
class Graph {
2525
int BiggestSize_;
26-
int V_;
27-
std::vector<Layer*> layers_;
28-
std::vector<int> arrayV_;
29-
std::vector<int> arrayE_;
26+
int V_; // amount of ids
27+
std::vector<Layer*> layers_; // layers vector with some ids
28+
std::vector<int> arrayV_; // vertices (id -> vertex number)
29+
std::vector<int> arrayE_; // edges (vertex number -> id)
3030
std::vector<Tensor> inten_;
3131
std::vector<Tensor> outten_;
3232
Tensor* outtenres_;
3333
int start_;
3434
int end_;
3535
std::list<BranchState> branch_list_;
36-
std::vector<std::vector<int>> in_edges_;
36+
std::vector<std::vector<int>> in_edges_; // next -> prev
3737
std::vector<std::vector<std::pair<int, int>>> split_distribution_;
3838
int count_used_split_distribution_;
3939
#ifdef ENABLE_STATISTIC_TENSORS
@@ -67,6 +67,35 @@ class Graph {
6767
in_edges_.clear();
6868
}
6969

70+
int getVertexValue(size_t layerID) const {
71+
if (layerID >= arrayV_.size()) {
72+
throw std::invalid_argument("ArrayV does not contain this ID.");
73+
}
74+
return arrayV_[layerID];
75+
}
76+
77+
int getEdgeValue(size_t pos) const {
78+
if (pos >= arrayE_.size()) {
79+
throw std::invalid_argument("ArrayE does not contain this.");
80+
}
81+
return arrayE_[pos];
82+
}
83+
84+
size_t getInputsSize(size_t layerID) const {
85+
if (layerID >= in_edges_.size()) {
86+
throw std::invalid_argument("Input edges array do not contain this ID.");
87+
}
88+
return in_edges_[layerID].size();
89+
}
90+
91+
int getLayersCount() const { return V_; }
92+
const Layer& getLayerFromID(size_t layerID) const {
93+
if (layerID >= layers_.size()) {
94+
throw std::invalid_argument("Layers do not contain this ID.");
95+
}
96+
return *layers_[layerID];
97+
}
98+
7099
void setInput(Layer& lay, Tensor& vec) {
71100
lay.setID(0);
72101
layers_.push_back(&lay);
@@ -76,6 +105,29 @@ class Graph {
76105
V_++;
77106
in_edges_.resize(1);
78107
}
108+
109+
void addSingleLayer(Layer& lay) {
110+
bool layer_exists = false;
111+
for (const auto* layer : layers_) {
112+
if (layer == &lay) {
113+
layer_exists = true;
114+
break;
115+
}
116+
}
117+
118+
if (!layer_exists) {
119+
lay.setID(V_);
120+
layers_.push_back(&lay);
121+
arrayV_.push_back(static_cast<int>(arrayE_.size()));
122+
123+
if (V_ >= static_cast<int>(in_edges_.size())) {
124+
in_edges_.resize(V_ + 1);
125+
}
126+
127+
V_++;
128+
}
129+
}
130+
79131
void makeConnection(const Layer& layPrev, Layer& layNext) {
80132
bool layer_exists = false;
81133
for (const auto* layer : layers_) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <algorithm>
2+
#include <vector>
3+
4+
#include "graph/graph.hpp"
5+
#include "layers/Layer.hpp"
6+
7+
namespace it_lab_ai {
8+
std::vector<std::vector<int>> find_subgraphs(const Graph& graph,
9+
const Graph& subgraph);
10+
bool has_edge(const Graph& graph, int id_from, int id_to);
11+
bool is_root(const Graph& graph, int id);
12+
bool is_leaf(const Graph& graph, int id);
13+
bool run_search(const Graph& graph, const Graph& subgraph,
14+
std::vector<int>& assignments,
15+
std::vector<std::vector<int>>& results);
16+
} // namespace it_lab_ai

include/layers/BinaryOpLayer.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ class BinaryOpLayer : public Layer {
1414
public:
1515
enum class Operation : uint8_t { kMul, kAdd, kSub, kDiv };
1616

17-
BinaryOpLayer() = default;
18-
explicit BinaryOpLayer(Operation op) : op_(op) {}
17+
BinaryOpLayer() : Layer(kBinaryOp), op_(Operation::kMul) {}
18+
explicit BinaryOpLayer(Operation op) : Layer(kBinaryOp), op_(op) {}
1919

20-
static std::string get_name() { return "Binary Operation Layer"; }
2120
void run(const std::vector<Tensor>& input,
2221
std::vector<Tensor>& output) override;
2322
static bool is_scalar_tensor(const Tensor& t);
@@ -30,7 +29,7 @@ class BinaryOpLayer : public Layer {
3029
#endif
3130

3231
private:
33-
Operation op_ = Operation::kMul;
32+
Operation op_;
3433

3534
template <typename ValueType>
3635
void run_with_scalar_impl(const Tensor& input, ValueType scalar,

include/layers/ConcatLayer.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@ namespace it_lab_ai {
1111

1212
class ConcatLayer : public Layer {
1313
public:
14-
explicit ConcatLayer(int64_t axis = 0) : axis_(axis) {}
14+
explicit ConcatLayer(int64_t axis = 0) : Layer(kConcat), axis_(axis) {}
1515

1616
void run(const std::vector<Tensor>& input,
1717
std::vector<Tensor>& output) override;
1818

19-
static std::string get_name() { return "ConcatLayer"; }
20-
2119
#ifdef ENABLE_STATISTIC_WEIGHTS
2220
Tensor get_weights() override { return Tensor(); }
2321
#endif

include/layers/ConvLayer.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,16 @@ class ConvolutionalLayer : public Layer {
1818
ImplType implType_;
1919

2020
public:
21-
ConvolutionalLayer() = default;
21+
ConvolutionalLayer() : Layer(kConvolution) {
22+
stride_ = 0;
23+
pads_ = 0;
24+
dilations_ = 0;
25+
implType_ = kDefault;
26+
}
2227
ConvolutionalLayer(size_t step, size_t pads, size_t dilations,
2328
const Tensor& kernel, const Tensor& bias = Tensor(),
24-
ImplType implType = kDefault) {
29+
ImplType implType = kDefault)
30+
: Layer(kConvolution) {
2531
stride_ = step;
2632
pads_ = pads;
2733
dilations_ = dilations;

include/layers/DropOutLayer.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ class DropOutLayer : public Layer {
1010
double drop_rate_;
1111

1212
public:
13-
DropOutLayer() = default;
14-
DropOutLayer(double drop_rate) { drop_rate_ = drop_rate; }
15-
static std::string get_name() { return "DropOut layer"; }
13+
DropOutLayer(double drop_rate = 0.0) : Layer(kDropout) {
14+
drop_rate_ = drop_rate;
15+
}
1616
void run(const std::vector<Tensor>& input,
1717
std::vector<Tensor>& output) override;
1818
#ifdef ENABLE_STATISTIC_WEIGHTS

include/layers/EWLayer.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ T relu(const T& value) {
1919

2020
class EWLayer : public Layer {
2121
public:
22-
EWLayer() = default;
22+
EWLayer() : Layer(kElementWise), func_("none"), alpha_(0.0F), beta_(0.0F) {}
2323
EWLayer(std::string function, float alpha = 0.0F, float beta = 0.0F)
24-
: func_(std::move(function)), alpha_(alpha), beta_(beta) {}
24+
: Layer(kElementWise),
25+
func_(std::move(function)),
26+
alpha_(alpha),
27+
beta_(beta) {}
2528

26-
static std::string get_name() { return "Element-wise layer"; }
2729
void run(const std::vector<Tensor>& input,
2830
std::vector<Tensor>& output) override;
2931
#ifdef ENABLE_STATISTIC_WEIGHTS

0 commit comments

Comments
 (0)