File size: 23,491 Bytes
99e41a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
caa90b0
 
 
 
b2ead12
caa90b0
b2ead12
caa90b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2ead12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99e41a8
caa90b0
b2ead12
99e41a8
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#!/usr/bin/env python3
"""Architecture diagrams in the style of Sebastian Raschka's LLM architecture figures
(grey outer container, coloured repeated block with an "N x" badge, white component boxes,
residual "+" nodes, thin leader lines to side annotations). Numbers come from the real
checkpoint config / safetensors header, not from memory.

  media/architecture/cosmos_reason2_verifier.png   Cosmos-Reason2-2B (Qwen3-VL) as our success verifier
  media/architecture/race_s.png                    RACE-S fusion model
  media/architecture/roborace_system.png           full system across the three tracks
"""
from pathlib import Path

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt  # noqa: E402
from matplotlib.patches import Circle, FancyArrowPatch, FancyBboxPatch  # noqa: E402

OUT = Path("media/architecture")
OUT.mkdir(parents=True, exist_ok=True)
GREY_BG, GREY_EDGE = "#ececec", "#b5b5b5"
INK = "#1f2937"
TEAL, TEAL_BG = "#0f766e", "#b9e3dc"
BLUE, BLUE_BG = "#1d4ed8", "#cddcff"
AMBER, AMBER_BG = "#b45309", "#fde3b8"
ROSE, ROSE_BG = "#be123c", "#fbd0da"
VIOLET_BG = "#e4dafc"
plt.rcParams.update({"font.family": "DejaVu Sans", "savefig.bbox": "tight", "figure.dpi": 170})


def box(ax, x, y, w, h, text="", fc="white", ec=INK, lw=1.2, r=0.012, fs=9, bold=False, color=INK, z=3, ls="-"):
    ax.add_patch(FancyBboxPatch((x, y), w, h, boxstyle=f"round,pad=0,rounding_size={r}", fc=fc, ec=ec, lw=lw,
                                zorder=z, ls=ls))
    if text:
        ax.text(x + w / 2, y + h / 2, text, ha="center", va="center", fontsize=fs, color=color, zorder=z + 1,
                fontweight="bold" if bold else None)


def arrow(ax, x0, y0, x1, y1, color=INK, lw=1.2, z=2, style="-|>", ms=9, ls="-"):
    ax.add_patch(FancyArrowPatch((x0, y0), (x1, y1), arrowstyle=style, mutation_scale=ms, color=color, lw=lw,
                                 zorder=z, linestyle=ls, shrinkA=0, shrinkB=0))


def plus(ax, x, y, r=0.011):
    ax.add_patch(Circle((x, y), r, fc="white", ec=INK, lw=1.1, zorder=4))
    ax.text(x, y - 0.001, "+", ha="center", va="center", fontsize=10, zorder=5)


def note(ax, x_from, y_from, x_txt, y_txt, text, color=INK, fs=8, ha="left"):
    ax.plot([x_from, x_txt - (0.006 if ha == "left" else -0.006)], [y_from, y_txt], color=color, lw=0.7, zorder=1)
    ax.text(x_txt, y_txt, text, fontsize=fs, color=color, va="center", ha=ha, zorder=5)


def canvas(w=11, h=12.5):
    fig = plt.figure(figsize=(w, h))
    ax = fig.add_axes([0, 0, 1, 1])
    ax.set_xlim(0, 1)
    ax.set_ylim(0, 1)
    ax.axis("off")
    return fig, ax


