Views
No views yet
linear_attn.out_proj, self_attn.o_proj
1from transformers import AutoModelForCausalLM, AutoTokenizer
2from datasets import load_dataset
3from quark.torch import LLMTemplate, ModelQuantizer, export_safetensors
4from quark.contrib.llm_eval import ppl_eval
5
6# Register qwen3_next template
7qwen3_next_template = LLMTemplate(
8 model_type="qwen3_next",
9 kv_layers_name=["*qkvz"],
10 q_layer_name="*qkvz",
11 exclude_layers_name=["lm_head", "*linear_attn.in_proj_ba", "*linear_attn.in_proj_qkvz","*mlp.gate", "*mlp.shared_expert_gate", "*self_attn.k_proj", "*self_attn.q_proj", "*self_attn.v_proj"],
12)
13LLMTemplate.register_template(qwen3_next_template)
14
15# Configuration
16ckpt_path = "Qwen/Qwen3-Coder-Next"
17output_dir = "amd/Qwen3-Coder-Next-MXFP4"
18quant_scheme = "mxfp4"
19exclude_layers = ["lm_head", "*linear_attn.in_proj_ba", "*linear_attn.in_proj_qkvz","*mlp.gate", "*mlp.shared_expert_gate", "*self_attn.k_proj", "*self_attn.q_proj", "*self_attn.v_proj"]
20
21# Load model
22tokenizer = AutoTokenizer.from_pretrained(ckpt_path, trust_remote_code=True)
23model = AutoModelForCausalLM.from_pretrained(ckpt_path, torch_dtype="auto", device_map="auto")
24model.eval()
25
26# Get quant config from template
27template = LLMTemplate.get(model.config.model_type)
28quant_config = template.get_config(scheme=quant_scheme, exclude_layers=exclude_layers)
29
30# Quantize
31quantizer = ModelQuantizer(quant_config)
32model = quantizer.quantize_model(model)
33model = quantizer.freeze(model)
34
35# Export hf_format
36export_safetensors(model, output_dir, custom_mode="quark")
37tokenizer.save_pretrained(output_dir)
38
39# Evaluate PPL (optional)
40testdata = load_dataset("wikitext", "wikitext-2-raw-v1", split="test")
41testenc = tokenizer("\n\n".join(testdata["text"]), return_tensors="pt")
42ppl = ppl_eval(model, testenc, model.device)
43print(f"Perplexity: {ppl.item()}")| Benchmark | Qwen3-Coder-Next | Qwen3-Coder-Next-MXFP4(this model) | Recovery |
| GSM8K (flexible-extract) | 94.54 | 93.25 | 98.6% |
lm-evaluation-harness framework, based on the Docker image vllm/vllm-openai-rocm:v0.14.0.(commit ecb4f822091a64b5084b3a4aff326906487a363f) and lm-eval (Version: 0.4.10) in container first.git clone https://github.com/vllm-project/vllm.git
cd vllm
python3 setup.py develop
pip install lm-evalMODEL=amd/Qwen3-Coder-Next-MXFP4
SAFETENSORS_FAST_GPU=1 \
VLLM_ALLOW_LONG_MAX_MODEL_LEN=1 \
vllm serve $MODEL \
--tensor-parallel-size 4 \
--reasoning-parser qwen3 \
--enable-auto-tool-choice \
--tool-call-parser qwen3_coder \
--trust-remote-codelm_eval \
--model local-completions \
--model_args "model=amd/Qwen3-Coder-Next-MXFP4,base_url=http://localhost:8000/v1/completions,num_concurrent=256,max_retries=10,max_gen_toks=2048,tokenized_requests=False,tokenizer_backend=None" \
--tasks gsm8k \
--num_fewshot 5 \
--batch_size auto