Instructions to use unsloth/Qwen3-8B-128K-GGUF with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use unsloth/Qwen3-8B-128K-GGUF with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="unsloth/Qwen3-8B-128K-GGUF") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForMultimodalLM tokenizer = AutoTokenizer.from_pretrained("unsloth/Qwen3-8B-128K-GGUF") model = AutoModelForMultimodalLM.from_pretrained("unsloth/Qwen3-8B-128K-GGUF") - llama-cpp-python
How to use unsloth/Qwen3-8B-128K-GGUF with llama-cpp-python:
# !pip install llama-cpp-python from llama_cpp import Llama llm = Llama.from_pretrained( repo_id="unsloth/Qwen3-8B-128K-GGUF", filename="Qwen3-8B-128K-BF16.gguf", )
llm.create_chat_completion( messages = [ { "role": "user", "content": "What is the capital of France?" } ] ) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- llama.cpp
How to use unsloth/Qwen3-8B-128K-GGUF with llama.cpp:
Install from brew
brew install llama.cpp # Start a local OpenAI-compatible server with a web UI: llama-server -hf unsloth/Qwen3-8B-128K-GGUF:UD-Q4_K_XL # Run inference directly in the terminal: llama-cli -hf unsloth/Qwen3-8B-128K-GGUF:UD-Q4_K_XL
Install from WinGet (Windows)
winget install llama.cpp # Start a local OpenAI-compatible server with a web UI: llama-server -hf unsloth/Qwen3-8B-128K-GGUF:UD-Q4_K_XL # Run inference directly in the terminal: llama-cli -hf unsloth/Qwen3-8B-128K-GGUF:UD-Q4_K_XL
Use pre-built binary
# Download pre-built binary from: # https://github.com/ggerganov/llama.cpp/releases # Start a local OpenAI-compatible server with a web UI: ./llama-server -hf unsloth/Qwen3-8B-128K-GGUF:UD-Q4_K_XL # Run inference directly in the terminal: ./llama-cli -hf unsloth/Qwen3-8B-128K-GGUF:UD-Q4_K_XL
Build from source code
git clone https://github.com/ggerganov/llama.cpp.git cd llama.cpp cmake -B build cmake --build build -j --target llama-server llama-cli # Start a local OpenAI-compatible server with a web UI: ./build/bin/llama-server -hf unsloth/Qwen3-8B-128K-GGUF:UD-Q4_K_XL # Run inference directly in the terminal: ./build/bin/llama-cli -hf unsloth/Qwen3-8B-128K-GGUF:UD-Q4_K_XL
Use Docker
docker model run hf.co/unsloth/Qwen3-8B-128K-GGUF:UD-Q4_K_XL
- LM Studio
- Jan
- vLLM
How to use unsloth/Qwen3-8B-128K-GGUF with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "unsloth/Qwen3-8B-128K-GGUF" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "unsloth/Qwen3-8B-128K-GGUF", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/unsloth/Qwen3-8B-128K-GGUF:UD-Q4_K_XL
- SGLang
How to use unsloth/Qwen3-8B-128K-GGUF with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "unsloth/Qwen3-8B-128K-GGUF" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "unsloth/Qwen3-8B-128K-GGUF", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "unsloth/Qwen3-8B-128K-GGUF" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "unsloth/Qwen3-8B-128K-GGUF", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Ollama
How to use unsloth/Qwen3-8B-128K-GGUF with Ollama:
ollama run hf.co/unsloth/Qwen3-8B-128K-GGUF:UD-Q4_K_XL
- Unsloth Studio
How to use unsloth/Qwen3-8B-128K-GGUF with Unsloth Studio:
Install Unsloth Studio (macOS, Linux, WSL)
curl -fsSL https://unsloth.ai/install.sh | sh # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for unsloth/Qwen3-8B-128K-GGUF to start chatting
Install Unsloth Studio (Windows)
irm https://unsloth.ai/install.ps1 | iex # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for unsloth/Qwen3-8B-128K-GGUF to start chatting
Using HuggingFace Spaces for Unsloth
# No setup required # Open https://huggingface.co/spaces/unsloth/studio in your browser # Search for unsloth/Qwen3-8B-128K-GGUF to start chatting
- Pi
How to use unsloth/Qwen3-8B-128K-GGUF with Pi:
Start the llama.cpp server
# Install llama.cpp: brew install llama.cpp # Start a local OpenAI-compatible server: llama-server -hf unsloth/Qwen3-8B-128K-GGUF:UD-Q4_K_XL
Configure the model in Pi
# Install Pi: npm install -g @mariozechner/pi-coding-agent # Add to ~/.pi/agent/models.json: { "providers": { "llama-cpp": { "baseUrl": "http://localhost:8080/v1", "api": "openai-completions", "apiKey": "none", "models": [ { "id": "unsloth/Qwen3-8B-128K-GGUF:UD-Q4_K_XL" } ] } } }Run Pi
# Start Pi in your project directory: pi
- Hermes Agent new
How to use unsloth/Qwen3-8B-128K-GGUF with Hermes Agent:
Start the llama.cpp server
# Install llama.cpp: brew install llama.cpp # Start a local OpenAI-compatible server: llama-server -hf unsloth/Qwen3-8B-128K-GGUF:UD-Q4_K_XL
Configure Hermes
# Install Hermes: curl -fsSL https://hermes-agent.nousresearch.com/install.sh | bash hermes setup # Point Hermes at the local server: hermes config set model.provider custom hermes config set model.base_url http://127.0.0.1:8080/v1 hermes config set model.default unsloth/Qwen3-8B-128K-GGUF:UD-Q4_K_XL
Run Hermes
hermes
- Atomic Chat new
- Docker Model Runner
How to use unsloth/Qwen3-8B-128K-GGUF with Docker Model Runner:
docker model run hf.co/unsloth/Qwen3-8B-128K-GGUF:UD-Q4_K_XL
- Lemonade
How to use unsloth/Qwen3-8B-128K-GGUF with Lemonade:
Pull the model
# Download Lemonade from https://lemonade-server.ai/ lemonade pull unsloth/Qwen3-8B-128K-GGUF:UD-Q4_K_XL
Run and chat with the model
lemonade run user.Qwen3-8B-128K-GGUF-UD-Q4_K_XL
List all available models
lemonade list
Add files using upload-large-folder tool
Browse files- .gitattributes +6 -0
- Qwen3-8B-128K-UD-IQ1_M.gguf +3 -0
- Qwen3-8B-128K-UD-IQ1_S.gguf +3 -0
- Qwen3-8B-128K-UD-IQ2_M.gguf +3 -0
- Qwen3-8B-128K-UD-IQ3_XXS.gguf +3 -0
- Qwen3-8B-128K-UD-Q2_K_XL.gguf +3 -0
- Qwen3-8B-128K-UD-Q4_K_XL.gguf +3 -0
- README.md +353 -0
- config.json +35 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,9 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
Qwen3-8B-128K-UD-IQ1_S.gguf filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
Qwen3-8B-128K-UD-IQ1_M.gguf filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
Qwen3-8B-128K-UD-IQ2_M.gguf filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
Qwen3-8B-128K-UD-IQ3_XXS.gguf filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
Qwen3-8B-128K-UD-Q2_K_XL.gguf filter=lfs diff=lfs merge=lfs -text
|
| 41 |
+
Qwen3-8B-128K-UD-Q4_K_XL.gguf filter=lfs diff=lfs merge=lfs -text
|
Qwen3-8B-128K-UD-IQ1_M.gguf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:05b0a520849fa7c2d69c99b9d6a9184595901792b173fcfabcf13ee24c6ffe3b
|
| 3 |
+
size 2484647680
|
Qwen3-8B-128K-UD-IQ1_S.gguf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:11e9dd7c4cc1fd03b147bef90ba0e2f042c26a89c49edce8126778fbcbf4b214
|
| 3 |
+
size 2364716800
|
Qwen3-8B-128K-UD-IQ2_M.gguf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1222316a886db2fd5f15a857b7d2d2b23851be4841cb129ec8ca97d34aa3d985
|
| 3 |
+
size 3199055616
|
Qwen3-8B-128K-UD-IQ3_XXS.gguf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f429cc8f3293849a1f1078e5b10675cd22afd54cc6b7a12c4157bd483b999011
|
| 3 |
+
size 3492919040
|
Qwen3-8B-128K-UD-Q2_K_XL.gguf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:da12d6354447055e7fc0d533bc78083bf550b24b9bc8925feef81a57db4c171a
|
| 3 |
+
size 3501975296
|
Qwen3-8B-128K-UD-Q4_K_XL.gguf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:54c8f703c1ef362dec0c1335a720afd2be7ac1055d3fbda8a1a926c6146c2ed7
|
| 3 |
+
size 5161674496
|
README.md
ADDED
|
@@ -0,0 +1,353 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
tags:
|
| 3 |
+
- unsloth
|
| 4 |
+
library_name: transformers
|
| 5 |
+
license: apache-2.0
|
| 6 |
+
license_link: https://huggingface.co/Qwen/Qwen3-8B/blob/main/LICENSE
|
| 7 |
+
pipeline_tag: text-generation
|
| 8 |
+
base_model:
|
| 9 |
+
- Qwen/Qwen3-8B
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# Qwen3-8B
|
| 13 |
+
<a href="https://chat.qwen.ai/" target="_blank" style="margin: 2px;">
|
| 14 |
+
<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;"/>
|
| 15 |
+
</a>
|
| 16 |
+
|
| 17 |
+
## Qwen3 Highlights
|
| 18 |
+
|
| 19 |
+
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:
|
| 20 |
+
|
| 21 |
+
- **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.
|
| 22 |
+
- **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.
|
| 23 |
+
- **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.
|
| 24 |
+
- **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.
|
| 25 |
+
- **Support of 100+ languages and dialects** with strong capabilities for **multilingual instruction following** and **translation**.
|
| 26 |
+
|
| 27 |
+
## Model Overview
|
| 28 |
+
|
| 29 |
+
**Qwen3-8B** has the following features:
|
| 30 |
+
- Type: Causal Language Models
|
| 31 |
+
- Training Stage: Pretraining & Post-training
|
| 32 |
+
- Number of Parameters: 8.2B
|
| 33 |
+
- Number of Paramaters (Non-Embedding): 6.95B
|
| 34 |
+
- Number of Layers: 36
|
| 35 |
+
- Number of Attention Heads (GQA): 32 for Q and 8 for KV
|
| 36 |
+
- Context Length: 32,768 natively and [131,072 tokens with YaRN](#processing-long-texts).
|
| 37 |
+
|
| 38 |
+
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/).
|
| 39 |
+
|
| 40 |
+
## Quickstart
|
| 41 |
+
|
| 42 |
+
The code of Qwen3 has been in the latest Hugging Face `transformers` and we advise you to use the latest version of `transformers`.
|
| 43 |
+
|
| 44 |
+
With `transformers<4.51.0`, you will encounter the following error:
|
| 45 |
+
```
|
| 46 |
+
KeyError: 'qwen3'
|
| 47 |
+
```
|
| 48 |
+
|
| 49 |
+
The following contains a code snippet illustrating how to use the model generate content based on given inputs.
|
| 50 |
+
```python
|
| 51 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 52 |
+
|
| 53 |
+
model_name = "Qwen/Qwen3-8B"
|
| 54 |
+
|
| 55 |
+
# load the tokenizer and the model
|
| 56 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 57 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 58 |
+
model_name,
|
| 59 |
+
torch_dtype="auto",
|
| 60 |
+
device_map="auto"
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# prepare the model input
|
| 64 |
+
prompt = "Give me a short introduction to large language model."
|
| 65 |
+
messages = [
|
| 66 |
+
{"role": "user", "content": prompt}
|
| 67 |
+
]
|
| 68 |
+
text = tokenizer.apply_chat_template(
|
| 69 |
+
messages,
|
| 70 |
+
tokenize=False,
|
| 71 |
+
add_generation_prompt=True,
|
| 72 |
+
enable_thinking=True # Switches between thinking and non-thinking modes. Default is True.
|
| 73 |
+
)
|
| 74 |
+
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
| 75 |
+
|
| 76 |
+
# conduct text completion
|
| 77 |
+
generated_ids = model.generate(
|
| 78 |
+
**model_inputs,
|
| 79 |
+
max_new_tokens=32768
|
| 80 |
+
)
|
| 81 |
+
output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
|
| 82 |
+
|
| 83 |
+
# parsing thinking content
|
| 84 |
+
try:
|
| 85 |
+
# rindex finding 151668 (</think>)
|
| 86 |
+
index = len(output_ids) - output_ids[::-1].index(151668)
|
| 87 |
+
except ValueError:
|
| 88 |
+
index = 0
|
| 89 |
+
|
| 90 |
+
thinking_content = tokenizer.decode(output_ids[:index], skip_special_tokens=True).strip("\n")
|
| 91 |
+
content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("\n")
|
| 92 |
+
|
| 93 |
+
print("thinking content:", thinking_content)
|
| 94 |
+
print("content:", content)
|
| 95 |
+
```
|
| 96 |
+
|
| 97 |
+
For deployment, you can use `sglang>=0.4.6.post1` or `vllm>=0.8.4` or to create an OpenAI-compatible API endpoint:
|
| 98 |
+
- SGLang:
|
| 99 |
+
```shell
|
| 100 |
+
python -m sglang.launch_server --model-path Qwen/Qwen3-8B --reasoning-parser qwen3
|
| 101 |
+
```
|
| 102 |
+
- vLLM:
|
| 103 |
+
```shell
|
| 104 |
+
vllm serve Qwen/Qwen3-8B --enable-reasoning --reasoning-parser deepseek_r1
|
| 105 |
+
```
|
| 106 |
+
|
| 107 |
+
For local use, applications such as llama.cpp, Ollama, LMStudio, and MLX-LM have also supported Qwen3.
|
| 108 |
+
|
| 109 |
+
## Switching Between Thinking and Non-Thinking Mode
|
| 110 |
+
|
| 111 |
+
> [!TIP]
|
| 112 |
+
> The `enable_thinking` switch is also available in APIs created by SGLang and vLLM.
|
| 113 |
+
> 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.
|
| 114 |
+
|
| 115 |
+
### `enable_thinking=True`
|
| 116 |
+
|
| 117 |
+
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.
|
| 118 |
+
|
| 119 |
+
```python
|
| 120 |
+
text = tokenizer.apply_chat_template(
|
| 121 |
+
messages,
|
| 122 |
+
tokenize=False,
|
| 123 |
+
add_generation_prompt=True,
|
| 124 |
+
enable_thinking=True # True is the default value for enable_thinking
|
| 125 |
+
)
|
| 126 |
+
```
|
| 127 |
+
|
| 128 |
+
In this mode, the model will generate think content wrapped in a `<think>...</think>` block, followed by the final response.
|
| 129 |
+
|
| 130 |
+
> [!NOTE]
|
| 131 |
+
> 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.
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
### `enable_thinking=False`
|
| 135 |
+
|
| 136 |
+
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.
|
| 137 |
+
|
| 138 |
+
```python
|
| 139 |
+
text = tokenizer.apply_chat_template(
|
| 140 |
+
messages,
|
| 141 |
+
tokenize=False,
|
| 142 |
+
add_generation_prompt=True,
|
| 143 |
+
enable_thinking=False # Setting enable_thinking=False disables thinking mode
|
| 144 |
+
)
|
| 145 |
+
```
|
| 146 |
+
|
| 147 |
+
In this mode, the model will not generate any think content and will not include a `<think>...</think>` block.
|
| 148 |
+
|
| 149 |
+
> [!NOTE]
|
| 150 |
+
> 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.
|
| 151 |
+
|
| 152 |
+
### Advanced Usage: Switching Between Thinking and Non-Thinking Modes via User Input
|
| 153 |
+
|
| 154 |
+
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.
|
| 155 |
+
|
| 156 |
+
Here is an example of a multi-turn conversation:
|
| 157 |
+
|
| 158 |
+
```python
|
| 159 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 160 |
+
|
| 161 |
+
class QwenChatbot:
|
| 162 |
+
def __init__(self, model_name="Qwen/Qwen3-8B"):
|
| 163 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 164 |
+
self.model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 165 |
+
self.history = []
|
| 166 |
+
|
| 167 |
+
def generate_response(self, user_input):
|
| 168 |
+
messages = self.history + [{"role": "user", "content": user_input}]
|
| 169 |
+
|
| 170 |
+
text = self.tokenizer.apply_chat_template(
|
| 171 |
+
messages,
|
| 172 |
+
tokenize=False,
|
| 173 |
+
add_generation_prompt=True
|
| 174 |
+
)
|
| 175 |
+
|
| 176 |
+
inputs = self.tokenizer(text, return_tensors="pt")
|
| 177 |
+
response_ids = self.model.generate(**inputs, max_new_tokens=32768)[0][len(inputs.input_ids[0]):].tolist()
|
| 178 |
+
response = self.tokenizer.decode(response_ids, skip_special_tokens=True)
|
| 179 |
+
|
| 180 |
+
# Update history
|
| 181 |
+
self.history.append({"role": "user", "content": user_input})
|
| 182 |
+
self.history.append({"role": "assistant", "content": response})
|
| 183 |
+
|
| 184 |
+
return response
|
| 185 |
+
|
| 186 |
+
# Example Usage
|
| 187 |
+
if __name__ == "__main__":
|
| 188 |
+
chatbot = QwenChatbot()
|
| 189 |
+
|
| 190 |
+
# First input (without /think or /no_think tags, thinking mode is enabled by default)
|
| 191 |
+
user_input_1 = "How many r's in strawberries?"
|
| 192 |
+
print(f"User: {user_input_1}")
|
| 193 |
+
response_1 = chatbot.generate_response(user_input_1)
|
| 194 |
+
print(f"Bot: {response_1}")
|
| 195 |
+
print("----------------------")
|
| 196 |
+
|
| 197 |
+
# Second input with /no_think
|
| 198 |
+
user_input_2 = "Then, how many r's in blueberries? /no_think"
|
| 199 |
+
print(f"User: {user_input_2}")
|
| 200 |
+
response_2 = chatbot.generate_response(user_input_2)
|
| 201 |
+
print(f"Bot: {response_2}")
|
| 202 |
+
print("----------------------")
|
| 203 |
+
|
| 204 |
+
# Third input with /think
|
| 205 |
+
user_input_3 = "Really? /think"
|
| 206 |
+
print(f"User: {user_input_3}")
|
| 207 |
+
response_3 = chatbot.generate_response(user_input_3)
|
| 208 |
+
print(f"Bot: {response_3}")
|
| 209 |
+
```
|
| 210 |
+
|
| 211 |
+
> [!NOTE]
|
| 212 |
+
> 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.
|
| 213 |
+
> 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.
|
| 214 |
+
|
| 215 |
+
## Agentic Use
|
| 216 |
+
|
| 217 |
+
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.
|
| 218 |
+
|
| 219 |
+
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.
|
| 220 |
+
```python
|
| 221 |
+
from qwen_agent.agents import Assistant
|
| 222 |
+
|
| 223 |
+
# Define LLM
|
| 224 |
+
llm_cfg = {
|
| 225 |
+
'model': 'Qwen3-8B',
|
| 226 |
+
|
| 227 |
+
# Use the endpoint provided by Alibaba Model Studio:
|
| 228 |
+
# 'model_type': 'qwen_dashscope',
|
| 229 |
+
# 'api_key': os.getenv('DASHSCOPE_API_KEY'),
|
| 230 |
+
|
| 231 |
+
# Use a custom endpoint compatible with OpenAI API:
|
| 232 |
+
'model_server': 'http://localhost:8000/v1', # api_base
|
| 233 |
+
'api_key': 'EMPTY',
|
| 234 |
+
|
| 235 |
+
# Other parameters:
|
| 236 |
+
# 'generate_cfg': {
|
| 237 |
+
# # Add: When the response content is `<think>this is the thought</think>this is the answer;
|
| 238 |
+
# # Do not add: When the response has been separated by reasoning_content and content.
|
| 239 |
+
# 'thought_in_content': True,
|
| 240 |
+
# },
|
| 241 |
+
}
|
| 242 |
+
|
| 243 |
+
# Define Tools
|
| 244 |
+
tools = [
|
| 245 |
+
{'mcpServers': { # You can specify the MCP configuration file
|
| 246 |
+
'time': {
|
| 247 |
+
'command': 'uvx',
|
| 248 |
+
'args': ['mcp-server-time', '--local-timezone=Asia/Shanghai']
|
| 249 |
+
},
|
| 250 |
+
"fetch": {
|
| 251 |
+
"command": "uvx",
|
| 252 |
+
"args": ["mcp-server-fetch"]
|
| 253 |
+
}
|
| 254 |
+
}
|
| 255 |
+
},
|
| 256 |
+
'code_interpreter', # Built-in tools
|
| 257 |
+
]
|
| 258 |
+
|
| 259 |
+
# Define Agent
|
| 260 |
+
bot = Assistant(llm=llm_cfg, function_list=tools)
|
| 261 |
+
|
| 262 |
+
# Streaming generation
|
| 263 |
+
messages = [{'role': 'user', 'content': 'https://qwenlm.github.io/blog/ Introduce the latest developments of Qwen'}]
|
| 264 |
+
for responses in bot.run(messages=messages):
|
| 265 |
+
pass
|
| 266 |
+
print(responses)
|
| 267 |
+
```
|
| 268 |
+
|
| 269 |
+
## Processing Long Texts
|
| 270 |
+
|
| 271 |
+
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.
|
| 272 |
+
|
| 273 |
+
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:
|
| 274 |
+
|
| 275 |
+
- Modifying the model files:
|
| 276 |
+
In the `config.json` file, add the `rope_scaling` fields:
|
| 277 |
+
```json
|
| 278 |
+
{
|
| 279 |
+
...,
|
| 280 |
+
"rope_scaling": {
|
| 281 |
+
"type": "yarn",
|
| 282 |
+
"factor": 4.0,
|
| 283 |
+
"original_max_position_embeddings": 32768
|
| 284 |
+
}
|
| 285 |
+
}
|
| 286 |
+
```
|
| 287 |
+
For `llama.cpp`, you need to regenerate the GGUF file after the modification.
|
| 288 |
+
|
| 289 |
+
- Passing command line arguments:
|
| 290 |
+
|
| 291 |
+
For `vllm`, you can use
|
| 292 |
+
```shell
|
| 293 |
+
vllm serve ... --rope-scaling '{"type":"yarn","factor":4.0,"original_max_position_embeddings":32768}' --max-model-len 131072
|
| 294 |
+
```
|
| 295 |
+
|
| 296 |
+
For `sglang`, you can use
|
| 297 |
+
```shell
|
| 298 |
+
python -m sglang.launch_server ... --json-model-override-args '{"rope_scaling":{"type":"yarn","factor":4.0,"original_max_position_embeddings":32768}}'
|
| 299 |
+
```
|
| 300 |
+
|
| 301 |
+
For `llama-server` from `llama.cpp`, you can use
|
| 302 |
+
```shell
|
| 303 |
+
llama-server ... --rope-scaling yarn --rope-scale 4 --yarn-orig-ctx 32768
|
| 304 |
+
```
|
| 305 |
+
|
| 306 |
+
> [!IMPORTANT]
|
| 307 |
+
> If you encounter the following warning
|
| 308 |
+
> ```
|
| 309 |
+
> Unrecognized keys in `rope_scaling` for 'rope_type'='yarn': {'original_max_position_embeddings'}
|
| 310 |
+
> ```
|
| 311 |
+
> please upgrade `transformers>=4.51.0`.
|
| 312 |
+
|
| 313 |
+
> [!NOTE]
|
| 314 |
+
> 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.**
|
| 315 |
+
> We advise adding the `rope_scaling` configuration only when processing long contexts is required.
|
| 316 |
+
> 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.
|
| 317 |
+
|
| 318 |
+
> [!NOTE]
|
| 319 |
+
> 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.
|
| 320 |
+
|
| 321 |
+
> [!TIP]
|
| 322 |
+
> The endpoint provided by Alibaba Model Studio supports dynamic YaRN by default and no extra configuration is needed.
|
| 323 |
+
|
| 324 |
+
## Best Practices
|
| 325 |
+
|
| 326 |
+
To achieve optimal performance, we recommend the following settings:
|
| 327 |
+
|
| 328 |
+
1. **Sampling Parameters**:
|
| 329 |
+
- 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.
|
| 330 |
+
- For non-thinking mode (`enable_thinking=False`), we suggest using `Temperature=0.7`, `TopP=0.8`, `TopK=20`, and `MinP=0`.
|
| 331 |
+
- 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.
|
| 332 |
+
|
| 333 |
+
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.
|
| 334 |
+
|
| 335 |
+
3. **Standardize Output Format**: We recommend using prompts to standardize model outputs when benchmarking.
|
| 336 |
+
- **Math Problems**: Include "Please reason step by step, and put your final answer within \boxed{}." in the prompt.
|
| 337 |
+
- **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"`."
|
| 338 |
+
|
| 339 |
+
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.
|
| 340 |
+
|
| 341 |
+
### Citation
|
| 342 |
+
|
| 343 |
+
If you find our work helpful, feel free to give us a cite.
|
| 344 |
+
|
| 345 |
+
```
|
| 346 |
+
@misc{qwen3,
|
| 347 |
+
title = {Qwen3},
|
| 348 |
+
url = {https://qwenlm.github.io/blog/qwen3/},
|
| 349 |
+
author = {Qwen Team},
|
| 350 |
+
month = {April},
|
| 351 |
+
year = {2025}
|
| 352 |
+
}
|
| 353 |
+
```
|
config.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"Qwen3ForCausalLM"
|
| 4 |
+
],
|
| 5 |
+
"attention_bias": false,
|
| 6 |
+
"attention_dropout": 0.0,
|
| 7 |
+
"bos_token_id": 151643,
|
| 8 |
+
"eos_token_id": 151645,
|
| 9 |
+
"head_dim": 128,
|
| 10 |
+
"hidden_act": "silu",
|
| 11 |
+
"hidden_size": 4096,
|
| 12 |
+
"initializer_range": 0.02,
|
| 13 |
+
"intermediate_size": 12288,
|
| 14 |
+
"max_position_embeddings": 131072,
|
| 15 |
+
"max_window_layers": 36,
|
| 16 |
+
"model_type": "qwen3",
|
| 17 |
+
"num_attention_heads": 32,
|
| 18 |
+
"num_hidden_layers": 36,
|
| 19 |
+
"num_key_value_heads": 8,
|
| 20 |
+
"rms_norm_eps": 1e-06,
|
| 21 |
+
"rope_scaling": {
|
| 22 |
+
"factor": 4.0,
|
| 23 |
+
"original_max_position_embeddings": 32768,
|
| 24 |
+
"rope_type": "yarn",
|
| 25 |
+
"type": "yarn"
|
| 26 |
+
},
|
| 27 |
+
"rope_theta": 1000000,
|
| 28 |
+
"sliding_window": null,
|
| 29 |
+
"tie_word_embeddings": false,
|
| 30 |
+
"torch_dtype": "bfloat16",
|
| 31 |
+
"transformers_version": "4.51.0",
|
| 32 |
+
"use_cache": true,
|
| 33 |
+
"use_sliding_window": false,
|
| 34 |
+
"vocab_size": 151936
|
| 35 |
+
}
|