-
Notifications
You must be signed in to change notification settings - Fork 146
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
Refactor the quantization modification logic #2233
Merged
Merged
Conversation
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
Satrat
reviewed
Apr 15, 2024
tests/sparseml/transformers/sparsification/modification/conftest.py
Outdated
Show resolved
Hide resolved
Satrat
reviewed
Apr 15, 2024
tests/sparseml/transformers/sparsification/modification/conftest.py
Outdated
Show resolved
Hide resolved
bfineran
previously approved these changes
Apr 15, 2024
dbogunowicz
changed the title
[Fix] Remove hidden issue in
Refactor the quantization modification logic
Apr 16, 2024
modification
repo that causes training slowdown
Satrat
reviewed
Apr 18, 2024
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.
LGTM, but two main thoughts: can we document this environment variable somewhere with its intended usage? And could we add a test script to this PR that demonstrates the speed issue being fixed?
Satrat
reviewed
Apr 29, 2024
tests/sparseml/transformers/sparsification/modification/test_modifying_llama.py
Show resolved
Hide resolved
bfineran
approved these changes
Apr 29, 2024
Satrat
approved these changes
Apr 29, 2024
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.
Feature Description
The
sparseml.transformers.sparsification.modification
package is a set of modifications that are applied to some of the transformer models, to make them compatible with our quantization flows.This PR moves the act of modification away from
SparseAutoModel
, to theQuantizationModifier
. This means we only modify the model when necessary - when we apply the quantization structure. This helps us to avoid clashes with the new behavior intransformers
, where the models by default get initialized withSDPAttention
.Notable changes
modification
logic tosparseml.modifiers.quantization
. Keeping only thetransformers
-specific logic in the original directorySparseAutoModel
.To my best knowledge, the failing tests are orthogonal to the contents of this PR.
Legacy PR description
Keeping the original PR message (analysis of the problem, that forced me to go down the final path present in the PR) for posteriority, as it contains a lot of useful context:
As reported by @Satrat, after upgrading the transformers version we did not see the expected training speedups during e.g. sparse fine-tuning process. It turned out this was caused by the
modify_model(...)
function during the initialization of theSparseAutoModelForCausalLM
.Let's explain what was happening using
LLaMa
as an example.The model as of
transformers==4.39.1
can be initialized with three types of attention:LlamaSdpaAttention
- the default in the current transformers version; it uses the CUDA optimizedtorch.nn.functional.scaled_dot_product_attention
for quicker computation of attentionLlamaAttention
- the previous default before the transformers upgrade; this is the attention type that usestorch.matmul
method and thus is being modified by us through themodify_model(...)
method.LlamaFlashAttention
- irrelevant in the context of this write-up.The current, erroneous behavior, was the following:
isinstance()
method, we were overriding this attention module'sforward
method, effectively replacing SDPA-attention'sforward
method with the original attention class'forward
method.This PR hardens the modification logic - it uses a more restrictive
type()
instead ofisinstance()
to pick the correct attention type to modify. Now there is no difference in iterations per second when sparse fine tuning with or without themodify_model(...)
function.