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.

lsq.cpp 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * \file dnn/test/cuda/lsq.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/lsq.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 lsq;
  19. TEST_F(CUDA, LSQ) {
  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. auto zeropoint_shape = arg.zeropoint_shape;
  27. auto gradscale_shape = arg.gradscale_shape;
  28. Checker<LSQForward> 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. .set_dtype(4, dtype)
  35. .execs({ishape, scale_shape, zeropoint_shape, gradscale_shape, ishape});
  36. }
  37. // test noncontiguous layout
  38. for (auto&& arg : args) {
  39. auto param = arg.param;
  40. auto ishape = arg.ishape;
  41. auto sshape = arg.scale_shape;
  42. auto zeropoint_shape = arg.zeropoint_shape;
  43. auto gradscale_shape = arg.gradscale_shape;
  44. Checker<LSQForward> checker(handle_cuda());
  45. TensorLayout ilayout(
  46. ishape,
  47. {(long int)(ishape[1] * ishape[2] * ishape[3] * 2),
  48. (long int)(ishape[2] * ishape[3]), (long int)ishape[3], 1},
  49. dtype::Float32());
  50. checker.set_param(param).execl(
  51. {ilayout,
  52. {sshape, dtype::Float32()},
  53. {zeropoint_shape, dtype::Float32()},
  54. {gradscale_shape, dtype::Float32()},
  55. ilayout});
  56. }
  57. }
  58. TEST_F(CUDA, LSQ_BACKWARD) {
  59. std::vector<TestArg> args = get_args();
  60. auto dtype = dtype::Float32();
  61. for (auto&& arg : args) {
  62. auto param = arg.param;
  63. auto ishape = arg.ishape;
  64. auto scale_shape = arg.scale_shape;
  65. auto zeropoint_shape = arg.zeropoint_shape;
  66. auto gradscale_shape = arg.gradscale_shape;
  67. Checker<LSQBackward> checker(handle_cuda());
  68. checker.set_param(param)
  69. .set_dtype(0, dtype)
  70. .set_dtype(1, dtype)
  71. .set_dtype(2, dtype)
  72. .set_dtype(3, dtype)
  73. .set_dtype(4, dtype)
  74. .set_dtype(5, dtype)
  75. .set_dtype(6, dtype)
  76. .execs({ishape, ishape, scale_shape, zeropoint_shape, gradscale_shape,
  77. ishape, ishape});
  78. }
  79. // test noncontiguous layout
  80. for (auto&& arg : args) {
  81. auto param = arg.param;
  82. auto ishape = arg.ishape;
  83. auto sshape = arg.scale_shape;
  84. auto zeropoint_shape = arg.zeropoint_shape;
  85. auto gradscale_shape = arg.gradscale_shape;
  86. Checker<LSQBackward> checker(handle_cuda());
  87. TensorLayout ilayout(
  88. ishape,
  89. {(long int)(ishape[1] * ishape[2] * ishape[3] * 2),
  90. (long int)(ishape[2] * ishape[3]), (long int)ishape[3], 1},
  91. dtype::Float32());
  92. checker.set_param(param).execl(
  93. {ilayout,
  94. ilayout,
  95. {sshape, dtype::Float32()},
  96. {zeropoint_shape, dtype::Float32()},
  97. {gradscale_shape, dtype::Float32()},
  98. ilayout,
  99. ilayout});
  100. }
  101. }
  102. } // namespace test
  103. } // namespace megdnn