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 | // 2x2 nearest-neighbor upsampling node |
24 | template<int K> |
25 | class UpsampleNode : public Node |
26 | { |
27 | private: |
28 | std::shared_ptr<memory> src; |
29 | std::shared_ptr<memory> dst; |
30 | |
31 | public: |
32 | UpsampleNode(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(BlockedFormat<K>::nChwKc))); |
42 | assert(memory_desc_matches_tag(dstDesc, mkldnn_format_tag_t(BlockedFormat<K>::nChwKc))); |
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(srcDesc.dims[0] == 1); |
48 | assert(dstDesc.dims[0] == 1); |
49 | // 2x2 upsampling |
50 | assert(dstDesc.dims[2] == srcDesc.dims[2] * 2); |
51 | assert(dstDesc.dims[3] == srcDesc.dims[3] * 2); |
52 | } |
53 | |
54 | void execute(stream& sm) override |
55 | { |
56 | const mkldnn_memory_desc_t& srcDesc = src->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 C = srcDesc.dims[1]; |
62 | const int H = srcDesc.dims[2]; |
63 | const int W = srcDesc.dims[3]; |
64 | const int CK = C / K; |
65 | |
66 | parallel_nd(CK, H, [&](int ck, int h) |
67 | { |
68 | const size_t offset = ck*H*W*K + h*W*K; |
69 | const float* srcPtr_line = srcPtr + offset; |
70 | float* dstPtr_line0 = dstPtr + offset * 4; |
71 | float* dstPtr_line1 = dstPtr_line0 + W*2*K; // next line |
72 | |
73 | for (int w = 0; w < W; ++w) |
74 | { |
75 | #pragma unroll |
76 | for (int k = 0; k < K; k += 4) |
77 | { |
78 | const __m128 m = _mm_load_ps(&srcPtr_line[w*K + k]); |
79 | |
80 | _mm_stream_ps(&dstPtr_line0[w*2*K + k], m); |
81 | _mm_stream_ps(&dstPtr_line0[w*2*K+K + k], m); |
82 | _mm_stream_ps(&dstPtr_line1[w*2*K + k], m); |
83 | _mm_stream_ps(&dstPtr_line1[w*2*K+K + k], m); |
84 | } |
85 | } |
86 | }); |
87 | } |
88 | |
89 | std::shared_ptr<memory> getDst() const override { return dst; } |
90 | }; |
91 | |
92 | } // namespace oidn |
93 | |