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.

bn.cpp 3.2 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * \file dnn/test/cuda/bn.cpp
  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
  10. * implied.
  11. */
  12. #include "test/cuda/fixture.h"
  13. #include "src/cuda/batch_normalization/opr_impl.h"
  14. #include "src/cuda/utils.h"
  15. #include "megdnn/opr_param_defs.h"
  16. #include "megdnn/oprs.h"
  17. #include "test/common/bn.h"
  18. #include "test/common/checker.h"
  19. #include "test/common/rng.h"
  20. #include "test/common/tensor.h"
  21. #include "test/common/workspace_wrapper.h"
  22. namespace megdnn {
  23. namespace test {
  24. TEST_F(CUDA, BN_FORWARD_BACKWARD) {
  25. using namespace batch_normalization;
  26. using cuda::cudnn_handle;
  27. using cuda::batch_normalization::BNTensorDescHolder;
  28. using cuda::batch_normalization::get_reserve_size;
  29. std::vector<TestArg> args = get_args();
  30. Checker<BNForward> checker(handle_cuda());
  31. Checker<BNBackward> checker_bwd(handle_cuda());
  32. for (auto&& arg : args) {
  33. auto tensor_desc = BNTensorDescHolder({arg.src, arg.dtype}, arg.param.param_dim,
  34. arg.param.fwd_mode);
  35. auto reserve = get_reserve_size(cudnn_handle(handle_cuda()), tensor_desc);
  36. // Forward
  37. for (int i = 0; i < 9; ++i) {
  38. checker.set_dtype(i, dtype::Float32());
  39. }
  40. checker.set_dtype(0, arg.dtype);
  41. checker.set_dtype(7, dtype::Byte());
  42. checker.set_dtype(8, arg.dtype);
  43. checker.set_bypass(7);
  44. checker.set_epsilon(1e-3).set_param(arg.param);
  45. for (bool need_statistic : {false, true})
  46. checker.exec({
  47. arg.src,
  48. arg.param_shape, // bn_scale
  49. arg.param_shape, // bn_bias
  50. need_statistic ? arg.param_shape
  51. : TensorShape({0}), // mean
  52. need_statistic ? arg.param_shape
  53. : TensorShape({0}), // variance
  54. arg.param_shape, // batch_mean
  55. arg.param_shape, // batch_inv_variance
  56. {reserve}, // reserve
  57. arg.src // dst
  58. });
  59. // Backward
  60. for (int i = 0; i < 9; ++i) {
  61. checker_bwd.set_dtype(i, dtype::Float32());
  62. }
  63. checker_bwd
  64. .set_dtype(0, arg.dtype) // x
  65. .set_dtype(1, arg.dtype) // dy
  66. .set_dtype(5, dtype::Byte()) // reserve
  67. .set_dtype(8, arg.dtype) // dx
  68. .set_bypass(5);
  69. checker_bwd.set_epsilon(1e-3).set_param(arg.param).exec(
  70. {arg.src, arg.src, arg.param_shape, arg.param_shape,
  71. arg.param_shape, {reserve}, arg.param_shape, arg.param_shape,
  72. arg.src});
  73. }
  74. }
  75. } // namespace test
  76. } // namespace megdnn
  77. // vim: syntax=cpp.doxygen

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