mstar.utils.fp8_block#

FP8 block-scale checkpoints: the quantization_config they declare, and their weights dequantized; the scale layout of cuBLASLt’s block-scaled GEMMs (MXFP8, NVFP4).

A block-scale checkpoint (DeepSeek-V3’s format, GLM-5.x’s) stores each linear weight as e4m3 with one fp32 weight_scale_inv per weight_block_size block. Triton-free, so a model’s config can import it on a CPU-only install; the activation quantizer that feeds the block-scale GEMMs is quant_fp8.

Functions

dequant_fp8_block_stream(weights, quant_config)

Pair fp8 .weight keys with .weight_scale_inv and dequantize.

dequantize_fp8_block_weight(weight_fp8, ...)

Dequantize one 2-D fp8 weight: w[i, j] * scale_inv[i//bo, j//bi].

swizzle_block_scales(scales)

[rows, k_blocks] -> the flat 32x4x4 layout of the cuBLAS block scaling factors: 128x4 tiles of 512 elements, row r, block j of a tile at (r % 32) * 16 + (r // 32) * 4 + j.

swizzled_scale_elements(rows, k_blocks)

Length of the flat scales of [rows, k_blocks]: both padded to whole tiles.

unswizzle_block_scales(flat, rows, k_blocks)

The inverse of swizzle_block_scales(), padding dropped.

Classes

BlockQuant(data, scale[, tensor_scale])

One block-scaled matrix [rows, K // packing].

Fp8BlockQuantConfig([weight_block_size, ...])

The fields of an HF fp8 quantization_config a block-scale checkpoint carries.

class mstar.utils.fp8_block.BlockQuant(data, scale, tensor_scale=None)[source]#

Bases: NamedTuple

One block-scaled matrix [rows, K // packing]. scale is flat in the GEMM’s swizzled layout; tensor_scale is NVFP4’s second level, a [1] fp32 (None under MXFP8).

Parameters:
data: Tensor#

Alias for field number 0

scale: Tensor#

Alias for field number 1

tensor_scale: Tensor | None#

Alias for field number 2

class mstar.utils.fp8_block.Fp8BlockQuantConfig(weight_block_size=(128, 128), fmt='e4m3', activation_scheme='dynamic', ignore=<factory>)[source]#

Bases: object

The fields of an HF fp8 quantization_config a block-scale checkpoint carries.

Parameters:
activation_scheme: str = 'dynamic'#
fmt: str = 'e4m3'#
classmethod from_hf_config_dict(quant)[source]#
Parameters:

quant (dict | None)

Return type:

Fp8BlockQuantConfig | None

ignore: tuple[str, ...]#
weight_block_size: tuple[int, int] = (128, 128)#
mstar.utils.fp8_block.dequant_fp8_block_stream(weights, quant_config, out_dtype=torch.bfloat16, keep_fp8=None)[source]#

Pair fp8 .weight keys with .weight_scale_inv and dequantize.

Parameters:
Return type:

Iterator[tuple[str, Tensor]]

mstar.utils.fp8_block.dequantize_fp8_block_weight(weight_fp8, scale_inv, block_size=(128, 128), out_dtype=torch.bfloat16)[source]#

Dequantize one 2-D fp8 weight: w[i, j] * scale_inv[i//bo, j//bi].

Parameters:
Return type:

Tensor

mstar.utils.fp8_block.swizzle_block_scales(scales)[source]#

[rows, k_blocks] -> the flat 32x4x4 layout of the cuBLAS block scaling factors: 128x4 tiles of 512 elements, row r, block j of a tile at (r % 32) * 16 + (r // 32) * 4 + j.

Parameters:

scales (Tensor)

Return type:

Tensor

mstar.utils.fp8_block.swizzled_scale_elements(rows, k_blocks)[source]#

Length of the flat scales of [rows, k_blocks]: both padded to whole tiles.

Parameters:
Return type:

int

mstar.utils.fp8_block.unswizzle_block_scales(flat, rows, k_blocks)[source]#

The inverse of swizzle_block_scales(), padding dropped.

Parameters:
Return type:

Tensor