SwiGLU Activation Viz
The gated activation modern transformer FFN blocks use end-to-end: SwiGLU(x, g) = Swish(x) · g. The gate g is a learned scalar that scales the bare Swish curve; this view pins g to a single slider so the gating intuition is mechanical and direct. Drag the gate up and the SwiGLU curve grows; push it through zero and the curve flips across the x-axis.
SwiGLU · g = 1.00SwiGLU(1.00, 1.00) = 0.731
1.00
Customize
Gate
1.00
Indicator
1.00
Overlays
Installation
npx shadcn@latest add https://craftbits.dev/r/swi-glu-activation-viz.jsonUsage
import { SwiGLUActivationViz } from "@craft-bits/core";
<SwiGLUActivationViz defaultGate={1} />Drive the gate from outside (parent scrubber, animation, or another control):
const [g, setG] = useState(1);
<SwiGLUActivationViz
gate={g}
onGateChange={setG}
currentX={0.75}
/>Add the sigmoid reference and hide the bare Swish overlay:
<SwiGLUActivationViz
defaultGate={1.2}
showSwish={false}
showSigmoid
/>Understanding the component
- Three curves on one axis. Accent curve:
SwiGLU(x, g) = Swish(x) · g. Muted dashed curve: the bareSwish(x) = x · σ(x)reference. Warning-toned horizontal line aty = g— the gate value made directly visible. The headline of the chart is the relationship between the three. - Gate range
[-2, 2]. Production LLMs see gate values that cluster around1but routinely cross zero — that's where SwiGLU "decides not to fire". The slider exposes the full bipolar range so the user can watch the curve invert wheng < 0. - Stable y-axis. The plot pre-samples SwiGLU at the worst-case gate (
|g| = 2) so dragging the slider never re-pans the y extents. The Swish and σ overlays only widen the axis when enabled. - Spring transitions. Both the SwiGLU curve and the gate line animate with
SPRINGS.smooth. Drags feel continuous; reduced-motion users snap. - Optional σ overlay. The sigmoid
σ(x) = 1 / (1 + e^-x)is the smooth-gate primitive Swish wraps. Off by default — toggle it on to remind the learner where the s-shape originates. - Optional
currentXhighlight. Pass a number and the chart draws a dashed vertical guide plus a marker on the SwiGLU curve at that x. The live readout reportsSwiGLU(currentX, g). - Controlled + uncontrolled. Pass
gate+onGateChangeto drivegfrom outside, or omit both and let the component own its state viadefaultGate.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
xRange | readonly [number, number] | [-5, 5] | Visible domain on the x-axis. |
gate | number | — | Controlled gate g. Pair with onGateChange. Clamped to [-2, 2]. |
defaultGate | number | 1 | Uncontrolled initial gate value. |
onGateChange | (gate: number) => void | — | Fires whenever the slider commits a new gate. |
showSwish | boolean | true | Render the bare Swish(x) reference curve. |
showSigmoid | boolean | false | Render the σ(x) curve as a secondary reference. |
currentX | number | — | Highlight a specific x with a dashed guide and a marker on the SwiGLU curve. |
transition | Transition | SPRINGS.smooth | Spring used for curve and gate-line transitions. |
className | string | — | Merged onto the root <div> via cn(). |
Accessibility
- The chart is
role="img"with anaria-labelledbyheading that names the current gate value. - The live readout uses
aria-live="polite"so screen-reader users get the same value updates as sighted ones whenever the gate orcurrentXchanges. - The gate slider is a
LabeledSlider(native<input type="range">) — full keyboard support out of the box (←/→step by0.01,Page Up/Page Downstep by 10%,Home/Endjump to the extents). - Animation is fully disabled when
prefers-reduced-motion: reduceis set — both the curve morph and gate-line spring snap.
Credits
- Extracted from:
craftingattention(app/src/lessons/primitives/nn/SwiGLUActivationViz.tsx). Stripped the lesson-specific phase / narration system, the parallel gate-lane / value-lane data-flow diagram, the SiLU inset plot, and the ReLU-vs-GELU comparison strip; generalized to a single function-plot primitive with a scrubbable gate, an optionalcurrentXhighlight, and Swish + σ reference overlays. Inlineanimate(..., SPRINGS.snappy)calls were replaced with declarativemotion.path/motion.linetransitions, and the project's--color-warn-400/--color-accent-400palette withcb-*semantic tokens.