Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 70 additions & 1 deletion RLA/easy_log/time_used_recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,78 @@
from RLA.easy_log import logger
import time

class SingleTimeTracker:
def __init__(self,name:str='untitled') -> None:
self.name=name
self.t = 0.0
self.call_time=0
self.time_cost = 0.0

rc_start_time = {}
def __enter__(self):
# trace time
self.call_time+=1
self.t = time.perf_counter()

return self

def __exit__(self, exc_type, exc_val, exc_tb):
self.time_cost += time.perf_counter() - self.t


class TimeTracker:
def __init__(self):
self.t0=time.time()#to calc total time
self.time_dict=dict()

def add(self,name='untitled'):
"""
:param name: specify the SingleTimeTracker in the time_dict, recommend use
line num in the scripts, like 'xxx.py Line xxx', can be easily got in python scripts using
`os.path.basename(__file__)+' line '+str(sys._getframe().f_lineno)`
"""
if name not in self.time_dict.keys():
self.time_dict.update({name:SingleTimeTracker(name)})
return self.time_dict[name]

def __call__(self, name:str):
return self.time_dict[name]

def clear(self):
self.time_dict=dict()

def statistic_entry(self,name:str):
"""
calc total calls of the
"""
assert name in self.time_dict.keys()

t1=time.time()
t_passed=t1-self.t0
return {
'total calls/'+name:self.time_dict[name].call_time,
'total time cost/'+name:self.time_dict[name].time_cost,
'average time cost/'+name:self.time_dict[name].time_cost/(1e-6+self.time_dict[name].call_time),
'time cost percentage/'+name:self.time_dict[name].time_cost/(1e-6+t_passed)
}

def get_info(self):
info={}
for k in self.time_dict.keys():
for entry_k,entry_v in self.statistic_entry(k).items():
info[entry_k]=entry_v
return info

def log(self,exclude_lst=['csv']):
logger.info('---------time dashboard---------')
for k in self.time_dict.keys():
for entry_k,entry_v in self.statistic_entry(k).items():
logger.record_tabular('time_used/'+entry_k,entry_v,exclude=exclude_lst)
logger.info(f"[{entry_k}]: {entry_v}")
logger.info('')
logger.info('---------dashboard end---------')


rc_start_time = {}

