You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

indexing_multi_axis_vec.h 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * \file dnn/test/common/indexing_multi_axis_vec.h
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #pragma once
  12. #include "test/common/opr_proxy.h"
  13. namespace megdnn {
  14. namespace test {
  15. struct OprProxyIndexingMultiAxisVecHelper {
  16. size_t axes[TensorLayout::MAX_NDIM];
  17. /*!
  18. * \brief OprProxy for indexing multi-vec family oprs
  19. *
  20. * \param init_axes axes that are indexed
  21. */
  22. OprProxyIndexingMultiAxisVecHelper(
  23. std::initializer_list<size_t> init_axes = {}) {
  24. size_t i = 0;
  25. for (auto ax : init_axes)
  26. axes[i++] = ax;
  27. }
  28. OprProxyIndexingMultiAxisVecHelper(SmallVector<size_t> init_axes) {
  29. size_t i = 0;
  30. for (auto ax : init_axes)
  31. axes[i++] = ax;
  32. }
  33. IndexingMultiAxisVec::IndexDesc make_index_desc(
  34. const TensorNDArray& tensors) const {
  35. megdnn_assert(tensors.size() >= 3);
  36. IndexingMultiAxisVec::IndexDesc ret;
  37. ret.resize(tensors.size() - 2);
  38. for (size_t i = 2; i < tensors.size(); ++i) {
  39. ret[i - 2] = {axes[i - 2], tensors[i]};
  40. }
  41. return ret;
  42. }
  43. IndexingMultiAxisVec::IndexDescLayoutOnly make_index_layout(
  44. const TensorLayoutArray& layouts) const {
  45. megdnn_assert(layouts.size() >= 3);
  46. IndexingMultiAxisVec::IndexDescLayoutOnly ret;
  47. ret.resize(layouts.size() - 2);
  48. for (size_t i = 2; i < layouts.size(); ++i) {
  49. ret[i - 2] = {axes[i - 2], layouts[i]};
  50. }
  51. return ret;
  52. }
  53. };
  54. template <>
  55. struct OprProxy<IndexingMultiAxisVec>
  56. : public OprProxyIndexingMultiAxisVecHelper {
  57. using OprProxyIndexingMultiAxisVecHelper::
  58. OprProxyIndexingMultiAxisVecHelper;
  59. void exec(IndexingMultiAxisVec* opr, const TensorNDArray& tensors) const {
  60. WorkspaceWrapper W(opr->handle(),
  61. opr->get_workspace_in_bytes(tensors[1].layout, axes,
  62. tensors.size() - 2));
  63. opr->exec(tensors[0], make_index_desc(tensors), tensors[1],
  64. W.workspace());
  65. }
  66. void deduce_layout(IndexingMultiAxisVec* opr, TensorLayoutArray& layouts) {
  67. opr->deduce_layout(layouts[0], make_index_layout(layouts), layouts[1]);
  68. }
  69. };
  70. template <>
  71. struct OprProxy<IndexingIncrMultiAxisVec>
  72. : public OprProxyIndexingMultiAxisVecHelper {
  73. using OprProxyIndexingMultiAxisVecHelper::
  74. OprProxyIndexingMultiAxisVecHelper;
  75. void exec(IndexingIncrMultiAxisVec* opr,
  76. const TensorNDArray& tensors) const {
  77. WorkspaceWrapper W(opr->handle(),
  78. opr->get_workspace_in_bytes(tensors[1].layout, axes,
  79. tensors.size() - 2));
  80. opr->exec(tensors[0], tensors[1], make_index_desc(tensors),
  81. W.workspace());
  82. }
  83. void deduce_layout(IndexingIncrMultiAxisVec*, TensorLayoutArray&) {}
  84. };
  85. template <>
  86. struct OprProxy<IndexingSetMultiAxisVec>
  87. : public OprProxyIndexingMultiAxisVecHelper {
  88. using OprProxyIndexingMultiAxisVecHelper::
  89. OprProxyIndexingMultiAxisVecHelper;
  90. void exec(IndexingSetMultiAxisVec* opr,
  91. const TensorNDArray& tensors) const {
  92. WorkspaceWrapper W(opr->handle(),
  93. opr->get_workspace_in_bytes(tensors[1].layout, axes,
  94. tensors.size() - 2));
  95. opr->exec(tensors[0], tensors[1], make_index_desc(tensors),
  96. W.workspace());
  97. }
  98. void deduce_layout(IndexingSetMultiAxisVec*, TensorLayoutArray&) {}
  99. };
  100. } // namespace test
  101. } // namespace megdnn
  102. // vim: syntax=cpp.doxygen

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台