Line data Source code
1 : // SPDX-License-Identifier: Apache-2.0
2 : /**
3 : * Copyright (C) 2024 SeungBaek Hong <sb92.hong@samsung.com>
4 : *
5 : * @file add_layer.h
6 : * @date 7 Oct 2024
7 : * @see https://github.com/nnstreamer/nntrainer
8 : * @author SeungBaek Hong <sb92.hong@samsung.com>
9 : * @bug No known bugs except for NYI items
10 : * @brief This is add layer class (operation layer)
11 : *
12 : */
13 :
14 : #ifndef __ADD_LAYER_H__
15 : #define __ADD_LAYER_H__
16 : #ifdef __cplusplus
17 :
18 : #include <common_properties.h>
19 : #include <layer_devel.h>
20 : #include <operation_layer.h>
21 :
22 : namespace nntrainer {
23 :
24 : /**
25 : * @class Add Layer
26 : * @brief Add Layer
27 : */
28 : class AddLayer : public BinaryOperationLayer {
29 : public:
30 : /**
31 : * @brief Destructor of Add Layer
32 : */
33 41 : ~AddLayer() {}
34 :
35 : /**
36 : * @brief Constructor of Add Layer
37 : */
38 41 : AddLayer() :
39 : BinaryOperationLayer(),
40 41 : add_props(props::Print(), props::InPlaceProp(),
41 82 : props::InPlaceDirectionProp()) {}
42 :
43 : /**
44 : * @brief Move constructor of Add Layer.
45 : * @param[in] AddLayer &&
46 : */
47 : AddLayer(AddLayer &&rhs) noexcept = default;
48 :
49 : /**
50 : * @brief Move assignment operator.
51 : * @parma[in] rhs AddLayer to be moved.
52 : */
53 : AddLayer &operator=(AddLayer &&rhs) = default;
54 :
55 : /**
56 : * @copydoc Layer::finalize(InitLayerContext &context)
57 : */
58 : void finalize(InitLayerContext &context) final;
59 :
60 : /**
61 : * @brief forwarding operation for add
62 : *
63 : * @param input0 input tensor 0
64 : * @param input1 input tensor 1
65 : * @param hidden tensor to store the result of addition
66 : */
67 : void forwarding_operation(const Tensor &input0, const Tensor &input1,
68 : Tensor &hidden) final;
69 :
70 : /**
71 : * @copydoc Layer::calcDerivative(RunLayerContext &context)
72 : */
73 : void calcDerivative(RunLayerContext &context) final;
74 :
75 : /**
76 : * @copydoc bool supportBackwarding() const
77 : */
78 14 : bool supportBackwarding() const final { return true; };
79 :
80 : /**
81 : * @brief Get the inplace direction for the tensor operation layer
82 : *
83 : * @return InPlaceDirection
84 : */
85 0 : InPlaceDirection getInPlaceDirection() override {
86 0 : if (!supportInPlace())
87 : return InPlaceDirection::NONE;
88 0 : if (std::get<props::InPlaceDirectionProp>(add_props).empty() ||
89 0 : (std::get<props::InPlaceDirectionProp>(add_props).get() == "left")) {
90 0 : return InPlaceDirection::LEFT;
91 : } else {
92 : return InPlaceDirection::RIGHT;
93 : }
94 : };
95 :
96 : /**
97 : * @brief Initialize the in-place settings of the layer
98 : * @return InPlaceType
99 : */
100 2 : InPlaceType initializeInPlace() final {
101 2 : if (std::get<props::InPlaceProp>(add_props).empty() ||
102 0 : std::get<props::InPlaceProp>(add_props).get()) {
103 2 : is_inplace = true;
104 : } else {
105 0 : is_inplace = false;
106 : }
107 2 : if (!supportInPlace())
108 : return InPlaceType::NONE;
109 : else
110 2 : return InPlaceType::NON_RESTRICTING;
111 : }
112 :
113 : /**
114 : * @copydoc Layer::exportTo(Exporter &exporter, ml::train::ExportMethods
115 : * method)
116 : */
117 2 : void exportTo(Exporter &exporter,
118 2 : const ml::train::ExportMethods &method) const final {}
119 :
120 : /**
121 : * @copydoc Layer::setProperty(const std::vector<std::string> &values)
122 : */
123 : void setProperty(const std::vector<std::string> &values) final;
124 :
125 : /**
126 : * @copydoc Layer::getType()
127 : */
128 137 : const std::string getType() const final { return AddLayer::type; }
129 :
130 : std::tuple<props::Print, props::InPlaceProp, props::InPlaceDirectionProp>
131 : add_props;
132 :
133 : static constexpr const char *type = "add";
134 : };
135 :
136 : } // namespace nntrainer
137 :
138 : #endif /* __cplusplus */
139 : #endif /* __ADD_LAYER_H__ */
|