Skip to content

Fix a bug of unintentionally using same process indices#455

Merged
toslunar merged 2 commits intochainer:masterfrom
muupan:fix-same-process-idx
May 9, 2019
Merged

Fix a bug of unintentionally using same process indices#455
toslunar merged 2 commits intochainer:masterfrom
muupan:fix-same-process-idx

Conversation

@muupan
Copy link
Copy Markdown
Member

@muupan muupan commented May 8, 2019

I noticed some of batch training examples have a bug that unintentionally uses same process indices (thus same random seeds!) across env processes. This PR fixes the bug.

You can see the current and new behaviors by running the code below.

import functools
import chainerrl
import gym

num_envs = 4


def make_env(process_idx, test):
    print(process_idx, test)
    return gym.make('Pendulum-v0')


def make_batch_env_old(test):
    return chainerrl.envs.MultiprocessVectorEnv(
        [(lambda: make_env(idx, test))
         for idx, env in enumerate(range(num_envs))])


def make_batch_env_new(test):
    return chainerrl.envs.MultiprocessVectorEnv(
        [functools.partial(make_env, idx, test)
         for idx, env in enumerate(range(num_envs))])


print('make_batch_env_old')
make_batch_env_old(test=False)
make_batch_env_old(test=True)

print('make_batch_env_new')
make_batch_env_new(test=False)
make_batch_env_new(test=True)

This code will outputs:

make_batch_env_old
3 False
3 False
3 False
3 False
3 True
3 True
3 True
3 True
make_batch_env_new
0 False
1 False
2 False
3 False
0 True
1 True
2 True
3 True

Even when same random seeds are used in env processes, actions sent by the agent are usually different due to the stochasticity of policy or eplorer, so this may not result in noticeable difference in learning results, but this is definitely a bug that needs to be fixed.

@muupan muupan added the bug label May 8, 2019
Copy link
Copy Markdown
Member

@toslunar toslunar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Copy Markdown
Member

@toslunar toslunar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you fix tests with the same kind of bugs?

# Wrap by FrameStack and MultiprocessVectorEnv
fs_env = chainerrl.envs.MultiprocessVectorEnv(
[(lambda: FrameStack(
make_env(idx), k=self.k, channel_order='chw'))
for idx, env in enumerate(range(self.num_envs))])
# Wrap by MultiprocessVectorEnv and VectorFrameStack
vfs_env = VectorFrameStack(
chainerrl.envs.MultiprocessVectorEnv(
[(lambda: make_env(idx))
for idx, env in enumerate(range(self.num_envs))]),
k=self.k, stack_axis=0)

@muupan
Copy link
Copy Markdown
Member Author

muupan commented May 9, 2019

Thanks for the review. I fixed tests/wrappers_tests/test_vector_frame_stack.py as well.

@toslunar toslunar merged commit 940ae01 into chainer:master May 9, 2019
@muupan muupan deleted the fix-same-process-idx branch May 9, 2019 04:06
@muupan muupan added this to the v0.7 milestone Jun 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants