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.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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-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 implied.
  10. */
  11. #include "test/cuda/fixture.h"
  12. #include "megdnn/opr_param_defs.h"
  13. #include "megdnn/oprs.h"
  14. #include "test/common/bn.h"
  15. #include "test/common/checker.h"
  16. #include "test/common/rng.h"
  17. #include "test/common/tensor.h"
  18. #include "test/common/workspace_wrapper.h"
  19. namespace megdnn {
  20. namespace test {
  21. TEST_F(CUDA, BN_FORWARD) {
  22. using namespace batch_normalization;
  23. std::vector<TestArg> args = get_args();
  24. Checker<BNForward> checker(handle_cuda());
  25. for (auto&& arg : args) {
  26. for (int i = 0; i < 8; ++i) {
  27. checker.set_dtype(i, dtype::Float32());
  28. }
  29. checker.set_dtype(0, arg.dtype);
  30. checker.set_epsilon(1e-3).set_param(arg.param);
  31. for (bool need_statistic : {false, true})
  32. checker.exec({
  33. arg.src,
  34. arg.param_shape, // bn_scale
  35. arg.param_shape, // bn_bias
  36. need_statistic ? arg.param_shape
  37. : TensorShape({0}), // mean
  38. need_statistic ? arg.param_shape
  39. : TensorShape({0}), // variance
  40. arg.param_shape, // batch_mean
  41. arg.param_shape, // batch_inv_variance
  42. {} // dst
  43. });
  44. }
  45. }
  46. TEST_F(CUDA, BN_BACKWARD) {
  47. using namespace batch_normalization;
  48. std::vector<TestArg> args = get_args();
  49. Checker<BNBackward> checker(handle_cuda());
  50. for (auto&& arg : args) {
  51. for (int i = 0; i < 8; ++i) {
  52. checker.set_dtype(i, dtype::Float32());
  53. }
  54. checker.set_dtype(0, arg.dtype) // x
  55. .set_dtype(1, arg.dtype) // dy
  56. .set_dtype(7, arg.dtype); // dx
  57. checker.set_epsilon(1e-3).set_param(arg.param).exec(
  58. {arg.src, arg.src, arg.param_shape, arg.param_shape,
  59. arg.param_shape, arg.param_shape, arg.param_shape, arg.src});
  60. }
  61. }
  62. } // namespace test
  63. } // namespace megdnn
  64. // vim: syntax=cpp.doxygen

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