Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
Back to tool/Back to issues
#790·laya

export_onnx --quantize: per-channel dynamic INT8 collapses the model (32% agreement with eager); per-tensor is far better but int8 is lossy overall

Author: AlKor13Created Oct 1, 2026Updated Oct 1, 2026

Summary

scripts/export_onnx.py --quantize defaults to per-channel dynamic INT8 (quantize_model(..., per_channel=True)). On the real checkpoints this collapses the decision model: the int8 logits flatten to a near-constant and the majority of decisions flip versus the fp32/eager model. quantize_model's docstring claims the opposite ("per-channel flipped none (max 0.09)"), and the unit test can't catch it because it runs on a toy graph.

Measured on convaiinnovations/laya (English, ModernBERT-large) and convaiinnovations/laya-multilingual (mmBERT-base), CPU, torch==2.14.1+cpu, onnxruntime==1.30.0, onnx==1.23.1, the current dynamo exporter (opset 18).

Evidence

English checkpoint — agreement with the eager PyTorch Agent over 96 held-out choice decisions (24 support-ticket states × 4 choice questions with 2/3/6/12 options), plus max probability drift:

variant choice agreement max prob drift
ONNX fp32 (reference) 96/96 (100%) 0.000
dynamic per-channel — shipped default 31/96 (32%) 0.92
dynamic per-tensor 64/96 (67%) 1.00
static QDQ MinMax per-tensor 43/96 (45%) 0.92
static QDQ MinMax per-channel 30/96 (31%) 0.93
static QDQ Percentile / Entropy fail at inference (degenerate scale / Where+LayerNorm runtime error) —

Multilingual checkpoint — 48 decisions: fp32 100%, dynamic per-tensor 83%, dynamic per-channel 40%.

A representative collapsed output (per-channel, English): for a clear "checkout 500 errors" ticket the 3-way topic choice comes back billing 0.332 / bug 0.336 / account 0.332 — essentially uniform; eager picks bug decisively.

  • The fp32 ONNX export is faithful (100%, zero drift) — this is purely a quantization problem, not an export problem.
  • Keeping the small decision head in fp32 (nodes_to_exclude) does not help (67%→66%, 32%→25%): the loss is in the encoder, consistent with activation outliers that a per-tensor dynamic scale handles poorly and a per-channel weight scale on the dynamic MatMulInteger path handles worse.
  • Speed/size are real wins: int8 ≈ 2× faster than eager / 1.8× faster than fp32 on CPU; 2.8× smaller (ModernBERT-large), 1.4× (mmBERT, the large multilingual embedding stays fp32). So the feature is worth keeping — it just needs an honest default and honest docs.

Root cause

per_channel=True is meant for the static/QDQ path. quantize_dynamic emits MatMulInteger + DynamicQuantizeLinear, where the activation scale is computed per-input at run time; a per-channel weight scale does not combine correctly with that per-tensor activation scale in the int32→float rescale, so the output collapses. Per-tensor is dramatically better on the same graph.

The max 0.09 / 0 flips figure in the docstring was almost certainly measured on the previous TorchScript exporter / older onnxruntime, before the switch to the dynamo exporter — it no longer reproduces.

Why CI didn't catch it

tests/test_onnx_quantize.py quantizes a hand-built 8×8 near-identity MatMul and checks closeness with rtol=atol=0.05. A near-identity weight has no outlier structure, so per-channel is fine there — the toy graph cannot exhibit the real-model collapse. The test even asserts "scales are per output channel (per-tensor flips decisions on the real model)", encoding the belief this issue disproves.

Proposed fix (PR to follow)

  1. Default quantize_model(..., per_channel=False) — a strict improvement over the broken default (32%→67% English, 40%→83% multilingual).
  2. Rewrite the docstring to state the measured trade-off honestly (int8 trades real accuracy for ~2× speed / ~3× size; not recommended where calibrated confidence matters) and drop the "0 flips / 0.09" claim.
  3. Expose --per-channel on the CLI (and the per_channel arg) so power users can opt back in knowingly.
  4. Update test_onnx_quantize.py to the new default, and add a note that a real-weight parity guard (agreement with eager ≥ threshold) belongs in the weighted/scheduled lane, since the toy graph can't catch this class of regression.

Accuracy-safe int8 for this model would need QAT or SmoothQuant-style outlier handling — out of scope for an export-time flag, but worth a note in the docs.

Happy to send the PR (fix + docstring + CLI opt-in + test update).

Source: NandhaKishorM/laya

View original on GitHubView discussion on GitHub