Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / src / include / convolution_inst.h
1 /*
2 // Copyright (c) 2016-2018 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 "api/CPP/convolution.hpp"
20 #include "primitive_inst.h"
21
22 #include <memory>
23
24 namespace cldnn
25 {
26
27 template <>
28 struct typed_program_node<convolution> : public typed_program_node_base<convolution>
29 {
30     using parent = typed_program_node_base<convolution>;
31
32 public:
33     typed_program_node(std::shared_ptr<primitive> prim, program_impl& prog)
34         : parent(prim, prog)
35         , split(this->get_primitive()->split())
36         , depthwise_sep_opt(false)
37         , transposed(false)
38         , input_qf(this->get_primitive()->input_quantization_factor)
39         , output_qf(this->get_primitive()->output_quantization_factor)
40         , groups(this->get_primitive()->groups)
41     {
42         support_padding(true);
43     }
44
45     void set_split(int32_t node_split) { split = node_split; }
46     int32_t get_split() const { return split; }
47
48     void set_depthwise_sep_opt(bool node_depthwise_sep_opt) { depthwise_sep_opt = node_depthwise_sep_opt; }
49     bool get_depthwise_sep_opt() const { return depthwise_sep_opt; }
50
51     void set_transposed(bool node_transposed) { transposed = node_transposed; }
52     bool get_transposed() const { return transposed; }
53
54     void set_groups(uint32_t node_groups) { groups = node_groups; }
55     uint32_t get_groups() const { return groups; }
56
57     program_node& input() const { return get_dependency(0); }
58
59     program_node& weights(size_t idx = 0) const
60     {
61         if (static_cast<int32_t>(idx) >= this->get_split())
62             throw std::range_error("weights offset too big");
63
64         return get_dependency(1 + idx);
65     }
66
67     program_node& bias(size_t idx = 0) const
68     { 
69         if (static_cast<int32_t>(idx) >= this->get_split())
70             throw std::range_error("bias offset too big");
71
72         return get_dependency(1 + this->get_split() + idx);
73     }
74
75     program_node& weights_quantization_factors(size_t idx = 0) const
76     {
77         if (static_cast<int32_t>(idx) >= this->get_split())
78             throw std::range_error("quantization factor offset too big");
79
80         return get_dependency(1 + 2*this->get_split() + idx);
81     }
82
83     program_node& output_calibration_factors(size_t idx = 0) const
84     {
85         if (static_cast<int32_t>(idx) >= this->get_split())
86             throw std::range_error("calibration factor offset too big");
87
88         return get_dependency(1 + 3 * this->get_split() + idx);
89     }
90
91     bool bias_term() const
92     {
93         return get_primitive()->bias.size() > 0;
94     }
95
96     bool weights_quantization_term() const
97     {
98         return get_primitive()->weights_quantization_factors.size() > 0;
99     }
100
101     bool output_calibration_term() const
102     {
103         return get_primitive()->output_calibration_factors.size() > 0;
104     }
105     
106     float get_input_qf() const { return input_qf; }
107     float get_output_qf() const { return output_qf; }
108
109 private:
110     int32_t split;
111     bool depthwise_sep_opt;
112     bool transposed;
113     float input_qf;
114     float output_qf;
115     uint32_t groups;
116 };
117
118 using convolution_node = typed_program_node<convolution>;
119
120 template <>
121 class typed_primitive_inst<convolution> : public typed_primitive_inst_base<convolution>
122 {
123     using parent = typed_primitive_inst_base<convolution>;
124
125 public:
126     static layout calc_output_layout(convolution_node const& node);
127     static std::string to_string(convolution_node const& node);
128
129 public:
130     typed_primitive_inst(network_impl& network, convolution_node const& node);
131
132     memory_impl& weights_memory(size_t index) const
133     {
134         if (node.get_groups() == 1) {
135             if (static_cast<int32_t>(index) >= node.get_split())
136                 throw std::range_error("weights offset too big");
137             return dep_memory(1 + index);
138         }
139         else { // all weights are in one buffer
140             return dep_memory(1);
141         }
142     }
143
144     memory_impl& bias_memory(size_t index) const
145     { 
146         if (node.get_groups() == 1) {
147             if (static_cast<int32_t>(index) >= node.get_split())
148                 throw std::range_error("bias offset too big");
149             return dep_memory(1 + node.get_split() + index);
150         }
151         else { // all bias are in one buffer
152             return dep_memory(2);
153         }
154     }
155
156     memory_impl& weights_quantization_factors_memory(size_t index) const
157     {
158         if (node.get_groups() == 1) {
159             if (static_cast<int32_t>(index) >= node.get_split())
160                 throw std::range_error("quantization factors offset too big");
161             return dep_memory(1 + 2 * node.get_split() + index);
162         }
163         else { // all quantization_factors are in one buffer
164             return dep_memory(3);
165         };
166     }
167
168     memory_impl& output_calibration_factors_memory(size_t index) const
169     {
170         if (node.get_groups() == 1) {
171             if (static_cast<int32_t>(index) >= node.get_split())
172                 throw std::range_error("quantization factors offset too big");
173             return dep_memory(1 + 3 * node.get_split() + index);
174         }
175         else { // all calibration_factors are in one buffer
176             return dep_memory(4);
177         }
178     }
179
180     bool bias_term() const
181     {
182         return node.bias_term();
183     }
184
185     bool weights_quantization_factors_term() const
186     {
187         return node.weights_quantization_term();
188     }
189
190     bool output_calibration_factors_term() const
191     {
192         return node.output_calibration_term();
193     }
194 };
195
196 using convolution_inst = typed_primitive_inst<convolution>;
197
198 }