anima-python 0.13.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- anima_py/README.md +39 -0
- anima_py/__init__.py +28 -0
- anima_py/__main__.py +7 -0
- anima_py/cli/__init__.py +1 -0
- anima_py/cli/anima.py +271 -0
- anima_py/cli/corpus.py +150 -0
- anima_py/cli/evaluate.py +1350 -0
- anima_py/cli/rho_axon.py +801 -0
- anima_py/cli/serialize.py +174 -0
- anima_py/cli/serialize_bind.py +100 -0
- anima_py/cli/sweep.py +509 -0
- anima_py/cli/train.py +1667 -0
- anima_py/core/__init__.py +1 -0
- anima_py/core/brain.py +601 -0
- anima_py/core/clm_serialize_v2.py +486 -0
- anima_py/core/clml.py +155 -0
- anima_py/core/decode.py +1386 -0
- anima_py/core/engine_cli.py +10307 -0
- anima_py/core/engine_g.py +129 -0
- anima_py/core/generator.py +318 -0
- anima_py/core/hippo_lane.py +133 -0
- anima_py/core/model.py +462 -0
- anima_py/core/pure_field.py +334 -0
- anima_py/core/rho_fan.py +442 -0
- anima_py/core/serialize.py +826 -0
- anima_py/core/serialize_standalone.py +186 -0
- anima_py/core/slw.py +211 -0
- anima_py/core/verify_clm_v2.py +844 -0
- anima_python-0.13.1.dist-info/METADATA +52 -0
- anima_python-0.13.1.dist-info/RECORD +34 -0
- anima_python-0.13.1.dist-info/WHEEL +5 -0
- anima_python-0.13.1.dist-info/entry_points.txt +2 -0
- anima_python-0.13.1.dist-info/licenses/LICENSE +21 -0
- anima_python-0.13.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# serialize.py — anima STANDALONE re-serialize entry (.pt → .clm v0.3 + held-out gate).
|
|
3
|
+
#
|
|
4
|
+
# WHY THIS FILE (recovery/re-export · a_clm_gen_pipeline): `anima train` ALREADY
|
|
5
|
+
# auto-serializes its trained weights to .clm v0.3 and runs the held-out mirror-DESCENT
|
|
6
|
+
# gate at the end of a run (cli/train.py / cli/train.hexa post-serialize block). This
|
|
7
|
+
# standalone command exists for the SEPARATE case: re-exporting an ALREADY-TRAINED torch
|
|
8
|
+
# .pt checkpoint (recovery, re-export, re-verification) without re-training.
|
|
9
|
+
#
|
|
10
|
+
# REFERENCE-MATCH (NOT a reimplementation): the byte layout comes from the GROUND-TRUTH
|
|
11
|
+
# unified serializer core/serialize.py::serialize_v3 (the SAME byte grammar core/decode.hexa
|
|
12
|
+
# parses and the golden reexport_d768_v2_fast.clm uses), and the held-out gate is the KEPT
|
|
13
|
+
# torch-interop sibling verify_clm_v2.py descent (math.log mirror, dt_ln-immune, under the
|
|
14
|
+
# CLM model dir). This file only loads the .pt, derives L/E from the state-dict keys, and
|
|
15
|
+
# calls those two backends — it invents no new layout and no new gate.
|
|
16
|
+
#
|
|
17
|
+
# TORCH IS ALLOWED HERE (training family): unlike cli/evaluate.py (torch-free
|
|
18
|
+
# measurement) this is the LEARNING-side re-export and must read a torch .pt. The gate
|
|
19
|
+
# enforcer's torch grep targets the measurement surfaces (evaluate / anima dispatch), not
|
|
20
|
+
# the trainer/serializer family (a_engine_native_learning: train/ may use torch).
|
|
21
|
+
#
|
|
22
|
+
# USAGE (installed `anima` PATH command after `hx install anima`)
|
|
23
|
+
# anima serialize <ckpt.pt> <out.clm> [--heldout <path>] [--train <path>] [--nwin N]
|
|
24
|
+
# <ckpt.pt> torch state_dict (or {"state_dict": ...}/{"model": ...}) checkpoint
|
|
25
|
+
# <out.clm> output .clm v0.3 path (engine-loadable, CLM magic)
|
|
26
|
+
# --heldout held-out byte corpus for the post-serialize mirror-DESCENT gate
|
|
27
|
+
# (a_clm_gen_pipeline: structure round-trip is NOT enough)
|
|
28
|
+
# --train optional train corpus (gap-vs-heldout overfit advisory)
|
|
29
|
+
# --nwin N descent window length (default 64)
|
|
30
|
+
|
|
31
|
+
import os
|
|
32
|
+
import sys
|
|
33
|
+
|
|
34
|
+
_HERE = os.path.dirname(os.path.abspath(__file__))
|
|
35
|
+
_REPO = os.path.dirname(_HERE)
|
|
36
|
+
# UNIFIED serializer = core/serialize.py (CLM serialize_v3 + ByteGPT .pt→.bin bridge),
|
|
37
|
+
# parallel to core/decode.py — add core/ so `import serialize` resolves (same resolution
|
|
38
|
+
# cli/evaluate.py uses for `import decode`). The held-out DESCENT verifier verify_clm_v2.py
|
|
39
|
+
# and the .pt→.clm bridge serialize_standalone.py are CORE-owned siblings in core/ too
|
|
40
|
+
# (owner directive: core-related lives in core/; no archive/train import — a_no_archive_import).
|
|
41
|
+
sys.path.insert(0, os.path.join(_REPO, "core"))
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def serialize_usage():
|
|
45
|
+
"""Print the canonical usage banner (installed `anima serialize` command form)."""
|
|
46
|
+
print("anima serialize — re-export a torch .pt to .clm v0.3 + held-out DESCENT gate.")
|
|
47
|
+
print("")
|
|
48
|
+
print("usage:")
|
|
49
|
+
print(" anima serialize <ckpt.pt> <out.clm> [--heldout <path>] [--train <path>] [--nwin N]")
|
|
50
|
+
print("")
|
|
51
|
+
print(" re-serialize an ALREADY-TRAINED torch checkpoint to an engine-loadable .clm")
|
|
52
|
+
print(" (ground-truth bridge serialize_v3) and verify it models held-out text")
|
|
53
|
+
print(" (verify_clm_v2 mirror-DESCENT gate). `anima train` does this automatically;")
|
|
54
|
+
print(" this command is for recovery / re-export of an existing .pt.")
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _strval(argv, flag, dflt):
|
|
58
|
+
i = 0
|
|
59
|
+
while i < len(argv):
|
|
60
|
+
if argv[i] == flag and i + 1 < len(argv):
|
|
61
|
+
return argv[i + 1]
|
|
62
|
+
i += 1
|
|
63
|
+
return dflt
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _intval(argv, flag, dflt):
|
|
67
|
+
v = _strval(argv, flag, None)
|
|
68
|
+
return int(v) if v is not None else dflt
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _derive_LE(sd):
|
|
72
|
+
"""Derive (L trunk layers, E experts) from the state-dict keys (reference-match
|
|
73
|
+
of serialize_v2._general_block_keymap naming: trunk.{i}.* and moe.experts.{j}.*)."""
|
|
74
|
+
L = 0
|
|
75
|
+
E = 0
|
|
76
|
+
for k in sd.keys():
|
|
77
|
+
if k.startswith("trunk.") and k.endswith(".conv.conv.weight"):
|
|
78
|
+
try:
|
|
79
|
+
L = max(L, int(k.split(".")[1]) + 1)
|
|
80
|
+
except (ValueError, IndexError):
|
|
81
|
+
pass
|
|
82
|
+
if k.startswith("moe.experts.") and k.endswith(".conv.conv.weight"):
|
|
83
|
+
try:
|
|
84
|
+
E = max(E, int(k.split(".")[2]) + 1)
|
|
85
|
+
except (ValueError, IndexError):
|
|
86
|
+
pass
|
|
87
|
+
return L, E
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def serialize_run(argv):
|
|
91
|
+
if len(argv) < 2:
|
|
92
|
+
serialize_usage()
|
|
93
|
+
return 2
|
|
94
|
+
|
|
95
|
+
pt = argv[0]
|
|
96
|
+
out = argv[1]
|
|
97
|
+
heldout = _strval(argv[2:], "--heldout", None)
|
|
98
|
+
train_corpus = _strval(argv[2:], "--train", None)
|
|
99
|
+
nwin = _intval(argv[2:], "--nwin", 64)
|
|
100
|
+
|
|
101
|
+
if not os.path.exists(pt):
|
|
102
|
+
print("ERROR: torch checkpoint not found: " + pt)
|
|
103
|
+
return 2
|
|
104
|
+
|
|
105
|
+
import torch # training family — torch OK here
|
|
106
|
+
import serialize as S # core/serialize.py — serialize_v3 = bridge SSOT
|
|
107
|
+
import verify_clm_v2 as VC # clm_decodable / descent (train/clm/model/)
|
|
108
|
+
|
|
109
|
+
print("=== anima serialize — .pt → .clm v0.3 (ground-truth bridge) ===")
|
|
110
|
+
print("pt: " + pt)
|
|
111
|
+
print("out: " + out)
|
|
112
|
+
|
|
113
|
+
raw = torch.load(pt, map_location="cpu")
|
|
114
|
+
# normalize to a flat state_dict (mirror serialize_v2._resolve_state_dict inputs).
|
|
115
|
+
if isinstance(raw, dict) and "state_dict" in raw and isinstance(raw["state_dict"], dict):
|
|
116
|
+
sd = raw["state_dict"]
|
|
117
|
+
elif isinstance(raw, dict) and "model" in raw and isinstance(raw["model"], dict):
|
|
118
|
+
sd = raw["model"]
|
|
119
|
+
else:
|
|
120
|
+
sd = raw
|
|
121
|
+
sd = {k: (v.detach().cpu() if hasattr(v, "detach") else v) for k, v in sd.items()}
|
|
122
|
+
|
|
123
|
+
L, E = _derive_LE(sd)
|
|
124
|
+
if L < 1 or E < 1:
|
|
125
|
+
print("ERROR: could not derive L/E from state-dict keys "
|
|
126
|
+
"(expected trunk.{i}.conv.conv.weight / moe.experts.{j}.conv.conv.weight). "
|
|
127
|
+
"Is this an anima CLMConvMoE checkpoint?")
|
|
128
|
+
return 2
|
|
129
|
+
print("derived: L(trunk)=" + str(L) + " E(experts)=" + str(E))
|
|
130
|
+
|
|
131
|
+
S.serialize_v3(sd, n_trunk_layers=L, n_experts=E, out_path=out)
|
|
132
|
+
nbytes = os.path.getsize(out)
|
|
133
|
+
print("WRITTEN " + str(nbytes) + " bytes -> " + out)
|
|
134
|
+
|
|
135
|
+
# ── CORE-loadable self-check (mirror of core/decode.hexa CONV mouth) ─────
|
|
136
|
+
rb = open(out, "rb").read()
|
|
137
|
+
decodable = VC.clm_decodable(rb)
|
|
138
|
+
print("")
|
|
139
|
+
print("=== CORE-loadable self-check (mirror of core/decode.hexa CONV mouth) ===")
|
|
140
|
+
print(" clm_decodable = " + str(decodable))
|
|
141
|
+
if not decodable:
|
|
142
|
+
print("FAIL — serialized .clm is NOT CORE-loadable (see above).")
|
|
143
|
+
return 1
|
|
144
|
+
|
|
145
|
+
# ── held-out mirror-DESCENT gate (a_clm_gen_pipeline) ────────────────────
|
|
146
|
+
if heldout:
|
|
147
|
+
if not os.path.exists(heldout):
|
|
148
|
+
print("ERROR: --heldout corpus not found: " + heldout)
|
|
149
|
+
return 2
|
|
150
|
+
print("")
|
|
151
|
+
print("post-serialize held-out DESCENT gate (verify_clm_v2 mirror, dt_ln-immune):")
|
|
152
|
+
rc = VC.run_descent_gate_cli(out, heldout, train_corpus, nwin)
|
|
153
|
+
if rc != 0:
|
|
154
|
+
print(" ⛔ DESCENT GATE FAIL — serialized .clm broken/overfit; do NOT mark "
|
|
155
|
+
"done / HF-upload (a_clm_gen_pipeline).")
|
|
156
|
+
return 1
|
|
157
|
+
print(" ✅ DESCENT GATE PASS — serialized .clm models held-out text.")
|
|
158
|
+
else:
|
|
159
|
+
print("")
|
|
160
|
+
print("NOTE: no --heldout corpus given — structure round-trip only. The .clm is "
|
|
161
|
+
"CORE-loadable but its held-out predictive descent is UNVERIFIED "
|
|
162
|
+
"(a_clm_gen_pipeline: decodable != generalizing). Pass --heldout to gate it.")
|
|
163
|
+
return 0
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def main(argv):
|
|
167
|
+
if len(argv) >= 1 and argv[0] in ("-h", "--help"):
|
|
168
|
+
serialize_usage()
|
|
169
|
+
return 0
|
|
170
|
+
return serialize_run(argv)
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
if __name__ == "__main__":
|
|
174
|
+
sys.exit(main(sys.argv[1:]) or 0)
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# serialize_bind.py — anima STANDALONE injected-bind re-serialize entry
|
|
3
|
+
# (base ByteGPT .bin + injected torch .pt → engine .bin with a "BGB\x01" trailer).
|
|
4
|
+
#
|
|
5
|
+
# WHY THIS FILE (H_9027 · a_clm_gen_pipeline sibling for the BYTE mouth): a trained
|
|
6
|
+
# BindAttn model = the FROZEN base ByteGPT + N appended GATED transformer blocks
|
|
7
|
+
# (applied after the L base blocks, before ln_f: x = x + gate*(block(x)-x)). This
|
|
8
|
+
# command bridges the injected torch .pt back onto the base .bin so the combined
|
|
9
|
+
# model is engine-loadable and scoreable via `anima-python evaluate <bin>` with ZERO
|
|
10
|
+
# changes to cli/evaluate.py (bg_is_bytegpt stays true; bg_load reads the trailer).
|
|
11
|
+
#
|
|
12
|
+
# REFERENCE-MATCH (NOT a reimplementation): the byte layout is core/serialize.py::
|
|
13
|
+
# serialize_bind (mirrors `serialize`'s per-layer write + the CLM "CLMB" bind-trailer
|
|
14
|
+
# precedent), and it is the SAME grammar core/decode.py bg_load parses. This file only
|
|
15
|
+
# reads argv, then calls that one backend — it invents no new layout.
|
|
16
|
+
#
|
|
17
|
+
# TORCH IS ALLOWED HERE (training family): reading the injected .pt (unpickle) is the
|
|
18
|
+
# only torch touch. This is the LEARNING-side re-export, NOT the torch-free measurement
|
|
19
|
+
# surface (cli/evaluate.py) the gate enforcer targets.
|
|
20
|
+
#
|
|
21
|
+
# USAGE (installed `anima` PATH command after `hx install anima`)
|
|
22
|
+
# anima serialize-bind <base.bin> <injected.pt> <out.bin>
|
|
23
|
+
# <base.bin> plain engine ByteGPT .bin (5xu32 header, NO trailer)
|
|
24
|
+
# <injected.pt> torch {"bind": <Block state_dict | list | dict>,
|
|
25
|
+
# "gate": <scalar | list | dict>, "config": {...}}
|
|
26
|
+
# <out.bin> output engine .bin = base bytes verbatim + BGB trailer
|
|
27
|
+
|
|
28
|
+
import os
|
|
29
|
+
import sys
|
|
30
|
+
|
|
31
|
+
_HERE = os.path.dirname(os.path.abspath(__file__))
|
|
32
|
+
_REPO = os.path.dirname(_HERE)
|
|
33
|
+
# UNIFIED serializer = core/serialize.py (same resolution cli/serialize.py uses).
|
|
34
|
+
sys.path.insert(0, os.path.join(_REPO, "core"))
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def serialize_bind_usage():
|
|
38
|
+
print("anima serialize-bind — base ByteGPT .bin + injected torch .pt → .bin + BGB trailer.")
|
|
39
|
+
print("")
|
|
40
|
+
print("usage:")
|
|
41
|
+
print(" anima serialize-bind <base.bin> <injected.pt> <out.bin>")
|
|
42
|
+
print("")
|
|
43
|
+
print(" splice N appended gated transformer blocks (BindAttn .pt) onto a base ByteGPT")
|
|
44
|
+
print(" .bin so the combined model is engine-loadable + scoreable via")
|
|
45
|
+
print(" `anima-python evaluate <out.bin>`. gate=0 => byte-identical to the base .bin.")
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def serialize_bind_run(argv):
|
|
49
|
+
if len(argv) < 3:
|
|
50
|
+
serialize_bind_usage()
|
|
51
|
+
return 2
|
|
52
|
+
base_bin, injected_pt, out_bin = argv[0], argv[1], argv[2]
|
|
53
|
+
if not os.path.exists(base_bin):
|
|
54
|
+
print("ERROR: base .bin not found: " + base_bin)
|
|
55
|
+
return 2
|
|
56
|
+
if not os.path.exists(injected_pt):
|
|
57
|
+
print("ERROR: injected .pt not found: " + injected_pt)
|
|
58
|
+
return 2
|
|
59
|
+
|
|
60
|
+
import serialize as S # core/serialize.py — serialize_bind = bind-trailer SSOT
|
|
61
|
+
import decode as D # core/decode.py — CORE-loadable self-check (bg_load)
|
|
62
|
+
|
|
63
|
+
print("=== anima serialize-bind — base .bin + injected .pt → .bin + BGB ===")
|
|
64
|
+
print("base: " + base_bin)
|
|
65
|
+
print("injected: " + injected_pt)
|
|
66
|
+
print("out: " + out_bin)
|
|
67
|
+
|
|
68
|
+
S.serialize_bind(base_bin, injected_pt, out_bin)
|
|
69
|
+
nbytes = os.path.getsize(out_bin)
|
|
70
|
+
print("WRITTEN " + str(nbytes) + " bytes -> " + out_bin)
|
|
71
|
+
|
|
72
|
+
# ── CORE-loadable self-check (mirror of core/decode.py BYTE mouth) ─────────
|
|
73
|
+
print("")
|
|
74
|
+
print("=== CORE-loadable self-check (core/decode.py bg_load BYTE mouth) ===")
|
|
75
|
+
is_bg = D.bg_is_bytegpt(out_bin)
|
|
76
|
+
print(" bg_is_bytegpt = " + str(is_bg))
|
|
77
|
+
if not is_bg:
|
|
78
|
+
print("FAIL — output .bin is NOT recognized as a ByteGPT mouth.")
|
|
79
|
+
return 1
|
|
80
|
+
W = D.bg_load(out_bin)
|
|
81
|
+
nb = len(W.get("bind", []))
|
|
82
|
+
print(" bg_load ok = " + str(W.get("ok")) + " n_bind blocks = " + str(nb))
|
|
83
|
+
if not W.get("ok") or nb < 1:
|
|
84
|
+
print("FAIL — output .bin did not round-trip a BGB trailer.")
|
|
85
|
+
return 1
|
|
86
|
+
print(" ✅ CORE-loadable with " + str(nb) + " injected-bind block(s).")
|
|
87
|
+
print(" NOTE: this is a STRUCTURE round-trip only — the engine-native ρ-AXON reach")
|
|
88
|
+
print(" verdict (former G0-G6) is `anima-python evaluate <out.bin>` (a_engine_native_learning).")
|
|
89
|
+
return 0
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def main(argv):
|
|
93
|
+
if len(argv) >= 1 and argv[0] in ("-h", "--help"):
|
|
94
|
+
serialize_bind_usage()
|
|
95
|
+
return 0
|
|
96
|
+
return serialize_bind_run(argv)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
if __name__ == "__main__":
|
|
100
|
+
sys.exit(main(sys.argv[1:]) or 0)
|