bitsandbytes-foundation / bitsandbytes

Accessible large language models via k-bit quantization for PyTorch.
https://huggingface.co/docs/bitsandbytes/main/en/index
MIT License
6.22k stars 625 forks source link

FP4 Format in Bitsandbytes Library vs ieee 754 standard #988

Open metamath1 opened 9 months ago

metamath1 commented 9 months ago

I have a question regarding the functional.py file in the bitsandbytes library, specifically the comment on line 855:

0b000 = 0
0b001 = 0.0625
0b010 = 8
0b011 = 12
0b100 = 4
0b101 = 6
0b110 = 2
0b111 = 3
can also be created with bnb.functional.create_fp8_map(signed=True, exponent_bits=2, precision_bits=1, total_bits=4)

For the FP4 format with 1 sign bit, 2 exponent bits, and 1 mantissa bit, where the sign bit is 0, I'm struggling to understand why the values are calculated as shown above.

According to the IEEE 754 standard, I believe the calculations should be(not considering bias):

0b000 = 0
0b001 = 1.5  (1 + 1*2^-1) * 2^0
0b010 = 2    (1 + 0*2^-1) * 2^1
0b011 = 3    (1 + 1*2^-1) * 2^1
0b100 = 4    (1 + 0*2^-1) * 2^2
0b101 = 6    (1 + 1*2^-1) * 2^2
0b110 = 8    (1 + 0*2^-1) * 2^3
0b111 = 12   (1 + 1*2^-1) * 2^3

What am I missing here? Additionally, could you advise on what documents I should refer to for a better understanding of the FP4 format?

Thank you for your assistance.

kkwhale7 commented 6 months ago

I am also thinking about this issue! help me ! @metamath1 @kashif @stephenroller @akx @jbn

xyliaaaaa commented 3 months ago

second this!