mlx.core.quantize#
- mlx.core.quantize(w: array, /, group_size: int = 64, bits: int = 4, *, stream: Union[None, Stream, Device] = None) Tuple[array, array, array]#
Quantize the matrix
wusingbitsbits per element.Note, every
group_sizeelements in a row ofware quantized together. Hence, number of columns ofwshould be divisible bygroup_size. In particular, the rows ofware divided into groups of sizegroup_sizewhich are quantized together.Warning
quantizecurrently only supports 2D inputs with dimensions which are multiples of 32Formally, for a group of \(g\) consecutive elements \(w_1\) to \(w_g\) in a row of
wwe compute the quantized representation of each element \(\hat{w_i}\) as follows\[\begin{split}\begin{aligned} \alpha &= \max_i w_i \\ \beta &= \min_i w_i \\ s &= \frac{\alpha - \beta}{2^b - 1} \\ \hat{w_i} &= \textrm{round}\left( \frac{w_i - \beta}{s}\right). \end{aligned}\end{split}\]After the above computation, \(\hat{w_i}\) fits in \(b\) bits and is packed in an unsigned 32-bit integer from the lower to upper bits. For instance, for 4-bit quantization we fit 8 elements in an unsigned 32 bit integer where the 1st element occupies the 4 least significant bits, the 2nd bits 4-7 etc.
In order to be able to dequantize the elements of
wwe also need to save \(s\) and \(\beta\) which are the returnedscalesandbiasesrespectively.- Parameters:
- Returns:
A tuple containing
w_q (array): The quantized version of
wscales (array): The scale to multiply each element with, namely \(s\)
biases (array): The biases to add to each element, namely \(\beta\)
- Return type:
(tuple)