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.

fake_quant.cpp 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * \file dnn/test/cuda/fake_quant.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/fake_quant.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 fake_quant;
  19. TEST_F(CUDA, FAKE_QUANT) {
  20. std::vector<TestArg> args = get_args();
  21. auto dtype = dtype::Float32();
  22. std::unique_ptr<RNG> rng;
  23. for (auto&& arg : args) {
  24. auto param = arg.param;
  25. auto ishape = arg.ishape;
  26. auto scale_shape = arg.scale_shape;
  27. auto zeropoint_shape = arg.zeropoint_shape;
  28. Checker<FakeQuantForward> checker(handle_cuda());
  29. checker.set_param(param)
  30. .set_dtype(0, dtype)
  31. .set_dtype(1, dtype)
  32. .set_dtype(2, dtype)
  33. .set_dtype(3, dtype)
  34. .execs(TensorShapeArray{ishape, scale_shape, zeropoint_shape, ishape});
  35. }
  36. // test noncontiguous layout
  37. for (auto&& arg : args) {
  38. auto param = arg.param;
  39. auto ishape = arg.ishape;
  40. auto scale_shape = arg.scale_shape;
  41. auto zeropoint_shape = arg.zeropoint_shape;
  42. Checker<FakeQuantForward> checker(handle_cuda());
  43. TensorLayout ilayout(
  44. ishape,
  45. {(long int)(ishape[1] * ishape[2] * ishape[3] * 2),
  46. (long int)(ishape[2] * ishape[3]), (long int)ishape[3], 1},
  47. dtype::Float32());
  48. checker.set_param(param).execl(
  49. {ilayout,
  50. {scale_shape, dtype::Float32()},
  51. {zeropoint_shape, dtype::Float32()},
  52. ilayout});
  53. }
  54. }
  55. TEST_F(CUDA, FAKE_QUANT_BACKWARD) {
  56. std::vector<TestArg> args = get_args();
  57. auto dtype = dtype::Float32();
  58. for (auto&& arg : args) {
  59. auto param = arg.param;
  60. auto ishape = arg.ishape;
  61. auto scale_shape = arg.scale_shape;
  62. auto zeropoint_shape = arg.zeropoint_shape;
  63. Checker<FakeQuantBackward> checker(handle_cuda());
  64. checker.set_param(param)
  65. .set_dtype(0, dtype)
  66. .set_dtype(1, dtype)
  67. .set_dtype(2, dtype)
  68. .set_dtype(3, dtype)
  69. .set_dtype(4, dtype)
  70. .execs(TensorShapeArray{
  71. ishape, ishape, scale_shape, zeropoint_shape, ishape});
  72. }
  73. // test noncontiguous layout
  74. for (auto&& arg : args) {
  75. auto param = arg.param;
  76. auto ishape = arg.ishape;
  77. auto scale_shape = arg.scale_shape;
  78. auto zeropoint_shape = arg.zeropoint_shape;
  79. Checker<FakeQuantBackward> checker(handle_cuda());
  80. TensorLayout ilayout(
  81. ishape,
  82. {(long int)(ishape[1] * ishape[2] * ishape[3] * 2),
  83. (long int)(ishape[2] * ishape[3]), (long int)ishape[3], 1},
  84. dtype::Float32());
  85. checker.set_param(param).execl(
  86. {ilayout,
  87. ilayout,
  88. {scale_shape, dtype::Float32()},
  89. {zeropoint_shape, dtype::Float32()},
  90. ilayout});
  91. }
  92. }
  93. } // namespace test
  94. } // namespace megdnn

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