# ============================================================== Cosmos-Reason2-2B verifier
def cosmos():
    fig, ax = canvas(11.5, 11.6)
    ax.set_ylim(0, 0.905)
    ax.text(0.5, 0.89, "Cosmos-Reason2 2B  ·  as RoboRACE's success verifier", ha="center", fontsize=16,
            fontweight="bold", color=TEAL)
    ax.text(0.5, 0.872, "Qwen3-VL architecture · 2.44 B parameters (counted from the safetensors header) · runs on 4 CPU cores",
            ha="center", fontsize=9, color="#4b5563")
    # ---------------- LLM decoder container
    X0, W = 0.40, 0.30
    box(ax, X0 - 0.03, 0.07, W + 0.06, 0.78, fc=GREY_BG, ec=GREY_EDGE, r=0.02, z=0)
    # input / embedding
    box(ax, X0 + 0.04, 0.085, W - 0.08, 0.035, "Tokenized text  +  image tokens", fs=8.5)
    arrow(ax, X0 + W / 2, 0.12, X0 + W / 2, 0.145)
    box(ax, X0 + 0.02, 0.145, W - 0.04, 0.04, "Token embedding layer", fc="#f3f4f6", fs=9)
    note(ax, X0 + W - 0.02, 0.165, 0.80, 0.165, "Vocabulary size: 151,936\nEmbedding dim: 2,048  (311 M params)", fs=8)
    arrow(ax, X0 + W / 2, 0.185, X0 + W / 2, 0.215)
    # repeated transformer block
    BY, BH = 0.215, 0.405
    box(ax, X0, BY, W, BH, fc=TEAL_BG, ec=TEAL, lw=1.6, r=0.02, z=1)
    ax.text(X0 + W + 0.008, BY + BH - 0.02, "28×", fontsize=15, fontweight="bold", color=TEAL)
    cx = X0 + W / 2
    y = BY + 0.025
    comps = [("RMSNorm 1", "#ffffff", 0.035), ("Grouped-query attention\n+ QK-norm  ·  interleaved M-RoPE", "#ffffff", 0.07),
             (None, None, 0.0), ("RMSNorm 2", "#ffffff", 0.035), ("Feed forward (SwiGLU)", "#ffffff", 0.05), (None, None, 0.0)]
    ys = {}
    arrow(ax, cx, BY, cx, y)
    for i, (name, fc, h) in enumerate(comps):
        if name is None:
            plus(ax, cx, y + 0.012)
            ys[f"plus{i}"] = y + 0.012
            arrow(ax, cx, y + 0.023, cx, y + 0.05)
            y += 0.05
            continue
        box(ax, X0 + 0.035, y, W - 0.07, h, name, fs=8.6)
        ys[name] = (y, h)
        arrow(ax, cx, y + h, cx, y + h + 0.02)
        y += h + 0.02
    # residual skips
    for top_key, bottom_y in (("plus2", BY + 0.012), ("plus5", ys["plus2"] + 0.02)):
        ty = ys[top_key]
        ax.plot([cx, X0 + 0.018, X0 + 0.018, cx - 0.011], [bottom_y, bottom_y, ty, ty], color=INK, lw=1.0, zorder=2)
    # attention details
    ay, ah = ys["Grouped-query attention\n+ QK-norm  ·  interleaved M-RoPE"]
    note(ax, X0 + W - 0.035, ay + ah / 2, 0.80, ay + ah / 2 + 0.035,
         "16 query heads · 8 key/value groups\nhead dim 128 · RoPE θ = 5,000,000\nM-RoPE sections (t, h, w) = (24, 20, 20)\n"
         "context 262,144 tokens", fs=8)
    fy, fh = ys["Feed forward (SwiGLU)"]
    note(ax, X0 + W - 0.035, fy + fh / 2, 0.80, fy + fh / 2 - 0.01, "SiLU-gated  2,048 → 6,144 → 2,048", fs=8)
    note(ax, X0 + W, BY + 0.06, 0.80, BY + 0.05, "28 decoder layers = 1,409 M params", fs=8, color=TEAL)
    # top: final norm + head
    arrow(ax, cx, BY + BH, cx, BY + BH + 0.025)
    box(ax, X0 + 0.035, BY + BH + 0.025, W - 0.07, 0.035, "Final RMSNorm", fs=9)
    arrow(ax, cx, BY + BH + 0.06, cx, BY + BH + 0.085)
    box(ax, X0 + 0.02, BY + BH + 0.085, W - 0.04, 0.04, "Linear output layer  (2,048 → 151,936)", fc="#f3f4f6", fs=8.6)
    note(ax, X0 + W - 0.02, BY + BH + 0.105, 0.80, BY + BH + 0.112, "untied in this checkpoint (311 M)", fs=8)
    arrow(ax, cx, BY + BH + 0.125, cx, BY + BH + 0.15, color=AMBER, lw=1.6)
    # our head
    hy = BY + BH + 0.15
    box(ax, X0 - 0.025, hy, W + 0.05, 0.06, "", fc=AMBER_BG, ec=AMBER, lw=1.5)
    ax.text(cx, hy + 0.04, "RoboRACE verifier head (no training)", ha="center", fontsize=9, fontweight="bold", color=AMBER)
    ax.text(cx, hy + 0.017, "P(success) = σ( logsumexp ℓ[yes-tokens] − logsumexp ℓ[no-tokens] )\n"
            "one forward pass · 1.4 s / view on CPU", ha="center", fontsize=7.2, color=AMBER, va="center")
    note(ax, X0 + W + 0.025, hy + 0.03, 0.80, hy + 0.03, "→ ACL / rank-Gauss calibration\n→ RACE-S fusion across cameras",
         color=AMBER, fs=8)
    # ---------------- vision encoder (left)
    VX, VW = 0.05, 0.25
    box(ax, VX - 0.02, 0.10, VW + 0.04, 0.53, fc=GREY_BG, ec=GREY_EDGE, r=0.02, z=0)
    ax.text(VX + VW / 2, 0.615, "Vision encoder (ViT)", ha="center", fontsize=11, fontweight="bold", color=BLUE)
    box(ax, VX + 0.02, 0.115, VW - 0.04, 0.035, "Camera view (≤ 448×448)", fs=8.5)
    arrow(ax, VX + VW / 2, 0.15, VX + VW / 2, 0.17)
    box(ax, VX + 0.01, 0.17, VW - 0.02, 0.045, "Patch embedding\n16×16 px × 2 frames → 1,024", fs=8)
    arrow(ax, VX + VW / 2, 0.215, VX + VW / 2, 0.24)
    box(ax, VX, 0.24, VW, 0.24, fc=BLUE_BG, ec=BLUE, lw=1.6, r=0.02, z=1)
    ax.text(VX + VW - 0.045, 0.488, "24×", fontsize=13, fontweight="bold", color=BLUE)
    box(ax, VX + 0.025, 0.255, VW - 0.05, 0.03, "LayerNorm", fs=8)
    box(ax, VX + 0.025, 0.30, VW - 0.05, 0.045, "Multi-head self-attention\n16 heads · 2-D RoPE", fs=8)
    box(ax, VX + 0.025, 0.36, VW - 0.05, 0.03, "LayerNorm", fs=8)
    box(ax, VX + 0.025, 0.405, VW - 0.05, 0.045, "MLP (GELU-tanh)\n1,024 → 4,096 → 1,024", fs=8)
    for y0, y1 in ((0.285, 0.30), (0.345, 0.36), (0.39, 0.405)):
        arrow(ax, VX + VW / 2, y0, VX + VW / 2, y1)
    ax.text(VX + VW / 2, 0.233, "302 M params", fontsize=7.5, color=BLUE, ha="center", va="top")
    arrow(ax, VX + VW / 2, 0.48, VX + VW / 2, 0.51)
    box(ax, VX + 0.01, 0.51, VW - 0.02, 0.05, "2×2 patch merger MLP\n4×1,024 → 2,048 (one token / 32 px)", fs=8)
    gx = 0.345   # routing channel between the two columns
    ax.plot([VX + VW - 0.01, gx, gx], [0.535, 0.535, 0.1025], color=BLUE, lw=1.5, zorder=2)
    arrow(ax, gx, 0.1025, X0 + 0.04, 0.1025, color=BLUE, lw=1.5)          # image tokens into the input sequence
    ax.text(gx - 0.008, 0.62, "image tokens\n(≈196 per view)", fontsize=8, color=BLUE, ha="center")
    # deepstack
    for k, yy in enumerate((0.31, 0.37, 0.43)):
        arrow(ax, VX + VW - 0.005, yy, X0, 0.25 + 0.04 * k, color=BLUE, lw=0.9, ls=(0, (3, 2)), ms=7)
    ax.text(VX + VW / 2, 0.075, "dashed = DeepStack: ViT layers 5, 11, 17 are\nadded to the first LLM layers (merger + DeepStack 101 M)",
            fontsize=7.5, color=BLUE, ha="center")
    # legend / provenance
    ax.text(0.03, 0.03, "Config: nvidia/Cosmos-Reason2-2B config.json · parameter counts from model.safetensors header · "
            "style after S. Raschka's LLM architecture figures", fontsize=7.2, color="#6b7280")
    fig.savefig(OUT / "cosmos_reason2_verifier.png")
    plt.close(fig)


