shard_cast_utils#

Shared primitives for streaming checkpoint casts and export.

Functions

build_w13_amax_overrides

Return one shared weight amax for each fused w1/w3 pair.

build_w13_kmax_overrides

Return one shared E8M0 maximum exponent for each fused w1/w3 pair.

dequantize_fp8_ue8m0_to_bf16

Decode a native DSV4 FP8 matrix with square UE8M0 blocks into BF16.

dequantize_mxfp4_to_bf16

Dequantize packed MXFP4 weights and E8M0 scales to BF16.

mxfp4_kmax

Return the largest non-zero unbiased exponent in an E8M0 scale tensor.

quantize_bf16_to_fp8_ue8m0

Encode a finite BF16 matrix as native DSV4 FP8 weights and uint8 UE8M0 scales.

quantize_mxfp4_to_nvfp4

Dequantize MXFP4 and requantize it to NVFP4 using an optional global amax.

quantize_mxfp4_to_nvfp4_lossless

Closed-form MXFP4-to-NVFP4 cast with lossless-block accounting.

build_w13_amax_overrides(expert_bases, get_amax)#

Return one shared weight amax for each fused w1/w3 pair.

Parameters:
  • expert_bases (list[str])

  • get_amax (Callable[[str], Tensor])

Return type:

dict[str, Tensor]

build_w13_kmax_overrides(expert_bases, get_scale, device)#

Return one shared E8M0 maximum exponent for each fused w1/w3 pair.

Parameters:
  • expert_bases (list[str])

  • get_scale (Callable[[str], Tensor])

  • device (str)

Return type:

dict[str, int]

dequantize_fp8_ue8m0_to_bf16(weight, scale, block=128, *, validate_scales=True)#

Decode a native DSV4 FP8 matrix with square UE8M0 blocks into BF16.

Uses square blocks of size block, including partial trailing blocks. Scales are uint8/int8 exponent bytes or float8_e8m0fnu, with no NaN scale codes. Set validate_scales=False only for immutable scales already checked at load time, to avoid synchronizing CUDA on repeated calls. Shape and dtype checks always run.

Parameters:
  • weight (Tensor)

  • scale (Tensor)

  • block (int)

  • validate_scales (bool)

Return type:

Tensor

dequantize_mxfp4_to_bf16(mxfp4_weight, mxfp4_scale, device)#

Dequantize packed MXFP4 weights and E8M0 scales to BF16.

Parameters:
  • mxfp4_weight (Tensor)

  • mxfp4_scale (Tensor)

  • device (str)

Return type:

Tensor

mxfp4_kmax(mxfp4_scale, device='cpu')#

Return the largest non-zero unbiased exponent in an E8M0 scale tensor.

Parameters:
  • mxfp4_scale (Tensor)

  • device (str)

Return type:

int

quantize_bf16_to_fp8_ue8m0(weight, block=128)#

Encode a finite BF16 matrix as native DSV4 FP8 weights and uint8 UE8M0 scales.

Uses square blocks of size block and recomputes scales for the trained weights. Rejects values whose FP8 rounding would decode to nonfinite BF16.

Parameters:
  • weight (Tensor)

  • block (int)

Return type:

tuple[Tensor, Tensor]

quantize_mxfp4_to_nvfp4(mxfp4_weight, mxfp4_scale, weight_amax, device)#

Dequantize MXFP4 and requantize it to NVFP4 using an optional global amax.

Parameters:
  • mxfp4_weight (Tensor)

  • mxfp4_scale (Tensor)

  • weight_amax (Tensor | None)

  • device (str)

Return type:

tuple[Tensor, Tensor, Tensor, bool]

quantize_mxfp4_to_nvfp4_lossless(mxfp4_weight, mxfp4_scale, k_max, device)#

Closed-form MXFP4-to-NVFP4 cast with lossless-block accounting.

Parameters:
  • mxfp4_weight (Tensor)

  • mxfp4_scale (Tensor)

  • k_max (int)

  • device (str)

Return type:

tuple[Tensor, Tensor, Tensor, int, int]