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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 "megdnn/opr_param_defs.h"
  14. #include "megdnn/oprs.h"
  15. #include "src/cuda/batch_normalization/opr_impl.h"
  16. #include "src/cuda/utils.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 = batch_normalization::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(
  34. {arg.src, arg.dtype}, arg.param.param_dim, 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 : TensorShape({0}), // mean
  51. need_statistic ? arg.param_shape : TensorShape({0}), // variance
  52. arg.param_shape, // batch_mean
  53. arg.param_shape, // batch_inv_variance
  54. {reserve}, // reserve
  55. arg.src // dst
  56. });
  57. // Backward
  58. for (int i = 0; i < 9; ++i) {
  59. checker_bwd.set_dtype(i, dtype::Float32());
  60. }
  61. checker_bwd
  62. .set_dtype(0, arg.dtype) // x
  63. .set_dtype(1, arg.dtype) // dy
  64. .set_dtype(5, dtype::Byte()) // reserve
  65. .set_dtype(8, arg.dtype) // dx
  66. .set_bypass(5);
  67. checker_bwd.set_epsilon(1e-3).set_param(arg.param).exec(
  68. {arg.src,
  69. arg.src,
  70. arg.param_shape,
  71. arg.param_shape,
  72. arg.param_shape,
  73. {reserve},
  74. arg.param_shape,
  75. arg.param_shape,
  76. arg.src});
  77. }
  78. }
  79. } // namespace test
  80. } // namespace megdnn
  81. // vim: syntax=cpp.doxygen