Skip to content

feat: refine quant config arguments#379

Open
RuibinCheung wants to merge 2 commits into
mainfrom
dev/zhangrb/refine_quant_config
Open

feat: refine quant config arguments#379
RuibinCheung wants to merge 2 commits into
mainfrom
dev/zhangrb/refine_quant_config

Conversation

@RuibinCheung

Copy link
Copy Markdown
Collaborator

Description

This PR exposes the low-precision (FP8/FP4) scaling strategy to users through the quantization config objects. Previously, key scaling behaviors in the MX GEMM forward and backward passes (such as 2D block weight scaling and randomized Hadamard transform on gradients) were hardcoded inside the operators, which prevented users from tuning the numerical recipe for their own training setups. By surfacing these knobs in Float8QuantConfig and Float4QuantConfig, users can now control the scaling recipe without modifying internal kernel code.

Fixes # (issue)

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refactoring

Changes

Please list the changes introduced in this PR:

  • Added use_2d_block_weight_scaling field to Float8QuantConfig in primus_turbo/pytorch/core/low_precision.py.
  • Added use_2d_block_weight_scaling and use_gradient_rht fields to Float4QuantConfig in primus_turbo/pytorch/core/low_precision.py.
  • Updated FP4GemmMXFunction in primus_turbo/pytorch/ops/gemm_fp4.py to drive the weight 2D-block scaling recipe from config.use_2d_block_weight_scaling and the gradient/weight RHT recipe from config.use_gradient_rht instead of hardcoded values, in both the forward and backward passes.
  • Updated FP8GemmMXFunction in primus_turbo/pytorch/ops/gemm_fp8.py to derive the weight scaling recipe's use_2d_block from the config instead of a hardcoded True.

Checklist:

  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Copilot AI review requested due to automatic review settings June 12, 2026 06:30

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

安全审查完成:未发现本 PR 引入或暴露的中高危安全漏洞。

检查范围包括新增/修改的量化配置字段、gemm_fp4/gemm_fp8 中配置到量化 recipe 的数据流,以及最终进入 C++/Torch 扩展布尔参数的调用路径。未发现可利用的注入、越权、SSRF/XSS/路径遍历、敏感信息泄露、unsafe deserialization 或新增依赖供应链风险。

Open in Web View Automation 

Sent by Cursor Automation: Find vulnerabilities

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR exposes previously hardcoded low-precision (MXFP8/MXFP4) scaling-recipe decisions as user-configurable fields on the quantization config objects, and wires those fields into the FP4/FP8 GEMM operators so users can tune numerical behavior without modifying kernels.

Changes:

  • Added scaling-recipe knobs to Float8QuantConfig / Float4QuantConfig (2D-block weight scaling; FP4 gradient RHT).
  • Updated FP4 GEMM forward/backward to use the config-driven scaling recipes instead of hardcoded values.
  • Updated FP8 MX GEMM to derive use_2d_block from the config instead of hardcoding it.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
primus_turbo/pytorch/core/low_precision.py Adds new config fields controlling scaling recipe behavior.
primus_turbo/pytorch/ops/gemm_fp4.py Threads FP4 weight 2D-block scaling + gradient RHT from config into forward/backward quantization recipes.
primus_turbo/pytorch/ops/gemm_fp8.py Threads FP8 MX weight 2D-block scaling from config into the quantization recipe.
tests/pytorch/ops/test_gemm_fp4.py Updates FP4 GEMM tests to construct configs consistent with the new config-driven recipes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 169 to +172
strategy: ScalingStrategy = ScalingStrategy.DYNAMIC
scale_dtype: ScaleDtype = ScaleDtype.FP32
block_size: Optional[int] = None # Default: not used for tensorwise/rowwise
use_2d_block_weight_scaling: Optional[bool] = None
Comment on lines 196 to 199
block_size: int = 32
use_2d_block_weight_scaling: bool = False
use_gradient_rht: bool = False
use_gradient_sr: bool = False
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