Harshil Patel commited on
Commit
0e323dc
·
1 Parent(s): 03de09a

Add code to make total budget expand with total resources

Browse files
Files changed (1) hide show
  1. src/manager/budget_manager.py +23 -1
src/manager/budget_manager.py CHANGED
@@ -1,10 +1,32 @@
1
  from src.manager.utils.singleton import singleton
2
-
 
3
  @singleton
4
  class BudgetManager():
5
  TOTAL_BUDGET = 100
6
  current_expense = 0
 
 
 
 
 
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  def get_total_budget(self):
9
  return self.TOTAL_BUDGET
10
 
 
1
  from src.manager.utils.singleton import singleton
2
+ import torch
3
+ import psutil
4
  @singleton
5
  class BudgetManager():
6
  TOTAL_BUDGET = 100
7
  current_expense = 0
8
+ is_budget_initialized = False
9
+ def __init__(self):
10
+ if not self.is_budget_initialized:
11
+ self.TOTAL_BUDGET = self.set_total_budget()
12
+ self.is_budget_initialized = True
13
 
14
+ def set_total_budget(self)-> int:
15
+ total_mem = 0
16
+ if torch.cuda.is_available():
17
+ gpu_index = torch.cuda.current_device()
18
+ gpu_name = torch.cuda.get_device_name(gpu_index)
19
+ total_vram = torch.cuda.get_device_properties(gpu_index).total_memory
20
+ total_mem = total_vram /1024 ** 3
21
+ print(f"GPU detected: {gpu_name}")
22
+ print(f"Total VRAM: {total_mem:.2f} GB")
23
+ else:
24
+ mem = psutil.virtual_memory()
25
+ total_mem = mem.total/ 1024 ** 3
26
+ print("No GPU detected. Using CPU.")
27
+ print(f"Total RAM: {total_mem:.2f} GB")
28
+ return round((total_mem / 16) * 100)
29
+
30
  def get_total_budget(self):
31
  return self.TOTAL_BUDGET
32