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

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