Closed Anya28 closed 4 years ago
Since you didn't post the source code, I'm assuming that you tried something like this?
import ray
import ray.rllib.agents.a3c as a3c
from ray.tune.logger import pretty_print
import line_profiler
@profile
def main():
ray.init()
config = a3c.DEFAULT_CONFIG.copy()
config["num_gpus"] = 0
config["num_workers"] = 1
agent = a3c.A3CAgent(config=config, env="SpaceInvaders-v4")
for i in range(1000):
result = agent.train()
print(pretty_print(result))
if i % 100 == 0:
checkpoint = agent.save()
print("checkpoint saved at", checkpoint)
if __name__ == "__main__":
main()
And then you called it with:
kernprof -l rllib_profiler.py
For only 10 epochs in this example, it worked fine:
$ python -m line_profiler rllib_profiler.py.lprof
Timer unit: 1e-06 s
Total time: 89.1993 s
File: rllib_profiler.py
Function: main at line 6
Line # Hits Time Per Hit % Time Line Contents
==============================================================
6 @profile
7 def main():
8 1 538095.0 538095.0 0.6 ray.init()
9 1 55.0 55.0 0.0 config = a3c.DEFAULT_CONFIG.copy()
10 1 2.0 2.0 0.0 config["num_gpus"] = 0
11 1 1.0 1.0 0.0 config["num_workers"] = 1
12 1 2102656.0 2102656.0 2.4 agent = a3c.A3CAgent(config=config, env="SpaceInvaders-v4")
13
14 11 17.0 1.5 0.0 for i in range(10):
15 10 86348015.0 8634801.5 96.8 result = agent.train()
16 10 182952.0 18295.2 0.2 print(pretty_print(result))
17
18 10 27.0 2.7 0.0 if i % 100 == 0:
19 1 27420.0 27420.0 0.0 checkpoint = agent.save()
20 1 37.0 37.0 0.0 print("checkpoint saved at", checkpoint)
Since you didn't post the source code, I'm assuming that you tried something like this?
import ray import ray.rllib.agents.a3c as a3c from ray.tune.logger import pretty_print import line_profiler @profile def main(): ray.init() config = a3c.DEFAULT_CONFIG.copy() config["num_gpus"] = 0 config["num_workers"] = 1 agent = a3c.A3CAgent(config=config, env="SpaceInvaders-v4") for i in range(1000): result = agent.train() print(pretty_print(result)) if i % 100 == 0: checkpoint = agent.save() print("checkpoint saved at", checkpoint) if __name__ == "__main__": main()
And then you called it with:
kernprof -l rllib_profiler.py
For only 10 epochs in this example, it worked fine:
$ python -m line_profiler rllib_profiler.py.lprof Timer unit: 1e-06 s Total time: 89.1993 s File: rllib_profiler.py Function: main at line 6 Line # Hits Time Per Hit % Time Line Contents ============================================================== 6 @profile 7 def main(): 8 1 538095.0 538095.0 0.6 ray.init() 9 1 55.0 55.0 0.0 config = a3c.DEFAULT_CONFIG.copy() 10 1 2.0 2.0 0.0 config["num_gpus"] = 0 11 1 1.0 1.0 0.0 config["num_workers"] = 1 12 1 2102656.0 2102656.0 2.4 agent = a3c.A3CAgent(config=config, env="SpaceInvaders-v4") 13 14 11 17.0 1.5 0.0 for i in range(10): 15 10 86348015.0 8634801.5 96.8 result = agent.train() 16 10 182952.0 18295.2 0.2 print(pretty_print(result)) 17 18 10 27.0 2.7 0.0 if i % 100 == 0: 19 1 27420.0 27420.0 0.0 checkpoint = agent.save() 20 1 37.0 37.0 0.0 print("checkpoint saved at", checkpoint)
Hi @gndctrl2mjrtm Thanks for pointing out such example for profiling. In my case, I have my customized env and nn model, run through PPO. Is there a way to profile the code to understand how much time are spending on env interaction and how much time are spending on nn weights updates? I tried to put @profile to my step/reset/.. function inside my customEnv class, but it won't go through.
custom_env example at: https://github.com/ray-project/ray/blob/master/rllib/examples/custom_env.py
Consider not using the decorator but rather the cProfile python API for manual profiling.
System information
Describe the problem
I have been trying to use profiling with ray, for which I have installed line_profiler. If I try to run the code as specified in https://ray.readthedocs.io/en/latest/user-profiling.html, I keep on getting the
NameError: name 'profile' is not defined
. However, if I try to run something outside of ray, then I never get this error for line_profiler, and it works perfectly. Has anyone come across this issue?Source code / logs