-
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
[Draft] Avoid loading model weights before recipe application if any #2230
Draft
rahul-tuli
wants to merge
2
commits into
main
Choose a base branch
from
avoid-loading-weights-before-recipe-application
base: main
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.
Draft
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,13 +33,15 @@ | |
from sparseml.pytorch.model_load.helpers import ( | ||
apply_recipe_structure_to_model, | ||
log_model_load, | ||
reload_model_state, | ||
) | ||
from sparseml.transformers.compression.utils import ( | ||
get_safetensors_folder, | ||
infer_compressor_from_model_config, | ||
modify_save_pretrained, | ||
) | ||
from sparseml.transformers.sparsification.modification import modify_model | ||
from sparseml.transformers.sparsification.sparse_config import SparseAutoConfig | ||
from sparseml.transformers.utils.helpers import download_model_directory, resolve_recipe | ||
|
||
|
||
|
@@ -111,9 +113,12 @@ def skip(*args, **kwargs): | |
logger = logging.getLogger("transformers.modeling_utils") | ||
restore_log_level = logger.getEffectiveLevel() | ||
logger.setLevel(level=logging.ERROR) | ||
model = super(AutoModelForCausalLM, cls).from_pretrained( | ||
pretrained_model_name_or_path, *model_args, **kwargs | ||
) | ||
|
||
config = SparseAutoConfig.from_pretrained(pretrained_model_name_or_path) | ||
|
||
# instantiate model without loading weights | ||
model = super(AutoModelForCausalLM, cls).from_config(config) | ||
|
||
logger.setLevel(level=restore_log_level) | ||
model = modify_model(model) | ||
# override the PreTrainedModel instance with compression save function | ||
|
@@ -130,12 +135,27 @@ def skip(*args, **kwargs): | |
compressor.overwrite_weights(model_path=model_path, model=model) | ||
|
||
recipe = resolve_recipe(recipe=recipe, model_path=pretrained_model_name_or_path) | ||
|
||
# this must be done before recipe is applied | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. curious, why? how does this modify the state of the model? |
||
original_state_dict = model.state_dict() | ||
|
||
if recipe: | ||
apply_recipe_structure_to_model( | ||
model=model, | ||
model_path=pretrained_model_name_or_path, | ||
recipe_path=recipe, | ||
reload_weights=False, | ||
) | ||
|
||
# Load the model weights | ||
if reload_model_state( | ||
model, pretrained_model_name_or_path, original_state_dict, force_reload=True | ||
): | ||
_LOGGER.info( | ||
"Loaded model state after SparseML recipe structure modifications " | ||
f"from {pretrained_model_name_or_path}" | ||
) | ||
|
||
return model | ||
|
||
|
||
|
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.