Binary-Coding Quantization: Weights as Sums of Signs

How BCQ writes a weight as a sum of signed scales, how greedy and alternating fits choose the scales, how fast its error falls with each bit, how it compares with uniform quantization, and how a kernel computes with bit planes.

Doc no.
KKZ-TN-160
Date
2026-11-24
GMT day
2026:328
Est. read
11 min

Binary-coding quantization (BCQ) stores a weight as a sum of terms, each a sign times a scale:

Each term corrects what the earlier ones left, so the first p terms are a p-bit quantization on their own. Stored as separate bit planes, one copy of the weights can be read at 1, 2, 3 or 4 bits, and the arithmetic reduces to adding and subtracting.

This post covers the format, the two ways to fit it, how fast its error falls with each bit, how it compares with uniform quantization, and how a kernel computes with it.

01The Format#

1.1Signs and Scales#

Weights are split into groups of g consecutive weights. Each weight stores P sign bits ; each group stores P scales , shared by its weights. The signs of all weights for one p form bit plane p.

Figure 1 shows one group with g = 8 and P = 3. Each column is one weight j; each row is one plane p, with its scale :

Figure 1 — BCQ on one group, fitted with the greedy method described below. The error on the right is a share of the group’s .

Storage per weight is P bits for the signs plus the group’s share of the scales. With an FP16 α per plane and group, each plane stores 1 bit for the sign of every weight, plus one 16-bit α shared by g weights, so one plane costs

1.125 bpw for g = 128 and 2 bpw for g = 16. Small groups fit better but pay more for their scales, the same trade-off as in group-wise INT4 formats.

1.2The Grid#

A group with P planes can take 2P values, the sums . We call this set the grid.

Figure 2 — The grid for four choices of α. Flipping every sign negates a value, so each grid is symmetric about 0.
  • The grid is symmetric about 0, because flipping every sign of a value gives its negative. It has no offset, so a group whose weights are centred away from 0 cannot shift its levels to follow them.
  • Uniform quantization is a special case. With , the levels are evenly spaced: gives {−1.5, −0.5, 0.5, 1.5}. Other α make the spacing uneven.

02Fitting the Signs and Scales#

To compare fits, we measure the error relative to the size of the weights. With the value of weight j after reading b planes,

is the share of the weights’ energy, their sum of squares, that is left as error. D = 0 is an exact fit, and D(0) = 1, since with no planes every weight is 0. In Figure 1, D(1), D(2) and D(3) are the numbers on the right.

One plane has a closed-form answer. For more planes there are two methods: greedy, which fits one plane at a time, and alternating, which fits all planes together.

2.1One Plane#

With one plane, we want the α and signs that minimize over a group. For any α > 0, the best sign is . Then each term is , and setting the derivative in α to zero gives

the scale XNOR-Net derived for binary-weight networks. Substituting it back, with and , the error is

Dividing by gives

It depends only on the shape of the distribution:

  • Gaussian: , so .
  • Laplace, with heavier tails: and , so D(1) = 0.5.
  • Uniform on [−a, a]: and , so D(1) = 0.25.

2.2Greedy: One Plane at a Time#

The greedy method (Guo et al., 2017) fits each plane like the single plane above, on what the earlier planes left:

python
r = w                        # residual: what the planes so far leave out
for p in range(P):
    b[p] = sign(r)           # one sign per weight
    alpha[p] = mean(abs(r))  # one scale for the group
    r = r - alpha[p] * b[p]  # remove what plane p represents
# now w = alpha[0] * b[0] + ... + alpha[P-1] * b[P-1] + r

With the four weights and P = 2, each line of the loop gives one row of Table 1:

Table 1 — Greedy BCQ with P = 2 on a group of four weights, one row per line of the loop. The squared error is after each plane.
Stepw1w2w3w4Shared by the group
w−0.3−0.10.21.4
−1−1+1+1
0.20.4−0.30.9squared error 1.10
+1+1−1+1
−0.25−0.050.150.45squared error 0.29
−0.05−0.050.050.95

Each step only adds a correction, so the first p planes are exactly what greedy would have built with P = p. Reading fewer planes gives the greedy fit at fewer bits, not a broken weight. In Figure 1, D falls from 0.304 to 0.082 to 0.034 as planes 2 and 3 are read.

2.3Where Greedy Falls Short#

