mojolearn.lowbit#
Low-bit weight storage for the inference classes, with float32 arithmetic.
A model’s 2-D weight matrices can be stored as bfloat16 bits or as int8 codes with
one power-of-two exponent per row, half or a quarter of the float32 size on disk and
in memory. An inference class built from packed weights first converts them back to
float32 exactly (bfloat16 by widening, int8 as codes * 2**exponent) and then runs
its normal float32 path, so it computes the same bits as the float32 block given the
converted weights. Packed weights are rounded weights: the model is not the original
float32 model, and the arithmetic is not lower precision.
pack converts every 2-D tensor of a weight dict, selected by rank, not by name
(so a Mamba-1 A_log of shape (d_inner, 16) is packed too). 1-D and 3-D tensors
(norm weights, biases, convolution taps, D) stay float32. Use pack_one to pack
selected tensors by name. unpack (also materialize) returns the float32 dict and
its format; unpack(pack(unpack(p)[0], fmt)) equals unpack(p).
Conversion runs on the GPU on a GPU install and on the host binding on a CPU-only
install; an install without the linalg extension raises an error. NumPy is not
required: packed tensors are mojolearn.Array objects and inputs can be any buffer.
Examples
>>> import mojolearn
>>> packed = mojolearn.lowbit.pack(weights, "bfloat16") # or "int8"
>>> blk = mojolearn.TransformerBlock(packed, n_heads=8)
>>> f32, fmt = mojolearn.lowbit.unpack(packed)
Functions
|
Pack every 2-D tensor of a weight dict to |
|
Convert every packed tensor of a weight dict to float32, exactly. |
|
Convert every packed tensor of a weight dict to float32, exactly. |
|
The format shared by the packed tensors of |
|
Whether |
|
Pack one 2-D float32 tensor to |
|
A packed tensor converted to float32 exactly, or a float32 tensor returned as is. |
|
Convert bfloat16 bits of any rank (a uint16 buffer) to float32, exactly. |
Classes
|
A 2-D weight stored as bfloat16 bits. |
|
A 2-D weight stored as int8 codes with one int32 exponent per row: |
- class mojolearn.lowbit.BF16Weight(bits)[source]#
Bases:
objectA 2-D weight stored as bfloat16 bits.
- Parameters:
bits (2-D uint16 buffer) – bfloat16 bits, for example from
mojolearn.linalg.to_bf16. Anything else raisesTypeError.- Variables:
bits (mojolearn.Array of uint16)
format (str) –
'bfloat16'.
- class mojolearn.lowbit.Int8Weight(codes, exponents)[source]#
Bases:
objectA 2-D weight stored as int8 codes with one int32 exponent per row:
row = codes * 2**exponent.- Parameters:
codes (2-D int8 buffer)
exponents (int32 buffer of shape (rows,)) – Wrong dtypes or shapes raise
TypeError.
- Variables:
codes (mojolearn.Array of int8)
exponents (mojolearn.Array of int32)
format (str) –
'int8'.
- mojolearn.lowbit.pack(weights, fmt)[source]#
Pack every 2-D tensor of a weight dict to
fmt; other tensors pass through unchanged.- Parameters:
weights (dict) – Tensors by name. Anything but a dict raises
TypeError.fmt ({‘float32’, ‘bfloat16’, ‘int8’}) – Target format; unknown formats raise
ValueError.
- Returns:
dict – A new dict.
- mojolearn.lowbit.unpack(weights, what='weights')[source]#
Convert every packed tensor of a weight dict to float32, exactly.
- Parameters:
weights (dict) – Tensors by name; other tensors pass through unchanged.
what (str, default ‘weights’) – Name used in error messages.
- Returns:
(dict, str) – The float32 dict and the format the weights were stored in.
- mojolearn.lowbit.materialize(weights, what='weights')#
Convert every packed tensor of a weight dict to float32, exactly.
- Parameters:
weights (dict) – Tensors by name; other tensors pass through unchanged.
what (str, default ‘weights’) – Name used in error messages.
- Returns:
(dict, str) – The float32 dict and the format the weights were stored in.
- mojolearn.lowbit.format_of(weights)[source]#
The format shared by the packed tensors of
weights, or'float32'when none is packed.Raises
ValueErrorwhen the dict mixes formats.
- mojolearn.lowbit.is_packed(value)[source]#
Whether
valueis a packed weight (BF16WeightorInt8Weight).
- mojolearn.lowbit.pack_one(value, fmt, name='weight')[source]#
Pack one 2-D float32 tensor to
fmt.- Parameters:
value (2-D float32 buffer)
fmt ({‘float32’, ‘bfloat16’, ‘int8’}) –
'float32'returns a float32 copy. Unknown formats and non-2-D tensors raiseValueError.name (str, default ‘weight’) – Name used in error messages.
- Returns:
BF16Weight, Int8Weight or mojolearn.Array