Search Unity

Resolved Agent's OnEnable freezes the main thread for long time

Discussion in 'ML-Agents' started by vipnet1, Apr 2, 2022.

  1. vipnet1

    vipnet1

    Joined:
    Mar 9, 2020
    Posts:
    2
    Hi everyone!

    I have a trained brain model and have bots that are using it in an android game(mlagents 2.0.1, inference). Everything works fine but the moment I load the scene with the bots or enable the bots for first time the main thread is stuck, sometimes even for 5+ seconds. After that everything continues as it should.
    The problem is that on mobile devices this is often considered as ANR, and I need to reduce the loading time to avoid it. I made some investigation and it happens the moment we inherit from Agent class - even if it has nothing in it(empty class). The results of the profiler:

    profiler.png

    and indeed after I override OnEnable of Agent class the loading times are normal again. This freeze happens only the first time the scene with the bots is loaded - after that the loading times are much better.

    So the main question is how do I avoid this freeze? is there any option to reduce the time of agent's OnEnable or maybe to run this not on the main thread? Do you have the same problem with the agents or your loading times are better than mine?

    Thanks in advance
     
  2. vipnet1

    vipnet1

    Joined:
    Mar 9, 2020
    Posts:
    2
    In case someone else encounters this I had to modify the source code a bit.
    In Academy.cs class, function InitializeEnvironment there's an attempt to connect to the trainer, and only after that we run inference only. Just remove/comment the part that tries to connect to the trainer - this fixed the long loading time for me.
    Just remember if you are going to resume the training to restore the missing part :)

    *Note* could be great if the behavior type set to inference only not to attempt to connect to the trainer.