Views
No views yet
1from vllm import LLM, SamplingParams
2from transformers import AutoTokenizer
3
4model_id = "neuralmagic/Qwen2-0.5B-Instruct-quantized.w8a16"
5number_gpus = 1
6
7sampling_params = SamplingParams(temperature=0.7, top_p=0.8, max_tokens=256)
8
9tokenizer = AutoTokenizer.from_pretrained(model_id)
10
11messages = [
12 {"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},
13 {"role": "user", "content": "Who are you?"},
14]
15
16prompts = tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
17
18llm = LLM(model=model_id, tensor_parallel_size=number_gpus)
19
20outputs = llm.generate(prompts, sampling_params)
21
22generated_text = outputs[0].outputs[0].text
23print(generated_text)generate() function.1from transformers import AutoTokenizer, AutoModelForCausalLM
2
3model_id = "neuralmagic/Qwen2-0.5B-Instruct-quantized.w8a16"
4
5tokenizer = AutoTokenizer.from_pretrained(model_id)
6model = AutoModelForCausalLM.from_pretrained(
7 model_id,
8 torch_dtype="auto",
9 device_map="auto",
10)
11
12messages = [
13 {"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},
14 {"role": "user", "content": "Who are you?"},
15]
16
17input_ids = tokenizer.apply_chat_template(
18 messages,
19 add_generation_prompt=True,
20 return_tensors="pt"
21).to(model.device)
22
23terminators = [
24 tokenizer.eos_token_id,
25 tokenizer.convert_tokens_to_ids("<|eot_id|>")
26]
27
28outputs = model.generate(
29 input_ids,
30 max_new_tokens=256,
31 eos_token_id=terminators,
32 do_sample=True,
33 temperature=0.7,
34 top_p=0.8,
35)
36response = outputs[0][input_ids.shape[-1]:]
37print(tokenizer.decode(response, skip_special_tokens=True))1from transformers import AutoTokenizer
2from auto_gptq import AutoGPTQForCausalLM, BaseQuantizeConfig
3import random
4
5model_id = "Qwen/Qwen2-0.5B-Instruct"
6
7num_samples = 256
8max_seq_len = 8192
9
10tokenizer = AutoTokenizer.from_pretrained(model_id)
11
12max_token_id = len(tokenizer.get_vocab()) - 1
13examples = []
14for _ in range(num_samples):
15 examples.append(
16 {
17 "input_ids": [random.randint(0, max_token_id) for _ in range(max_seq_len)],
18 "attention_mask": max_seq_len*[1],
19 }
20)
21
22quantize_config = BaseQuantizeConfig(
23 bits=8,
24 group_size=-1,
25 desc_act=False,
26 model_file_base_name="model",
27 damp_percent=0.01,
28)
29
30model = AutoGPTQForCausalLM.from_pretrained(
31 model_id,
32 quantize_config,
33 device_map="auto",
34)
35
36model.quantize(examples)
37model.save_pretrained("Qwen2-0.5B-Instruct-quantized.w8a16")lm_eval \
--model vllm \
--model_args pretrained="neuralmagic/Qwen2-0.5B-Instruct-quantized.w8a16",dtype=auto,gpu_memory_utilization=0.4,add_bos_token=True,max_model_len=4096,tensor_parallel_size=1 \
--tasks openllm \
--batch_size auto| Benchmark | Qwen2-0.5B-Instruct | Qwen2-0.5B-Instruct-quantized.w8a16(this model) | Recovery |
| MMLU (5-shot) | 43.72 | 43.85 | 100.3% |
| ARC Challenge (25-shot) | 31.83 | 31.74 | 99.7% |
| GSM-8K (5-shot, strict-match) | 37.68 | 38.06 | 101.0% |
| Hellaswag (10-shot) | 49.50 | 49.42 | 99.8% |
| Winogrande (5-shot) | 56.27 | 55.64 | 99.7% |
| TruthfulQA (0-shot) | 39.38 | 39.24 | 99.7% |
| Average | 43.06 | 42.99 | 99.8% |