Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multi-gpu bug with central_value #191

Merged
merged 1 commit into from
Jul 26, 2022

Conversation

vwxyzjn
Copy link
Contributor

@vwxyzjn vwxyzjn commented Jul 21, 2022

This PR fixes multi-gpu bug w/ central_value.

Tested w/ torchrun --standalone --nnodes=1 --nproc_per_node=2 train.py multi_gpu=True task=Ant task=ShadowHandOpenAI_LSTM num_envs=2048 wandb_activate=True wandb_entity=costa-huang train.params.config.minibatch_size=8192

Which yields this run

image

if param.grad is not None:
all_grads_list.append(param.grad.view(-1))
all_grads = torch.cat(all_grads_list)
dist.all_reduce(all_grads, op=dist.ReduceOp.SUM)
Copy link
Owner

Choose a reason for hiding this comment

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

why do we need to work with gradients manually?
Are there any prallel optimizer like we had in horovod?

Copy link
Owner

Choose a reason for hiding this comment

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

I will merge it now but wanna look into the torch distributed module later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See vwxyzjn/cleanrl#162 (comment). Option 2 (implemented here) is faster than the pytorch recommended gradient averaging -

def average_gradients(model):
    size = float(dist.get_world_size())
    for param in model.parameters():
        dist.all_reduce(param.grad.data, op=dist.ReduceOp.SUM)
        param.grad.data /= size

Copy link
Collaborator

Choose a reason for hiding this comment

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

The trouble with cat is that for a bigger network, we will have OOM because it's concatenating all the grads on a single GPU i.e. we can only roughly train a model of size 2GB on 8 GPU V100 with 16GBs each.

@vwxyzjn vwxyzjn marked this pull request as ready for review July 23, 2022 14:04
@Denys88 Denys88 merged commit 9f9c321 into Denys88:master Jul 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants