Draft
[WIP][New operators] MACAQ W4A4 fused dense and grouped matmul for Ascend 950 #12568
Mocchibird Add on 20 days ago
[WIP][New operators] MACAQ W4A4 fused dense and grouped matmul for Ascend 950 #12568
Draft
Mocchibird Add on 20 days ago
Mocchibird
Mocchibird
20 days ago

MACAQ: better W4A4 accuracy with a fused inference path

Project repository: huawei-csl/MACaQ.

Add dense and grouped MACAQ W4A4 matmul for Ascend 950 using PTO-ISA through the ascend_ops_nn PyTorch extension.
MACAQ (Model Agnostic Calibrated Quantization) applies channel scaling,
block-32 BF16 Hadamard rotation, MXFP4 activation quantization and matrix
multiplication in one mixed Vector/Cube kernel, producing BF16 output.
Calibration, weight transformation (W / D) @ H and Flip-Refinement are offline.

The key result is better quantization accuracy with competitive small-batch
kernel latency
. The online accuracy transform is included in every MACAQ
timing below. Native CANN quantization and matmul are unchanged independent
baselines. Large-prefill and grouped losses remain visible.

Accuracy results

Model perplexity: 23.7% lower than ordinary W4A4

Qwen3-0.6B on held-out WikiText-2; lower is better:

Model reference Perplexity
BF16 21.3111
Ordinary MXFP4 W4A4, scaling algorithm 0 33.4286
MACAQ W4A4 25.4962

This is a CPU model-reference pilot, using 16,384 held-out tokens
(16,368 scored predictions, context 1,024). All 196 decoder linear projections
are quantized; embeddings and the LM head remain BF16. Calibration uses
1,024 training tokens, with no held-out hyperparameter tuning. It is not
full-dataset perplexity or full-model NPU execution. Algorithm 2 model
perplexity has not been measured.

MACAQ perplexity compared with ordinary W4A4 and BF16

Actual NPU outputs: lower error on all nine fixed projections

MACAQ lowers normalized mean squared error against both native FP4 scaling
presets
on q/up/down projections at layers 0, 14 and 27. Native error is
1.58–4.61× MACAQ error for algorithm 0 and
1.68–4.14× for algorithm 2.
The measured ops-nn, TileLang regular and TileLang prepared outputs agree
exactly with the MACAQ CPU reference on all nine fixtures. Evaluation uses
1,024 separate held-out test tokens. Unsupported ops-nn dimensions are
zero-padded for accuracy coverage only, then sliced; those calls make no latency claim.

Actual NPU projection error for MACAQ and both native scaling presets

Accuracy protocol, all projection rows, grouped arithmetic checks and raw data.
Runtime and accuracy are separate experiments; this is not a joint
model-level speed-and-accuracy result.

Latency against existing native CANN operators

Baseline: npu_dynamic_mx_quant (block32 E2M1, round-to-even, scale_alg=0)
followed by npu_quant_matmul, or npu_grouped_matmul for grouped workloads.
MACAQ additionally performs scaling and Hadamard rotation. Each numerical
function is checked against its own CPU reference. Our split MACAQ paths
are ablations, not existing external baselines.

Native latency / MACAQ latency; above 1 favors MACAQ. Geometric means
give every configuration equal weight: all 24 (K,N) pairs at dense
M=16/128/4096 plus four-expert grouped workloads, for 96 cases in total.
Win counts compare medians and do not establish statistical significance.

Workload Native / MACAQ Median wins
Dense M=16 1.507× 24/24
Dense M=128 1.412× 21/24
Dense M=4096 0.831× 1/24
Grouped E=4 1.001× 11/24
All 96, equal-case GM 1.154× 57/96

Both small-batch groups are faster on the geometric mean, while the grouped mean is near parity. One call per configuration has total native/MACAQ time ratio 0.930×; MACAQ is slower in that mixture because larger cases contribute more time.

MACAQ latency by workload, including all-case geometric mean

The accuracy gain comes without a latency penalty on the faster measured
workloads. It is not free for every shape: large prefill and many grouped
cases still lose. No end-to-end NPU throughput gain is claimed.

Complete protocol, all 96 rows, heatmap and raw samples.

Timing protocol and current-source validation

Ascend 950, CANN 9.3 weekly, PyTorch 2.9.0 / torch_npu 2.9.0.post2,
native TileLang 0.1.15. Each case uses 20 warmups and 15 rotated trials of
20 eager calls. Two distinct activation buffers alternate. Outputs and all
intermediate allocations count inside timed calls; compilation, calibration
and static weight/layout preparation are excluded. No explicit cache flush.
These are repeated API timings, not single-request latency or model throughput.
Within-run bootstrap intervals and an earlier equivalent TileLang sweep are
available in the reports; they do not cover systematic or across-run uncertainty.

  • All 96 timed configurations pass 384 complete output checks at BF16 rtol=atol=1/128.
  • The existing 13 device cases, two signed-D/in-place/stream regressions and three exact maximum-row checks pass, including M=65,536.
  • Raw D preparation is fused in UB; producer runahead is bounded at eight slabs for safe large-row handoffs.
  • Source hashes and the installed wheel SHA256 are recorded in the timing metadata. The measured kernel sources are unchanged by this results update.
  • A fresh five-file OAT scan passed. The unchanged two-device context guard was not rerun for this update.
  • Dense accuracy checks: 36 exact CPU-reference matches, plus nine native algorithm-2 decoding checks.
  • Grouped arithmetic: 24 exact checks on three real down projections with four replicated expert banks. This is not a MoE-model quality evaluation or runtime measurement.
  • Supported-stack CI remains pending; earlier validation reported a driver-version mismatch.

Related implementation: TileLang PR #44.

AI/Agent disclosure

likedislike
Merge blocked
Mocchibird Mocchibird
20 days ago Created pull request, commit 1054f61e
CANN-robot
CANN-robotCollaborators
20 days ago Comments:

Hi @Mocchibird, welcome to submitting your first PR to CANN!

PR Merge Steps

1. CLA Signing

If the current PR label includes cann-cla/yes, it means you have signed the CLA and can proceed to the next step. If the label includes cann-cla/no, please sign the CLA first. If you have any questions, please refer to the FAQ.

2. CI Check

Please comment /compile to trigger the CI pipeline check. If the CI run is successful, the PR will be tagged with ci-pipeline-passed and you can proceed to the next step. If the CI run fails, the PR will be tagged with ci-pipeline-failed, please check the CI logs to fix the issues in the PR. If you have any questions, please refer to the FAQ.

3. Code Review

After CI passes, please refer to the PR Approval Progress and proactively @ the committers in the table to review the code. After approval, committers will comment /lgtm and /approve. Once the lgtm and approved labels are successfully added, the PR will be merged automatically.

likedislike
CANN-robotCANN-robotCollaborators
20 days ago Added label: cann-cla/no
CANN-robotCANN-robotCollaborators
20 days ago Added label: stat/needs-squash
CANN-robot
CANN-robotCollaborators
20 days ago Comments:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here.
You can get sig-info at here.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
For more, you also can visit HICANN.


PR Approval Progress

⚠️ This PR does not yet meet the following requirements:lgtm (requires ≥ 2 person(s) per module)、approve (requires ≥ 1 person(s) per module)

Module Approval Details

module lgtm status approve status
experimental ❌ (0/2)(You can also ask: 石向阳, 陈昊文, 沈承宗, 杨东, 高成) ❌ (0/1)(You can also ask: 王子韬, 柳宗谷, 刘闯, 钱泽洪, 余鑫)
scripts ❌ (0/2)(You can also ask: 杨阳, 汤平川, 范其瑞, 胡碧霞, 商晓波) ❌ (0/1)(You can also ask: 胡碧霞, 杨阳, 商晓波, 林鹏翔, 范其瑞)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

Mocchibird, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
87 messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
6 days ago Removed label: ci-pipeline-failed
CANN-robotCANN-robotCollaborators
6 days ago Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
6 days ago Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
6 days ago Added label: ci-pipeline-passed
Mocchibird Mocchibird
5 hours ago Modify description of pull request