| 59 |
59 |
h = x[0, a:b].astype(mx.float32) |
| 60 |
60 |
e = mx.square(h) |
| 61 |
61 |
be = e.reshape(h.shape[0], -1, self.block).sum(axis=-1) |
|
62 |
+ # normalize per position: raw energies overflow float16 storage, |
|
63 |
+ # and only relative importance matters for expA/expB |
|
64 |
+ be = be / (be.sum(axis=-1, keepdims=True) + 1e-12) |
| 62 |
65 |
mx.eval(be) |
| 63 |
66 |
self.block_energy = np.array(be) |
| 64 |
|
− ne = np.array(e) # (T, D_int) — reduced immediately by caller |
| 65 |
|
− self.neuron_energy = ne |
|
67 |
+ self.neuron_energy = np.array(e) # (T, D_int) — reduced by caller |
| 66 |
68 |
return self.inner(x) |
| 67 |
69 |
|
| 68 |
70 |
|
| 91 |
93 |
ap.add_argument("--model", default="mlx-community/Qwen3-1.7B-bf16") |
| 92 |
94 |
ap.add_argument("--gen-tokens", type=int, default=128) |
| 93 |
95 |
ap.add_argument("--per-domain", type=int, default=8) |
| 94 |
|
− ap.add_argument("--block", type=int, default=64) |
|
96 |
+ ap.add_argument("--block", type=int, default=16, |
|
97 |
+ help="trace granularity; analysis also derives 4x-coarser blocks") |
| 95 |
98 |
args = ap.parse_args() |
| 96 |
99 |
|
| 97 |
100 |
domains = json.loads((REPO_ROOT / "benchmarks/datasets/eval_prompts.json").read_text())["domains"] |
| 140 |
143 |
P, L, B = all_blocks.shape |
| 141 |
144 |
print(f"trace shape {all_blocks.shape}", flush=True) |
| 142 |
145 |
|
| 143 |
|
− # expA aggregates at block granularity |
|
146 |
+ # expA aggregates at trace granularity and 4x-coarser derived granularity |
|
147 |
+ coarse = all_blocks.reshape(P, L, B // 4, 4).astype(np.float32).sum(axis=-1) |
| 144 |
148 |
per_layer = [concentration_stats(all_blocks[:, li, :].astype(np.float32)) for li in range(L)] |
| 145 |
149 |
overall = concentration_stats(all_blocks.reshape(P * L, B).astype(np.float32)) |
|
150 |
+ overall_coarse = concentration_stats(coarse.reshape(P * L, B // 4)) |
| 146 |
151 |
per_domain = {} |
| 147 |
152 |
pos_domain = np.concatenate([[ix["domain"]] * ix["n_pos"] for ix in index]) |
| 148 |
153 |
for dom in domains: |
| 170 |
175 |
"manifest": collect_manifest(), |
| 171 |
176 |
"config": vars(args), |
| 172 |
177 |
"n_positions": int(P), "n_layers": int(L), "n_blocks": int(B), |
|
178 |
+ "coarse_block_granularity": {"block_size": args.block * 4, "overall": overall_coarse}, |
| 173 |
179 |
"block_granularity": {"overall": overall, |
| 174 |
180 |
"per_layer": {str(i): s for i, s in enumerate(per_layer)}, |
| 175 |
181 |
"per_domain": per_domain}, |