Views
No views yet
1from vllm import LLM, SamplingParams
2from transformers import AutoTokenizer
3
4model_id = "neuralmagic/Phi-3-medium-128k-instruct-FP8"
5
6sampling_params = SamplingParams(temperature=0.6, top_p=0.9, max_tokens=256)
7
8tokenizer = AutoTokenizer.from_pretrained(model_id)
9
10messages = [
11 {"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},
12 {"role": "user", "content": "Who are you? Remember to respond in pirate speak!"},
13]
14
15prompts = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
16
17llm = LLM(model=model_id)
18
19outputs = llm.generate(prompts, sampling_params)
20
21generated_text = outputs[0].outputs[0].text
22print(generated_text)1import torch
2from datasets import load_dataset
3from transformers import AutoTokenizer
4
5from llmcompressor.transformers import SparseAutoModelForCausalLM, oneshot
6from llmcompressor.transformers.compression.helpers import (
7 calculate_offload_device_map,
8 custom_offload_device_map,
9)
10
11recipe = """
12quant_stage:
13 quant_modifiers:
14 QuantizationModifier:
15 ignore: ["lm_head"]
16 config_groups:
17 group_0:
18 weights:
19 num_bits: 8
20 type: float
21 strategy: tensor
22 dynamic: false
23 symmetric: true
24 input_activations:
25 num_bits: 8
26 type: float
27 strategy: tensor
28 dynamic: false
29 symmetric: true
30 targets: ["Linear"]
31"""
32
33model_stub = "microsoft/Phi-3-medium-128k-instruct"
34model_name = model_stub.split("/")[-1]
35
36device_map = calculate_offload_device_map(
37 model_stub, reserve_for_hessians=False, num_gpus=1, torch_dtype=torch.float16
38)
39
40model = SparseAutoModelForCausalLM.from_pretrained(
41 model_stub, torch_dtype=torch.float16, device_map=device_map
42)
43tokenizer = AutoTokenizer.from_pretrained(model_stub)
44
45output_dir = f"./{model_name}-FP8"
46
47DATASET_ID = "HuggingFaceH4/ultrachat_200k"
48DATASET_SPLIT = "train_sft"
49NUM_CALIBRATION_SAMPLES = 512
50MAX_SEQUENCE_LENGTH = 4096
51
52ds = load_dataset(DATASET_ID, split=DATASET_SPLIT)
53ds = ds.shuffle(seed=42).select(range(NUM_CALIBRATION_SAMPLES))
54
55def preprocess(example):
56 return {
57 "text": tokenizer.apply_chat_template(
58 example["messages"],
59 tokenize=False,
60 )
61 }
62
63ds = ds.map(preprocess)
64
65def tokenize(sample):
66 return tokenizer(
67 sample["text"],
68 padding=False,
69 max_length=MAX_SEQUENCE_LENGTH,
70 truncation=True,
71 add_special_tokens=False,
72 )
73
74ds = ds.map(tokenize, remove_columns=ds.column_names)
75
76oneshot(
77 model=model,
78 output_dir=output_dir,
79 dataset=ds,
80 recipe=recipe,
81 max_seq_length=MAX_SEQUENCE_LENGTH,
82 num_calibration_samples=NUM_CALIBRATION_SAMPLES,
83 save_compressed=True,
84)lm_eval \
--model vllm \
--model_args pretrained="neuralmagic/Phi-3-medium-128k-instruct-FP8",dtype=auto,gpu_memory_utilization=0.7,add_bos_token=True,max_model_len=4096 \
--tasks openllm \
--batch_size auto| Benchmark | Phi-3-medium-128k-instruct | Phi-3-medium-128k-instruct-FP8(this model) | Recovery |
| MMLU (5-shot) | 76.53 | 76.66 | 100.1% |
| ARC Challenge (25-shot) | 68.17 | 67.06 | 98.37% |
| GSM-8K (5-shot, strict-match) | 84.46 | 84.31 | 99.82% |
| Hellaswag (10-shot) | 84.77 | 84.63 | 99.83% |
| Winogrande (5-shot) | 75.22 | 74.51 | 99.06% |
| TruthfulQA (0-shot) | 54.52 | 54.71 | 100.35% |
| Average | 73.95 | 73.65 | 99.60% |