File size: 17,189 Bytes
0f999d0 9a87fa7 0f999d0 9a87fa7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 |
---
tags:
- gptq
- quantization
- 4bit
- confidentialmind
- text-generation
- apache2.0
- arcee-ai
license: apache-2.0
base_model:
- arcee-ai/Virtuoso-Medium-v2
---
# π₯ Quantized Model: Virtuoso-Medium-v2_gptq_g32_4bit π₯
This is a 4-bit quantized version of [arcee-ai/Virtuoso-Medium-v2](https://huggingface.co/arcee-ai/Virtuoso-Medium-v2) model, quantized by [ConfidentialMind.com](https://www.confidentialmind.com) π€β¨
It leverages the open-source GPTQModel quantization to achieve 4-bit precision with a group size of 128 resulting in a
smaller,
faster model with minimal performance degradation.
Ran on a single NVIDIA A100 GPU with 80GB of VRAM.
*Note* `batch_size` is set quite high as the model is small, you may need to adjust this to your GPU VRAM.
## Model Details
- **Original Model:** [arcee-ai/Virtuoso-Medium-v2](https://huggingface.co/arcee-ai/Virtuoso-Medium-v2)
- **Quantized Model:** Virtuoso-Medium-v2_gptq_g32_4bit (this repository)
- **Quantization Method:** GPTQ (4-bit, group size 128)
- **Quantization Library:** [GPTQModel](https://github.com/ModelCloud/GPTQModel/tree/main)
- **Calibration Dataset:** neuralmagic/LLM_compression_calibration (using 256 samples with seq len 4096)
- **Quantized by:** [ConfidentialMind.com](https://www.confidentialmind.com)
## Usage
```python
from gptqmodel import GPTQModel
from transformers import AutoTokenizer
# Use the local directory or JustJaro/Virtuoso-Medium-v2_gptq_g32_4bit after upload
quantized_model_id = "/home/jaro/models/quantized/Virtuoso-Medium-v2_gptq_g32_4bit" # or "JustJaro/Virtuoso-Medium-v2_gptq_g32_4bit"
tokenizer = AutoTokenizer.from_pretrained(quantized_model_id)
model = GPTQModel.load(quantized_model_id, device="cuda:0") # or "cpu"
input_text = "This is a test prompt"
inputs = tokenizer(input_text, return_tensors="pt").to("cuda:0")
outputs = model.generate(**inputs)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
```
## Package Versions and Installation Instructions
See pyproject.toml for the exact UV project file. See the [GPTQModel](
https://github.com/ModelCloud/GPTQModel/tree/main) repo for more details. on how to install the package.
Use the provided pyproject.toml:
```bash
uv venv
source venv/bin/activate
uv sync
```
### Environment Variables
```bash
HF_TOKEN=<YOUR_HF_TOKEN>
TOKENIZERS_PARALLELISM="true"
PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True
```
## Quantization Script
Below is the exact quantize.py script used to generate this model (with the exact versions of the dependencies):
```python
#!/usr/bin/env python3
"""
This script loads a source Hugging Face model and a calibration dataset,
quantizes the model using GPTQModel (with 4-bit precision and group size 128),
saves the quantized model using the Transformers API with safetensors (safe serialization)
under ~/models/quantized/, and then creates/updates a Hugging Face repository (with the
_gptq_g128_4bit suffix) by uploading the model, tokenizer, and an auto-generated README.md.
Usage example:
python quantize.py --source-model TinyLlama/TinyLlama-1.1B-Chat-v1.0 \
--calibration-dataset wikitext/wikitext-2-raw-v1 \
--seq-len 1024 --nsamples 256 --hf-token <YOUR_HF_TOKEN>
"""
import os
import shutil
import subprocess
from enum import Enum
from pathlib import Path
from typing import List
import torch
import typer
from datasets import load_dataset
from dotenv import load_dotenv, find_dotenv
from gptqmodel import GPTQModel, QuantizeConfig
from gptqmodel.utils import Perplexity
# For later pushing to the model hub
from huggingface_hub import HfApi
from transformers import AutoTokenizer, PreTrainedTokenizerBase
load_dotenv(find_dotenv())
HF_TOKEN = os.getenv("HF_TOKEN")
app = typer.Typer()
class GroupSize(str, Enum):
accurate:int = 32
balanced:int = 64
fast:int = 128
def get_text_from_example(example: dict) -> str:
"""
Returns text from a dataset example.
If the example contains a "text" field, and it is nonempty, that text is used.
Otherwise, if it has a "messages" field (a list of dicts with a "content" key),
the function returns the concatenation of all non-empty message contents.
"""
if "text" in example and example["text"]:
return example["text"]
elif "messages" in example:
contents = [msg.get("content", "").strip() for msg in example["messages"]]
return " ".join([s for s in contents if s])
else:
return ""
def get_calibration_dataset(
tokenizer: PreTrainedTokenizerBase,
nsamples: int,
seqlen: int,
calibration_dataset: str
) -> List[dict]:
"""
Loads a calibration dataset from the Hugging Face Hub (or from a local file).
It accepts datasets with a single "text" field (like wikitext)
or with a "messages" field (as in the Neural Magic LLM Compression Calibration dataset).
Only examples whose extracted text length is at least 'seqlen' are kept.
Each chosen example is tokenized (with truncation up to 'seqlen') and returned as a dict.
"""
ds = None
try:
# Attempt to load from HF Hub.
try:
if "/" in calibration_dataset:
parts = calibration_dataset.split("/", 1)
ds = load_dataset(parts[0], parts[1], split="train")
else:
ds = load_dataset(calibration_dataset, split="train")
except Exception as e:
print(f"Error loading dataset '{calibration_dataset}' via load_dataset: {e}")
ds = load_dataset(calibration_dataset, split="train")
print(f"Loaded calibration dataset from full remote path {calibration_dataset}.")
except Exception as e:
print(f"Error loading dataset '{calibration_dataset}' via load_dataset: {e}")
# Fallback: if the supplied calibration_dataset is a local path, try to load it as JSON-lines.
if os.path.exists(calibration_dataset):
try:
ds = load_dataset("json", data_files=calibration_dataset, split="train")
print(f"Loaded calibration dataset from local file {calibration_dataset}.")
except Exception as e2:
print(f"Error loading local json dataset from '{calibration_dataset}': {e2}")
return []
else:
return []
print(f"Dataset features: {ds.features}")
# Filter examples that have at least 80% 'seqlen' of extracted text (wikitext-2-raw-v1 dataset has short examples).
ds = ds.filter(lambda x: len(get_text_from_example(x)) <= int(seqlen*0.8))
sample_range = min(nsamples, len(ds))
calibration_data = []
for i in range(sample_range):
example = ds[i]
text = get_text_from_example(example)
tokenized = tokenizer(text, truncation=True, max_length=seqlen, return_tensors="pt")
tokenized = {k: v.squeeze(0) for k, v in tokenized.items()}
calibration_data.append(tokenized)
return calibration_data
def calculate_avg_ppl(model, tokenizer):
"""
Computes the average perplexity on the wikitext-2-raw-v1 train split using GPTQModel's Perplexity utility.
"""
ppl = Perplexity(
model=model,
tokenizer=tokenizer,
dataset_path="wikitext",
dataset_name="wikitext-2-raw-v1",
split="train",
text_column="text",
)
ppl_values = ppl.calculate(n_ctx=512, n_batch=512)
avg = sum(ppl_values) / len(ppl_values)
return avg
def get_pinned_package_versions():
"""
Retrieves pinned package versions using 'uv pip freeze'.
Returns a dictionary mapping lowercased package names to their versions.
"""
try:
result = subprocess.run(["uv", "pip", "freeze"], capture_output=True, text=True, check=True)
packages_output = result.stdout.strip()
versions = {}
for line in packages_output.splitlines():
if "==" in line:
package_name, package_version = line.split("==", 1)
versions[package_name.lower()] = package_version
return versions
except subprocess.CalledProcessError as e:
typer.echo(f"Error running 'uv pip freeze': {e}", err=True)
return {}
except FileNotFoundError:
typer.echo("uv command not found. Make sure uv is installed and in your PATH.", err=True)
return {}
@app.command()
def main(
seq_len: int = typer.Option(4096, help="Sequence length for tokenization and calibration."),
nsamples: int = typer.Option(256, help="Number of samples to use for calibration."),
source_model: str = typer.Option("arcee-ai/Virtuoso-Medium-v2",
help="Source model HF repository identifier."),
calibration_dataset: str = typer.Option("wikitext/wikitext-2-raw-v1",
help="Calibration dataset identifier (in 'dataset/config' format) or local file path."),
hf_token: str = typer.Option(HF_TOKEN,
help="Hugging Face token for creating/updating your repo."),
upload_only: bool = typer.Option(False, help="Only upload the quantized model to the Hugging Face Hub."),
# Allow for 32, 64, 128 only using typer:
group_size: GroupSize = typer.Option(GroupSize.accurate, help="Group size for quantization accurate: 32, "
"balanced: 64, fast: 128. Default: accurate."),
):
# Prepare destination directory and model names.
model_name = source_model.split("/")[-1]
quantized_model_name = f"{model_name}_gptq_g{int(group_size.value)}_4bit"
quantized_model_dir = os.path.expanduser(os.path.join("~/models/quantized", quantized_model_name))
if not os.path.exists(quantized_model_dir) or not upload_only:
os.makedirs(quantized_model_dir, exist_ok=True)
os.makedirs(quantized_model_dir, exist_ok=True)
typer.echo("Loading tokenizer from source model...")
tokenizer_obj = AutoTokenizer.from_pretrained(source_model, use_fast=True)
typer.echo("Loading calibration dataset...")
typer.echo(f"Calibration dataset: {calibration_dataset}")
calibration_data = get_calibration_dataset(tokenizer_obj, nsamples, seq_len, calibration_dataset)
if not calibration_data:
typer.echo("Calibration dataset is empty. Aborting.", err=True)
raise typer.Exit(code=1)
quantize_config = QuantizeConfig(bits=4, group_size=int(group_size.value), damp_percent=0.01)
device = "cuda:0" if torch.cuda.is_available() else "cpu"
typer.echo(f"Loading model in {device} mode...")
model = GPTQModel.load(source_model, quantize_config)
typer.echo("Quantizing model...")
group_size_factor = int(128 / int(group_size.value))
model.quantize(calibration_data, auto_gc=False, batch_size=int((nsamples*0.1)/group_size_factor))
# Retrieve Hugging Face user info for README generation.
package_versions = get_pinned_package_versions()
username = get_my_user(hf_token)
script_content = self_read_script()
typer.echo(f"Saving quantized model to {quantized_model_dir} using Transformers safe serialization...")
try:
model.save_pretrained(quantized_model_dir)
tokenizer_obj.save_pretrained(quantized_model_dir)
except Exception as ex:
typer.echo(f"Error during saving with safe_serialization: {ex}. Aborting.")
raise
typer.echo(f"Model uploaded to Hugging Face repo: {quantized_model_name}")
else:
tokenizer_obj = AutoTokenizer.from_pretrained(source_model, use_fast=True)
package_versions = get_pinned_package_versions()
username = get_my_user(hf_token)
script_content = self_read_script()
device = "cuda:0" if torch.cuda.is_available() else "cpu"
model = GPTQModel.load(quantized_model_dir, device=device)
avg_ppl = calculate_avg_ppl(model, tokenizer_obj)
typer.echo(f"Average perplexity (PPL) on wikitext v2 dataset: {avg_ppl}")
deps = Path("./pyproject.toml")
shutil.copy(deps, quantized_model_dir)
generate_readme(calibration_dataset, nsamples, quantized_model_dir,
quantized_model_name, script_content, seq_len, source_model, username, avg_ppl)
GPTQModel.push_to_hub(quantized_path=quantized_model_dir, private=False, repo_id=quantized_model_name,
token=HF_TOKEN)
typer.echo(f"Model uploaded to Hugging Face repo: {quantized_model_name}")
demo_input = tokenizer_obj("test is", return_tensors="pt").to(device)
generated_ids = model.generate(**demo_input)
output_text = tokenizer_obj.decode(generated_ids[0])
typer.echo(f"Inference demo output: {output_text}")
typer.echo(f"Average perplexity (PPL) on calibration dataset: {avg_ppl}")
def self_read_script():
try:
script_path = os.path.abspath(__file__)
with open(script_path, "r") as f:
script_content = f.read()
except Exception as e:
script_content = "Error reading script content: " + str(e)
return script_content
def get_my_user(hf_token):
api = HfApi(token=hf_token)
user_info = api.whoami()
try:
username = user_info.get("name") or user_info.get("username")
except Exception as e:
typer.echo(f"Error retrieving username from Hugging Face API: {e}. Using default username.")
username = api.whoami()
if not username:
typer.echo("Could not determine your Hugging Face username from the token, defaulting to hard coded username.",
err=True)
username = "JustJaro"
return username
def generate_readme(calibration_dataset, nsamples, quantized_model_dir,
quantized_model_name, script_content, seq_len, source_model, username, avg_ppl):
readme_content = f"""---
tags:
- gptq
- quantization
- 4bit
- confidentialmind
- text-generation
- apache2.0
- mistral-small-24b
---
# π₯ Quantized Model: {quantized_model_name} π₯
This is a 4-bit quantized version of [{source_model}](https://huggingface.co/{source_model}) model, quantized by [ConfidentialMind.com](https://www.confidentialmind.com) π€β¨
It leverages the open-source GPTQModel quantization to achieve 4-bit precision with a group size of 128 resulting in a
smaller,
faster model with minimal performance degradation.
Ran on a single NVIDIA A100 GPU with 80GB of VRAM.
*Note* `batch_size` is set quite high as the model is small, you may need to adjust this to your GPU VRAM.
## Model Details
- **Original Model:** [{source_model}](https://huggingface.co/{source_model})
- **Quantized Model:** {quantized_model_name} (this repository)
- **Quantization Method:** GPTQ (4-bit, group size 128)
- **Quantization Library:** [GPTQModel](https://github.com/ModelCloud/GPTQModel/tree/main)
- **Calibration Dataset:** {calibration_dataset} (using {nsamples} samples with seq len {seq_len})
- **Quantized by:** [ConfidentialMind.com](https://www.confidentialmind.com)
## Usage
```python
from gptqmodel import GPTQModel
from transformers import AutoTokenizer
# Use the local directory or {username}/{quantized_model_name} after upload
quantized_model_id = "{quantized_model_dir}" # or "{username}/{quantized_model_name}"
tokenizer = AutoTokenizer.from_pretrained(quantized_model_id)
model = GPTQModel.load(quantized_model_id, device="cuda:0") # or "cpu"
input_text = "This is a test prompt"
inputs = tokenizer(input_text, return_tensors="pt").to("cuda:0")
outputs = model.generate(**inputs)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
```
## Package Versions and Installation Instructions
See pyproject.toml for the exact UV project file. See the [GPTQModel](
https://github.com/ModelCloud/GPTQModel/tree/main) repo for more details. on how to install the package.
Use the provided pyproject.toml:
```bash
uv venv
source venv/bin/activate
uv sync
```
### Environment Variables
```bash
HF_TOKEN=<YOUR_HF_TOKEN>
TOKENIZERS_PARALLELISM="true"
PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True
```
## Quantization Script
Below is the exact quantize.py script used to generate this model (with the exact versions of the dependencies):
```python
{script_content}
```
## Quantization Performance
Average perplexity (PPL) on wikitext v2 dataset: {avg_ppl}
## Disclaimer
This model is for research purposes only. It may inherit limitations and biases from the original model and the quantization process. Please use responsibly and refer to the original model card for more details.
## Contact
For any questions or support, please visit [ConfidentialMind.com](https://www.confidentialmind.com) or contact us directly.
## License
This model inherits the license from the original model. Please refer to the original model card for more details.
Original model card: `{source_model}`
## Author
This model was quantized by [Jaro](https://www.linkedin.com/in/jaroai/)
## Acknowledgements
Quantization performed using the GPTQModel pipeline.
TODO: Add `gptqmodel.utils.eval` integration and auto-generation of eval table. |