2 // Copyright (c) 2016 Intel Corporation
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 ///////////////////////////////////////////////////////////////////////////////////////////////////
19 #include "../C/activation.h"
20 #include "primitive.hpp"
24 /// @addtogroup cpp_api C++ API
26 /// @addtogroup cpp_topology Network Topology
28 /// @addtogroup cpp_primitives Primitives
31 /// @brief Activation using rectified linear unit or parameterized rectified linear unit.
32 /// @details Can get one negative slope or negative slope per channel.
34 /// out(i,x,y) = max(0, in(i,x,y)) + slope(i) * min(0, in(i,x,y))
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 CLDNN_DECLARE_PRIMITIVE(activation)
42 /// @brief Constructs Relu primitive.
43 /// @param id This primitive id.
44 /// @param input Input primitive id.
45 /// @param activation_func activation function.
46 /// @param additional_params additional params (slope/max_val/linear a,b).
47 activation(const primitive_id& id,
48 const primitive_id& input,
49 cldnn_activation_func activation_func,
50 cldnn_activation_additional_params additional_params = {0.f, 0.f},
51 const padding& output_padding = padding())
52 : primitive_base(id, {input}, output_padding),
53 activation_func(activation_func),
54 additional_params(additional_params),
55 additional_params_input("") {}
57 /// @brief Constructs activation with input per feature.
58 /// @param id This primitive id.
59 /// @param input Input primitive id.
60 /// @param additional_params_input additional params stored on a memory.
61 /// Input x dimension should be equal to input feature size (one value per channel. in case of linear is one pair per channel).
62 /// All other dimensions should be 1.
63 activation(const primitive_id& id,
64 const primitive_id& input,
65 const primitive_id& additional_params_input,
66 cldnn_activation_func activation_func,
67 const padding& output_padding = padding())
68 : primitive_base(id, {input}, output_padding),
69 activation_func(activation_func),
70 additional_params({0, 0}),
71 additional_params_input(additional_params_input) {}
73 /// @brief Constructs a copy from basic C API @CLDNN_PRIMITIVE_DESC{activation}
74 activation(const dto* dto)
75 : primitive_base(dto),
76 activation_func(dto->activation_func),
77 additional_params(dto->additional_params),
78 additional_params_input(dto->additional_params_input) {}
80 /// @brief activation function.
81 cldnn_activation_func activation_func;
83 /// @brief activation additional params.
84 cldnn_activation_additional_params additional_params;
86 /// @brief PRelu activation slope input primitive id.
87 /// Input x dimension should be equal to input feature size (one slope per channel).
88 /// All other dimensions should be 1.
89 primitive_id additional_params_input;
92 std::vector<std::reference_wrapper<const primitive_id>> get_dependencies() const override {
93 if (additional_params_input.empty())
95 return {additional_params_input};
98 void update_dto(dto& dto) const override {
99 dto.activation_func = activation_func;
100 dto.additional_params = additional_params;
101 dto.additional_params_input = additional_params_input.c_str();