# ============================================================== RACE-S
def race_s():
    fig, ax = canvas(12, 7.2)
    ax.text(0.5, 0.965, "RACE-S  ·  score-level Receiver-Anchored Channel Estimation", ha="center", fontsize=15,
            fontweight="bold", color=TEAL)
    ax.text(0.5, 0.925, "label-free · per episode cost O(J) after fitting · fit cost O(T·J·iters)", ha="center",
            fontsize=9, color="#4b5563")
    # agents column
    names = ["front (receiver)", "left", "right", "back", "top"]
    cols = [TEAL_BG, BLUE_BG, ROSE_BG, ROSE_BG, BLUE_BG]
    for i, (n, c) in enumerate(zip(names, cols)):
        y = 0.74 - i * 0.13
        box(ax, 0.02, y, 0.13, 0.08, f"camera agent\n{n}", fc=c, fs=8.2)
        box(ax, 0.17, y + 0.015, 0.07, 0.05, "VLM\np_j(t)", fs=8)
        arrow(ax, 0.15, y + 0.04, 0.17, y + 0.04)
        arrow(ax, 0.24, y + 0.04, 0.29, 0.44 + (2 - i) * 0.03)
    # calibration
    box(ax, 0.29, 0.30, 0.12, 0.30, "", fc=GREY_BG, ec=GREY_EDGE, r=0.02, z=0)
    box(ax, 0.30, 0.47, 0.10, 0.08, "rank-Gauss\nz = Φ⁻¹(F̂_j(p))", fs=8)
    box(ax, 0.30, 0.34, 0.10, 0.08, "clone\ntempering\nw_j = 1/|group|", fs=7.8)
    ax.text(0.35, 0.31, "per-agent, label-free", fontsize=7, ha="center", color="#6b7280")
    arrow(ax, 0.41, 0.45, 0.46, 0.45)
    # EM core
    box(ax, 0.46, 0.18, 0.30, 0.58, fc=TEAL_BG, ec=TEAL, lw=1.6, r=0.02, z=0)
    ax.text(0.61, 0.735, "anchored EM", fontsize=11, fontweight="bold", color=TEAL, ha="center")
    box(ax, 0.48, 0.60, 0.26, 0.08, "latent truth y ∈ {−1,+1}\nz_j | y ~ N(b_j + s_j·y, v_j)", fs=8.4)
    box(ax, 0.48, 0.47, 0.26, 0.09, "E-step\nr_t = σ( Σ_j w_j·2s_j(z_tj − b_j)/v_j + log π )\ninit: from the receiver only", fs=7.8)
    box(ax, 0.48, 0.33, 0.26, 0.10, "M-step + RACE priors\ns_own ≥ 0.5  (receiver ≥ 75% acc.)\npeers: s_j ← soft-thr(s_j, 1·SE_j)", fs=7.8)
    box(ax, 0.48, 0.20, 0.26, 0.09, "signed weights  λ_j = 2 w_j s_j / v_j\n> 0 trust · < 0 INVERT · ≈ 0 discard", fs=7.8,
        fc=AMBER_BG, ec=AMBER)
    arrow(ax, 0.61, 0.47, 0.61, 0.43)
    arrow(ax, 0.61, 0.33, 0.61, 0.29)
    ax.annotate("", xy=(0.745, 0.52), xytext=(0.745, 0.38), arrowprops=dict(arrowstyle="-|>", color=TEAL, lw=1.2,
                                                                          connectionstyle="arc3,rad=-0.5"))
    ax.text(0.77, 0.45, "iterate\nto conv.", fontsize=7.5, color=TEAL)
    # output
    arrow(ax, 0.76, 0.245, 0.81, 0.245)
    box(ax, 0.81, 0.19, 0.17, 0.11, "fused log-odds\nŷ = Σ_j λ_j (z_j − b_j)\ndecision: ŷ > 0", fs=8.2)
    box(ax, 0.81, 0.48, 0.17, 0.2, "online variant\n(sleeper defence):\nfit on the most recent\nunlabelled window\ninstead of a stale\nhistory", fs=8, fc=VIOLET_BG,
        ec="#6d28d9")
    ax.text(0.02, 0.06, "Identification: without the anchor, EM is symmetric in y ↔ −y and liar majorities flip it (Dawid–Skene "
            "failure). Pinning the receiver's loading positive breaks the symmetry, so coherent liars receive negative weight "
            "and their reports are inverted and reused.", fontsize=8, color=INK, wrap=True)
    fig.savefig(OUT / "race_s.png")
    plt.close(fig)


