#!/usr/bin/env python3 # /// script # dependencies = [ # "transformers>=4.36.0", # "peft>=0.7.0", # "torch>=2.0.0", # "accelerate>=0.24.0", # "huggingface_hub>=0.20.0", # "sentencepiece>=0.1.99", # "protobuf>=3.20.0", # "numpy", # "gguf", # ] # /// """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)") # Step 1: Load and merge 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") # Step 2: Save merged model 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") # Step 3: Setup llama.cpp 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") # Step 4: Convert to GGUF (FP16) 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") # Step 5: Build quantize and create Q8_0 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") # Step 6: Upload 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)