-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Add bnb.nn.StableEmbedding for quantized training #1770
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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 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 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
Oops, something went wrong.
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.
If it doesn't cause any issues, later it should be moved to PTL/Fabric's
BitsandbytesPrecision
plugin.In addition, it should be better from a memory usage perspective.
Now the model is materialized on the device, embeddings aren't quantized (only Linear layers are) and only after embeddings weights are replaced with quantized once.
If the code is in the PTL code, then embeddings will be quantized during materialization.
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.
Nice! Agreed
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.
@Andrei-Aksionov @lantiga I think the idea is not to quantize StableEmbedding during training due to precision issues, but I am not an expert in BitsandBytes tbh.
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.
But I think I maybe understand what you are saying: the modified model will be more inefficient during inference because quantization will not be applied to the StableEmbedding layer after training?
In this case, perhaps we should convert the
StableEmbedding
layer back into the regularEmbedding
layer before saving the model at the end of training. Let me investigate what impact that has on performance during inference.Regarding integration into PTL/Fabric, that's a good point, but I am a bit concerned if we can/should make model architecture modifications like that?
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.
No, it was my mistake 😄.
I thought that it's a way to quantize embeddings 🙈.
But it still might be beneficial to upstream the code.
Right now, the model is initialized on a meta device and materialized on a target device via Fabric.
After the model is materialized the code creates StableEmbeddings and after replaces weights of the regular embeddings. So, at the same moment in time, we have two sets of weights: regular embeddings and stable ones.
If we move the code to Fabric, the replacement will happen during materialization.
Or you can try to replace the layer while the model is on meta device.
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.
Good point. We could/should explicitly delete and garbage-collect the redundant original weight embedding after initializing the StableEmbedding. Sure, it would be even more efficient to never have 2 sets of weight at the same time during this weight-swapping point, but I don't think this will create a memory bottleneck there because the forward and backward passes in the training afterwards will likely be much higher than having temporarily a second embedding layer in memory.
The one concern is though if we move the code to Fabric, it will apply the Fabric precision settings to StableEmbedding, but if I understand correctly, this is a special layer that should retain float32 bit precision. So we would have to make a special exception for that layer in Fabric.
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.
Correct. It's in a category of "nice to have".
If we move it to Fabric, it will just mean that whenever you apply BnB prevision plugin, this type of embedding, with the correct precision, will be used. Moving the code to Fabric doesn't require embeddings to be quantized :)
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.
Sounds good! And it might also be a good first Fabric contribution for me to familiarize myself with the code base.