Skip to content

vllm.model_executor.layers.quantization.utils.marlin_utils_fp4

Functions:

_nvfp4_compute_scale_factor(marlin_scales, a_dtype=None)

Compute the power-of-2 scale_factor needed so that all non-zero values in marlin_scales * 2^7 are >= 2 after rescaling. Returns a Python float (power of 2, >= 1.0).

_repack_marlin_experts(weight, size_n, size_k)

Repack each expert to marlin format into a preallocated output.

nvfp4_marlin_process_scales(marlin_scales, scale_factor=None, a_dtype=None)

Process NVFP4 weight scales into the special S0E5M3 format for Marlin.

Parameters:

  • marlin_scales

    (Tensor) –

    Weight scales tensor in half precision, already permuted for the Marlin kernel layout.

  • scale_factor

    (float | None, default: None ) –

    Optional power-of-2 rescaling factor. If None, the factor is computed automatically so that every non-zero scale satisfies scale * 2^7 >= 2 (i.e., the MSB of the S0E5M3 representation is always 1). When provided (e.g., for MoE layers where all experts must share the same factor), the given value is used directly. The caller is responsible for dividing global_scale by the returned scale_factor to preserve numerical correctness.

  • a_dtype

    (dtype | None, default: None ) –

    dtype of the activations the scales will be used with. None keeps the dtype of marlin_scales.

Returns:

prepare_moe_mxfp4_layer_for_marlin(layer, w13, w2, w13_scale, w2_scale, w13_bias, w2_bias)

Pure-function version of prepare_moe_fp4_layer_for_marlin for MXFP4.

Takes weight tensors as inputs and returns transformed tensors. Does NOT modify the layer in-place.