Efficiency-of-Neural-Archit.../run_service.py

123 lines
3.1 KiB
Python
Raw Normal View History

2023-06-28 16:04:24 +00:00
import psutil
import pickle
2023-06-01 08:20:51 +00:00
import arguments
2022-04-17 16:18:54 +00:00
from time import sleep
2022-04-16 12:20:44 +00:00
import subprocess as sub
2023-06-01 08:20:51 +00:00
from arguments import makeArguments
2022-04-16 12:20:44 +00:00
2023-06-28 16:04:24 +00:00
def kill(proc_pid):
process = psutil.Process(proc_pid)
for proc in process.children(recursive=True):
proc.kill()
process.kill()
cfg = {
"model": {"net_type": None, "type": None, "size": None, "layer_type": "lrt",
"activation_type": "softplus", "priors": {
'prior_mu': 0,
'prior_sigma': 0.1,
'posterior_mu_initial': (0, 0.1), # (mean, std) normal_
'posterior_rho_initial': (-5, 0.1), # (mean, std) normal_
},
"n_epochs": 3,
"sens": 1e-9,
"energy_thrs": 10000,
"acc_thrs": 0.99,
"lr": 0.001,
"num_workers": 4,
"valid_size": 0.2,
"batch_size": 256,
"train_ens": 1,
"valid_ens": 1,
"beta_type": 0.1, # 'Blundell', 'Standard', etc. Use float for const value
},
"data": None,
"stopping_crit": None,
"save": None,
"pickle_path": None,
}
2023-06-01 08:20:51 +00:00
args = makeArguments(arguments.all_args)
2022-04-17 16:18:54 +00:00
check = list(args.values())
if all(v is None for v in check):
raise Exception("One argument required")
elif None in check:
if args['f'] is not None:
cmd = ["python", "main_frequentist.py"]
2023-06-28 16:04:24 +00:00
cfg["model"]["type"] = "frequentist"
2022-04-17 16:18:54 +00:00
elif args['b'] is not None:
cmd = ["python", "main_bayesian.py"]
2023-06-28 16:04:24 +00:00
cfg["model"]["type"] = "bayesian"
2022-04-17 16:18:54 +00:00
else:
raise Exception("Only one argument allowed")
wide = args["f"] or args["b"]
2023-06-28 16:04:24 +00:00
cfg["model"]["size"] = wide
cfg["data"] = args["dataset"]
cfg["model"]["net_type"] = args["net_type"]
2022-04-17 16:18:54 +00:00
2023-06-01 08:20:51 +00:00
if args['EarlyStopping']:
2023-06-28 16:04:24 +00:00
cfg["stopping_crit"] = 2
2023-06-01 08:20:51 +00:00
elif args['EnergyBound']:
2023-06-28 16:04:24 +00:00
cfg["stopping_crit"] = 3
2023-06-01 08:20:51 +00:00
elif args['AccuracyBound']:
2023-06-28 16:04:24 +00:00
cfg["stopping_crit"] = 4
2023-06-01 08:20:51 +00:00
else:
2023-06-28 16:04:24 +00:00
cfg["stopping_crit"] = 1
2023-06-01 08:20:51 +00:00
if args['Save']:
2023-06-28 16:04:24 +00:00
cfg["save"] = 1
2023-06-01 08:20:51 +00:00
else:
2023-06-28 16:04:24 +00:00
cfg["save"] = 0
cfg["pickle_path"] = "{}_wattdata_{}.pkl".format(cfg["model"]["type"],cfg["model"]["size"])
with open("configuration.pkl", "wb") as f:
pickle.dump(cfg, f)
#print(args)
#print(cfg)
2023-06-01 08:20:51 +00:00
2022-04-17 16:18:54 +00:00
sleep(3)
if cmd[1] == "main_frequentist.py":
cmd2 = ["./cpu_watt.sh", "freq_{}_cpu_watts".format(wide)]
cmd3 = ["./mem_free.sh", "freq_{}_ram_use".format(wide)]
2023-06-28 16:04:24 +00:00
cmd4 = ["./radeontop.sh", "freq_{}_flop_app".format(wide)]
2022-04-17 16:18:54 +00:00
elif cmd[1] == "main_bayesian.py":
cmd2 = ["./cpu_watt.sh", "bayes_{}_cpu_watts".format(wide)]
cmd3 = ["./mem_free.sh", "bayes_{}_ram_use".format(wide)]
2023-06-28 16:04:24 +00:00
cmd4 = ["./radeontop.sh", "bayes_{}_flop_app".format(wide)]
2022-04-16 12:20:44 +00:00
path = sub.check_output(['pwd'])
path = path.decode()
path = path.replace('\n', '')
2023-06-01 08:20:51 +00:00
startWattCounter = 'python ' + path + '/amd_sample_draw.py'
2022-04-16 12:20:44 +00:00
2023-06-28 16:04:24 +00:00
2022-04-16 12:20:44 +00:00
p1 = sub.Popen(cmd)
p2 = sub.Popen(startWattCounter.split(),stdin=sub.PIPE,stdout=sub.PIPE, stderr=sub.PIPE)
2023-06-28 16:04:24 +00:00
p3 = sub.Popen(cmd2,stdin=sub.PIPE,stdout=sub.PIPE, stderr=sub.PIPE)
p4 = sub.Popen(cmd3,stdin=sub.PIPE,stdout=sub.PIPE, stderr=sub.PIPE)
p5 = sub.Popen(cmd4,stdin=sub.PIPE,stdout=sub.PIPE, stderr=sub.PIPE)
2022-04-16 12:20:44 +00:00
retcode = p1.wait()
2022-04-17 16:18:54 +00:00
print("Return code: {}".format(retcode))
2022-04-16 12:20:44 +00:00
p1.kill()
2023-06-28 16:04:24 +00:00
kill(p2.pid)
kill(p3.pid)
kill(p4.pid)
kill(p5.pid)