-
Notifications
You must be signed in to change notification settings - Fork 193
Add a generic wrap_hf_model_class
utility to support VLMs
#185
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
af76f6a
[WIP] Add a generic `create_sparse_auto_model_class` utility
mgoin bd0b84d
Update examples
mgoin 5fb9d85
Fix llava example
mgoin 87f57c4
Merge branch 'main' into generic-sparse-auto-model
mgoin b4cb673
Add Llama 3.2 vision example
mgoin 7555560
Update interface to wrap_hf_model_class
mgoin f900137
Quality
mgoin a960451
Update src/llmcompressor/transformers/sparsification/sparse_model.py
mgoin 6594c8c
Address comments
mgoin 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from transformers import AutoProcessor, MllamaForConditionalGeneration | ||
|
||
from llmcompressor.modifiers.quantization import QuantizationModifier | ||
from llmcompressor.transformers import oneshot, wrap_hf_model_class | ||
|
||
MODEL_ID = "meta-llama/Llama-3.2-11B-Vision-Instruct" | ||
|
||
# Load model. | ||
model_class = wrap_hf_model_class(MllamaForConditionalGeneration) | ||
model = model_class.from_pretrained(MODEL_ID, device_map="auto", torch_dtype="auto") | ||
processor = AutoProcessor.from_pretrained(MODEL_ID) | ||
|
||
# Configure the quantization algorithm and scheme. | ||
# In this case, we: | ||
# * quantize the weights to fp8 with per channel via ptq | ||
# * quantize the activations to fp8 with dynamic per token | ||
recipe = QuantizationModifier( | ||
targets="Linear", | ||
scheme="FP8_DYNAMIC", | ||
ignore=["re:.*lm_head", "re:multi_modal_projector.*", "re:vision_model.*"], | ||
) | ||
|
||
# Apply quantization and save to disk in compressed-tensors format. | ||
SAVE_DIR = MODEL_ID.split("/")[1] + "-FP8-Dynamic" | ||
oneshot(model=model, recipe=recipe, output_dir=SAVE_DIR) | ||
processor.save_pretrained(SAVE_DIR) | ||
|
||
# Confirm generations of the quantized model look sane. | ||
print("========== SAMPLE GENERATION ==============") | ||
input_ids = processor(text="Hello my name is", return_tensors="pt").input_ids.to("cuda") | ||
output = model.generate(input_ids, max_new_tokens=20) | ||
print(processor.decode(output[0])) | ||
print("==========================================") |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from transformers import AutoProcessor, LlavaForConditionalGeneration | ||
|
||
from llmcompressor.modifiers.quantization import QuantizationModifier | ||
from llmcompressor.transformers import oneshot, wrap_hf_model_class | ||
|
||
MODEL_ID = "llava-hf/llava-1.5-7b-hf" | ||
|
||
# Load model. | ||
model_class = wrap_hf_model_class(LlavaForConditionalGeneration) | ||
model = model_class.from_pretrained(MODEL_ID, device_map="auto", torch_dtype="auto") | ||
processor = AutoProcessor.from_pretrained(MODEL_ID) | ||
|
||
# Configure the quantization algorithm and scheme. | ||
# In this case, we: | ||
# * quantize the weights to fp8 with per channel via ptq | ||
# * quantize the activations to fp8 with dynamic per token | ||
recipe = QuantizationModifier( | ||
targets="Linear", | ||
scheme="FP8_DYNAMIC", | ||
ignore=["re:.*lm_head", "re:multi_modal_projector.*", "re:vision_tower.*"], | ||
) | ||
|
||
# Apply quantization and save to disk in compressed-tensors format. | ||
SAVE_DIR = MODEL_ID.split("/")[1] + "-FP8-Dynamic" | ||
oneshot(model=model, recipe=recipe, output_dir=SAVE_DIR) | ||
processor.save_pretrained(SAVE_DIR) | ||
|
||
# Confirm generations of the quantized model look sane. | ||
print("========== SAMPLE GENERATION ==============") | ||
input_ids = processor(text="Hello my name is", return_tensors="pt").input_ids.to("cuda") | ||
output = model.generate(input_ids, max_new_tokens=20) | ||
print(processor.decode(output[0])) | ||
print("==========================================") |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from transformers import AutoProcessor, Qwen2VLForConditionalGeneration | ||
|
||
from llmcompressor.modifiers.quantization import QuantizationModifier | ||
from llmcompressor.transformers import oneshot, wrap_hf_model_class | ||
|
||
MODEL_ID = "Qwen/Qwen2-VL-7B-Instruct" | ||
|
||
# Load model. | ||
model_class = wrap_hf_model_class(Qwen2VLForConditionalGeneration) | ||
model = model_class.from_pretrained(MODEL_ID, device_map="auto", torch_dtype="auto") | ||
processor = AutoProcessor.from_pretrained(MODEL_ID) | ||
|
||
# Configure the quantization algorithm and scheme. | ||
# In this case, we: | ||
# * quantize the weights to fp8 with per channel via ptq | ||
# * quantize the activations to fp8 with dynamic per token | ||
recipe = QuantizationModifier( | ||
targets="Linear", | ||
scheme="FP8_DYNAMIC", | ||
ignore=["re:.*lm_head", "re:visual.*"], | ||
) | ||
|
||
# Apply quantization and save to disk in compressed-tensors format. | ||
SAVE_DIR = MODEL_ID.split("/")[1] + "-FP8-Dynamic" | ||
oneshot(model=model, recipe=recipe, output_dir=SAVE_DIR) | ||
processor.save_pretrained(SAVE_DIR) | ||
|
||
# Confirm generations of the quantized model look sane. | ||
print("========== SAMPLE GENERATION ==============") | ||
input_ids = processor(text="Hello my name is", return_tensors="pt").input_ids.to("cuda") | ||
output = model.generate(input_ids, max_new_tokens=20) | ||
print(processor.decode(output[0])) | ||
print("==========================================") |
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
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.
Uh oh!
There was an error while loading. Please reload this page.