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: Batchnorm does not work properly when training on multiple devices #1167

Merged
merged 3 commits into from Jan 27, 2024

Conversation

wcshds
Copy link
Contributor

@wcshds wcshds commented Jan 23, 2024

It seems that running_var and runninng_mean are shared across devices when training on multiple devices. Therefore, ensuring that running_var/var and running_mean/mean are on the same device will allow Batchnorm to work properly during multiple devices training.

Comment on lines 139 to 145
let running_mean = running_mean.clone().mul_scalar(1.0 - self.momentum).add(
mean.clone()
.to_device(&running_mean.device())
.detach()
.mul_scalar(self.momentum)
.reshape([channels]),
);
Copy link
Member

Choose a reason for hiding this comment

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

I think we should do the opposite: change the device of the running_mean after detaching it so that the gradients are calculated on each device. When we store the newly updated running_mean, we should also check the devices. Do you think it would work?

Copy link
Member

@nathanielsimard nathanielsimard left a comment

Choose a reason for hiding this comment

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

LGTM

@nathanielsimard nathanielsimard merged commit 8686082 into tracel-ai:main Jan 27, 2024
12 checks passed
@wcshds wcshds deleted the batchnorm-multi-device branch January 27, 2024 16:00
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.

None yet

2 participants