# ============================================================== system
def system():
    fig, ax = canvas(13, 7.6)
    ax.text(0.5, 0.965, "RoboRACE system  ·  three agent swarms, one receiver-anchored principle", ha="center",
            fontsize=15, fontweight="bold", color=INK)
    lanes = [("Track A · perception", 0.66, TEAL_BG, TEAL,
              ["SO-101 / tabletop\nrollout (MuJoCo)", "5 cameras ×\nCosmos-Reason2-2B", "attack zoo\n(7 attacks)",
               "ACL / rank-Gauss\ncalibration", "RACE-S (online)", "success / failure\n→ retry or replan"]),
             ("Track B · control", 0.40, BLUE_BG, BLUE,
              ["12 robots,\nk-NN radio graph", "noisy target\nmeasurements", "Byzantine robots\n(6 attacks)",
               "channel estimates\nbias_ij, var_ij", "RACE-C gate +\nW-MSR consensus", "rendezvous at\ntarget"]),
             ("Track C · language", 0.14, AMBER_BG, AMBER,
              ["instruction +\nscene", "4 LLM planners\n(+ saboteurs)", "spec + few-shot\nprompting", "symbolic validator\n(grounding / VAL)",
               "repair loop\n(LLM-Modulo)", "RACE vote over\ncanonical plans"])]
    for title, y, bg, ec, steps in lanes:
        box(ax, 0.02, y - 0.02, 0.96, 0.22, fc=bg, ec=ec, lw=1.4, r=0.02, z=0)
        ax.text(0.03, y + 0.175, title, fontsize=11, fontweight="bold", color=ec)
        for i, s in enumerate(steps):
            x = 0.04 + i * 0.158
            box(ax, x, y + 0.01, 0.135, 0.13, s, fs=8.4)
            if i < len(steps) - 1:
                arrow(ax, x + 0.135, y + 0.075, x + 0.158, y + 0.075)
    ax.text(0.5, 0.06, "Benchmarks: RoboRACE-Bench (A/B/C, bootstrap-ranked) · ERQA (embodied reasoning, 400 Q) · "
            "PlanBench Blocksworld (plan generation, VAL-equivalent validator)", ha="center", fontsize=9, color="#4b5563")
    fig.savefig(OUT / "roborace_system.png")
    plt.close(fig)


