Prhub

#47201 [ROCm][Bugfix] Convert ModelOpt FP8 per-channel weights to e4m3fnuz on MI300/MI325

原始 PR 作者 micah-wil 合并时间 2026-07-07 14:24 文件变更 2 提交数 5 评论 4 代码增减 +11 / -6

执行摘要

修复 ROCm 平台 ModelOpt FP8 权重格式不兼容问题

ROCm CI中ModelOpt FP8测试因权重dtype硬编码为e4m3fn而非平台所需的e4m3fnuz导致ValueError。PR body指出:We are seeing a CI failure due to the modelopt tests hardcoding the e4m3fn fp8 dtype rather than utilizing the appropriate format for the current platform

值得阅读。该PR演示了如何利用平台抽象层(current_platform)和现有工具函数实现跨平台FP8 dtype兼容,是一个简洁的跨平台修复范例。建议关注其他ModelOpt方法是否需类似修复。

讨论亮点

Reviewer fxmarty-amd 建议复用现有的process_fp8_weight_channel_strategy抽象(引自compressed_tensors的使用),替代手动判断is_fp8_fnuz()并调用normalize_e4m3fn_to_e4m3fnuz。作者采纳并修改代码。另外,fxmarty-amd 离线指出其他ModelOpt方法(如ModelOptFp8LinearMethodModelOptFp8PbWoLinearMethod)也可能缺失类似转换,但未在当前PR中处理。

实现拆解

  1. vllm/model_executor/layers/quantization/modelopt.py中导入process_fp8_weight_channel_strategy函数。
  2. 修改ModelOptFp8PcPtLinearMethod.process_weights_after_loading方法,调用该函数处理权重和scale张量,利用其内部自动判断平台FP8格式(对ROCm执行e4m3fn->e4m3fnuz转换)。
  3. tests/quantization/test_modelopt.py中将权重dtype断言从硬编码torch.float8_e4m3fn替换为current_platform.fp8_dtype(),使测试兼容不同平台。
  4. 验证:在MI325上执行test_modelopt_fp8_pc_pt_checkpoint_setup通过。
文件 模块 状态 重要度
vllm/model_executor/layers/quantization/modelopt.py 量化层 modified 5.88
tests/quantization/test_modelopt.py 测试 modified 4.45

关键符号

process_weights_after_loading test_modelopt_fp8_pc_pt_checkpoint_setup

关键源码片段

vllm/model_executor/layers/quantization/modelopt.py data-contract

核心修复文件,修改 process_weights_after_loading 方法以支持 ROCm 平台的 e4m3fnuz 格式,导入并调用 process_fp8_weight_channel_strategy。

def process_weights_after_loading(self, layer: torch.nn.Module) -> None:
    # 利用现有的 process_fp8_weight_channel_strategy 统一处理权重格式转换
    # 在 ROCm 上会自动将 e4m3fn 转换为 e4m3fnuz
    weight, weight_scale, _ = process_fp8_weight_channel_strategy(
        layer.weight, layer.weight_scale.data
    )
    layer.weight = Parameter(weight.t(), requires_grad=False)
    layer.weight_scale = Parameter(weight_scale, requires_grad=False)
    self.fp8_linear.process_weights_after_loading(layer)

评论区精华

复用 process_fp8_weight_channel_strategy 替代手动转换 设计

fxmarty-amd 建议使用现有的 process_fp8_weight_channel_strategy 抽象,而不是在方法内手动判断 is_fp8_fnuz() 并调用 normalize_e4m3fn_to_e4m3fnuz。他引用了 compressed_tensors 中的使用示例。

结论:作者采纳建议,修改代码使用 process_fp8_weight_channel_strategy,并在之后更新提交。 · 已解决

风险与影响

风险较低。核心变更仅影响ModelOptFp8PcPtLinearMethod,复用已有抽象确保正确性。但其他ModelOpt方法(ModelOptFp8LinearMethodModelOptFp8PbWoLinearMethod)可能仍存在dtype不兼容问题,待后续覆盖。测试仅限于checkpoint加载和权重属性检查,未包含端到端推理准确性验证。对NVIDIA平台无副作用。

对ROCm用户:修复了ModelOpt FP8 per-channel per-token量化在MI300/MI325上的CI失败,使该量化路径在AMD GPU上可正常运行。对NVIDIA用户无影响,因为current_platform.fp8_dtype()返回torch.float8_e4m3fn,行为完全一致。变更影响范围小,仅涉及量化权重加载阶段。

其他 ModelOpt 方法可能缺失类似修正

关联 Issue

未识别关联 Issue

当前没有检测到明确关联的 Issue 链接,后续同步到相关引用后会出现在这里。

完整报告

参与讨论