KV Head Sharing Viz
A two-row arrow diagram of the three attention-sharing schemes — MHA, GQA, MQA. The top row holds numQueryHeads query heads; the bottom row holds the active number of KV heads (numQueryHeads for MHA, gqaGroups for GQA, 1 for MQA). One arrow per query head points to the KV head it shares. Flipping the active scheme retweens the KV row and retargets the arrows.
KV head sharing diagram.GQA: query heads grouped onto shared KV heads. 8 query heads share 4 KV heads — group size 2 query heads per KV head.
KV head sharing8 Q → 4 KV · 2:1
Customize
Shape
8
4
Display
GQA
Installation
npx shadcn@latest add https://craftbits.dev/r/kv-head-sharing-viz.jsonUsage
import { KVHeadSharingViz } from "@craft-bits/core";
<KVHeadSharingViz
numQueryHeads={8}
gqaGroups={4}
defaultAttentionType="gqa"
/>Drive the active type from outside (Radix-style controlled mode):
<KVHeadSharingViz
numQueryHeads={8}
gqaGroups={4}
attentionType={type}
onAttentionTypeChange={setType}
/>Hide the picker pills when the parent already owns the toggle UI:
<KVHeadSharingViz showPicker={false} attentionType={type} />Understanding the component
- Two rows, one arrow per query. Top row:
numQueryHeadsevenly-spaced query-head circles. Bottom row: the active KV-head count, also evenly spaced. Each query head emits a single arrow downward to the KV head it reads from. - Three sharing schemes.
MHAkeeps a KV head per query head (1:1, no sharing).GQAcollapses the KV row togqaGroups— query heads form contiguous groups of sizenumQueryHeads / gqaGroups, all sharing one KV head.MQAcollapses the KV row to a single head shared by every query. - Group colouring. KV heads alternate between accent and muted tone; every query head inherits the colour of its KV head. This makes the grouping structure read at a glance — MHA looks like a striped fan, GQA looks like a small set of coloured clusters, MQA is a single coloured starburst.
- Controlled or uncontrolled.
attentionTypesupports the Radix pattern — passattentionTypeplusonAttentionTypeChangefor controlled mode,defaultAttentionTypefor uncontrolled. The built-in picker is arole="radiogroup"ofrole="radio"pills witharia-checked. Suppress it viashowPicker={false}when the parent owns the toggle UI. SPRINGS.smootheverywhere. KV-row position changes and arrow re-targets animate with the canonical smooth spring;prefers-reduced-motion: reducecollapses every spring to an instant swap.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
numQueryHeads | number | 8 | Number of query heads on the top row. |
attentionType | `'mha' | 'gqa' | 'mqa'` |
defaultAttentionType | `'mha' | 'gqa' | 'mqa'` |
onAttentionTypeChange | (t) => void | — | Fires when the picker commits a new value. |
gqaGroups | number | 4 | KV heads when attentionType === "gqa". Clamped to [1, numQueryHeads]. |
showPicker | boolean | true | Render the built-in MHA / GQA / MQA picker pills. |
transition | Transition | SPRINGS.smooth | Spring used for KV-row plus arrow tween. |
className | string | — | Merged onto the root via cn(). |
Accessibility
- The figure is
role="figure"with a hidden,aria-live="polite"summary describing the active scheme, the query-to-KV head count, and the group size — screen readers hear the new structure on every change. - The attention-type picker is a
role="radiogroup"ofrole="radio"pills witharia-checked. Tab focuses the group; Space and Enter commit a selection. - Each pill carries a textual label (
MHA,GQA,MQA) plus the resulting KV-head count so colour is never the only signal. - The SVG itself is
aria-hidden="true"— the summary text carries the semantics. Node labels (Q1...Q8,KV1...KVk) are visual aids only. - Motion respects
prefers-reduced-motion: reduce.
Credits
- Extracted from:
craftingattention(app/src/lessons/primitives/viz/KVHeadSharingViz.tsx). Stripped the Widget chrome (useWidgetHistoryundo / redo, bookmarks, eyebrow plus premise plus caption plus formula), the Explore / Predict / ChallengeModeStrip, the four-question predict-round generator (kv heads / group size / mem percent / mem savings), the five-challenge progression withFeedbackBadgeplusScoreDots, thegqa-2andgqa-4enumeratedSharingModeunion, theCompareViewof all four modes side-by-side, and the memory-percentage bar at the bottom of the SVG. The library extract is the pure two-row arrow diagram — query heads, KV heads, arrows, picker — driven entirely by props withSPRINGS.smoothon the row reflow.