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 | |
24 | using namespace mkldnn::impl; |
25 | using namespace mkldnn::impl::utils; |
26 | using namespace mkldnn::impl::status; |
27 | using namespace mkldnn::impl::prop_kind; |
28 | using namespace mkldnn::impl::types; |
29 | |
30 | namespace { |
31 | status_t ip_desc_init(inner_product_desc_t *ip_desc, prop_kind_t prop_kind, |
32 | const memory_desc_t *src_desc, const memory_desc_t *weights_desc, |
33 | const memory_desc_t *bias_desc, const memory_desc_t *dst_desc) { |
34 | bool args_ok = !any_null(ip_desc, src_desc, weights_desc, dst_desc); |
35 | if (!args_ok) return invalid_arguments; |
36 | |
37 | auto id = inner_product_desc_t(); |
38 | id.primitive_kind = primitive_kind::inner_product; |
39 | id.prop_kind = prop_kind; |
40 | |
41 | id.diff_src_desc = id.src_desc = zero_md(); |
42 | id.diff_dst_desc = id.dst_desc = zero_md(); |
43 | id.diff_weights_desc = id.weights_desc = zero_md(); |
44 | id.diff_bias_desc = id.bias_desc = zero_md(); |
45 | |
46 | const bool is_fwd = one_of(prop_kind, forward_training, forward_inference); |
47 | const bool with_bias = |
48 | bias_desc && bias_desc->format_kind != format_kind::undef; |
49 | |
50 | (prop_kind == backward_data ? id.diff_src_desc : id.src_desc) = *src_desc; |
51 | (is_fwd ? id.dst_desc : id.diff_dst_desc) = *dst_desc; |
52 | (prop_kind == backward_weights ? id.diff_weights_desc : id.weights_desc) = |
53 | *weights_desc; |
54 | if (with_bias) |
55 | (prop_kind == backward_weights ? id.diff_bias_desc : id.bias_desc) = |
56 | *bias_desc; |
57 | |
58 | id.accum_data_type = types::default_accum_data_type(src_desc->data_type, |
59 | weights_desc->data_type, dst_desc->data_type, prop_kind); |
60 | |
61 | bool consistency = true |
62 | && memory_desc_wrapper(weights_desc).nelems() |
63 | && one_of(src_desc->ndims, 2, 3, 4, 5) |
64 | && dst_desc->ndims == 2 |
65 | && weights_desc->ndims == src_desc->ndims |
66 | && (with_bias ? bias_desc->ndims == 1 : true) |
67 | && (with_bias ? bias_desc->dims[0] == dst_desc->dims[1] : true) |
68 | && src_desc->dims[0] == dst_desc->dims[0] |
69 | && array_cmp(&src_desc->dims[1], &weights_desc->dims[1], |
70 | src_desc->ndims - 1) |
71 | && dst_desc->dims[1] == weights_desc->dims[0]; |
72 | if (!consistency) return invalid_arguments; |
73 | |
74 | *ip_desc = id; |
75 | return success; |
76 | } |
77 | } |
78 | |
79 | status_t mkldnn_inner_product_forward_desc_init(inner_product_desc_t *ip_desc, |
80 | prop_kind_t prop_kind, const memory_desc_t *src_desc, |
81 | const memory_desc_t *weights_desc, const memory_desc_t *bias_desc, |
82 | const memory_desc_t *dst_desc) { |
83 | if (!one_of(prop_kind, forward_training, forward_inference)) |
84 | return invalid_arguments; |
85 | return ip_desc_init(ip_desc, prop_kind, src_desc, weights_desc, bias_desc, |
86 | dst_desc); |
87 | } |
88 | |
89 | status_t mkldnn_inner_product_backward_data_desc_init( |
90 | inner_product_desc_t *ip_desc, const memory_desc_t *diff_src_desc, |
91 | const memory_desc_t *weights_desc, const memory_desc_t *diff_dst_desc) |
92 | { |
93 | return ip_desc_init(ip_desc, backward_data, diff_src_desc, weights_desc, |
94 | nullptr, diff_dst_desc); |
95 | } |
96 | |
97 | status_t mkldnn_inner_product_backward_weights_desc_init( |
98 | inner_product_desc_t *ip_desc, const memory_desc_t *src_desc, |
99 | const memory_desc_t *diff_weights_desc, |
100 | const memory_desc_t *diff_bias_desc, |
101 | const memory_desc_t *diff_dst_desc) { |
102 | return ip_desc_init(ip_desc, backward_weights, src_desc, diff_weights_desc, |
103 | diff_bias_desc, diff_dst_desc); |
104 | } |
105 | |
106 | // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s |
107 | |