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
Describe the bug Using Release Version 7 with TensorFlow (default installation, PyTorch is not installed). When trying to run training using ml-agents/mlagents/trainers/learn.py script command. The script generates an error and fails to run. I am not trying to use PyTorch:
AttributeError: module 'torch' has no attribute 'set_num_threads'
I like to run training from the python scripts instead of the compiled binary. PyTorch is not installed because I am using TensorFlow. Also the installer does not install PyTorch. I understand it is optional.
To Reproduce
-
Default installation of ML-Agents Version 7
-
I like to run the training using the python source files rather than the compiled binary. This worked up until this version. To set this up:
-
Uninstall the pip packages for mlagents and mlagents-envs
-
Set up the PYTHON_PATH variable to point to the /ml-agents/ and /ml-agents-envs python script packages.
-
Try to run the python script from the command line:
python ml-agents\mlagents\trainers\learn.py --help
See the error below. Note that I am able to fix the issue by commenting out the attempt to load PyTorch but I thought the MLAgents team would like to know.
Console logs / stack traces
Traceback (most recent call last):
File "ml-agents\mlagents\trainers\learn.py", line 13, in <module>
from mlagents.trainers.trainer_controller import TrainerController
File "F:\Workspace\Unity\ShadowOfCollususControllerML-Agents\ml-agents\ml-agents\mlagents\trainers\trainer_controller.py", line 28, in <module>
from mlagents.trainers.trainer_util import TrainerFactory
File "F:\Workspace\Unity\ShadowOfCollususControllerML-Agents\ml-agents\ml-agents\mlagents\trainers\trainer_util.py", line 9, in <module>
from mlagents.trainers.ppo.trainer import PPOTrainer
File "F:\Workspace\Unity\ShadowOfCollususControllerML-Agents\ml-agents\ml-agents\mlagents\trainers\ppo\trainer.py", line 12, in <module>
from mlagents.trainers.trainer.rl_trainer import RLTrainer
File "F:\Workspace\Unity\ShadowOfCollususControllerML-Agents\ml-agents\ml-agents\mlagents\trainers\trainer\rl_trainer.py", line 29, in <module>
from mlagents import torch_utils
File "F:\Workspace\Unity\ShadowOfCollususControllerML-Agents\ml-agents\ml-agents\mlagents\torch_utils\__init__.py", line 1, in <module>
from mlagents.torch_utils.torch import torch as torch # noqa
File "F:\Workspace\Unity\ShadowOfCollususControllerML-Agents\ml-agents\ml-agents\mlagents\torch_utils\torch.py", line 12, in <module>
torch.set_num_threads(cpu_utils.get_num_threads_to_use())
AttributeError: module 'torch' has no attribute 'set_num_threads'
Screenshots
Environment (please complete the following information):
- 2019.4.11
- Windows 10
- Version 7
- TensorFlow 2.3.1 (no PyTorch)
- Any environment, but this error happens without an envoronment. trying to run ".../learn.py --help" generates the error
NOTE: We are unable to help reproduce bugs with custom environments. Please attempt to reproduce your issue with one of the example environments, or provide a minimal patch to one of the environments needed to reproduce the issue.
Thanks for reporting this, it looks like python import resolutions shot us in the foot in the specific case where you run learn.py directly. Does using python -m mlagents.trainers.learn work for your use case? IIUC it should be a suitable replacement.
It is easy for me to work around. I just comment out the that generates the error in the torch.py file. After that learn.py is working great. Feels like version 7 is a bit faster than previous versions. Reported it just so you are aware of the issue.
Thanks for the report, we don't plan to support running learn.py directly going forward (running python -m mlagents.trainers.learn will be the preferred method if you need to run it directly). If you encounter any documentation that still refers to running it directly please let us know and we will be happy to change it so that others don't run into this going forward.
Hello. I tried to do the same thing, and found the reason. The thing is, in "mlagents/torch_utils/torch.py" torch means "mlagents/trainers/torch", you need to refactor rename "mlagents/trainers/torch" folder. Hope it helps :)
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.