Skip to content

[CPU] Support global scale in qqmm/quantize/dequantize - #4923

Merged
zcbenz merged 2 commits into
ml-explore:mainfrom
Yunt1:fix/4800-cpu-qqmm-global-scale
Nov 24, 2026
Merged

zcbenz merged 2 commits into
ml-explore:mainfrom
Yunt1:fix/4800-cpu-qqmm-global-scale

Conversation

@Yunt1

@Yunt1 Yunt1 commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Fixes #4800.

Summary

  • In QQMatmul::eval_cpu (mlx/backend/cpu/quantized.cpp), handle the optional global_scale_x (inputs[3]) and global_scale_w (inputs[4]) tensors when mode_ == QuantizationMode::Nvfp4 && inputs.size() == 5.
  • Update fp_quantize_dequantize / dispatch_quantize_dequantize to apply scale_enc = (448.0f * 6.0f) / global_scale_x before quantizing group scales to FP8 E4M3 and divide back by scale_enc on dequantization, matching the Metal fp_quantize_dequantize kernel.
  • Thread inv_scale_enc = global_scale_w / (448.0f * 6.0f) into fp_qmm_dispatch (fp_qmm_t_simd and fp_qmm_t) and apply it in float32 before casting to the output dtype to avoid inner-loop overhead and fp16/bf16 underflow.
  • Add a C++ regression test (test qqmm nvfp4 global scale in tests/ops_tests.cpp) covering float32, float16, and bfloat16.

Testing

Built with -DMLX_BUILD_METAL=OFF -DMLX_BUILD_TESTS=ON on macOS arm64:

  • ./build/tests/tests -tc="*qqmm nvfp4*,*quantize*": 2/2 test cases passed (21/21 assertions)
  • ./build/tests/tests -sf="*ops_tests.cpp": 95/95 test cases passed (2632/2632 assertions)

@zcbenz zcbenz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you please rebase the PR on main branch?

@zcbenz zcbenz added the await response This pull request is waiting for response from the author. label Nov 23, 2026
@Yunt1
Yunt1 force-pushed the fix/4800-cpu-qqmm-global-scale branch from 67a9731 to fac202d Compare November 23, 2026 02:12
@zcbenz zcbenz removed the await response This pull request is waiting for response from the author. label Nov 23, 2026

@zcbenz zcbenz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would say the quantize/dequantize should get global scale support first and only then could we properly do tests.

@Yunt1
Yunt1 force-pushed the fix/4800-cpu-qqmm-global-scale branch from fac202d to 06041db Compare November 23, 2026 06:44
@Yunt1

Yunt1 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Rebased on main and updated test qqmm nvfp4 global scale in tests/ops_tests.cpp to use quantize(..., "nvfp4", global_scale) and dequantize(..., "nvfp4", global_scale) directly as the reference (the CPU fallback paths in fp_quantize / fp_dequantize in mlx/ops.cpp already support global_scale). All 96 test cases in ops_tests.cpp pass locally.

@zcbenz
zcbenz force-pushed the fix/4800-cpu-qqmm-global-scale branch from 06041db to a7035b7 Compare November 24, 2026 02:03
@zcbenz zcbenz changed the title [CPU] Apply global_scale_x and global_scale_w in QQMatmul (nvfp4) [CPU] Support global scale in qqmm/quantize/dequantize Nov 24, 2026
@zcbenz
zcbenz merged commit 3fe3cbc into ml-explore:main Nov 24, 2026
24 checks passed
rcfa pushed a commit to cubiculum-ai/mlx that referenced this pull request Nov 26, 2026
Co-authored-by: Yunt1 <Yunt1@users.noreply.github.com>
Co-authored-by: Cheng <git@zcbenz.com>

Ported: Apple's fix for ml-explore/mlx issue 4800 replaces the fork's own,
osaurus-ai/mlx PR 31, which applied the nvfp4 global scales the same way
(g_x in x's round trip, g_w / 2688 on each block scale) with other names
(scale_dec for inv_scale_enc). The fork now has Apple's code.

Kept from the fork: QQMatmul returns first for empty inputs (osaurus-ai/mlx
PR 30), and the fork's tests. "test qqmm with an unquantized weight" now
expects Apple's message, "[QQMatmul] Weights must be quantized.", which the
Highway backend's own QQMatmul now raises too. Removed: the fork's
dequantize_scale(s, scale_dec), now unused. Not taken from the test file:
test_qqmm_matrix_kernels and test_qmv_fast_unaligned_n, context from
ml-explore/mlx PRs the fork does not have.

(cherry picked from commit 3fe3cbc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CPU qqmm ignores global_scale_x and global_scale_w (nvfp4)

2 participants