Views
No views yet
██╗ ██╗██╗ ██╗ █████╗ ██╗ ██████╗
██║ ██║██║ ██║██╔══██╗███║██╔════╝
██║ █╗ ██║███████║███████║╚██║███████╗
██║███╗██║╚════██║██╔══██║ ██║██╔═══██╗
╚███╔███╔╝ ██║██║ ██║ ██║╚██████╔╝
╚══╝╚══╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═════╝
🗜️ COMPRESSED & OPTIMIZED 🚀1from datasets import load_dataset
2from llmcompressor.modifiers.quantization import GPTQModifier
3from llmcompressor import oneshot
4from transformers import AutoModelForCausalLM, AutoTokenizer
5
6# Load model with memory management
7model_stub = "Qwen/Qwen3-30B-A3B-Thinking-2507"
8model_name = model_stub.split("/")[-1]
9
10# Use conservative parameters
11num_samples = 1024
12max_seq_len = 8192
13
14print(f"Loading model: {model_stub}")
15model = AutoModelForCausalLM.from_pretrained(
16 model_stub,
17 torch_dtype="auto",
18 device_map="auto",
19 low_cpu_mem_usage=True,
20 max_memory={0: "44GB", "cpu": "55GB"},
21)
22
23print("Loading tokenizer...")
24tokenizer = AutoTokenizer.from_pretrained(model_stub)
25
26print("Loading calibration dataset...")
27def preprocess_fn(example):
28 return {"text": tokenizer.apply_chat_template(
29 example["messages"],
30 add_generation_prompt=False,
31 tokenize=False
32 )}
33
34# Load dataset and preprocess
35ds = load_dataset("neuralmagic/LLM_compression_calibration", split=f"train[:{num_samples}]")
36ds = ds.map(preprocess_fn)
37ds = ds.shuffle(seed=42)
38
39# Tokenize the dataset
40def tokenize(sample):
41 return tokenizer(
42 sample["text"],
43 padding=False,
44 max_length=max_seq_len,
45 truncation=True,
46 add_special_tokens=False,
47 )
48
49print("Tokenizing dataset...")
50ds = ds.map(tokenize, remove_columns=ds.column_names)
51
52# Configure GPTQ with proper Qwen3 MoE ignore patterns
53print("Configuring quantization recipe...")
54recipe = GPTQModifier(
55 targets="Linear",
56 scheme="W4A16",
57 ignore=["lm_head", "re:.*mlp.gate$"], # Qwen3 MoE pattern (no shared experts)
58 dampening_frac=0.01,
59 # Remove sequential_targets - let llmcompressor handle automatically
60)
61
62# Apply quantization
63print("Starting quantization process...")
64oneshot(
65 model=model,
66 dataset=ds,
67 recipe=recipe,
68 max_seq_length=max_seq_len,
69 num_calibration_samples=num_samples,
70)
71
72# Save quantized model
73save_path = model_name + "-gptq-w4a16"
74print(f"Saving model to: {save_path}")
75model.save_pretrained(save_path, save_compressed=True)
76tokenizer.save_pretrained(save_path)
77
78print("Quantization completed successfully!")
enable_thinking=True is no longer required.<think>. Therefore, it is normal for the model's output to contain only </think> without an explicit opening <think> tag.| Gemini2.5-Flash-Thinking | Qwen3-235B-A22B Thinking | Qwen3-30B-A3B Thinking | Qwen3-30B-A3B-Thinking-2507 | |
|---|---|---|---|---|
| Knowledge | ||||
| MMLU-Pro | 81.9 | 82.8 | 78.5 | 80.9 |
| MMLU-Redux | 92.1 | 92.7 | 89.5 | 91.4 |
| GPQA | 82.8 | 71.1 | 65.8 | 73.4 |
| SuperGPQA | 57.8 | 60.7 | 51.8 | 56.8 |
| Reasoning | ||||
| AIME25 | 72.0 | 81.5 | 70.9 | 85.0 |
| HMMT25 | 64.2 | 62.5 | 49.8 | 71.4 |
| LiveBench 20241125 | 74.3 | 77.1 | 74.3 | 76.8 |
| Coding | ||||
| LiveCodeBench v6 (25.02-25.05) | 61.2 | 55.7 | 57.4 | 66.0 |
| CFEval | 1995 | 2056 | 1940 | 2044 |
| OJBench | 23.5 | 25.6 | 20.7 | 25.1 |
| Alignment | ||||
| IFEval | 89.8 | 83.4 | 86.5 | 88.9 |
| Arena-Hard v2$ | 56.7 | 61.5 | 36.3 | 56.0 |
| Creative Writing v3 | 85.0 | 84.6 | 79.1 | 84.4 |
| WritingBench | 83.9 | 80.3 | 77.0 | 85.0 |
| Agent | ||||
| BFCL-v3 | 68.6 | 70.8 | 69.1 | 72.4 |
| TAU1-Retail | 65.2 | 54.8 | 61.7 | 67.8 |
| TAU1-Airline | 54.0 | 26.0 | 32.0 | 48.0 |
| TAU2-Retail | 66.7 | 40.4 | 34.2 | 58.8 |
| TAU2-Airline | 52.0 | 30.0 | 36.0 | 58.0 |
| TAU2-Telecom | 31.6 | 21.9 | 22.8 | 26.3 |
| Multilingualism | ||||
| MultiIF | 74.4 | 71.9 | 72.2 | 76.4 |
| MMLU-ProX | 80.2 | 80.0 | 73.1 | 76.4 |
| INCLUDE | 83.9 | 78.7 | 71.9 | 74.4 |
| PolyMATH | 49.8 | 54.7 | 46.1 | 52.6 |
transformers and we advise you to use the latest version of transformers.transformers<4.51.0, you will encounter the following error:KeyError: 'qwen3_moe'1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model_name = "Qwen/Qwen3-30B-A3B-Thinking-2507"
4
5# load the tokenizer and the model
6tokenizer = AutoTokenizer.from_pretrained(model_name)
7model = AutoModelForCausalLM.from_pretrained(
8 model_name,
9 torch_dtype="auto",
10 device_map="auto"
11)
12
13# prepare the model input
14prompt = "Give me a short introduction to large language model."
15messages = [
16 {"role": "user", "content": prompt}
17]
18text = tokenizer.apply_chat_template(
19 messages,
20 tokenize=False,
21 add_generation_prompt=True,
22)
23model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
24
25# conduct text completion
26generated_ids = model.generate(
27 **model_inputs,
28 max_new_tokens=32768
29)
30output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
31
32# parsing thinking content
33try:
34 # rindex finding 151668 (</think>)
35 index = len(output_ids) - output_ids[::-1].index(151668)
36except ValueError:
37 index = 0
38
39thinking_content = tokenizer.decode(output_ids[:index], skip_special_tokens=True).strip("\n")
40content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("\n")
41
42print("thinking content:", thinking_content) # no opening <think> tag
43print("content:", content)
44sglang>=0.4.6.post1 or vllm>=0.8.5 or to create an OpenAI-compatible API endpoint:python -m sglang.launch_server --model-path Qwen/Qwen3-30B-A3B-Thinking-2507 --context-length 262144 --reasoning-parser deepseek-r1vllm serve Qwen/Qwen3-30B-A3B-Thinking-2507 --max-model-len 262144 --enable-reasoning --reasoning-parser deepseek_r11from qwen_agent.agents import Assistant
2
3# Define LLM
4# Using Alibaba Cloud Model Studio
5llm_cfg = {
6 'model': 'qwen3-30b-a3b-thinking-2507',
7 'model_type': 'qwen_dashscope',
8}
9
10# Using OpenAI-compatible API endpoint. It is recommended to disable the reasoning and the tool call parsing
11# functionality of the deployment frameworks and let Qwen-Agent automate the related operations. For example,
12# `VLLM_USE_MODELSCOPE=true vllm serve Qwen/Qwen3-30B-A3B-Thinking-2507 --served-model-name Qwen3-30B-A3B-Thinking-2507 --tensor-parallel-size 8 --max-model-len 262144`.
13#
14# llm_cfg = {
15# 'model': 'Qwen3-30B-A3B-Thinking-2507',
16#
17# # Use a custom endpoint compatible with OpenAI API:
18# 'model_server': 'http://localhost:8000/v1', # api_base without reasoning and tool call parsing
19# 'api_key': 'EMPTY',
20# 'generate_cfg': {
21# 'thought_in_content': True,
22# },
23# }
24
25
26# Define Tools
27tools = [
28 {'mcpServers': { # You can specify the MCP configuration file
29 'time': {
30 'command': 'uvx',
31 'args': ['mcp-server-time', '--local-timezone=Asia/Shanghai']
32 },
33 "fetch": {
34 "command": "uvx",
35 "args": ["mcp-server-fetch"]
36 }
37 }
38 },
39 'code_interpreter', # Built-in tools
40]
41
42# Define Agent
43bot = Assistant(llm=llm_cfg, function_list=tools)
44
45# Streaming generation
46messages = [{'role': 'user', 'content': 'https://qwenlm.github.io/blog/ Introduce the latest developments of Qwen'}]
47for responses in bot.run(messages=messages):
48 pass
49print(responses)Temperature=0.6, TopP=0.95, TopK=20, and MinP=0.presence_penalty parameter between 0 and 2 to reduce endless repetitions. However, using a higher value may occasionally result in language mixing and a slight decrease in model performance.answer field with only the choice letter, e.g., "answer": "C"."@misc{qwen3technicalreport,
title={Qwen3 Technical Report},
author={Qwen Team},
year={2025},
eprint={2505.09388},
archivePrefix={arXiv},
primaryClass={cs.CL},
url={https://arxiv.org/abs/2505.09388},
}