2023-02-14 03:55:23 +00:00
|
|
|
from enum import EnumMeta
|
|
|
|
|
|
|
|
|
|
|
|
class GeminiMemoryManager(object):
|
|
|
|
def __init__(self, states_cls: EnumMeta):
|
|
|
|
super().__init__()
|
|
|
|
self.states_cls = states_cls
|
2023-09-19 06:20:26 +00:00
|
|
|
self._cnter = 0 # the counter of instances
|
2023-02-14 03:55:23 +00:00
|
|
|
|
|
|
|
self.total_mem = dict()
|
|
|
|
self.state_mem = dict()
|
2023-09-19 06:20:26 +00:00
|
|
|
self.state_mem["cpu"] = dict()
|
|
|
|
self.state_mem["cuda"] = dict()
|
2023-02-14 03:55:23 +00:00
|
|
|
|
|
|
|
self.reset()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def total_number(self):
|
|
|
|
return self._cnter
|
|
|
|
|
|
|
|
def reset(self):
|
2023-09-19 06:20:26 +00:00
|
|
|
self._cnter = 0 # the counter of instances
|
2023-02-14 03:55:23 +00:00
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
self.total_mem["cpu"] = 0 # memory occupation of instances in cpu
|
|
|
|
self.total_mem["cuda"] = 0 # memory of occupation of instances in cuda
|
2023-02-14 03:55:23 +00:00
|
|
|
|
|
|
|
# memory conditions for all states
|
|
|
|
for state in self.states_cls:
|
2023-09-19 06:20:26 +00:00
|
|
|
self.state_mem["cpu"][state] = 0
|
|
|
|
self.state_mem["cuda"][state] = 0
|
2023-02-14 03:55:23 +00:00
|
|
|
|
|
|
|
def register_new_instance(self):
|
|
|
|
self._cnter += 1
|
|
|
|
|
|
|
|
def delete_instance(self):
|
|
|
|
self._cnter -= 1
|
|
|
|
|
|
|
|
def print_info(self):
|
2023-09-19 06:20:26 +00:00
|
|
|
print(
|
|
|
|
f"Total number: {self.total_number}",
|
|
|
|
f"Total CPU memory occupation: {self.total_mem['cpu']}",
|
|
|
|
f"Total CUDA memory occupation: {self.total_mem['cuda']}\n",
|
|
|
|
sep="\n",
|
|
|
|
)
|
2023-02-14 03:55:23 +00:00
|
|
|
|
|
|
|
for state in self.states_cls:
|
2023-09-19 06:20:26 +00:00
|
|
|
print(
|
|
|
|
f"{state}: CPU memory occupation: {self.state_mem['cpu'][state]}",
|
|
|
|
f"{state}: CUDA memory occupation: {self.state_mem['cuda'][state]}\n",
|
|
|
|
sep="\n",
|
|
|
|
)
|