Add changes to use union data type for better conversion to strong type - Based on 5f2e011e2eed2f685521c707b3e74280fcb81dd3 from llamafile

This commit is contained in:
Srihari-mcw 2024-08-01 23:57:12 -07:00
parent ef693e99d7
commit 4a2f703fbb
3 changed files with 9 additions and 7 deletions

View file

@ -346,7 +346,6 @@ extern "C" {
// google brain half-precision bfloat16
typedef struct { uint16_t bits; } ggml_bf16_t;
GGML_API ggml_bf16_t ggml_make_bf16(uint16_t val);
GGML_API ggml_bf16_t ggml_fp32_to_bf16(float);
GGML_API float ggml_bf16_to_fp32(ggml_bf16_t); // consider just doing << 16
GGML_API void ggml_bf16_to_fp32_row(const ggml_bf16_t *, float *, int64_t);

View file

@ -102,6 +102,15 @@ static inline ggml_bf16_t ggml_compute_fp32_to_bf16(float s) {
return h;
}
static inline ggml_bf16_t ggml_make_bf16(uint16_t h) {
union {
ggml_bf16_t f;
uint16_t i;
} u;
u.i = h;
return u.f;
}
#define GGML_FP32_TO_BF16(x) ggml_compute_fp32_to_bf16(x)
#define GGML_BF16_TO_FP32(x) ggml_compute_bf16_to_fp32(x)

View file

@ -428,12 +428,6 @@ float ggml_bf16_to_fp32(ggml_bf16_t x) {
return GGML_BF16_TO_FP32(x); // it just left shifts
}
ggml_bf16_t ggml_make_bf16(uint16_t x) {
ggml_bf16_t bf16_value;
bf16_value.bits = x;
return bf16_value;
}
ggml_bf16_t ggml_fp32_to_bf16(float x) {
#define ggml_fp32_to_bf16 do_not_use__ggml_fp32_to_bf16__in_ggml
return GGML_FP32_TO_BF16(x);