# ============================================================== RACE-Net
def racenet():
    fig, ax = canvas(10.5, 11.5)
    ax.set_ylim(0, 0.955)
    ax.text(0.5, 0.93, "RACE-Net-V 0.2M  ·  amortized receiver-anchored truth inference", ha="center", fontsize=15,
            fontweight="bold", color="#6d28d9")
    ax.text(0.5, 0.91, "verification model (pick-and-place camera swarm) · axial set transformer · 213,889 parameters · synthetic training only, CPU",
            ha="center", fontsize=9, color="#4b5563")
    X0, W = 0.33, 0.34
    cx = X0 + W / 2
    box(ax, X0 - 0.03, 0.05, W + 0.06, 0.825, fc=GREY_BG, ec=GREY_EDGE, r=0.02, z=0)
    box(ax, X0 + 0.01, 0.065, W - 0.02, 0.045, "Window of unlabelled swarm reports\nT episodes × J agents (receiver = column 0)", fs=8.2)
    note(ax, X0 + W - 0.01, 0.087, 0.74, 0.087, "any T and J (no positional encodings):\npermutation-equivariant over episodes and peers", fs=8)
    arrow(ax, cx, 0.11, cx, 0.13)
    box(ax, X0 + 0.03, 0.13, W - 0.06, 0.035, "Rank-Gauss per agent (label-free)", fc="#f3f4f6", fs=8.6)
    note(ax, X0 + W - 0.03, 0.147, 0.74, 0.147, "z = Φ⁻¹(rank / T): invariant to any monotone\nmiscalibration of an agent's scores", fs=8)
    arrow(ax, cx, 0.165, cx, 0.185)
    box(ax, X0 + 0.02, 0.185, W - 0.04, 0.045, "Token embedding MLP  [z, tanh 2z, z²−1] → 64\n+ receiver embedding on column 0", fc="#f3f4f6", fs=8.2)
    note(ax, X0 + W - 0.02, 0.207, 0.74, 0.207, "the receiver flag is the anchor:\nthe only agent assumed honest", fs=8, color="#6d28d9")
    arrow(ax, cx, 0.23, cx, 0.26)
    BY, BH = 0.26, 0.425
    box(ax, X0, BY, W, BH, fc=VIOLET_BG, ec="#6d28d9", lw=1.6, r=0.02, z=1)
    ax.text(X0 + W + 0.008, BY + BH - 0.022, "4×", fontsize=15, fontweight="bold", color="#6d28d9")
    y = BY + 0.02
    arrow(ax, cx, BY, cx, y)
    plus_y = []
    start = BY + 0.008
    for norm, name, h in (("LayerNorm 1", "Row attention across AGENTS\n(within one episode · 4 heads)", 0.055),
                          ("LayerNorm 2", "Column attention across EPISODES\n(within one agent · 4 heads)", 0.055),
                          ("LayerNorm 3", "Feed forward (GELU)  64 → 128 → 64", 0.04)):
        box(ax, X0 + 0.05, y, W - 0.10, 0.026, norm, fs=7.8)
        arrow(ax, cx, y + 0.026, cx, y + 0.038)
        box(ax, X0 + 0.035, y + 0.038, W - 0.07, h, name, fs=8.2)
        py = y + 0.038 + h + 0.022
        arrow(ax, cx, y + 0.038 + h, cx, py - 0.011)
        plus(ax, cx, py)
        ax.plot([cx, X0 + 0.016, X0 + 0.016, cx - 0.011], [start, start, py, py], color=INK, lw=1.0, zorder=2)
        plus_y.append(py)
        start = py + 0.014
        arrow(ax, cx, py + 0.011, cx, py + 0.028)
        y = py + 0.028
    note(ax, X0 + W - 0.035, BY + 0.085, 0.74, BY + 0.085, "who agrees with whom right now", fs=8)
    note(ax, X0 + W - 0.035, BY + 0.215, 0.74, BY + 0.215, "in-context channel estimation: how each\nagent's stream relates to the others over\nthe window (trust · invert · ignore)", fs=8)
    arrow(ax, cx, BY + BH, cx, BY + BH + 0.02)
    box(ax, X0 + 0.05, BY + BH + 0.02, W - 0.10, 0.03, "Final LayerNorm", fs=8.6)
    arrow(ax, cx, BY + BH + 0.05, cx, BY + BH + 0.07)
    box(ax, X0 + 0.02, BY + BH + 0.07, W - 0.04, 0.045, "Readout MLP on [receiver token, mean over agents]\n128 → 64 → 1", fc="#f3f4f6", fs=8.2)
    arrow(ax, cx, BY + BH + 0.115, cx, BY + BH + 0.135, color=AMBER, lw=1.6)
    box(ax, X0 - 0.01, BY + BH + 0.135, W + 0.02, 0.04, "log-odds of task success for every episode", fc=AMBER_BG, ec=AMBER, lw=1.5, fs=9,
        bold=True, color=AMBER)
    # left: where it sits
    box(ax, 0.02, 0.30, 0.22, 0.30, fc=GREY_BG, ec=GREY_EDGE, r=0.02, z=0)
    ax.text(0.13, 0.575, "replaces", ha="center", fontsize=10, fontweight="bold", color=INK)
    box(ax, 0.035, 0.47, 0.19, 0.08, "RACE / RACE-S:\nhand-specified latent-truth\nmodel + EM per window", fs=7.8)
    box(ax, 0.035, 0.34, 0.19, 0.10, "RACE-Net:\none forward pass that has\nLEARNED the inference\nfrom simulated swarms", fs=7.8, fc=VIOLET_BG, ec="#6d28d9")
    arrow(ax, 0.13, 0.47, 0.13, 0.44)
    ax.text(0.03, 0.02, "Training: synthetic swarms only (heterogeneous skill, correlated honest errors, attack zoo with up to J−1 liars). "
            "No benchmark data, labels or agent outputs.", fontsize=7.2, color="#6b7280")
    fig.savefig(OUT / "racenet.png")
    plt.close(fig)


