-
Notifications
You must be signed in to change notification settings - Fork 475
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
Llama Vision PEFT #1937
Open
pbontrager
wants to merge
7
commits into
pytorch:main
Choose a base branch
from
pbontrager:vision_peft
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.
Open
Llama Vision PEFT #1937
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
dcc92cc
initial version
pbontrager 481aaa3
Merge branch 'main' into vision_peft
pbontrager 1fe2825
separate peft function
pbontrager 6be3747
testing broken checkpoints
pbontrager 83820cf
Merge branch 'main' into vision_peft
pbontrager 861a68a
remove test
pbontrager 8d960b5
remove comment
pbontrager 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
# TODO: (philip) remove after tests | ||
|
||
from transformers import AutoModelForCausalLM # , AutoTokenizer | ||
|
||
model_id = "meta-llama/Llama-3.2-11B-Vision" | ||
peft_model_id = "/tmp/Llama-3.2-11B-Vision-Instruct/" | ||
|
||
model = AutoModelForCausalLM.from_pretrained(model_id) | ||
model.load_adapter(peft_model_id) |
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 |
---|---|---|
|
@@ -635,6 +635,7 @@ def save_checkpoint( | |
) | ||
|
||
if training.ADAPTER_KEY in state_dict: | ||
# import pdb; pdb.set_trace() | ||
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. remove |
||
# Save torchtune format adapter weights even if we save PEFT format | ||
# This way we can resume no matter what (and memory footprint of adapter weights is small) | ||
output_path = Path.joinpath( | ||
|
@@ -651,20 +652,38 @@ def save_checkpoint( | |
logger.warning( | ||
"Saving Phi-3 Mini adapter weights to PEFT format is not supported, saving to torchtune format instead" | ||
) | ||
elif self._model_type == ModelType.LLAMA3_VISION: | ||
elif self._model_type == ModelType.QWEN2: | ||
logger.warning( | ||
"Saving Llama3.2 Vision adapter weights to PEFT format is not supported, saving to torchtune format instead" | ||
"Saving QWEN2 adapter weights to PEFT format is not supported, saving to torchtune format instead" | ||
) | ||
else: | ||
state_dict[ | ||
training.ADAPTER_KEY | ||
] = convert_weights.tune_to_peft_adapter_weights( | ||
state_dict[training.ADAPTER_KEY], | ||
num_heads=self._config["num_attention_heads"], | ||
num_kv_heads=self._config["num_key_value_heads"], | ||
dim=self._config["hidden_size"], | ||
head_dim=self._config.get("head_dim", None), | ||
) | ||
if self._model_type == ModelType.LLAMA3_VISION: | ||
from torchtune.models.llama3_2_vision._convert_weights import ( | ||
llama3_vision_tune_to_peft_adapter_weights, | ||
) | ||
|
||
state_dict[ | ||
training.ADAPTER_KEY | ||
] = llama3_vision_tune_to_peft_adapter_weights( | ||
state_dict[training.ADAPTER_KEY], | ||
num_heads=text_config["num_attention_heads"], | ||
num_kv_heads=text_config["num_key_value_heads"], | ||
dim=text_config["hidden_size"], | ||
head_dim=text_config.get("head_dim", None), | ||
cross_attention_layers=text_config.get( | ||
"cross_attention_layers", None | ||
), | ||
) | ||
else: | ||
state_dict[ | ||
training.ADAPTER_KEY | ||
] = convert_weights.tune_to_peft_adapter_weights( | ||
state_dict[training.ADAPTER_KEY], | ||
num_heads=self._config["num_attention_heads"], | ||
num_kv_heads=self._config["num_key_value_heads"], | ||
dim=self._config["hidden_size"], | ||
head_dim=self._config.get("head_dim", None), | ||
) | ||
peft_output_path = Path.joinpath( | ||
self._output_dir, "adapter_model" | ||
).with_suffix(".bin") | ||
|
@@ -680,28 +699,19 @@ def save_checkpoint( | |
) | ||
|
||
if training.ADAPTER_CONFIG in state_dict: | ||
if self._model_type == ModelType.PHI3_MINI: | ||
logger.warning( | ||
"PEFT integration for Phi-3 Mini is not supported, skipping adapter config save" | ||
) | ||
elif self._model_type == ModelType.LLAMA3_VISION: | ||
logger.warning( | ||
"PEFT integration for Llama3.2 Vision is not supported, skipping adapter config save" | ||
) | ||
else: | ||
state_dict[ | ||
training.ADAPTER_CONFIG | ||
] = convert_weights.tune_to_peft_adapter_config( | ||
state_dict[training.ADAPTER_CONFIG] | ||
) | ||
output_path = Path.joinpath(self._output_dir, "adapter_config.json") | ||
with open(output_path, "w") as f: | ||
json.dump(state_dict[training.ADAPTER_CONFIG], f) | ||
logger.info( | ||
"Adapter checkpoint of size " | ||
f"{os.path.getsize(output_path) / 1000**3:.2f} GB " | ||
f"saved to {output_path}" | ||
) | ||
state_dict[ | ||
training.ADAPTER_CONFIG | ||
] = convert_weights.tune_to_peft_adapter_config( | ||
state_dict[training.ADAPTER_CONFIG] | ||
) | ||
output_path = Path.joinpath(self._output_dir, "adapter_config.json") | ||
with open(output_path, "w") as f: | ||
json.dump(state_dict[training.ADAPTER_CONFIG], f) | ||
logger.info( | ||
"Adapter checkpoint of size " | ||
f"{os.path.getsize(output_path) / 1000**3:.2f} GB " | ||
f"saved to {output_path}" | ||
) | ||
|
||
# If the recipe state needs to be output, first remove the model state dict | ||
# and if it exists, remove the adapter state dict as well | ||
|
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.
remove