Publishing R3
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / api / CPP / activation.hpp
1 /*
2 // Copyright (c) 2016 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 ///////////////////////////////////////////////////////////////////////////////////////////////////
18 #pragma once
19 #include "../C/activation.h"
20 #include "primitive.hpp"
21
22 namespace cldnn
23 {
24 /// @addtogroup cpp_api C++ API
25 /// @{
26 /// @addtogroup cpp_topology Network Topology
27 /// @{
28 /// @addtogroup cpp_primitives Primitives
29 /// @{
30
31 /// @brief Activation using rectified linear unit or parameterized rectified linear unit.
32 /// @details Can get one negative slope or negative slope per channel.
33 /// @par Algorithm:
34 ///   out(i,x,y) = max(0, in(i,x,y)) + slope(i) * min(0, in(i,x,y))
35 /// @par Where:
36 ///   @li out(i,x,y) : value at x, y from i-th feature map after activation.
37 ///   @li in(i,x,y) : value at x, y from i-th feature map before activation.
38 ///   @li slope(i) : the slope value of the i-th feature map (can be shared across channels or one slope per channel).
39 struct activation : public primitive_base<activation, CLDNN_PRIMITIVE_DESC(activation)>
40 {
41     CLDNN_DECLARE_PRIMITIVE(activation)
42
43     /// @brief Constructs Relu primitive.
44     /// @param id This primitive id.
45     /// @param input Input primitive id.
46     /// @param activation_func activation function.
47     /// @param additional_params additional params (slope/max_val/linear a,b).
48     activation(
49         const primitive_id& id,
50         const primitive_id& input,
51         cldnn_activation_func activation_func,
52         cldnn_activation_additional_params additional_params = { 0.f,0.f },
53         const padding& output_padding = padding()
54         )
55         : primitive_base(id, {input}, output_padding)
56         , activation_func(activation_func)
57         , additional_params(additional_params)
58         , additional_params_input("")
59     {
60     }
61
62     /// @brief Constructs activation with input per feature.
63     /// @param id This primitive id.
64     /// @param input Input primitive id.
65     /// @param additional_params_input additional params stored on a memory.
66     /// Input x dimension should be equal to input feature size (one value per channel. in case of linear is one pair per channel).
67     /// All other dimensions should be 1.
68     activation(
69         const primitive_id& id,
70         const primitive_id& input,
71         const primitive_id& additional_params_input,
72         cldnn_activation_func activation_func,
73         const padding& output_padding = padding()
74     )
75         : primitive_base(id, { input }, output_padding)
76         , activation_func(activation_func)
77         , additional_params({ 0,0 })
78         , additional_params_input(additional_params_input)
79     {
80     }
81
82     /// @brief Constructs a copy from basic C API @CLDNN_PRIMITIVE_DESC{activation}
83     activation(const dto* dto)
84         : primitive_base(dto)
85         , activation_func(dto->activation_func)
86         , additional_params(dto->additional_params)
87         , additional_params_input(dto->additional_params_input)
88     {
89     }
90
91     /// @brief activation function.
92     cldnn_activation_func activation_func;
93
94     /// @brief activation additional params.
95     cldnn_activation_additional_params additional_params;
96
97     /// @brief PRelu activation slope input primitive id.
98     /// Input x dimension should be equal to input feature size (one slope per channel).
99     /// All other dimensions should be 1.
100     primitive_id additional_params_input;
101
102 protected:
103
104     std::vector<std::reference_wrapper<const primitive_id>> get_dependencies() const override
105     {
106         if (additional_params_input.empty())
107             return{};
108         return{ additional_params_input };
109     }
110
111     void update_dto(dto& dto) const override
112     {
113         dto.activation_func = activation_func;
114         dto.additional_params = additional_params;
115         dto.additional_params_input = additional_params_input.c_str();
116     }
117 };
118 /// @}
119 /// @}
120 /// @}
121 }