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.

tqt.cpp 3.0 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * \file dnn/test/cuda/tqt.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/common/tqt.h"
  13. #include "megdnn/oprs.h"
  14. #include "test/common/checker.h"
  15. #include "test/cuda/fixture.h"
  16. namespace megdnn {
  17. namespace test {
  18. using namespace tqt;
  19. TEST_F(CUDA, TQT) {
  20. std::vector<TestArg> args = get_args();
  21. auto dtype = dtype::Float32();
  22. for (auto&& arg : args) {
  23. auto param = arg.param;
  24. auto ishape = arg.ishape;
  25. auto scale_shape = arg.scale_shape;
  26. Checker<TQTForward> checker(handle_cuda());
  27. checker.set_param(param)
  28. .set_dtype(0, dtype)
  29. .set_dtype(1, dtype)
  30. .set_dtype(2, dtype)
  31. .execs({ishape, scale_shape, ishape});
  32. }
  33. // test noncontiguous layout
  34. for (auto&& arg : args) {
  35. auto param = arg.param;
  36. auto ishape = arg.ishape;
  37. auto sshape = arg.scale_shape;
  38. Checker<TQTForward> checker(handle_cuda());
  39. TensorLayout ilayout(
  40. ishape,
  41. {(long int)(ishape[1] * ishape[2] * ishape[3] * 2),
  42. (long int)(ishape[2] * ishape[3]), (long int)ishape[3], 1},
  43. dtype::Float32());
  44. checker.set_param(param).execl(
  45. {ilayout, {sshape, dtype::Float32()}, ilayout});
  46. }
  47. }
  48. TEST_F(CUDA, TQT_BACKWARD) {
  49. std::vector<TestArg> args = get_args();
  50. auto dtype = dtype::Float32();
  51. for (auto&& arg : args) {
  52. auto param = arg.param;
  53. auto ishape = arg.ishape;
  54. auto scale_shape = arg.scale_shape;
  55. Checker<TQTBackward> checker(handle_cuda());
  56. checker.set_param(param)
  57. .set_dtype(0, dtype)
  58. .set_dtype(1, dtype)
  59. .set_dtype(2, dtype)
  60. .set_dtype(3, dtype)
  61. .set_dtype(4, dtype)
  62. .execs({ishape, ishape, scale_shape, ishape, ishape});
  63. }
  64. // test noncontiguous layout
  65. for (auto&& arg : args) {
  66. auto param = arg.param;
  67. auto ishape = arg.ishape;
  68. auto sshape = arg.scale_shape;
  69. Checker<TQTBackward> checker(handle_cuda());
  70. TensorLayout ilayout(
  71. ishape,
  72. {(long int)(ishape[1] * ishape[2] * ishape[3] * 2),
  73. (long int)(ishape[2] * ishape[3]), (long int)ishape[3], 1},
  74. dtype::Float32());
  75. checker.set_param(param).execl({ilayout,
  76. ilayout,
  77. {sshape, dtype::Float32()},
  78. ilayout,
  79. ilayout});
  80. }
  81. }
  82. } // namespace test
  83. } // namespace megdnn

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