Skip to content

Commit

Permalink
Create resource.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 6, 2024
1 parent a9a805d commit 7a75861
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions ai/optimization/resource.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import psutil
import torch

class ResourceOptimizer:
def __init__(self, model: torch.nn.Module):
self.model = model
self.cpu_usage = 0
self.memory_usage = 0

def monitor_resources(self):
# Monitor CPU and memory usage
self.cpu_usage = psutil.cpu_percent()
self.memory_usage = psutil.virtual_memory().percent

def optimize_cpu(self, threshold: float):
# Optimize CPU usage by reducing batch size or model complexity
if self.cpu_usage > threshold:
batch_size = self.model.batch_size
self.model.batch_size = batch_size // 2
print(f"Reducing batch size to {batch_size // 2} to optimize CPU usage")

def optimize_memory(self, threshold: float):
# Optimize memory usage by reducing model size or using mixed precision
if self.memory_usage > threshold:
model_size = self.model.size()
self.model.size = model_size // 2
print(f"Reducing model size to {model_size // 2} to optimize memory usage")

class ResourceScheduler:
def __init__(self, optimizer: ResourceOptimizer, monitor_frequency: int):
self.optimizer = optimizer
self.monitor_frequency = monitor_frequency
self.epoch = 0

def step(self):
if self.epoch % self.monitor_frequency == 0:
self.optimizer.monitor_resources()
self.optimizer.optimize_cpu(0.8)
self.optimizer.optimize_memory(0.8)
self.epoch += 1

0 comments on commit 7a75861

Please sign in to comment.