llama

Форк
0
/
test-double-float.cpp 
57 строк · 1.8 Кб
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

5
#undef NDEBUG
6
#include <cassert>
7
#if !defined(__riscv) && !defined(__s390__) && !defined(__ARM_NEON)
8
#include <immintrin.h>
9
#endif
10
#include <cmath>
11
#include <cstdint>
12
#include <cstring>
13

14
#pragma GCC diagnostic push
15
#pragma GCC diagnostic ignored "-Wdouble-promotion"
16

17
// ggml.c::quantize_row_q4_0_ref
18
inline static uint8_t round_orig(float v0) { return ((int8_t) (round(v0))) + 8; }
19

20
// ggml.c::ggml_silu_f32
21
inline static float silu_orig(float x) {
22
    return x/(1.0 + exp(-x));
23
}
24

25
#pragma GCC diagnostic pop
26

27
// ggml.c::quantize_row_q4_0_ref
28
inline static uint8_t round_float(float v0) { return (int8_t)roundf(v0) + 8; }
29

30
// ggml.c::ggml_silu_f32
31
inline static float silu_float(float x) {
32
    return x/(1.0f + expf(-x));
33
}
34

35
int main(void) {
36
    uint32_t x = UINT32_MAX;
37
    do {
38
        float f;
39
        memcpy(&f, &x, sizeof(x));
40
        assert(!std::isfinite(f) || (round_orig(f) == round_float(f)));
41
    } while (x--);
42

43
#ifdef __F16C__
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));
55
    }
56
#endif
57
}
58

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.