summaryrefslogtreecommitdiff
path: root/iqk-quantize.h
diff options
context:
space:
mode:
authorIwan Kawrakow <iwan.kawrakow@gmail.com>2024-06-18 20:08:28 +0300
committerIwan Kawrakow <iwan.kawrakow@gmail.com>2024-06-22 12:02:52 +0300
commit927e251a12fa287e13c6bd9667ee97d783486c09 (patch)
tree90ed8827fc28630f52e92d8b8ea664198a6f5829 /iqk-quantize.h
parent181fd9c56eaa64d0a92f9e8be7387f409cfa8745 (diff)
Bitnet(1.75 bpw): higher precision fp8 scale
Use 3 bits for the exponent and 5 bits for the mantissa. This makes PPL to be the same as fp16 (but the previous version with 4 bits for the exponent and mantissa was good enough for any practical purposes).
Diffstat (limited to 'iqk-quantize.h')
-rw-r--r--iqk-quantize.h46
1 files changed, 46 insertions, 0 deletions
diff --git a/iqk-quantize.h b/iqk-quantize.h
new file mode 100644
index 00000000..b89c9427
--- /dev/null
+++ b/iqk-quantize.h
@@ -0,0 +1,46 @@
+#pragma once
+
+#include <stdint.h>
+
+typedef union {
+ float f;
+ uint32_t i;
+} iq1bn_scale_t;
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef BITNET_IQ1BN_4x4
+static inline float iq1bn_min_value(void) { return 1.9074e-06f; }
+static inline float iq1bn_max_value(void) { return 0.12109f; }
+#else
+static inline float iq1bn_min_value(void) { return 0.000488281f; }
+static inline float iq1bn_max_value(void) { return 0.123047f; }
+#endif
+
+static inline uint8_t iq1bn_float_to_fp8(float f) {
+ if (f <= iq1bn_min_value()) return 0;
+ if (f >= iq1bn_max_value()) return 255;
+ iq1bn_scale_t s;
+ s.f = f;
+#ifdef BITNET_IQ1BN_4x4
+ return ((((s.i >> 23) + 132) & 0xf) << 4) | ((s.i >> 19) & 0xf);
+#else
+ return ((s.i >> 18) & 0x1f) | (((s.i >> 23) - 116) << 5);
+#endif
+}
+
+static inline float iq1bn_fp8_to_float(uint8_t fp8) {
+ iq1bn_scale_t s;
+#ifdef BITNET_IQ1BN_4x4
+ s.i = ((((fp8 >> 4) | 0xf0) - 132) << 23) | ((fp8 & 0x0f) << 19);
+#else
+ s.i = (((fp8 >> 5) + 116) << 23) | ((fp8 & 0x1f) << 18);
+#endif
+ return s.f;
+}
+
+#ifdef __cplusplus
+}
+#endif