-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Big image quality improvement! Kahan summation for Adafactor-optimized Flux FFT #2159
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
Open
araleza
wants to merge
8
commits into
kohya-ss:sd3
Choose a base branch
from
araleza:sd3-kahan
base: sd3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6517b2b
Added support for Kahan summation for Adafactor-optimized Flux FFT
araleza da6416a
Restoring the deleted __main__ function and fixing a warning typo
araleza bb7750f
Fixed typo in comment
araleza acb4cf3
Fixed a warning typo, and changed --kahan-summation to --kahan_summation
araleza 3f0230a
Now sending int16s instead of f32s to cpu device; faster and maybe mo…
araleza 6489942
Added log output message to show that Kahan summation is being used
araleza cd239f0
Moved kahan state from file globals to optimizer state variables
araleza ac8ae58
Removed some no-effect lines used for a debug breakpoint
araleza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -28,6 +28,72 @@ def copy_stochastic_(target: torch.Tensor, source: torch.Tensor): | |||||||||||||||||||||||||
| del result | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # Kahan summation for bfloat16 | ||||||||||||||||||||||||||
| # The implementation was provided by araleza. | ||||||||||||||||||||||||||
| # Based on paper "Revisiting BFloat16 Training": https://arxiv.org/pdf/2010.06192 | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| kahan_residuals = [] | ||||||||||||||||||||||||||
| tensor_index = 0 | ||||||||||||||||||||||||||
| prev_step = 0 | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def copy_kahan_(target: torch.Tensor, source: torch.Tensor, step, update): | ||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||
| Copies source into target using Kahan summation. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| The lower bits of the float32 weight that are lost on conversion to bfloat16 | ||||||||||||||||||||||||||
| are sent to the CPU until the next step, where they are re-added onto the weights | ||||||||||||||||||||||||||
| before adding the gradient update. This produces near float32-like weight behavior, | ||||||||||||||||||||||||||
| although the copies back and forth to main memory result in slower training steps. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||
| target: the target tensor with dtype=bfloat16 | ||||||||||||||||||||||||||
| source: the target tensor with dtype=float32 | ||||||||||||||||||||||||||
|
Comment on lines
+36
to
+46
|
||||||||||||||||||||||||||
| """ | |
| Copies source into target using Kahan summation. | |
| The lower bits of the float32 weight that are lost on conversion to bfloat16 | |
| are sent to the CPU until the next step, where they are re-added onto the weights | |
| before adding the gradient update. This produces near float32-like weight behavior, | |
| although the copies back and forth to main memory result in slower training steps. | |
| Args: | |
| target: the target tensor with dtype=bfloat16 | |
| source: the target tensor with dtype=float32 | |
| source: the source tensor with dtype=float32 |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since step starts from 0, it would be better to set this to -1. The tensor_index of the first step starts from 1, which will cause a mismatch with the next step.