Views
No views yet
1from transformers import AutoTokenizer, AutoModelForCausalLM
2from datasets import load_dataset
3from llmcompressor import oneshot
4from llmcompressor.modifiers.quantization import GPTQModifier
5from llmcompressor.modifiers.smoothquant import SmoothQuantModifier
6
7model_id = "canopylabs/orpheus-3b-0.1-ft"
8model_out = "orpheus-3b-0.1-ft.w8a8"
9
10num_samples = 64
11max_seq_len = 4096
12
13tokenizer = AutoTokenizer.from_pretrained(model_id)
14
15def preprocess_fn(example):
16 return {"text": tokenizer.apply_chat_template(example["messages"], add_generation_prompt=False, tokenize=False)}
17
18ds = load_dataset("neuralmagic/LLM_compression_calibration", split="train")
19ds = ds.shuffle().select(range(num_samples))
20ds = ds.map(preprocess_fn)
21
22recipe = [
23 SmoothQuantModifier(
24 smoothing_strength=0.7,
25 mappings=[
26 [["re:.*q_proj", "re:.*k_proj", "re:.*v_proj"], "re:.*input_layernorm"],
27 [["re:.*gate_proj", "re:.*up_proj"], "re:.*post_attention_layernorm"],
28 [["re:.*down_proj"], "re:.*up_proj"],
29 ],
30 ),
31 GPTQModifier(
32 sequential=True,
33 targets="Linear",
34 scheme="W8A8",
35 ignore=["lm_head"],
36 dampening_frac=0.01,
37 )
38]
39
40model = AutoModelForCausalLM.from_pretrained(
41 model_id,
42 device_map="auto",
43 torch_dtype="bfloat16",
44)
45
46oneshot(
47 model=model,
48 dataset=ds,
49 recipe=recipe,
50 max_seq_length=max_seq_len,
51 num_calibration_samples=num_samples,
52)
53
54model.save_pretrained(model_out)