File size: 1,447 Bytes
8246b15 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import torch, numpy as np, matplotlib; matplotlib.use('Agg'); import matplotlib.pyplot as plt
S = torch.load('S.pt'); layers = [1, 12, 22]
fig, ax = plt.subplots(3, 3, figsize=(10.5, 13));
for i, L in enumerate(layers):
for j, (nm, title, rows) in enumerate((('mlp', 'MLP neurons (768)', 24), ('mamba', 'Mamba heads (24 x 32)', 24), ('attn', 'Attention heads (8 x 64)', 8))):
s = S[str((nm, L))].float()
if nm == 'mlp': v = s.sort(descending=True).values; img = v.reshape(rows, -1)
else:
h = s.reshape(rows, -1); order = h.sum(1).argsort(descending=True); img = h[order]; v = img.sum(1)
img = img.numpy(); lo = np.percentile(img, 1); hi = img.max()
x = np.log10(np.clip(img, lo, hi)/lo)/np.log10(hi/lo)
a = ax[i][j]; a.imshow(x, cmap='rainbow', vmin=0, vmax=1, aspect='auto', interpolation='nearest')
for cut, col in ((0.25, 'white'), (0.5, 'black')):
y = rows*(1-cut)-0.5; a.axhline(y, color=col, lw=2.5, ls='--')
a.set_title(f'Layer {L}: {title}', fontsize=11); a.set_xticks([]); a.set_yticks([0, rows-1]); a.set_yticklabels(['top', 'bottom'], fontsize=9)
fig.suptitle('Falcon-H1-Tiny-90M energy map\nred = high energy (mean|act| x |W_out|), violet = low\nwhite dashed: below = removed at 25% cut | black dashed: below = removed at 50% cut', fontsize=12)
fig.tight_layout(rect=[0, 0, 1, 0.94]); fig.savefig('/downloads/falcon_energy_map.png', dpi=110)
|