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.1 kB

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