Views
No views yet
1from transformers import AutoTokenizer
2from vllm import LLM, SamplingParams
3
4max_model_len, tp_size = 4096, 2
5model_name = "neuralmagic-ent/Llama-3.1-Nemotron-70B-Instruct-HF-quantized.w8a8"
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 nvidia/Llama-3.1-Nemotron-70B-Instruct-HF --quant_path "output_dir" --calib_size 1024 --dampening_frac 0.05 --observer minmax1from datasets import load_dataset
2from transformers import AutoTokenizer
3from llmcompressor.modifiers.quantization import GPTQModifier
4from llmcompressor.transformers import SparseAutoModelForCausalLM, oneshot, apply
5import argparse
6from compressed_tensors.quantization import QuantizationScheme, QuantizationArgs, QuantizationType, QuantizationStrategy
7from llmcompressor.modifiers.smoothquant import SmoothQuantModifier
8
9
10parser = argparse.ArgumentParser()
11parser.add_argument('--model_path', type=str)
12parser.add_argument('--quant_path', type=str)
13parser.add_argument('--calib_size', type=int, default=256)
14parser.add_argument('--dampening_frac', type=float, default=0.01)
15parser.add_argument('--observer', type=str, default="minmax")
16args = parser.parse_args()
17
18model = SparseAutoModelForCausalLM.from_pretrained(
19 args.model_path,
20 device_map="auto",
21 torch_dtype="auto",
22 use_cache=False,
23 trust_remote_code=True,
24)
25tokenizer = AutoTokenizer.from_pretrained(args.model_path)
26
27
28NUM_CALIBRATION_SAMPLES = args.calib_size
29DATASET_ID = "garage-bAInd/Open-Platypus"
30DATASET_SPLIT = "train"
31ds = load_dataset(DATASET_ID, split=DATASET_SPLIT)
32ds = ds.shuffle(seed=42).select(range(NUM_CALIBRATION_SAMPLES))
33
34def preprocess(example):
35 concat_txt = example["instruction"] + "\n" + example["output"]
36 return {"text": concat_txt}
37
38ds = ds.map(preprocess)
39
40def tokenize(sample):
41 return tokenizer(
42 sample["text"],
43 padding=False,
44 truncation=False,
45 add_special_tokens=True,
46 )
47
48
49ds = ds.map(tokenize, remove_columns=ds.column_names)
50
51recipe = [
52 SmoothQuantModifier(smoothing_strength=0.8),
53 GPTQModifier(
54 targets=["Linear"],
55 ignore=["lm_head"],
56 scheme="W8A8",
57 dampening_frac=args.dampening_frac,
58 observer=args.observer,
59 #config_groups={"group_0": quant_scheme},
60 )
61]
62oneshot(
63 model=model,
64 dataset=ds,
65 recipe=recipe,
66 num_calibration_samples=args.calib_size,
67 max_seq_length=8192,
68)
69
70# Save to disk compressed.
71SAVE_DIR = args.quant_path
72model.save_pretrained(SAVE_DIR, save_compressed=True)
73tokenizer.save_pretrained(SAVE_DIR)lm_eval \
--model vllm \
--model_args pretrained="neuralmagic-ent/Llama-3.1-Nemotron-70B-Instruct-HF-quantized.w8a8",dtype=auto,add_bos_token=True,max_model_len=4096,tensor_parallel_size=1,gpu_memory_utilization=0.8,enable_chunked_prefill=True,trust_remote_code=True \
--tasks openllm \
--write_out \
--batch_size auto \
--output_path output_dir \
--show_configlm_eval \
--model vllm \
--model_args pretrained="neuralmagic-ent/Llama-3.1-Nemotron-70B-Instruct-HF-quantized.w8a8",dtype=auto,add_bos_token=False,max_model_len=4096,tensor_parallel_size=1,gpu_memory_utilization=0.8,enable_chunked_prefill=True,trust_remote_code=True \
--apply_chat_template \
--fewshot_as_multiturn \
--tasks leaderboard \
--write_out \
--batch_size auto \
--output_path output_dir \
--show_config
| Metric | nvidia/Llama-3.1-Nemotron-70B-Instruct-HF | neuralmagic-ent/Llama-3.1-Nemotron-70B-Instruct-HF-quantized.w8a8 |
|---|---|---|
| ARC-Challenge (Acc-Norm, 25-shot) | 71.76 | 72.61 |
| GSM8K (Strict-Match, 5-shot) | 82.94 | 83.55 |
| HellaSwag (Acc-Norm, 10-shot) | 87.61 | 87.43 |
| MMLU (Acc, 5-shot) | 82.56 | 82.55 |
| TruthfulQA (MC2, 0-shot) | 64.85 | 65.94 |
| Winogrande (Acc, 5-shot) | 83.82 | 84.14 |
| Average Score | 78.92 | 79.37 |
| Recovery | 100.00 | 100.57 |
| Metric | nvidia/Llama-3.1-Nemotron-70B-Instruct-HF | neuralmagic-ent/Llama-3.1-Nemotron-70B-Instruct-HF-quantized.w8a8 |
|---|---|---|
| IFEval (Inst-and-Prompt Level Strict Acc, 0-shot) | 74.3 | 74.75 |
| BBH (Acc-Norm, 3-shot) | 47.39 | 47.17 |
| Math-Hard (Exact-Match, 4-shot) | 23.21 | 22.61 |
| GPQA (Acc-Norm, 0-shot) | 1.2 | 2.52 |
| MUSR (Acc-Norm, 0-shot) | 13.2 | 13.28 |
| MMLU-Pro (Acc, 5-shot) | 43.45 | 43.1 |
| Average Score | 33.79 | 33.91 |
| Recovery | 100.00 | 100.36 |