summaryrefslogtreecommitdiff
path: root/experiments/snapshot_evolution_residual_explosion.py
blob: 1dc09f20f9b9aa468394a72392c5dc32ac1e1c29 (plain)
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
"""
Snapshot evolution: per-epoch logging of residual-stream norms and BP-gradient norms
during BP and DFA training of a 4-block d=256 ResMLP on CIFAR-10.

Goal: confirm that ||h_l||_2 grows monotonically over epochs in DFA but stays
bounded in BP, and that ||BP_grad||_2 collapses correspondingly. This generates
the killer figure for the P4 (residual-stream pathology) finding in the
NeurIPS 2026 FA Evaluation paper.

Usage:
    CUDA_VISIBLE_DEVICES=2 nohup python experiments/snapshot_evolution_residual_explosion.py \
        --output_dir results/snapshot_evolution_v2 > results/snapshot_evolution_v2.log 2>&1 &
"""
import os, sys, json, argparse, time
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import DataLoader
import torchvision
import torchvision.transforms as transforms

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from models.residual_mlp import ResidualMLP
from metrics.credit_metrics import cosine_similarity_batch


def get_cifar10(batch_size=128, num_workers=2):
    tv = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2470, 0.2435, 0.2616)),
    ])
    tv_train = transforms.Compose([
        transforms.RandomCrop(32, padding=4),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2470, 0.2435, 0.2616)),
    ])
    tr = torchvision.datasets.CIFAR10('./data', True,  download=True, transform=tv_train)
    te = torchvision.datasets.CIFAR10('./data', False, download=True, transform=tv)
    return (DataLoader(tr, batch_size=batch_size, shuffle=True,  num_workers=num_workers),
            DataLoader(te, batch_size=batch_size, shuffle=False, num_workers=num_workers))


def fixed_eval_buffer(test_loader, device, n_samples=1024):
    xs, ys = [], []
    for x, y in test_loader:
        xs.append(x.view(x.size(0), -1)); ys.append(y)
        if sum(xb.size(0) for xb in xs) >= n_samples:
            break
    x = torch.cat(xs)[:n_samples].to(device)
    y = torch.cat(ys)[:n_samples].to(device)
    return x, y


def diagnose(model, x_eval, y_eval, dfa_Bs=None):
    """
    Returns dict with:
      - hidden_norms: list of L+1 floats, median per-sample ||h_l||_2 on eval buffer
      - bp_grad_norms: list of L+1 floats, median per-sample ||g_l||_2 (BP grad)
      - bp_grad_norms_F: list of L+1 floats, ||g_l||_F per layer (Frobenius)
      - gamma_dfa: mean cosine over layers between DFA credit and BP grad (only if dfa_Bs given)
      - acc: test accuracy on the eval buffer
      - loss: mean CE on the eval buffer
    Critically: ALL norms use .norm(dim=-1), never .norm(-1).
    """
    was_training = model.training
    model.eval()
    L = model.num_blocks
    C = 10
    bs = x_eval.size(0)

    # Hidden states (no grad)
    with torch.no_grad():
        _, hiddens = model(x_eval, return_hidden=True)
    hidden_norms = [h.norm(dim=-1).median().item() for h in hiddens]

    # BP gradients via manual graph, with x_eval as the input
    h0 = model.embed(x_eval.detach())
    hs = [h0.clone().requires_grad_(True)]
    for b in model.blocks:
        hs.append(hs[-1] + b(hs[-1]))
    logits = model.out_head(model.out_ln(hs[-1]))
    loss = F.cross_entropy(logits, y_eval)
    grads = torch.autograd.grad(loss, hs)
    bp_grad_per_sample_l2 = [g.norm(dim=-1).median().item() for g in grads]
    bp_grad_F = [g.norm().item() for g in grads]
    bp_grad_full = [g.detach() for g in grads]

    acc = (logits.argmax(-1) == y_eval).float().mean().item()
    loss_val = loss.item()

    # DFA credit cosine to BP grad, if requested.
    # Convention (matches confirmatory_paper_experiments.compute_diagnostics_generic):
    # DFA's a_l represents the credit at the *input* to block l, which is h_l, so it
    # is compared against bp_grad_full[l] (gradient at h_l = input to block l).
    gamma_dfa = float('nan')
    if dfa_Bs is not None:
        with torch.no_grad():
            e_T = logits.softmax(dim=-1)
            e_T[torch.arange(bs), y_eval] -= 1.0
        cos_per_layer = []
        for l in range(L):
            a_dfa = (e_T @ dfa_Bs[l].T).detach()
            cos_per_layer.append(cosine_similarity_batch(a_dfa, bp_grad_full[l]))
        gamma_dfa = float(np.mean(cos_per_layer))

    if was_training:
        model.train()

    return {
        'hidden_norms': hidden_norms,
        'bp_grad_norms_per_sample_med': bp_grad_per_sample_l2,
        'bp_grad_norms_F': bp_grad_F,
        'gamma_dfa': gamma_dfa,
        'acc_eval': acc,
        'loss_eval': loss_val,
    }


