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

UnityEnvironment Support For WebGL Builds

Open cm107 opened this issue 3 years ago • 3 comments

Is your feature request related to a problem? Please describe. Right now it is possible to do a WebGL build of a scene that contains agents referencing trained NN Model. This is an example that I created on huggingface. The problem is that ml-agents doesn't support GPU inference for WebGL builds. Furthermore, it seems that WebGL doesn't support multithreading. Consequently, the resulting simulation ends up being way too slow in a WebGL build, even if it was running fast in a Linux build. Yes, I have tried profiling the WebGL build. I think it was like 99% of the processing time was spent on performing inference.

Thus, I am forced to look for an alternative.

The idea that I came up with is this:

  1. Run the Unity environment in the browser (client) with all of the NN Model references removed.
  2. Perform GPU inference on the server and send the resulting action back to the client.

In order to do this, I need to enable communication between the Unity environment and the server. I already know that this can easily be done with Windows, Mac, Linux builds, since that generates an executable. UnityEnvironment works perfectly fine as long as you specify the file_name parameter.

env = UnityEnvironment(file_name='path/to/build.x86_64')

However, I can't do this with WebGL builds, since there is no executable. I have tried the following:

env = UnityEnvironment(port=SERVER_PORT)
But this just throws an error.
Traceback (most recent call last):
  File "/home/clayton/workspace/git/ml-agents/ml-agents-envs/mlagents_envs/rpc_communicator.py", line 87, in check_port
    s.bind(("localhost", port))
OSError: [Errno 98] Address already in use

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/clayton/Unity/Projects/excav_simul_3d/interface/test/webgl/control.py", line 4, in <module>
    env = UnityEnvironment(
  File "/home/clayton/workspace/git/ml-agents/ml-agents-envs/mlagents_envs/environment.py", line 189, in __init__
    self._communicator = self._get_communicator(worker_id, base_port, timeout_wait)
  File "/home/clayton/workspace/git/ml-agents/ml-agents-envs/mlagents_envs/environment.py", line 267, in _get_communicator
    return RpcCommunicator(worker_id, base_port, timeout_wait)
  File "/home/clayton/workspace/git/ml-agents/ml-agents-envs/mlagents_envs/rpc_communicator.py", line 51, in __init__
    self.create_server()
  File "/home/clayton/workspace/git/ml-agents/ml-agents-envs/mlagents_envs/rpc_communicator.py", line 57, in create_server
    self.check_port(self.port)
  File "/home/clayton/workspace/git/ml-agents/ml-agents-envs/mlagents_envs/rpc_communicator.py", line 89, in check_port
    raise UnityWorkerInUseException(self.worker_id)
mlagents_envs.exception.UnityWorkerInUseException: Couldn't start socket communication because worker number 0 is still in use. You may need to manually close a previously opened environment or use a different worker number.
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/home/clayton/workspace/git/ml-agents/ml-agents-envs/mlagents_envs/environment.py", line 439, in _close
    self._communicator.close()
AttributeError: 'UnityEnvironment' object has no attribute '_communicator'

Upon further investigation, it seems that the Academy's ReadPortFromArgs() will return -1 no matter what unless you are either using an executable or using the Unity Editor. With the current implementation, there doesn't seem to be any way to interface with a WebGL build.

Describe the solution you'd like Please create a way for Academy and UnityEnvironment to communicate with file_name=None, port=SERVER_PORT (for WebGL builds). It looks like this will require changes to both Academy.cs and environment.py.

Describe alternatives you've considered Alternatively, you could implement a way to convert saved models to tensorflow so that tensorflow.js could be used. This was suggested by #2991. It sounds like a great idea to me, so I'm not sure why it was rejected. That way I might be able to run inference on the client-side and wouldn't have to resort to running it on the server side.

Additional context Please help.

cm107 avatar Dec 22 '22 04:12 cm107

I've the same issue as you, I did update the to Burst 3.0 , I was able to run the build on Webgl using Burst as interference device.

Yet, the amount of GC that is produced by burrcuda is too much and its the main reason about FPS spikes.

From my side, I cut as much as anything that uses CPU, and was able to make it run 30-40 FPS on 1080P / intel i5 with UHD 630 GPU mini mac, for 4Vs4 in reach visual environment , but what is annoying is the spikes. otherwise, on higher device like macbook pro, was able to achieve 60FPS 4K on webgl, but still there is spikes that drop fps few frames from time to time.

My last profiling, I found that burrcuda uses 60% of main thread with tons of GC and render thread is waiting for main thread to finish GC which is reason why the spikes happens.

I hope they really consider resolving this, otherwise, on phone , run 60fps stable on mini iphone SE

OmarVector avatar Feb 24 '23 01:02 OmarVector

@miguelalonsojr May I ask since its closed and marked as complete, does it really resolved and improved the webgl inference speed? ... like now we can use GPU on webgl ?

OmarVector avatar Oct 09 '23 05:10 OmarVector

We are upgrading to Sentis and will have an official release soon. If the problem persists after the next release, please open a new issue.

miguelalonsojr avatar Oct 09 '23 11:10 miguelalonsojr