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 5.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. UniformFloatRNG rng(-1.0f, 1.0f);
  23. const auto nan = std::numeric_limits<float>::quiet_NaN();
  24. UniformFloatWithValueRNG rng1 = UniformFloatWithValueRNG(-1.0f, 1.0f, 0.5f, nan);
  25. for (auto&& arg : args) {
  26. auto param = arg.param;
  27. auto ishape = arg.ishape;
  28. auto scale_shape = arg.scale_shape;
  29. auto zeropoint_shape = arg.zeropoint_shape;
  30. Checker<FakeQuantForward> checker(handle_cuda());
  31. checker.set_param(param)
  32. .set_dtype(0, dtype)
  33. .set_dtype(1, dtype)
  34. .set_dtype(2, dtype)
  35. .set_dtype(3, dtype)
  36. .execs(TensorShapeArray{ishape, scale_shape, zeropoint_shape, ishape});
  37. checker.set_allow_invalid_check(true);
  38. checker.set_rng(0, &rng1);
  39. checker.set_param(param)
  40. .set_dtype(0, dtype)
  41. .set_dtype(1, dtype)
  42. .set_dtype(2, dtype)
  43. .set_dtype(3, dtype)
  44. .execs(TensorShapeArray{ishape, scale_shape, zeropoint_shape, ishape});
  45. checker.set_rng(0, &rng);
  46. checker.set_allow_invalid_check(false);
  47. }
  48. // test noncontiguous layout
  49. for (auto&& arg : args) {
  50. auto param = arg.param;
  51. auto ishape = arg.ishape;
  52. auto scale_shape = arg.scale_shape;
  53. auto zeropoint_shape = arg.zeropoint_shape;
  54. Checker<FakeQuantForward> checker(handle_cuda());
  55. TensorLayout ilayout(
  56. ishape,
  57. {(long int)(ishape[1] * ishape[2] * ishape[3] * 2),
  58. (long int)(ishape[2] * ishape[3]), (long int)ishape[3], 1},
  59. dtype::Float32());
  60. checker.set_param(param).execl(
  61. {ilayout,
  62. {scale_shape, dtype::Float32()},
  63. {zeropoint_shape, dtype::Float32()},
  64. ilayout});
  65. checker.set_allow_invalid_check(true);
  66. checker.set_rng(0, &rng1);
  67. checker.set_param(param).execl(
  68. {ilayout,
  69. {scale_shape, dtype::Float32()},
  70. {zeropoint_shape, dtype::Float32()},
  71. ilayout});
  72. checker.set_rng(0, &rng);
  73. checker.set_allow_invalid_check(false);
  74. }
  75. }
  76. TEST_F(CUDA, FAKE_QUANT_BACKWARD) {
  77. std::vector<TestArg> args = get_args();
  78. auto dtype = dtype::Float32();
  79. UniformFloatRNG rng(-1.0f, 1.0f);
  80. const auto nan = std::numeric_limits<float>::quiet_NaN();
  81. UniformFloatWithValueRNG rng1 = UniformFloatWithValueRNG(-1.0f, 1.0f, 0.5f, nan);
  82. for (auto&& arg : args) {
  83. auto param = arg.param;
  84. auto ishape = arg.ishape;
  85. auto scale_shape = arg.scale_shape;
  86. auto zeropoint_shape = arg.zeropoint_shape;
  87. Checker<FakeQuantBackward> checker(handle_cuda());
  88. checker.set_param(param)
  89. .set_dtype(0, dtype)
  90. .set_dtype(1, dtype)
  91. .set_dtype(2, dtype)
  92. .set_dtype(3, dtype)
  93. .set_dtype(4, dtype)
  94. .execs(TensorShapeArray{
  95. ishape, ishape, scale_shape, zeropoint_shape, ishape});
  96. checker.set_allow_invalid_check(true);
  97. checker.set_rng(0, &rng1);
  98. checker.set_param(param)
  99. .set_dtype(0, dtype)
  100. .set_dtype(1, dtype)
  101. .set_dtype(2, dtype)
  102. .set_dtype(3, dtype)
  103. .set_dtype(4, dtype)
  104. .execs(TensorShapeArray{
  105. ishape, ishape, scale_shape, zeropoint_shape, ishape});
  106. checker.set_rng(0, &rng);
  107. checker.set_allow_invalid_check(false);
  108. }
  109. // test noncontiguous layout
  110. for (auto&& arg : args) {
  111. auto param = arg.param;
  112. auto ishape = arg.ishape;
  113. auto scale_shape = arg.scale_shape;
  114. auto zeropoint_shape = arg.zeropoint_shape;
  115. Checker<FakeQuantBackward> checker(handle_cuda());
  116. TensorLayout ilayout(
  117. ishape,
  118. {(long int)(ishape[1] * ishape[2] * ishape[3] * 2),
  119. (long int)(ishape[2] * ishape[3]), (long int)ishape[3], 1},
  120. dtype::Float32());
  121. checker.set_param(param).execl(
  122. {ilayout,
  123. ilayout,
  124. {scale_shape, dtype::Float32()},
  125. {zeropoint_shape, dtype::Float32()},
  126. ilayout});
  127. checker.set_allow_invalid_check(true);
  128. checker.set_rng(0, &rng1);
  129. checker.set_param(param).execl(
  130. {ilayout,
  131. ilayout,
  132. {scale_shape, dtype::Float32()},
  133. {zeropoint_shape, dtype::Float32()},
  134. ilayout});
  135. checker.set_rng(0, &rng);
  136. checker.set_allow_invalid_check(false);
  137. }
  138. }
  139. } // namespace test
  140. } // namespace megdnn

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