Skip to content

Commit 5b67a6c

Browse files
committed
ggml-impl : do not flush bf16 subnormals to zero
* ggml : add reference fp32 to bf16 conversion The fast version is no longer equivalent for all platforms because of the handling of subnormal values. * gguf-py : remove flush to zero for bf16 subnormals * gguf-py : remove float32 truncation to bf16 Rounding achieves the same thing in the cases where this was used.
1 parent e8e2b7e commit 5b67a6c

File tree

5 files changed

+14
-31
lines changed

5 files changed

+14
-31
lines changed

convert-hf-to-gguf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ def write_tensors(self):
295295

296296
if self.ftype != gguf.LlamaFileType.ALL_F32 and extra_f16 and not extra_f32:
297297
if self.ftype == gguf.LlamaFileType.MOSTLY_BF16:
298-
data = gguf.truncate_bf16(data) if old_dtype == torch.bfloat16 else gguf.quantize_bf16(data)
298+
data = gguf.quantize_bf16(data)
299299
assert data.dtype == np.uint16
300300
data_qtype = gguf.GGMLQuantizationType.BF16
301301

ggml-impl.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ static inline float ggml_compute_bf16_to_fp32(ggml_bf16_t h) {
8080
/**
8181
* Converts float32 to brain16.
8282
*
83-
* This function is binary identical to AMD Zen4 VCVTNEPS2BF16.
84-
* Subnormals shall be flushed to zero, and NANs will be quiet.
83+
* This is binary identical with Google Brain float conversion.
84+
* Floats shall round to nearest even, and NANs shall be quiet.
85+
* Subnormals aren't flushed to zero, except perhaps when used.
8586
* This code should vectorize nicely if using modern compilers.
8687
*/
8788
static inline ggml_bf16_t ggml_compute_fp32_to_bf16(float s) {
@@ -95,10 +96,6 @@ static inline ggml_bf16_t ggml_compute_fp32_to_bf16(float s) {
9596
h.bits = (u.i >> 16) | 64; /* force to quiet */
9697
return h;
9798
}
98-
if (!(u.i & 0x7f800000)) { /* subnormal */
99-
h.bits = (u.i & 0x80000000) >> 16; /* flush to zero */
100-
return h;
101-
}
10299
h.bits = (u.i + (0x7fff + ((u.i >> 16) & 1))) >> 16;
103100
return h;
104101
}

ggml.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,16 @@ void ggml_bf16_to_fp32_row(const ggml_bf16_t * x, float * y, int64_t n) {
411411
}
412412
}
413413

414+
void ggml_fp32_to_bf16_row_reference(const float * x, ggml_bf16_t * y, int64_t n) {
415+
for (int i = 0; i < n; i++) {
416+
y[i] = ggml_compute_fp32_to_bf16(x[i]);
417+
}
418+
}
419+
414420
void ggml_fp32_to_bf16_row(const float * x, ggml_bf16_t * y, int64_t n) {
415421
int i = 0;
416422
#if defined(__AVX512BF16__)
423+
// subnormals are flushed to zero on this platform
417424
for (; i + 32 <= n; i += 32) {
418425
_mm512_storeu_si512(
419426
(__m512i *)(y + i),
@@ -904,7 +911,7 @@ static const ggml_type_traits_t type_traits[GGML_TYPE_COUNT] = {
904911
.is_quantized = false,
905912
.to_float = (ggml_to_float_t) ggml_bf16_to_fp32_row,
906913
.from_float = (ggml_from_float_t) ggml_fp32_to_bf16_row,
907-
.from_float_reference = (ggml_from_float_t) ggml_fp32_to_bf16_row,
914+
.from_float_reference = (ggml_from_float_t) ggml_fp32_to_bf16_row_reference,
908915
.vec_dot = (ggml_vec_dot_t) ggml_vec_dot_bf16,
909916
.vec_dot_type = GGML_TYPE_BF16,
910917
.nrows = 1,
@@ -21334,7 +21341,7 @@ size_t ggml_quantize_chunk(
2133421341
case GGML_TYPE_BF16:
2133521342
{
2133621343
size_t elemsize = sizeof(ggml_bf16_t);
21337-
ggml_fp32_to_bf16_row(src + start, (ggml_bf16_t *)dst + start, n);
21344+
ggml_fp32_to_bf16_row_reference(src + start, (ggml_bf16_t *)dst + start, n);
2133821345
result = n * elemsize;
2133921346
} break;
2134021347
case GGML_TYPE_F32:

ggml.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ extern "C" {
339339
GGML_API ggml_bf16_t ggml_fp32_to_bf16(float);
340340
GGML_API float ggml_bf16_to_fp32(ggml_bf16_t); // consider just doing << 16
341341
GGML_API void ggml_bf16_to_fp32_row(const ggml_bf16_t *, float *, int64_t);
342+
GGML_API void ggml_fp32_to_bf16_row_reference(const float *, ggml_bf16_t *, int64_t);
342343
GGML_API void ggml_fp32_to_bf16_row(const float *, ggml_bf16_t *, int64_t);
343344

344345
struct ggml_object;

gguf-py/gguf/quants.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,11 @@ def __compute_fp32_to_bf16(n: np.ndarray) -> np.ndarray:
2828
n = n.astype(np.float32, copy=False).view(np.uint32)
2929
# force nan to quiet
3030
n = np.where((n & 0x7fffffff) > 0x7f800000, (n & np.uint32(0xffff0000)) | np.uint32(64 << 16), n)
31-
# flush subnormals to zero
32-
n = np.where((n & 0x7f800000) == 0, n & np.uint32(0x80000000), n)
3331
# round to nearest even
3432
n = (np.uint64(n) + (0x7fff + ((n >> 16) & 1))) >> 16
3533
return n.astype(np.uint16)
3634

3735

38-
# for fp32 values that are just extended bf16
39-
def __truncate_fp32_to_bf16(n: np.ndarray) -> np.ndarray:
40-
n = n.astype(np.float32, copy=False).view(np.uint32) >> 16
41-
return n.astype(np.uint16)
42-
43-
4436
# This is faster than np.vectorize and np.apply_along_axis because it works on more than one row at a time
4537
def __apply_over_grouped_rows(func: Callable[[np.ndarray], np.ndarray], arr: np.ndarray, otype: DTypeLike, oshape: tuple[int, ...]) -> np.ndarray:
4638
rows = arr.reshape((-1, arr.shape[-1]))
@@ -68,20 +60,6 @@ def quantize_bf16(n: np.ndarray):
6860
return __quantize_bf16_array(n)
6961

7062

71-
def __truncate_bf16_array(n: np.ndarray) -> np.ndarray:
72-
return __apply_over_grouped_rows(__truncate_fp32_to_bf16, arr=n, otype=np.uint16, oshape=n.shape)
73-
74-
75-
__truncate_bf16_lazy = LazyNumpyTensor._wrap_fn(__truncate_bf16_array, meta_noop=np.uint16)
76-
77-
78-
def truncate_bf16(n: np.ndarray):
79-
if type(n) is LazyNumpyTensor:
80-
return __truncate_bf16_lazy(n)
81-
else:
82-
return __truncate_bf16_array(n)
83-
84-
8563
__q8_block_size, __q8_type_size = GGML_QUANT_SIZES[GGMLQuantizationType.Q8_0]
8664

8765

0 commit comments

Comments
 (0)