File size: 2,897 Bytes
99e41a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
"""PlotNeuralNet (HarisIqbal88/PlotNeuralNet, MIT) 3-D diagram of the Cosmos-Reason2-2B verifier path:
camera view -> patch embedding -> ViT x24 -> 2x2 merger -> LLM decoder x28 -> yes/no logit head.
Tensor shapes are for one 448x448 view. Needs pdflatex + pdftoppm.  -> media/architecture/plotnn/*.{tex,pdf,png}
"""
import subprocess
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "third_party/PlotNeuralNet"))
from pycore.tikzeng import (to_begin, to_connection, to_Conv, to_ConvConvRelu, to_cor, to_end, to_generate,  # noqa: E402
                            to_head, to_input, to_Pool, to_SoftMax)

OUT = ROOT / "media/architecture/plotnn"
arch = [
    to_head(str(ROOT / "third_party/PlotNeuralNet")),
    to_cor(),
    to_begin(),
    to_input("camera.png", to="(-3.2,0,0)", width=7, height=7),
    to_Conv("patch", 28, 1024, offset="(0,0,0)", to="(0,0,0)", width=4, height=28, depth=28,
            caption="\\mbox{patch embed}"),
    to_ConvConvRelu("vit", 28, (1024, 1024), offset="(2.2,0,0)", to="(patch-east)", width=(3, 3), height=28, depth=28,
                    caption="\\mbox{ViT blocks $\\times$24}"),
    to_Pool("merge", offset="(1.6,0,0)", to="(vit-east)", width=3, height=14, depth=14, opacity=0.6,
            caption="\\mbox{2$\\times$2 merge}"),
    to_Conv("tok", 196, 2048, offset="(1.6,0,0)", to="(merge-east)", width=5, height=14, depth=14,
            caption="\\mbox{image tokens}"),
    to_ConvConvRelu("llm", 196, (2048, 2048), offset="(2.4,0,0)", to="(tok-east)", width=(5, 5), height=14, depth=14,
                    caption="\\mbox{LLM decoder $\\times$28}"),
    to_Conv("head", "", 151936, offset="(2.4,0,0)", to="(llm-east)", width=1, height=22, depth=4,
            caption="\\mbox{LM head}"),
    to_SoftMax("p", 2, offset="(2.0,0,0)", to="(head-east)", width=1.5, height=4, depth=4, caption="\\mbox{P(yes)}"),
    to_connection("patch", "vit"),
    to_connection("vit", "merge"),
    to_connection("merge", "tok"),
    to_connection("tok", "llm"),
    to_connection("llm", "head"),
    to_connection("head", "p"),
    to_end(),
]


def main():
    OUT.mkdir(parents=True, exist_ok=True)
    tex = OUT / "cosmos_verifier_plotnn.tex"
    to_generate(arch, str(tex))
    r = subprocess.run(["pdflatex", "-interaction=nonstopmode", tex.name], cwd=OUT, capture_output=True, text=True)
    if not (OUT / "cosmos_verifier_plotnn.pdf").exists():
        print(r.stdout[-2000:])
        raise SystemExit("pdflatex failed")
    subprocess.run(["pdftoppm", "-png", "-r", "220", "-singlefile", "cosmos_verifier_plotnn.pdf", "cosmos_verifier_plotnn"],
                   cwd=OUT, check=True)
    for ext in ("aux", "log"):
        (OUT / f"cosmos_verifier_plotnn.{ext}").unlink(missing_ok=True)
    print(OUT / "cosmos_verifier_plotnn.png")


if __name__ == "__main__":
    main()