Quantization Explorer
Back-to-back histograms for uniform mid-tread quantization. The top panel shows the original distribution of values; the bottom panel shows the same values after every entry snaps to one of 2^bits evenly spaced levels across range. Underneath, a mono readout prints max / mean / RMS error so the precision tax is visible at a glance — drag bits down and the smooth bell collapses into a handful of fat spikes; the max-error cell switches to cb-warning the moment any value misses its level.
Different from BitFieldExplorer — that one snaps a single number onto a bit-field row. This one buckets the entire distribution and aggregates the error.
Quantization at 4 bits, 16 levels across -1.000 to 1.000. 1024 values. Max error 0.0666, mean absolute error 0.0339, RMS error 0.0393.
Quantization · 4 bits16 levels · n = 1024
- max error
- 0.0666
- mean |err|
- 0.0339
- RMS error
- 0.0393
4 bits
Customize
Quantization
4 bits
±1.0
Display
24
Installation
npx shadcn@latest add https://craftbits.dev/r/quantization-explorer.jsonUsage
import { QuantizationExplorer } from "@craft-bits/core";
<QuantizationExplorer values={weights} defaultBits={8} />Drive bits externally:
<QuantizationExplorer
values={weights}
bits={bits}
onBitsChange={setBits}
range={[-2, 2]}
/>Hide the slider for a purely declarative readout:
<QuantizationExplorer
values={weights}
bits={4}
showBitsSlider={false}
/>Anatomy
- Uniform mid-tread quantizer. Every value is clipped to
rangeand snapped to the nearest of2^bitsevenly spaced levels — the canonical post-training quantization recipe. The same nearest-level math is applied across the full input so the two histograms share an x-axis and the comparison reads top-to-bottom. - Shared y-axis for like-for-like comparison. Both panels are scaled to the same
max(count)so a tall spike in the quantized panel is visibly taller than the matching spike upstairs — the collapse of mass onto a level grid is the teaching moment, and a per-panel rescale would hide it. - Three error readouts. Max absolute error is the worst-case ULP; mean absolute error is the bulk feel; RMS is what loss functions actually accumulate. The max cell switches to
cb-warningwhen any error is non-zero so the "your bit budget isn't free" moment is visible without reading the digits. - Controlled or uncontrolled bits.
bitsplusonBitsChangefor controlled,defaultBitsfor uncontrolled — the Radix idiom. The built-in slider is a real<input type="range">so Tab / Arrow / Home / End all work for free;aria-valuetextspells out the level count. SPRINGS.smoothfor the bars. Histogram bars spring between heights whenbitsorvalueschange;prefers-reduced-motion: reducecollapses every transition to an instant swap.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
values | readonly number[] | required | Continuous, real-valued inputs to quantize. Non-finite entries are silently dropped. |
bits | number | — | Controlled bit width, clamped to [1, 12]. Pair with onBitsChange. |
defaultBits | number | 8 | Uncontrolled initial bit width. |
onBitsChange | (bits) => void | — | Fires when the user drags the slider. |
range | readonly [number, number] | [-1, 1] | [lo, hi] quantization range. Values outside saturate to the nearest endpoint. |
histogramBins | number | 24 | Bin count for both histograms, clamped to [8, 64]. |
showBitsSlider | boolean | true | Render the built-in bit-width slider. |
transition | Transition | SPRINGS.smooth | Override the spring for histogram bar transitions. |
className | string | — | Merged onto the root via cn(). |
Accessibility
- The outer element is
role="figure"with anaria-labelledbyheading and anaria-describedbyaria-live="polite"summary that announces the bit width, level count, range, and all three error statistics whenever they change. - The SVG carries
role="img"and inherits the same heading viaaria-labelledby; decorative axis lines and tick labels are markedaria-hidden. - The bit-width slider is a native
<input type="range">with Arrow / Home / End keyboard support and anaria-valuetextthat spells out the current level count. - Color is never the only signal: every readout has a textual label, and the max-error cell only switches to
cb-warningas an emphasis on top of the always-visible numeric value. - Histogram bar animations respect
prefers-reduced-motion: reduceand collapse to an instant swap when the user opts out.
Credits
- Extracted from:
craftingattention(app/src/lessons/primitives/viz/QuantizationExplorer.tsx). The source was a single-number-line viz bound to a hard-coded 8-weight set, an Explore / Predict mode strip, a 6-round quiz pool, narration heuristics, and the project'sModeStrip/TogglePill/ScoreDots/DoneCard/ChallengeBtn/FeedbackBadgechrome. The library version reframes the same teaching surface as a pure distribution-vs-quantization viz — arbitraryvalues, Radix-style controlled / uncontrolledbits, a configurablerangeandhistogramBins, the built-in slider as the only interaction, andcb-accent/cb-warning/cb-fg-mutedsemantic tokens in place of the inline raw colour vars.