|
| 1 | +"""V7-F54: cross-preset block transplant. |
| 2 | +
|
| 3 | +Proves brick modularity: take MoE (from a mixtral-style preset), |
| 4 | +graft it into a llama-style attention→mlp chain, build, train. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from cppmega_v4.jsonrpc.schema import VerifyParams |
| 12 | +from cppmega_v4.runner import Pipeline, run_pipeline |
| 13 | + |
| 14 | + |
| 15 | +def _hybrid_spec() -> VerifyParams: |
| 16 | + """attention → moe (transplanted) → mlp.""" |
| 17 | + return VerifyParams.model_validate({ |
| 18 | + "graph": { |
| 19 | + "nodes": [ |
| 20 | + {"id": "attn", "kind": "attention", |
| 21 | + "params": {"num_heads": 4, "head_dim": 64}}, |
| 22 | + {"id": "moe_xplant", "kind": "moe", |
| 23 | + "params": {"num_experts": 4, "top_k": 2}}, |
| 24 | + {"id": "mlp", "kind": "mlp", "params": {}}, |
| 25 | + ], |
| 26 | + "edges": [ |
| 27 | + {"src": "attn", "dst": "moe_xplant"}, |
| 28 | + {"src": "moe_xplant", "dst": "mlp"}, |
| 29 | + ], |
| 30 | + }, |
| 31 | + "dim_env": {"B": 1, "S": 8, "H": 128, |
| 32 | + "nh": 2, "nkv": 1, "head_dim": 64, |
| 33 | + "num_experts": 4, "top_k": 2}, |
| 34 | + "loss": {"kind": "cross_entropy", "head_outputs": ["mlp"]}, |
| 35 | + "optim": {"kind": "adamw", |
| 36 | + "groups": [{"matcher": "all", "lr": 1e-3, |
| 37 | + "weight_decay": 0.01, |
| 38 | + "betas": [0.9, 0.95]}]}, |
| 39 | + }) |
| 40 | + |
| 41 | + |
| 42 | +def test_v7_f54_transplanted_moe_trains_in_llama_chain(): |
| 43 | + rep = run_pipeline(_hybrid_spec(), Pipeline.from_dict({ |
| 44 | + "stages": ["parse", "verify_build_spec", "build_model", |
| 45 | + "train"], |
| 46 | + "stage_options": {"train": {"num_steps": 4}}, |
| 47 | + })) |
| 48 | + tr = next(s for s in rep.stages if s.name == "train") |
| 49 | + assert tr.status == "ok", ( |
| 50 | + f"transplanted graph failed: {tr.error}" |
| 51 | + ) |
| 52 | + for L in tr.extras["losses"]: |
| 53 | + assert -1e10 < L < 1e10 |
| 54 | + assert tr.extras["weight_delta_norm"] > 0 |
| 55 | + |
| 56 | + |
| 57 | +def test_v7_f54_moe_extras_surface_through_transplant(): |
| 58 | + """The transplanted MoE still surfaces extras.moe — its routing |
| 59 | + metrics aren't lost just because it's in a foreign chain.""" |
| 60 | + rep = run_pipeline(_hybrid_spec(), Pipeline.from_dict({ |
| 61 | + "stages": ["parse", "verify_build_spec", "build_model", |
| 62 | + "train"], |
| 63 | + "stage_options": {"train": {"num_steps": 2}}, |
| 64 | + })) |
| 65 | + tr = next(s for s in rep.stages if s.name == "train") |
| 66 | + moe = tr.extras["moe"] |
| 67 | + assert moe is not None |
| 68 | + assert moe["num_experts"] == 4 |
| 69 | + assert moe["top_k"] == 2 |
0 commit comments