node.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 "common.h"
  18. #include <vector>
  19. namespace oidn {
  20. class Node
  21. {
  22. public:
  23. virtual ~Node() = default;
  24. virtual void execute(stream& sm) = 0;
  25. virtual std::shared_ptr<memory> getDst() const { return nullptr; }
  26. virtual size_t getScratchpadSize() const { return 0; }
  27. virtual void setScratchpad(const std::shared_ptr<memory>& mem) {}
  28. virtual void setTile(int h1, int w1, int h2, int w2, int H, int W)
  29. {
  30. assert(0); // not supported
  31. }
  32. };
  33. // Node wrapping an MKL-DNN primitive
  34. class MklNode : public Node
  35. {
  36. private:
  37. primitive prim;
  38. std::unordered_map<int, memory> args;
  39. std::shared_ptr<memory> scratchpad;
  40. public:
  41. MklNode(const primitive& prim, const std::unordered_map<int, memory>& args)
  42. : prim(prim),
  43. args(args)
  44. {}
  45. size_t getScratchpadSize() const override
  46. {
  47. const auto primDesc = prim.get_primitive_desc();
  48. const mkldnn_memory_desc_t* scratchpadDesc = mkldnn_primitive_desc_query_md(primDesc, mkldnn_query_scratchpad_md, 0);
  49. if (scratchpadDesc == nullptr)
  50. return 0;
  51. return mkldnn_memory_desc_get_size(scratchpadDesc);
  52. }
  53. void setScratchpad(const std::shared_ptr<memory>& mem) override
  54. {
  55. scratchpad = mem;
  56. args.insert(std::make_pair(MKLDNN_ARG_SCRATCHPAD, *scratchpad));
  57. }
  58. void execute(stream& sm) override
  59. {
  60. prim.execute(sm, args);
  61. }
  62. };
  63. // Convolution node
  64. class ConvNode : public MklNode
  65. {
  66. private:
  67. std::shared_ptr<memory> src;
  68. std::shared_ptr<memory> weights;
  69. std::shared_ptr<memory> bias;
  70. std::shared_ptr<memory> dst;
  71. public:
  72. ConvNode(const convolution_forward::primitive_desc& desc,
  73. const std::shared_ptr<memory>& src,
  74. const std::shared_ptr<memory>& weights,
  75. const std::shared_ptr<memory>& bias,
  76. const std::shared_ptr<memory>& dst)
  77. : MklNode(convolution_forward(desc),
  78. { { MKLDNN_ARG_SRC, *src },
  79. { MKLDNN_ARG_WEIGHTS, *weights },
  80. { MKLDNN_ARG_BIAS, *bias },
  81. { MKLDNN_ARG_DST, *dst } }),
  82. src(src), weights(weights), bias(bias), dst(dst)
  83. {}
  84. std::shared_ptr<memory> getDst() const override { return dst; }
  85. };
  86. // Pooling node
  87. class PoolNode : public MklNode
  88. {
  89. private:
  90. std::shared_ptr<memory> src;
  91. std::shared_ptr<memory> dst;
  92. public:
  93. PoolNode(const pooling_forward::primitive_desc& desc,
  94. const std::shared_ptr<memory>& src,
  95. const std::shared_ptr<memory>& dst)
  96. : MklNode(pooling_forward(desc),
  97. { { MKLDNN_ARG_SRC, *src },
  98. { MKLDNN_ARG_DST, *dst } }),
  99. src(src), dst(dst)
  100. {}
  101. std::shared_ptr<memory> getDst() const override { return dst; }
  102. };
  103. // Reorder node
  104. class ReorderNode : public MklNode
  105. {
  106. private:
  107. std::shared_ptr<memory> src;
  108. std::shared_ptr<memory> dst;
  109. public:
  110. ReorderNode(const std::shared_ptr<memory>& src,
  111. const std::shared_ptr<memory>& dst)
  112. : MklNode(reorder(reorder::primitive_desc(*src, *dst)),
  113. { { MKLDNN_ARG_SRC, *src },
  114. { MKLDNN_ARG_DST, *dst } }),
  115. src(src), dst(dst)
  116. {}
  117. std::shared_ptr<memory> getDst() const override { return dst; }
  118. };
  119. } // namespace oidn