Views
No views yet
vllm serve nm-testing/Phi-3.5-vision-instruct-W8A8-Dynamic-Per-Token --trust-remote-code --max-model-len 100000python -m eval.run eval_vllm --model_name nm-testing/Phi-3.5-vision-instruct-W8A8-Dynamic-Per-Token --url http://0.0.0.0:8000 --output_dir output/ --eval_name "chartqa"
...
================================================================================
Metrics:
{
"explicit_prompt_relaxed_correctness": 0.6472,
"anywhere_in_answer_relaxed_correctness": 0.6616
}
================================================================================1from datasets import load_dataset
2from transformers import AutoTokenizer, AutoModelForCausalLM
3
4from llmcompressor.modifiers.quantization import GPTQModifier
5# from llmcompressor.modifiers.smoothquant import SmoothQuantModifier
6from llmcompressor.transformers import oneshot, wrap_hf_model_class
7
8# Select model and load it.
9MODEL_ID = "microsoft/Phi-3.5-vision-instruct"
10model_class = wrap_hf_model_class(AutoModelForCausalLM)
11model = model_class.from_pretrained(
12 MODEL_ID,
13 device_map="auto",
14 torch_dtype="auto",
15 trust_remote_code=True,
16 _attn_implementation="eager",
17)
18processor = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
19
20# Select calibration dataset.
21DATASET_ID = "HuggingFaceH4/ultrachat_200k"
22DATASET_SPLIT = "train_sft"
23
24# Select number of samples. 512 samples is a good place to start.
25# Increasing the number of samples can improve accuracy.
26NUM_CALIBRATION_SAMPLES = 512
27MAX_SEQUENCE_LENGTH = 2048
28
29# Load dataset and preprocess.
30ds = load_dataset(DATASET_ID, split=DATASET_SPLIT)
31ds = ds.shuffle(seed=42).select(range(NUM_CALIBRATION_SAMPLES))
32
33
34def preprocess(example):
35 return {
36 "text": processor.apply_chat_template(
37 example["messages"],
38 tokenize=False,
39 )
40 }
41
42
43ds = ds.map(preprocess)
44
45
46# Tokenize inputs.
47def tokenize(sample):
48 return processor(
49 sample["text"],
50 padding=False,
51 max_length=MAX_SEQUENCE_LENGTH,
52 truncation=True,
53 add_special_tokens=False,
54 )
55
56
57ds = ds.map(tokenize, remove_columns=ds.column_names)
58print(ds)
59
60# Configure algorithms. In this case, we:
61# * apply SmoothQuant to make the activations easier to quantize
62# * quantize the weights to int8 with GPTQ (static per channel)
63# * quantize the activations to int8 (dynamic per token)
64# Note: set sequential_update: true in the recipe to reduce memory
65ignore=["re:.*lm_head", "re:model.vision_embed_tokens.*"]
66recipe = [
67 # SmoothQuantModifier(smoothing_strength=0.8, ignore=ignore),
68 GPTQModifier(targets="Linear", scheme="W8A8", ignore=ignore),
69]
70
71# Apply algorithms.
72oneshot(
73 model=model,
74 dataset=ds,
75 recipe=recipe,
76 max_seq_length=MAX_SEQUENCE_LENGTH,
77 num_calibration_samples=NUM_CALIBRATION_SAMPLES,
78 trust_remote_code_model=True,
79)
80
81# Confirm generations of the quantized model look sane.
82print("\n\n")
83print("========== SAMPLE GENERATION ==============")
84input_ids = processor("Hello my name is", return_tensors="pt").input_ids.to("cuda")
85output = model.generate(input_ids, max_new_tokens=100)
86print(processor.decode(output[0]))
87print("==========================================\n\n")
88
89# Save to disk compressed.
90SAVE_DIR = MODEL_ID.split("/")[1] + "-W8A8-Dynamic-Per-Token"
91model.save_pretrained(SAVE_DIR, save_compressed=True)
92processor.save_pretrained(SAVE_DIR)