1/*******************************************************************************
2* Copyright 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#include "mkldnn.h"
18#include <assert.h>
19
20#include "c_types_map.hpp"
21#include "type_helpers.hpp"
22#include "utils.hpp"
23
24using namespace mkldnn::impl;
25using namespace mkldnn::impl::utils;
26using namespace mkldnn::impl::status;
27using namespace mkldnn::impl::prop_kind;
28using namespace mkldnn::impl::alg_kind;
29using namespace mkldnn::impl::types;
30
31namespace {
32status_t deconv_desc_init(deconvolution_desc_t *deconv_desc,
33 prop_kind_t prop_kind, alg_kind_t alg_kind,
34 const memory_desc_t *src_desc, const memory_desc_t *weights_desc,
35 const memory_desc_t *bias_desc, const memory_desc_t *dst_desc,
36 const dims_t strides, const dims_t dilates, const dims_t padding_l,
37 const dims_t padding_r, padding_kind_t padding_kind) {
38 bool args_ok = true
39 && !any_null(deconv_desc, src_desc, weights_desc, dst_desc, strides,
40 padding_l)
41 && one_of(alg_kind, deconvolution_direct, deconvolution_winograd)
42 && one_of(padding_kind, padding_kind::padding_zero);
43 if (!args_ok)
44 return invalid_arguments;
45
46 if (padding_r == nullptr)
47 padding_r = padding_l;
48
49 auto dd = deconvolution_desc_t();
50 dd.primitive_kind = primitive_kind::deconvolution;
51 dd.prop_kind = prop_kind;
52 dd.alg_kind = alg_kind;
53
54 dd.diff_src_desc = dd.src_desc = zero_md();
55 dd.diff_dst_desc = dd.dst_desc = zero_md();
56 dd.diff_weights_desc = dd.weights_desc = zero_md();
57 dd.diff_bias_desc = dd.bias_desc = zero_md();
58
59 const bool is_fwd = one_of(prop_kind, forward_training, forward_inference);
60 const bool with_bias
61 = bias_desc && bias_desc->format_kind != format_kind::undef;
62 const bool with_groups = weights_desc->ndims == src_desc->ndims + 1;
63
64 (prop_kind == backward_data ? dd.diff_src_desc : dd.src_desc) = *src_desc;
65 (is_fwd ? dd.dst_desc : dd.diff_dst_desc) = *dst_desc;
66 (prop_kind == backward_weights ? dd.diff_weights_desc : dd.weights_desc)
67 = *weights_desc;
68 if (with_bias)
69 (prop_kind == backward_weights ? dd.diff_bias_desc : dd.bias_desc)
70 = *bias_desc;
71
72 int sp_dims = src_desc->ndims - 2;
73 utils::array_copy(dd.strides, strides, sp_dims);
74 utils::array_copy(dd.padding[0], padding_l, sp_dims);
75 utils::array_copy(dd.padding[1], padding_r, sp_dims);
76 if (dilates)
77 utils::array_copy(dd.dilates, dilates, sp_dims);
78 else
79 utils::array_set(dd.dilates, 0, sp_dims);
80
81 dd.padding_kind = padding_kind;
82 dd.accum_data_type = types::default_accum_data_type(src_desc->data_type,
83 weights_desc->data_type, dst_desc->data_type, prop_kind);
84
85 const int g = with_groups ? weights_desc->dims[0] : 1;
86 bool consistency = true
87 && src_desc->ndims == dst_desc->ndims
88 && utils::one_of(src_desc->ndims, 3, 4, 5)
89 && utils::one_of(weights_desc->ndims, src_desc->ndims,
90 src_desc->ndims + 1)
91 && (with_bias ? bias_desc->ndims == 1 : true)
92 && (with_bias ? bias_desc->dims[0] == dst_desc->dims[1] : true)
93 && src_desc->dims[0] == dst_desc->dims[0]
94 && src_desc->dims[1] == g * weights_desc->dims[with_groups + 1]
95 && dst_desc->dims[1] == g * weights_desc->dims[with_groups + 0];
96 for (int i = 2; i < src_desc->ndims; ++i) {
97 int src = src_desc->dims[i];
98 int ker = weights_desc->dims[with_groups + i];
99 int dil = dd.dilates[i - 2];
100 int pad = padding_l[i - 2] + padding_r[i - 2];
101 int str = strides[i - 2];
102 int dst = dst_desc->dims[i];
103 int ker_range = 1 + (ker - 1) * (dil + 1);
104
105 consistency
106 = consistency && (dst - ker_range + pad) / str + 1 == src;
107 }
108 if (!consistency)
109 return invalid_arguments;
110
111 *deconv_desc = dd;
112 return success;
113}
114}
115
116status_t mkldnn_deconvolution_forward_desc_init(
117 deconvolution_desc_t *deconv_desc, prop_kind_t prop_kind,
118 alg_kind_t alg_kind, const memory_desc_t *src_desc,
119 const memory_desc_t *weights_desc, const memory_desc_t *bias_desc,
120 const memory_desc_t *dst_desc, const dims_t strides,
121 const dims_t padding_l, const dims_t padding_r,
122 padding_kind_t padding_kind) {
123 if (!one_of(prop_kind, forward_training, forward_inference))
124 return invalid_arguments;
125 return deconv_desc_init(deconv_desc, prop_kind, alg_kind, src_desc,
126 weights_desc, bias_desc, dst_desc, strides, nullptr, padding_l,
127 padding_r, padding_kind);
128}
129
130status_t mkldnn_dilated_deconvolution_forward_desc_init(
131 deconvolution_desc_t *deconv_desc, prop_kind_t prop_kind,
132 alg_kind_t alg_kind, const memory_desc_t *src_desc,
133 const memory_desc_t *weights_desc, const memory_desc_t *bias_desc,
134 const memory_desc_t *dst_desc, const dims_t strides,
135 const dims_t dilates, const dims_t padding_l, const dims_t padding_r,
136 padding_kind_t padding_kind) {
137 if (!one_of(prop_kind, forward_training, forward_inference))
138 return invalid_arguments;
139 return deconv_desc_init(deconv_desc, prop_kind, alg_kind, src_desc,
140 weights_desc, bias_desc, dst_desc, strides, dilates, padding_l,
141 padding_r, padding_kind);
142}
143
144status_t mkldnn_deconvolution_backward_data_desc_init(
145 deconvolution_desc_t *deconv_desc, alg_kind_t alg_kind,
146 const memory_desc_t *diff_src_desc, const memory_desc_t *weights_desc,
147 const memory_desc_t *diff_dst_desc, const dims_t strides,
148 const dims_t padding_l, const dims_t padding_r,
149 padding_kind_t padding_kind) {
150 return deconv_desc_init(deconv_desc, backward_data, alg_kind, diff_src_desc,
151 weights_desc, nullptr, diff_dst_desc, strides, nullptr, padding_l,
152 padding_r, padding_kind);
153}
154
155status_t mkldnn_dilated_deconvolution_backward_data_desc_init(
156 deconvolution_desc_t *deconv_desc, alg_kind_t alg_kind,
157 const memory_desc_t *diff_src_desc, const memory_desc_t *weights_desc,
158 const memory_desc_t *diff_dst_desc, const dims_t strides,
159 const dims_t dilates, const dims_t padding_l, const dims_t padding_r,
160 padding_kind_t padding_kind) {
161 return deconv_desc_init(deconv_desc, backward_data, alg_kind, diff_src_desc,
162 weights_desc, nullptr, diff_dst_desc, strides,dilates, padding_l,
163 padding_r, padding_kind);
164}
165
166status_t mkldnn_deconvolution_backward_weights_desc_init(
167 deconvolution_desc_t *deconv_desc, alg_kind_t alg_kind,
168 const memory_desc_t *src_desc, const memory_desc_t *diff_weights_desc,
169 const memory_desc_t *diff_bias_desc, const memory_desc_t *diff_dst_desc,
170 const dims_t strides, const dims_t padding_l, const dims_t padding_r,
171 padding_kind_t padding_kind) {
172 return deconv_desc_init(deconv_desc, backward_weights, alg_kind, src_desc,
173 diff_weights_desc, diff_bias_desc, diff_dst_desc, strides, nullptr,
174 padding_l, padding_r, padding_kind);
175}
176
177status_t mkldnn_dilated_deconvolution_backward_weights_desc_init(
178 deconvolution_desc_t *deconv_desc, alg_kind_t alg_kind,
179 const memory_desc_t *src_desc, const memory_desc_t *diff_weights_desc,
180 const memory_desc_t *diff_bias_desc, const memory_desc_t *diff_dst_desc,
181 const dims_t strides, const dims_t dilates, const dims_t padding_l,
182 const dims_t padding_r, padding_kind_t padding_kind) {
183 return deconv_desc_init(deconv_desc, backward_weights, alg_kind, src_desc,
184 diff_weights_desc, diff_bias_desc, diff_dst_desc, strides, dilates,
185 padding_l, padding_r, padding_kind);
186}
187
188// vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s
189