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.

cudnn_wrapper.h 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * \file dnn/src/cuda/cudnn_wrapper.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 <unordered_map>
  13. #include "megdnn/basic_types.h"
  14. #include "megdnn/oprs/nn.h"
  15. #include "src/cuda/cudnn_with_check.h"
  16. namespace megdnn {
  17. namespace cuda {
  18. /*!
  19. * \brief get compute_type of convolution operations
  20. */
  21. cudnnDataType_t get_compute_type_fp16(param::Convolution::ComputeMode comp_mode);
  22. class TensorDesc {
  23. public:
  24. TensorDesc();
  25. //! default layout is nchw
  26. void set(
  27. const TensorLayout& layout,
  28. const param::Convolution::Format = param::Convolution::Format::NCHW);
  29. std::string to_string();
  30. ~TensorDesc();
  31. cudnnTensorDescriptor_t desc;
  32. };
  33. template <typename Param>
  34. class FilterDesc {
  35. public:
  36. FilterDesc();
  37. void set(const typename ConvolutionBase<Param>::CanonizedFilterMeta& meta);
  38. std::string to_string();
  39. ~FilterDesc();
  40. cudnnFilterDescriptor_t desc;
  41. };
  42. class ConvDesc {
  43. public:
  44. ConvDesc();
  45. void set(DType data_type, const param::Convolution& param, const size_t nr_group);
  46. ~ConvDesc();
  47. cudnnConvolutionDescriptor_t desc;
  48. };
  49. class LRNDesc {
  50. public:
  51. LRNDesc();
  52. void set(const param::LRN& param);
  53. ~LRNDesc();
  54. cudnnLRNDescriptor_t desc;
  55. };
  56. class BNParamDesc {
  57. public:
  58. BNParamDesc();
  59. void set(const cudnnTensorDescriptor_t xDesc, cudnnBatchNormMode_t mode);
  60. ~BNParamDesc();
  61. cudnnTensorDescriptor_t desc;
  62. };
  63. // the classes below is used to deal with 3d situations
  64. class Tensor3DDesc {
  65. public:
  66. Tensor3DDesc();
  67. //! default layout is NCDHW
  68. void set(const TensorLayout& layout, bool is_ndhwc = false);
  69. ~Tensor3DDesc();
  70. cudnnTensorDescriptor_t desc;
  71. };
  72. class Filter3DDesc {
  73. public:
  74. Filter3DDesc();
  75. void set(const Convolution3DBase::CanonizedFilterMeta& meta);
  76. ~Filter3DDesc();
  77. cudnnFilterDescriptor_t desc;
  78. };
  79. class Conv3DDesc {
  80. public:
  81. Conv3DDesc();
  82. void set(const param::Convolution3D& param, const size_t nr_group);
  83. ~Conv3DDesc();
  84. cudnnConvolutionDescriptor_t desc;
  85. };
  86. class CudnnAlgoPack {
  87. public:
  88. //! algorithm attr
  89. struct Attr {
  90. std::string name;
  91. bool is_reproducible;
  92. bool accuracy_depend_on_batch;
  93. };
  94. static const std::unordered_map<cudnnConvolutionBwdDataAlgo_t, Attr>
  95. conv_bwd_data_algos();
  96. static const std::unordered_map<cudnnConvolutionBwdFilterAlgo_t, Attr>
  97. conv_bwd_flt_algos();
  98. static const std::unordered_map<cudnnConvolutionFwdAlgo_t, Attr> conv_fwd_algos();
  99. static const std::unordered_map<cudnnConvolutionBwdDataAlgo_t, Attr>
  100. conv3d_bwd_data_algos();
  101. static const std::unordered_map<cudnnConvolutionBwdFilterAlgo_t, Attr>
  102. conv3d_bwd_flt_algos();
  103. static const std::unordered_map<cudnnConvolutionFwdAlgo_t, Attr> conv3d_fwd_algos();
  104. };
  105. } // namespace cuda
  106. } // namespace megdnn
  107. namespace std {
  108. #define DEF_HASH(_type) \
  109. template <> \
  110. struct hash<_type> { \
  111. std::size_t operator()(const _type& algo) const { \
  112. return std::hash<uint32_t>()(static_cast<uint32_t>(algo)); \
  113. } \
  114. }
  115. DEF_HASH(cudnnConvolutionBwdDataAlgo_t);
  116. DEF_HASH(cudnnConvolutionBwdFilterAlgo_t);
  117. DEF_HASH(cudnnConvolutionFwdAlgo_t);
  118. #undef DEF_HASH
  119. } // namespace std
  120. // vim: syntax=cpp.doxygen