Each greedy α is the best scale for its own plane given the earlier ones, but it is chosen before the later planes exist. In Table 1, greedy ends with , the grid {−0.95, −0.05, 0.05, 0.95} and a squared error of 0.29. With the same signs, two planes can do much better.

To see where greedy’s α go wrong, split each value into a sign and a magnitude. With two planes and :

  • The sign of the value is the sign of , because is too small to flip it.
  • The magnitude depends on whether has the same sign as :

    • same sign: the two terms add, and the magnitude is , the large level;
    • opposite signs: the two terms partly cancel, and the magnitude is , the small level.

So plane 2 splits the weights into two classes by magnitude. In Table 1, has the opposite sign of for w1, w2 and w3, the small class (|w| = 0.3, 0.1, 0.2), and the same sign for w4, the large class (|w| = 1.4).

For a fixed split, the best level for each class is the mean |w| of that class, for the same reason that one plane takes :

  • small class: (0.3 + 0.1 + 0.2) / 3 = 0.2;
  • large class: 1.4.

Solving and gives . Greedy instead puts the two levels at , that is, 0.05 and 0.95, centred on mean |w| = 0.5 rather than on the two class means.

With and the same signs, the grid is {−1.4, −0.2, 0.2, 1.4} and the squared error is 0.02, 14 times lower:

Figure 3 — The same four weights and the same signs, with greedy α (a) and with α refitted by least squares (b). Each weight’s value is the tip of its orange arrow.

Greedy never makes this correction. It fixes before plane 2 exists and does not go back to it. Refitting α for fixed signs is the first step of the alternating method in the next section.

Greedy’s two levels are the class means only when as many weights fall above mean |w| as below. With half the weights in each class, of class means and , mean |w| is and the mean distance from it is , so greedy lands on and exactly. The example has 3 small weights and 1 large one.

Bell-shaped weights are unbalanced in the same direction: for a Gaussian, only 42.5% of the magnitudes exceed their mean. The greedy levels then sit below both class means:

Figure 4 — Two planes on Gaussian weights, seen as a 2-level quantizer of |w|. (a) Greedy splits at mean |w| = 0.798 and puts the levels at 0.798 ± 0.483; the class means (hollow) are 0.378 and 1.365. (b) The best 2-plane fit: each level is its class mean and the split lies halfway between them. D(2) is the remaining share of the energy.

2.4Alternating: All Planes Together#

Xu et al. (2018) fit all planes together, alternating two steps that each cannot raise the error:

  1. Fix the signs, solve for α. With the signs of a group as a P × g matrix B, the best α is the least-squares solution , a P × P system per group.
  2. Fix α, choose the signs. Each weight takes the nearest of the 2P grid values, which sets all its P signs. With the grid sorted, this is a comparison against the midpoints between neighbouring values (Xu et al. use a binary search tree).

On the example of the previous section:

  1. Step 1 takes greedy’s signs from Table 1 and gives , the fit of Figure 3 (b).
  2. Step 2 checks each weight against the new grid {−1.4, −0.2, 0.2, 1.4}. Every weight is already nearest to its current value, so no sign changes, and the fit stops after one round.

In general, new α move the grid, some weights end up nearer a different value and change their signs, and the two steps repeat until nothing changes. Starting from the greedy fit, a few rounds suffice. This is Lloyd’s algorithm (k-means in one dimension) restricted to grids of the form .

The price is the prefix property. Alternating fits α for one P. In both cases below, its first plane keeps an larger than mean |w|, so read alone it is a worse 1-bit fit:

Table 2 — Error after reading the first plane and both planes of a 2-plane fit. Example: squared error on . Gaussian: remaining share of the energy on 106 samples (numpy), one α per plane for all of them.
FitExample, 1 planeExample, 2 planesGaussian D, 1 planeGaussian D, 2 planes
Greedy1.100.290.3630.130
Alternating1.460.020.3970.117

03How Fast the Error Falls#

This section follows D(b) as b grows. For a uniform quantizer, one more bit halves the step between levels, and the rounding error, whose variance is proportional to the step squared, falls by 4.

Greedy BCQ falls by 4 per plane only on uniformly distributed weights. There, the residual after a plane is again uniform, at half the width, so every plane solves the same problem at half the scale. On bell-shaped weights it falls more slowly:

