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