upsample.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #pragma once
  17. #include "node.h"
  18. namespace oidn {
  19. // 2x2 nearest-neighbor upsampling node
  20. template<int K>
  21. class UpsampleNode : public Node
  22. {
  23. private:
  24. std::shared_ptr<memory> src;
  25. std::shared_ptr<memory> dst;
  26. public:
  27. UpsampleNode(const std::shared_ptr<memory>& src,
  28. const std::shared_ptr<memory>& dst)
  29. : src(src),
  30. dst(dst)
  31. {
  32. const mkldnn_memory_desc_t& srcDesc = src->get_desc().data;
  33. const mkldnn_memory_desc_t& dstDesc = dst->get_desc().data;
  34. MAYBE_UNUSED(srcDesc);
  35. MAYBE_UNUSED(dstDesc);
  36. assert(memory_desc_matches_tag(srcDesc, mkldnn_format_tag_t(BlockedFormat<K>::nChwKc)));
  37. assert(memory_desc_matches_tag(dstDesc, mkldnn_format_tag_t(BlockedFormat<K>::nChwKc)));
  38. assert(srcDesc.ndims == 4);
  39. assert(dstDesc.ndims == 4);
  40. assert(srcDesc.data_type == memory::data_type::f32);
  41. assert(dstDesc.data_type == memory::data_type::f32);
  42. assert(srcDesc.dims[0] == 1);
  43. assert(dstDesc.dims[0] == 1);
  44. // 2x2 upsampling
  45. assert(dstDesc.dims[2] == srcDesc.dims[2] * 2);
  46. assert(dstDesc.dims[3] == srcDesc.dims[3] * 2);
  47. }
  48. void execute(stream& sm) override
  49. {
  50. const mkldnn_memory_desc_t& srcDesc = src->get_desc().data;
  51. const float* srcPtr = (float*)src->get_data_handle();
  52. float* dstPtr = (float*)dst->get_data_handle();
  53. const int C = srcDesc.dims[1];
  54. const int H = srcDesc.dims[2];
  55. const int W = srcDesc.dims[3];
  56. const int CK = C / K;
  57. parallel_nd(CK, H, [&](int ck, int h)
  58. {
  59. const size_t offset = ck*H*W*K + h*W*K;
  60. const float* srcPtr_line = srcPtr + offset;
  61. float* dstPtr_line0 = dstPtr + offset * 4;
  62. float* dstPtr_line1 = dstPtr_line0 + W*2*K; // next line
  63. for (int w = 0; w < W; ++w)
  64. {
  65. #pragma unroll
  66. for (int k = 0; k < K; k += 4)
  67. {
  68. const __m128 m = _mm_load_ps(&srcPtr_line[w*K + k]);
  69. _mm_stream_ps(&dstPtr_line0[w*2*K + k], m);
  70. _mm_stream_ps(&dstPtr_line0[w*2*K+K + k], m);
  71. _mm_stream_ps(&dstPtr_line1[w*2*K + k], m);
  72. _mm_stream_ps(&dstPtr_line1[w*2*K+K + k], m);
  73. }
  74. }
  75. });
  76. }
  77. std::shared_ptr<memory> getDst() const override { return dst; }
  78. };
  79. } // namespace oidn