Figure 5 — D against planes read, on a log scale, simulated with numpy (4 × 105 samples per distribution, one α per plane for all of them). Blue: greedy fits on Gaussian and on Laplace weights. Teal: alternating fits on Gaussian weights, each depth fitted on its own. Grey: greedy on uniform weights, which divides D by exactly 4 per plane.
  • Gaussian, greedy: 0.363, 0.130, 0.058, 0.033. Planes 2, 3 and 4 divide D by 2.8, 2.2 and 1.8.
  • Laplace, greedy: 0.501, 0.230, 0.143, 0.096, divided by 2.2, 1.6 and 1.5. The heavier the tails, the slower the fall.
  • Gaussian, alternating: 0.363, 0.117, 0.037, 0.012, about 3× per plane. The best possible 16-level quantizer for a Gaussian (Lloyd–Max) leaves 0.0095, so at 4 bits most of greedy’s shortfall comes from its fitting order, not from the grid.

Figure 4 shows the first step of the mechanism. Inside each sign, the residual left by plane 1 is skewed: more weights sit just below than far above it. A symmetric step of ± fits a skewed residual poorly, and each later plane inherits a skewed residual again.

04BCQ Against Uniform Quantization#

A uniform format with an offset, such as Q2_K, writes a weight as with , a scale d and a min m per group. Its four levels are evenly spaced but shifted to fit each group. Two BCQ planes give four levels that are unevenly spaced but centred on 0. Both spend two parameters per group: the uniform format on a step and an offset, BCQ on two steps.

To see which parameter matters more, we fitted each 2-bit grid to Gaussian weights in groups of 16, with exact (unquantized) parameters, so only the shape of the grid differs:

Table 3 — Remaining share of the energy at 2 bits per weight, on 20,000 groups of 16 Gaussian samples (numpy). Greedy BCQ as in the pseudocode above; every other grid fitted by alternating least squares, from the greedy fit or from the group’s min and max.
2-bit gridParameters per groupD(2)
BCQ, greedyα1, α20.114
Even and centred on 0 (BCQ with )one scale0.104
BCQ, alternatingα1, α20.092
Even with an offsetscale, min0.072
BCQ with an offset, alternatingα1, α2, offset0.071
  • The offset is worth more than uneven spacing. Going from an even, centred grid to an uneven one lowers D from 0.104 to 0.092; adding an offset to the even grid lowers it to 0.072.
  • Small groups are where the offset pays. The mean of 16 Gaussian samples carries 1/16 ≈ 6.3% of their energy, close to the offset grid’s whole error of 7.2%. A grid centred on 0 has to cover that shift with its levels; an offset removes it first.
  • BCQ with an offset matches the uniform grid: reaches 0.071, at the cost of a third parameter per group. LUT-GEMM adds such an offset so that its kernel also covers uniform quantization: with and , the four values are exactly .

The last point also means that the bits of a uniform integer, stored most significant bit first, are bit planes of BCQ with an offset. Reading only the top bits of such an integer is a coarser uniform quantization, so it keeps the prefix property too.

05Computing with Bit Planes#

5.1The Planar Layout#

A kernel saves time by reading fewer planes only if each plane is stored on its own. A packed format keeps both bits of a weight in one byte; a planar layout stores plane 1 of a row, then plane 2:

Figure 6 — Eight 2-bit weights in two layouts. Packed, a 1-bit read still loads every byte; in planes, it loads half of them.

5.2Each Plane Is an Add or a Subtract#

The output of a matrix-vector product with BCQ weights is

Suppose row i of W holds the weights of one input for every output j, and α is shared by 16 consecutive outputs. The kernel computes once per plane and group, then adds c to or subtracts it from each of the 16 accumulators, as the 16 sign bits say. No weight needs a multiply.

5.3Lookup Tables#

In the other layout, each output is a dot product along a row, for one plane. LUT-GEMM (Park et al., ICLR 2024) splits x into chunks of μ inputs and computes all signed sums of each chunk once per input vector. Then every μ sign bits of a row form an index into that table:

Figure 7 — A table over μ = 3 inputs. Bit 1 means +1 and bit 0 means −1, so 101 selects . Each row looks up its value instead of adding three inputs.

With μ = 8, a table of 256 sums replaces 8 additions by one lookup. The table is built once per chunk and input vector and is shared by every row and every plane, so its cost is spread over all of them. It needs α to be constant across the μ inputs of a chunk, so groups run along the input dimension.