Update README.md
Browse files
README.md
CHANGED
|
@@ -6,18 +6,23 @@ base_model:
|
|
| 6 |
|
| 7 |
## Model Details
|
| 8 |
|
| 9 |
-
This model is a mixed int4 model with group_size 64 and symmetric quantization of [Qwen/Qwen3-Coder-480B-A35B-Instruct](https://huggingface.co/Qwen/Qwen3-Coder-480B-A35B-Instruct) generated by [intel/auto-round](https://github.com/intel/auto-round) via **RTN** (no algorithm tuning).
|
|
|
|
| 10 |
|
| 11 |
Please follow the license of the original model.
|
| 12 |
|
| 13 |
## How To Use
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
**INT4 Inference on CPU/Intel GPU/CUDA**
|
| 16 |
|
| 17 |
~~~python
|
| 18 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 19 |
|
| 20 |
-
model_name = "Intel/Qwen3-Coder-480B-A35B-Instruct-int4-mixed-
|
| 21 |
|
| 22 |
# load the tokenizer and the model
|
| 23 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
@@ -63,227 +68,6 @@ for i, prompt in enumerate(prompts):
|
|
| 63 |
print(f"Generated: {decoded_outputs[i]}")
|
| 64 |
print("-" * 50)
|
| 65 |
|
| 66 |
-
"""
|
| 67 |
-
Prompt: Write a quick sort algorithm.
|
| 68 |
-
Generated: Here's a QuickSort implementation in Python with both in-place and simple versions:
|
| 69 |
-
|
| 70 |
-
## In-Place QuickSort (More Efficient)
|
| 71 |
-
|
| 72 |
-
```python
|
| 73 |
-
def quicksort(arr, low=0, high=None):
|
| 74 |
-
"""
|
| 75 |
-
Sorts an array using the QuickSort algorithm (in-place).
|
| 76 |
-
|
| 77 |
-
Args:
|
| 78 |
-
arr: List to be sorted
|
| 79 |
-
low: Starting index (default: 0)
|
| 80 |
-
high: Ending index (default: len(arr) - 1)
|
| 81 |
-
"""
|
| 82 |
-
if high is None:
|
| 83 |
-
high = len(arr) - 1
|
| 84 |
-
|
| 85 |
-
if low < high:
|
| 86 |
-
# Partition the array and get the pivot index
|
| 87 |
-
pivot_index = partition(arr, low, high)
|
| 88 |
-
|
| 89 |
-
# Recursively sort elements before and after partition
|
| 90 |
-
quicksort(arr, low, pivot_index - 1)
|
| 91 |
-
quicksort(arr, pivot_index + 1, high)
|
| 92 |
-
|
| 93 |
-
def partition(arr, low, high):
|
| 94 |
-
"""
|
| 95 |
-
Partitions the array around a pivot element.
|
| 96 |
-
Elements smaller than pivot go to the left, larger to the right.
|
| 97 |
-
|
| 98 |
-
Returns:
|
| 99 |
-
The final position of the pivot
|
| 100 |
-
"""
|
| 101 |
-
# Choose the rightmost element as pivot
|
| 102 |
-
pivot = arr[high]
|
| 103 |
-
|
| 104 |
-
# Index of smaller element (indicates right position of pivot)
|
| 105 |
-
i = low - 1
|
| 106 |
-
|
| 107 |
-
for j in range(low, high):
|
| 108 |
-
# If current element is smaller than or equal to pivot
|
| 109 |
-
if arr[j] <= pivot:
|
| 110 |
-
i += 1
|
| 111 |
-
arr[i], arr[j] = arr[j], arr[i] # Swap elements
|
| 112 |
-
|
| 113 |
-
# Place pivot in correct position
|
| 114 |
-
arr[i + 1], arr[high] = arr[high], arr[i + 1]
|
| 115 |
-
return i + 1
|
| 116 |
-
|
| 117 |
-
# Example usage
|
| 118 |
-
if __name__ == "__main__":
|
| 119 |
-
# Test the algorithm
|
| 120 |
-
test_array = [64, 34, 25, 12, 22, 11, 90]
|
| 121 |
-
print("Original array:", test_array)
|
| 122 |
-
|
| 123 |
-
quicksort(test_array)
|
| 124 |
-
print("Sorted array:", test_array)
|
| 125 |
-
```
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
## Simple Version (Creates New Arrays)
|
| 129 |
-
|
| 130 |
-
```python
|
| 131 |
-
def quicksort_simple(arr):
|
| 132 |
-
"""
|
| 133 |
-
Simple QuickSort implementation that creates new arrays.
|
| 134 |
-
Less memory efficient but easier to understand.
|
| 135 |
-
"""
|
| 136 |
-
if len(arr) <= 1:
|
| 137 |
-
return arr
|
| 138 |
-
|
| 139 |
-
pivot = arr[len(arr) // 2] # Choose middle element as pivot
|
| 140 |
-
left =
|
| 141 |
-
--------------------------------------------------
|
| 142 |
-
Prompt: Write a flappy bird.
|
| 143 |
-
Generated: # Flappy Bird in PyGame
|
| 144 |
-
|
| 145 |
-
Here's a complete implementation of Flappy Bird using PyGame:
|
| 146 |
-
|
| 147 |
-
```python
|
| 148 |
-
import pygame
|
| 149 |
-
import sys
|
| 150 |
-
import random
|
| 151 |
-
import math
|
| 152 |
-
|
| 153 |
-
# Initialize pygame
|
| 154 |
-
pygame.init()
|
| 155 |
-
|
| 156 |
-
# Game constants
|
| 157 |
-
WIDTH, HEIGHT = 800, 600
|
| 158 |
-
FPS = 60
|
| 159 |
-
GRAVITY = 0.5
|
| 160 |
-
FLAP_STRENGTH = -8
|
| 161 |
-
PIPE_SPEED = 3
|
| 162 |
-
PIPE_GAP = 200
|
| 163 |
-
PIPE_FREQUENCY = 1800 # milliseconds
|
| 164 |
-
GROUND_HEIGHT = 100
|
| 165 |
-
BIRD_RADIUS = 20
|
| 166 |
-
|
| 167 |
-
# Colors
|
| 168 |
-
SKY_BLUE = (113, 197, 207)
|
| 169 |
-
GREEN = (111, 196, 69)
|
| 170 |
-
DARK_GREEN = (76, 145, 65)
|
| 171 |
-
BROWN = (160, 120, 40)
|
| 172 |
-
YELLOW = (255, 217, 61)
|
| 173 |
-
RED = (231, 76, 60)
|
| 174 |
-
WHITE = (255, 255, 255)
|
| 175 |
-
BLACK = (0, 0, 0)
|
| 176 |
-
|
| 177 |
-
# Set up the display
|
| 178 |
-
screen = pygame.display.set_mode((WIDTH, HEIGHT))
|
| 179 |
-
pygame.display.set_caption("Flappy Bird")
|
| 180 |
-
clock = pygame.time.Clock()
|
| 181 |
-
|
| 182 |
-
# Font setup
|
| 183 |
-
font = pygame.font.SysFont(None, 48)
|
| 184 |
-
small_font = pygame.font.SysFont(None, 36)
|
| 185 |
-
|
| 186 |
-
class Bird:
|
| 187 |
-
def __init__(self):
|
| 188 |
-
self.x = WIDTH // 3
|
| 189 |
-
self.y = HEIGHT // 2
|
| 190 |
-
self.velocity = 0
|
| 191 |
-
self.alive = True
|
| 192 |
-
self.rotation = 0
|
| 193 |
-
|
| 194 |
-
def flap(self):
|
| 195 |
-
if self.alive:
|
| 196 |
-
self.velocity = FLAP_STRENGTH
|
| 197 |
-
|
| 198 |
-
def update(self):
|
| 199 |
-
# Apply gravity
|
| 200 |
-
self.velocity += GRAVITY
|
| 201 |
-
self.y += self.velocity
|
| 202 |
-
|
| 203 |
-
# Rotate bird based on velocity
|
| 204 |
-
self.rotation = max(-30, min(self.velocity * 2, 90))
|
| 205 |
-
|
| 206 |
-
# Check boundaries
|
| 207 |
-
if self.y < 0:
|
| 208 |
-
self.y = 0
|
| 209 |
-
self.velocity = 0
|
| 210 |
-
if self.y > HEIGHT - GROUND_HEIGHT - BIRD_RADIUS:
|
| 211 |
-
self.y = HEIGHT - GROUND_HEIGHT - BIRD_RADIUS
|
| 212 |
-
self.velocity = 0
|
| 213 |
-
self.alive = False
|
| 214 |
-
--------------------------------------------------
|
| 215 |
-
Prompt: Write a llm quantization algorithm.
|
| 216 |
-
Generated: Here's a comprehensive implementation of LLM quantization algorithms, including post-training quantization and QLoRA-style quantization:
|
| 217 |
-
|
| 218 |
-
```python
|
| 219 |
-
import torch
|
| 220 |
-
import torch.nn as nn
|
| 221 |
-
from typing import Dict, Tuple, Optional
|
| 222 |
-
import math
|
| 223 |
-
|
| 224 |
-
class Quantizer:
|
| 225 |
-
"""Base class for quantization operations"""
|
| 226 |
-
|
| 227 |
-
@staticmethod
|
| 228 |
-
def linear_quantize(tensor: torch.Tensor, bits: int, symmetric: bool = True) -> Tuple[torch.Tensor, float, float]:
|
| 229 |
-
"""
|
| 230 |
-
Linearly quantize a tensor to specified bit-width
|
| 231 |
-
|
| 232 |
-
Args:
|
| 233 |
-
tensor: Input tensor to quantize
|
| 234 |
-
bits: Number of bits for quantization (e.g., 4, 8)
|
| 235 |
-
symmetric: Whether to use symmetric or asymmetric quantization
|
| 236 |
-
|
| 237 |
-
Returns:
|
| 238 |
-
Tuple of (quantized_tensor, scale, zero_point)
|
| 239 |
-
"""
|
| 240 |
-
# Calculate range
|
| 241 |
-
if symmetric:
|
| 242 |
-
max_val = torch.max(torch.abs(tensor))
|
| 243 |
-
min_val = -max_val
|
| 244 |
-
else:
|
| 245 |
-
max_val = tensor.max()
|
| 246 |
-
min_val = tensor.min()
|
| 247 |
-
|
| 248 |
-
# Calculate scale and zero point
|
| 249 |
-
qmin = 0
|
| 250 |
-
qmax = 2 ** bits - 1
|
| 251 |
-
scale = (max_val - min_val) / (qmax - qmin)
|
| 252 |
-
|
| 253 |
-
if symmetric:
|
| 254 |
-
zero_point = 0.0
|
| 255 |
-
else:
|
| 256 |
-
zero_point = qmin - min_val / scale
|
| 257 |
-
|
| 258 |
-
# Quantize
|
| 259 |
-
quantized = torch.round(tensor / scale + zero_point).clamp(qmin, qmax)
|
| 260 |
-
|
| 261 |
-
return quantized.to(torch.uint8), scale.item(), zero_point
|
| 262 |
-
|
| 263 |
-
@staticmethod
|
| 264 |
-
def linear_dequantize(quantized: torch.Tensor, scale: float, zero_point: float) -> torch.Tensor:
|
| 265 |
-
"""Dequantize tensor back to floating point"""
|
| 266 |
-
return (quantized.float() - zero_point) * scale
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
class PostTrainingQuantizer:
|
| 270 |
-
"""Post-training quantization for transformer models"""
|
| 271 |
-
|
| 272 |
-
def __init__(self, bits: int = 8):
|
| 273 |
-
self.bits = bits
|
| 274 |
-
self.quant_params = {}
|
| 275 |
-
|
| 276 |
-
def quantize_model(self, model: nn.Module) -> nn.Module:
|
| 277 |
-
"""Quantize all linear layers in the model"""
|
| 278 |
-
for name, module in model.named_modules():
|
| 279 |
-
if isinstance(module, nn.Linear):
|
| 280 |
-
# Store original weight
|
| 281 |
-
weight = module.weight.data
|
| 282 |
-
|
| 283 |
-
# Quantize weight
|
| 284 |
-
q_weight, scale, zero_point = Quantizer.linear_quantize(
|
| 285 |
-
weight
|
| 286 |
-
"""
|
| 287 |
|
| 288 |
~~~
|
| 289 |
|
|
@@ -293,10 +77,10 @@ Here is the sample command to reproduce the model
|
|
| 293 |
|
| 294 |
```python
|
| 295 |
import torch
|
| 296 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 297 |
from auto_round import AutoRound
|
| 298 |
|
| 299 |
-
model_name = "
|
| 300 |
|
| 301 |
model = AutoModelForCausalLM.from_pretrained(model_name,
|
| 302 |
device_map="cpu", torch_dtype="auto")
|
|
@@ -305,12 +89,30 @@ tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
| 305 |
|
| 306 |
layer_config = {}
|
| 307 |
for n, m in model.named_modules():
|
| 308 |
-
if
|
|
|
|
|
|
|
| 309 |
layer_config[n] = {"bits": 8, "group_size": 128}
|
| 310 |
|
| 311 |
autoround = AutoRound(model, tokenizer, iters=0, group_size=64, layer_config=layer_config)
|
| 312 |
-
|
| 313 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
```
|
| 315 |
|
| 316 |
|
|
|
|
| 6 |
|
| 7 |
## Model Details
|
| 8 |
|
| 9 |
+
This model is a mixed int4 model with group_size 64 and symmetric quantization of [Qwen/Qwen3-Coder-480B-A35B-Instruct](https://huggingface.co/Qwen/Qwen3-Coder-480B-A35B-Instruct) generated by [intel/auto-round](https://github.com/intel/auto-round) via **RTN** (no algorithm tuning).
|
| 10 |
+
Non expert layers fallback to 8 bits and group_size 128. mlp.gate layers fallback to 16 bits to ensure runing successfully on vLLM.
|
| 11 |
|
| 12 |
Please follow the license of the original model.
|
| 13 |
|
| 14 |
## How To Use
|
| 15 |
|
| 16 |
+
**vLLM usage**
|
| 17 |
+
~~~bash
|
| 18 |
+
vllm serve Intel/Qwen3-Coder-480B-A35B-Instruct-int4-mixed-ar --tensor-parallel-size 4 --max-model-len 65536
|
| 19 |
+
~~~
|
| 20 |
**INT4 Inference on CPU/Intel GPU/CUDA**
|
| 21 |
|
| 22 |
~~~python
|
| 23 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 24 |
|
| 25 |
+
model_name = "Intel/Qwen3-Coder-480B-A35B-Instruct-int4-mixed-ar"
|
| 26 |
|
| 27 |
# load the tokenizer and the model
|
| 28 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
|
| 68 |
print(f"Generated: {decoded_outputs[i]}")
|
| 69 |
print("-" * 50)
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
~~~
|
| 73 |
|
|
|
|
| 77 |
|
| 78 |
```python
|
| 79 |
import torch
|
| 80 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, AutoConfig
|
| 81 |
from auto_round import AutoRound
|
| 82 |
|
| 83 |
+
model_name = "Qwen/Qwen3-Coder-480B-A35B-Instruct"
|
| 84 |
|
| 85 |
model = AutoModelForCausalLM.from_pretrained(model_name,
|
| 86 |
device_map="cpu", torch_dtype="auto")
|
|
|
|
| 89 |
|
| 90 |
layer_config = {}
|
| 91 |
for n, m in model.named_modules():
|
| 92 |
+
if "mlp.gate" in n: ## vllm only support 16 bit for this layer
|
| 93 |
+
layer_config[n] = {"bits": 16}
|
| 94 |
+
elif isinstance(m, torch.nn.Linear) and (not "expert" in n or "shared_experts" in n) and n != "lm_head":
|
| 95 |
layer_config[n] = {"bits": 8, "group_size": 128}
|
| 96 |
|
| 97 |
autoround = AutoRound(model, tokenizer, iters=0, group_size=64, layer_config=layer_config)
|
| 98 |
+
output_dir = "/dataset/Qwen3-Coder-480B-A35B-Instruct-int4-mixed"
|
| 99 |
+
autoround.quantize_and_save(output_dir)
|
| 100 |
+
|
| 101 |
+
## tricky code to handle qkv fusing issue, we will fix it in vllm later
|
| 102 |
+
import os
|
| 103 |
+
import json
|
| 104 |
+
|
| 105 |
+
config_path = os.path.join(output_dir, "config.json")
|
| 106 |
+
|
| 107 |
+
with open(config_path, "r") as file:
|
| 108 |
+
config = json.load(file)
|
| 109 |
+
extra_config = config["quantization_config"]["extra_config"]
|
| 110 |
+
num_hidden_layers = config["num_hidden_layers"]
|
| 111 |
+
for i in range(num_hidden_layers):
|
| 112 |
+
qkv_name = f"model.layers.{str(i)}.self_attn.qkv_proj"
|
| 113 |
+
extra_config[qkv_name] = {"bits": 8, "group_size": 128}
|
| 114 |
+
with open(config_path, "w") as file:
|
| 115 |
+
json.dump(config, file, indent=2)
|
| 116 |
```
|
| 117 |
|
| 118 |
|