opencl: support bf16 by converting to f16 (#23839)

This commit is contained in:
lhez
2026-05-30 10:17:47 -07:00
committed by GitHub
parent d38d50e7ff
commit d6588daa80
2 changed files with 121 additions and 2 deletions
+42
View File
@@ -117,6 +117,48 @@ struct block_iq4_nl
uint8_t qs[QK4_NL / 2];
};
//------------------------------------------------------------------------------
// bf16 to f16
//------------------------------------------------------------------------------
kernel void kernel_convert_bf16_to_f16(
global const ushort * src,
global half * dst,
ulong off_dst,
ulong n
) {
uint i = get_global_id(0);
if (i >= n) {
return;
}
dst[i + off_dst] = (half) as_float((uint) src[i] << 16);
}
//------------------------------------------------------------------------------
// f16 to bf16
//------------------------------------------------------------------------------
kernel void kernel_convert_f16_to_bf16(
global const half * src,
ulong off_src,
global ushort * dst,
ulong n
) {
uint i = get_global_id(0);
if (i >= n) {
return;
}
float f = (float) src[i + off_src];
uint bits = as_uint(f);
if ((bits & 0x7fffffffu) > 0x7f800000u) {
// nan to quiet nan
dst[i] = (ushort)((bits >> 16) | 0x40u);
} else {
uint rounded = bits + 0x7fffu + ((bits >> 16) & 1u);
dst[i] = (ushort)(rounded >> 16);
}
}
//------------------------------------------------------------------------------
// kernel_convert_block_q4_0
// Convert the block_q4_0 format to 2 separate arrays (AOS -> SOA).