def racenet_m():
    fig, ax = canvas(11.5, 11.5)
    ax.set_ylim(0, 0.955)
    TEAL_ = "#0f766e"
    ax.text(0.5, 0.93, "RACE-Net-M 45K  ·  receiver-anchored channel estimation on a map", ha="center", fontsize=15,
            fontweight="bold", color=TEAL_)
    ax.text(0.5, 0.91, "map-fusion model (maze swarm) · anchored channel encoder + peer transformer + signed read-out · 44,837 parameters · CPU",
            ha="center", fontsize=9, color="#4b5563")
    X0, W, NX = 0.25, 0.42, 0.715
    cx = X0 + W / 2
    box(ax, X0 - 0.02, 0.05, W + 0.04, 0.825, fc=GREY_BG, ec=GREY_EDGE, r=0.02, z=0)
    box(ax, X0 + 0.02, 0.065, W - 0.04, 0.055, "Peer broadcasts:  P peers × N cells\nclaim ∈ {none, free, wall}, recent / old flag\n+ the receiver's own sensed cells", fs=8)
    note(ax, X0 + W - 0.02, 0.092, NX, 0.092, "peers and cells are sets (no positions):\nany map size, any swarm size", fs=8)
    arrow(ax, cx, 0.12, cx, 0.145)
    box(ax, X0 + 0.02, 0.145, W - 0.04, 0.055, "Anchored counts per peer:  claim × truth\non cells the receiver SENSED ITSELF\n(recent and old claims counted separately)", fc="#f3f4f6", fs=8)
    note(ax, X0 + W - 0.02, 0.172, NX, 0.172, "the anchor: the receiver's own sensor is\nthe only trusted evidence\n(no labels, no majority)", fs=8, color=TEAL_)
    arrow(ax, cx, 0.20, cx, 0.235)
    BY, BH = 0.235, 0.46
    box(ax, X0, BY, W, BH, fc="#d9f0ec", ec=TEAL_, lw=1.6, r=0.02, z=1)
    ax.text(X0 + 0.012, BY + BH - 0.03, "3 refinement rounds", fontsize=9, fontweight="bold", color=TEAL_)
    y = BY + 0.02
    arrow(ax, cx, BY, cx, y)
    mids = []
    blocks = (("Soft counts per peer on cells the receiver has NOT sensed:\nclaim × current belief of all the OTHER peers", 0.055),
              ("Channel embedding MLP  (36 → 48) + round embedding\ninput: hard counts, soft counts, coverage", 0.055),
              ("Transformer across PEERS\n2 layers · 4 heads · pre-norm", 0.055),
              ("Signed read-out  (48 → 4): evidence of 'wall'\nfor {free, wall} × {old, recent} claims", 0.055),
              ("Cell log-odds = bias + Σ over peers of the evidence\nof what each peer claimed about the cell", 0.055))
    for k, (name, h) in enumerate(blocks):
        box(ax, X0 + 0.045, y, W - 0.07, h, name, fs=7.9)
        mids.append(y + h / 2)
        if k < len(blocks) - 1:
            arrow(ax, cx + 0.01, y + h, cx + 0.01, y + h + 0.022)
        y += h + 0.022
    # feedback loop on the left edge of the block: cell log-odds -> soft counts of the next round
    lx = X0 + 0.02
    ax.plot([X0 + 0.045, lx, lx], [mids[-1], mids[-1], mids[0]], color=TEAL_, lw=1.4, zorder=4)
    arrow(ax, lx, mids[0], X0 + 0.045, mids[0], color=TEAL_, lw=1.4)
    for k, (txt, col) in enumerate((("leave-one-out feedback: a peer is checked against\nwhat everybody else implies, so a robot can verify a\npeer it never met through a peer it already trusts", TEAL_),
                                    ("in-context channel estimation, as in RACE-Net-V:\nhow does this peer's stream relate to the anchor", INK),
                                    ("the estimate of one peer depends on the others\n(who else is reliable, who repeats whom)", INK),
                                    ("signed: a reliable peer is trusted, a consistent liar\nis INVERTED and used, a random one is ignored", TEAL_),
                                    ("claims are conditionally independent given the\ntruth: evidence adds up over peers", INK))):
        note(ax, X0 + W - 0.025, mids[k], NX, mids[k], txt, fs=8, color=col)
    arrow(ax, cx, y - 0.022, cx, BY + BH + 0.03, color=AMBER, lw=1.6)
    box(ax, X0 + 0.03, BY + BH + 0.03, W - 0.06, 0.04, "log-odds that each reported cell is a wall", fc=AMBER_BG, ec=AMBER, lw=1.5, fs=9, bold=True, color=AMBER)
    arrow(ax, cx, BY + BH + 0.07, cx, BY + BH + 0.095)
    box(ax, X0 + 0.02, BY + BH + 0.095, W - 0.04, 0.05, "Planner: block a cell above 2 log-odds. If the goal then looks walled off,\nun-believe only the claims that enclose it (reachability repair)", fc="#f3f4f6", fs=7.8)
    box(ax, 0.01, 0.30, 0.20, 0.30, fc=GREY_BG, ec=GREY_EDGE, r=0.02, z=0)
    ax.text(0.11, 0.575, "replaces", ha="center", fontsize=10, fontweight="bold", color=INK)
    box(ax, 0.02, 0.47, 0.18, 0.08, "race_map:\nhand-written Bayesian\nmodel averaging over\n49 channel types", fs=7.6)
    box(ax, 0.02, 0.34, 0.18, 0.10, "RACE-Net-M:\nthe channel estimator\nis LEARNED from\nsimulated swarms", fs=7.6, fc="#d9f0ec", ec=TEAL_)
    arrow(ax, 0.11, 0.47, 0.11, 0.44)
    ax.text(0.03, 0.02, "Training: maze swarms with random two-coin liars and change points only. The benchmark's attack names never appear in training; "
            "the colluding ring attack is outside that family.", fontsize=7.2, color="#6b7280")
    fig.savefig(OUT / "racenet_m.png")
    plt.close(fig)


if __name__ == "__main__":
    racenet()
    racenet_m()
    cosmos()
    race_s()
    system()
    print(sorted(p.name for p in OUT.iterdir()))