ramblingpolymath commited on
Commit
d186d20
·
verified ·
1 Parent(s): f7b71c0

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +280 -0
README.md ADDED
@@ -0,0 +1,280 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: transformers
3
+ license: apache-2.0
4
+ pipeline_tag: text-generation
5
+ base_model: Qwen/Qwen3-Coder-30B-A3B-Instruct
6
+ tags:
7
+ - quantized
8
+ - w8a8
9
+ - llm-compressor
10
+ ---
11
+
12
+ ```
13
+ ██╗ ██╗ █████╗ █████╗ █████╗
14
+ ██║ ██║██╔══██╗ ██╔══██╗██╔══██╗
15
+ ██║ █╗ ██║╚█████╔╝ ███████║╚█████╔╝
16
+ ██║███╗██║██╔══██╗ ██╔══██║██╔══██╗
17
+ ╚███╔███╔╝╚█████╔╝ ██║ ██║╚█████╔╝
18
+ ╚══╝╚══╝ ╚════╝ ╚═╝ ╚═╝ ╚════╝
19
+ 🗜️ COMPRESSED & OPTIMIZED 🚀
20
+ ```
21
+
22
+ # Qwen3-Coder-30B-A3B-Instruct - W8A8 Quantized
23
+
24
+ W8A8 (8-bit weights and activations) quantized version of Qwen/Qwen3-Coder-30B-A3B-Instruct using **LLM-Compressor**.
25
+
26
+ - 🗜️ **Memory**: ~50% reduction vs FP16
27
+ - 🚀 **Speed**: Faster inference on supported hardware
28
+ - 🔗 **Original model**: https://huggingface.co/Qwen/Qwen3-Coder-30B-A3B-Instruct
29
+ - 🏗️ **Recommended architectures**: Ampere and older
30
+
31
+ <details>
32
+ <summary>Click to view compression config</summary>
33
+
34
+ ```python
35
+ cat Qwen3-Coder-30B-A3B-w8a8-Instruct.py
36
+ from datasets import load_dataset
37
+ from transformers import AutoModelForCausalLM, AutoTokenizer
38
+
39
+ from llmcompressor.modifiers.quantization import GPTQModifier
40
+ from llmcompressor.modifiers.smoothquant import SmoothQuantModifier
41
+ from llmcompressor.transformers import oneshot
42
+ from llmcompressor.utils import dispatch_for_generation
43
+ from llmcompressor.modifiers.quantization import QuantizationModifier
44
+ # Select model and load it.
45
+ model_id = "Qwen/Qwen3-Coder-30B-A3B-Instruct"
46
+ model = AutoModelForCausalLM.from_pretrained(
47
+ model_id,
48
+ torch_dtype="auto",
49
+ device_map="auto",
50
+ low_cpu_mem_usage=True,
51
+ offload_folder="./offload_tmp", # Add offload directory
52
+ max_memory={0: "22GB", 1: "22GB", "cpu": "64GB"},
53
+ )
54
+ tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
55
+
56
+ # Select calibration dataset.
57
+ DATASET_ID = "mit-han-lab/pile-val-backup"
58
+ DATASET_SPLIT = "validation"
59
+
60
+ # Select number of samples. 512 samples is a good place to start.
61
+ # Increasing the number of samples can improve accuracy.
62
+ NUM_CALIBRATION_SAMPLES = 256
63
+ MAX_SEQUENCE_LENGTH = 512
64
+
65
+ # Load dataset and preprocess.
66
+ ds = load_dataset(DATASET_ID, split=f"{DATASET_SPLIT}[:{NUM_CALIBRATION_SAMPLES}]")
67
+ ds = ds.shuffle(seed=42)
68
+
69
+
70
+ def preprocess(example):
71
+ return {
72
+ "text": tokenizer.apply_chat_template(
73
+ [{"role": "user", "content": example["text"]}],
74
+ tokenize=False,
75
+ )
76
+ }
77
+
78
+
79
+ ds = ds.map(preprocess)
80
+
81
+
82
+ # Tokenize inputs.
83
+ def tokenize(sample):
84
+ return tokenizer(
85
+ sample["text"],
86
+ padding=False,
87
+ max_length=MAX_SEQUENCE_LENGTH,
88
+ truncation=True,
89
+ add_special_tokens=False,
90
+ )
91
+
92
+
93
+ ds = ds.map(tokenize, remove_columns=ds.column_names)
94
+
95
+ # Configure the quantization algorithm to run.
96
+ # * quantize the activations to int8 (dynamic per token)
97
+ recipe = QuantizationModifier(targets="Linear", scheme="W8A8", ignore=["lm_head", "re:.*mlp.gate$"])
98
+
99
+ # Apply algorithms.
100
+ oneshot(
101
+ model=model,
102
+ dataset=ds,
103
+ recipe=recipe,
104
+ max_seq_length=MAX_SEQUENCE_LENGTH,
105
+ num_calibration_samples=NUM_CALIBRATION_SAMPLES,
106
+ output_dir="./Qwen/Qwen3-Coder-30B-A3B-Instruct-W8A8", # Add this line
107
+ )
108
+
109
+
110
+ # Save to disk compressed.
111
+ SAVE_DIR = model_id.rstrip("/").split("/")[-1] + "-W8A8"
112
+ model.save_pretrained(SAVE_DIR, save_compressed=True)
113
+ tokenizer.save_pretrained(SAVE_DIR)
114
+ ```
115
+
116
+ </details>
117
+
118
+ ---
119
+
120
+ ## 📄 Original Model README
121
+
122
+ # Qwen3-Coder-30B-A3B-Instruct
123
+ <a href="https://chat.qwen.ai/" target="_blank" style="margin: 2px;">
124
+ <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;"/>
125
+ </a>
126
+
127
+ ## Highlights
128
+
129
+ **Qwen3-Coder** is available in multiple sizes. Today, we're excited to introduce **Qwen3-Coder-30B-A3B-Instruct**. This streamlined model maintains impressive performance and efficiency, featuring the following key enhancements:
130
+
131
+ - **Significant Performance** among open models on **Agentic Coding**, **Agentic Browser-Use**, and other foundational coding tasks.
132
+ - **Long-context Capabilities** with native support for **256K** tokens, extendable up to **1M** tokens using Yarn, optimized for repository-scale understanding.
133
+ - **Agentic Coding** supporting for most platform such as **Qwen Code**, **CLINE**, featuring a specially designed function call format.
134
+
135
+ ![image/jpeg](https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen3-Coder/qwen3-coder-30a3-main.jpg)
136
+
137
+ ## Model Overview
138
+
139
+ **Qwen3-Coder-30B-A3B-Instruct** has the following features:
140
+ - Type: Causal Language Models
141
+ - Training Stage: Pretraining & Post-training
142
+ - Number of Parameters: 30.5B in total and 3.3B activated
143
+ - Number of Layers: 48
144
+ - Number of Attention Heads (GQA): 32 for Q and 4 for KV
145
+ - Number of Experts: 128
146
+ - Number of Activated Experts: 8
147
+ - Context Length: **262,144 natively**.
148
+
149
+ **NOTE: This model supports only non-thinking mode and does not generate ``<think></think>`` blocks in its output. Meanwhile, specifying `enable_thinking=False` is no longer required.**
150
+
151
+ For more details, including benchmark evaluation, hardware requirements, and inference performance, please refer to our [blog](https://qwenlm.github.io/blog/qwen3-coder/), [GitHub](https://github.com/QwenLM/Qwen3-Coder), and [Documentation](https://qwen.readthedocs.io/en/latest/).
152
+
153
+
154
+ ## Quickstart
155
+
156
+ We advise you to use the latest version of `transformers`.
157
+
158
+ With `transformers<4.51.0`, you will encounter the following error:
159
+ ```
160
+ KeyError: 'qwen3_moe'
161
+ ```
162
+
163
+ The following contains a code snippet illustrating how to use the model generate content based on given inputs.
164
+ ```python
165
+ from transformers import AutoModelForCausalLM, AutoTokenizer
166
+
167
+ model_name = "Qwen/Qwen3-Coder-30B-A3B-Instruct"
168
+
169
+ # load the tokenizer and the model
170
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
171
+ model = AutoModelForCausalLM.from_pretrained(
172
+ model_name,
173
+ torch_dtype="auto",
174
+ device_map="auto"
175
+ )
176
+
177
+ # prepare the model input
178
+ prompt = "Write a quick sort algorithm."
179
+ messages = [
180
+ {"role": "user", "content": prompt}
181
+ ]
182
+ text = tokenizer.apply_chat_template(
183
+ messages,
184
+ tokenize=False,
185
+ add_generation_prompt=True,
186
+ )
187
+ model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
188
+
189
+ # conduct text completion
190
+ generated_ids = model.generate(
191
+ **model_inputs,
192
+ max_new_tokens=65536
193
+ )
194
+ output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
195
+
196
+ content = tokenizer.decode(output_ids, skip_special_tokens=True)
197
+
198
+ print("content:", content)
199
+ ```
200
+
201
+ **Note: If you encounter out-of-memory (OOM) issues, consider reducing the context length to a shorter value, such as `32,768`.**
202
+
203
+ For local use, applications such as Ollama, LMStudio, MLX-LM, llama.cpp, and KTransformers have also supported Qwen3.
204
+
205
+ ## Agentic Coding
206
+
207
+ Qwen3-Coder excels in tool calling capabilities.
208
+
209
+ You can simply define or use any tools as following example.
210
+ ```python
211
+ # Your tool implementation
212
+ def square_the_number(num: float) -> dict:
213
+ return num ** 2
214
+
215
+ # Define Tools
216
+ tools=[
217
+ {
218
+ "type":"function",
219
+ "function":{
220
+ "name": "square_the_number",
221
+ "description": "output the square of the number.",
222
+ "parameters": {
223
+ "type": "object",
224
+ "required": ["input_num"],
225
+ "properties": {
226
+ 'input_num': {
227
+ 'type': 'number',
228
+ 'description': 'input_num is a number that will be squared'
229
+ }
230
+ },
231
+ }
232
+ }
233
+ }
234
+ ]
235
+
236
+ import OpenAI
237
+ # Define LLM
238
+ client = OpenAI(
239
+ # Use a custom endpoint compatible with OpenAI API
240
+ base_url='http://localhost:8000/v1', # api_base
241
+ api_key="EMPTY"
242
+ )
243
+
244
+ messages = [{'role': 'user', 'content': 'square the number 1024'}]
245
+
246
+ completion = client.chat.completions.create(
247
+ messages=messages,
248
+ model="Qwen3-Coder-30B-A3B-Instruct",
249
+ max_tokens=65536,
250
+ tools=tools,
251
+ )
252
+
253
+ print(completion.choice[0])
254
+ ```
255
+
256
+ ## Best Practices
257
+
258
+ To achieve optimal performance, we recommend the following settings:
259
+
260
+ 1. **Sampling Parameters**:
261
+ - We suggest using `temperature=0.7`, `top_p=0.8`, `top_k=20`, `repetition_penalty=1.05`.
262
+
263
+ 2. **Adequate Output Length**: We recommend using an output length of 65,536 tokens for most queries, which is adequate for instruct models.
264
+
265
+
266
+ ### Citation
267
+
268
+ If you find our work helpful, feel free to give us a cite.
269
+
270
+ ```
271
+ @misc{qwen3technicalreport,
272
+ title={Qwen3 Technical Report},
273
+ author={Qwen Team},
274
+ year={2025},
275
+ eprint={2505.09388},
276
+ archivePrefix={arXiv},
277
+ primaryClass={cs.CL},
278
+ url={https://arxiv.org/abs/2505.09388},
279
+ }
280
+ ```