ml-agents icon indicating copy to clipboard operation
ml-agents copied to clipboard

Parallel Simulation

Open donamin opened this issue 8 years ago • 12 comments

Hi Thanks for the great toolkit! I am currently working on developing physical agents for sports games. One thing that is very crucial in this field is the ability of multi-threaded simulation since a lot of algorithms are sampling-based. Is it possible in this toolkit to have multi-threaded simulation? If not, do you have any plan on adding this feature in the future? BTW, I hear that Mujoco physics engine authors are working on developing some interface for integrating Mujoco with Unity, and it's going to be released pretty soon. Having multi-threaded simulation on top of this can be really great.

donamin avatar Dec 26 '17 09:12 donamin

Hi @donamin,

Thanks for your question. You are right that having an efficient multithreaded solution will be necessary for the highest performance. The 2018 version of Unity will be released with a new multi-threading job system, and we plan to eventually have ML-Agents use it. Currently the options for parallelization involve having multiple instances of your environment within a single unity scene and/or launching multiple instances of the Unity binary and collecting data from all of them simultaneously.

The Mujoco integration is something we are following ourselves, and are excited to see it as part of Unity too!

awjuliani avatar Dec 26 '17 16:12 awjuliani

@awjuliani Could I ask you to explain how to set up using multiple instances of the envirornment? I have a game I'm training where it can't have several copies in one scene, but if I could use several instances of the game that'd be helpful!

MDReptile avatar Dec 31 '17 17:12 MDReptile

@MDReptile did you get this working?

Lakrix avatar Feb 12 '18 16:02 Lakrix

@Lakrix Unfortunately no

MDReptile avatar Feb 12 '18 20:02 MDReptile

Currently the options for parallelization involve having multiple instances of your environment within a single unity scene @awjuliani

What are the steps for this? Is it simply creating multiple copies of the Academy + Brain + Agent(s) in a single scene or is there more to it?

Also I'm curious if you have any tips for general performance+speed gains during training, I'm currently trying to figure out what the performance bottlenecks are for ML Agents as I'm only able to utilize 5-15% of my CPU/GPU during training

sterlingcrispin avatar Mar 20 '18 17:03 sterlingcrispin

You can look at the 3DBall example in the SDK for this. You only need one instance of the Academy and Brain. Make copies of the Agents (and the other environment objects) and have them all use the same Brain.

jo3w4rd avatar Mar 20 '18 18:03 jo3w4rd

@jo3w4rd is there a formula for determining the upper limit of the number of instances of agents that would be useful to have in the environment?

For example, how can I tell if 10, or 100, or 1000 agents is enough / too much

sterlingcrispin avatar Mar 20 '18 19:03 sterlingcrispin

I'll have to let @awjuliani answer the aspect of your question on what the optimal number of agents is for the machine learning algorithms.

On the Unity side, you won't be able to get 1000 agents to work in the current code because it takes too long to spin-up that many agents -- there's a bottleneck in the initialization code that isn't hard to fix, though. After removing that bottleneck, I am able to run a training session of the Balance Ball example with 1000 agents and it utilizes about 150% of my dual-core laptop.

jo3w4rd avatar Mar 21 '18 01:03 jo3w4rd

@jo3w4rd whats the bottleneck in the init code? I'd really like to utilize more of my PC

It seems like there's also a bottleneck between the communication speed between tensorflow code and the unity simulation during training

I asked Arthur and he said

Between 8 and 64 simultaneous agents would be recommended, You would only want to provide information that the network could actually utilize for making decisions. The 64 limit is around trading-off data throughput: data points per step of simulation vs simulation steps per second.

sterlingcrispin avatar Mar 21 '18 02:03 sterlingcrispin

There is a call to FindObjectOfType() to get a reference to the Academy in the Agent.OnEnable() function. This function is kind of slow and as a result it takes about 8 seconds to enable 1000 agents. The training process times out in less than 8 seconds. I fixed it temporarily by making the Academy a singleton and adding a static GetInstance() function, but I doubt that's the best long term fix.

And the training is very slow with the default hyperparameters, I guess since the data points per simulation is so much higher with that many agents.

jo3w4rd avatar Mar 21 '18 03:03 jo3w4rd

Hi @donamin , thank you for the discussion! We are closing this issue due to inactivity. Feel free to reopen it if you’d like to continue to discussion though.

vladimiroster avatar Mar 28 '18 21:03 vladimiroster

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.

lock[bot] avatar Jan 03 '20 14:01 lock[bot]