| """Plot per-layer probe accuracy for the architectures on one figure. |
| |
| Data is loaded automatically from the kdetour result files saved by |
| maze_kstep_detour_test.py (out/maze_kdetour/kdetour_*.npz, key |
| 'layer_probe_acc'). You only need to set TASK / DATASET / configs below. |
| |
| Horizontal axis = absolute layer number. Because the RNN/SSM models here have |
| twice as many layers as the transformers, one transformer layer is aligned to |
| two Mamba/GRU layers: transformer layer i is drawn at x = 2*i. The bottom of |
| the plot uses two rows of tick labels -- top row = Mamba/GRU layer number, |
| bottom row = Transformer layer number. |
| """ |
| import os |
| import argparse |
| import numpy as np |
| import matplotlib |
| matplotlib.use('Agg') |
| import matplotlib.pyplot as plt |
|
|
| |
| |
| |
| TASK = 'I1' |
| DATASET = '10M' |
|
|
| |
| TF_CONFIG = '6_6_384' |
| RNN_CONFIG = '12_384' |
|
|
| |
| CKPT_ITER = 10000 |
| PATH_TYPE = 'RWs' |
| KDETOUR_DIR = 'out/maze_kdetour' |
| OUT_DIR = 'out/plot' |
|
|
|
|
| def parse_args(): |
| p = argparse.ArgumentParser(description='Plot per-layer probe accuracy from kdetour npz files.') |
| p.add_argument('--task', default=TASK) |
| p.add_argument('--dataset', default=DATASET) |
| p.add_argument('--tf_config', default=TF_CONFIG) |
| p.add_argument('--rnn_config', default=RNN_CONFIG) |
| p.add_argument('--ckpt_iter', type=int, default=CKPT_ITER) |
| p.add_argument('--path_type', default=PATH_TYPE) |
| p.add_argument('--kdetour_dir', default=KDETOUR_DIR) |
| p.add_argument('--out_dir', default=OUT_DIR) |
| return p.parse_args() |
|
|
|
|
| |
| MODEL_SPECS = [ |
| ('Transformer', 'transformer', True), |
| ('Transformer-NextLat', 'transformer_nextlat', True), |
| ('Mamba', 'mamba', False), |
| ('Mamba-2', 'mamba2', False), |
| ('Gated-DeltaNet', 'gated_deltanet', False), |
| ('GRU', 'gru', False), |
| ] |
|
|
| MARKERS = ['o', 's', '^', 'D', 'P', 'v'] |
|
|
|
|
| def load_layer_probe_acc(args, model_key, config): |
| """Return (layer_probe_acc array, best_layer int) from the kdetour npz, or |
| (None, None) if the file is missing / has no layer_probe_acc.""" |
| fname = f'kdetour_{args.task}_{args.path_type}_{args.ckpt_iter}_{args.dataset}_{model_key}_{config}.npz' |
| path = os.path.join(args.kdetour_dir, fname) |
| if not os.path.exists(path): |
| print(f"[missing] {path}") |
| return None, None |
| d = np.load(path, allow_pickle=True) |
| if 'layer_probe_acc' not in d: |
| print(f"[no layer_probe_acc] {path}") |
| return None, None |
| vals = np.asarray(d['layer_probe_acc'], dtype=float) |
| best = int(d['best_layer']) if 'best_layer' in d else int(np.argmax(vals) + 1) |
| return vals, best |
|
|
|
|
| def main(): |
| args = parse_args() |
| os.makedirs(args.out_dir, exist_ok=True) |
| out = os.path.join( |
| args.out_dir, |
| f'layer_probe_acc_{args.task}_{args.dataset}_{args.tf_config}_{args.rnn_config}.png') |
| plt.figure(figsize=(10, 5.5)) |
| |
| |
| parsed = [] |
| maxx = 1 |
| for (name, key, is_tf), marker in zip(MODEL_SPECS, MARKERS): |
| config = args.tf_config if is_tf else args.rnn_config |
| vals, best = load_layer_probe_acc(args, key, config) |
| if vals is None or len(vals) == 0: |
| parsed.append(None) |
| continue |
| n = len(vals) |
| x = [2 * i for i in range(1, n + 1)] if is_tf else list(range(1, n + 1)) |
| label = f'{name} ({config})' |
| parsed.append((label, x, vals, marker, best)) |
| maxx = max(maxx, max(x)) |
|
|
| plotted = 0 |
| for item in parsed: |
| if item is None: |
| continue |
| label, x, vals, marker, best = item |
| plt.plot(x, vals, marker=marker, markersize=6, linewidth=2, label=label) |
| print(f"{label}: {len(vals)} layers, best = L{best} ({vals[best - 1]:.1f}%)") |
| plotted += 1 |
|
|
| if plotted == 0: |
| print("Nothing to plot: no kdetour npz files found for these settings.") |
| return |
|
|
| |
| ticks = list(range(1, maxx + 1)) |
| labels = [] |
| for t in ticks: |
| tf = str(t // 2) if t % 2 == 0 else '' |
| labels.append(f"{t}\n{tf}") |
| plt.xticks(ticks, labels, fontsize=8) |
|
|
| plt.xlabel('layer (top: Mamba/GRU layer, bottom: Transformer layer)', fontsize=11) |
| plt.ylabel('probe accuracy (%)', fontsize=12) |
| plt.title(f'Per-layer probe accuracy (Task {args.task}, {args.dataset})', fontsize=13) |
| plt.ylim(-2, 105) |
| plt.grid(True, alpha=0.3) |
| plt.legend(fontsize=10, framealpha=0.9) |
| plt.tight_layout() |
| plt.savefig(out, dpi=150) |
| plt.close() |
| print(f"Saved figure to {out}") |
|
|
|
|
| if __name__ == '__main__': |
| main() |
|
|