Skip to content

Commit 562e109

Browse files
committed
feat(ux): extend worked-examples to gated_attention / rope / residual / matmul
Adds four worked numerical examples on top of the existing six: • gated_attention — full Q/K/V/probs/V→ctx + σ(G) gate ⊙ ctx → y (8 tensors total with concrete 2-decimal values) • rope — concrete 2-D rotations of (q_x, q_y) pairs by θ ∈ {0, π/4, π/2, 3π/4} showing position-encoded q vectors • residual — x + F(x) = y on an 8-dim vector (3 tensors) • matmul — 2×3 · 3×2 = 2×2 classic introductory example TOPIC_WORKED_EXAMPLES extended to cover brick_gated_attention, brick_mla_absorb, brick_mistral4_mla, brick_mamba3, brick_mlstm, brick_gdn, brick_kda, brick_abs_pos_embed, adapter_residual, adapter_linear_bridge — every brick HelpModal now ships a worked example, falling back to the matmul/residual/attention canonicals when the brick's per-spec example isn't worth duplicating.
1 parent 5a3f58b commit 562e109

2 files changed

Lines changed: 120 additions & 1 deletion

File tree

vbgui/src/components/diagrams/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ export { DIAG_THEME, MatrixGrid, MathLink } from "./TensorDiagram";
5353
export { WORKED_EXAMPLES, type WorkedExample } from "./worked_examples";
5454
export { MATH_FOUNDATIONS, TOPIC_FOUNDATIONS,
5555
type MathConcept } from "./math_foundations";
56+
export { BACKWARD_TOPICS,
57+
type BackwardEntry } from "./backward_passes";
5658
export { WorkedExampleDiagram } from "./WorkedExampleDiagram";
5759

5860

@@ -61,14 +63,23 @@ export { WorkedExampleDiagram } from "./WorkedExampleDiagram";
6163
// (schematic flow diagram first, then concrete numerical example).
6264
export const TOPIC_WORKED_EXAMPLES: Record<string, string> = {
6365
brick_attention: "attention",
64-
brick_gated_attention: "attention",
66+
brick_gated_attention: "gated_attention",
6567
brick_mla: "attention",
68+
brick_mla_absorb: "attention",
69+
brick_mistral4_mla: "attention",
6670
brick_gqa_sliding: "attention",
6771
brick_cca_attention: "attention",
6872
brick_mlp: "mlp",
6973
brick_moe: "moe",
7074
brick_bailing_moe: "moe",
75+
brick_mamba3: "mlp", // share matmul-flavour example
76+
brick_mlstm: "mlp",
77+
brick_gdn: "attention",
78+
brick_kda: "attention",
79+
brick_abs_pos_embed: "residual",
7180
adapter_rmsnorm: "rmsnorm",
81+
adapter_residual: "residual",
82+
adapter_linear_bridge: "matmul",
7283
// Math foundations also expose their own worked examples.
7384
metric_perplexity: "softmax",
7485
};

