Views
No views yet
lm_head layer kept in its original precision to maintain output quality.oneshot method from llm-compressor with the FP8_DYNAMIC scheme.
No calibration dataset was required for this quantization scheme.1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3from llmcompressor import oneshot
4from llmcompressor.modifiers.quantization import QuantizationModifier
5import os
6
7# --- 1. Set the new Model ID ---
8MODEL_ID = "Qwen/Qwen2.5-Coder-32B-Instruct"
9
10# --- 2. Load model and tokenizer using Auto classes ---
11print(f"Loading model: {MODEL_ID}...")
12model = AutoModelForCausalLM.from_pretrained(
13 MODEL_ID,
14 device_map="auto",
15 torch_dtype="auto",
16 trust_remote_code=True,
17)
18print("Loading tokenizer...")
19tokenizer = AutoTokenizer.from_pretrained(
20 MODEL_ID,
21 trust_remote_code=True,
22)
23
24# --- 3. The quantization recipe remains the same ---
25print("Configuring FP8 quantization recipe...")
26recipe = QuantizationModifier(
27 targets="Linear", scheme="FP8_DYNAMIC", ignore=["lm_head"]
28)
29
30# Apply quantization. This step can take some time.
31print("Applying one-shot quantization...")
32oneshot(model=model, recipe=recipe, tokenizer=tokenizer)
33print("Quantization complete.")
34
35# --- 4. Confirm generation with the Qwen chat template ---
36print("\n========== SAMPLE GENERATION ==============")
37prompt = "Write a Python function for a quicksort algorithm. Include comments to explain the logic."
38messages = [
39 {"role": "system", "content": "You are a helpful assistant specialized in writing code."},
40 {"role": "user", "content": prompt}
41]
42
43input_text = tokenizer.apply_chat_template(
44 messages,
45 tokenize=False,
46 add_generation_prompt=True
47)
48model_inputs = tokenizer([input_text], return_tensors="pt").to(model.device)
49
50output_ids = model.generate(
51 **model_inputs,
52 max_new_tokens=256,
53)
54
55input_token_len = model_inputs.input_ids.shape[1]
56generated_tokens = output_ids[0, input_token_len:]
57response = tokenizer.decode(generated_tokens, skip_special_tokens=True)
58
59print(f"Generated Response:\n{response}")
60print("==========================================")
61
62
63# --- 5. Save the quantized model and the tokenizer correctly ---
64SAVE_DIR = MODEL_ID.split("/")[1] + "-FP8-Dynamic"
65print(f"\nSaving quantized model to {SAVE_DIR}...")
66model.save_pretrained(SAVE_DIR)
67
68print(f"Saving tokenizer to {SAVE_DIR}...")
69tokenizer.save_pretrained(SAVE_DIR)
70
71print(f"\nModel and tokenizer saved successfully to '{SAVE_DIR}'")transformers, or for optimized FP8 inference, with vLLM.transformers (for functional checking, not FP8 optimized)1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4MODEL_REPO_ID = "textgeflecht/Qwen2.5-Coder-32B-Instruct-FP8-dynamic"
5
6# For Qwen models, it is recommended to use trust_remote_code=True
7model = AutoModelForCausalLM.from_pretrained(
8 MODEL_REPO_ID,
9 device_map="auto",
10 torch_dtype="auto",
11 trust_remote_code=True
12)
13tokenizer = AutoTokenizer.from_pretrained(
14 MODEL_REPO_ID,
15 trust_remote_code=True
16)
17
18prompt = "Write a complete and efficient implementation of the merge sort algorithm in Rust."
19messages = [
20 {"role": "system", "content": "You are a helpful assistant specialized in writing high-quality Rust code."},
21 {"role": "user", "content": prompt}
22]
23
24# Apply the chat template to format the prompt correctly
25input_text = tokenizer.apply_chat_template(
26 messages,
27 tokenize=False,
28 add_generation_prompt=True
29)
30
31# Tokenize the input and move to the device
32model_inputs = tokenizer([input_text], return_tensors="pt").to(model.device)
33
34# Generate output
35output_ids = model.generate(
36 **model_inputs,
37 max_new_tokens=1024,
38 do_sample=True,
39 temperature=0.6,
40 top_p=0.9
41)
42
43# Decode only the newly generated tokens
44input_token_len = model_inputs.input_ids.shape[1]
45generated_tokens = output_ids[0, input_token_len:]
46response = tokenizer.decode(generated_tokens, skip_special_tokens=True)
47
48print("--- Prompt ---")
49print(prompt)
50print("\n--- Qwen Response ---")
51print(response)1# 1. Set your Hugging Face Token (optional, but recommended)
2# export HF_TOKEN="YOUR_HUGGINGFACE_ACCESS_TOKEN_HERE"
3
4# 2. Run the vLLM Docker container.
5# Replace 'vllm/vllm-openai:latest' with a recent official build.
6sudo docker run --gpus all \
7 -v "$HOME/.cache/huggingface:/root/.cache/huggingface" \
8 -p 8000:8000 \
9 -e HF_TOKEN="$HF_TOKEN" \
10 vllm/vllm-openai:latest \
11 --model textgeflecht/Qwen2.5-Coder-32B-Instruct-FP8-dynamic \
12 --tokenizer-mode auto \
13 --load-format auto \
14 --trust-remote-code \
15 --max-model-len 4096 # Optional: Adjust based on your VRAM