How to attach debugger

Hey all,

I’m able to train using the standard procedure but now I want to use multiple Unity instances for the RollerBall example. When I attach my debugger to the mlagents.trainers.learn script (from PyCharm) I get “torch has no attribute set_num_threads” and found a github issue here: Trying to use the learn.py script from the command line throws a "module 'torch' has no attribute 'set_num_threads'" error if PyTorch is not installed · Issue #4526 · Unity-Technologies/ml-agents · GitHub. Is there another way to attach a debugger?

Thanks, specifics below in case anyone is familiar with the problem.

Version information:
Unity version: 2019.4.21f1
ml-agents: 0.24.0,
ml-agents-envs: 0.24.0,
Communicator API: 1.4.0,
PyTorch: 1.7.0

command:
python -m mlagents.trainers.learn config/rollerball_config.yaml --run-id=RollerBall3 --num-envs=2 --env=$UNITY_PATH --force

Traceback (most recent call last):
File “/home/joe/Documents/tools/anaconda3/envs/shade/lib/python3.8/runpy.py”, line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File “/home/joe/Documents/tools/anaconda3/envs/shade/lib/python3.8/runpy.py”, line 87, in _run_code
exec(code, run_globals)
File “/home/joe/Documents/ml-agents/ml-agents/mlagents/trainers/learn.py”, line 255, in
main()
File “/home/joe/Documents/ml-agents/ml-agents/mlagents/trainers/learn.py”, line 250, in main
run_cli(parse_command_line())
File “/home/joe/Documents/ml-agents/ml-agents/mlagents/trainers/learn.py”, line 246, in run_cli
run_training(run_seed, options)
File “/home/joe/Documents/ml-agents/ml-agents/mlagents/trainers/learn.py”, line 125, in run_training
tc.start_learning(env_manager)
File “/home/joe/Documents/ml-agents/ml-agents-envs/mlagents_envs/timers.py”, line 305, in wrapped
return func(*args, **kwargs)
File “/home/joe/Documents/ml-agents/ml-agents/mlagents/trainers/trainer_controller.py”, line 197, in start_learning
raise ex
File “/home/joe/Documents/ml-agents/ml-agents/mlagents/trainers/trainer_controller.py”, line 173, in start_learning
self._reset_env(env_manager)
File “/home/joe/Documents/ml-agents/ml-agents-envs/mlagents_envs/timers.py”, line 305, in wrapped
return func(*args, **kwargs)
File “/home/joe/Documents/ml-agents/ml-agents/mlagents/trainers/trainer_controller.py”, line 105, in _reset_env
env_manager.reset(config=new_config)
File “/home/joe/Documents/ml-agents/ml-agents/mlagents/trainers/env_manager.py”, line 68, in reset
self.first_step_infos = self._reset_env(config)
File “/home/joe/Documents/ml-agents/ml-agents/mlagents/trainers/subprocess_env_manager.py”, line 333, in _reset_env
ew.previous_step = EnvironmentStep(ew.recv().payload, ew.worker_id, {}, {})
File “/home/joe/Documents/ml-agents/ml-agents/mlagents/trainers/subprocess_env_manager.py”, line 98, in recv
raise env_exception
mlagents_envs.exception.UnityEnvironmentException: Environment shut down with return code 0.

Able to attach debugger by moving learn.py up a directory. Able to make it further using --no-graphics. Still can’t open multiple instances:

Aborting batchmode due to failure:
Fatal Error! It looks like another Unity instance is running with this project open.

Multiple Unity instances cannot open the same project.

Just to confirm my understanding. Does running the above command break even without trying to connect with PyCharm or does it break only when you try to connect with PyCharm?

Command breaks in general, e.g. this doesn’t work:

joe@xps:~/Documents/ml-agents$ python ml-agents/mlagents/trainers/learn.py config/rollerball_config.yaml --run-id=RollerBall3 --num-envs=2 --env=$UNITY_PATH --force

Traceback (most recent call last):
File “ml-agents/mlagents/trainers/learn.py”, line 2, in
from mlagents import torch_utils
File “/home/joe/Documents/ml-agents/ml-agents/mlagents/torch_utils/init.py”, line 1, in
from mlagents.torch_utils.torch import torch as torch # noqa
File “/home/joe/Documents/ml-agents/ml-agents/mlagents/torch_utils/torch.py”, line 39, in
torch.set_num_threads(cpu_utils.get_num_threads_to_use())
AttributeError: module ‘torch’ has no attribute ‘set_num_threads’

Moving learn.py up a directory works:

joe@xps:~/Documents/ml-agents$ python ml-agents/mlagents/learn.py config/rollerball_config.yaml --run-id=RollerBall3 --num-envs=2 --env=$UNITY_PATH --force

Moving learn.py to a new module (I called it “run”) works:

joe@xps:~/Documents/ml-agents$ python ml-agents/mlagents/run/learn.py config/rollerball_config.yaml --run-id=RollerBall3 --num-envs=2 --env=$UNITY_PATH --force

Probably best to isolate executables into their own python module, I’d be happy to make a pull request. Are breaking API changes are a concern?

Thanks, we have been aware of this issue but didn’t plan on supporting running these python files directly, we rely on using it as a module via python3 -m mlagents.trainers.learn.

Can you confirm that the command breaks when using the module as mentioned in Trying to use the learn.py script from the command line throws a "module 'torch' has no attribute 'set_num_threads'" error if PyTorch is not installed · Issue #4526 · Unity-Technologies/ml-agents · GitHub and in your earlier comment (-m mlagents.trainers.learn)?

When running locally I can confirm that when running “learn.py” I get the same error, but it does not occur when running with “python3 -m mlagents.trainers.learn” which is our supported use case.

If it does break please provide more info about your python installation (version and if you are using a virtual environment so I can try to reproduce).

If not then it is still likely worth considering moving this or adding an FAQ about it. I am sure that if there are already a couple of questions about this there are plenty of other people that are also confused.

python -m works fine but I’m not sure how to get my debugger to call the script this way

I got this working locally. You’ll need to set the “Script path” to the output of which mlagents-learn (which is a python script automatically set up during installation). Note that you may also want to specify “Working directory”

1 Like

Thanks, can confirm that this works with my anaconda environment

For windows users, I haven’t tried it, but the response here should also be applicable to running with PyCharm.