Fix/Improve: Batch paired inputs to remove BatchNorm order dependence and speed up training - #33
Open
Kaminyou wants to merge 1 commit into
Open
Fix/Improve: Batch paired inputs to remove BatchNorm order dependence and speed up training#33Kaminyou wants to merge 1 commit into
Kaminyou wants to merge 1 commit into
Conversation
Concatenate x1/x2 to remove order-dependent running-stat updates and reduce per-iteration runtime by approximately 15%.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The paired
x1/x2andnet_input1/net_input2tensors should be concatenated along the batch dimension and each pair should be processed in a shared forward pass.This change:
Motivation
Previously, paired inputs were processed sequentially:
In training mode, x1 and x2 therefore used independently computed BatchNorm statistics. Their running statistics were also updated sequentially.
With the default BatchNorm momentum of 0.1:
r1 = 0.9 * r0 + 0.1 * statistic_x1
r2 = 0.9 * r1 + 0.1 * statistic_x2
= 0.81 * r0 + 0.09 * statistic_x1 + 0.10 * statistic_x2
Consequently, the second input contributes 10% to the final running statistic, while the first contributes only 9%. Reversing the input order therefore produces different running means and variances.
Because x1 and x2 are paired even/odd observations of the same underlying volume, this order-dependent behavior is undesirable.
Changes
The paired inputs are now concatenated and processed together:
The same change is applied to
net_input1andnet_input2. This gives both halves the same BatchNorm statistics, removes ordering bias, and improves GPU utilization.