Skip to content

Commit 7312711

Browse files
authored
Implement graph deep copy function, switch to pointers to weights/bias (#269)
1 parent 3791483 commit 7312711

18 files changed

Lines changed: 479 additions & 102 deletions

File tree

include/graph/graph.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ struct BranchState {
2323
std::vector<std::pair<int, int>> distribution;
2424
};
2525

26+
std::shared_ptr<Layer> layer_based_shared_copy(
27+
const std::shared_ptr<Layer>& layer, const RuntimeOptions& options);
28+
2629
class Graph {
2730
int BiggestSize_;
2831
int V_; // amount of ids
@@ -72,6 +75,9 @@ class Graph {
7275
Graph& operator=(Graph&&) noexcept = default;
7376
~Graph() = default;
7477

78+
void clone(Graph& result, Tensor& out,
79+
const RuntimeOptions& options = RuntimeOptions()) const;
80+
7581
void setSplitDistribution(
7682
std::vector<std::vector<std::pair<int, int>>> split_dist) {
7783
split_distribution_ = std::move(split_dist);

include/layers/ConvLayer.hpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,48 @@ class ConvolutionalLayer : public Layer {
2222
size_t stride_;
2323
size_t pads_;
2424
size_t dilations_;
25-
Tensor kernel_;
26-
Tensor bias_;
25+
std::shared_ptr<Tensor> kernel_;
26+
std::shared_ptr<Tensor> bias_;
2727
size_t group_;
2828
bool useLegacyImpl_;
2929

3030
public:
31-
ConvolutionalLayer() : Layer(kConvolution) {
31+
ConvolutionalLayer() : Layer(kConvolution), kernel_(nullptr), bias_(nullptr) {
3232
stride_ = 0;
3333
pads_ = 0;
3434
dilations_ = 0;
3535
}
3636
ConvolutionalLayer(size_t step, size_t pads, size_t dilations,
3737
const Tensor& kernel, const Tensor& bias = Tensor(),
3838
size_t group = 1, bool useLegacyImpl = false)
39-
: Layer(kConvolution) {
39+
: Layer(kConvolution),
40+
kernel_(std::make_shared<Tensor>(kernel)),
41+
bias_(std::make_shared<Tensor>(bias)) {
42+
stride_ = step;
43+
pads_ = pads;
44+
group_ = group;
45+
dilations_ = dilations;
46+
useLegacyImpl_ = useLegacyImpl;
47+
}
48+
ConvolutionalLayer(size_t step, size_t pads, size_t dilations,
49+
std::shared_ptr<Tensor> kernel,
50+
std::shared_ptr<Tensor> bias = std::make_shared<Tensor>(),
51+
size_t group = 1, bool useLegacyImpl = false)
52+
: Layer(kConvolution),
53+
kernel_(std::move(kernel)),
54+
bias_(std::move(bias)) {
4055
stride_ = step;
4156
pads_ = pads;
4257
group_ = group;
4358
dilations_ = dilations;
44-
kernel_ = kernel;
45-
bias_ = bias;
4659
useLegacyImpl_ = useLegacyImpl;
4760
}
4861
void run(const std::vector<Tensor>& input,
4962
std::vector<Tensor>& output) override;
5063
void run(const std::vector<Tensor>& input, std::vector<Tensor>& output,
5164
const RuntimeOptions& options) override;
5265
#ifdef ENABLE_STATISTIC_WEIGHTS
53-
Tensor get_weights() override { return kernel_; }
66+
Tensor get_weights() override { return *kernel_; }
5467
#endif
5568
};
5669

include/layers/FCLayer.hpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,23 @@ namespace it_lab_ai {
1111

1212
class FCLayer : public Layer {
1313
private:
14-
Tensor weights_;
15-
Tensor bias_;
14+
std::shared_ptr<Tensor> weights_;
15+
std::shared_ptr<Tensor> bias_;
1616

1717
public:
18-
FCLayer() : Layer(kFullyConnected) {}
19-
FCLayer(Tensor weights, const Tensor& bias)
20-
: Layer(kFullyConnected), weights_(std::move(weights)), bias_(bias) {}
18+
FCLayer() : Layer(kFullyConnected), weights_(nullptr), bias_(nullptr) {}
19+
FCLayer(const Tensor& weights, const Tensor& bias)
20+
: Layer(kFullyConnected),
21+
weights_(std::make_shared<Tensor>(weights)),
22+
bias_(std::make_shared<Tensor>(bias)) {}
23+
FCLayer(std::shared_ptr<Tensor> weights, std::shared_ptr<Tensor> bias)
24+
: Layer(kFullyConnected),
25+
weights_(std::move(weights)),
26+
bias_(std::move(bias)) {}
2127
void run(const std::vector<Tensor>& input,
2228
std::vector<Tensor>& output) override;
2329
#ifdef ENABLE_STATISTIC_WEIGHTS
24-
Tensor get_weights() override { return weights_; }
30+
Tensor get_weights() override { return *weights_; }
2531
#endif
2632
};
2733

include/layers_oneDNN/BinaryOpLayer.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class BinaryOpLayerOneDnn : public Layer {
1717
explicit BinaryOpLayerOneDnn(BinaryOpLayer::Operation op)
1818
: Layer(kBinaryOp), op_(op) {}
1919

20+
BinaryOpLayerOneDnn(const BinaryOpLayerOneDnn& c) : Layer(kBinaryOp) {
21+
this->op_ = c.op_;
22+
}
23+
2024
void run(const std::vector<Tensor>& input,
2125
std::vector<Tensor>& output) override;
2226

include/layers_oneDNN/ConvLayer.hpp

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class ConvLayerOneDnn : public Layer {
1616
stride_(1),
1717
pads_(0),
1818
dilations_(1),
19+
kernel_(nullptr),
20+
bias_(nullptr),
1921
group_(1),
2022
use_legacy_(false) {}
2123

@@ -26,16 +28,39 @@ class ConvLayerOneDnn : public Layer {
2628
stride_(stride),
2729
pads_(pads),
2830
dilations_(dilations),
29-
kernel_(kernel),
30-
bias_(bias),
31+
kernel_(std::make_shared<Tensor>(kernel)),
32+
bias_(std::make_shared<Tensor>(bias)),
3133
group_(group),
3234
use_legacy_(use_legacy) {}
3335

36+
ConvLayerOneDnn(size_t stride, size_t pads, size_t dilations,
37+
std::shared_ptr<Tensor> kernel,
38+
std::shared_ptr<Tensor> bias = std::make_shared<Tensor>(),
39+
size_t group = 1, bool use_legacy = false)
40+
: Layer(kConvolution),
41+
stride_(stride),
42+
pads_(pads),
43+
dilations_(dilations),
44+
kernel_(std::move(kernel)),
45+
bias_(std::move(bias)),
46+
group_(group),
47+
use_legacy_(use_legacy) {}
48+
49+
ConvLayerOneDnn(const ConvLayerOneDnn& c) : Layer(kConvolution) {
50+
this->stride_ = c.stride_;
51+
this->pads_ = c.pads_;
52+
this->dilations_ = c.dilations_;
53+
this->kernel_ = c.kernel_;
54+
this->bias_ = c.bias_;
55+
this->group_ = c.group_;
56+
this->use_legacy_ = c.use_legacy_;
57+
}
58+
3459
void run(const std::vector<Tensor>& input,
3560
std::vector<Tensor>& output) override;
3661

3762
#ifdef ENABLE_STATISTIC_WEIGHTS
38-
Tensor get_weights() override { return kernel_; }
63+
Tensor get_weights() override { return *kernel_; }
3964
#endif
4065

4166
private:
@@ -85,8 +110,8 @@ class ConvLayerOneDnn : public Layer {
85110
size_t stride_;
86111
size_t pads_;
87112
size_t dilations_;
88-
Tensor kernel_;
89-
Tensor bias_;
113+
std::shared_ptr<Tensor> kernel_;
114+
std::shared_ptr<Tensor> bias_;
90115
size_t group_;
91116
bool use_legacy_;
92117

include/layers_oneDNN/EWLayer.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ class EwLayerOneDnn : public Layer {
2121
alpha_(alpha),
2222
beta_(beta) {}
2323

24+
EwLayerOneDnn(const EwLayerOneDnn& c) : Layer(kElementWise) {
25+
this->func_ = c.func_;
26+
this->alpha_ = c.alpha_;
27+
this->beta_ = c.beta_;
28+
}
29+
2430
void run(const std::vector<Tensor>& input,
2531
std::vector<Tensor>& output) override;
2632
static bool is_function_supported(const std::string& function);

include/layers_oneDNN/PoolingLayer.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ class PoolingLayerOneDnn : public Layer {
2727
engine_(std::make_unique<dnnl::engine>(dnnl::engine::kind::cpu, 0)),
2828
stream_(std::make_unique<dnnl::stream>(*engine_)) {}
2929

30+
PoolingLayerOneDnn(const PoolingLayerOneDnn& c)
31+
: Layer(kPooling),
32+
engine_(std::make_unique<dnnl::engine>(dnnl::engine::kind::cpu, 0)),
33+
stream_(std::make_unique<dnnl::stream>(*engine_)) {
34+
this->poolingShape_ = c.poolingShape_;
35+
this->strides_ = c.strides_;
36+
this->pads_ = c.pads_;
37+
this->dilations_ = c.dilations_;
38+
this->ceil_mode_ = c.ceil_mode_;
39+
this->poolingType_ = c.poolingType_;
40+
}
41+
3042
void run(const std::vector<Tensor>& input,
3143
std::vector<Tensor>& output) override;
3244

include/layers_oneDNN/ReduceLayer.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ class ReduceLayerOneDnn : public Layer {
2020
const std::vector<int64_t>& axes = {})
2121
: ReduceLayerOneDnn(ReduceLayer::Operation::kSum, keepdims, axes) {}
2222

23+
ReduceLayerOneDnn(const ReduceLayerOneDnn& c) : Layer(kReduce) {
24+
this->op_ = c.op_;
25+
this->keepdims_ = c.keepdims_;
26+
this->axes_ = c.axes_;
27+
}
28+
2329
void run(const std::vector<Tensor>& input,
2430
std::vector<Tensor>& output) override;
2531

src/graph/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
file(GLOB_RECURSE graph_src *.cpp)
22
add_library(graph_lib STATIC "${GRAPH_HEADERS}" "${graph_src}")
3-
target_link_libraries(graph_lib PUBLIC TBB_unified)
3+
target_link_libraries(graph_lib PUBLIC dnnl TBB_unified)
44
add_dependencies(graph_lib kokkos_external)

0 commit comments

Comments
 (0)