def train_bp(model, train_loader, x_eval, y_eval, device, epochs, lr, wd, log_every=1):
    optimizer = optim.AdamW(model.parameters(), lr=lr, weight_decay=wd)
    scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=epochs)
    log = []
    # Epoch 0 (pre-training)
    d0 = diagnose(model, x_eval, y_eval)
    d0['epoch'] = 0
    log.append(d0)
    print(f"  [BP] Ep 0: ||h||_med={d0['hidden_norms']} ||g||_med={d0['bp_grad_norms_per_sample_med']} acc={d0['acc_eval']:.4f}", flush=True)
    for epoch in range(1, epochs + 1):
        model.train()
        for x, y in train_loader:
            x = x.view(x.size(0), -1).to(device)
            y = y.to(device)
            logits = model(x)
            loss = F.cross_entropy(logits, y)
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
        scheduler.step()
        if epoch % log_every == 0 or epoch == epochs:
            d = diagnose(model, x_eval, y_eval)
            d['epoch'] = epoch
            log.append(d)
            print(f"  [BP] Ep {epoch}: ||h_L||={d['hidden_norms'][-1]:.3e} "
                  f"||g_2||={d['bp_grad_norms_per_sample_med'][2]:.3e} "
                  f"acc={d['acc_eval']:.4f}", flush=True)
    return log


def train_dfa(model, train_loader, x_eval, y_eval, device, epochs, lr, wd, log_every=1,
              random_targets: bool = False):
    d_hidden = model.d_hidden
    L = model.num_blocks
    C = 10
    Bs = [torch.randn(d_hidden, C, device=device) / np.sqrt(C) for _ in range(L)]
    block_opts = [optim.AdamW(block.parameters(), lr=lr, weight_decay=wd) for block in model.blocks]
    embed_opt = optim.AdamW(model.embed.parameters(), lr=lr, weight_decay=wd)
    head_opt = optim.AdamW(list(model.out_head.parameters()) + list(model.out_ln.parameters()),
                           lr=lr, weight_decay=wd)
    all_sch = ([optim.lr_scheduler.CosineAnnealingLR(o, T_max=epochs) for o in block_opts]
               + [optim.lr_scheduler.CosineAnnealingLR(embed_opt, T_max=epochs),
                  optim.lr_scheduler.CosineAnnealingLR(head_opt, T_max=epochs)])
    log = []
    d0 = diagnose(model, x_eval, y_eval, dfa_Bs=Bs)
    d0['epoch'] = 0
    log.append(d0)
    print(f"  [DFA] Ep 0: ||h||_med={d0['hidden_norms']} ||g||_med={d0['bp_grad_norms_per_sample_med']} acc={d0['acc_eval']:.4f}", flush=True)
    for epoch in range(1, epochs + 1):
        model.train()
        for x, y in train_loader:
            x = x.view(x.size(0), -1).to(device)
            y = y.to(device)
            if random_targets:
                # iid random class targets refreshed every minibatch (codex round 34 sharper variant)
                y = torch.randint(0, 10, y.shape, device=device)
            batch = x.size(0)
            with torch.no_grad():
                logits, hiddens = model(x, return_hidden=True)
                e_T = logits.softmax(dim=-1)
                e_T[torch.arange(batch), y] -= 1
            hL_det = hiddens[-1].detach()
            logits_out = model.out_head(model.out_ln(hL_det))
            loss_out = F.cross_entropy(logits_out, y)
            head_opt.zero_grad(); loss_out.backward(); head_opt.step()
            for l in range(L):
                h_l = hiddens[l].detach()
                a_dfa = (e_T @ Bs[l].T).detach()
                rms = (a_dfa ** 2).mean(dim=-1, keepdim=True).sqrt() + 1e-6
                a_norm = a_dfa / rms
                f_l = model.blocks[l](h_l)
                local_loss = (f_l * a_norm).sum(dim=-1).mean()
                block_opts[l].zero_grad(); local_loss.backward()
                torch.nn.utils.clip_grad_norm_(model.blocks[l].parameters(), 1.0)
                block_opts[l].step()
            a_0 = (e_T @ Bs[0].T).detach()
            rms_0 = (a_0 ** 2).mean(dim=-1, keepdim=True).sqrt() + 1e-6
            h0 = model.embed(x)
            embed_loss = (h0 * (a_0 / rms_0)).sum(dim=-1).mean()
            embed_opt.zero_grad(); embed_loss.backward(); embed_opt.step()
        for s in all_sch:
            s.step()
        if epoch % log_every == 0 or epoch == epochs:
            d = diagnose(model, x_eval, y_eval, dfa_Bs=Bs)
            d['epoch'] = epoch
            log.append(d)
            print(f"  [DFA] Ep {epoch}: ||h_L||={d['hidden_norms'][-1]:.3e} "
                  f"||g_2||={d['bp_grad_norms_per_sample_med'][2]:.3e} "
                  f"acc={d['acc_eval']:.4f} gamma_dfa={d['gamma_dfa']:.4f}", flush=True)
    return log


