Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / api / CPP / contract.hpp
1 // Copyright (c) 2019 Intel Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 ///////////////////////////////////////////////////////////////////////////////////////////////////
16 #pragma once
17
18 #include "../C/contract.h"
19 #include "primitive.hpp"
20
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 Select mode for the @ref contract layer.
32     enum class contract_mode : int32_t
33     {
34         /// @brief Sum reduction.
35         sum = cldnn_contract_sum,
36         /// @brief Product reduction.
37         prod = cldnn_contract_product,
38         /// @brief All reduction.
39         all = cldnn_contract_all,
40         /// @brief Any reduction.
41         any = cldnn_contract_any,
42         /// @brief Max reduction.
43         max = cldnn_contract_max
44     };
45
46     /// @brief Reduces input with an operation defined by @p mode along defined
47     ///        by @p reduction_axes dimensions.
48     ///
49     /// @details Reduces the input using the binary operation determined by
50     ///          @p mode. The @p reduction_axes determine the final shape of the
51     ///          output, which is calculated based on the input shape by
52     ///          collapsing the dimensions along which the reduction happens.
53     ///          For example, for the input with
54     /// @n      <tt>input_sizes = (in_b, in_f, in_y, in_x)</tt>
55     /// @n a reduction with
56     /// @n      <tt>reduction_axes = (2)</tt>
57     /// @n would collapse the Y dimension, producing
58     /// @n      <tt>output_shape = (1, in_b, in_f, in_x)</tt>
59     /// @n where every element is a @p mode reduction of the input elements with
60     /// @n the same B, F and X coordinates.
61     /// @n
62     /// @n@b Requirements:
63     /// @n - @p reduction_axes size (dimensions count) must be within (inclusive) range
64     ///      1 - 4.
65     /// @n - @p reduction_axes mustn't have duplicate values.
66     /// @n - Values of @p reduction_axes must be within (inclusive) range 0 - 3
67     /// @n Breaking any of these conditions will raise an exception.
68     struct contract : public primitive_base<contract, CLDNN_PRIMITIVE_DESC(contract)>
69     {
70         CLDNN_DECLARE_PRIMITIVE(contract)
71
72             /// @brief Constructs contract primitive / layer.
73             ///
74             /// @param id              An identifier of new primitive.
75             /// @param input           An identifier of primitive which is an input for newly created
76             ///                        contract primitive.
77             /// @param mode            Reduction mode.
78             /// @param reduction_axes  Axes positions (0-based, from left to right) in input_shape
79             ///                        that are being reduced.
80             /// @param output_padding  Optional padding for output from primitive.
81             contract(
82                 const primitive_id& id,
83                 const primitive_id& input,
84                 contract_mode mode,
85                 const std::vector<uint16_t>& reduction_axes = {},
86                 const padding& output_padding = padding()
87             )
88             : primitive_base(id, { input }, output_padding),
89             mode(mode),
90             reduction_axes(reduction_axes)
91         {
92         }
93
94         /// @brief Constructs a copy from C API @CLDNN_PRIMITIVE_DESC{contract}
95         contract(const dto* dto)
96             : primitive_base(dto),
97             mode(static_cast<contract_mode>(dto->mode)),
98             reduction_axes(uint16_t_arr_to_vector(dto->reduction_axes))
99
100         {
101         }
102
103         /// @param mode Contract mode.
104         contract_mode mode;
105         /// @brief Array of axes positions from input shape (0-based, from left to right)
106         ///        along which reduction should happen.
107         std::vector<uint16_t> reduction_axes;
108
109     protected:
110         void update_dto(dto& dto) const override
111         {
112             dto.mode = static_cast<cldnn_contract_mode>(mode);
113             dto.reduction_axes = uint16_t_vector_to_arr(reduction_axes);
114         }
115     };
116     /// @}
117     /// @}
118     /// @}
119 }