-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBinaryOpLayer.hpp
More file actions
55 lines (44 loc) · 1.66 KB
/
Copy pathBinaryOpLayer.hpp
File metadata and controls
55 lines (44 loc) · 1.66 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
#pragma once
#include <algorithm>
#include <memory>
#include <stdexcept>
#include <utility>
#include <vector>
#include "Tensor.hpp"
#include "layers/Layer.hpp"
namespace it_lab_ai {
class BinaryOpLayer : public Layer {
public:
enum class Operation : uint8_t { kMul, kAdd, kSub, kDiv };
BinaryOpLayer() : Layer(kBinaryOp), op_(Operation::kMul) {}
explicit BinaryOpLayer(Operation op) : Layer(kBinaryOp), op_(op) {}
void run(const std::vector<Tensor>& input,
std::vector<Tensor>& output) override;
static bool is_scalar_tensor(const Tensor& t);
#ifdef ENABLE_STATISTIC_WEIGHTS
Tensor get_weights() override {
std::vector<int> v = {0};
return make_tensor(v);
}
#endif
private:
Operation op_;
template <typename ValueType>
void run_with_scalar_impl(const Tensor& input, ValueType scalar,
Tensor& output) const;
template <typename ValueType>
void run_broadcast_impl(const Tensor& A, const Tensor& B, Tensor& output,
const Shape& output_shape) const;
void run_with_scalar(const Tensor& input, float scalar, Tensor& output) const;
static bool can_broadcast(const Shape& shape_A, const Shape& shape_B);
static Shape calculate_broadcasted_shape(const Shape& shape_A,
const Shape& shape_B);
static std::vector<size_t> get_strides(const Shape& shape);
static size_t get_broadcasted_index(
size_t flat_index, const Shape& input_shape, const Shape& output_shape,
const std::vector<size_t>& input_strides,
const std::vector<size_t>& output_strides);
template <typename ValueType>
class BinaryOpLayerImpl;
};
} // namespace it_lab_ai