From 0adcf8935f0775dd9d5936efe30e442be0e745b7 Mon Sep 17 00:00:00 2001 From: Hunter Nichols Date: Wed, 9 Dec 2020 15:31:43 -0800 Subject: [PATCH] Added linear regression model for power. --- compiler/characterizer/lib.py | 2 +- compiler/characterizer/linear_regression.py | 40 ++++++++++++--------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/compiler/characterizer/lib.py b/compiler/characterizer/lib.py index ad1f5e1c..6fdcb1bf 100644 --- a/compiler/characterizer/lib.py +++ b/compiler/characterizer/lib.py @@ -600,7 +600,7 @@ class lib: OPTS.word_size, OPTS.words_per_row, self.sram.width * self.sram.height] - char_results = m.get_prediction(model_inputs) + char_results = m.get_predictions(model_inputs) #self.d = elmore(self.sram, self.sp_file, self.corner) # char_results = self.d.analytical_delay(self.slews,self.loads) diff --git a/compiler/characterizer/linear_regression.py b/compiler/characterizer/linear_regression.py index 267ccebc..6f63c3c4 100644 --- a/compiler/characterizer/linear_regression.py +++ b/compiler/characterizer/linear_regression.py @@ -13,42 +13,48 @@ from globals import OPTS import debug relative_data_path = "/sim_data" -data_filename = "data.csv" +delay_data_filename = "data.csv" +power_data_filename = "power_data.csv" tech_path = os.environ.get('OPENRAM_TECH') data_dir = tech_path+'/'+OPTS.tech_name+relative_data_path class linear_regression(): def __init__(self): - self.model = None + self.delay_model = None + self.power_model = None - def get_prediction(self, model_inputs): + def get_predictions(self, model_inputs): - file_path = data_dir +'/'+data_filename - scaled_inputs = np.asarray([scale_input_datapoint(model_inputs, file_path)]) + delay_file_path = data_dir +'/'+delay_data_filename + power_file_path = data_dir +'/'+power_data_filename + scaled_inputs = np.asarray([scale_input_datapoint(model_inputs, delay_file_path)]) - features, labels = get_scaled_data(file_path) - self.train_model(features, labels) - scaled_pred = self.model_prediction(scaled_inputs) - pred = unscale_data(scaled_pred.tolist(), file_path) - debug.info(1,"Unscaled Prediction = {}".format(pred)) - return pred + predictions = [] + for path, model in zip([delay_file_path, power_file_path], [self.delay_model, self.power_model]): + features, labels = get_scaled_data(path) + model = self.generate_model(features, labels) + scaled_pred = self.model_prediction(model, scaled_inputs) + pred = unscale_data(scaled_pred.tolist(), path) + debug.info(1,"Unscaled Prediction = {}".format(pred)) + predictions.append(pred) + return predictions - def train_model(self, features, labels): + def generate_model(self, features, labels): """ Supervised training of model. """ - self.model = LinearRegression() - self.model.fit(features, labels) + model = LinearRegression() + model.fit(features, labels) + return model - def model_prediction(self, features): + def model_prediction(self, model, features): """ Have the model perform a prediction and unscale the prediction as the model is trained with scaled values. """ - pred = self.model.predict(features) - debug.info(1, "pred={}".format(pred)) + pred = model.predict(features) return pred \ No newline at end of file