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.
7
#if !defined(__riscv) && !defined(__s390__) && !defined(__ARM_NEON)
14
#pragma GCC diagnostic push
15
#pragma GCC diagnostic ignored "-Wdouble-promotion"
17
// ggml.c::quantize_row_q4_0_ref
18
inline static uint8_t round_orig(float v0) { return ((int8_t) (round(v0))) + 8; }
20
// ggml.c::ggml_silu_f32
21
inline static float silu_orig(float x) {
22
return x/(1.0 + exp(-x));
25
#pragma GCC diagnostic pop
27
// ggml.c::quantize_row_q4_0_ref
28
inline static uint8_t round_float(float v0) { return (int8_t)roundf(v0) + 8; }
30
// ggml.c::ggml_silu_f32
31
inline static float silu_float(float x) {
32
return x/(1.0f + expf(-x));
36
uint32_t x = UINT32_MAX;
39
memcpy(&f, &x, sizeof(x));
40
assert(!std::isfinite(f) || (round_orig(f) == round_float(f)));
44
// GELU and SILU implementations are used with a FP16 lookup table.
45
// The original and float-only results are not equal for all inputs after converting to FP16.
46
// GELU is an approximation anyway (tanh), not tested here.
47
// For SILU, verify that the results are at least the closest floating point numbers, if the FP16 values don't match.
48
for (x = 0; x <= UINT16_MAX; x++) {
49
float f = _cvtsh_ss(x);
50
const float so = silu_orig(f);
51
const float sf = silu_float(f);
52
assert( (_cvtss_sh(so, 0) == _cvtss_sh(sf, 0))
53
|| (nextafterf(so, sf) == sf)
54
|| (nextafterf(sf, so) == so));