| 1 | // ======================================================================== // |
| 2 | // Copyright 2009-2019 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 | #pragma once |
| 18 | |
| 19 | #include "node.h" |
| 20 | |
| 21 | namespace oidn { |
| 22 | |
| 23 | // Reorders weights from oihw to padded oihw format |
| 24 | template<int K> |
| 25 | class WeightsReorderNode : public Node |
| 26 | { |
| 27 | private: |
| 28 | std::shared_ptr<memory> src; |
| 29 | std::shared_ptr<memory> dst; |
| 30 | |
| 31 | public: |
| 32 | WeightsReorderNode(const std::shared_ptr<memory>& src, |
| 33 | const std::shared_ptr<memory>& dst) |
| 34 | : src(src), |
| 35 | dst(dst) |
| 36 | { |
| 37 | const mkldnn_memory_desc_t& srcDesc = src->get_desc().data; |
| 38 | const mkldnn_memory_desc_t& dstDesc = dst->get_desc().data; |
| 39 | MAYBE_UNUSED(srcDesc); |
| 40 | MAYBE_UNUSED(dstDesc); |
| 41 | assert(memory_desc_matches_tag(srcDesc, mkldnn_format_tag_t(memory::format_tag::oihw))); |
| 42 | assert(memory_desc_matches_tag(dstDesc, mkldnn_format_tag_t(memory::format_tag::oihw))); |
| 43 | assert(srcDesc.ndims == 4); |
| 44 | assert(dstDesc.ndims == 4); |
| 45 | assert(srcDesc.data_type == memory::data_type::f32); |
| 46 | assert(dstDesc.data_type == memory::data_type::f32); |
| 47 | assert(getPadded<K>(srcDesc.dims[0]) == dstDesc.dims[0]); // OC |
| 48 | assert(getPadded<K>(srcDesc.dims[1]) == dstDesc.dims[1]); // IC |
| 49 | assert(srcDesc.dims[2] == dstDesc.dims[2]); |
| 50 | assert(srcDesc.dims[3] == dstDesc.dims[3]); |
| 51 | } |
| 52 | |
| 53 | void execute(stream& sm) override |
| 54 | { |
| 55 | const mkldnn_memory_desc_t& srcDesc = src->get_desc().data; |
| 56 | const mkldnn_memory_desc_t& dstDesc = dst->get_desc().data; |
| 57 | |
| 58 | const float* srcPtr = (float*)src->get_data_handle(); |
| 59 | float* dstPtr = (float*)dst->get_data_handle(); |
| 60 | |
| 61 | const int OC1 = srcDesc.dims[0]; |
| 62 | const int OC2 = dstDesc.dims[0]; |
| 63 | const int IC1 = srcDesc.dims[1]; |
| 64 | const int IC2 = dstDesc.dims[1]; |
| 65 | const int H = dstDesc.dims[2]; |
| 66 | const int W = dstDesc.dims[3]; |
| 67 | |
| 68 | for (int oc = 0; oc < OC2; ++oc) |
| 69 | { |
| 70 | for (int ic = 0; ic < IC2; ++ic) |
| 71 | { |
| 72 | for (int h = 0; h < H; ++h) |
| 73 | { |
| 74 | for (int w = 0; w < W; ++w) |
| 75 | { |
| 76 | // Output is in oihw format |
| 77 | float* dstPtr_c = dstPtr + oc*IC2*H*W + ic*H*W + h*W + w; |
| 78 | |
| 79 | if (oc < OC1 && ic < IC1) |
| 80 | { |
| 81 | // Input is in oihw format |
| 82 | const float* srcPtr_c = srcPtr + oc*IC1*H*W + ic*H*W + h*W + w; |
| 83 | *dstPtr_c = *srcPtr_c; |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | // padding |
| 88 | *dstPtr_c = 0; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | std::shared_ptr<memory> getDst() const override { return dst; } |
| 97 | }; |
| 98 | |
| 99 | } // namespace oidn |
| 100 | |