FP8 Qwen/Qwen3-8B model
Developed by: jerryzh168
License: apache-2.0
Quantized from Model : Qwen/Qwen3-8B
Quantization Method : FP8
Inference with vLLM
Install vllm nightly and torchao nightly to get some recent changes:
pip install vllm --pre --extra-index-url https://wheels.vllm.ai/nightly
pip install torchao
Serving
Then we can serve with the following command:
1 # Server
2 export MODEL=jerryzh168/Qwen3-8B-FP8
3 VLLM_DISABLE_COMPILE_CACHE=1 vllm serve $MODEL --tokenizer $MODEL -O3
1 # Client
2 curl http://localhost:8000/v1/chat/completions -H "Content-Type: application/json" -d '{
3 "model": "jerryzh168/Qwen3-8B-FP8",
4 "messages": [
5 {"role": "user", "content": "Give me a short introduction to large language models."}
6 ],
7 "temperature": 0.6,
8 "top_p": 0.95,
9 "top_k": 20,
10 "max_tokens": 32768
11 }'
Note: please use VLLM_DISABLE_COMPILE_CACHE=1 to disable compile cache when running this code, e.g. VLLM_DISABLE_COMPILE_CACHE=1 python example.py, since there are some issues with the composability of compile in vLLM and torchao,
this is expected be resolved in pytorch 2.8.
Inference with Transformers
Install the required packages:
1 pip install git+https://github.com/huggingface/transformers@main
2 pip install torchao
3 pip install torch
4 pip install accelerate
Example:
1 import torch
2 from transformers import AutoModelForCausalLM, AutoTokenizer
3
4 model_name = "jerryzh168/Qwen3-8B-FP8"
5
6 # load the tokenizer and the model
7 tokenizer = AutoTokenizer.from_pretrained(model_name)
8 model = AutoModelForCausalLM.from_pretrained(
9 model_name,
10 torch_dtype="auto",
11 device_map="auto"
12 )
13
14 # prepare the model input
15 prompt = "Give me a short introduction to large language model."
16 messages = [
17 {"role": "user", "content": prompt}
18 ]
19 text = tokenizer.apply_chat_template(
20 messages,
21 tokenize=False,
22 add_generation_prompt=True,
23 enable_thinking=True # Switches between thinking and non-thinking modes. Default is True.
24 )
25 model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
26
27 # conduct text completion
28 generated_ids = model.generate(
29 **model_inputs,
30 max_new_tokens=32768
31 )
32 output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
33
34 # parsing thinking content
35 try:
36 # rindex finding 151668 (</think>)
37 index = len(output_ids) - output_ids[::-1].index(151668)
38 except ValueError:
39 index = 0
40
41 thinking_content = tokenizer.decode(output_ids[:index], skip_special_tokens=True).strip("
42 ")
43 content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("
44 ")
45
46 print("thinking content:", thinking_content)
47 print("content:", content)
Quantization Recipe
Install the required packages:
1 pip install git+https://github.com/huggingface/transformers@main
2 pip install --pre torchao --index-url https://download.pytorch.org/whl/nightly/cu126
3 pip install torch
4 pip install accelerate
Use the following code to get the quantized model:
1 import torch
2 from transformers import AutoModelForCausalLM, AutoTokenizer, TorchAoConfig
3
4 model_id = "Qwen/Qwen3-8B"
5 model_to_quantize = "Qwen/Qwen3-8B"
6
7
8 from torchao.quantization import Float8DynamicActivationFloat8WeightConfig, PerRow
9 quant_config = Float8DynamicActivationFloat8WeightConfig(granularity=PerRow())
10 quantization_config = TorchAoConfig(quant_type=quant_config)
11
12 quantized_model = AutoModelForCausalLM.from_pretrained(model_to_quantize, device_map="auto", torch_dtype=torch.bfloat16, quantization_config=quantization_config)
13 tokenizer = AutoTokenizer.from_pretrained(model_id)
14
15 # Push to hub
16 USER_ID = "YOUR_USER_ID"
17 MODEL_NAME = model_id.split("/")[-1]
18 save_to = f"{USER_ID}/{MODEL_NAME}-FP8"
19 quantized_model.push_to_hub(save_to, safe_serialization=False)
20 tokenizer.push_to_hub(save_to)
21
22 # Manual Testing
23 prompt = "Hey, are you conscious? Can you talk to me?"
24 messages = [
25 {
26 "role": "system",
27 "content": "",
28 },
29 {"role": "user", "content": prompt},
30 ]
31 templated_prompt = tokenizer.apply_chat_template(
32 messages,
33 tokenize=False,
34 add_generation_prompt=True,
35 )
36 print("Prompt:", prompt)
37 print("Templated prompt:", templated_prompt)
38 inputs = tokenizer(
39 templated_prompt,
40 return_tensors="pt",
41 ).to("cuda")
42 generated_ids = quantized_model.generate(**inputs, max_new_tokens=128)
43 output_text = tokenizer.batch_decode(
44 generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
45 )
46 print("Response:", output_text[0][len(prompt):])
Note: to push_to_hub you need to run
1 pip install -U "huggingface_hub[cli]"
2 huggingface-cli login
and use a token with write access, from
https://huggingface.co/settings/tokens
Model Quality
We rely on
lm-evaluation-harness to evaluate the quality of the quantized model. Here we only run on mmlu for sanity check.
Benchmark Qwen/Qwen3-8B jerryzh168/Qwen3-8B-FP8 mmlu To be filled To be filled
Reproduce Model Quality Results
baseline
lm_eval --model hf --model_args pretrained=Qwen/Qwen3-8B --tasks mmlu --device cuda:0 --batch_size 8
int4 weight only quantization with hqq (INT4)
1 export MODEL=jerryzh168/Qwen3-8B-FP8
2 lm_eval --model hf --model_args pretrained=$MODEL --tasks mmlu --device cuda:0 --batch_size 8
Peak Memory Usage
Results
Benchmark Qwen/Qwen3-8B jerryzh168/Qwen3-8B-FP8 Peak Memory (GB) To be filled To be filled (?% reduction)
Reproduce Peak Memory Usage Results
We can use the following code to get a sense of peak memory usage during inference:
1 import torch
2 from transformers import AutoModelForCausalLM, AutoTokenizer, TorchAoConfig
3
4 # use "Qwen/Qwen3-8B" or "jerryzh168/Qwen3-8B-FP8"
5 model_id = "jerryzh168/Qwen3-8B-FP8"
6 quantized_model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", torch_dtype=torch.bfloat16)
7 tokenizer = AutoTokenizer.from_pretrained(model_id)
8
9 torch.cuda.reset_peak_memory_stats()
10
11 prompt = "Hey, are you conscious? Can you talk to me?"
12 messages = [
13 {
14 "role": "system",
15 "content": "",
16 },
17 {"role": "user", "content": prompt},
18 ]
19 templated_prompt = tokenizer.apply_chat_template(
20 messages,
21 tokenize=False,
22 add_generation_prompt=True,
23 )
24 print("Prompt:", prompt)
25 print("Templated prompt:", templated_prompt)
26 inputs = tokenizer(
27 templated_prompt,
28 return_tensors="pt",
29 ).to("cuda")
30 generated_ids = quantized_model.generate(**inputs, max_new_tokens=128)
31 output_text = tokenizer.batch_decode(
32 generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
33 )
34 print("Response:", output_text[0][len(prompt):])
35
36 mem = torch.cuda.max_memory_reserved() / 1e9
37 print(f"Peak Memory Usage: {mem:.02f} GB")
Model Performance
Results (A100 machine)
Benchmark (Latency) Qwen/Qwen3-8B jerryzh168/Qwen3-8B-FP8 latency (batch_size=1) ?s ?s (?x speedup)
Reproduce Model Performance Results
Setup
Get vllm source code:
git clone git@github.com:vllm-project/vllm.git
Install vllm
VLLM_USE_PRECOMPILED=1 pip install --editable .
Run the benchmarks under vllm root folder:
benchmark_latency
baseline
1 export MODEL=Qwen/Qwen3-8B
2 python benchmarks/benchmark_latency.py --input-len 256 --output-len 256 --model $MODEL --batch-size 1
INT4
1 export MODEL=jerryzh168/Qwen3-8B-FP8
2 VLLM_DISABLE_COMPILE_CACHE=1 python benchmarks/benchmark_latency.py --input-len 256 --output-len 256 --model $MODEL --batch-size 1
benchmark_serving
We benchmarked the throughput in a serving environment.
Download sharegpt dataset:
wget https://huggingface.co/datasets/anon8231489123/ShareGPT_Vicuna_unfiltered/resolve/main/ShareGPT_V3_unfiltered_cleaned_split.json
Note: you can change the number of prompts to be benchmarked with --num-prompts argument for benchmark_serving script.
baseline
Server:
1 export MODEL=Qwen/Qwen3-8B
2 vllm serve $MODEL --tokenizer $MODEL -O3
Client:
1 export MODEL=Qwen/Qwen3-8B
2 python benchmarks/benchmark_serving.py --backend vllm --dataset-name sharegpt --tokenizer $MODEL --dataset-path ./ShareGPT_V3_unfiltered_cleaned_split.json --model $MODEL --num-prompts 1
FP8
Server:
1 export MODEL=jerryzh168/Qwen3-8B-FP8
2 VLLM_DISABLE_COMPILE_CACHE=1 vllm serve $MODEL --tokenizer $MODEL -O3 --pt-load-map-location cuda:0
Client:
1 export MODEL=jerryzh168/Qwen3-8B-FP8
2 python benchmarks/benchmark_serving.py --backend vllm --dataset-name sharegpt --tokenizer $MODEL --dataset-path ./ShareGPT_V3_unfiltered_cleaned_split.json --model $MODEL --num-prompts 1
Paper: TorchAO: PyTorch-Native Training-to-Serving Model Optimization
The model's quantization is powered by
TorchAO , a framework presented in the paper
TorchAO: PyTorch-Native Training-to-Serving Model Optimization .
Abstract: We present TorchAO, a PyTorch-native model optimization framework leveraging quantization and sparsity to provide an end-to-end, training-to-serving workflow for AI models. TorchAO supports a variety of popular model optimization techniques, including FP8 quantized training, quantization-aware training (QAT), post-training quantization (PTQ), and 2:4 sparsity, and leverages a novel tensor subclass abstraction to represent a variety of widely-used, backend agnostic low precision data types, including INT4, INT8, FP8, MXFP4, MXFP6, and MXFP8. TorchAO integrates closely with the broader ecosystem at each step of the model optimization pipeline, from pre-training (TorchTitan) to fine-tuning (TorchTune, Axolotl) to serving (HuggingFace, vLLM, SGLang, ExecuTorch), connecting an otherwise fragmented space in a single, unified workflow. TorchAO has enabled recent launches of the quantized Llama 3.2 1B/3B and LlamaGuard3-8B models and is open-source at this https URL .
Resources
Disclaimer
PyTorch has not performed safety evaluations or red teamed the quantized models. Performance characteristics, outputs, and behaviors may differ from the original models. Users are solely responsible for selecting appropriate use cases, evaluating and mitigating for accuracy, safety, and fairness, ensuring security, and complying with all applicable laws and regulations.
Nothing contained in this Model Card should be interpreted as or deemed a restriction or modification to the licenses the models are released under, including any limitations of liability or disclaimers of warranties provided therein.