| 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 | #ifndef SHUFFLE_PD_HPP |
| 18 | #define SHUFFLE_PD_HPP |
| 19 | |
| 20 | #include "mkldnn.h" |
| 21 | |
| 22 | #include "c_types_map.hpp" |
| 23 | #include "primitive_desc.hpp" |
| 24 | |
| 25 | namespace mkldnn { |
| 26 | namespace impl { |
| 27 | |
| 28 | struct shuffle_pd_t: public primitive_desc_t { |
| 29 | static constexpr auto base_pkind = primitive_kind::shuffle; |
| 30 | |
| 31 | typedef shuffle_pd_t base_class; |
| 32 | typedef shuffle_pd_t hint_class; |
| 33 | |
| 34 | shuffle_pd_t(engine_t *engine, |
| 35 | const shuffle_desc_t *adesc, |
| 36 | const primitive_attr_t *attr, |
| 37 | const shuffle_pd_t *hint_fwd_pd) |
| 38 | : primitive_desc_t(engine, attr, base_pkind) |
| 39 | , desc_(*adesc) |
| 40 | , hint_fwd_pd_(hint_fwd_pd) |
| 41 | , data_md_(desc_.data_desc) |
| 42 | {} |
| 43 | |
| 44 | const shuffle_desc_t *desc() const { return &desc_; } |
| 45 | virtual const op_desc_t *op_desc() const override |
| 46 | { return reinterpret_cast<const op_desc_t *>(this->desc()); } |
| 47 | virtual void init_info() override { impl::init_info(this, this->info_); } |
| 48 | |
| 49 | virtual status_t query(query_t what, int idx, void *result) const override { |
| 50 | switch (what) { |
| 51 | case query::shuffle_d: |
| 52 | *(const shuffle_desc_t**)result = desc(); break; |
| 53 | default: return primitive_desc_t::query(what, idx, result); |
| 54 | } |
| 55 | return status::success; |
| 56 | } |
| 57 | |
| 58 | virtual arg_usage_t arg_usage(primitive_arg_index_t arg) const override { |
| 59 | if (is_fwd()) { |
| 60 | if (arg == MKLDNN_ARG_SRC) |
| 61 | return arg_usage_t::input; |
| 62 | |
| 63 | if (arg == MKLDNN_ARG_DST) |
| 64 | return arg_usage_t::output; |
| 65 | } else { |
| 66 | if (arg == MKLDNN_ARG_DIFF_DST) |
| 67 | return arg_usage_t::input; |
| 68 | |
| 69 | if (arg == MKLDNN_ARG_DIFF_SRC) |
| 70 | return arg_usage_t::output; |
| 71 | } |
| 72 | |
| 73 | return primitive_desc_t::arg_usage(arg); |
| 74 | } |
| 75 | |
| 76 | virtual const memory_desc_t *src_md(int index = 0) const override |
| 77 | { return index == 0 && is_fwd() ? &data_md_ : nullptr; } |
| 78 | virtual const memory_desc_t *dst_md(int index = 0) const override |
| 79 | { return index == 0 && is_fwd() ? &data_md_ : nullptr; } |
| 80 | |
| 81 | virtual const memory_desc_t *diff_src_md(int index = 0) const override |
| 82 | { return index == 0 && !is_fwd() ? &data_md_ : nullptr; } |
| 83 | virtual const memory_desc_t *diff_dst_md(int index = 0) const override |
| 84 | { return index == 0 && !is_fwd() ? &data_md_ : nullptr; } |
| 85 | |
| 86 | virtual int n_inputs() const override { return 1; } |
| 87 | virtual int n_outputs() const override { return 1; } |
| 88 | |
| 89 | /* shuffle aux functions */ |
| 90 | |
| 91 | dim_t MB() const { return data_md()->dims[0]; } |
| 92 | dim_t C() const { return ndims() >= 2 ? data_md()->dims[1] : 1; } |
| 93 | dim_t D() const { return ndims() >= 5 ? data_md()->dims[ndims() - 3] : 1; } |
| 94 | dim_t H() const { return ndims() >= 4 ? data_md()->dims[ndims() - 2] : 1; } |
| 95 | dim_t W() const { return ndims() >= 3 ? data_md()->dims[ndims() - 1] : 1; } |
| 96 | |
| 97 | int ndims() const { return data_md()->ndims; } |
| 98 | |
| 99 | int axis() const { return desc_.axis; } |
| 100 | dim_t group_size() const { return desc_.group_size; } |
| 101 | dim_t axis_size() const { return data_md()->dims[axis()]; } |
| 102 | |
| 103 | bool is_fwd() const { |
| 104 | return utils::one_of(desc_.prop_kind, prop_kind::forward_training, |
| 105 | prop_kind::forward_inference); |
| 106 | } |
| 107 | |
| 108 | const memory_desc_t *data_md() const { return &data_md_; } |
| 109 | |
| 110 | protected: |
| 111 | shuffle_desc_t desc_; |
| 112 | const shuffle_pd_t *hint_fwd_pd_; |
| 113 | memory_desc_t data_md_; |
| 114 | }; |
| 115 | |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | #endif |
| 120 | |
| 121 | // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s |
| 122 | |