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.

helper.h 3.0 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * \file dnn/src/cuda/convolution/helper.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 "./opr_impl.h"
  13. #include "src/common/algo_chooser.h"
  14. #include "src/common/utils.h"
  15. #include "src/cuda/cudnn_wrapper.h"
  16. #include "src/cuda/handle.h"
  17. namespace megdnn {
  18. namespace cuda {
  19. namespace convolution {
  20. using CanonizedFilterMeta = ConvolutionForward::CanonizedFilterMeta;
  21. //! conv size descriptor in the forward view
  22. struct ForwardSizeArgs {
  23. HandleImpl* handle;
  24. const TensorLayout* src_layout;
  25. const TensorLayout* filter_layout;
  26. CanonizedFilterMeta filter_meta;
  27. const TensorLayout* dst_layout;
  28. };
  29. //! whether cudnn is supported for a filter meta
  30. bool is_cudnn_supported(const ForwardSizeArgs& args);
  31. //! get workspace bundle for matmul algo
  32. SmallVector<size_t> matmul_get_workspace_bundle(const ForwardSizeArgs& args);
  33. struct CUDNNForwardDescs {
  34. TensorDesc src_desc, dst_desc;
  35. FilterDesc<param::Convolution> filter_desc;
  36. ConvDesc conv_desc;
  37. void set(
  38. const TensorLayout& src, const CanonizedFilterMeta& filter,
  39. const TensorLayout& dst, const param::Convolution& param) {
  40. src_desc.set(src, param.format);
  41. filter_desc.set(filter);
  42. dst_desc.set(dst, param.format);
  43. conv_desc.set(src.dtype, param, filter.group);
  44. }
  45. };
  46. struct CUDNNBwdDataDescs {
  47. TensorDesc diff_desc, grad_desc;
  48. FilterDesc<param::Convolution> filter_desc;
  49. ConvDesc conv_desc;
  50. void set(
  51. const CanonizedFilterMeta& filter, const TensorLayout& diff,
  52. const TensorLayout& grad, const param::Convolution& param) {
  53. filter_desc.set(filter);
  54. diff_desc.set(diff, param.format);
  55. grad_desc.set(grad, param.format);
  56. conv_desc.set(filter.dtype, param, filter.group);
  57. }
  58. };
  59. struct CUDNNBwdFilterDescs {
  60. TensorDesc diff_desc, src_desc;
  61. FilterDesc<param::Convolution> grad_desc;
  62. ConvDesc conv_desc;
  63. void set(
  64. const TensorLayout& src, const TensorLayout& diff,
  65. const CanonizedFilterMeta& grad, const param::Convolution& param) {
  66. src_desc.set(src, param.format);
  67. diff_desc.set(diff, param.format);
  68. grad_desc.set(grad);
  69. conv_desc.set(src.dtype, param, grad.group);
  70. }
  71. };
  72. /*!
  73. * \brief flip conv filter
  74. *
  75. * Flip conv filter pointed by \p raw_ptr, store result in workspace, and
  76. * change \p raw_ptr to workspace.
  77. */
  78. void flip_filter(
  79. const ForwardSizeArgs& args, const Workspace& workspace, RefPtr& raw_ptr);
  80. } // namespace convolution
  81. } // namespace cuda
  82. } // namespace megdnn
  83. // vim: syntax=cpp.doxygen

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