2025-01-29 11:26:17 +00:00
|
|
|
# import math
|
2024-05-10 09:59:24 +00:00
|
|
|
import pickle
|
|
|
|
from time import sleep
|
2025-01-15 10:26:48 +00:00
|
|
|
|
2024-05-10 09:59:24 +00:00
|
|
|
from gpu_power_func import total_watt_consumed
|
|
|
|
|
2025-01-15 10:26:48 +00:00
|
|
|
with open("configuration.pkl", "rb") as file:
|
2024-05-10 09:59:24 +00:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
cfg = pickle.load(file)
|
|
|
|
except EOFError:
|
|
|
|
break
|
|
|
|
|
2025-01-15 10:26:48 +00:00
|
|
|
|
2025-01-29 11:26:17 +00:00
|
|
|
def non_decreasing(lst: list):
|
|
|
|
"""
|
|
|
|
Check that a list is non decreasing
|
|
|
|
"""
|
|
|
|
return all(x <= y for x, y in zip(lst, lst[1:]))
|
2025-01-15 10:26:48 +00:00
|
|
|
|
|
|
|
|
2025-01-29 11:26:17 +00:00
|
|
|
def non_increasing(lst):
|
|
|
|
"""
|
|
|
|
Check that a list is non inreasing
|
|
|
|
"""
|
|
|
|
return all(x >= y for x, y in zip(lst, lst[1:]))
|
2025-01-15 10:26:48 +00:00
|
|
|
|
|
|
|
|
2025-01-29 11:26:17 +00:00
|
|
|
def monotonic(lst):
|
|
|
|
"""
|
|
|
|
Check that a list is monotonic
|
|
|
|
"""
|
|
|
|
return non_decreasing(lst) or non_increasing(lst)
|
2025-01-15 10:26:48 +00:00
|
|
|
|
|
|
|
|
2025-01-29 11:26:17 +00:00
|
|
|
def strictly_increasing(lst):
|
|
|
|
"""
|
|
|
|
Check that a list is strictly inreasing
|
|
|
|
"""
|
|
|
|
return all(x < y for x, y in zip(lst, lst[1:]))
|
2025-01-15 10:26:48 +00:00
|
|
|
|
|
|
|
|
2025-01-29 11:26:17 +00:00
|
|
|
def strictly_decreasing(lst):
|
|
|
|
"""
|
|
|
|
Check that a list is strictly decreasing
|
|
|
|
"""
|
|
|
|
return all(x > y for x, y in zip(lst, lst[1:]))
|
2025-01-15 10:26:48 +00:00
|
|
|
|
|
|
|
|
2025-01-29 11:26:17 +00:00
|
|
|
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
|
2025-01-15 10:26:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def e_stop(
|
|
|
|
early_stopping: list,
|
|
|
|
train_acc: float,
|
|
|
|
epoch: int,
|
|
|
|
patience: int = 4,
|
|
|
|
sensitivity: float = 1e-9,
|
|
|
|
):
|
2025-01-29 11:26:17 +00:00
|
|
|
"""
|
|
|
|
This function stops training early
|
|
|
|
"""
|
2024-05-10 09:59:24 +00:00
|
|
|
early_stopping.append(train_acc)
|
2025-01-15 10:26:48 +00:00
|
|
|
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
|
2024-05-10 09:59:24 +00:00
|
|
|
del early_stopping[:]
|
|
|
|
return 0
|
|
|
|
|
2025-01-15 10:26:48 +00:00
|
|
|
|
|
|
|
def energy_bound(threshold: float = 100000.0):
|
|
|
|
"""Stops training when a specified amount of energy is consumed"""
|
2024-05-10 09:59:24 +00:00
|
|
|
try:
|
|
|
|
energy = total_watt_consumed(cfg["pickle_path"])
|
|
|
|
except Exception as e:
|
|
|
|
sleep(3)
|
|
|
|
energy = total_watt_consumed(cfg["pickle_path"])
|
2025-01-29 11:26:17 +00:00
|
|
|
print(f"Energy used: {energy}")
|
2024-05-10 09:59:24 +00:00
|
|
|
if energy > threshold:
|
|
|
|
print("Energy bound achieved")
|
|
|
|
return 1
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2025-01-15 10:26:48 +00:00
|
|
|
def accuracy_bound(train_acc: float, threshold: float = 0.99):
|
2025-01-29 11:26:17 +00:00
|
|
|
"""Stops training when a specified amount of accuracy is achieved"""
|
2024-05-10 09:59:24 +00:00
|
|
|
if train_acc >= threshold:
|
|
|
|
print("Accuracy bound achieved")
|
|
|
|
return 1
|
|
|
|
return 0
|