Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to attach debugger

Discussion in 'ML-Agents' started by jtatusko, Mar 2, 2021.

  1. jtatusko

    jtatusko

    Joined:
    Mar 2, 2021
    Posts:
    10
    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: https://github.com/Unity-Technologies/ml-agents/issues/4526. 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 <module>
    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.
     
  2. jtatusko

    jtatusko

    Joined:
    Mar 2, 2021
    Posts:
    10
    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.
     
  3. henrypeteet

    henrypeteet

    Unity Technologies

    Joined:
    Aug 19, 2020
    Posts:
    37
    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?
     
  4. jtatusko

    jtatusko

    Joined:
    Mar 2, 2021
    Posts:
    10
    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 <module>
    from mlagents import torch_utils
    File "/home/joe/Documents/ml-agents/ml-agents/mlagents/torch_utils/__init__.py", line 1, in <module>
    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 <module>
    torch.set_num_threads(cpu_utils.get_num_threads_to_use())
    AttributeError: module 'torch' has no attribute 'set_num_threads'
     
  5. jtatusko

    jtatusko

    Joined:
    Mar 2, 2021
    Posts:
    10
    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?
     
  6. henrypeteet

    henrypeteet

    Unity Technologies

    Joined:
    Aug 19, 2020
    Posts:
    37
    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 https://github.com/Unity-Technologies/ml-agents/issues/4526#issuecomment-702322576 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.
     
  7. jtatusko

    jtatusko

    Joined:
    Mar 2, 2021
    Posts:
    10
    python -m works fine but I'm not sure how to get my debugger to call the script this way
     
  8. celion_unity

    celion_unity

    Unity Technologies

    Joined:
    Jun 12, 2019
    Posts:
    289
    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"

    upload_2021-3-3_14-51-11.png
     
    jtatusko likes this.
  9. jtatusko

    jtatusko

    Joined:
    Mar 2, 2021
    Posts:
    10
    Thanks, can confirm that this works with my anaconda environment
     
  10. celion_unity

    celion_unity

    Unity Technologies

    Joined:
    Jun 12, 2019
    Posts:
    289
    For windows users, I haven't tried it, but the response here should also be applicable to running with PyCharm.