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.

powc.cpp 2.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * \file dnn/test/common/powc.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 implied.
  10. */
  11. #include "test/common/powc.h"
  12. #include "test/common/checker.h"
  13. using namespace megdnn;
  14. using namespace test;
  15. void test::run_powc_test(Handle* handle, DType dtype) {
  16. Checker<PowC> checker{handle};
  17. checker.set_dtype(0, dtype);
  18. float dt_val_max;
  19. if (dtype == dtype::Float32{}) {
  20. dt_val_max = DTypeTrait<dt_float32>::max();
  21. } else {
  22. megdnn_assert(dtype == dtype::Float16{});
  23. dt_val_max = DTypeTrait<dt_float16>::max();
  24. checker.set_epsilon(1e-2);
  25. }
  26. dt_val_max /= 4;
  27. for (float exp :
  28. {0.f, 1.f / 3.f, 1.f / 3.f + 0.01f, .5f, 1.f, 1.2f, 2.f, 3.f, 4.f, 7.f, 8.f}) {
  29. float rng_max =
  30. exp ? std::pow(dt_val_max, std::min(1.f / exp, 1.f)) : dt_val_max;
  31. bool allow_neg;
  32. {
  33. auto d = exp - std::floor(exp);
  34. if (d >= .1f) {
  35. allow_neg = false;
  36. } else {
  37. allow_neg = true;
  38. }
  39. }
  40. UniformFloatRNG rng0{-rng_max, rng_max}, rng1{0.f, rng_max};
  41. checker.set_rng(0, allow_neg ? &rng0 : &rng1);
  42. checker.set_param(exp);
  43. checker.execs({TensorShape{23, 34}, {}});
  44. if (::testing::Test::HasFailure()) {
  45. printf("failed for %g\n", exp);
  46. return;
  47. }
  48. UniformFloatNonZeroRNG rng2{1.f / rng_max, dt_val_max};
  49. UniformFloatRNG rng3{1.f / rng_max, dt_val_max};
  50. if (allow_neg) {
  51. checker.set_rng(0, &rng2);
  52. } else {
  53. checker.set_rng(0, &rng3);
  54. }
  55. checker.set_param(-exp);
  56. checker.execs({TensorShape{3, 7, 2}, {}});
  57. if (::testing::Test::HasFailure()) {
  58. printf("failed for %g\n", -exp);
  59. return;
  60. }
  61. // non contig
  62. TensorLayout layout{{4, 9}, dtype};
  63. layout.stride[0] *= 3;
  64. layout.stride[1] *= 2;
  65. checker.execl({layout, {}});
  66. if (::testing::Test::HasFailure()) {
  67. printf("failed for %g noncontig\n", -exp);
  68. return;
  69. }
  70. }
  71. }
  72. // vim: syntax=cpp.doxygen