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 subtract_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 subtract layer class (operation layer)
11 : *
12 : */
13 :
14 : #ifndef __SUBTRACT_LAYER_H__
15 : #define __SUBTRACT_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 Subtract Layer
26 : * @brief Subtract Layer
27 : */
28 : class SubtractLayer : public BinaryOperationLayer {
29 : public:
30 : /**
31 : * @brief Constructor of Subtract Layer
32 : */
33 41 : SubtractLayer() :
34 : BinaryOperationLayer(),
35 41 : subtract_props(props::Print(), props::InPlaceProp(),
36 82 : props::InPlaceDirectionProp()) {}
37 :
38 : /**
39 : * @brief Destructor of Sub Layer
40 : */
41 41 : ~SubtractLayer(){};
42 :
43 : /**
44 : * @brief Move constructor of Sub Layer.
45 : * @param[in] SubtractLayer &&
46 : */
47 : SubtractLayer(SubtractLayer &&rhs) noexcept = default;
48 :
49 : /**
50 : * @brief Move assignment operator.
51 : * @parma[in] rhs SubtractLayer to be moved.
52 : */
53 : SubtractLayer &operator=(SubtractLayer &&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>(subtract_props).empty() ||
89 0 : (std::get<props::InPlaceDirectionProp>(subtract_props).get() ==
90 : "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>(subtract_props).empty() ||
103 0 : std::get<props::InPlaceProp>(subtract_props).get()) {
104 2 : is_inplace = true;
105 : } else {
106 0 : is_inplace = false;
107 : }
108 2 : if (!supportInPlace())
109 : return InPlaceType::NONE;
110 : else
111 2 : return InPlaceType::NON_RESTRICTING;
112 : }
113 :
114 : /**
115 : * @copydoc Layer::exportTo(Exporter &exporter, ml::train::ExportMethods
116 : * method)
117 : */
118 2 : void exportTo(Exporter &exporter,
119 2 : const ml::train::ExportMethods &method) const final {}
120 :
121 : /**
122 : * @copydoc Layer::setProperty(const std::vector<std::string> &values)
123 : */
124 : void setProperty(const std::vector<std::string> &values) final;
125 :
126 : /**
127 : * @copydoc Layer::getType()
128 : */
129 137 : const std::string getType() const final { return SubtractLayer::type; };
130 :
131 : std::tuple<props::Print, props::InPlaceProp, props::InPlaceDirectionProp>
132 : subtract_props;
133 :
134 : static constexpr const char *type = "subtract";
135 : };
136 :
137 : } // namespace nntrainer
138 :
139 : #endif /* __cplusplus */
140 : #endif /* __SUBTRACT_LAYER_H__ */
|