Modified the code for frequentist experiment

This commit is contained in:
Eduardo Cueto 2023-06-29 11:45:28 +01:00
parent a2300273a5
commit 0f8ee842a3
3 changed files with 35 additions and 64 deletions

18
.gitignore vendored
View File

@ -1,19 +1,21 @@
__pycache__/
checkpoints/
data/
data_budget/
experiment-power-draw/ experiment-power-draw/
**/__pycache__/
**/__init__.py
**/**/__pycache__/ **/**/__pycache__/
**/**/__init__.py **/**/__init__.py
stp **/__pycache__/
sav **/__init__.py
checkpoints/
__pycache__/
data_budget/
bayes_* bayes_*
times_* times_*
.vscode
freq_* freq_*
data/
.idea
*.pkl *.pkl
*.txt *.txt
stp
sav
bay bay
frq frq
sav sav

View File

@ -8,20 +8,26 @@ import metrics
import argparse import argparse
import numpy as np import numpy as np
import torch.nn as nn import torch.nn as nn
import amd_sample_draw
from datetime import datetime from datetime import datetime
import config_frequentist as cfg
from torch.optim import Adam, lr_scheduler from torch.optim import Adam, lr_scheduler
from gpu_power_func import total_watt_consumed
from models.NonBayesianModels.LeNet import LeNet from models.NonBayesianModels.LeNet import LeNet
from models.NonBayesianModels.AlexNet import AlexNet from models.NonBayesianModels.AlexNet import AlexNet
from stopping_crit import earlyStopping, energyBound, accuracyBound from stopping_crit import earlyStopping, energyBound, accuracyBound
from models.NonBayesianModels.ThreeConvThreeFC import ThreeConvThreeFC from models.NonBayesianModels.ThreeConvThreeFC import ThreeConvThreeFC
with (open("configuration.pkl", "rb")) as file:
while True:
try:
cfg = pickle.load(file)
except EOFError:
break
# CUDA settings # CUDA settings
device = torch.device("cuda:1" if torch.cuda.is_available() else "cpu") device = torch.device("cuda:1" if torch.cuda.is_available() else "cpu")
def getModel(net_type, inputs, outputs,wide=cfg.wide): def getModel(net_type, inputs, outputs,wide=cfg["model"]["size"]):
if (net_type == 'lenet'): if (net_type == 'lenet'):
return LeNet(outputs, inputs,wide) return LeNet(outputs, inputs,wide)
elif (net_type == 'alexnet'): elif (net_type == 'alexnet'):
@ -64,11 +70,11 @@ def validate_model(net, criterion, valid_loader):
def run(dataset, net_type): def run(dataset, net_type):
# Hyper Parameter settings # Hyper Parameter settings
n_epochs = cfg.n_epochs n_epochs = cfg["model"]["n_epochs"]
lr = cfg.lr lr = cfg["model"]["lr"]
num_workers = cfg.num_workers num_workers = cfg["model"]["num_workers"]
valid_size = cfg.valid_size valid_size = cfg["model"]["valid_size"]
batch_size = cfg.batch_size batch_size = cfg["model"]["batch_size"]
trainset, testset, inputs, outputs = data.getDataset(dataset) trainset, testset, inputs, outputs = data.getDataset(dataset)
train_loader, valid_loader, test_loader = data.getDataloader( train_loader, valid_loader, test_loader = data.getDataloader(
@ -76,15 +82,13 @@ def run(dataset, net_type):
net = getModel(net_type, inputs, outputs).to(device) net = getModel(net_type, inputs, outputs).to(device)
ckpt_dir = f'checkpoints/{dataset}/frequentist' ckpt_dir = f'checkpoints/{dataset}/frequentist'
ckpt_name = f'checkpoints/{dataset}/frequentist/model_{net_type}_{cfg.wide}.pt' ckpt_name = f'checkpoints/{dataset}/frequentist/model_{net_type}_{cfg["model"]["size"]}.pt'
if not os.path.exists(ckpt_dir): if not os.path.exists(ckpt_dir):
os.makedirs(ckpt_dir, exist_ok=True) os.makedirs(ckpt_dir, exist_ok=True)
with open("stp", "r") as file: stp = cfg["stopping_crit"]
stp = int(file.read()) sav = cfg["save"]
with open("sav", "r") as file:
sav = int(file.read())
criterion = nn.CrossEntropyLoss() criterion = nn.CrossEntropyLoss()
optimizer = Adam(net.parameters(), lr=lr) optimizer = Adam(net.parameters(), lr=lr)
@ -108,25 +112,25 @@ def run(dataset, net_type):
if stp == 2: if stp == 2:
#print('Using early stopping') #print('Using early stopping')
if earlyStopping(early_stop,valid_acc,epoch,cfg.sens) == 1: if earlyStopping(early_stop,valid_acc,epoch,cfg["model"]["sens"]) == 1:
break break
elif stp == 3: elif stp == 3:
#print('Using energy bound') #print('Using energy bound')
if energyBound(cfg.energy_thrs) == 1: if energyBound(cfg["model"]["energy_thrs"]) == 1:
break break
elif stp == 4: elif stp == 4:
#print('Using accuracy bound') #print('Using accuracy bound')
if accuracyBound(train_acc,cfg.acc_thrs) == 1: if accuracyBound(train_acc,cfg["model"]["acc_thrs"]) == 1:
break break
else: else:
print('Training for {} epochs'.format(cfg.n_epochs)) print('Training for {} epochs'.format(cfg["model"]["n_epochs"]))
if sav == 1: if sav == 1:
# save model when finished # save model when finished
if epoch == n_epochs: if epoch == n_epochs:
torch.save(net.state_dict(), ckpt_name) torch.save(net.state_dict(), ckpt_name)
with open("freq_exp_data_"+str(cfg.wide)+".pkl", 'wb') as f: with open("freq_exp_data_"+str(cfg["model"]["size"])+".pkl", 'wb') as f:
pickle.dump(train_data, f) pickle.dump(train_data, f)
@ -134,11 +138,7 @@ if __name__ == '__main__':
now = datetime.now() now = datetime.now()
current_time = now.strftime("%H:%M:%S") current_time = now.strftime("%H:%M:%S")
print("Initial Time =", current_time) print("Initial Time =", current_time)
parser = argparse.ArgumentParser(description = "PyTorch Frequentist Model Training") run(cfg["model"]["data"], cfg["model"]["net_type"])
parser.add_argument('--net_type', default='lenet', type=str, help='model')
parser.add_argument('--dataset', default='MNIST', type=str, help='dataset = [MNIST/CIFAR10/CIFAR100]')
args = parser.parse_args()
run(args.dataset, args.net_type)
now = datetime.now() now = datetime.now()
current_time = now.strftime("%H:%M:%S") current_time = now.strftime("%H:%M:%S")
print("Final Time =", current_time) print("Final Time =", current_time)

View File

@ -60,34 +60,20 @@ cfg["model"]["size"] = wide
cfg["data"] = args["dataset"] cfg["data"] = args["dataset"]
cfg["model"]["net_type"] = args["net_type"] cfg["model"]["net_type"] = args["net_type"]
#with open("tmp", "w") as file:
# file.write(str(wide))
if args['EarlyStopping']: if args['EarlyStopping']:
cfg["stopping_crit"] = 2 cfg["stopping_crit"] = 2
#with open("stp", "w") as file:
# file.write('2')
elif args['EnergyBound']: elif args['EnergyBound']:
cfg["stopping_crit"] = 3 cfg["stopping_crit"] = 3
#with open("stp", "w") as file:
# file.write('3')
elif args['AccuracyBound']: elif args['AccuracyBound']:
cfg["stopping_crit"] = 4 cfg["stopping_crit"] = 4
#with open("stp", "w") as file:
# file.write('4')
else: else:
cfg["stopping_crit"] = 1 cfg["stopping_crit"] = 1
#with open("stp", "w") as file:
# file.write('1')
if args['Save']: if args['Save']:
cfg["save"] = 1 cfg["save"] = 1
#with open("sav", "w") as file:
# file.write('1')
else: else:
cfg["save"] = 0 cfg["save"] = 0
#with open("sav", "w") as file:
# file.write('0')
cfg["pickle_path"] = "{}_wattdata_{}.pkl".format(cfg["model"]["type"],cfg["model"]["size"]) cfg["pickle_path"] = "{}_wattdata_{}.pkl".format(cfg["model"]["type"],cfg["model"]["size"])
@ -96,7 +82,7 @@ with open("configuration.pkl", "wb") as f:
pickle.dump(cfg, f) pickle.dump(cfg, f)
#print(args) #print(args)
print(cfg) #print(cfg)
sleep(3) sleep(3)
@ -105,18 +91,10 @@ if cmd[1] == "main_frequentist.py":
cmd2 = ["./cpu_watt.sh", "freq_{}_cpu_watts".format(wide)] cmd2 = ["./cpu_watt.sh", "freq_{}_cpu_watts".format(wide)]
cmd3 = ["./mem_free.sh", "freq_{}_ram_use".format(wide)] cmd3 = ["./mem_free.sh", "freq_{}_ram_use".format(wide)]
cmd4 = ["./radeontop.sh", "freq_{}_flop_app".format(wide)] cmd4 = ["./radeontop.sh", "freq_{}_flop_app".format(wide)]
#with open("frq", "w") as file:
# file.write(str(1))
#with open("bay", "w") as file:
# file.write(str(0))
elif cmd[1] == "main_bayesian.py": elif cmd[1] == "main_bayesian.py":
cmd2 = ["./cpu_watt.sh", "bayes_{}_cpu_watts".format(wide)] cmd2 = ["./cpu_watt.sh", "bayes_{}_cpu_watts".format(wide)]
cmd3 = ["./mem_free.sh", "bayes_{}_ram_use".format(wide)] cmd3 = ["./mem_free.sh", "bayes_{}_ram_use".format(wide)]
cmd4 = ["./radeontop.sh", "bayes_{}_flop_app".format(wide)] cmd4 = ["./radeontop.sh", "bayes_{}_flop_app".format(wide)]
#with open("bay", "w") as file:
# file.write(str(1))
#with open("frq", "w") as file:
# file.write(str(0))
path = sub.check_output(['pwd']) path = sub.check_output(['pwd'])
@ -126,18 +104,9 @@ path = path.replace('\n', '')
startWattCounter = 'python ' + path + '/amd_sample_draw.py' startWattCounter = 'python ' + path + '/amd_sample_draw.py'
#test = startNODE.split()
#test.append(pythonEnd)
#test.append(pythonEnd2)
#startNODE = test
#print(startNODE)
#print(startWattCounter)
p1 = sub.Popen(cmd) p1 = sub.Popen(cmd)
#p2 = sub.Popen(startWattCounter.split(),stdin=sub.PIPE,stdout=sub.PIPE, stderr=sub.PIPE) p2 = sub.Popen(startWattCounter.split(),stdin=sub.PIPE,stdout=sub.PIPE, stderr=sub.PIPE)
p2 = sub.Popen(startWattCounter.split())
p3 = sub.Popen(cmd2,stdin=sub.PIPE,stdout=sub.PIPE, stderr=sub.PIPE) 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) 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) p5 = sub.Popen(cmd4,stdin=sub.PIPE,stdout=sub.PIPE, stderr=sub.PIPE)