anima_closeout v17: the bed called transformer.forward() directly, which is NOT the trainer's path — concat_padding_mask is on so forward()'s padding_mask=None default dies in prepare_embedded_sequence, and it also skips the llm_adapter and the per-block multiband window dispatch. The bed now runs pipe.to_layers() (InitialLayer -> LLMAdapterLayer -> 28x TransformerLayer -> FinalLayer) with the exact input tuple prepare_inputs builds, so it inherits the zero padding mask, the adapter call and the band dispatch instead of re-implementing any of them. The image_spec join from v16 is confirmed working on the live cache: 512 unique specs, 184+189+139 rows joined.
Browse files- colab/anima_closeout.ipynb +44 -39
colab/anima_closeout.ipynb
CHANGED
|
@@ -935,7 +935,7 @@
|
|
| 935 |
"# arm. Reuses the fork's eval cache (latents + text embeds) and the fork's\n",
|
| 936 |
"# own transformer + llm_adapter call path (mirrored from\n",
|
| 937 |
"# cosmos_predict2.py InitialLayer/LLMAdapterLayer).\n",
|
| 938 |
-
"import json, os, sys\n",
|
| 939 |
"sys.path.insert(0, \".\") # fork root (cwd)\n",
|
| 940 |
"import utils.common # bind fork utils BEFORE ComfyUI\n",
|
| 941 |
"sys.path.append(\"submodules/ComfyUI\")\n",
|
|
@@ -1059,7 +1059,22 @@
|
|
| 1059 |
"pipe = cosmos_predict2.CosmosPredict2Pipeline(mc)\n",
|
| 1060 |
"pipe.load_diffusion_model()\n",
|
| 1061 |
"tf = pipe.transformer.to(DEV).eval()\n",
|
| 1062 |
-
"from models.aleph_relay import attach_aleph_relays
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1063 |
"\n",
|
| 1064 |
"def clear_adapters():\n",
|
| 1065 |
" for b in tf.blocks:\n",
|
|
@@ -1067,25 +1082,17 @@
|
|
| 1067 |
" if hasattr(b, attr):\n",
|
| 1068 |
" delattr(b, attr)\n",
|
| 1069 |
"\n",
|
| 1070 |
-
"#
|
| 1071 |
-
"#
|
| 1072 |
-
"
|
| 1073 |
-
"
|
| 1074 |
-
"
|
| 1075 |
-
"
|
| 1076 |
-
" e =
|
| 1077 |
-
"
|
| 1078 |
-
"
|
| 1079 |
-
"
|
| 1080 |
-
"
|
| 1081 |
-
" ids = r[\"t5_input_ids\"].to(DEV).reshape(1, -1)\n",
|
| 1082 |
-
" t5m = r[\"t5_attn_mask\"].to(DEV).reshape(1, -1)\n",
|
| 1083 |
-
" e = la(source_hidden_states=e, target_input_ids=ids,\n",
|
| 1084 |
-
" target_attention_mask=t5m, source_attention_mask=am)\n",
|
| 1085 |
-
" e = e.clone()\n",
|
| 1086 |
-
" e[~t5m.bool()] = 0\n",
|
| 1087 |
-
" embeds.append(e)\n",
|
| 1088 |
-
"print(\"embeds prepared (post-llm_adapter)\", flush=True)\n",
|
| 1089 |
"\n",
|
| 1090 |
"# fixed noise bank + the fork's exact eval-t transform:\n",
|
| 1091 |
"# t = sigmoid(Normal.icdf(q)) (logit_normal, scale 1, no shift \u2014\n",
|
|
@@ -1106,33 +1113,32 @@
|
|
| 1106 |
"results = {\"quantiles\": QUANTILES, \"t_of_q\": T_OF_Q, \"n_rows\": len(rows),\n",
|
| 1107 |
" \"arms\": {}}\n",
|
| 1108 |
"\n",
|
| 1109 |
-
"def eval_arm(name, lesion=None
|
|
|
|
| 1110 |
" per_q = {}\n",
|
| 1111 |
" with torch.no_grad():\n",
|
| 1112 |
" for q in QUANTILES:\n",
|
| 1113 |
" t = T_OF_Q[q]\n",
|
| 1114 |
-
" if needs_bands:\n",
|
| 1115 |
-
" w = band_weights(torch.tensor([t], device=DEV))\n",
|
| 1116 |
-
" for b in tf.blocks:\n",
|
| 1117 |
-
" b.aleph_w_bands = w\n",
|
| 1118 |
" losses = []\n",
|
| 1119 |
-
" for l, nz,
|
| 1120 |
-
" ld = l.to(DEV,
|
| 1121 |
-
" nd = nz.to(DEV
|
| 1122 |
" x_t = (1 - t) * ld + t * nd\n",
|
| 1123 |
-
" ts = torch.full((ld.shape[0],), t, device=DEV,\n",
|
| 1124 |
-
" dtype=torch.
|
| 1125 |
-
"
|
| 1126 |
-
"
|
| 1127 |
-
"
|
|
|
|
|
|
|
| 1128 |
" losses.append(((pred.float().cpu() - (nz - l)) ** 2)\n",
|
| 1129 |
" .mean().item())\n",
|
| 1130 |
" per_q[str(q)] = sum(losses) / len(losses)\n",
|
| 1131 |
" results[\"arms\"][name] = {\"loss_by_quantile\": per_q,\n",
|
| 1132 |
" \"loss_mean\": sum(per_q.values()) / len(per_q),\n",
|
| 1133 |
" \"lesion\": lesion}\n",
|
| 1134 |
-
" print(f\"[{name}] mean {results['arms'][name]['loss_mean']:.6f}\"
|
| 1135 |
-
" flush=True)\n",
|
| 1136 |
"\n",
|
| 1137 |
"clear_adapters()\n",
|
| 1138 |
"eval_arm(\"frozen\")\n",
|
|
@@ -1154,15 +1160,14 @@
|
|
| 1154 |
" gates.extend(torch.as_tensor(g).float().flatten().tolist())\n",
|
| 1155 |
" for b in tf.blocks: # toggle assert per arm\n",
|
| 1156 |
" b.aleph_relay.enabled = False\n",
|
| 1157 |
-
" eval_arm(name + \"_toggled_off\"
|
| 1158 |
" assert results[\"arms\"][name + \"_toggled_off\"][\"loss_by_quantile\"] \\\n",
|
| 1159 |
" == f_ref, f\"{name}: TOGGLE LAW VIOLATED\"\n",
|
| 1160 |
" for b in tf.blocks:\n",
|
| 1161 |
" b.aleph_relay.enabled = True\n",
|
| 1162 |
" if arm.get(\"lesion\") is not None:\n",
|
| 1163 |
" b.aleph_relay.band_enabled[arm[\"lesion\"]] = False\n",
|
| 1164 |
-
" eval_arm(name, lesion=arm.get(\"lesion\")
|
| 1165 |
-
" needs_bands=(mode == \"multiband3\"))\n",
|
| 1166 |
" results[\"arms\"][name][\"gates_fp32\"] = {\n",
|
| 1167 |
" \"min\": min(gates), \"max\": max(gates),\n",
|
| 1168 |
" \"moved\": sum(1 for g in gates if abs(g + 3.0) > 1e-3)}\n",
|
|
|
|
| 935 |
"# arm. Reuses the fork's eval cache (latents + text embeds) and the fork's\n",
|
| 936 |
"# own transformer + llm_adapter call path (mirrored from\n",
|
| 937 |
"# cosmos_predict2.py InitialLayer/LLMAdapterLayer).\n",
|
| 938 |
+
"import json, os, sys, time\n",
|
| 939 |
"sys.path.insert(0, \".\") # fork root (cwd)\n",
|
| 940 |
"import utils.common # bind fork utils BEFORE ComfyUI\n",
|
| 941 |
"sys.path.append(\"submodules/ComfyUI\")\n",
|
|
|
|
| 1059 |
"pipe = cosmos_predict2.CosmosPredict2Pipeline(mc)\n",
|
| 1060 |
"pipe.load_diffusion_model()\n",
|
| 1061 |
"tf = pipe.transformer.to(DEV).eval()\n",
|
| 1062 |
+
"from models.aleph_relay import attach_aleph_relays\n",
|
| 1063 |
+
"\n",
|
| 1064 |
+
"# THE fork's own execution path (cosmos_predict2.py:460-470):\n",
|
| 1065 |
+
"# InitialLayer -> LLMAdapterLayer -> 28x TransformerLayer -> FinalLayer\n",
|
| 1066 |
+
"# Calling transformer.forward() directly is NOT what the trainer does and\n",
|
| 1067 |
+
"# does not work: concat_padding_mask is on, so forward()'s padding_mask=None\n",
|
| 1068 |
+
"# default crashes in prepare_embedded_sequence \u2014 InitialLayer builds a zero\n",
|
| 1069 |
+
"# padding mask itself (:612). The layer path also applies the llm_adapter\n",
|
| 1070 |
+
"# (LLMAdapterLayer) and sets each block's multiband window from the actual\n",
|
| 1071 |
+
"# timesteps (TransformerLayer:663-668), so the bed inherits all of it\n",
|
| 1072 |
+
"# instead of re-implementing it. The layers hold references to tf.blocks,\n",
|
| 1073 |
+
"# so adapters attached per-arm below are picked up without rebuilding.\n",
|
| 1074 |
+
"LAYERS = pipe.to_layers()\n",
|
| 1075 |
+
"print(f\"model layers: {len(LAYERS)} \"\n",
|
| 1076 |
+
" f\"({type(LAYERS[0]).__name__} .. {type(LAYERS[-1]).__name__})\",\n",
|
| 1077 |
+
" flush=True)\n",
|
| 1078 |
"\n",
|
| 1079 |
"def clear_adapters():\n",
|
| 1080 |
" for b in tf.blocks:\n",
|
|
|
|
| 1082 |
" if hasattr(b, attr):\n",
|
| 1083 |
" delattr(b, attr)\n",
|
| 1084 |
"\n",
|
| 1085 |
+
"# conditioning exactly as prepare_inputs hands it over (:412): the CACHED\n",
|
| 1086 |
+
"# embeds, un-adapted \u2014 LLMAdapterLayer does the adapter call itself.\n",
|
| 1087 |
+
"conds = []\n",
|
| 1088 |
+
"for r in rows:\n",
|
| 1089 |
+
" e = r[\"prompt_embeds\"]\n",
|
| 1090 |
+
" if e.ndim == 2:\n",
|
| 1091 |
+
" e = e.unsqueeze(0)\n",
|
| 1092 |
+
" conds.append((e, r[\"attn_mask\"].reshape(1, -1),\n",
|
| 1093 |
+
" r[\"t5_input_ids\"].reshape(1, -1),\n",
|
| 1094 |
+
" r[\"t5_attn_mask\"].reshape(1, -1)))\n",
|
| 1095 |
+
"print(f\"conditioning prepared for {len(conds)} rows\", flush=True)\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1096 |
"\n",
|
| 1097 |
"# fixed noise bank + the fork's exact eval-t transform:\n",
|
| 1098 |
"# t = sigmoid(Normal.icdf(q)) (logit_normal, scale 1, no shift \u2014\n",
|
|
|
|
| 1113 |
"results = {\"quantiles\": QUANTILES, \"t_of_q\": T_OF_Q, \"n_rows\": len(rows),\n",
|
| 1114 |
" \"arms\": {}}\n",
|
| 1115 |
"\n",
|
| 1116 |
+
"def eval_arm(name, lesion=None):\n",
|
| 1117 |
+
" t0 = time.time()\n",
|
| 1118 |
" per_q = {}\n",
|
| 1119 |
" with torch.no_grad():\n",
|
| 1120 |
" for q in QUANTILES:\n",
|
| 1121 |
" t = T_OF_Q[q]\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1122 |
" losses = []\n",
|
| 1123 |
+
" for l, nz, cd in zip(lats, noises, conds):\n",
|
| 1124 |
+
" ld = l.to(DEV) # float32, as :409\n",
|
| 1125 |
+
" nd = nz.to(DEV)\n",
|
| 1126 |
" x_t = (1 - t) * ld + t * nd\n",
|
| 1127 |
+
" ts = torch.full((ld.shape[0], 1), t, device=DEV,\n",
|
| 1128 |
+
" dtype=torch.float32) # (B,1), as :456\n",
|
| 1129 |
+
" out = (x_t, ts, cd[0].to(DEV), cd[1].to(DEV),\n",
|
| 1130 |
+
" cd[2].to(DEV), cd[3].to(DEV))\n",
|
| 1131 |
+
" for layer in LAYERS:\n",
|
| 1132 |
+
" out = layer(out)\n",
|
| 1133 |
+
" pred = out[0] if isinstance(out, (list, tuple)) else out\n",
|
| 1134 |
" losses.append(((pred.float().cpu() - (nz - l)) ** 2)\n",
|
| 1135 |
" .mean().item())\n",
|
| 1136 |
" per_q[str(q)] = sum(losses) / len(losses)\n",
|
| 1137 |
" results[\"arms\"][name] = {\"loss_by_quantile\": per_q,\n",
|
| 1138 |
" \"loss_mean\": sum(per_q.values()) / len(per_q),\n",
|
| 1139 |
" \"lesion\": lesion}\n",
|
| 1140 |
+
" print(f\"[{name}] mean {results['arms'][name]['loss_mean']:.6f} \"\n",
|
| 1141 |
+
" f\"({(time.time() - t0) / 60:.1f} min)\", flush=True)\n",
|
| 1142 |
"\n",
|
| 1143 |
"clear_adapters()\n",
|
| 1144 |
"eval_arm(\"frozen\")\n",
|
|
|
|
| 1160 |
" gates.extend(torch.as_tensor(g).float().flatten().tolist())\n",
|
| 1161 |
" for b in tf.blocks: # toggle assert per arm\n",
|
| 1162 |
" b.aleph_relay.enabled = False\n",
|
| 1163 |
+
" eval_arm(name + \"_toggled_off\")\n",
|
| 1164 |
" assert results[\"arms\"][name + \"_toggled_off\"][\"loss_by_quantile\"] \\\n",
|
| 1165 |
" == f_ref, f\"{name}: TOGGLE LAW VIOLATED\"\n",
|
| 1166 |
" for b in tf.blocks:\n",
|
| 1167 |
" b.aleph_relay.enabled = True\n",
|
| 1168 |
" if arm.get(\"lesion\") is not None:\n",
|
| 1169 |
" b.aleph_relay.band_enabled[arm[\"lesion\"]] = False\n",
|
| 1170 |
+
" eval_arm(name, lesion=arm.get(\"lesion\"))\n",
|
|
|
|
| 1171 |
" results[\"arms\"][name][\"gates_fp32\"] = {\n",
|
| 1172 |
" \"min\": min(gates), \"max\": max(gates),\n",
|
| 1173 |
" \"moved\": sum(1 for g in gates if abs(g + 3.0) > 1e-3)}\n",
|