"""Paper-style CTA framework figure — mimics images/2.jpg layout. Left panel: main pipeline (real/fake input thumbnails + audio waveform → backbones → dual cross-modal predictors → residuals → asym score → classifier). Right panel: CrossModalPredictor internals (N × TransformerDecoder with Q/K/V). Output: outputs/analysis/figs_framework/cta_framework_paper.{png,pdf} """ from __future__ import annotations from pathlib import Path import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt import matplotlib.image as mpimg from matplotlib.patches import FancyArrowPatch, FancyBboxPatch, Rectangle, Circle, Polygon from matplotlib.offsetbox import OffsetImage, AnnotationBbox import numpy as np ROOT = Path("/apdcephfs_gy4/share_303628665/joywu/research/fairtalking-second-work") OUT_DIR = ROOT / "outputs/analysis/figs_framework" THUMB_DIR = OUT_DIR / "_thumbs" OUT_DIR.mkdir(parents=True, exist_ok=True) # ----------------------------------------------------------------- palette C_REAL_EDGE = "#1E8449" C_FAKE_EDGE = "#C0392B" C_AUDIO = "#B7950B" C_BB_FILL = "#D6EAF8" C_BB_EDGE = "#2874A6" C_BB_TEXT = "#1B4F72" C_TOK_FILL = "#EAF2F8" C_TOK_EDGE = "#5499C7" C_PRED_FILL = "#F5CBA7" C_PRED_EDGE = "#BA4A00" C_PRED_TEXT = "#7B241C" C_LOSS_FILL = "#FCF3CF" C_LOSS_EDGE = "#B7950B" C_ASYM_FILL = "#D5F5E3" C_ASYM_EDGE = "#1E8449" C_CLS_FILL = "#E8DAEF" C_CLS_EDGE = "#7D3C98" C_ARROW = "#34495E" C_DETACH = "#C0392B" # ----------------------------------------------------------------- helpers def rounded(ax, cx, cy, w, h, fill, edge, lw=1.6, pad=0.02, zorder=2): r = FancyBboxPatch((cx-w/2, cy-h/2), w, h, boxstyle=f"round,pad={pad}", facecolor=fill, edgecolor=edge, linewidth=lw, zorder=zorder) ax.add_patch(r); return r def txt(ax, x, y, s, *, size=10, color="black", weight="normal", style="normal", ha="center", va="center", zorder=5): ax.text(x, y, s, ha=ha, va=va, fontsize=size, color=color, weight=weight, style=style, zorder=zorder) def arrow(ax, x1, y1, x2, y2, *, color=C_ARROW, lw=1.6, dashed=False, rad=0.0, mut=14, zorder=3): ls = (0, (5, 3)) if dashed else "-" ax.add_patch(FancyArrowPatch( (x1, y1), (x2, y2), arrowstyle="->", mutation_scale=mut, color=color, linewidth=lw, linestyle=ls, connectionstyle=f"arc3,rad={rad}", shrinkA=2, shrinkB=2, zorder=zorder)) def add_image(ax, img_path, cx, cy, size, *, edge=None, edge_lw=2.5, zorder=4): """Place an image centered at (cx, cy) with side length = size (data units).""" img = mpimg.imread(str(img_path)) ax.imshow(img, extent=(cx-size/2, cx+size/2, cy-size/2, cy+size/2), interpolation="bilinear", zorder=zorder, aspect="auto") if edge is not None: ax.add_patch(Rectangle((cx-size/2, cy-size/2), size, size, fill=False, edgecolor=edge, linewidth=edge_lw, zorder=zorder+1)) def snowflake(ax, x, y, r=0.5, color="#3498DB"): """Small snowflake ~ frozen indicator.""" for ang in range(0, 360, 60): rad = np.deg2rad(ang) ax.plot([x, x + r*np.cos(rad)], [y, y + r*np.sin(rad)], color=color, lw=1.4, zorder=6, solid_capstyle="round") ax.add_patch(Circle((x, y), 0.13, color=color, zorder=7)) def detach_perp(ax, x, y, size=1.2, color=C_DETACH): ax.plot([x-size/2, x+size/2], [y, y], color=color, lw=2.6, zorder=6) ax.plot([x, x], [y-size/2.6, y+size/2.6], color=color, lw=2.6, zorder=6) def token_grid(ax, cx, cy, cols, rows, cell=0.55, color_a=C_TOK_FILL, color_b=C_TOK_EDGE, gap=0.05): """Small grid of squares -> a stack of tokens.""" total_w = cols*cell + (cols-1)*gap total_h = rows*cell + (rows-1)*gap x0 = cx - total_w/2 y0 = cy - total_h/2 for i in range(rows): for j in range(cols): xx = x0 + j*(cell+gap) yy = y0 + i*(cell+gap) ax.add_patch(Rectangle((xx, yy), cell, cell, facecolor=color_a, edgecolor=color_b, linewidth=0.9, zorder=4)) def isometric_stack(ax, cx, cy, w, h, depth=0.9, layers=3, fill=C_BB_FILL, edge=C_BB_EDGE): """Draw an isometric (3D) stacked block for a backbone visualization.""" off = depth # back layers first for k in range(layers, 0, -1): dx = k*off*0.35; dy = k*off*0.35 ax.add_patch(Rectangle((cx-w/2+dx, cy-h/2+dy), w, h, facecolor=fill, edgecolor=edge, linewidth=1.4, zorder=2)) # front (main) layer ax.add_patch(FancyBboxPatch((cx-w/2, cy-h/2), w, h, boxstyle="round,pad=0.02", facecolor=fill, edgecolor=edge, linewidth=2.2, zorder=3)) # ----------------------------------------------------------------- canvas fig = plt.figure(figsize=(22, 12)) gs = fig.add_gridspec(1, 1) ax = fig.add_subplot(gs[0, 0]) ax.set_xlim(0, 100) ax.set_ylim(0, 52) ax.set_aspect("equal") ax.axis("off") # background separators for main / inset panel (subtle) ax.add_patch(Rectangle((0.5, 0.5), 71, 51, fill=False, edgecolor="#BDC3C7", linewidth=1.0, linestyle=(0, (2, 3)), zorder=1)) ax.add_patch(Rectangle((72, 0.5), 27.5, 51, fill=False, edgecolor="#BDC3C7", linewidth=1.0, linestyle=(0, (2, 3)), zorder=1)) txt(ax, 36, 50.4, "CTA — Cross-modal Translation Asymmetry", size=15, weight="bold") txt(ax, 36, 48.7, "Real / Fake share the SAME real audio; only video differs.", size=9.5, style="italic", color="#566573") txt(ax, 85.75, 50.4, "Cross-modal Predictor (module inset)", size=12.5, weight="bold", color=C_PRED_TEXT) # ================================================================= INPUTS # left column: real video + audio (label 0) # right column: fake video + audio (label 1) # audio row is shared (same waveform, same label as ALWAYS REAL) y_vid = 42 y_aud = 34 thumb_size = 5.0 add_image(ax, THUMB_DIR/"real.png", 6.5, y_vid, thumb_size, edge=C_REAL_EDGE) add_image(ax, THUMB_DIR/"fake.png", 15.5, y_vid, thumb_size, edge=C_FAKE_EDGE) txt(ax, 6.5, y_vid + 3.2, "Real video", size=9.5, color=C_REAL_EDGE, weight="bold") txt(ax, 6.5, y_vid - 3.4, "label = 0", size=8.5, color=C_REAL_EDGE) txt(ax, 15.5, y_vid + 3.2, "Fake video", size=9.5, color=C_FAKE_EDGE, weight="bold") txt(ax, 15.5, y_vid - 3.4, "label = 1", size=8.5, color=C_FAKE_EDGE) # audio: single row, spans both columns ax.add_patch(FancyBboxPatch((3.5, y_aud-2.3), 14, 4.6, boxstyle="round,pad=0.05", facecolor="#FCF3CF", edgecolor=C_AUDIO, linewidth=2.0, zorder=3)) add_image(ax, THUMB_DIR/"audio_wave.png", 11, y_aud, 12, edge=None, edge_lw=0) txt(ax, 11, y_aud + 3.4, "Real audio (shared for real & fake)", size=9.5, color=C_AUDIO, weight="bold") txt(ax, 11, y_aud - 3.4, "★ generator only synthesizes video", size=8.5, color="#7E5109", style="italic") # ================================================================= BACKBONES y_bb = 25 # video backbone isometric_stack(ax, 8, y_bb, 8, 6, depth=0.9, layers=3, fill=C_BB_FILL, edge=C_BB_EDGE) txt(ax, 8, y_bb+0.9, "VideoMAE-base", size=10, color=C_BB_TEXT, weight="bold") txt(ax, 8, y_bb-0.6, "(1568 × 768)", size=8.5, color=C_BB_TEXT, style="italic") snowflake(ax, 4.7, y_bb+2.2) txt(ax, 3.7, y_bb+2.2, "70%", size=7.5, color="#2874A6", weight="bold", ha="right") # audio backbone isometric_stack(ax, 15, y_bb, 6, 4.5, depth=0.7, layers=3, fill=C_BB_FILL, edge=C_BB_EDGE) txt(ax, 15, y_bb+0.55, "Wav2Vec2", size=9.5, color=C_BB_TEXT, weight="bold") txt(ax, 15, y_bb-0.7, "(127 × 768)", size=8, color=C_BB_TEXT, style="italic") snowflake(ax, 12.2, y_bb+1.7) txt(ax, 11.3, y_bb+1.7, "80%", size=7.5, color="#2874A6", weight="bold", ha="right") # arrows: video → v backbone (real+fake merge), audio → a backbone arrow(ax, 6.5, y_vid-thumb_size/2, 8, y_bb+3, color=C_REAL_EDGE, lw=1.7) arrow(ax, 15.5, y_vid-thumb_size/2, 8, y_bb+3, color=C_FAKE_EDGE, lw=1.7, dashed=True) arrow(ax, 11, y_aud-2.3, 15, y_bb+2.2, color=C_AUDIO, lw=1.7) # ================================================================= TOKEN STACKS (v.tokens / a.tokens) y_tok = 25 token_grid(ax, 22, y_tok+2, cols=8, rows=3, cell=0.5, gap=0.06, color_a="#D6EAF8", color_b=C_BB_EDGE) txt(ax, 22, y_tok+4.7, "v.tokens (768-d)", size=8.5, color=C_BB_TEXT, weight="bold") token_grid(ax, 22, y_tok-2.5, cols=8, rows=3, cell=0.5, gap=0.06, color_a="#FCF3CF", color_b=C_AUDIO) txt(ax, 22, y_tok-4.9, "a.tokens (768-d)", size=8.5, color="#7E5109", weight="bold") arrow(ax, 12, y_bb+3, 19, y_tok+2, lw=1.4) arrow(ax, 18, y_bb-1, 19, y_tok-2.5, lw=1.4) # ================================================================= DUAL PREDICTORS (with explicit projection layers) # Each predictor is a big rounded box that shows: src_proj + tgt_proj → (Decoder × 4) → out_proj # f_{A→V} (top branch) def draw_predictor(cx, cy, direction="AV"): w, h = 22, 10.5 rounded(ax, cx, cy, w, h, C_PRED_FILL, C_PRED_EDGE, lw=2.4, pad=0.03, zorder=3) if direction == "AV": title = r"$f_{A \to V}$ Cross-modal Predictor" qtok = "tgt = v.tokens" kvtok = "src = a.tokens" pred = "v_pred (B, 1568, 768)" else: title = r"$f_{V \to A}$ Cross-modal Predictor" qtok = "tgt = a.tokens" kvtok = "src = v.tokens" pred = "a_pred (B, 127, 768)" txt(ax, cx, cy+4.35, "★ " + title, size=11, weight="bold", color=C_PRED_TEXT) # explicit projection layers on the LEFT edge, one per input # tgt_proj (Q side) rounded(ax, cx-8.6, cy+2.3, 4.4, 2.0, "white", C_PRED_EDGE, lw=1.2, pad=0.02, zorder=4) txt(ax, cx-8.6, cy+2.6, "tgt_proj", size=7.4, weight="bold", color=C_PRED_TEXT) txt(ax, cx-8.6, cy+1.8, "768→512", size=6.6, style="italic", color="#566573") txt(ax, cx-8.6, cy+3.7, qtok, size=6.6, style="italic", color="#34495E") # src_proj (K,V side) rounded(ax, cx-8.6, cy-2.3, 4.4, 2.0, "white", C_PRED_EDGE, lw=1.2, pad=0.02, zorder=4) txt(ax, cx-8.6, cy-2.0, "src_proj", size=7.4, weight="bold", color=C_PRED_TEXT) txt(ax, cx-8.6, cy-2.8, "768→512", size=6.6, style="italic", color="#566573") txt(ax, cx-8.6, cy-3.9, kvtok, size=6.6, style="italic", color="#34495E") # decoder-stack "N ×" box in the middle rounded(ax, cx-1.5, cy+0.3, 8.5, 5.6, "white", C_PRED_EDGE, lw=1.4, pad=0.02, zorder=4) txt(ax, cx-1.5, cy+2.4, "Decoder Block × 4", size=8.4, weight="bold", color=C_PRED_TEXT) txt(ax, cx-1.5, cy+1.1, "MHSA(Q)", size=7.2, color=C_PRED_TEXT) txt(ax, cx-1.5, cy-0.1, "CrossAttn(Q; K,V)", size=7.2, color=C_PRED_TEXT) txt(ax, cx-1.5, cy-1.3, "FFN 512→2048→512", size=7.2, color=C_PRED_TEXT) txt(ax, cx-1.5, cy-2.4, "pre-LN + residual", size=6.6, style="italic", color="#7B241C") # arrows: proj → decoder-stack (Q top, K,V bottom) arrow(ax, cx-6.2, cy+2.3, cx-5.7, cy+1.8, lw=1.1, color=C_PRED_EDGE) # tgt_proj -> stack arrow(ax, cx-6.2, cy-2.3, cx-5.7, cy-1.2, lw=1.1, color=C_PRED_EDGE, dashed=True) # src_proj -> stack # out_proj (right edge) rounded(ax, cx+6.5, cy, 4.4, 2.4, "white", C_PRED_EDGE, lw=1.2, pad=0.02, zorder=4) txt(ax, cx+6.5, cy+0.4, "out_proj", size=7.4, weight="bold", color=C_PRED_TEXT) txt(ax, cx+6.5, cy-0.5, "512→768", size=6.6, style="italic", color="#566573") arrow(ax, cx+3.0, cy+0.3, cx+4.3, cy, lw=1.1, color=C_PRED_EDGE) # prediction label leaving the box txt(ax, cx+9.2, cy-3.4, pred, size=7.6, weight="bold", color=C_PRED_TEXT, ha="right") # REAL-only training badge along bottom txt(ax, cx, cy-4.9, "trained on REAL pairs only", size=7.8, color=C_REAL_EDGE, weight="bold", style="italic") draw_predictor(35, 32, direction="AV") draw_predictor(35, 15, direction="VA") # tokens → predictors (Q vs KV) — routed to each proj entry point arrow(ax, 25, 27, 25.4, 34.3, lw=1.3) # v.tokens → tgt_proj of A→V arrow(ax, 25, 22.5, 25.4, 29.7, lw=1.0, dashed=True) # a.tokens → src_proj of A→V arrow(ax, 25, 22.5, 25.4, 17.3, lw=1.3) # a.tokens → tgt_proj of V→A arrow(ax, 25, 27, 25.4, 12.7, lw=1.0, dashed=True) # v.tokens → src_proj of V→A # ================================================================= RESIDUALS # L_AV & L_VA w_l, h_l = 12, 4.5 rounded(ax, 54, 32, w_l, h_l, C_LOSS_FILL, C_LOSS_EDGE, lw=1.8) txt(ax, 54, 33.2, r"$L_{A \to V}$", size=11.5, weight="bold") txt(ax, 54, 31.5, "MSE(v_pred, v.tokens)", size=8.5, style="italic", color="#7E5109") txt(ax, 54, 30.0, "real 0.001 fake 0.048", size=7.5, color="#1B4F72", weight="bold") rounded(ax, 54, 15, w_l, h_l, C_LOSS_FILL, C_LOSS_EDGE, lw=1.8) txt(ax, 54, 16.2, r"$L_{V \to A}$", size=11.5, weight="bold") txt(ax, 54, 14.5, "MSE(a_pred, a.tokens)", size=8.5, style="italic", color="#7E5109") txt(ax, 54, 13.0, "real 0.0004 fake 0.013", size=7.5, color="#1B4F72", weight="bold") arrow(ax, 46.5, 32, 48, 32, lw=1.6) arrow(ax, 46.5, 15, 48, 15, lw=1.6) # ================================================================= s_asym & L_total rounded(ax, 65, 23.5, 14, 6, C_ASYM_FILL, C_ASYM_EDGE, lw=2.4, pad=0.03) txt(ax, 65, 25.5, r"★ $s_{\rm asym}$ = $L_{V \to A}$ − $L_{A \to V}$", size=10.0, weight="bold", color=C_REAL_EDGE) txt(ax, 65, 23.5, r"$L_{\rm total}$ = $L_{V \to A}$ + $L_{A \to V}$", size=9.2, color=C_REAL_EDGE) txt(ax, 65, 21.5, "real ≈ 0 fake ≈ −0.23 (× 45)", size=8.2, color="#1B4F72", weight="bold") arrow(ax, 60, 32, 62, 25.7, lw=1.4) arrow(ax, 60, 15, 62, 22, lw=1.4) # ================================================================= detach + classifier # arrow s_asym → down toward classifier, with detach barrier arrow(ax, 65, 20.5, 65, 12, lw=1.6) detach_perp(ax, 65, 18.8, size=1.6) txt(ax, 68, 18.8, ".detach()", size=9, color=C_DETACH, weight="bold", style="italic", ha="left") txt(ax, 68, 17.4, "cuts BCE ↛ predictor", size=7.6, color=C_DETACH, style="italic", ha="left") # classifier head w_c, h_c = 15, 6 rounded(ax, 65, 8, w_c, h_c, C_CLS_FILL, C_CLS_EDGE, lw=2.2, pad=0.03) txt(ax, 65, 9.7, "Classifier Head", size=10.5, weight="bold", color=C_CLS_EDGE) txt(ax, 65, 8.2, "MLP (1538 → 256 → 1)", size=8.8, color=C_CLS_EDGE) txt(ax, 65, 6.7, r"in = [v.pooled, a.pooled, $s_{\rm asym}$, $L_{\rm total}$]", size=7.8, color=C_CLS_EDGE, style="italic") # pooled features feed in arrow(ax, 12, y_bb-3.2, 58, 8, lw=0.9, rad=-0.28, color="#7F8C8D") arrow(ax, 18, y_bb-2.4, 58, 8, lw=0.9, rad=-0.20, color="#7F8C8D") txt(ax, 32, 5.2, "v.pooled / a.pooled (long skip)", size=7.5, color="#7F8C8D", style="italic") # score rounded(ax, 65, 2, 15, 3.4, "#F9E79F", "#B7950B", lw=1.8) txt(ax, 65, 2.4, "score = σ(logit) ∈ [0, 1]", size=9.5, weight="bold", color="#7E5109") txt(ax, 65, 0.9, "high ⇒ fake", size=8, color="#7E5109", style="italic") arrow(ax, 65, 5, 65, 3.7, lw=1.6) # ================================================================= mini panel: real / fake s_asym distribution # tiny embedded histogram to preview signal mini_x, mini_y, mw, mh = 54, 46, 15, 4.2 ax.add_patch(Rectangle((mini_x-mw/2, mini_y-mh/2), mw, mh, facecolor="white", edgecolor="#95A5A6", linewidth=1.0, zorder=3)) # fake gaussian (left, negative), real gaussian (right, near 0) xs = np.linspace(-0.35, 0.05, 200) def g(mu, s): return np.exp(-((xs-mu)/s)**2 / 2) fake = g(-0.228, 0.05); fake /= fake.max() * 1.3 real = g(-0.005, 0.02); real /= real.max() * 1.3 xr = mini_x-mw/2 + (xs+0.35)/0.4 * mw # map [-0.35,0.05] to full width ax.fill_between(xr, mini_y-mh/2+0.1, mini_y-mh/2+0.1 + fake*mh*0.8, color=C_FAKE_EDGE, alpha=0.55, zorder=4) ax.fill_between(xr, mini_y-mh/2+0.1, mini_y-mh/2+0.1 + real*mh*0.8, color=C_REAL_EDGE, alpha=0.55, zorder=4) txt(ax, mini_x, mini_y+mh/2+0.9, r"$s_{\rm asym}$ distribution (val)", size=8.5, weight="bold") txt(ax, mini_x-mw/2+2.5, mini_y-mh/2+0.6, "fake", size=7.5, color=C_FAKE_EDGE, weight="bold", ha="left") txt(ax, mini_x+mw/2-2.5, mini_y-mh/2+0.6, "real", size=7.5, color=C_REAL_EDGE, weight="bold", ha="right") # axis marker ax.plot([mini_x-mw/2, mini_x+mw/2], [mini_y-mh/2+0.1, mini_y-mh/2+0.1], color="#7F8C8D", lw=0.8, zorder=5) # 0 line zero_x = mini_x-mw/2 + (0+0.35)/0.4 * mw ax.plot([zero_x, zero_x], [mini_y-mh/2+0.1, mini_y+mh/2-0.4], color="#7F8C8D", lw=0.8, linestyle=":", zorder=5) txt(ax, zero_x, mini_y-mh/2-0.3, "0", size=7.5, color="#7F8C8D", ha="center", va="top") # ================================================================= RIGHT PANEL — CrossModalPredictor internals # One decoder block fully expanded (pre-LN + 3 residuals + MHSA + CrossAttn + FFN), # stacked with a "× N=4" wrapper. Inputs go through explicit tgt_proj / src_proj first; # stack output goes through out_proj to modality dim. xp0 = 74 # left edge of inset xp1 = 99 # right edge xp_c = (xp0+xp1)/2 # Two thin input strips at the top: tgt (Q) and src (K,V) rounded(ax, xp0+3, 46, 4, 2.4, "#EAF2F8", C_BB_EDGE, lw=1.4) txt(ax, xp0+3, 46, "tgt tokens", size=7.6, weight="bold") rounded(ax, xp1-3, 46, 4, 2.4, "#FCF3CF", C_AUDIO, lw=1.4) txt(ax, xp1-3, 46, "src tokens", size=7.6, weight="bold") # tgt_proj / src_proj row rounded(ax, xp0+3, 42.4, 4.4, 2.2, "white", C_PRED_EDGE, lw=1.2) txt(ax, xp0+3, 42.7, "tgt_proj", size=7.4, weight="bold", color=C_PRED_TEXT) txt(ax, xp0+3, 41.8, "768→512", size=6.4, style="italic", color="#566573") rounded(ax, xp1-3, 42.4, 4.4, 2.2, "white", C_PRED_EDGE, lw=1.2) txt(ax, xp1-3, 42.7, "src_proj", size=7.4, weight="bold", color=C_PRED_TEXT) txt(ax, xp1-3, 41.8, "768→512", size=6.4, style="italic", color="#566573") arrow(ax, xp0+3, 44.7, xp0+3, 43.6, lw=1.1) arrow(ax, xp1-3, 44.7, xp1-3, 43.6, lw=1.1, dashed=True) # ×N wrapper (dashed rectangle around the block) wrap_x0, wrap_x1 = xp0+0.6, xp1-0.6 wrap_y0, wrap_y1 = 8.4, 40.6 ax.add_patch(Rectangle((wrap_x0, wrap_y0), wrap_x1-wrap_x0, wrap_y1-wrap_y0, facecolor="none", edgecolor=C_PRED_EDGE, linewidth=1.4, linestyle=(0, (5, 3)), zorder=3)) txt(ax, xp1-1.5, wrap_y1-1.0, "N × 4", size=9.5, weight="bold", color=C_PRED_TEXT, ha="right") # ---- One expanded Decoder Block ---- # Central spine x, three sub-blocks vertically, each with pre-LN + module + residual. sx = xp_c # spine (main data flow) lx = xp0+2.4 # left column: LayerNorms mx = xp_c # middle column: modules plus_r = 0.55 def ln_box(cy, label): rounded(ax, lx, cy, 4.6, 1.9, "#F4ECF7", "#7D3C98", lw=1.0) txt(ax, lx, cy, label, size=7.0, weight="bold", color="#4A235A") def module_box(cy, h, main, sub, fill="#FDEBD0"): rounded(ax, mx+2.5, cy, 10.5, h, fill, C_PRED_EDGE, lw=1.4) txt(ax, mx+2.5, cy+0.55, main, size=7.9, weight="bold", color=C_PRED_TEXT) txt(ax, mx+2.5, cy-0.55, sub, size=6.8, style="italic", color=C_PRED_TEXT) def plus_node(cy): ax.add_patch(Circle((sx-2.6, cy), plus_r, facecolor="white", edgecolor=C_PRED_EDGE, linewidth=1.4, zorder=6)) txt(ax, sx-2.6, cy, "+", size=10.5, weight="bold", color=C_PRED_TEXT) # vertical positions inside the block y_in = 39.0 # top entry (after tgt_proj → x) y_ln1 = 36.6 y_mha = 34.4 y_res1 = 32.2 y_ln2 = 29.9 y_ca = 27.7 y_res2 = 25.5 y_ln3 = 23.0 y_ffn = 20.6 y_res3 = 18.4 y_out = 15.7 # Down spine (thick line across the block, showing the residual highway) ax.plot([sx-2.6, sx-2.6], [y_in, y_out], color=C_PRED_EDGE, lw=2.0, zorder=4) # Sub-block 1: LN1 → MHSA → + ln_box(y_ln1, "LayerNorm₁") module_box(y_mha, 1.9, "Multi-Head Self-Attn", "Q = K = V = x (heads=8)") plus_node(y_res1) # arrows: spine → LN → MHSA → module → back-to-plus (the residual) arrow(ax, sx-2.6, y_ln1+0.6, lx+2.3, y_ln1, lw=1.0, color=C_PRED_EDGE) arrow(ax, lx+2.3, y_ln1, mx+2.5-5.25, y_mha, lw=1.0, color=C_PRED_EDGE) arrow(ax, mx+2.5-5.25, y_mha, sx-2.6, y_res1, lw=1.0, color=C_PRED_EDGE, rad=-0.15) txt(ax, sx-4.1, (y_in+y_res1)/2, "residual", size=6.4, style="italic", color=C_PRED_TEXT, ha="right", va="center") # Sub-block 2: LN2 → CrossAttn → + (this one takes memory from src) ln_box(y_ln2, "LayerNorm₂") module_box(y_ca, 1.9, "Multi-Head Cross-Attn", "Q = x , K, V = memory") plus_node(y_res2) arrow(ax, sx-2.6, y_ln2+0.6, lx+2.3, y_ln2, lw=1.0, color=C_PRED_EDGE) arrow(ax, lx+2.3, y_ln2, mx+2.5-5.25, y_ca, lw=1.0, color=C_PRED_EDGE) arrow(ax, mx+2.5-5.25, y_ca, sx-2.6, y_res2, lw=1.0, color=C_PRED_EDGE, rad=-0.15) # cross-attention memory injection (from src side, curved in) arrow(ax, xp1-3, 41.3, mx+2.5+5.25, y_ca, lw=1.0, color=C_AUDIO, dashed=True, rad=-0.35) txt(ax, mx+2.5+5.9, y_ca-1.6, "memory", size=6.6, style="italic", color=C_AUDIO, ha="left") # Sub-block 3: LN3 → FFN → + ln_box(y_ln3, "LayerNorm₃") module_box(y_ffn, 1.9, "FFN (2-layer, GELU)", "512 → 2048 → 512 , dropout 0.1") plus_node(y_res3) arrow(ax, sx-2.6, y_ln3+0.6, lx+2.3, y_ln3, lw=1.0, color=C_PRED_EDGE) arrow(ax, lx+2.3, y_ln3, mx+2.5-5.25, y_ffn, lw=1.0, color=C_PRED_EDGE) arrow(ax, mx+2.5-5.25, y_ffn, sx-2.6, y_res3, lw=1.0, color=C_PRED_EDGE, rad=-0.15) # down to output tokens arrow(ax, sx-2.6, y_res3-plus_r, sx-2.6, y_out, lw=1.6, color=C_PRED_EDGE) # entry into the block arrow(ax, xp0+3, 41.3, sx-2.6, y_in+0.6, lw=1.4, color=C_PRED_EDGE, rad=-0.15) # out_proj + output rounded(ax, xp_c, 13.5, 8, 2.6, "white", C_PRED_EDGE, lw=1.2) txt(ax, xp_c, 14.0, "out_proj", size=7.6, weight="bold", color=C_PRED_TEXT) txt(ax, xp_c, 12.9, "512 → 768", size=6.6, style="italic", color="#566573") arrow(ax, sx-2.6, y_out, xp_c, 14.7, lw=1.3, color=C_PRED_EDGE) rounded(ax, xp_c, 10.6, 12, 2.4, "#D5F5E3", C_REAL_EDGE, lw=1.5) txt(ax, xp_c, 11.0, "output tokens = v_pred / a_pred", size=8.2, weight="bold", color=C_REAL_EDGE) txt(ax, xp_c, 10.0, "same length as tgt tokens, dim = 768", size=6.6, style="italic", color=C_REAL_EDGE) arrow(ax, xp_c, 12.2, xp_c, 11.7, lw=1.2) # training badge ax.add_patch(FancyBboxPatch((xp0+0.5, 5), 24, 3.0, boxstyle="round,pad=0.05", facecolor="#D5F5E3", edgecolor=C_REAL_EDGE, linewidth=1.5, zorder=3)) txt(ax, xp_c, 6.5, "★ REAL only (is_real mask) → learns real manifold", size=8.5, weight="bold", color=C_REAL_EDGE) # hyperparams strip along the very bottom of the inset txt(ax, xp_c, 3.2, "d_model=512 heads=8 FFN=2048 depth=N=4", size=8.0, color=C_PRED_TEXT, weight="bold") txt(ax, xp_c, 1.8, "pre-LN + 3 residual connections (norm_first=True)", size=7.2, color=C_PRED_TEXT, style="italic") # ================================================================= LEGEND (bottom left) lgx, lgy = 3, 12 snowflake(ax, lgx, lgy, r=0.5) txt(ax, lgx+1.2, lgy, "= frozen ratio", size=8, ha="left") detach_perp(ax, lgx, lgy-2, size=1.2) txt(ax, lgx+1.2, lgy-2, "= .detach() barrier", size=8, ha="left", color=C_DETACH) ax.plot([lgx-0.5, lgx+0.5], [lgy-4, lgy-4], color=C_ARROW, lw=1.6) txt(ax, lgx+1.2, lgy-4, "= forward flow", size=8, ha="left") ax.plot([lgx-0.5, lgx+0.5], [lgy-6, lgy-6], color=C_ARROW, lw=1.6, linestyle=(0,(4,3))) txt(ax, lgx+1.2, lgy-6, "= optional / weak flow", size=8, ha="left") # ================================================================= save plt.tight_layout(pad=0.5) out_png = OUT_DIR / "cta_framework_paper.png" out_pdf = OUT_DIR / "cta_framework_paper.pdf" plt.savefig(out_png, dpi=280, bbox_inches="tight", facecolor="white") plt.savefig(out_pdf, bbox_inches="tight", facecolor="white") plt.close(fig) print("saved:", out_png) print("saved:", out_pdf)