Views
No views yet
1from vllm import LLM, SamplingParams
2from transformers import AutoTokenizer
3
4model_id = "neuralmagic/Llama-2-7b-chat-hf-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?"},
13]
14
15prompts = tokenizer.apply_chat_template(messages, tokenize=False)
16
17llm = LLM(model=model_id)
18
19outputs = llm.generate(prompts, sampling_params)
20
21generated_text = outputs[0].outputs[0].text
22print(generated_text)1from datasets import load_dataset
2from transformers import AutoTokenizer
3import numpy as np
4import torch
5
6from auto_fp8 import AutoFP8ForCausalLM, BaseQuantizeConfig
7
8MODEL_DIR = "meta-llama/Llama-2-7b-chat-hf"
9final_model_dir = MODEL_DIR.split("/")[-1]
10
11CONTEXT_LENGTH = 4096
12NUM_SAMPLES = 512
13NUM_REPEATS = 10
14
15pretrained_model_dir = MODEL_DIR
16tokenizer = AutoTokenizer.from_pretrained(pretrained_model_dir, use_fast=True, model_max_length=CONTEXT_LENGTH)
17tokenizer.pad_token = tokenizer.eos_token
18
19tokenizer_num_tokens = len(list(tokenizer.get_vocab().values()))
20total_token_samples = NUM_REPEATS * tokenizer_num_tokens
21num_random_samp = -(-total_token_samples // CONTEXT_LENGTH)
22
23input_ids = np.tile(np.arange(tokenizer_num_tokens), NUM_REPEATS + 1)[:num_random_samp * CONTEXT_LENGTH]
24np.random.shuffle(input_ids)
25input_ids = input_ids.reshape(num_random_samp, CONTEXT_LENGTH)
26input_ids = torch.tensor(input_ids, dtype=torch.int64).to("cuda")
27
28quantize_config = BaseQuantizeConfig(
29 quant_method="fp8",
30 activation_scheme="static",
31)
32
33examples = input_ids
34
35model = AutoFP8ForCausalLM.from_pretrained(pretrained_model_dir, quantize_config=quantize_config)
36
37model.quantize(examples)
38
39quantized_model_dir = f"{final_model_dir}-FP8"
40model.save_quantized(quantized_model_dir)lm_eval \
--model vllm \
--model_args pretrained="neuralmagic/Llama-2-7b-chat-hf-FP8",dtype=auto,gpu_memory_utilization=0.4,add_bos_token=True,max_model_len=4096 \
--tasks openllm \
--batch_size auto| Benchmark | Llama-2-7b-chat-hf | Llama-2-7b-chat-hf-FP8(this model) | Recovery |
| MMLU (5-shot) | 47.39 | 47.33 | 99.87% |
| ARC Challenge (25-shot) | 53.33 | 53.58 | 100.4% |
| GSM-8K (5-shot, strict-match) | 23.28 | 22.82 | 98.02% |
| Hellaswag (10-shot) | 78.64 | 78.31 | 99.58% |
| Winogrande (5-shot) | 72.38 | 72.22 | 99.77% |
| TruthfulQA (0-shot) | 45.58 | 45.73 | 100.3% |
| Average | 53.43 | 53.28 | 99.72% |