def main():
    p = argparse.ArgumentParser()
    p.add_argument('--output_dir', type=str, default='results/snapshot_evolution_v2')
    p.add_argument('--epochs', type=int, default=100)
    p.add_argument('--lr', type=float, default=1e-3)
    p.add_argument('--wd', type=float, default=0.01)
    p.add_argument('--seed', type=int, default=42)
    p.add_argument('--depth', type=int, default=4)
    p.add_argument('--d_hidden', type=int, default=256)
    p.add_argument('--log_every', type=int, default=1)
    p.add_argument('--no_residual_add', action='store_true',
                   help='Replace h = h + f with h = f (non-residual stack of LN-W1-GELU-W2 blocks).')
    p.add_argument('--w2_std', type=float, default=0.01,
                   help='Init std for w2 in each block. Bump to 0.05 for non-residual stack.')
    p.add_argument('--random_targets', action='store_true',
                   help='Replace each minibatch label with iid random class targets (codex round 34 OPTION A).')
    p.add_argument('--skip_bp', action='store_true',
                   help='Only train DFA, skip BP. Useful for cheap DFA-only ablations.')
    args = p.parse_args()

    os.makedirs(args.output_dir, exist_ok=True)
    device = torch.device('cuda:0')  # CUDA_VISIBLE_DEVICES selects which physical GPU
    print(f"device={device}, depth={args.depth}, d_hidden={args.d_hidden}, "
          f"epochs={args.epochs}, seed={args.seed}", flush=True)

    train_loader, test_loader = get_cifar10(batch_size=128)
    x_eval, y_eval = fixed_eval_buffer(test_loader, device, n_samples=1024)
    print(f"eval buffer: {x_eval.shape}", flush=True)

    L, d, C = args.depth, args.d_hidden, 10

    bp_log = None
    if not args.skip_bp:
        print("\n=== BP training ===", flush=True)
        torch.manual_seed(args.seed); np.random.seed(args.seed); torch.cuda.manual_seed_all(args.seed)
        bp_model = ResidualMLP(3072, d, C, L,
                               residual_add=not args.no_residual_add,
                               w2_std=args.w2_std).to(device)
        bp_log = train_bp(bp_model, train_loader, x_eval, y_eval, device,
                          args.epochs, args.lr, args.wd, log_every=args.log_every)

    print("\n=== DFA training ===", flush=True)
    torch.manual_seed(args.seed); np.random.seed(args.seed); torch.cuda.manual_seed_all(args.seed)
    dfa_model = ResidualMLP(3072, d, C, L,
                            residual_add=not args.no_residual_add,
                            w2_std=args.w2_std).to(device)
    dfa_log = train_dfa(dfa_model, train_loader, x_eval, y_eval, device,
                        args.epochs, args.lr, args.wd, log_every=args.log_every,
                        random_targets=args.random_targets)

    out = {
        'config': vars(args),
        'depth': L, 'd_hidden': d, 'num_classes': C,
        'bp_log': bp_log,
        'dfa_log': dfa_log,
    }
    out_path = os.path.join(args.output_dir, f'snapshot_evolution_s{args.seed}.json')
    with open(out_path, 'w') as f:
        json.dump(out, f, indent=2)
    print(f"\nSaved {out_path}", flush=True)


if __name__ == '__main__':
    main()