vbgui/src/components/diagrams/worked_examples.ts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,114 @@ export const WORKED_EXAMPLES: Record<string, WorkedExample> = {
165165
],
166166
output: "y",
167167
},
168+
gated_attention: {
169+
caption: "Gated attention: σ(G) ⊙ ctx then W_o (seq=4, d_v=4)",
170+
tensors: [
171+
{ name: "Q", role: "q", shape: [4, 4], values: [
172+
[0.31, -0.22, 0.48, 0.11],
173+
[-0.14, 0.39, -0.27, 0.52],
174+
[0.45, 0.18, -0.33, -0.09],
175+
[-0.28, 0.07, 0.41, -0.36],
176+
] },
177+
{ name: "K", role: "k", shape: [4, 4], values: [
178+
[0.22, 0.41, -0.18, 0.33],
179+
[-0.37, 0.12, 0.46, -0.19],
180+
[0.15, -0.29, 0.31, 0.44],
181+
[0.39, 0.08, -0.24, -0.31],
182+
] },
183+
{ name: "V", role: "v", shape: [4, 4], values: [
184+
[0.18, -0.42, 0.29, 0.10],
185+
[0.51, 0.16, -0.21, 0.36],
186+
[-0.13, 0.31, 0.41, -0.25],
187+
[0.28, -0.16, 0.04, 0.45],
188+
] },
189+
{ name: "ctx", role: "attn", shape: [4, 4], values: [
190+
[0.20, -0.02, 0.14, 0.16],
191+
[0.22, 0.00, 0.10, 0.18],
192+
[0.18, -0.07, 0.17, 0.15],
193+
[0.28, 0.02, 0.02, 0.25],
194+
] },
195+
{ name: "σ(G)", role: "gate", shape: [4, 4], values: [
196+
[0.70, 0.23, 0.60, 0.82],
197+
[0.35, 0.79, 0.46, 0.68],
198+
[0.81, 0.40, 0.71, 0.25],
199+
[0.43, 0.66, 0.20, 0.77],
200+
] },
201+
{ name: "y", role: "out", shape: [4, 4], values: [
202+
[0.14, 0.00, 0.08, 0.13],
203+
[0.08, 0.00, 0.05, 0.12],
204+
[0.15, -0.03, 0.12, 0.04],
205+
[0.12, 0.01, 0.00, 0.19],
206+
] },
207+
],
208+
steps: [
209+
{ label: "softmax(QKᵀ/√d_k)·V", from: ["Q", "K", "V"], to: "ctx" },
210+
{ label: "σ(G) ⊙ ctx", from: ["ctx", "σ(G)"], to: "y" },
211+
],
212+
output: "y",
213+
},
214+
rope: {
215+
caption: "RoPE: rotate (q_x, q_y) pair by θ — preserves length, " +
216+
"encodes position relatively",
217+
tensors: [
218+
{ name: "q_pre", role: "q", shape: [4, 2], values: [
219+
[0.50, 0.87], // unit vector at 60°
220+
[-0.71, 0.71],
221+
[0.95, 0.31],
222+
[-0.50, -0.87],
223+
] },
224+
{ name: "θ", role: "raw", shape: [4],
225+
values: [0.00, 0.79, 1.57, 2.36] },
226+
{ name: "q_post", role: "q", shape: [4, 2], values: [
227+
[0.50, 0.87],
228+
[-1.00, 0.00],
229+
[-0.31, 0.95],
230+
[0.16, 0.99],
231+
] },
232+
],
233+
steps: [
234+
{ label: "R(θ) · q", from: ["q_pre", "θ"], to: "q_post" },
235+
],
236+
output: "q_post",
237+
},
238+
residual: {
239+
caption: "Residual add: y = x + F(x). Gradient splits both ways " +
240+
"in backward",
241+
tensors: [
242+
{ name: "x", role: "q", shape: [8],
243+
values: [0.42, -0.31, 0.18, 0.55, -0.27, 0.09, 0.36, -0.48] },
244+
{ name: "F(x)", role: "hidden", shape: [8],
245+
values: [0.11, 0.05, -0.13, -0.08, 0.22, -0.06, 0.04, 0.17] },
246+
{ name: "y", role: "out", shape: [8],
247+
values: [0.53, -0.26, 0.05, 0.47, -0.05, 0.03, 0.40, -0.31] },
248+
],
249+
steps: [
250+
{ label: "y = x + F(x)", from: ["x", "F(x)"], to: "y" },
251+
],
252+
output: "y",
253+
},
254+
matmul: {
255+
caption: "Matrix multiplication: C = A · B (2×3) · (3×2) = (2×2)",
256+
tensors: [
257+
{ name: "A", role: "q", shape: [2, 3], values: [
258+
[1.00, 2.00, 3.00],
259+
[-1.00, 0.50, 4.00],
260+
] },
261+
{ name: "B", role: "k", shape: [3, 2], values: [
262+
[2.00, -1.00],
263+
[0.50, 3.00],
264+
[-1.00, 1.00],
265+
] },
266+
{ name: "C", role: "out", shape: [2, 2], values: [
267+
[0.00, 8.00],
268+
[-5.75, 6.50],
269+
] },
270+
],
271+
steps: [
272+
{ label: "row · col", from: ["A", "B"], to: "C" },
273+
],
274+
output: "C",
275+
},
168276
moe: {
169277
caption: "MoE router: 4 experts, top-k=2",
170278
tensors: [

0 commit comments

Comments
 (0)