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.

adaptive_pooling.cpp 3.7 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * \file dnn/test/cuda/adaptive_pooling.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2020 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
  10. * implied.
  11. */
  12. #include "test/cuda/fixture.h"
  13. #include "megdnn/tensor_iter.h"
  14. #include "test/common/adaptive_pooling.h"
  15. #include "test/common/checker.h"
  16. #include "src/common/utils.h"
  17. #include "test/cuda/utils.h"
  18. #include <cudnn.h>
  19. #include "test/cuda/benchmark.h"
  20. namespace megdnn {
  21. namespace test {
  22. TEST_F(CUDA, ADAPTIVE_POOLING_FORWARD) {
  23. auto args = adaptive_pooling::get_args();
  24. using Format = param::AdaptivePooling::Format;
  25. DType dtype = dtype::Float32();
  26. for (auto&& arg : args) {
  27. auto param = arg.param;
  28. auto src = arg.ishape;
  29. auto dst = arg.oshape;
  30. param.format = Format::NCHW;
  31. Checker<AdaptivePooling> checker(handle_cuda());
  32. checker.set_epsilon(1e-2);
  33. checker.set_param(param).set_dtype(0, dtype).set_dtype(1, dtype).exec(
  34. TensorShapeArray{src, dst, {}});
  35. }
  36. }
  37. TEST_F(CUDA, ADAPTIVE_POOLING_BACKWARD) {
  38. auto args = adaptive_pooling::get_args();
  39. for (auto&& arg : args) {
  40. Checker<AdaptivePoolingBackward> checker(handle_cuda());
  41. TensorLayout ilayout = TensorLayout(arg.ishape, dtype::Float32());
  42. TensorLayout olayout = TensorLayout(arg.oshape, dtype::Float32());
  43. auto constraint = [this,
  44. arg](CheckerHelper::TensorValueArray& tensors_orig) {
  45. megdnn_assert(tensors_orig.size() == 4);
  46. auto opr = handle_cuda()->create_operator<AdaptivePoolingForward>();
  47. opr->param() = arg.param;
  48. auto tensors_cuda_storage = CheckerHelper::alloc_tensors(
  49. handle_cuda(),
  50. {tensors_orig[0].layout, tensors_orig[1].layout}, 0);
  51. auto&& tensors_cuda = *tensors_cuda_storage;
  52. auto span = tensors_cuda[0].layout.span();
  53. auto dst = static_cast<dt_byte*>(tensors_cuda[0].raw_ptr) +
  54. span.low_byte;
  55. auto src = static_cast<const dt_byte*>(tensors_orig[0].raw_ptr) +
  56. span.low_byte;
  57. megdnn_memcpy_H2D(handle_cuda(), dst, src, span.dist_byte());
  58. auto workspace_size = opr->get_workspace_in_bytes(
  59. tensors_cuda[0].layout, tensors_cuda[1].layout);
  60. auto workspace_cuda = megdnn_malloc(handle_cuda(), workspace_size);
  61. Workspace workspace{static_cast<dt_byte*>(workspace_cuda),
  62. workspace_size};
  63. opr->exec(tensors_cuda[0], tensors_cuda[1], workspace);
  64. megdnn_free(handle_cuda(), workspace_cuda);
  65. span = tensors_cuda[1].layout.span();
  66. dst = static_cast<dt_byte*>(tensors_orig[1].raw_ptr) +
  67. span.low_byte;
  68. src = static_cast<const dt_byte*>(tensors_cuda[1].raw_ptr) +
  69. span.low_byte;
  70. megdnn_memcpy_D2H(handle_cuda(), dst, src, span.dist_byte());
  71. };
  72. DType dtype = dtype::Float32();
  73. checker.set_tensors_constraint(constraint)
  74. .set_dtype(0, dtype)
  75. .set_dtype(1, dtype)
  76. .set_dtype(2, dtype)
  77. .set_dtype(3, dtype)
  78. .set_param(arg.param)
  79. .exec(TensorShapeArray{ilayout, olayout, olayout, ilayout});
  80. }
  81. }
  82. } // namespace test
  83. } // namespace megdnn
  84. // vim: syntax=cpp.doxygen

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