aboutsummaryrefslogtreecommitdiff
path: root/src/dtype.h
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-06-17 01:21:41 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-06-17 01:21:41 +0200
commite00becb643b3a99de16462b3a032717a93b983d9 (patch)
tree873ad2646b0ef2f07eae5a5a74387d062c77ec0c /src/dtype.h
parentb4343d6ac1c6cc7a7d228f954220def2f6e6ed67 (diff)
downloadffwd-e00becb643b3a99de16462b3a032717a93b983d9.tar.gz
ffwd-e00becb643b3a99de16462b3a032717a93b983d9.zip
all: accept F16 safetensors weights (widen to F32 at load)
Diffstat (limited to 'src/dtype.h')
-rw-r--r--src/dtype.h36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/dtype.h b/src/dtype.h
index 5c1ce8f..2a25cae 100644
--- a/src/dtype.h
+++ b/src/dtype.h
@@ -16,4 +16,40 @@ static inline float embed_bf16_to_f32(uint16_t bf16) {
return f;
}
+/* IEEE-754 binary16 -> binary32. Unlike BF16, F16 has a 5-bit exponent and a
+ * 10-bit mantissa, so widening is a field re-pack, not a shift: rebias the
+ * exponent (15 -> 127), shift the mantissa up by 13, and special-case zeros,
+ * subnormals, and inf/NaN. Used by the loaders to accept F16 safetensors
+ * snapshots (e.g. the public multilingual-e5 weights); there is no fused F16
+ * kernel, so weights are widened to F32 once at load. */
+static inline float embed_f16_to_f32(uint16_t f16) {
+ uint32_t sign = (uint32_t)(f16 & 0x8000u) << 16;
+ uint32_t exp = (f16 >> 10) & 0x1fu;
+ uint32_t mant = f16 & 0x3ffu;
+ uint32_t bits;
+ if (exp == 0) {
+ if (mant == 0) {
+ bits = sign; /* signed zero */
+ } else {
+ /* Subnormal half: renormalize into a normal float. Shift the
+ * mantissa left until its leading 1 reaches the hidden-bit
+ * position, decrementing the exponent for each shift. */
+ exp = 127 - 15 + 1;
+ while ((mant & 0x400u) == 0) {
+ mant <<= 1;
+ exp--;
+ }
+ mant &= 0x3ffu;
+ bits = sign | (exp << 23) | (mant << 13);
+ }
+ } else if (exp == 0x1fu) {
+ bits = sign | 0x7f800000u | (mant << 13); /* inf or NaN */
+ } else {
+ bits = sign | ((exp + (127 - 15)) << 23) | (mant << 13);
+ }
+ float f;
+ memcpy(&f, &bits, sizeof(f));
+ return f;
+}
+
#endif /* EMBED_DTYPE_H */