Skip to content

fix(metrics): correct params & MACs for quantized models#7

Closed
nathanhubens wants to merge 1 commit into
fix/quantized-benchmark-segfaultfrom
fix/quantized-metrics
Closed

fix(metrics): correct params & MACs for quantized models#7
nathanhubens wants to merge 1 commit into
fix/quantized-benchmark-segfaultfrom
fix/quantized-metrics

Conversation

@nathanhubens

Copy link
Copy Markdown
Contributor

Summary

fasterbench's param-count and MAC metrics were silently wrong for quantized modelsget_num_parameters returned 0 (packed int8 weights aren't in .parameters()) and thop-based MACs read ≈0 (no quantized-op handlers).

Stacked on #6 (reuses _is_quantized).

The conceptual fix

Quantization does not change the number of params or MACs — an int8 conv has the same weight count and multiply-accumulate count as its fp32 twin; only bytes and op-cost drop. So the counts should come out ≈ unchanged, and only model size (bytes) should shrink.

  • get_num_parameters — when _is_quantized, also sum the numel of quantized modules' callable .weight()/.bias() accessors.
  • compute_compute — route quantized models through a forward-hook MAC counter (handles quantized + fp Conv2d/Linear); the thop path is untouched for fp models.

Measured (before → after)

metric fp32 quant BEFORE quant AFTER
params 5220 0 5220 ✓ (1.000×)
MACs (M) 1.299 0.008 ❌ 1.29 ✓ (0.99×)
disk (MiB) 0.024 0.017 ✓ (int8 shrink)

Why it matters

The optimize(..., report=True) before/after table benchmarks the optimized model — which for the default size/speed recipe is quantized. Without this, it would print misleading params/MACs → 0 (−100%) rows. nbdev-test green.

Quantized weights live in packed params (_packed_params), not .parameters(), so
get_num_parameters returned 0; thop has no quantized-op handlers so MACs read ~0.
But quantization keeps the SAME param & MAC counts (int8 vs fp32) -- only bytes and
op-cost drop. Now correct:
- get_num_parameters: when _is_quantized, add the numel of quantized modules'
  callable .weight()/.bias() accessors -> params(quant) == params(fp32).
- compute_compute: route quantized models through a forward-hook MAC counter
  (handles quantized + fp Conv2d/Linear); the thop path is unchanged for fp models.
Measured: params 0 -> 5220 (= fp32); MACs 0.008 -> 1.29M (0.99x fp32); disk still
drops (int8). Protects the before/after report from fake 'params/MACs -> 0 (-100%)'
rows on quantized outputs. nbdev-test green.
@nathanhubens
nathanhubens deleted the branch fix/quantized-benchmark-segfault July 6, 2026 13:21
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.

1 participant