File size: 1,421 Bytes
24aaa76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""arch.json: layer list and parameter count per module of every arm, counted from the model objects (CPU, no weights needed)."""
import argparse
import os

from kraken.lib.codec import PytorchCodec

from .common import DATA, LD_OUT, load_json, save_json, utcnow
from .models import build_arm_for_inference, layer_report, n_params


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--codec", default=LD_OUT + "/codec_union.json")
    ap.add_argument("--out", default=LD_OUT + "/arch.json")
    a = ap.parse_args()
    codec = PytorchCodec(load_json(a.codec)["c2l"])
    out = {"created": utcnow(), "codec_classes_including_blank": codec.max_label + 1,
           "note": "parameters counted with sum(p.numel()) on the built model objects; 'deployed' is what runs at inference; the NRTR guide head of A1/A2/A1s is used in training only"}
    for arm in ("A1", "A2", "A3", "A4", "A1s"):
        m = build_arm_for_inference(arm, codec)
        rep = m.arch()
        entry = {"deployed_params": m.deploy_params(), "layers_deployed": [{"module": n, "class": c, "params": p} for n, c, p in rep["deployed"]]}
        if "aux_training_only" in rep:
            n, c, p = rep["aux_training_only"]
            entry["aux_training_only"] = {"module": "NRTRHead", "params": p}
        out[arm] = entry
        print(arm, entry["deployed_params"])
    save_json(a.out, out)


if __name__ == "__main__":
    main()