def time_record(name):
"""
Expand Down
15 changes: 8 additions & 7 deletions RLA/easy_plot/plot_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ def plot_results(
smooth_step: float - when resampling (i.e. when resample > 0 or average_group is True), use this EMA decay parameter (in units of the new grid step).
See docstrings for decay_steps in symmetric_ema or one_sided_ema functions.


'''
score_results = {}
if vary_len_plot:
Expand Down Expand Up @@ -390,18 +391,18 @@ def plot_results(
elif tiling == 'symmetric':
import math
N = len(sk2r)
largest_divisor = 1
for i in range(1, int(math.sqrt(N))+1):
if N % i == 0:
largest_divisor = i
ncols = largest_divisor
nrows = N // ncols
largest_divisor = int(math.sqrt(N))
# for i in range(1, int(math.sqrt(N))+1):
# if N % i == 0:
# largest_divisor = i
nrows = largest_divisor
ncols = int(math.ceil(N / nrows))
figsize = figsize or (7 * ncols, 6 * nrows)
# if legend_outside:
# figsize = list(figsize)
# figsize[0] += 4
# figsize = tuple(figsize)
f, axarr = plt.subplots(nrows, ncols, sharex=False, squeeze=False, figsize=figsize, dpi=90 * ncols)
f, axarr = plt.subplots(nrows, ncols, sharex=False, squeeze=False, figsize=figsize)
groups = []
for results in allresults:
groups.extend(group_fn(results)[0])
Expand Down
2 changes: 2 additions & 0 deletions RLA/trackers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Created by xionghuichen at 2022/11/25
# Email: chenxh@lamda.nju.edu.cn
28 changes: 28 additions & 0 deletions RLA/trackers/memory_used_recorder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Created by xionghuichen at 2022/11/25
# Email: chenxh@lamda.nju.edu.cn
import sys


from RLA import logger, exp_manager


def print_large_memory_variable():
large_mermory_dict = {}

def sizeof_fmt(num, suffix='B'):
for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']:
if abs(num) < 1024.0:
return "%3.1f %s%s" % (num, unit, suffix), unit
num /= 1024.0
return "%.1f %s%s" % (num, 'Yi', suffix), 'Yi'

for name, size in sorted(((name, sys.getsizeof(value)) for name, value in locals().items()),
key=lambda x: -x[1])[:10]:
size_str, fmt_type = sizeof_fmt(size)
if fmt_type in ['', 'Ki', 'Mi']:
continue
logger.info("{:>30}: {:>8}".format(name, size_str))
large_mermory_dict[str(name)] = size_str
if large_mermory_dict != {}:
summary = exp_manager.dict_to_table_text_summary(large_mermory_dict, 'large_memory')
exp_manager.add_summary_to_logger(summary, 'large_memory')
107 changes: 107 additions & 0 deletions RLA/trackers/time_used_recorder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Created by xionghuichen at 2022/7/29
# Email: chenxh@lamda.nju.edu.cn
from RLA.easy_log import logger
import time

class SingleTimeTracker:
def __init__(self,name:str='untitled') -> None:
self.name=name
self.t = 0.0
self.call_time=0
self.time_cost = 0.0

def __enter__(self):
# trace time
self.call_time+=1
self.t = time.perf_counter()

return self

def __exit__(self, exc_type, exc_val, exc_tb):
self.time_cost += time.perf_counter() - self.t


class TimeTracker:
def __init__(self):
self.t0=time.time()#to calc total time
self.time_dict=dict()

def add(self,name='untitled'):
"""
:param name: specify the SingleTimeTracker in the time_dict, recommend use
line num in the scripts, like 'xxx.py Line xxx', can be easily got in python scripts using
`os.path.basename(__file__)+' line '+str(sys._getframe().f_lineno)`
"""
if name not in self.time_dict.keys():
self.time_dict.update({name:SingleTimeTracker(name)})
return self.time_dict[name]

def __call__(self, name:str):
return self.time_dict[name]

def clear(self):
self.time_dict=dict()

def statistic_entry(self,name:str):
"""
calc total calls of the
"""
assert name in self.time_dict.keys()

t1=time.time()
t_passed=t1-self.t0
return {
'total calls/'+name:self.time_dict[name].call_time,
'total time cost/'+name:self.time_dict[name].time_cost,
'average time cost/'+name:self.time_dict[name].time_cost/(1e-6+self.time_dict[name].call_time),
'time cost percentage/'+name:self.time_dict[name].time_cost/(1e-6+t_passed)
}

def get_info(self):
info={}
for k in self.time_dict.keys():
for entry_k,entry_v in self.statistic_entry(k).items():
info[entry_k]=entry_v
return info

def log(self,exclude_lst=['csv']):
logger.info('---------time dashboard---------')
for k in self.time_dict.keys():
for entry_k,entry_v in self.statistic_entry(k).items():
logger.record_tabular('time_used/'+entry_k,entry_v,exclude=exclude_lst)
logger.info(f"[{entry_k}]: {entry_v}")
logger.info('')
logger.info('---------dashboard end---------')


rc_start_time = {}

def time_record(name):
"""
record the consumed time of your code snippet. call this function to start a recorder.
"name" is identifier to distinguish different recorder and record different snippets at the same time.
call time_record_end to end a recorder.
:param name: identifier of your code snippet.
:type name: str
:return:
:rtype:
"""
assert name not in rc_start_time
rc_start_time[name] = time.time()


def time_record_end(name):
"""
record the consumed time of your code snippet. call this function to start a recorder.
"name" is identifier to distinguish different recorder and record different snippets at the same time.
call time_record_end to end a recorder.
:param name: identifier of your code snippet.
:type name: str
:return:
:rtype:
"""
end_time = time.time()
start_time = rc_start_time[name]
logger.record_tabular("time_used/{}".format(name), end_time - start_time)
logger.info("[test] func {0} time used {1:.2f}".format(name, end_time - start_time))
del rc_start_time[name]
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='RLA',
version="0.6.0-pre",
version="0.6.0",
description=(
'RL assistant'
),
Expand Down