| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| """GGUF Conversion - Q8_0 Quantization""" |
|
|
| import os |
| import torch |
| from transformers import AutoModelForCausalLM, AutoTokenizer |
| from peft import PeftModel |
| from huggingface_hub import HfApi |
| import subprocess |
|
|
| print("π GGUF Conversion Script - Q8_0") |
| print("=" * 60) |
|
|
| ADAPTER_MODEL = "chaddy81/qwen3-0.6b-multicode-sft" |
| BASE_MODEL = "Qwen/Qwen3-0.6B" |
| OUTPUT_REPO = "chaddy81/qwen3-0.6b-multicode-sft-gguf" |
|
|
| print(f"\nπ¦ Configuration:") |
| print(f" Base model: {BASE_MODEL}") |
| print(f" Adapter model: {ADAPTER_MODEL}") |
| print(f" Output repo: {OUTPUT_REPO}") |
| print(f" Quantization: Q8_0 (8-bit)") |
|
|
| |
| print("\nπ§ Step 1: Loading base model and LoRA adapter...") |
| base_model = AutoModelForCausalLM.from_pretrained( |
| BASE_MODEL, torch_dtype=torch.float16, device_map="auto", trust_remote_code=True, |
| ) |
| print(" β
Base model loaded") |
|
|
| model = PeftModel.from_pretrained(base_model, ADAPTER_MODEL) |
| print(" β
Adapter loaded") |
|
|
| merged_model = model.merge_and_unload() |
| print(" β
Models merged!") |
|
|
| tokenizer = AutoTokenizer.from_pretrained(ADAPTER_MODEL, trust_remote_code=True) |
| print(" β
Tokenizer loaded") |
|
|
| |
| print("\nπΎ Step 2: Saving merged model...") |
| merged_dir = "/tmp/merged_model" |
| merged_model.save_pretrained(merged_dir, safe_serialization=True) |
| tokenizer.save_pretrained(merged_dir) |
| print(f" β
Merged model saved") |
|
|
| |
| print("\nπ₯ Step 3: Setting up llama.cpp...") |
| subprocess.run(["apt-get", "update", "-qq"], check=True, capture_output=True) |
| subprocess.run(["apt-get", "install", "-y", "-qq", "build-essential", "cmake"], check=True, capture_output=True) |
| print(" β
Build tools installed") |
|
|
| subprocess.run(["git", "clone", "--depth", "1", "https://github.com/ggerganov/llama.cpp.git", "/tmp/llama.cpp"], check=True, capture_output=True) |
| print(" β
llama.cpp cloned") |
|
|
| subprocess.run(["pip", "install", "-r", "/tmp/llama.cpp/requirements.txt"], check=True, capture_output=True) |
| subprocess.run(["pip", "install", "sentencepiece", "protobuf"], check=True, capture_output=True) |
| print(" β
Dependencies installed") |
|
|
| |
| print("\nπ Step 4: Converting to GGUF format...") |
| gguf_output_dir = "/tmp/gguf_output" |
| os.makedirs(gguf_output_dir, exist_ok=True) |
|
|
| model_name = "qwen3-0.6b-multicode-sft" |
| gguf_file = f"{gguf_output_dir}/{model_name}-f16.gguf" |
|
|
| result = subprocess.run( |
| ["python", "/tmp/llama.cpp/convert_hf_to_gguf.py", merged_dir, "--outfile", gguf_file, "--outtype", "f16"], |
| check=True, capture_output=True, text=True |
| ) |
| print(f" β
FP16 GGUF created") |
|
|
| |
| print("\nβοΈ Step 5: Creating Q8_0 quantization...") |
| os.makedirs("/tmp/llama.cpp/build", exist_ok=True) |
| subprocess.run( |
| ["cmake", "-B", "/tmp/llama.cpp/build", "-S", "/tmp/llama.cpp", "-DGGML_CUDA=OFF"], |
| check=True, capture_output=True, text=True |
| ) |
| subprocess.run( |
| ["cmake", "--build", "/tmp/llama.cpp/build", "--target", "llama-quantize", "-j", "4"], |
| check=True, capture_output=True, text=True |
| ) |
| print(" β
Quantize tool built") |
|
|
| quant_file = f"{gguf_output_dir}/{model_name}-q8_0.gguf" |
| result = subprocess.run( |
| ["/tmp/llama.cpp/build/bin/llama-quantize", gguf_file, quant_file, "Q8_0"], |
| check=True, capture_output=True, text=True |
| ) |
| size_mb = os.path.getsize(quant_file) / (1024 * 1024) |
| print(f" β
Q8_0: {size_mb:.1f} MB") |
|
|
| |
| print("\nβοΈ Step 6: Uploading to Hub...") |
| api = HfApi() |
| api.create_repo(repo_id=OUTPUT_REPO, repo_type="model", exist_ok=True) |
| print(" β
Repository ready") |
|
|
| api.upload_file(path_or_fileobj=quant_file, path_in_repo=f"{model_name}-q8_0.gguf", repo_id=OUTPUT_REPO) |
| print(" β
Q8_0 uploaded") |
|
|
| readme = f"""--- |
| base_model: {BASE_MODEL} |
| tags: |
| - gguf |
| - llama.cpp |
| - quantized |
| - trl |
| - sft |
| - qwen3 |
| - code |
| --- |
| |
| # {model_name} GGUF |
| |
| GGUF conversion of [{ADAPTER_MODEL}](https://huggingface.co/{ADAPTER_MODEL}). |
| |
| ## Model Details |
| |
| - **Base Model:** {BASE_MODEL} |
| - **Fine-tuned Model:** {ADAPTER_MODEL} |
| - **Training:** SFT on code datasets (Codeforces, Golang, Vue/Nuxt, React) |
| - **Quantization:** Q8_0 (8-bit, high quality) |
| |
| ## Usage |
| |
| ### With Ollama |
| |
| ```bash |
| huggingface-cli download {OUTPUT_REPO} {model_name}-q8_0.gguf |
| echo "FROM ./{model_name}-q8_0.gguf" > Modelfile |
| ollama create qwen3-multicode -f Modelfile |
| ollama run qwen3-multicode |
| ``` |
| |
| ### With llama.cpp |
| |
| ```bash |
| huggingface-cli download {OUTPUT_REPO} {model_name}-q8_0.gguf |
| ./llama-cli -m {model_name}-q8_0.gguf -p "Write a React component" |
| ``` |
| |
| ### With LM Studio |
| |
| Download the `.gguf` file and import into LM Studio. |
| """ |
|
|
| api.upload_file(path_or_fileobj=readme.encode(), path_in_repo="README.md", repo_id=OUTPUT_REPO) |
| print(" β
README uploaded") |
|
|
| print("\n" + "=" * 60) |
| print("β
GGUF Conversion Complete!") |
| print(f"π¦ https://huggingface.co/{OUTPUT_REPO}") |
| print(f"π₯ huggingface-cli download {OUTPUT_REPO} {model_name}-q8_0.gguf") |
| print("=" * 60) |
|
|