Views
No views yet
lm_eval --model vllm --model_args pretrained=/home/mgoin/code/llm-compressor/examples/quantizing_moe/OLMoE-1B-7B-0924-Instruct-FP8,tensor_parallel_size=1,trust_remote_code=True --tasks gsm8k --num_fewshot 5 --batch_size auto
vllm (pretrained=/home/mgoin/code/llm-compressor/examples/quantizing_moe/OLMoE-1B-7B-0924-Instruct-FP8,tensor_parallel_size=1,trust_remote_code=True), gen_kwargs: (None), limit: None, num_fewshot: 5, batch_size: auto
|Tasks|Version| Filter |n-shot| Metric | |Value | |Stderr|
|-----|------:|----------------|-----:|-----------|---|-----:|---|-----:|
|gsm8k| 3|flexible-extract| 5|exact_match|↑ |0.3510|± |0.0131|
| | |strict-match | 5|exact_match|↑ |0.3389|± |0.0130|1import torch
2from datasets import load_dataset
3from transformers import AutoTokenizer
4
5from llmcompressor.modifiers.quantization import QuantizationModifier
6from llmcompressor.transformers import SparseAutoModelForCausalLM, oneshot
7
8# select a Mixture of Experts model for quantization
9MODEL_ID = "allenai/OLMoE-1B-7B-0924-Instruct"
10
11model = SparseAutoModelForCausalLM.from_pretrained(
12 MODEL_ID, device_map="auto", torch_dtype="auto", trust_remote_code=True
13)
14tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
15
16# Select calibration dataset.
17# its recommended to use more calibration samples for MoE models so each expert is hit
18DATASET_ID = "HuggingFaceH4/ultrachat_200k"
19DATASET_SPLIT = "train_sft"
20NUM_CALIBRATION_SAMPLES = 2048
21MAX_SEQUENCE_LENGTH = 2048
22
23
24# Load dataset and preprocess.
25ds = load_dataset(DATASET_ID, split=DATASET_SPLIT)
26ds = ds.shuffle(seed=42).select(range(NUM_CALIBRATION_SAMPLES))
27
28
29def preprocess(example):
30 return {
31 "text": tokenizer.apply_chat_template(
32 example["messages"],
33 tokenize=False,
34 )
35 }
36
37
38ds = ds.map(preprocess)
39
40
41# Tokenize inputs.
42def tokenize(sample):
43 return tokenizer(
44 sample["text"],
45 padding=False,
46 max_length=MAX_SEQUENCE_LENGTH,
47 truncation=True,
48 add_special_tokens=False,
49 )
50
51
52ds = ds.map(tokenize, remove_columns=ds.column_names)
53
54# define a llmcompressor recipe for FP8 W8A8 quantization
55# since the MoE gate layers are sensitive to quantization, we add them to the ignore
56# list so they remain at full precision
57recipe = [
58 QuantizationModifier(
59 targets="Linear",
60 scheme="FP8",
61 ignore=["lm_head", "re:.*mlp.gate$"],
62 ),
63]
64
65SAVE_DIR = MODEL_ID.split("/")[1] + "-FP8"
66
67oneshot(
68 model=model,
69 dataset=ds,
70 recipe=recipe,
71 max_seq_length=MAX_SEQUENCE_LENGTH,
72 num_calibration_samples=NUM_CALIBRATION_SAMPLES,
73 save_compressed=True,
74 output_dir=SAVE_DIR,
75)
76
77
78print("========== SAMPLE GENERATION ==============")
79SAMPLE_INPUT = ["I love quantization because"]
80tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
81inputs = tokenizer(SAMPLE_INPUT, return_tensors="pt", padding=True).to(model.device)
82output = model.generate(**inputs, max_length=50)
83text_output = tokenizer.batch_decode(output)
84print(text_output)