Upload 22 files
Browse filesFiles changed (22)
- classes/__pycache__/dataloader.cpython-311.pycAdded
- classes/__pycache__/dataloader.cpython-39.pycAdded
- classes/__pycache__/delayobject.cpython-311.pycAdded
- classes/__pycache__/delayobject.cpython-39.pycAdded
- classes/__pycache__/newNode.cpython-311.pycAdded
- classes/__pycache__/node.cpython-311.pycAdded
- classes/__pycache__/node.cpython-39.pycAdded
- classes/__pycache__/pin.cpython-311.pycAdded
- classes/__pycache__/pin.cpython-39.pycAdded
- classes/dataloader.pyAdded+4
- classes/delayobject.pyAdded+10
- classes/newNode.pyAdded+72
- classes/node.pyAdded+15
- classes/pin.pyAdded+6
- combined_generator.pyAdded+16
- constants.pyAdded+12
- discriminator.pyAdded+17
- edge_generator.pyAdded+25
- filtered_dataset.pickleAdded+3
- gan_train.pyAdded+152
- load_model.pyAdded+40
- matrix_generator.pyAdded+22
1234class DataLoader: def __init__(self,graph,node_feats=[],edge_index=None,label={}): self.graph = graph12345678910class DelayObject: def __init__(self, width, radix, name, range_="", toggle=None,binary =False, binary_flag = 0): self.width = width self.radix = radix self.name = name self.range_ = range_ self.binary = binary #if the object has a long binary output self.binary_flag = binary_flag #binary object switching value self.toggle = toggle if toggle is not None else {}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172class newNode: name_to_io = {'': (0,0), 'A2O1A1O1Ixp25_ASAP7_75t_L':(5,1), 'AND2x2_ASAP7_75t_L':(2,1), 'AND2x6_ASAP7_75t_L':(2,1), 'AND3x1_ASAP7_75t_L':(3,1), 'AND3x4_ASAP7_75t_L':(3,1), 'AND4x2_ASAP7_75t_L':(4,1), 'AO211x2_ASAP7_75t_L':(4,1), 'AO21x2_ASAP7_75t_L':(3,1), 'AO221x2_ASAP7_75t_L':(5,1), 'AO222x2_ASAP7_75t_L':(6,1), 'AO22x2_ASAP7_75t_L':(4,1), 'AO31x2_ASAP7_75t_L':(4,1), 'AO32x2_ASAP7_75t_L':(5,1), 'AO33x2_ASAP7_75t_L':(6,1), 'AOI211xp5_ASAP7_75t_L':(4,1), 'AOI21xp5_ASAP7_75t_L':(4,1), 'AOI221xp5_ASAP7_75t_L':(5,1), 'AOI222xp33_ASAP7_75t_L':(6,1), 'AOI22xp5_ASAP7_75t_L':(4,1), 'AOI31xp67_ASAP7_75t_L':(4,1), 'AOI32xp33_ASAP7_75t_L':(5,1), 'AOI33xp33_ASAP7_75t_L':(6,1), 'BUFx8_ASAP7_75t_SL':(1,1), 'DFFLQx4_ASAP7_75t_L':(3,1), 'FAx1_ASAP7_75t_L':(4,1), 'INVxp67_ASAP7_75t_SL':(1,1), 'NAND2xp5_ASAP7_75t_L':(2,1), 'NAND2xp67_ASAP7_75t_L':(2,1), 'NAND3xp33_ASAP7_75t_L':(3,1), 'NAND4xp75_ASAP7_75t_L':(4,1), 'NOR2xp67_ASAP7_75t_L':(2,1), 'NOR3xp33_ASAP7_75t_L':(3,1), 'NOR4xp75_ASAP7_75t_L':(4,1), 'OA211x2_ASAP7_75t_L':(4,1), 'OA21x2_ASAP7_75t_L':(3,1), 'OA221x2_ASAP7_75t_L':(5,1), 'OA222x2_ASAP7_75t_L':(6,1), 'OA22x2_ASAP7_75t_L':(4,1), 'OA31x2_ASAP7_75t_L':(4,1), 'OA33x2_ASAP7_75t_L':(6,1), 'OAI211xp5_ASAP7_75t_L':(4,1), 'OAI21xp5_ASAP7_75t_L':(3,1), 'OAI221xp5_ASAP7_75t_L':(5,1), 'OAI222xp33_ASAP7_75t_L':(6,1), 'OAI22xp5_ASAP7_75t_L':(4,1), 'OAI31xp67_ASAP7_75t_L':(4,1), 'OAI32xp33_ASAP7_75t_L':(5,1), 'OAI33xp33_ASAP7_75t_L':(6,1), 'OR2x6_ASAP7_75t_L':(2,1), 'OR3x4_ASAP7_75t_L':(3,1), 'OR4x2_ASAP7_75t_L':(4,1), 'XNOR2xp5_ASAP7_75t_L':(2,1), 'XOR2xp5_ASAP7_75t_L':(2,1)} def __init__(self,inputs:int, outputs:int, input_val:int, output_val:int, cell_name:str, signal_type:str): self.inputs = inputs self.outputs = outputs self.output_vals = output_val self.input_vals = input_val self.cell_name = cell_name self.signal_type = signal_type
def __init__(self,old): self.name = old.name self.output_vals = None self.input_vals = None self.cell_name = old.cell self.signal_type = None self.inputs = self.name_to_io[self.cell_name][0] self.outputs = self.name_to_io[self.cell_name][1]123456789101112131415class Node: def __init__(self,name:str,cell:str,output:str,inputs=[],radix="",toggle={},maxcap=0,fanout=0,load=0,peak=0,t1=0,t2=0,powertimedict={}): self.name=name self.cell=cell self.output=output self.inputs=inputs self.radix=radix self.toggle=toggle self.fanout=fanout self.load=load self.maxcap=maxcap self.powertimedict=powertimedict self.t1=t1 self.t2=t2 self.peak=peak123456class Pin: def __init__(self,pg,rv,fv,pin=" "): self.pin=pin self.pg=pg self.rv=rv self.fv=fv12345678910111213141516import torchimport torch.nn as nn
class CombinedGenerator(nn.Module): def __init__(self, edge_generator, matrix_generator): super(CombinedGenerator, self).__init__() self.edge_generator = edge_generator self.matrix_generator = matrix_generator
def forward(self, rand_noise): adj = self.edge_generator(rand_noise) matrices = self.matrix_generator(rand_noise)
comb = torch.cat((matrices, adj), dim=1) return comb123456789101112# Constants for the generatorNUM_ROWS = 16NODE_FEATURES = 3INPUT_SIZE_GEN = 100 # Size of the random noise vector for the generatorHIDDEN_SIZE_GEN = 200OUTPUT_SIZE_EDGE_GEN = NUM_ROWS * NUM_ROWS # Output size for the matrix used to generate edge in edge generatorOUTPUT_SIZE_MAT_GEN = NUM_ROWS * NODE_FEATURES # Output size for the matrix generator
# Constants for the discriminatorINPUT_SIZE_DISCRIM = NUM_ROWS * (NUM_ROWS + NODE_FEATURES)OUTPUT_SIZE_DISCRIM = 1HIDDEN_SIZE_DISCRIM = 1001234567891011121314151617import torchimport torch.nn as nn
class Discriminator(nn.Module): def __init__(self, input_size, hidden_size, output_size): super(Discriminator, self).__init__() self.fc1 = nn.Linear(input_size, hidden_size) self.fc2 = nn.Linear(hidden_size, hidden_size) self.fc3 = nn.Linear(hidden_size, output_size) self.relu = nn.ReLU() self.sigmoid = nn.Sigmoid()
def forward(self, x): out = self.relu(self.fc1(x)) out = self.relu(self.fc2(out)) out = self.sigmoid(self.fc3(out)) return out12345678910111213141516171819202122232425import torchimport torch.nn as nnfrom constants import NUM_ROWS
class EdgeGenerator(nn.Module): def __init__(self, input_size, hidden_size, output_size): super(EdgeGenerator, self).__init__() self.generator = nn.Sequential( nn.Linear(input_size, hidden_size), nn.ReLU(inplace=True), nn.Linear(hidden_size, hidden_size), nn.ReLU(inplace=True), nn.Linear(hidden_size, output_size), nn.Tanh() )
def forward(self, noise): matrix_for_edge = self.generator(noise) matrix_for_edge = torch.where(matrix_for_edge >= 0, torch.tensor(1.0), torch.tensor(0.0))
matrix_for_edge = torch.reshape(matrix_for_edge, (NUM_ROWS,NUM_ROWS))
matrix_for_edge = matrix_for_edge.fill_diagonal_(0)
return matrix_for_edge123version https://git-lfs.github.com/spec/v1oid sha256:737b8260a918acbd452977bcca7f979bcba4462e38b3acd50d78a95e97f00ad2size 18002213123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152import reimport osimport sysimport picklesys.path.append("classes")from pin import Pinfrom delayobject import DelayObjectfrom node import Nodeimport numpy as npimport matplotlib.cm as cmimport matplotlib.pyplot as pltimport networkx as nxfrom torch_geometric.utils import to_networkximport torchimport torch.nn as nnimport torch.optim as optimfrom torch_geometric.data import Batchfrom torch_geometric.nn import GCNConv, GraphConv, global_mean_poolfrom torch_geometric.data import Datafrom torch.nn import Linearimport torch.nn.functional as Ffrom constants import *import torch.utils.datafrom torch.utils.data import Dataset, DataLoader, random_splitfrom edge_generator import EdgeGeneratorfrom matrix_generator import MatrixGeneratorfrom combined_generator import CombinedGeneratorfrom discriminator import Discriminator
edge_generator = EdgeGenerator(INPUT_SIZE_GEN, HIDDEN_SIZE_GEN, OUTPUT_SIZE_EDGE_GEN)matrix_generator = MatrixGenerator(INPUT_SIZE_GEN, HIDDEN_SIZE_GEN, OUTPUT_SIZE_MAT_GEN)discriminator = Discriminator(INPUT_SIZE_DISCRIM, HIDDEN_SIZE_DISCRIM, OUTPUT_SIZE_DISCRIM)combined = CombinedGenerator(edge_generator,matrix_generator)
with open("filtered_dataset.pickle",'rb') as file: dataset = pickle.load(file)
new_dataset = []for data in dataset: if len(data.adj) == 16: new_dataset.append(torch.cat((data.x,data.adj),dim=1))
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()# Fit the scaler to the data and transform itfor data in dataset: data.x = scaler.fit_transform(data.x)
for data in dataset: data.x = torch.tensor(data.x, dtype=torch.float32) data.edge_index = torch.tensor(data.edge_index, dtype=torch.long) # Corrected dtype data.y = torch.tensor(data.y, dtype=torch.float32)
train_ratio = 0.8num_train = int(len(new_dataset) * train_ratio)num_test = len(new_dataset) - num_train
# Using random_split to create training and testing datasetstrain_dataset, test_dataset = random_split(new_dataset, [num_train, num_test])
# Create DataLoader instances for training and testingtrain_loader = DataLoader(train_dataset, batch_size=64, shuffle=True, drop_last=True, num_workers=2)test_loader = DataLoader(test_dataset, batch_size=64, shuffle=False, drop_last=True, num_workers=2)
discriminator_optimizer = optim.Adam(discriminator.parameters(), lr=0.01)combined_optimizer = optim.Adam(combined.parameters(), lr=0.001)
criterion = nn.MSELoss() device = torch.device("xpu" if torch.cuda.is_available() else "cpu")num_epochs = 1000iters = 0
G_losses = []D_losses = []
def get_fake_data(batch_size, combined): fake_graphs=[] for i in range(batch_size): rand_noise = torch.randn(1, INPUT_SIZE_GEN) fake_graphs.append(combined(rand_noise)) return fake_graphs for epoch in range(num_epochs):
for i, batch in enumerate(train_loader,0): #DISCRIM TRAINING W/ REAL DATASET batch = batch.to(device)
batch_flattened = batch.view(batch.size(0), -1)
discriminator.zero_grad() outputs = discriminator(batch_flattened).view(-1)
label = torch.ones(64).float()
errD_real = criterion(outputs,label) errD_real.backward() D_x = outputs.mean().item()
#DISCRIM TRAINING W/ FAKE DATASET fake_data = get_fake_data(64, combined) tensor_batch = torch.stack(fake_data)
tensor_batch_flattened = tensor_batch.view(tensor_batch.size(0), -1)
label.fill_(0) outputs = discriminator(tensor_batch_flattened.detach()).view(-1)
errD_fake = criterion(outputs, label) errD_fake.backward()
D_G_z1 = outputs.mean().item() errD = errD_real + errD_fake discriminator_optimizer.step()
#GENERATOR TRAINING combined.zero_grad() label.fill_(1) outputs = discriminator(tensor_batch_flattened).view(-1) errG = criterion(outputs, label) errG.backward() D_G_z2 = outputs.mean().item()
combined_optimizer.step()
# Output training stats if i % 50 == 0: print('[%d/%d][%d/%d]\tLoss_D: %.4f\tLoss_G: %.4f\tD(x): %.4f\tD(G(z)): %.4f / %.4f' % (epoch, num_epochs, i, len(train_loader), errD.item(), errG.item(), D_x, D_G_z1, D_G_z2))
# Save Losses for plotting later G_losses.append(errG.item()) D_losses.append(errD.item())
ay = get_fake_data(64, combined)
def deconstructor(matrix): mat1 = matrix[:, :3] # Columns 0 to 2 mat2 = matrix[:, 3:] # The rest of the columns return mat1, mat2
node_features, adj = deconstructor(ay[0])edge_list = torch.nonzero(adj, as_tuple=False).t()data_obj = Data(x=node_features, y = 0, edge_index = edge_list)
torch.save(combined.state_dict(), "model.pth")12345678910111213141516171819202122232425262728293031323334353637383940import torchimport torch.nn as nnfrom constants import *import torch.optim as optimfrom edge_generator import EdgeGeneratorfrom matrix_generator import MatrixGeneratorfrom combined_generator import CombinedGenerator
edge_generator = EdgeGenerator(INPUT_SIZE_GEN, HIDDEN_SIZE_GEN, OUTPUT_SIZE_EDGE_GEN)matrix_generator = MatrixGenerator(INPUT_SIZE_GEN, HIDDEN_SIZE_GEN, OUTPUT_SIZE_MAT_GEN)
model = CombinedGenerator(edge_generator, matrix_generator)model.load_state_dict(torch.load("model.pth"))model.eval()
def get_fake_data(batch_size, combined): fake_graphs=[] for i in range(batch_size): rand_noise = torch.randn(1, INPUT_SIZE_GEN) fake_graphs.append(combined(rand_noise)) return fake_graphs fake_data = get_fake_data(64,model)
def deconstructor(matrix): mat1 = matrix[:, :3] mat2 = matrix[:, 3:] return mat1, mat2
def adj_matrix_to_dict(adj_matrix): adj_dict = {} for i, row in enumerate(adj_matrix): adj_dict[i] = [] for j, edge in enumerate(row): if edge != 0: adj_dict[i].append(j) return adj_dict
dict_list = []for data in fake_data: dict_list.append(adj_matrix_to_dict(deconstructor(data)[1]))12345678910111213141516171819202122import torchimport torch.nn as nnfrom constants import NUM_ROWS, NODE_FEATURES
class MatrixGenerator(nn.Module): def __init__(self, input_size, hidden_size, output_size): super(MatrixGenerator, self).__init__() self.generator = nn.Sequential( nn.Linear(input_size, hidden_size), nn.ReLU(inplace=True), nn.Linear(hidden_size, hidden_size), nn.ReLU(inplace=True), nn.Linear(hidden_size, output_size), nn.Sigmoid() )
def forward(self, noise): gen_matrix = self.generator(noise)
gen_matrix = torch.reshape(gen_matrix, (NUM_ROWS,NODE_FEATURES))
return gen_matrixclasses/__pycache__/dataloader.cpython-311.pycAdded
classes/__pycache__/dataloader.cpython-39.pycAdded
classes/__pycache__/delayobject.cpython-311.pycAdded
classes/__pycache__/delayobject.cpython-39.pycAdded
classes/__pycache__/newNode.cpython-311.pycAdded
classes/__pycache__/node.cpython-311.pycAdded
classes/__pycache__/node.cpython-39.pycAdded
classes/__pycache__/pin.cpython-311.pycAdded
classes/__pycache__/pin.cpython-39.pycAdded
classes/dataloader.pyAdded+4
classes/delayobject.pyAdded+10
classes/newNode.pyAdded+72
classes/node.pyAdded+15
classes/pin.pyAdded+6
combined_generator.pyAdded+16
constants.pyAdded+12
discriminator.pyAdded+17
edge_generator.pyAdded+25
filtered_dataset.pickleAdded+3
gan_train.pyAdded+152
load_model.pyAdded+40
matrix_generator.pyAdded+22