Views
No views yet
1from vllm import LLM, SamplingParams
2from transformers import AutoProcessor
3
4model_id = "Ju214/Mistral-Small-24B-3.1"
5number_gpus = 1
6
7sampling_params = SamplingParams(temperature=0.7, top_p=0.8, max_tokens=256)
8processor = AutoProcessor.from_pretrained(model_id)
9
10messages = [{"role": "user", "content": "Give me a introduction to large language model."}]
11
12prompts = processor.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
13
14llm = LLM(model=model_id, tensor_parallel_size=number_gpus)
15
16outputs = llm.generate(prompts, sampling_params)
17
18generated_text = outputs[0].outputs[0].text
19print(generated_text)1from transformers import AutoProcessor
2from llmcompressor.modifiers.quantization import GPTQModifier
3from llmcompressor.transformers import oneshot
4from llmcompressor.transformers.tracing import TraceableMistral3ForConditionalGeneration
5from datasets import load_dataset, interleave_datasets
6from PIL import Image
7import io
8
9# Load model
10model_stub = "mistralai/Mistral-Small-3.1-24B-Instruct-2503"
11model_name = model_stub.split("/")[-1]
12
13num_text_samples = 1024
14num_vision_samples = 1024
15max_seq_len = 8192
16
17processor = AutoProcessor.from_pretrained(model_stub)
18
19model = TraceableMistral3ForConditionalGeneration.from_pretrained(
20 model_stub,
21 device_map="auto",
22 torch_dtype="auto",
23)
24
25# Text-only data subset
26def preprocess_text(example):
27 input = {
28 "text": processor.apply_chat_template(
29 example["messages"],
30 add_generation_prompt=False,
31 ),
32 "images": None,
33 }
34 tokenized_input = processor(**input, max_length=max_seq_len, truncation=True)
35 tokenized_input["pixel_values"] = tokenized_input.get("pixel_values", None)
36 tokenized_input["image_sizes"] = tokenized_input.get("image_sizes", None)
37 return tokenized_input
38
39dst = load_dataset("neuralmagic/calibration", name="LLM", split="train").select(range(num_text_samples))
40dst = dst.map(preprocess_text, remove_columns=dst.column_names)
41
42# Text + vision data subset
43def preprocess_vision(example):
44 messages = []
45 image = None
46 for message in example["messages"]:
47 message_content = []
48 for content in message["content"]:
49 if content["type"] == "text":
50 message_content.append({"type": "text", "text": content["text"]})
51 else:
52 message_content.append({"type": "image"})
53 image = Image.open(io.BytesIO(content["image"]))
54
55 messages.append(
56 {
57 "role": message["role"],
58 "content": message_content,
59 }
60 )
61
62 input = {
63 "text": processor.apply_chat_template(
64 messages,
65 add_generation_prompt=False,
66 ),
67 "images": image,
68 }
69 tokenized_input = processor(**input, max_length=max_seq_len, truncation=True)
70 tokenized_input["pixel_values"] = tokenized_input.get("pixel_values", None)
71 tokenized_input["image_sizes"] = tokenized_input.get("image_sizes", None)
72 return tokenized_input
73
74dsv = load_dataset("neuralmagic/calibration", name="VLM", split="train").select(range(num_vision_samples))
75dsv = dsv.map(preprocess_vision, remove_columns=dsv.column_names)
76
77# Interleave subsets
78ds = interleave_datasets((dsv, dst))
79
80# Configure the quantization algorithm and scheme
81recipe = GPTQModifier(
82 ignore=["language_model.lm_head", "re:vision_tower.*", "re:multi_modal_projector.*"],
83 sequential_targets=["MistralDecoderLayer"],
84 dampening_frac=0.01,
85 targets="Linear",
86 scheme="W4A16",
87)
88
89# Define data collator
90def data_collator(batch):
91 import torch
92 assert len(batch) == 1
93 collated = {}
94 for k, v in batch[0].items():
95 if v is None:
96 continue
97 if k == "input_ids":
98 collated[k] = torch.LongTensor(v)
99 elif k == "pixel_values":
100 collated[k] = torch.tensor(v, dtype=torch.bfloat16)
101 else:
102 collated[k] = torch.tensor(v)
103 return collated
104
105
106# Apply quantization
107oneshot(
108 model=model,
109 dataset=ds,
110 recipe=recipe,
111 max_seq_length=max_seq_len,
112 data_collator=data_collator,
113 num_calibration_samples=num_text_samples + num_vision_samples,
114)
115
116# Save to disk in compressed-tensors format
117save_path = model_name + "-quantized.w4a16"
118model.save_pretrained(save_path)
119processor.save_pretrained(save_path)
120print(f"Model and tokenizer saved to: {save_path}")lm_eval \
--model vllm \
--model_args pretrained="RedHatAI/Mistral-Small-3.1-24B-Instruct-2503-quantized.w4a16",dtype=auto,gpu_memory_utilization=0.5,max_model_len=8192,enable_chunk_prefill=True,tensor_parallel_size=2 \
--tasks mmlu \
--num_fewshot 5 \
--apply_chat_template\
--fewshot_as_multiturn \
--batch_size autolm_eval \
--model vllm \
--model_args pretrained="RedHatAI/Mistral-Small-3.1-24B-Instruct-2503-quantized.w4a16",dtype=auto,gpu_memory_utilization=0.5,max_model_len=8192,enable_chunk_prefill=True,tensor_parallel_size=2 \
--tasks arc_challenge \
--num_fewshot 25 \
--apply_chat_template\
--fewshot_as_multiturn \
--batch_size autolm_eval \
--model vllm \
--model_args pretrained="RedHatAI/Mistral-Small-3.1-24B-Instruct-2503-quantized.w4a16",dtype=auto,gpu_memory_utilization=0.9,max_model_len=8192,enable_chunk_prefill=True,tensor_parallel_size=2 \
--tasks gsm8k \
--num_fewshot 8 \
--apply_chat_template\
--fewshot_as_multiturn \
--batch_size autolm_eval \
--model vllm \
--model_args pretrained="RedHatAI/Mistral-Small-3.1-24B-Instruct-2503-quantized.w4a16",dtype=auto,gpu_memory_utilization=0.5,max_model_len=8192,enable_chunk_prefill=True,tensor_parallel_size=2 \
--tasks hellaswag \
--num_fewshot 10 \
--apply_chat_template\
--fewshot_as_multiturn \
--batch_size autolm_eval \
--model vllm \
--model_args pretrained="RedHatAI/Mistral-Small-3.1-24B-Instruct-2503-quantized.w4a16",dtype=auto,gpu_memory_utilization=0.5,max_model_len=8192,enable_chunk_prefill=True,tensor_parallel_size=2 \
--tasks winogrande \
--num_fewshot 5 \
--apply_chat_template\
--fewshot_as_multiturn \
--batch_size autolm_eval \
--model vllm \
--model_args pretrained="RedHatAI/Mistral-Small-3.1-24B-Instruct-2503-quantized.w4a16",dtype=auto,gpu_memory_utilization=0.5,max_model_len=8192,enable_chunk_prefill=True,tensor_parallel_size=2 \
--tasks truthfulqa \
--num_fewshot 0 \
--apply_chat_template\
--batch_size autolm_eval \
--model vllm \
--model_args pretrained="RedHatAI/Mistral-Small-3.1-24B-Instruct-2503-quantized.w4a16",dtype=auto,gpu_memory_utilization=0.5,max_model_len=8192,enable_chunk_prefill=True,tensor_parallel_size=2 \
--tasks mmlu_pro \
--num_fewshot 5 \
--apply_chat_template\
--fewshot_as_multiturn \
--batch_size autolm_eval \
--model vllm \
--model_args pretrained="RedHatAI/Mistral-Small-3.1-24B-Instruct-2503-quantized.w4a16",dtype=auto,gpu_memory_utilization=0.9,max_images=8,enable_chunk_prefill=True,tensor_parallel_size=2 \
--tasks mmmu_val \
--apply_chat_template\
--batch_size autolm_eval \
--model vllm \
--model_args pretrained="RedHatAI/Mistral-Small-3.1-24B-Instruct-2503-quantized.w4a16",dtype=auto,gpu_memory_utilization=0.9,max_images=8,enable_chunk_prefill=True,tensor_parallel_size=2 \
--tasks chartqa \
--apply_chat_template\
--batch_size autopython3 codegen/generate.py \
--model RedHatAI/Mistral-Small-3.1-24B-Instruct-2503-quantized.w4a16 \
--bs 16 \
--temperature 0.2 \
--n_samples 50 \
--root "." \
--dataset humaneval
python3 evalplus/sanitize.py \
humaneval/RedHatAI--Mistral-Small-3.1-24B-Instruct-2503-quantized.w4a16_vllm_temp_0.2evalplus.evaluate \
--dataset humaneval \
--samples humaneval/RedHatAI--Mistral-Small-3.1-24B-Instruct-2503-quantized.w4a16_vllm_temp_0.2-sanitized| Category | Benchmark | Mistral-Small-3.1-24B-Instruct-2503 | Mistral-Small-3.1-24B-Instruct-2503-quantized.w4a16 (this model) | Recovery |
|---|---|---|---|---|
| OpenLLM v1 | MMLU (5-shot) | 80.67 | 79.74 | 98.9% |
| ARC Challenge (25-shot) | 72.78 | 72.18 | 99.2% | |
| GSM-8K (5-shot, strict-match) | 58.68 | 59.59 | 101.6% | |
| Hellaswag (10-shot) | 83.70 | 83.25 | 99.5% | |
| Winogrande (5-shot) | 83.74 | 83.43 | 99.6% | |
| TruthfulQA (0-shot, mc2) | 70.62 | 69.56 | 98.5% | |
| Average | 75.03 | 74.63 | 99.5% | |
| MMLU-Pro (5-shot) | 67.25 | 66.56 | 99.0% | |
| GPQA CoT main (5-shot) | 42.63 | 47.10 | 110.5% | |
| GPQA CoT diamond (5-shot) | 45.96 | 44.95 | 97.80% | |
| Coding | HumanEval pass@1 | 84.70 | 84.60 | 99.9% |
| HumanEval+ pass@1 | 79.50 | 79.90 | 100.5% | |
| MBPP pass@1 | 71.10 | 70.10 | 98.6% | |
| MBPP+ pass@1 | 60.60 | 60.70 | 100.2% | |
| Vision | MMMU (0-shot) | 52.11 | 50.11 | 96.2% |
| ChartQA (0-shot) | 81.36 | 80.92 | 99.5% | |