← phi · docs

Quantization and the code

After pruning, phi can quantize the kept weights to the integers 0 to F(n+2) − 1, write each level as its Zeckendorf digits, which never hold two adjacent 1s, and store a model's levels as a Fibonacci-coded bitstream that a reader splits with no lengths written. This page is phi_prune.encoding, the bitstream half of phi_prune.export, and phi_prune.integrity. The figure runs their arithmetic in the page.

FibonacciEncoder and its levels

FibonacciEncoder(n_digits=8) takes the place values 1, 2, 3, 5, 8 and onward, the first n_digits of them, and builds its levels as every sum of place values no two of which are adjacent. Those sums are every integer from 0 to F(n_digits + 2) − 1, with F(1) = F(2) = 1, so the levels are evenly spaced. It keeps them as grid, their count as n_levels and the highest as max_value.

n_digitsn_levelsmax_valuebits of a fixed-length code
621205
855546
101441438
123773769

to_codeword(level) writes a level greedily from the top place value down, least significant digit first, n_digits long. A greedy Zeckendorf representation never holds two adjacent 1s.

Source: phi_prune/encoding.py. The representation is Zeckendorf's theorem: every positive integer is a unique sum of non-consecutive Fibonacci numbers.

encode_tensor: uniform affine quantization

encode_tensor(tensor, mask=None, *, return_offset=False, axis=None) rounds each kept weight to the nearest level and writes back the value that level stands for, which is fake quantization: the tensor stays float. Only positions where mask is set are touched, or every position when it is None.

  • The offset is the smallest kept weight, which lands on level 0.
  • The scale is max_value / span, levels per unit of weight, the inverse of the quantization step.
  • The level is round((w - offset) * scale), computed as a floor plus a compare of the remainder with 0.5, so a tie goes up in every dtype, and clipped to 0 to max_value. A NaN goes to max_value.
  • The weight written back is level / scale + offset, in the tensor's dtype.
  • Kept weights spanning less than 1e-10 all go to level 0 with a scale of 1.0, and a mask that keeps nothing returns the tensor unchanged with a scale of 1.0.

It returns the quantized tensor, the scale and the RMSE of the kept weights, with the quantization offset as a fourth value when return_offset is set. export_bitstream, save_checkpoint and cassini_check take that scale and quantization offset as a pair per parameter.

With axis=None one scale and one quantization offset serve the whole tensor, which is per-tensor quantization. With axis=0 each output channel gets its own, which is per-channel quantization, and the scale and quantization offset come back as float64 arrays shaped to broadcast against the tensor. On torchvision's ImageNet ResNets per-tensor quantization changed the top-1 class of every probed image for three of the four models, and per-channel kept most of them; Measurements has the run.

levels(tensor, scale, offset, mask=None) inverts the scale-back: it returns the int64 level of each kept weight in row-major order.

The terms are those of Nagel et al., "A White Paper on Neural Network Quantization", sections 2.2 and 2.3.

The Fibonacci code and the bitstream file

phi_prune.gaps.fib_code(n) writes an integer n of at least 1 as its Zeckendorf digits, least significant first, followed by a 1: 1 → 11, 2 → 011, 3 → 0011, 4 → 1011, 5 → 00011. The top digit of a Zeckendorf representation is a 1 and no two of its 1s are adjacent, so the appended 1 makes the first 11 of each codeword, which ends it. No codeword is the start of another, so read_codes splits a stream with no lengths or separators. This is Fibonacci coding, after Apostolico and Fraenkel, 1987.

export_bitstream(model, masks, encoder, scales, path) writes a model's kept weights as one file:

  1. One JSON line: each layer's name, shape, kept count n_active, scale and offset, with scale_shape for per-channel pairs stored flattened, the encoder's n_digits, codeword_digits = n_digits + 1 and level_bias = 1, and payload_bits.
  2. The payload: the Fibonacci code of every kept weight's level plus 1, in parameter order, packed eight bits to a byte with the first bit high and the last byte padded with zeros. The bias of 1 is there because Fibonacci codes start at 1.

It returns path, n_weights, bitstream_length in bits, bits_per_weight and header_bytes. A small level takes fewer bits than a large one, so the bits per weight depend on where the weights fall on the grid, and a fixed-length code of the same levels can be shorter.

load_bitstream(model, masks, path) reads the file back into the kept positions of a model, in place, and returns the header. The header holds no masks, so pass the masks the export used, which save_checkpoint stores. Before it writes any weight it raises ValueError for a header whose codeword_digits and level_bias are not n_digits + 1 and 1, for packed bytes that do not hold payload_bits with zero padding, for a level above the grid, for a count of levels other than the header's, and for a layer missing from the model or from masks or whose shape, mask, kept count or scale_shape does not fit. It also reads a file whose header has no payload_bits and whose payload is the characters 0 and 1.

Source: phi_prune/export/__init__.py and phi_prune/gaps.py.

Checking quantized weights

  • check_tensor_integrity(tensor, mask, encoder, scale, offset) passes a kept weight when it is finite, its level lies in 0 to max_value, and it equals level / scale + offset in the tensor's dtype, the value encode_tensor and load_bitstream write. Noise, a NaN or a flipped bit of a stored float almost never lands on a level, so it fails. It returns passed, failed, total and pass_rate.
  • cassini_check(model, masks, encoder, scales) runs that check on every masked parameter and adds "_aggregate". A masked layer missing from scales raises ValueError.
  • adjacency_check(codeword) is the minimal-form check of one list of Zeckendorf digits: false when two adjacent digits are 1. A single-bit error in stored digits that makes such a pair shows up with no parity bits or CRC. Only a 0 flipped to 1 beside a 1 does that, so the share caught depends on the levels stored.
  • simulate_corruption(tensor, mask, encoder, scale, offset, n_flips=1000) flips one random digit of the digits of a random kept weight's level, n_flips times, and returns how many flips adjacency_check caught and n_flips, or 0 and 0 when the mask keeps nothing.

On synthetic weights the simulated share runs from 0.4116 to 0.5181 at 8 and 10 digits, and over every level it is 0.4091 and 0.4167; Measurements has the table.

Source: phi_prune/integrity.py.

Multiplying by a quantized weight

phi holds no multiplication kernel. A weight written in Zeckendorf digits multiplies an input by adding the input's Fibonacci multiples, one per 1 digit; those multiples are not shifts of the input, as binary partial products are.

Simon, Rey, Levisse, Ansaloni, Zapater and Atienza, DAC 2021, quantize weights to values whose binary form has no two adjacent 1s, and report an approximate multiplier that cuts area by 73% and power-delay product by 43% and computes exactly when one input is such a value. That is a rule on the binary digits of the weight, a different representation from the Zeckendorf digits phi stores, and no circuit cost has been estimated for phi's. Fiandaca and Gomony, 2025, apply the same quantization to neural radio receivers.