mahiatlinux commited on
Commit
9563d3c
·
verified ·
1 Parent(s): 1ba5586

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +90 -234
README.md CHANGED
@@ -4,22 +4,24 @@ base_model:
4
  - Qwen/Qwen3-30B-A3B
5
  library_name: transformers
6
  ---
 
7
  # Qwen3-30B-A3B-AWQ
8
 
9
- Uploaded by Eric Hartford
10
 
11
- Copied from Modelscope https://www.modelscope.cn/models/swift/Qwen3-30B-A3B-AWQ
12
 
13
- Original model https://huggingface.co/Qwen/Qwen3-30B-A3B
14
 
15
- # Modelscope AWQ Modelcard:
16
 
 
17
  import torch
18
  from modelscope import AutoModelForCausalLM, AutoTokenizer
19
 
20
  model_name = "swift/Qwen3-30B-A3B-AWQ"
21
 
22
- ## load the tokenizer and the model
23
  tokenizer = AutoTokenizer.from_pretrained(model_name)
24
  model = AutoModelForCausalLM.from_pretrained(
25
  model_name,
@@ -27,7 +29,7 @@ model = AutoModelForCausalLM.from_pretrained(
27
  device_map="auto"
28
  )
29
 
30
- ## prepare the model input
31
  prompt = "Give me a short introduction to large language model."
32
  messages = [
33
  {"role": "user", "content": prompt}
@@ -36,18 +38,18 @@ text = tokenizer.apply_chat_template(
36
  messages,
37
  tokenize=False,
38
  add_generation_prompt=True,
39
- enable_thinking=True # Switches between thinking and non-thinking modes. Default is True.
40
  )
41
  model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
42
 
43
- ## conduct text completion
44
  generated_ids = model.generate(
45
  **model_inputs,
46
  max_new_tokens=32768
47
  )
48
- output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
49
 
50
- ## parsing thinking content
51
  try:
52
  # rindex finding 151668 (</think>)
53
  index = len(output_ids) - output_ids[::-1].index(151668)
@@ -59,49 +61,43 @@ content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("
59
 
60
  print("thinking content:", thinking_content)
61
  print("content:", content)
 
62
 
63
  # Original Modelcard
64
 
65
  # Qwen3-30B-A3B
66
- <a href="https://chat.qwen.ai/" target="_blank" style="margin: 2px;">
67
- <img alt="Chat" src="https://img.shields.io/badge/%F0%9F%92%9C%EF%B8%8F%20Qwen%20Chat%20-536af5" style="display: inline-block; vertical-align: middle;"/>
68
- </a>
69
 
70
  ## Qwen3 Highlights
71
 
72
- Qwen3 is the latest generation of large language models in Qwen series, offering a comprehensive suite of dense and mixture-of-experts (MoE) models. Built upon extensive training, Qwen3 delivers groundbreaking advancements in reasoning, instruction-following, agent capabilities, and multilingual support, with the following key features:
73
 
74
- - **Uniquely support of seamless switching between thinking mode** (for complex logical reasoning, math, and coding) and **non-thinking mode** (for efficient, general-purpose dialogue) **within single model**, ensuring optimal performance across various scenarios.
75
- - **Significantly enhancement in its reasoning capabilities**, surpassing previous QwQ (in thinking mode) and Qwen2.5 instruct models (in non-thinking mode) on mathematics, code generation, and commonsense logical reasoning.
76
- - **Superior human preference alignment**, excelling in creative writing, role-playing, multi-turn dialogues, and instruction following, to deliver a more natural, engaging, and immersive conversational experience.
77
- - **Expertise in agent capabilities**, enabling precise integration with external tools in both thinking and unthinking modes and achieving leading performance among open-source models in complex agent-based tasks.
78
- - **Support of 100+ languages and dialects** with strong capabilities for **multilingual instruction following** and **translation**.
79
 
80
  ## Model Overview
81
 
82
  **Qwen3-30B-A3B** has the following features:
83
- - Type: Causal Language Models
84
- - Training Stage: Pretraining & Post-training
85
- - Number of Parameters: 30.5B in total and 3.3B activated
86
- - Number of Paramaters (Non-Embedding): 29.9B
87
- - Number of Layers: 48
88
- - Number of Attention Heads (GQA): 32 for Q and 4 for KV
89
- - Number of Experts: 128
90
- - Number of Activated Experts: 8
91
- - Context Length: 32,768 natively and [131,072 tokens with YaRN](#processing-long-texts).
92
-
93
- For more details, including benchmark evaluation, hardware requirements, and inference performance, please refer to our [blog](https://qwenlm.github.io/blog/qwen3/), [GitHub](https://github.com/QwenLM/Qwen3), and [Documentation](https://qwen.readthedocs.io/en/latest/).
94
 
95
- ## Quickstart
 
 
 
 
 
 
 
96
 
97
- The code of Qwen3-MoE has been in the latest Hugging Face `transformers` and we advise you to use the latest version of `transformers`.
98
 
99
- With `transformers<4.51.0`, you will encounter the following error:
100
- ```
101
- KeyError: 'qwen3_moe'
102
- ```
103
 
104
- The following contains a code snippet illustrating how to use the model generate content based on given inputs.
105
  ```python
106
  from transformers import AutoModelForCausalLM, AutoTokenizer
107
 
@@ -124,7 +120,7 @@ text = tokenizer.apply_chat_template(
124
  messages,
125
  tokenize=False,
126
  add_generation_prompt=True,
127
- enable_thinking=True # Switches between thinking and non-thinking modes. Default is True.
128
  )
129
  model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
130
 
@@ -133,15 +129,13 @@ generated_ids = model.generate(
133
  **model_inputs,
134
  max_new_tokens=32768
135
  )
136
- output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
137
 
138
  # parsing thinking content
139
  try:
140
- # rindex finding 151668 (</think>)
141
  index = len(output_ids) - output_ids[::-1].index(151668)
142
  except ValueError:
143
  index = 0
144
-
145
  thinking_content = tokenizer.decode(output_ids[:index], skip_special_tokens=True).strip("\n")
146
  content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("\n")
147
 
@@ -149,172 +143,71 @@ print("thinking content:", thinking_content)
149
  print("content:", content)
150
  ```
151
 
152
- For deployment, you can use `sglang>=0.4.6.post1` or `vllm>=0.8.5` or to create an OpenAI-compatible API endpoint:
153
- - SGLang:
154
- ```shell
155
- python -m sglang.launch_server --model-path Qwen/Qwen3-30B-A3B --reasoning-parser qwen3
156
- ```
157
- - vLLM:
158
- ```shell
159
- vllm serve Qwen/Qwen3-30B-A3B --enable-reasoning --reasoning-parser deepseek_r1
160
- ```
161
-
162
- For local use, applications such as Ollama, LMStudio, MLX-LM, llama.cpp, and KTransformers have also supported Qwen3.
163
 
164
- ## Switching Between Thinking and Non-Thinking Mode
165
 
166
- > [!TIP]
167
- > The `enable_thinking` switch is also available in APIs created by SGLang and vLLM.
168
- > Please refer to our documentation for [SGLang](https://qwen.readthedocs.io/en/latest/deployment/sglang.html#thinking-non-thinking-modes) and [vLLM](https://qwen.readthedocs.io/en/latest/deployment/vllm.html#thinking-non-thinking-modes) users.
169
-
170
- ### `enable_thinking=True`
171
 
172
- By default, Qwen3 has thinking capabilities enabled, similar to QwQ-32B. This means the model will use its reasoning abilities to enhance the quality of generated responses. For example, when explicitly setting `enable_thinking=True` or leaving it as the default value in `tokenizer.apply_chat_template`, the model will engage its thinking mode.
173
 
174
- ```python
175
- text = tokenizer.apply_chat_template(
176
- messages,
177
- tokenize=False,
178
- add_generation_prompt=True,
179
- enable_thinking=True # True is the default value for enable_thinking
180
- )
181
  ```
182
 
183
- In this mode, the model will generate think content wrapped in a `<think>...</think>` block, followed by the final response.
184
 
185
- > [!NOTE]
186
- > For thinking mode, use `Temperature=0.6`, `TopP=0.95`, `TopK=20`, and `MinP=0` (the default setting in `generation_config.json`). **DO NOT use greedy decoding**, as it can lead to performance degradation and endless repetitions. For more detailed guidance, please refer to the [Best Practices](#best-practices) section.
187
 
 
188
 
189
- ### `enable_thinking=False`
190
 
191
- We provide a hard switch to strictly disable the model's thinking behavior, aligning its functionality with the previous Qwen2.5-Instruct models. This mode is particularly useful in scenarios where disabling thinking is essential for enhancing efficiency.
192
 
193
  ```python
194
  text = tokenizer.apply_chat_template(
195
  messages,
196
  tokenize=False,
197
  add_generation_prompt=True,
198
- enable_thinking=False # Setting enable_thinking=False disables thinking mode
199
  )
200
  ```
201
 
202
- In this mode, the model will not generate any think content and will not include a `<think>...</think>` block.
203
-
204
- > [!NOTE]
205
- > For non-thinking mode, we suggest using `Temperature=0.7`, `TopP=0.8`, `TopK=20`, and `MinP=0`. For more detailed guidance, please refer to the [Best Practices](#best-practices) section.
206
 
207
- ### Advanced Usage: Switching Between Thinking and Non-Thinking Modes via User Input
208
-
209
- We provide a soft switch mechanism that allows users to dynamically control the model's behavior when `enable_thinking=True`. Specifically, you can add `/think` and `/no_think` to user prompts or system messages to switch the model's thinking mode from turn to turn. The model will follow the most recent instruction in multi-turn conversations.
210
-
211
- Here is an example of a multi-turn conversation:
212
 
213
- ```python
214
- from transformers import AutoModelForCausalLM, AutoTokenizer
215
 
216
- class QwenChatbot:
217
- def __init__(self, model_name="Qwen/Qwen3-30B-A3B"):
218
- self.tokenizer = AutoTokenizer.from_pretrained(model_name)
219
- self.model = AutoModelForCausalLM.from_pretrained(model_name)
220
- self.history = []
221
-
222
- def generate_response(self, user_input):
223
- messages = self.history + [{"role": "user", "content": user_input}]
224
-
225
- text = self.tokenizer.apply_chat_template(
226
- messages,
227
- tokenize=False,
228
- add_generation_prompt=True
229
- )
230
-
231
- inputs = self.tokenizer(text, return_tensors="pt")
232
- response_ids = self.model.generate(**inputs, max_new_tokens=32768)[0][len(inputs.input_ids[0]):].tolist()
233
- response = self.tokenizer.decode(response_ids, skip_special_tokens=True)
234
-
235
- # Update history
236
- self.history.append({"role": "user", "content": user_input})
237
- self.history.append({"role": "assistant", "content": response})
238
-
239
- return response
240
-
241
- # Example Usage
242
- if __name__ == "__main__":
243
- chatbot = QwenChatbot()
244
-
245
- # First input (without /think or /no_think tags, thinking mode is enabled by default)
246
- user_input_1 = "How many r's in strawberries?"
247
- print(f"User: {user_input_1}")
248
- response_1 = chatbot.generate_response(user_input_1)
249
- print(f"Bot: {response_1}")
250
- print("----------------------")
251
-
252
- # Second input with /no_think
253
- user_input_2 = "Then, how many r's in blueberries? /no_think"
254
- print(f"User: {user_input_2}")
255
- response_2 = chatbot.generate_response(user_input_2)
256
- print(f"Bot: {response_2}")
257
- print("----------------------")
258
-
259
- # Third input with /think
260
- user_input_3 = "Really? /think"
261
- print(f"User: {user_input_3}")
262
- response_3 = chatbot.generate_response(user_input_3)
263
- print(f"Bot: {response_3}")
264
- ```
265
 
266
- > [!NOTE]
267
- > For API compatibility, when `enable_thinking=True`, regardless of whether the user uses `/think` or `/no_think`, the model will always output a block wrapped in `<think>...</think>`. However, the content inside this block may be empty if thinking is disabled.
268
- > When `enable_thinking=False`, the soft switches are not valid. Regardless of any `/think` or `/no_think` tags input by the user, the model will not generate think content and will not include a `<think>...</think>` block.
269
 
270
  ## Agentic Use
271
 
272
- Qwen3 excels in tool calling capabilities. We recommend using [Qwen-Agent](https://github.com/QwenLM/Qwen-Agent) to make the best use of agentic ability of Qwen3. Qwen-Agent encapsulates tool-calling templates and tool-calling parsers internally, greatly reducing coding complexity.
273
 
274
- To define the available tools, you can use the MCP configuration file, use the integrated tool of Qwen-Agent, or integrate other tools by yourself.
275
  ```python
276
  from qwen_agent.agents import Assistant
277
 
278
- # Define LLM
279
  llm_cfg = {
280
  'model': 'Qwen3-30B-A3B',
281
-
282
- # Use the endpoint provided by Alibaba Model Studio:
283
- # 'model_type': 'qwen_dashscope',
284
- # 'api_key': os.getenv('DASHSCOPE_API_KEY'),
285
-
286
- # Use a custom endpoint compatible with OpenAI API:
287
- 'model_server': 'http://localhost:8000/v1', # api_base
288
  'api_key': 'EMPTY',
289
-
290
- # Other parameters:
291
- # 'generate_cfg': {
292
- # # Add: When the response content is `<think>this is the thought</think>this is the answer;
293
- # # Do not add: When the response has been separated by reasoning_content and content.
294
- # 'thought_in_content': True,
295
- # },
296
  }
297
 
298
- # Define Tools
299
  tools = [
300
- {'mcpServers': { # You can specify the MCP configuration file
301
- 'time': {
302
- 'command': 'uvx',
303
- 'args': ['mcp-server-time', '--local-timezone=Asia/Shanghai']
304
- },
305
- "fetch": {
306
- "command": "uvx",
307
- "args": ["mcp-server-fetch"]
308
- }
309
- }
310
- },
311
- 'code_interpreter', # Built-in tools
312
  ]
313
 
314
- # Define Agent
315
  bot = Assistant(llm=llm_cfg, function_list=tools)
316
-
317
- # Streaming generation
318
  messages = [{'role': 'user', 'content': 'https://qwenlm.github.io/blog/ Introduce the latest developments of Qwen'}]
319
  for responses in bot.run(messages=messages):
320
  pass
@@ -323,81 +216,44 @@ print(responses)
323
 
324
  ## Processing Long Texts
325
 
326
- Qwen3 natively supports context lengths of up to 32,768 tokens. For conversations where the total length (including both input and output) significantly exceeds this limit, we recommend using RoPE scaling techniques to handle long texts effectively. We have validated the model's performance on context lengths of up to 131,072 tokens using the [YaRN](https://arxiv.org/abs/2309.00071) method.
327
-
328
- YaRN is currently supported by several inference frameworks, e.g., `transformers` and `llama.cpp` for local use, `vllm` and `sglang` for deployment. In general, there are two approaches to enabling YaRN for supported frameworks:
329
-
330
- - Modifying the model files:
331
- In the `config.json` file, add the `rope_scaling` fields:
332
- ```json
333
- {
334
- ...,
335
- "rope_scaling": {
336
- "rope_type": "yarn",
337
- "factor": 4.0,
338
- "original_max_position_embeddings": 32768
339
- }
340
- }
341
- ```
342
- For `llama.cpp`, you need to regenerate the GGUF file after the modification.
343
-
344
- - Passing command line arguments:
345
-
346
- For `vllm`, you can use
347
- ```shell
348
- vllm serve ... --rope-scaling '{"rope_type":"yarn","factor":4.0,"original_max_position_embeddings":32768}' --max-model-len 131072
349
- ```
350
-
351
- For `sglang`, you can use
352
- ```shell
353
- python -m sglang.launch_server ... --json-model-override-args '{"rope_scaling":{"rope_type":"yarn","factor":4.0,"original_max_position_embeddings":32768}}'
354
- ```
355
-
356
- For `llama-server` from `llama.cpp`, you can use
357
- ```shell
358
- llama-server ... --rope-scaling yarn --rope-scale 4 --yarn-orig-ctx 32768
359
- ```
360
-
361
- > [!IMPORTANT]
362
- > If you encounter the following warning
363
- > ```
364
- > Unrecognized keys in `rope_scaling` for 'rope_type'='yarn': {'original_max_position_embeddings'}
365
- > ```
366
- > please upgrade `transformers>=4.51.0`.
367
-
368
- > [!NOTE]
369
- > All the notable open-source frameworks implement static YaRN, which means the scaling factor remains constant regardless of input length, **potentially impacting performance on shorter texts.**
370
- > We advise adding the `rope_scaling` configuration only when processing long contexts is required.
371
- > It is also recommended to modify the `factor` as needed. For example, if the typical context length for your application is 65,536 tokens, it would be better to set `factor` as 2.0.
372
-
373
- > [!NOTE]
374
- > The default `max_position_embeddings` in `config.json` is set to 40,960. This allocation includes reserving 32,768 tokens for outputs and 8,192 tokens for typical prompts, which is sufficient for most scenarios involving short text processing. If the average context length does not exceed 32,768 tokens, we do not recommend enabling YaRN in this scenario, as it may potentially degrade model performance.
375
-
376
- > [!TIP]
377
- > The endpoint provided by Alibaba Model Studio supports dynamic YaRN by default and no extra configuration is needed.
378
 
379
- ## Best Practices
 
 
 
 
 
 
 
 
380
 
381
- To achieve optimal performance, we recommend the following settings:
382
 
383
- 1. **Sampling Parameters**:
384
- - For thinking mode (`enable_thinking=True`), use `Temperature=0.6`, `TopP=0.95`, `TopK=20`, and `MinP=0`. **DO NOT use greedy decoding**, as it can lead to performance degradation and endless repetitions.
385
- - For non-thinking mode (`enable_thinking=False`), we suggest using `Temperature=0.7`, `TopP=0.8`, `TopK=20`, and `MinP=0`.
386
- - For supported frameworks, you can adjust the `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.
 
 
 
 
 
387
 
388
- 2. **Adequate Output Length**: We recommend using an output length of 32,768 tokens for most queries. For benchmarking on highly complex problems, such as those found in math and programming competitions, we suggest setting the max output length to 38,912 tokens. This provides the model with sufficient space to generate detailed and comprehensive responses, thereby enhancing its overall performance.
389
 
390
- 3. **Standardize Output Format**: We recommend using prompts to standardize model outputs when benchmarking.
391
- - **Math Problems**: Include "Please reason step by step, and put your final answer within \boxed{}." in the prompt.
392
- - **Multiple-Choice Questions**: Add the following JSON structure to the prompt to standardize responses: "Please show your choice in the `answer` field with only the choice letter, e.g., `"answer": "C"`."
393
 
394
- 4. **No Thinking Content in History**: In multi-turn conversations, the historical model output should only include the final output part and does not need to include the thinking content. It is implemented in the provided chat template in Jinja2. However, for frameworks that do not directly use the Jinja2 chat template, it is up to the developers to ensure that the best practice is followed.
395
 
396
- ### Citation
 
 
 
397
 
398
- If you find our work helpful, feel free to give us a cite.
399
 
400
- ```
401
  @misc{qwen3,
402
  title = {Qwen3},
403
  url = {https://qwenlm.github.io/blog/qwen3/},
 
4
  - Qwen/Qwen3-30B-A3B
5
  library_name: transformers
6
  ---
7
+
8
  # Qwen3-30B-A3B-AWQ
9
 
10
+ *Uploaded by Eric Hartford*
11
 
12
+ Copied from Modelscope [Modelscope link](https://www.modelscope.cn/models/swift/Qwen3-30B-A3B-AWQ)
13
 
14
+ Original model: [huggingface.co/Qwen/Qwen3-30B-A3B](https://huggingface.co/Qwen/Qwen3-30B-A3B)
15
 
16
+ ## Modelscope AWQ Modelcard
17
 
18
+ ```python
19
  import torch
20
  from modelscope import AutoModelForCausalLM, AutoTokenizer
21
 
22
  model_name = "swift/Qwen3-30B-A3B-AWQ"
23
 
24
+ # load the tokenizer and the model
25
  tokenizer = AutoTokenizer.from_pretrained(model_name)
26
  model = AutoModelForCausalLM.from_pretrained(
27
  model_name,
 
29
  device_map="auto"
30
  )
31
 
32
+ # prepare the model input
33
  prompt = "Give me a short introduction to large language model."
34
  messages = [
35
  {"role": "user", "content": prompt}
 
38
  messages,
39
  tokenize=False,
40
  add_generation_prompt=True,
41
+ enable_thinking=True # Switches between thinking and non-thinking modes. Default is True.
42
  )
43
  model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
44
 
45
+ # conduct text completion
46
  generated_ids = model.generate(
47
  **model_inputs,
48
  max_new_tokens=32768
49
  )
50
+ output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
51
 
52
+ # parsing thinking content
53
  try:
54
  # rindex finding 151668 (</think>)
55
  index = len(output_ids) - output_ids[::-1].index(151668)
 
61
 
62
  print("thinking content:", thinking_content)
63
  print("content:", content)
64
+ ```
65
 
66
  # Original Modelcard
67
 
68
  # Qwen3-30B-A3B
69
+
70
+ [Chat interface](https://chat.qwen.ai/) <img alt="Chat" src="https://img.shields.io/badge/%F0%9F%92%9C%EF%B8%8F%20Qwen%20Chat%20-536af5" style="display: inline-block; vertical-align: middle;" />
 
71
 
72
  ## Qwen3 Highlights
73
 
74
+ Qwen3 is the latest generation of large language models in the Qwen series, offering a comprehensive suite of dense and mixture-of-experts (MoE) models. Built upon extensive training, Qwen3 delivers groundbreaking advancements in reasoning, instruction-following, agent capabilities, and multilingual support, with the following key features:
75
 
76
+ * **Seamless switching between thinking mode** (for complex logical reasoning, math, and coding) and **non-thinking mode** (for efficient, general-purpose dialogue) within a single model, ensuring optimal performance across various scenarios.
77
+ * **Enhanced reasoning capabilities**, surpassing previous QwQ (in thinking mode) and Qwen2.5 instruct models (in non-thinking mode) on mathematics, code generation, and commonsense logical reasoning.
78
+ * **Superior human preference alignment**, excelling in creative writing, role-playing, multi-turn dialogues, and instruction following, to deliver a more natural, engaging, and immersive conversational experience.
79
+ * **Expertise in agent capabilities**, enabling precise integration with external tools in both thinking and non-thinking modes and achieving leading performance among open-source models in complex agent-based tasks.
80
+ * **Support for 100+ languages and dialects** with strong capabilities for **multilingual instruction following** and **translation**.
81
 
82
  ## Model Overview
83
 
84
  **Qwen3-30B-A3B** has the following features:
 
 
 
 
 
 
 
 
 
 
 
85
 
86
+ * **Type**: Causal Language Model
87
+ * **Training Stage**: Pretraining & Post-training
88
+ * **Number of Parameters**: 30.5B total, 3.3B activated
89
+ * **Non-Embedding Parameters**: 29.9B
90
+ * **Layers**: 48
91
+ * **Attention Heads (GQA)**: 32 for Q, 4 for KV
92
+ * **Experts**: 128 (8 activated)
93
+ * **Context Length**: 32,768 tokens natively (**131,072 tokens with YaRN**)
94
 
95
+ For more details, including benchmark evaluation, hardware requirements, and inference performance, refer to our [blog](https://qwenlm.github.io/blog/qwen3/), [GitHub](https://github.com/QwenLM/Qwen3), and [documentation](https://qwen.readthedocs.io/en/latest/).
96
 
97
+ ## Quickstart
98
+
99
+ > **Note:** Use `transformers>=4.51.0` to avoid `KeyError: 'qwen3_moe'`.
 
100
 
 
101
  ```python
102
  from transformers import AutoModelForCausalLM, AutoTokenizer
103
 
 
120
  messages,
121
  tokenize=False,
122
  add_generation_prompt=True,
123
+ enable_thinking=True # Switches between thinking and non-thinking modes. Default is True.
124
  )
125
  model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
126
 
 
129
  **model_inputs,
130
  max_new_tokens=32768
131
  )
132
+ output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
133
 
134
  # parsing thinking content
135
  try:
 
136
  index = len(output_ids) - output_ids[::-1].index(151668)
137
  except ValueError:
138
  index = 0
 
139
  thinking_content = tokenizer.decode(output_ids[:index], skip_special_tokens=True).strip("\n")
140
  content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("\n")
141
 
 
143
  print("content:", content)
144
  ```
145
 
146
+ ## Deployment
 
 
 
 
 
 
 
 
 
 
147
 
148
+ * **SGLang**:
149
 
150
+ ```shell
151
+ python -m sglang.launch_server --model-path Qwen/Qwen3-30B-A3B --reasoning-parser qwen3
152
+ ```
 
 
153
 
154
+ * **vLLM**:
155
 
156
+ ```shell
157
+ vllm serve Qwen/Qwen3-30B-A3B --enable-reasoning --reasoning-parser deepseek_r1
 
 
 
 
 
158
  ```
159
 
160
+ Local use supported by Ollama, LMStudio, MLX-LM, llama.cpp, and KTransformers.
161
 
162
+ ## Switching Between Thinking and Non-Thinking Modes
 
163
 
164
+ > **Tip:** Use `enable_thinking` in `tokenizer.apply_chat_template` or soft switches `/think` and `/no_think` in prompts.
165
 
166
+ ### `enable_thinking=True`
167
 
168
+ The model generates reasoning wrapped in `<think>...</think>` followed by the final response.
169
 
170
  ```python
171
  text = tokenizer.apply_chat_template(
172
  messages,
173
  tokenize=False,
174
  add_generation_prompt=True,
175
+ enable_thinking=True
176
  )
177
  ```
178
 
179
+ > **Note:** For thinking mode, use `Temperature=0.6`, `TopP=0.95`, `TopK=20`, `MinP=0`. Do **not** use greedy decoding.
 
 
 
180
 
181
+ ### `enable_thinking=False`
 
 
 
 
182
 
183
+ No `<think>...</think>` blocks are produced. Recommended settings: `Temperature=0.7`, `TopP=0.8`, `TopK=20`, `MinP=0`.
 
184
 
185
+ ### Soft Switches
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
 
187
+ Add `/think` or `/no_think` to prompts when `enable_thinking=True`; ignored otherwise.
 
 
188
 
189
  ## Agentic Use
190
 
191
+ Qwen3 excels in tool calling. Use [Qwen-Agent](https://github.com/QwenLM/Qwen-Agent) for seamless integration.
192
 
 
193
  ```python
194
  from qwen_agent.agents import Assistant
195
 
 
196
  llm_cfg = {
197
  'model': 'Qwen3-30B-A3B',
198
+ 'model_server': 'http://localhost:8000/v1',
 
 
 
 
 
 
199
  'api_key': 'EMPTY',
 
 
 
 
 
 
 
200
  }
201
 
 
202
  tools = [
203
+ {'mcpServers': {
204
+ 'time': {'command': 'uvx', 'args': ['mcp-server-time', '--local-timezone=Asia/Shanghai']},
205
+ 'fetch': {'command': 'uvx', 'args': ['mcp-server-fetch']}
206
+ }},
207
+ 'code_interpreter',
 
 
 
 
 
 
 
208
  ]
209
 
 
210
  bot = Assistant(llm=llm_cfg, function_list=tools)
 
 
211
  messages = [{'role': 'user', 'content': 'https://qwenlm.github.io/blog/ Introduce the latest developments of Qwen'}]
212
  for responses in bot.run(messages=messages):
213
  pass
 
216
 
217
  ## Processing Long Texts
218
 
219
+ Qwen3 supports up to 32,768 tokens natively and 131,072 with YaRN \[ArXiv:2309.00071].
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
220
 
221
+ ### Enabling YaRN
222
+
223
+ * **Transformers config**:
224
+
225
+ ```json
226
+ {
227
+ "rope_scaling": {"rope_type": "yarn", "factor": 4.0, "original_max_position_embeddings": 32768}
228
+ }
229
+ ```
230
 
231
+ * **vLLM**:
232
 
233
+ ```shell
234
+ vllm serve ... --rope-scaling '{"rope_type":"yarn","factor":4.0,"original_max_position_embeddings":32768}' --max-model-len 131072
235
+ ```
236
+
237
+ * **llama.cpp**:
238
+
239
+ ```shell
240
+ llama-server ... --rope-scaling yarn --rope-scale 4 --yarn-orig-ctx 32768
241
+ ```
242
 
243
+ > **Important:** Upgrade to `transformers>=4.51.0` to avoid warnings.
244
 
245
+ > **Tip:** Only enable YaRN when needed; default 40,960 embeddings suffice for most scenarios.
 
 
246
 
247
+ ## Best Practices
248
 
249
+ 1. **Sampling**: Thinking mode `T=0.6 P=0.95 K=20 MinP=0`; non-thinking `T=0.7 P=0.8 K=20 MinP=0`.
250
+ 2. **Output Length**: 32,768 tokens standard, 38,912 for complex tasks.
251
+ 3. **Format**: Math in `\boxed{}`, MCQs via JSON answers.
252
+ 4. **History**: Exclude `<think>` blocks.
253
 
254
+ ## Citation
255
 
256
+ ```bibtex
257
  @misc{qwen3,
258
  title = {Qwen3},
259
  url = {https://qwenlm.github.io/blog/qwen3/},