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 divide_layer.h
6 : * @date 10 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 div layer class (operation layer)
11 : *
12 : */
13 :
14 : #ifndef __DIVIDE_LAYER_H__
15 : #define __DIVIDE_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 Divide Layer
26 : * @brief Divide Layer
27 : */
28 : class DivideLayer : public BinaryOperationLayer {
29 : public:
30 : /**
31 : * @brief Constructor of Divide Layer
32 : */
33 40 : DivideLayer() :
34 : BinaryOperationLayer(),
35 40 : divide_props(props::Print(), props::InPlaceProp(),
36 40 : props::InPlaceDirectionProp()),
37 80 : support_backwarding(true) {}
38 :
39 : /**
40 : * @brief Destructor of Divide Layer
41 : */
42 40 : ~DivideLayer(){};
43 :
44 : /**
45 : * @brief Move constructor of Divide Layer.
46 : * @param[in] DivideLayer &&
47 : */
48 : DivideLayer(DivideLayer &&rhs) noexcept = default;
49 :
50 : /**
51 : * @brief Move assignment operator.
52 : * @parma[in] rhs DivideLayer to be moved.
53 : */
54 : DivideLayer &operator=(DivideLayer &&rhs) = default;
55 :
56 : /**
57 : * @copydoc Layer::finalize(InitLayerContext &context)
58 : */
59 : void finalize(InitLayerContext &context) final;
60 :
61 : /**
62 : * @brief forwarding operation for add
63 : *
64 : * @param input0 input tensor 0
65 : * @param input1 input tensor 1
66 : * @param hidden tensor to store the result of addition
67 : */
68 : void forwarding_operation(const Tensor &input0, const Tensor &input1,
69 : Tensor &hidden) final;
70 :
71 : /**
72 : * @copydoc Layer::calcDerivative(RunLayerContext &context)
73 : */
74 : void calcDerivative(RunLayerContext &context) final;
75 :
76 : /**
77 : * @copydoc bool supportBackwarding() const
78 : */
79 14 : bool supportBackwarding() const final { return support_backwarding; };
80 :
81 : /**
82 : * @brief Get the inplace direction for the tensor operation layer
83 : *
84 : * @return InPlaceDirection
85 : */
86 0 : InPlaceDirection getInPlaceDirection() override {
87 0 : if (!supportInPlace())
88 : return InPlaceDirection::NONE;
89 0 : if (std::get<props::InPlaceDirectionProp>(divide_props).empty() ||
90 0 : (std::get<props::InPlaceDirectionProp>(divide_props).get() == "left")) {
91 0 : return InPlaceDirection::LEFT;
92 : } else {
93 : return InPlaceDirection::RIGHT;
94 : }
95 : };
96 :
97 : /**
98 : * @brief Initialize the in-place settings of the layer
99 : * @return InPlaceType
100 : */
101 2 : InPlaceType initializeInPlace() final {
102 2 : if (std::get<props::InPlaceProp>(divide_props).empty() ||
103 0 : !std::get<props::InPlaceProp>(divide_props).get()) {
104 2 : is_inplace = false;
105 2 : support_backwarding = true;
106 : } else {
107 0 : is_inplace = true;
108 0 : support_backwarding = false;
109 : }
110 :
111 2 : if (!supportInPlace())
112 : return InPlaceType::NONE;
113 : else
114 0 : return InPlaceType::NON_RESTRICTING;
115 : }
116 :
117 : /**
118 : * @copydoc Layer::exportTo(Exporter &exporter, ml::train::ExportMethods
119 : * method)
120 : */
121 2 : void exportTo(Exporter &exporter,
122 2 : const ml::train::ExportMethods &method) const final {}
123 :
124 : /**
125 : * @copydoc Layer::setProperty(const std::vector<std::string> &values)
126 : */
127 : void setProperty(const std::vector<std::string> &values) final;
128 :
129 : /**
130 : * @copydoc Layer::getType()
131 : */
132 130 : const std::string getType() const final { return DivideLayer::type; };
133 :
134 : std::tuple<props::Print, props::InPlaceProp, props::InPlaceDirectionProp>
135 : divide_props;
136 : bool support_backwarding; /**< support backwarding */
137 :
138 : static constexpr const char *type = "divide";
139 : };
140 :
141 : } // namespace nntrainer
142 :
143 : #endif /* __cplusplus */
144 : #endif /* __DIVIDE_LAYER_H__ */
|