[Intel HPU] enable tensor_wise_fp8 - #5324
Conversation
|
Thanks for your contribution! |
There was a problem hiding this comment.
Pull request overview
This PR enables tensor-wise FP8 quantization support on Intel HPU (Habana Processing Units). It introduces a measurement mode for calibration, implements HPU-specific FP8 quantization methods for linear and MoE layers, and adds support for FP8 KV cache quantization with SDPA operations.
Key Changes:
- Added measurement mode controlled by
FD_HPU_MEASUREMENT_MODEenvironment variable for collecting activation scales - Implemented
HpuTensorWiseFP8LinearMethodandHpuTensorWiseFP8MoEMethodfor HPU-specific FP8 quantization - Enhanced KV cache quantization with new FP8_E4M3 type and additional scale parameters for Q/S matrices
Reviewed changes
Copilot reviewed 15 out of 15 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| fastdeploy/worker/hpu_model_runner.py | Added measurement mode logic, model conversion updates, and FP8 calibration workflow |
| fastdeploy/platforms/intel_hpu.py | Updated attention backend import path for consistency |
| fastdeploy/model_executor/models/ernie4_5_moe.py | Added weight scale metadata for MoE expert layers |
| fastdeploy/model_executor/layers/quantization/tensor_wise_fp8.py | Added HPU-specific FP8 method routing based on platform detection |
| fastdeploy/model_executor/layers/quantization/kv_cache.py | Extended KV cache with FP8_E4M3 support and additional SDPA scale parameters |
| fastdeploy/model_executor/layers/moe/moe.py | Removed commented code line |
| fastdeploy/model_executor/layers/backends/intel_hpu/quantization/tensor_wise_fp8.py | New file implementing HPU tensor-wise FP8 linear quantization method |
| fastdeploy/model_executor/layers/backends/intel_hpu/quantization/init.py | New initialization file for HPU quantization module |
| fastdeploy/model_executor/layers/backends/intel_hpu/moe/fused_moe_hpu_backend.py | Added FP8 quantization support for HPU MoE with weight processing and measurement mode |
| fastdeploy/model_executor/layers/backends/intel_hpu/moe/init.py | Fixed docstring formatting |
| fastdeploy/model_executor/layers/backends/intel_hpu/attention/hpu_attn_backend.py | Integrated measurement mode and FP8 scale parameters into attention operations |
| fastdeploy/model_executor/layers/backends/intel_hpu/attention/init.py | Simplified module to documentation-only |
| fastdeploy/model_executor/layers/backends/intel_hpu/init.py | Added HpuTensorWiseFP8LinearMethod export |
| fastdeploy/model_executor/forward_meta.py | Added measurement_mode flag to HPUForwardMeta |
| fastdeploy/envs.py | Added FD_HPU_MEASUREMENT_MODE environment variable |
Comments suppressed due to low confidence (1)
fastdeploy/model_executor/layers/quantization/kv_cache.py:1
- The variable name
max_tensoris misleading because the operation being performed ispaddle.min, notpaddle.max. Rename this variable tomin_tensorordescale_tensorto accurately reflect the computation being performed.
"""
| if isinstance(module, Qwen2MLP): | ||
| module.forward = types.MethodType(fused_mlp_forward, module) |
There was a problem hiding this comment.
The measurement_mode attribute is set for Ernie4_5_MLP at line 332, but it is not set for Qwen2MLP before overriding its forward method. This inconsistency may cause the measurement mode logic in fused_mlp_forward to fail for Qwen2MLP. Add module.measurement_mode = measurement_mode before line 335 to ensure consistent behavior across both MLP types.
There was a problem hiding this comment.
measurement mode is only supported/verified for Ernie
| # Take max over all logical_expert_ids (axis=0) | ||
| max_tensor = paddle.min(reciprocal, axis=0) | ||
| return max_tensor.cast(paddle.get_default_dtype()) |
There was a problem hiding this comment.
The variable name max_tensor is misleading because the operation being performed is paddle.min, not paddle.max. Rename this variable to min_tensor or min_descale_tensor to accurately reflect the computation being performed.
| # Take max over all logical_expert_ids (axis=0) | |
| max_tensor = paddle.min(reciprocal, axis=0) | |
| return max_tensor.cast(paddle.get_default_dtype()) | |
| # Take min over all logical_expert_ids (axis=0) | |
| min_tensor = paddle.min(reciprocal, axis=0) | |
| return min_tensor.cast(paddle.get_default_dtype()) |
| self.device_id = device_id | ||
| self.speculative_method = self.fd_config.speculative_config.method | ||
| self.speculative_decoding = self.speculative_method is not None | ||
| # This measurement_mode only works in BF16 mode! |
There was a problem hiding this comment.
This comment indicates an important constraint, but it lacks explanation. Clarify why measurement mode only works in BF16 mode or add a reference to documentation that explains this limitation to help developers understand the restriction.
| # This measurement_mode only works in BF16 mode! | |
| # Measurement mode only works in BF16 mode because HPU hardware and associated libraries (e.g., SynapseAI) provide measurement and profiling features exclusively for BF16 precision. | |
| # For more details, see: https://docs.habana.ai/en/latest/SupportedFrameworks/PaddlePaddle/PaddlePaddle.html#measurement-mode-and-bf16 |
There was a problem hiding this comment.
Comment is not helpful
| act_scale = get_tensor(state_dict.pop(layer.act_scale_key)) | ||
| act_scale = 1.0 / act_scale | ||
|
|
||
| # quant_weight = quant_weight.transpose([1, 0]).contiguous() |
There was a problem hiding this comment.
This commented-out code should either be removed if no longer needed, or uncommented with proper explanation if it's needed for future reference. Leaving unexplained commented code reduces code clarity and maintainability.
| # quant_weight = quant_weight.transpose([1, 0]).contiguous() |
be00d48 to
4a4c5a5
Compare
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## develop #5324 +/- ##
==========================================
Coverage ? 62.11%
==========================================
Files ? 329
Lines ? 41566
Branches ? 6349
==========================================
Hits ? 25819
Misses ? 13805
Partials ? 1942
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
4a4c5a5 to
024af03
Compare
| pre_caches_length: int = 0 | ||
|
|
||
| # | ||
| measurement_mode: bool = False |
There was a problem hiding this comment.
measurement_mode是什么意思呢?
| **extra_weight_attrs, | ||
| }, | ||
| ) | ||
| if "sdpa" in layer.cache_quant_type_str: |
There was a problem hiding this comment.
sdpa是什么意思的量化?除了这里,有文档对其进行解释嘛?
There was a problem hiding this comment.
layer.cache_quant_type_str = "cache_fp8_sdpa_fp8" 意思是 cache 用 fp8量化,sdpa 也用fp8量化计算,也就是QK^T和 SV两个matmul是fp8的。所以除了cache 需要的k和v相关的scale,还需要q和score的相关scale.
|
|
||
| INT8 = "int8" | ||
| FP8 = "float8_e4m3fn" | ||
| FP8_E4M3 = "float8_e4m3" |
There was a problem hiding this comment.
是否考虑将kv_cache.py在hpu backend里新增一份单独维护?
There was a problem hiding this comment.
HPU对KV_cache的改动主要集中在:
不仅仅是对cache本身做量化,在计算SDPA的过程中就做两个低精度矩阵乘。
这导致两点变化:
- 除了k和v需要scale,q和score同样需要scale
- scale的数据类型是float32
另外一点不同是max_bound=240
如果觉得这些改动对目前代码比较大,别的厂家也没有类似需求,kv_cache今后也没有这方面扩展需要,那我们就把它单独拎出来,HPU单独维护一下
024af03 to
aad21bd
Compare
62960f9 to
89dea27
Compare
zoooo0820
left a comment
There was a problem hiding this comment.
这里能否参考 tests/quantization,补充下新增部分的单测;尽量保证CI覆盖率的检测
02ede20 to
5aca689
Compare
|
@zoooo0820 已经添加 |
* [Intel HPU] enable tensor_wise_fp8 * update code based on comments * fix code style issue * fix bug about RP 5138 * mv kv_cache modifications to HPU backend * fix FP8 Precision Issues * fix FP8 Precision Issues * Add quantization UT --------- Co-authored-by: yanfeich <yanfei.cheng@intel.com> Co-authored-by: YuBaoku <49938469+EmmonsCurse@users.noreply.github.com>
* [Intel HPU] enable tensor_wise_fp8 * update code based on comments * fix code style issue * fix bug about RP 5138 * mv kv_cache modifications to HPU backend * fix FP8 Precision Issues * fix FP8 Precision Issues * Add quantization UT --------- Co-authored-by: yanfeich <yanfei.cheng@intel.com> Co-authored-by: YuBaoku <49938469+EmmonsCurse@users.noreply.github.com>
Motivation
Enable tensor_wise_fp8 on HPU
Modifications
fastdeploy/model_executor/layers/backends/intel_hpu/attention/hpu_attn_backend.py
fastdeploy/model_executor/layers/backends/intel_hpu/moe/fused_moe_hpu_backend.py
fastdeploy/model_executor/layers/backends/intel_hpu/quantization/tensor_wise_fp8.py
fastdeploy/model_executor/layers/quantization/kv_cache.py
fastdeploy/model_executor/layers/quantization/tensor_wise_fp8.py
fastdeploy/worker/hpu_model_runner.py
Usage or Command
Accuracy Tests
Checklist
[FDConfig],[APIServer],[Engine],[Scheduler],[PD Disaggregation],[Executor],[Graph Optimization],[Speculative Decoding],[RL],[Models],[Quantization],[Loader],[OP],[KVCache],[DataProcessor],[BugFix],[Docs],[CI],[Optimization],[Feature],[Benchmark],[Others],[XPU],[HPU],[GCU],[DCU],[Iluvatar],[Metax]]pre-commitbefore commit.conducted by local tests
releasebranch, make sure the PR has been submitted to thedevelopbranch, then cherry-pick it to thereleasebranch with the[Cherry-Pick]PR tag.