# import math import pickle from time import sleep from gpu_power_func import total_watt_consumed with open("configuration.pkl", "rb") as file: while True: try: cfg = pickle.load(file) except EOFError: break def non_decreasing(lst: list): """ Check that a list is non decreasing """ return all(x <= y for x, y in zip(lst, lst[1:])) def non_increasing(lst): """ Check that a list is non inreasing """ return all(x >= y for x, y in zip(lst, lst[1:])) def monotonic(lst): """ Check that a list is monotonic """ return non_decreasing(lst) or non_increasing(lst) def strictly_increasing(lst): """ Check that a list is strictly inreasing """ return all(x < y for x, y in zip(lst, lst[1:])) def strictly_decreasing(lst): """ Check that a list is strictly decreasing """ return all(x > y for x, y in zip(lst, lst[1:])) def strictly_monotonic(lst): """ Check that a list is strictly monotonic """ return strictly_increasing(lst) or strictly_decreasing(lst) def count_parameters(model): """Counts model amount of trainable parameters""" return sum(p.numel() for p in model.parameters() if p.requires_grad) def efficiency_stop(model, accuracy, batch, sensitivity=0.001): """ This function stops when a certain amount of generalization takes place taking into account the model efficiency """ try: energy = total_watt_consumed(cfg["pickle_path"]) except Exception as e: sleep(3) energy = total_watt_consumed(cfg["pickle_path"]) efficiency = accuracy / energy print(f"Current Efficiency: {1 - efficiency}") no_parameters = count_parameters(model) if (efficiency * no_parameters / (batch / 2) >= sensitivity) and (accuracy >= 0.5): return 1 return 0 def e_stop( early_stopping: list, train_acc: float, epoch: int, patience: int = 4, sensitivity: float = 1e-9, ): """ This function stops training early """ early_stopping.append(train_acc) if patience in (0, 1): print("Stopping Early") return 1 if epoch % patience == 0 and epoch > 0: early_stopping = early_stopping[-patience : len(early_stopping)] ini = early_stopping.pop(0) early_stopping = list(map(lambda x: x - sensitivity, early_stopping)) early_stopping.insert(0, ini) values = "" for i, v in enumerate(early_stopping): values += f"Value {i+1}: {v} > " print(values) if (train_acc > 0.5) and not strictly_increasing(early_stopping): print("Stopping Early") return 1 del early_stopping[:] return 0 def energy_bound(threshold: float = 100000.0): """Stops training when a specified amount of energy is consumed""" try: energy = total_watt_consumed(cfg["pickle_path"]) except Exception as e: sleep(3) energy = total_watt_consumed(cfg["pickle_path"]) print(f"Energy used: {energy}") if energy > threshold: print("Energy bound achieved") return 1 return 0 def accuracy_bound(train_acc: float, threshold: float = 0.99): """Stops training when a specified amount of accuracy is achieved""" if train_acc >= threshold: print("Accuracy bound achieved") return 1 return 0