Skip to content

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 9 commits into from
Sep 27, 2024

Conversation

mgoin
Copy link
Member

@mgoin mgoin commented Sep 18, 2024

An idea to allow a generic wrapper for HF model classes to work with llmcompressor. I am specifically interested in this so we can compressed VLMs that have custom model classes, rather than "Auto" classes

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"

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)

recipe = QuantizationModifier(
    targets="Linear",
    scheme="FP8_DYNAMIC",
    ignore=["re:.*lm_head", "re:multi_modal_projector.*", "re:vision_model.*"],
)

SAVE_DIR = MODEL_ID.split("/")[1] + "-FP8-Dynamic"
oneshot(model=model, recipe=recipe, output_dir=SAVE_DIR)
processor.save_pretrained(SAVE_DIR)

Copy link

👋 Hi! Thank you for contributing to llm-compressor. Please add the ready label when the PR is ready for review.

Copy link
Collaborator

@rahul-tuli rahul-tuli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love python!

@mgoin mgoin changed the title [WIP] Add a generic create_sparse_auto_model_class utility Add a generic wrap_hf_model_class utility to support VLMs Sep 25, 2024
@mgoin mgoin marked this pull request as ready for review September 25, 2024 21:27
@kylesayrs kylesayrs self-requested a review September 26, 2024 01:07
kylesayrs
kylesayrs previously approved these changes Sep 26, 2024
Copy link
Collaborator

@kylesayrs kylesayrs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice

Copy link
Collaborator

@rahul-tuli rahul-tuli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm!

@mgoin mgoin merged commit 4407f13 into main Sep 27, 2024
6 of 7 checks passed
@mgoin mgoin deleted the generic-sparse-auto-model branch September 27, 2024 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants