1/*******************************************************************************
2* Copyright 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#include <assert.h>
18#include "mkldnn.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 pooling_desc_init(pooling_desc_t *pool_desc,
33 prop_kind_t prop_kind, alg_kind_t alg_kind,
34 const memory_desc_t *src_desc, const memory_desc_t *dst_desc,
35 const dims_t strides, const dims_t kernel, const dims_t padding_l,
36 const dims_t padding_r, padding_kind_t padding_kind) {
37 bool args_ok = true
38 && !any_null(pool_desc, src_desc, dst_desc, strides, kernel, padding_l)
39 && one_of(alg_kind, pooling_max,
40 pooling_avg_include_padding,
41 pooling_avg_exclude_padding)
42 && one_of(padding_kind, padding_kind::padding_zero);
43 if (!args_ok) return invalid_arguments;
44
45 if (padding_r == nullptr) padding_r = padding_l;
46
47 auto pd = pooling_desc_t();
48 pd.primitive_kind = primitive_kind::pooling;
49 pd.prop_kind = prop_kind;
50 pd.alg_kind = alg_kind;
51 pd.src_desc.ndims = src_desc->ndims;
52
53 const bool is_fwd = one_of(prop_kind, forward_training, forward_inference);
54
55 pd.diff_src_desc = pd.src_desc = zero_md();
56 pd.diff_dst_desc = pd.dst_desc = zero_md();
57
58 (is_fwd ? pd.src_desc : pd.diff_src_desc) = *src_desc;
59 (is_fwd ? pd.dst_desc : pd.diff_dst_desc) = *dst_desc;
60
61 int sp_dims = src_desc->ndims - 2;
62 utils::array_copy(pd.strides, strides, sp_dims);
63 utils::array_copy(pd.kernel, kernel, sp_dims);
64 utils::array_copy(pd.padding[0], padding_l, sp_dims);
65 utils::array_copy(pd.padding[1], padding_r, sp_dims);
66
67 pd.padding_kind = padding_kind;
68 if (one_of(alg_kind, pooling_max, pooling_avg_include_padding,
69 pooling_avg_exclude_padding)) {
70 pd.accum_data_type = types::default_accum_data_type(
71 src_desc->data_type, dst_desc->data_type);
72 } else {
73 pd.accum_data_type = dst_desc->data_type;
74 }
75
76 bool consistency = true
77 && utils::one_of(src_desc->ndims, 4, 5)
78 && utils::one_of(dst_desc->ndims, 4, 5)
79 && src_desc->dims[0] == dst_desc->dims[0]
80 && src_desc->dims[1] == dst_desc->dims[1];
81 for (int i = 2; i < src_desc->ndims; ++i)
82 consistency = consistency && (
83 (src_desc->dims[i] - kernel[i - 2] + padding_l[i - 2]
84 + padding_r[i - 2]) / strides[i - 2] + 1
85 == dst_desc->dims[i]);
86 if (!consistency) return invalid_arguments;
87
88 *pool_desc = pd;
89 return success;
90}
91}
92
93status_t mkldnn_pooling_forward_desc_init(pooling_desc_t *pool_desc,
94 prop_kind_t prop_kind, alg_kind_t alg_kind,
95 const memory_desc_t *src_desc, const memory_desc_t *dst_desc,
96 const dims_t strides, const dims_t kernel, const dims_t padding_l,
97 const dims_t padding_r, padding_kind_t padding_kind) {
98 if (!one_of(prop_kind, forward_training, forward_inference))
99 return invalid_arguments;
100 return pooling_desc_init(pool_desc, prop_kind, alg_kind, src_desc,
101 dst_desc, strides, kernel, padding_l, padding_r, padding_kind);
102}
103
104status_t mkldnn_pooling_backward_desc_init(pooling_desc_t *pool_desc,
105 alg_kind_t alg_kind, const memory_desc_t *diff_src_desc,
106 const memory_desc_t *diff_dst_desc, const dims_t strides,
107 const dims_t kernel, const dims_t padding_l, const dims_t padding_r,
108 padding_kind_t padding_kind) {
109 return pooling_desc_init(pool_desc, prop_kind::backward_data, alg_kind,
110 diff_src_desc, diff_dst_desc, strides, kernel, padding_l,
111 padding_r, padding_kind);
112}
113
114// vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s
115