How to use the Low bit tensor packing Calculator tool
- Type the number of elements in the tensor in the Number of values field, using digits only.
- Type the width of each packed element, a whole number from 1 to 64, in the Bits per value field.
- Select Calculate, or select Example for 1048576 four-bit values.
- Check the Padding line for the unused bits and the Efficiency line for the share of the buffer that holds data.
- Select Copy to save the result, or Clear to try another width.
How low-bit tensor packing fills bytes
Bit packing writes values back to back with no gaps. With 4-bit values, two fit in each byte. With 2-bit values, four fit. Widths that do not divide 8 cross byte boundaries: eight 3-bit values fill exactly three bytes, eight 5-bit values fill five, and four 6-bit values fill three.
The buffer length is the total number of payload bits divided by 8, rounded up. If the total is not a multiple of 8, the last byte is only partly used. The tool reports those leftover bits as Padding, a number from 0 to 7, and reports Efficiency as payload bits divided by buffer bits.
Worked example: ten 3-bit values
Ten values at 3 bits are 30 bits of payload. Thirty bits need four bytes, which hold 32 bits, so two bits are padding and 30 / 32 = 93.75 percent of the buffer is data. Make it sixteen values and the payload is 48 bits, exactly six bytes with nothing wasted.
Number of values: 10, Bits per value: 3
4 bytes
0.000004 MiB
Payload: 30 bits
Padding: 2 bits
Efficiency: 93.75%
Number of values: 16, Bits per value: 3
6 bytes
0.000006 MiB
Payload: 48 bits
Padding: 0 bits
Efficiency: 100.00%When padding matters and when it does not
For one large tensor, padding is negligible. A tensor of 1,000,003 three-bit values has 7 padding bits in 375,002 bytes, and the Efficiency line rounds to 100.00%. The picture changes when you pack many small pieces separately. If every row or block starts on a fresh byte, each one pays its own padding.
Take rows of 5 values at 3 bits. One row is 15 bits and needs 2 bytes with 1 bit unused. Pack 1,000 such rows independently and you spend 2,000 bytes, while packing all 5,000 values as one stream needs 1,875. To size a format with per-row alignment, run the calculator for one row and multiply.
Packed versus unpacked storage
The alternative to packing is to keep each small value in its own byte, which is simpler and faster to index but wasteful: a 3-bit value in an int8 uses 37.5 percent of the space it occupies. Packing recovers that space at the cost of shifts and masks on every read. The calculator covers the packed case only, and it assumes one fixed width for all elements. It works from the two numbers you type, in your browser, and never sees tensor data.
Frequently asked questions
How many bytes do N values of B bits need when packed?
Multiply N by B, add 7 and divide by 8, discarding the remainder. That is the ceiling of the bit count over 8. For 21 values of 3 bits: 63 bits, so 8 bytes with 1 bit of padding.
Which bit widths pack without padding?
It depends on the count as well as the width. Padding is zero whenever count times width is a multiple of 8. Widths of 8, 16, 32 and 64 always qualify. A 4-bit tensor needs an even count, and a 3-bit tensor needs a multiple of 8 values.
Why does Efficiency show 100.00% when Padding is not zero?
Efficiency is rounded to two decimal places. In a large buffer a few unused bits are a tiny fraction, so the percentage rounds up to 100.00. Read the Padding line for the exact number of unused bits.
Does the calculator handle ternary weights?
It handles fixed-width fields only. Ternary values stored in 2 bits each can be entered as width 2. Denser encodings, such as five ternary values in one byte, are not fixed-width and need their own arithmetic.
Where do the padding bits go?
The tool only counts them. Whether they sit in the high or low end of the last byte, and whether they must be zero, is defined by the storage format you are implementing.