1
0

test-double-float.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // These tests may take a long time!
  2. // They are to prove that conversion from double to float of various functions in ggml.c doesn't affect the result.
  3. // This is done by checking all finite (non-NaN, non-infinite) floats.
  4. #undef NDEBUG
  5. #include <cassert>
  6. #if !defined(__riscv) && !defined(__s390__) && !defined(__ARM_NEON)
  7. #include <immintrin.h>
  8. #endif
  9. #include <cmath>
  10. #include <cstdint>
  11. #include <cstring>
  12. #pragma GCC diagnostic push
  13. #pragma GCC diagnostic ignored "-Wdouble-promotion"
  14. // ggml.c::quantize_row_q4_0_reference
  15. inline static uint8_t round_orig(float v0) { return ((int8_t) (round(v0))) + 8; }
  16. // ggml.c::ggml_silu_f32
  17. inline static float silu_orig(float x) {
  18. return x/(1.0 + exp(-x));
  19. }
  20. #pragma GCC diagnostic pop
  21. // ggml.c::quantize_row_q4_0_reference
  22. inline static uint8_t round_float(float v0) { return (int8_t)roundf(v0) + 8; }
  23. // ggml.c::ggml_silu_f32
  24. inline static float silu_float(float x) {
  25. return x/(1.0f + expf(-x));
  26. }
  27. int main(void) {
  28. uint32_t x = UINT32_MAX;
  29. do {
  30. float f;
  31. memcpy(&f, &x, sizeof(x));
  32. assert(!std::isfinite(f) || (round_orig(f) == round_float(f)));
  33. } while (x--);
  34. #ifdef __F16C__
  35. // GELU and SILU implementations are used with a FP16 lookup table.
  36. // The original and float-only results are not equal for all inputs after converting to FP16.
  37. // GELU is an approximation anyway (tanh), not tested here.
  38. // For SILU, verify that the results are at least the closest floating point numbers, if the FP16 values don't match.
  39. for (x = 0; x <= UINT16_MAX; x++) {
  40. float f = _cvtsh_ss(x);
  41. const float so = silu_orig(f);
  42. const float sf = silu_float(f);
  43. assert( (_cvtss_sh(so, 0) == _cvtss_sh(sf, 0))
  44. || (nextafterf(so, sf) == sf)
  45. || (nextafterf(sf, so) == so));
  46. }
  47. #endif
  48. }