2 // Copyright (c) 2016-2018 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 "api/CPP/convolution.hpp"
20 #include "primitive_inst.h"
28 struct typed_program_node<convolution> : public typed_program_node_base<convolution>
30 using parent = typed_program_node_base<convolution>;
33 typed_program_node(std::shared_ptr<primitive> prim, program_impl& prog)
35 , split(this->get_primitive()->split())
36 , depthwise_sep_opt(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)
42 support_padding(true);
45 void set_split(int32_t node_split) { split = node_split; }
46 int32_t get_split() const { return split; }
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; }
51 void set_transposed(bool node_transposed) { transposed = node_transposed; }
52 bool get_transposed() const { return transposed; }
54 void set_groups(uint32_t node_groups) { groups = node_groups; }
55 uint32_t get_groups() const { return groups; }
57 program_node& input() const { return get_dependency(0); }
59 program_node& weights(size_t idx = 0) const
61 if (static_cast<int32_t>(idx) >= this->get_split())
62 throw std::range_error("weights offset too big");
64 return get_dependency(1 + idx);
67 program_node& bias(size_t idx = 0) const
69 if (static_cast<int32_t>(idx) >= this->get_split())
70 throw std::range_error("bias offset too big");
72 return get_dependency(1 + this->get_split() + idx);
75 program_node& weights_quantization_factors(size_t idx = 0) const
77 if (static_cast<int32_t>(idx) >= this->get_split())
78 throw std::range_error("quantization factor offset too big");
80 return get_dependency(1 + 2*this->get_split() + idx);
83 program_node& output_calibration_factors(size_t idx = 0) const
85 if (static_cast<int32_t>(idx) >= this->get_split())
86 throw std::range_error("calibration factor offset too big");
88 return get_dependency(1 + 3 * this->get_split() + idx);
91 bool bias_term() const
93 return get_primitive()->bias.size() > 0;
96 bool weights_quantization_term() const
98 return get_primitive()->weights_quantization_factors.size() > 0;
101 bool output_calibration_term() const
103 return get_primitive()->output_calibration_factors.size() > 0;
106 float get_input_qf() const { return input_qf; }
107 float get_output_qf() const { return output_qf; }
111 bool depthwise_sep_opt;
118 using convolution_node = typed_program_node<convolution>;
121 class typed_primitive_inst<convolution> : public typed_primitive_inst_base<convolution>
123 using parent = typed_primitive_inst_base<convolution>;
126 static layout calc_output_layout(convolution_node const& node);
127 static std::string to_string(convolution_node const& node);
130 typed_primitive_inst(network_impl& network, convolution_node const& node);
132 memory_impl& weights_memory(size_t index) const
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);
139 else { // all weights are in one buffer
140 return dep_memory(1);
144 memory_impl& bias_memory(size_t index) const
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);
151 else { // all bias are in one buffer
152 return dep_memory(2);
156 memory_impl& weights_quantization_factors_memory(size_t index) const
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);
163 else { // all quantization_factors are in one buffer
164 return dep_memory(3);
168 memory_impl& output_calibration_factors_memory(size_t index) const
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);
175 else { // all calibration_factors are in one buffer
176 return dep_memory(4);
180 bool bias_term() const
182 return node.bias_term();
185 bool weights_quantization_factors_term() const
187 return node.weights_quantization_term();
190 bool output_calibration_factors_term() const
192 return node.output_calibration_term();
196 using convolution_inst = typed_primitive_inst<convolution>;