weights_reorder.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. // Reorders weights from oihw to padded oihw format
  20. template<int K>
  21. class WeightsReorderNode : public Node
  22. {
  23. private:
  24. std::shared_ptr<memory> src;
  25. std::shared_ptr<memory> dst;
  26. public:
  27. WeightsReorderNode(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(memory::format_tag::oihw)));
  37. assert(memory_desc_matches_tag(dstDesc, mkldnn_format_tag_t(memory::format_tag::oihw)));
  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(getPadded<K>(srcDesc.dims[0]) == dstDesc.dims[0]); // OC
  43. assert(getPadded<K>(srcDesc.dims[1]) == dstDesc.dims[1]); // IC
  44. assert(srcDesc.dims[2] == dstDesc.dims[2]);
  45. assert(srcDesc.dims[3] == dstDesc.dims[3]);
  46. }
  47. void execute(stream& sm) override
  48. {
  49. const mkldnn_memory_desc_t& srcDesc = src->get_desc().data;
  50. const mkldnn_memory_desc_t& dstDesc = dst->get_desc().data;
  51. const float* srcPtr = (float*)src->get_data_handle();
  52. float* dstPtr = (float*)dst->get_data_handle();
  53. const int OC1 = srcDesc.dims[0];
  54. const int OC2 = dstDesc.dims[0];
  55. const int IC1 = srcDesc.dims[1];
  56. const int IC2 = dstDesc.dims[1];
  57. const int H = dstDesc.dims[2];
  58. const int W = dstDesc.dims[3];
  59. for (int oc = 0; oc < OC2; ++oc)
  60. {
  61. for (int ic = 0; ic < IC2; ++ic)
  62. {
  63. for (int h = 0; h < H; ++h)
  64. {
  65. for (int w = 0; w < W; ++w)
  66. {
  67. // Output is in oihw format
  68. float* dstPtr_c = dstPtr + oc*IC2*H*W + ic*H*W + h*W + w;
  69. if (oc < OC1 && ic < IC1)
  70. {
  71. // Input is in oihw format
  72. const float* srcPtr_c = srcPtr + oc*IC1*H*W + ic*H*W + h*W + w;
  73. *dstPtr_c = *srcPtr_c;
  74. }
  75. else
  76. {
  77. // padding
  78. *dstPtr_c = 0;
  79. }
  80. }
  81. }
  82. }
  83. }
  84. }
  85. std::shared_ptr<memory> getDst() const override { return dst; }
  86. };
  87. } // namespace oidn