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 <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 shuffle_desc_init(shuffle_desc_t *shuffle_desc, prop_kind_t prop_kind, |
32 | const memory_desc_t *data_desc, int axis, dim_t group_size) { |
33 | bool args_ok = true |
34 | && !any_null(shuffle_desc, data_desc) |
35 | && one_of(prop_kind, forward_training, forward_inference, |
36 | backward, backward_data) |
37 | && axis >= 0 && axis < data_desc->ndims |
38 | && group_size > 0 && group_size <= data_desc->dims[axis]; |
39 | if (!args_ok) return invalid_arguments; |
40 | |
41 | auto sd = shuffle_desc_t(); |
42 | sd.primitive_kind = primitive_kind::shuffle; |
43 | sd.prop_kind = prop_kind; |
44 | sd.data_desc = *data_desc; |
45 | sd.axis = axis; |
46 | sd.group_size = group_size; |
47 | |
48 | bool consistency = true |
49 | && sd.data_desc.dims[axis] % sd.group_size == 0; |
50 | if (!consistency) return invalid_arguments; |
51 | |
52 | *shuffle_desc = sd; |
53 | return success; |
54 | } |
55 | } |
56 | |
57 | status_t mkldnn_shuffle_forward_desc_init(shuffle_desc_t *shuffle_desc, |
58 | prop_kind_t prop_kind, const memory_desc_t *data_desc, int axis, |
59 | dim_t group_size) { |
60 | if (!one_of(prop_kind, forward_training, forward_inference)) |
61 | return invalid_arguments; |
62 | return shuffle_desc_init(shuffle_desc, prop_kind, data_desc, axis, |
63 | group_size); |
64 | } |
65 | |
66 | status_t mkldnn_shuffle_backward_desc_init(shuffle_desc_t *shuffle_desc, |
67 | const memory_desc_t *diff_data_desc, int axis, dim_t group_size) { |
68 | return shuffle_desc_init(shuffle_desc, backward_data, diff_data_desc, axis, |
69 | group_size); |
70 | } |
71 | |
72 | // vim: et ts=5 sw=4 cindent cino^=l0,\:0,N-s |
73 | |