Views
No views yet
1from transformers import AutoProcessor, AutoModelForCausalLM
2
3from llmcompressor.modifiers.quantization import QuantizationModifier
4from llmcompressor.transformers import oneshot, wrap_hf_model_class
5
6MODEL_ID = "microsoft/Phi-3.5-vision-instruct"
7
8# Load model.
9model_class = wrap_hf_model_class(AutoModelForCausalLM)
10model = model_class.from_pretrained(MODEL_ID, device_map="auto", torch_dtype="auto", trust_remote_code=True, _attn_implementation="eager")
11processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)
12
13# Configure the quantization algorithm and scheme.
14# In this case, we:
15# * quantize the weights to fp8 with per channel via ptq
16# * quantize the activations to fp8 with dynamic per token
17recipe = QuantizationModifier(
18 targets="Linear",
19 scheme="FP8_DYNAMIC",
20 ignore=["re:.*lm_head", "re:model.vision_embed_tokens.*"],
21)
22
23# Apply quantization and save to disk in compressed-tensors format.
24SAVE_DIR = MODEL_ID.split("/")[1] + "-FP8-Dynamic"
25oneshot(model=model, recipe=recipe, output_dir=SAVE_DIR)
26processor.save_pretrained(SAVE_DIR)