Views
No views yet
1from transformers import AutoTokenizer
2from vllm import LLM, SamplingParams
3
4max_model_len, tp_size = 4096, 4
5model_name = "neuralmagic-ent/DeepSeek-Coder-V2-Instruct-0724-FP8"
6tokenizer = AutoTokenizer.from_pretrained(model_name)
7llm = LLM(model=model_name, tensor_parallel_size=tp_size, max_model_len=max_model_len, trust_remote_code=True)
8sampling_params = SamplingParams(temperature=0.3, max_tokens=256, stop_token_ids=[tokenizer.eos_token_id])
9
10messages_list = [
11 [{"role": "user", "content": "Who are you? Please respond in pirate speak!"}],
12]
13
14prompt_token_ids = [tokenizer.apply_chat_template(messages, add_generation_prompt=True) for messages in messages_list]
15
16outputs = llm.generate(prompt_token_ids=prompt_token_ids, sampling_params=sampling_params)
17
18generated_text = [output.outputs[0].text for output in outputs]
19print(generated_text)python quantize.py --model_path deepseek-ai/DeepSeek-Coder-V2-Instruct-0724 --quant_path "output_dir" --calib_size 128 1import argparse
2from datasets import load_dataset
3from transformers import AutoModelForCausalLM, AutoTokenizer
4from llmcompressor.modifiers.quantization import QuantizationModifier
5from llmcompressor.transformers import oneshot
6from llmcompressor.transformers.compression.helpers import calculate_offload_device_map
7import torch
8import os
9
10
11def main():
12 # Set up command line argument parsing
13 parser = argparse.ArgumentParser(description='Quantize a transformer model to FP8')
14 parser.add_argument('--model_id', type=str, required=True,
15 help='The model ID from HuggingFace (e.g., "meta-llama/Meta-Llama-3-8B-Instruct")')
16 parser.add_argument('--save_path', type=str, default='.',
17 help='Custom path to save the quantized model. If not provided, will use model_name-FP8')
18 parser.add_argument('--calib_size', type=int, default=256)
19 args = parser.parse_args()
20
21 device_map = calculate_offload_device_map(
22 args.model_id,
23 reserve_for_hessians=False,
24 num_gpus=torch.cuda.device_count(),
25 trust_remote_code=True,
26 torch_dtype=torch.bfloat16,
27 )
28
29 model = AutoModelForCausalLM.from_pretrained(
30 args.model_id, device_map=device_map, torch_dtype=torch.bfloat16, trust_remote_code=True,
31 )
32 tokenizer = AutoTokenizer.from_pretrained(args.model_id)
33
34 NUM_CALIBRATION_SAMPLES = args.calib_size
35 DATASET_ID = "garage-bAInd/Open-Platypus"
36 DATASET_SPLIT = "train"
37 ds = load_dataset(DATASET_ID, split=DATASET_SPLIT)
38 ds = ds.shuffle(seed=42).select(range(NUM_CALIBRATION_SAMPLES))
39
40 def preprocess(example):
41 concat_txt = example["instruction"] + "\n" + example["output"]
42 return {"text": concat_txt}
43
44 ds = ds.map(preprocess)
45
46 def tokenize(sample):
47 return tokenizer(
48 sample["text"],
49 padding=False,
50 truncation=False,
51 add_special_tokens=True,
52 )
53
54 ds = ds.map(tokenize, remove_columns=ds.column_names)
55
56 # Configure the quantization algorithm and scheme
57 recipe = QuantizationModifier(
58 targets="Linear", scheme="FP8", ignore=["lm_head", "re:.*\.mlp\.gate$"]
59 )
60
61 # Apply quantization
62 oneshot(
63 model=model,
64 dataset=ds,
65 recipe=recipe,
66 num_calibration_samples=args.calib_size
67 )
68
69 save_path = os.path.join(args.save_path, args.model_id.split("/")[1] + "-FP8")
70 os.makedirs(save_path, exist_ok=True)
71
72 # Save to disk in compressed-tensors format
73 model.save_pretrained(save_path, save_compressed=True, skip_compression_stats=True)
74 tokenizer.save_pretrained(save_path)
75 print(f"Model and tokenizer saved to: {save_path}")
76
77if __name__ == "__main__":
78 main()python evalplus/codegen/generate.py --model neuralmagic-ent/DeepSeek-Coder-V2-Instruct-0724-FP8 --bs 16 --temperature 0.2 --n_samples 50 --root "./results" --dataset humaneval --backend vllm --dtype auto --tp 8
python evalplus/evalplus/sanitize.py results/humaneval/neuralmagic-ent--DeepSeek-Coder-V2-Instruct-0724-FP8_vllm_temp_0.2
evalplus.evaluate --dataset humaneval --samples results/humaneval/neuralmagic-ent--DeepSeek-Coder-V2-Instruct-0724-FP8_vllm_temp_0.2-sanitized| Metric | deepseek-ai/DeepSeek-Coder-V2-Instruct-0724 | neuralmagic-ent/DeepSeek-Coder-V2-Instruct-0724-FP8 |
|---|---|---|
| HumanEval pass@1 | 89.3 | 88.7 |
| HumanEval pass@10 | 93.1 | 92.9 |
| HumanEval+ pass@1 | 82.9 | 82.8 |
| HumanEval+ pass@10 | 87.6 | 86.9 |
| Average Score | 88.23 | 87.83 |
| Recovery | 100.00 | 99.55 |