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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
# /// script
|
| 3 |
+
# dependencies = [
|
| 4 |
+
# "transformers>=4.36.0",
|
| 5 |
+
# "peft>=0.7.0",
|
| 6 |
+
# "torch>=2.0.0",
|
| 7 |
+
# "accelerate>=0.24.0",
|
| 8 |
+
# "huggingface_hub>=0.20.0",
|
| 9 |
+
# "sentencepiece>=0.1.99",
|
| 10 |
+
# "protobuf>=3.20.0",
|
| 11 |
+
# "numpy",
|
| 12 |
+
# "gguf",
|
| 13 |
+
# ]
|
| 14 |
+
# ///
|
| 15 |
+
|
| 16 |
+
"""GGUF Conversion - Q8_0 Quantization"""
|
| 17 |
+
|
| 18 |
+
import os
|
| 19 |
+
import torch
|
| 20 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 21 |
+
from peft import PeftModel
|
| 22 |
+
from huggingface_hub import HfApi
|
| 23 |
+
import subprocess
|
| 24 |
+
|
| 25 |
+
print("π GGUF Conversion Script - Q8_0")
|
| 26 |
+
print("=" * 60)
|
| 27 |
+
|
| 28 |
+
ADAPTER_MODEL = "chaddy81/qwen3-0.6b-multicode-sft"
|
| 29 |
+
BASE_MODEL = "Qwen/Qwen3-0.6B"
|
| 30 |
+
OUTPUT_REPO = "chaddy81/qwen3-0.6b-multicode-sft-gguf"
|
| 31 |
+
|
| 32 |
+
print(f"\nπ¦ Configuration:")
|
| 33 |
+
print(f" Base model: {BASE_MODEL}")
|
| 34 |
+
print(f" Adapter model: {ADAPTER_MODEL}")
|
| 35 |
+
print(f" Output repo: {OUTPUT_REPO}")
|
| 36 |
+
print(f" Quantization: Q8_0 (8-bit)")
|
| 37 |
+
|
| 38 |
+
# Step 1: Load and merge
|
| 39 |
+
print("\nπ§ Step 1: Loading base model and LoRA adapter...")
|
| 40 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 41 |
+
BASE_MODEL, torch_dtype=torch.float16, device_map="auto", trust_remote_code=True,
|
| 42 |
+
)
|
| 43 |
+
print(" β
Base model loaded")
|
| 44 |
+
|
| 45 |
+
model = PeftModel.from_pretrained(base_model, ADAPTER_MODEL)
|
| 46 |
+
print(" β
Adapter loaded")
|
| 47 |
+
|
| 48 |
+
merged_model = model.merge_and_unload()
|
| 49 |
+
print(" β
Models merged!")
|
| 50 |
+
|
| 51 |
+
tokenizer = AutoTokenizer.from_pretrained(ADAPTER_MODEL, trust_remote_code=True)
|
| 52 |
+
print(" β
Tokenizer loaded")
|
| 53 |
+
|
| 54 |
+
# Step 2: Save merged model
|
| 55 |
+
print("\nπΎ Step 2: Saving merged model...")
|
| 56 |
+
merged_dir = "/tmp/merged_model"
|
| 57 |
+
merged_model.save_pretrained(merged_dir, safe_serialization=True)
|
| 58 |
+
tokenizer.save_pretrained(merged_dir)
|
| 59 |
+
print(f" β
Merged model saved")
|
| 60 |
+
|
| 61 |
+
# Step 3: Setup llama.cpp
|
| 62 |
+
print("\nπ₯ Step 3: Setting up llama.cpp...")
|
| 63 |
+
subprocess.run(["apt-get", "update", "-qq"], check=True, capture_output=True)
|
| 64 |
+
subprocess.run(["apt-get", "install", "-y", "-qq", "build-essential", "cmake"], check=True, capture_output=True)
|
| 65 |
+
print(" β
Build tools installed")
|
| 66 |
+
|
| 67 |
+
subprocess.run(["git", "clone", "--depth", "1", "https://github.com/ggerganov/llama.cpp.git", "/tmp/llama.cpp"], check=True, capture_output=True)
|
| 68 |
+
print(" β
llama.cpp cloned")
|
| 69 |
+
|
| 70 |
+
subprocess.run(["pip", "install", "-r", "/tmp/llama.cpp/requirements.txt"], check=True, capture_output=True)
|
| 71 |
+
subprocess.run(["pip", "install", "sentencepiece", "protobuf"], check=True, capture_output=True)
|
| 72 |
+
print(" β
Dependencies installed")
|
| 73 |
+
|
| 74 |
+
# Step 4: Convert to GGUF (FP16)
|
| 75 |
+
print("\nπ Step 4: Converting to GGUF format...")
|
| 76 |
+
gguf_output_dir = "/tmp/gguf_output"
|
| 77 |
+
os.makedirs(gguf_output_dir, exist_ok=True)
|
| 78 |
+
|
| 79 |
+
model_name = "qwen3-0.6b-multicode-sft"
|
| 80 |
+
gguf_file = f"{gguf_output_dir}/{model_name}-f16.gguf"
|
| 81 |
+
|
| 82 |
+
result = subprocess.run(
|
| 83 |
+
["python", "/tmp/llama.cpp/convert_hf_to_gguf.py", merged_dir, "--outfile", gguf_file, "--outtype", "f16"],
|
| 84 |
+
check=True, capture_output=True, text=True
|
| 85 |
+
)
|
| 86 |
+
print(f" β
FP16 GGUF created")
|
| 87 |
+
|
| 88 |
+
# Step 5: Build quantize and create Q8_0
|
| 89 |
+
print("\nβοΈ Step 5: Creating Q8_0 quantization...")
|
| 90 |
+
os.makedirs("/tmp/llama.cpp/build", exist_ok=True)
|
| 91 |
+
subprocess.run(
|
| 92 |
+
["cmake", "-B", "/tmp/llama.cpp/build", "-S", "/tmp/llama.cpp", "-DGGML_CUDA=OFF"],
|
| 93 |
+
check=True, capture_output=True, text=True
|
| 94 |
+
)
|
| 95 |
+
subprocess.run(
|
| 96 |
+
["cmake", "--build", "/tmp/llama.cpp/build", "--target", "llama-quantize", "-j", "4"],
|
| 97 |
+
check=True, capture_output=True, text=True
|
| 98 |
+
)
|
| 99 |
+
print(" β
Quantize tool built")
|
| 100 |
+
|
| 101 |
+
quant_file = f"{gguf_output_dir}/{model_name}-q8_0.gguf"
|
| 102 |
+
result = subprocess.run(
|
| 103 |
+
["/tmp/llama.cpp/build/bin/llama-quantize", gguf_file, quant_file, "Q8_0"],
|
| 104 |
+
check=True, capture_output=True, text=True
|
| 105 |
+
)
|
| 106 |
+
size_mb = os.path.getsize(quant_file) / (1024 * 1024)
|
| 107 |
+
print(f" β
Q8_0: {size_mb:.1f} MB")
|
| 108 |
+
|
| 109 |
+
# Step 6: Upload
|
| 110 |
+
print("\nβοΈ Step 6: Uploading to Hub...")
|
| 111 |
+
api = HfApi()
|
| 112 |
+
api.create_repo(repo_id=OUTPUT_REPO, repo_type="model", exist_ok=True)
|
| 113 |
+
print(" β
Repository ready")
|
| 114 |
+
|
| 115 |
+
api.upload_file(path_or_fileobj=quant_file, path_in_repo=f"{model_name}-q8_0.gguf", repo_id=OUTPUT_REPO)
|
| 116 |
+
print(" β
Q8_0 uploaded")
|
| 117 |
+
|
| 118 |
+
readme = f"""---
|
| 119 |
+
base_model: {BASE_MODEL}
|
| 120 |
+
tags:
|
| 121 |
+
- gguf
|
| 122 |
+
- llama.cpp
|
| 123 |
+
- quantized
|
| 124 |
+
- trl
|
| 125 |
+
- sft
|
| 126 |
+
- qwen3
|
| 127 |
+
- code
|
| 128 |
+
---
|
| 129 |
+
|
| 130 |
+
# {model_name} GGUF
|
| 131 |
+
|
| 132 |
+
GGUF conversion of [{ADAPTER_MODEL}](https://huggingface.co/{ADAPTER_MODEL}).
|
| 133 |
+
|
| 134 |
+
## Model Details
|
| 135 |
+
|
| 136 |
+
- **Base Model:** {BASE_MODEL}
|
| 137 |
+
- **Fine-tuned Model:** {ADAPTER_MODEL}
|
| 138 |
+
- **Training:** SFT on code datasets (Codeforces, Golang, Vue/Nuxt, React)
|
| 139 |
+
- **Quantization:** Q8_0 (8-bit, high quality)
|
| 140 |
+
|
| 141 |
+
## Usage
|
| 142 |
+
|
| 143 |
+
### With Ollama
|
| 144 |
+
|
| 145 |
+
```bash
|
| 146 |
+
huggingface-cli download {OUTPUT_REPO} {model_name}-q8_0.gguf
|
| 147 |
+
echo "FROM ./{model_name}-q8_0.gguf" > Modelfile
|
| 148 |
+
ollama create qwen3-multicode -f Modelfile
|
| 149 |
+
ollama run qwen3-multicode
|
| 150 |
+
```
|
| 151 |
+
|
| 152 |
+
### With llama.cpp
|
| 153 |
+
|
| 154 |
+
```bash
|
| 155 |
+
huggingface-cli download {OUTPUT_REPO} {model_name}-q8_0.gguf
|
| 156 |
+
./llama-cli -m {model_name}-q8_0.gguf -p "Write a React component"
|
| 157 |
+
```
|
| 158 |
+
|
| 159 |
+
### With LM Studio
|
| 160 |
+
|
| 161 |
+
Download the `.gguf` file and import into LM Studio.
|
| 162 |
+
"""
|
| 163 |
+
|
| 164 |
+
api.upload_file(path_or_fileobj=readme.encode(), path_in_repo="README.md", repo_id=OUTPUT_REPO)
|
| 165 |
+
print(" β
README uploaded")
|
| 166 |
+
|
| 167 |
+
print("\n" + "=" * 60)
|
| 168 |
+
print("β
GGUF Conversion Complete!")
|
| 169 |
+
print(f"π¦ https://huggingface.co/{OUTPUT_REPO}")
|
| 170 |
+
print(f"π₯ huggingface-cli download {OUTPUT_REPO} {model_name}-q8_0.gguf")
|
| 171 |
+
print("=" * 60)
|