Views
No views yet
1from transformers import AutoProcessor, LlavaOnevisionForConditionalGeneration
2
3from llmcompressor.modifiers.quantization import QuantizationModifier
4from llmcompressor.transformers import oneshot, wrap_hf_model_class
5
6MODEL_ID = "llava-hf/llava-onevision-qwen2-7b-ov-hf"
7
8# Load model.
9model_class = wrap_hf_model_class(LlavaOnevisionForConditionalGeneration)
10model = model_class.from_pretrained(MODEL_ID, device_map="auto", torch_dtype="auto")
11processor = AutoProcessor.from_pretrained(MODEL_ID)
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:multi_modal_projector.*", "re:vision_tower.*"],
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)
27
28# Confirm generations of the quantized model look sane.
29print("========== SAMPLE GENERATION ==============")
30input_ids = processor(text="Hello my name is", return_tensors="pt").input_ids.to("cuda")
31output = model.generate(input_ids, max_new_tokens=20)
32print(processor.decode(output[0]))
33print("==========================================")