Skip to content

Conversation

@Liu-congo
Copy link
Contributor

Motivation

this pr is a follow-up for #11272 and #11360
in previous pr, i fix the ci failure using input_to_float8, after some further exploration, i find that the failure is caused by the improvement in #5549
the function was first proposed in #5370 ,
in it's raw implementation:it create a "x_s = torch.zeros((1,), dtype=torch.float32, device=x.device)" when being called. x_s is the scaling factor for the input matrix, also the B_scale in bmm_fp8
then in #5549, it introduce a bumpallocator to allocate buffer beforehand to enhance throughput, however it caused the ci failure in test/srt/test_mla_fp8 under cublas12.9

Changes

the lastest fix(#11360) for #11272 is adding version check for cublas12.9, and replace the per_tensor_quant_mla_fp8 with input_to_float8 if needed
now, i deprecated the input_to_float8 function. Substitute the 'zero_allocator.allocate(1)' with 'torch.zeros((1,), dtype=torch.float32, device=x.device)'.
i think the root cause of #11272 lies in cublas, but i'm not quite familiar with that : (, waiting for help to solve the issue throughly.

Minimal Script to reproduce the issue in #11272

the following script can reproduce the issue easily.
a core thing is that if the loop body executes only once, the bmm_fp8 function can work normally, however when it executes more than once, it would throw exception 'RuntimeError: bmm_fp8_internal_cublaslt failed: the requested functionality is not supported'. So far, i think it might have something to do with the pointer of q_nope_scale.

import torch
from sglang.srt.layers.quantization.fp8_kernel import per_tensor_quant_mla_fp8
from sglang.srt.utils import BumpAllocator
from sgl_kernel import bmm_fp8

zero_allocator = BumpAllocator(16, dtype=torch.float32, device='cuda')
for i in range(16):
    q = torch.randn((256, 16, 192)).to(torch.float8_e4m3fn).cuda()
    q_nope, q_pe = q.split(
                    [128, 64], dim=-1
                )
    q_nope_val, q_nope_scale = per_tensor_quant_mla_fp8(
                        q_nope.transpose(0, 1),
                        zero_allocator.allocate(1),
                    )
    print(q_nope_scale.data_ptr(), q_nope_scale)
    w_kc = torch.randn((16, 512, 128)).to(torch.float8_e4m3fn).cuda()
    w_kc = w_kc.transpose(1, 2)
    w_scale = torch.Tensor([0.0009]).to(torch.float32).to('cuda')
    q_nope_out = bmm_fp8(
                    q_nope_val, w_kc, q_nope_scale, w_scale, torch.bfloat16
                )

Tests

Checklist

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Liu-congo, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request provides a targeted fix for a persistent CI failure in the dsv2 model that occurred when using FP8 quantization with cublas12.9. The core issue stemmed from how scaling factors were handled during FP8 matrix multiplications, particularly an interaction with a BumpAllocator introduced in a prior optimization. By deprecating a problematic function and implementing a conditional allocation strategy for the scaling factor, the PR ensures stable operation across different CUDA versions, improving the robustness of the FP8 implementation.

Highlights

  • CI Failure Resolution: This pull request addresses and resolves a recurring CI failure related to FP8 quantization in the dsv2 model, specifically when running with cublas12.9.
  • Deprecation of input_to_float8: The input_to_float8 function, previously used for handling FP8 inputs, has been deprecated and removed from the codebase.
  • Conditional Scaling Factor Allocation: The logic for allocating the scaling factor (q_nope_scale and attn_output_scale) in per_tensor_quant_mla_fp8 has been refined. For cublas12.9 and newer, a new torch.zeros tensor is explicitly created for the scaling factor, replacing the previous input_to_float8 call and addressing issues with the BumpAllocator.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses a bug related to per_tensor_quant_mla_fp8 with newer versions of cublas by conditionally creating a new tensor instead of using a BumpAllocator. The changes look correct and are consistent with the problem description.

I've identified some code duplication that could be refactored to improve maintainability. Please see my detailed comments below.

Comment on lines +1578 to +1585
q_nope_val, q_nope_scale = per_tensor_quant_mla_fp8(
q_nope.transpose(0, 1),
(
torch.zeros((1,), dtype=torch.float32, device=q_nope.device)
if _is_cublas_ge_129
else zero_allocator.allocate(1)
),
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This block of code for quantization is duplicated in forward_absorb_core (lines 1726-1733). To improve maintainability and reduce code duplication, consider extracting this logic into a helper method within the DeepseekV2AttentionMLA class.

For example, you could create a method like this:

    def _quantize_for_bmm_fp8(self, x: torch.Tensor, zero_allocator: BumpAllocator):
        return per_tensor_quant_mla_fp8(
            x.transpose(0, 1),
            (
                torch.zeros((1,), dtype=torch.float32, device=x.device)
                if _is_cublas_ge_129
                else zero_allocator.allocate(1)
            ),
        )

Then you can replace the duplicated blocks with:
q_nope_val, q_nope_scale = self._quantize_for_bmm_fp8(q_nope, zero_allocator)
and
attn_output_val, attn_output_scale = self._quantize_for_bmm_fp8(attn_output, zero_allocator)
respectively.

Comment on lines +1726 to +1733
attn_output_val, attn_output_scale = per_tensor_quant_mla_fp8(
attn_output.transpose(0, 1),
(
torch.zeros((1,), dtype=torch.float32, device=attn_output.device)
if _is_cublas_ge_129
else zero_allocator.allocate(1)
),
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This is a duplicated block of code. As suggested in the comment for forward_absorb_prepare, this could be refactored into a helper method to avoid code duplication.

@Liu-congo
Copy link
Contributor Author

@Fridge003 hello, could you please help to review the pr? many thanks

@Fridge003 Fridge003 self-assigned this Oct 15, 2025
@Fridge003 Fridge003 merged commit be0058b into sgl-project:main Oct 20, 2025
102 of 107 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants