stable-baselines icon indicating copy to clipboard operation
stable-baselines copied to clipboard

SubprocVecEnv and large shared data between processes

Open manuel-armbruster opened this issue 7 years ago • 3 comments

I hope this is the right place to ask. I am using A2C with custom envs over SubprocVecEnv with >32 n_cpu. As far as I understand, n_cpu environments are instantiated after calling model.learn(...). In my custom environments a gather information on next_observation by reading data from a dataframe which I fetch into memory by pandas.read_csv(...). The corresponding csv file is large (>4GB) with millions of rows, so in order to avoid loading this file in memory for each environment I wanted to load it once before the model.learn(...) call and share the dataframe over all parallel running environments. Is there a way to prevent each environment of copying the dataframe into its memory and instead using one shared dataframe reference?

manuel-armbruster avatar Aug 26 '19 15:08 manuel-armbruster

Hello,

As far as I understand, n_cpu environments are instantiated after calling model.learn(...).

The environments are instantiated when the constructor of SubprocVecEnv is called (cf code)

Is there a way to prevent each environment of copying the dataframe into its memory and instead using one shared dataframe reference?

You can try the DummyVecEnv at first, with it, you can share references. It is usually (and unfortunately) competitive if not faster than the SubprocVecEnv. Otherwise, because of the way python subprocesses work (it copies memory), you will need to create a custom VecEnv (either using threads (but I don't recommend that with python) or sharing memory between processes (but I never had that issue before, so I don't know how to do it...)).

araffin avatar Aug 26 '19 15:08 araffin

Thanks for the quick reply :) Before I start implementing an own VecEnv I would prefer trying out DummyVecEnv again. But as far as I understood, with DummyVecEnv all environments will be played sequentially. So this should take longer than the asynchronous approach... But you mentioned this is not the case. Can you explain shortly why this is the case, please? Regarding the shared reference: If I use DummyVecEnv all envs will hold one reference to my large object and after each env finished it will be closed and so I will have only one of this large object in memory, right? Thanks for your time!!

manuel-armbruster avatar Aug 27 '19 08:08 manuel-armbruster

Can you explain shortly why this is the case, please?

I've already answered the question here ;): https://github.com/hill-a/stable-baselines/issues/322#issuecomment-492202915

I will have only one of this large object in memory, right?

yes, that's the idea of sharing a reference to an object.

araffin avatar Aug 27 '19 09:08 araffin