diff --git a/python/sglang/srt/layers/quantization/fp8_utils.py b/python/sglang/srt/layers/quantization/fp8_utils.py index ab7d91b27..2c8339497 100755 --- a/python/sglang/srt/layers/quantization/fp8_utils.py +++ b/python/sglang/srt/layers/quantization/fp8_utils.py @@ -1499,7 +1499,12 @@ def per_block_cast_to_fp8(x: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: # COPIED FROM DeepGEMM def ceil_to_ue8m0(x: torch.Tensor): - return torch.pow(2.0, torch.ceil(torch.log2(x.abs()))) + bits = x.abs().float().view(torch.int32) + exp = (bits >> 23) & 0xFF + mantissa = bits & 0x7FFFFF + exp = exp + (mantissa != 0).to(torch.int32) + exp = exp.clamp(1, 254) + return (exp << 23).view(torch.float32) def channel_quant_to_tensor_quant( diff --git a/test/manual/quant/test_block_fp8_deep_gemm_blackwell.py b/test/manual/quant/test_block_fp8_deep_gemm_blackwell.py index 833c23e7c..1d138ee41 100644 --- a/test/manual/quant/test_block_fp8_deep_gemm_blackwell.py +++ b/test/manual/quant/test_block_fp8_deep_gemm_blackwell.py @@ -46,7 +46,12 @@ def per_block_quant_fp8(x: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: def ceil_to_ue8m0(x: torch.Tensor): assert x.view(-1).amax().item() > 0 - return torch.pow(2.0, torch.ceil(torch.log2(x.abs()))) + bits = x.abs().float().view(torch.int32) + exp = (bits >> 23) & 0xFF + mantissa = bits & 0x7FFFFF + exp = exp + (mantissa != 0).to(torch.int32) + exp = exp.clamp(1, 254) + return (exp << 23).view(torch.float32) def per_token_group_quant_mxfp8(x: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: