Skip to content

Commit 7509f2f

Browse files
authored
Mul, Sub and Add Layer with broadcasting support (#186)
I will also add the functionality of element-by-element subtraction and element-by-element addition layers - sub and add
1 parent e986e85 commit 7509f2f

46 files changed

Lines changed: 570 additions & 67 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

app/Accuracy/accuracy_check.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "layers/OutputLayer.hpp"
88
#include "layers/PoolingLayer.hpp"
99

10-
using namespace itlab_2023;
10+
using namespace it_lab_ai;
1111

1212
int main() {
1313
std::string image_path = IMAGE1_PATH;

app/AccuracyImgNet/accimgnet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "layers/OutputLayer.hpp"
1111
#include "layers/PoolingLayer.hpp"
1212

13-
using namespace itlab_2023;
13+
using namespace it_lab_ai;
1414

1515
bool cmp_by_first(const std::pair<size_t, std::string>& a,
1616
const std::pair<size_t, std::string>& b) {

app/Graph/acc_check_mnist.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "build.cpp"
66
#include "build.hpp"
77

8-
using namespace itlab_2023;
8+
using namespace it_lab_ai;
99

1010
int main(int argc, char* argv[]) {
1111
bool parallel = false;

app/Graph/build.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void build_graph(Tensor& input, Tensor& output, bool comments,
8686
Tensor tmp_bias = make_tensor(tensor.get_bias());
8787
Tensor tmp_tensor =
8888
Tensor(Shape({tensor.get_shape()[1], tensor.get_shape()[0]}),
89-
itlab_2023::Type::kFloat);
89+
it_lab_ai::Type::kFloat);
9090
// kernel is always transposed ?
9191
for (size_t h = 0; h < tensor.get_shape()[0]; h++) {
9292
for (size_t w = 0; w < tensor.get_shape()[1]; w++) {

app/Graph/graph_build.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "build.hpp"
33

44
namespace fs = std::filesystem;
5-
using namespace itlab_2023;
5+
using namespace it_lab_ai;
66

77
int main(int argc, char* argv[]) {
88
std::string image_folder = IMAGE1_PATH;

include/Weights_Reader/reader_weights.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "layers/Tensor.hpp"
66

77
using json = nlohmann::json;
8-
using namespace itlab_2023;
8+
using namespace it_lab_ai;
99

1010
json read_json(const std::string& filename);
1111
void extract_values_from_json(const json& j, std::vector<float>& values);

include/graph/graph.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#include "layers/Layer.hpp"
1212

13-
namespace itlab_2023 {
13+
namespace it_lab_ai {
1414

1515
class Graph {
1616
int BiggestSize_;
@@ -148,4 +148,4 @@ class Graph {
148148
std::vector<Tensor> getWEIGHTS() { return weights_; }
149149
#endif
150150
};
151-
} // namespace itlab_2023
151+
} // namespace it_lab_ai

include/layers/BinaryOpLayer.hpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#pragma once
2+
#include <algorithm>
3+
#include <memory>
4+
#include <stdexcept>
5+
#include <utility>
6+
#include <vector>
7+
8+
#include "Tensor.hpp"
9+
#include "layers/Layer.hpp"
10+
11+
namespace it_lab_ai {
12+
13+
class BinaryOpLayer : public Layer {
14+
public:
15+
enum class Operation : uint8_t { kMul, kAdd, kSub, kDiv };
16+
17+
BinaryOpLayer() = default;
18+
explicit BinaryOpLayer(Operation op) : op_(op) {}
19+
20+
static std::string get_name() { return "Binary Operation Layer"; }
21+
void run(const Tensor& input, Tensor& output) override;
22+
void run(const Tensor& A, const Tensor& B, Tensor& output);
23+
static bool is_scalar_tensor(const Tensor& t);
24+
25+
#ifdef ENABLE_STATISTIC_WEIGHTS
26+
Tensor get_weights() override {
27+
std::vector<int> v = {0};
28+
return make_tensor(v);
29+
}
30+
#endif
31+
32+
private:
33+
Operation op_ = Operation::kMul;
34+
35+
template <typename ValueType>
36+
void run_with_scalar_impl(const Tensor& input, ValueType scalar,
37+
Tensor& output) const;
38+
template <typename ValueType>
39+
void run_broadcast_impl(const Tensor& A, const Tensor& B, Tensor& output,
40+
const Shape& output_shape) const;
41+
void run_with_scalar(const Tensor& input, float scalar, Tensor& output) const;
42+
43+
static bool can_broadcast(const Shape& shape_A, const Shape& shape_B);
44+
static Shape calculate_broadcasted_shape(const Shape& shape_A,
45+
const Shape& shape_B);
46+
static std::vector<size_t> get_strides(const Shape& shape);
47+
static size_t get_broadcasted_index(
48+
size_t flat_index, const Shape& input_shape, const Shape& output_shape,
49+
const std::vector<size_t>& input_strides,
50+
const std::vector<size_t>& output_strides);
51+
52+
template <typename ValueType>
53+
class BinaryOpLayerImpl;
54+
};
55+
56+
} // namespace it_lab_ai

include/layers/ConvLayer.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include "layers/Layer.hpp"
88

9-
namespace itlab_2023 {
9+
namespace it_lab_ai {
1010

1111
class ConvolutionalLayer : public Layer {
1212
private:
@@ -402,4 +402,4 @@ void Conv4DSTL(const Tensor& input, const Tensor& kernel_, const Tensor& bias_,
402402
output = make_tensor<ValueType>(one_d_vector, sh);
403403
}
404404

405-
} // namespace itlab_2023
405+
} // namespace it_lab_ai

include/layers/DropOutLayer.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include "layers/Layer.hpp"
55

6-
namespace itlab_2023 {
6+
namespace it_lab_ai {
77

88
class DropOutLayer : public Layer {
99
private:
@@ -16,4 +16,4 @@ class DropOutLayer : public Layer {
1616
void run(const Tensor& input, Tensor& output) override;
1717
};
1818

19-
} // namespace itlab_2023
19+
} // namespace it_lab_ai

0 commit comments

Comments
 (0)