forked from markus7800/PPLStaticFactor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsanity_check.py
More file actions
136 lines (104 loc) · 4.54 KB
/
sanity_check.py
File metadata and controls
136 lines (104 loc) · 4.54 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import subprocess
import sys
from bcolors import bcolors
import sys
sys.path.append("src/")
from formal.formal_cfg import get_IR_for_formal
from formal.factorisation_builder import FactorisationBuilder
import time
import os
from pathlib import Path
Path("evaluation/benchmark/generated/").mkdir(parents=True, exist_ok=True)
filename = "hmm.jl"
print(bcolors.HEADER + "Check build IR..." + bcolors.ENDC)
ir = get_IR_for_formal("evaluation/benchmark/" + filename)
print(bcolors.OKGREEN + "Check OK." + bcolors.ENDC + "\n")
print(bcolors.HEADER + "Check generate factorisation..." + bcolors.ENDC)
pw = FactorisationBuilder(filename, ir, True, True)
pw.write_program()
with open("evaluation/benchmark/generated/" + filename, "w") as f:
f.write(pw.out)
print(bcolors.OKGREEN + "Check OK." + bcolors.ENDC + "\n")
print(bcolors.HEADER + "Check build IR (unrolled)..." + bcolors.ENDC)
ir = get_IR_for_formal("evaluation/unrolled/" + filename, unroll_loops=True)
print(bcolors.OKGREEN + "Check OK." + bcolors.ENDC + "\n")
print(bcolors.HEADER + "Check generate factorisation (unrolled)..." + bcolors.ENDC)
pw = FactorisationBuilder(filename, ir, True, True)
pw.write_program()
with open("evaluation/unrolled/generated/" + filename, "w") as f:
f.write(pw.out)
print(bcolors.OKGREEN + "Check OK." + bcolors.ENDC + "\n")
print(bcolors.HEADER + "Check LMH..." + bcolors.ENDC)
cmd = ["julia", "--project=.", "evaluation/bench_lmh.jl", "benchmark", filename]
subprocess.run(cmd, capture_output=False)
os.remove("evaluation/lmh_results.csv")
print(bcolors.OKGREEN + "Check OK." + bcolors.ENDC + "\n")
print(bcolors.HEADER + "Check BBVI..." + bcolors.ENDC)
cmd = ["julia", "--project=.", "evaluation/bench_vi.jl", "benchmark", filename, str(0)]
subprocess.run(cmd, capture_output=False)
os.remove("evaluation/vi_results.csv")
print(bcolors.OKGREEN + "Check OK." + bcolors.ENDC + "\n")
print(bcolors.HEADER + "Check SMC..." + bcolors.ENDC)
cmd = ["julia", "--project=.", "evaluation/bench_smc.jl", "benchmark", filename]
subprocess.run(cmd, capture_output=False)
os.remove("evaluation/smc_results.csv")
print(bcolors.OKGREEN + "Check OK." + bcolors.ENDC + "\n")
print(bcolors.HEADER + "Check Gen LMH..." + bcolors.ENDC)
filename = "hmm.jl"
cmd = ["julia", "--project=.", "compare/gen/" + filename]
subprocess.run(cmd, capture_output=False)
os.remove("compare/gen/lmh_results.csv")
print(bcolors.OKGREEN + "Check OK." + bcolors.ENDC + "\n")
print(bcolors.HEADER + "Check Pyro BBVI..." + bcolors.ENDC)
filename = "hmm.py"
from compare.pyro_bbvi.run_vi import infer_str
with open("compare/pyro_bbvi/" + filename, "r") as src_f:
src = src_f.read()
src += f"\nN_ITER = {10}\nL = {10}\n"
src += infer_str
with open("compare/pyro_bbvi/tmp.py", "w") as tmp_f:
tmp_f.write(src)
cmd = ["python3", "compare/pyro_bbvi/tmp.py"]
res = subprocess.run(cmd, capture_output=False)
os.remove("compare/pyro_bbvi/vi_results.csv")
print(bcolors.OKGREEN + "Check OK." + bcolors.ENDC + "\n")
print(bcolors.HEADER + "Check WebPPL BBVI..." + bcolors.ENDC)
filename, N, variants = ("hmm.wppl", 1e4, ("rec",))
from compare.webppl.run_lmh import rec_lmh_str, iter_lmh_str
N_SEEDS = 10
with open("compare/webppl/" + filename, "r") as src_f:
src = src_f.read()
src += f"\nvar N = {N}\nvar N_seeds = {N_SEEDS}\n"
if "rec" in variants:
src += rec_lmh_str
if "iter" in variants:
src += iter_lmh_str
with open("compare/webppl/tmp.wppl", "w") as tmp_f:
tmp_f.write(src)
cmd = ["webppl", "--random-seed=0", "compare/webppl/tmp.wppl"]
res = subprocess.run(cmd, capture_output=True)
out = res.stdout.decode()
print(out)
assert res.stderr.decode() == ""
print(bcolors.OKGREEN + "Check OK." + bcolors.ENDC + "\n")
print(bcolors.HEADER + "Check WebPPL SMC..." + bcolors.ENDC)
filename = "hmm.wppl"
from compare.webppl.run_smc import infer_1_str, infer_2_str
N_seeds = 10
with open("compare/webppl/" + filename, "r") as src_f:
src = src_f.read()
with open("compare/webppl/tmp.wppl", "w") as tmp_f:
tmp_f.write(src + f"\nvar N_seeds = {N_seeds}\n" + infer_1_str)
cmd = ["webppl", "--random-seed=0", "compare/webppl/tmp.wppl"]
res = subprocess.run(cmd, capture_output=True)
out = res.stdout.decode()
print(out)
with open("compare/webppl/smc/" + filename[:-5] + ".js", "r") as src_f:
src = src_f.read()
with open("compare/webppl/smc/tmp.js", "w") as tmp_f:
tmp_f.write(src + f"\nvar N_seeds = {N_seeds}\n" + infer_2_str)
cmd = ["node", "compare/webppl/smc/tmp.js"]
res = subprocess.run(cmd, capture_output=True)
out = res.stdout.decode()
print(out)
print(bcolors.OKGREEN + "Check OK." + bcolors.ENDC + "\n")