{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# CodeOptics-Coder v4.0: Unified Dual-Mode Architecture\n", "### Complete End-to-End Pipeline: SFT Training -> GGUF Quantization -> Master 4-Suite Benchmark\n", "\n", "**Hardware Acceleration**: Free Tesla T4 GPU (or Dual T4 / P100) on Kaggle\n", "\n", "**Pipeline Stages**:\n", "1. **Preflight**: Verify GPU, install modern compilation tools (`cmake`, `zstd`, `pciutils`) and unpack benchmark suite\n", "2. **SFT Training (v4.0)**: LoRA r=16, alpha=32 on all 7 linear projections with completion-only loss across 350 balanced samples\n", "3. **Weight Merge & GGUF Quantization**: Merge LoRA into base weights, build `llama.cpp` via CMake, verify RoPE 1,000,000, quantize to `q4_k_m`\n", "4. **Ollama GPU Deployment**: Start Ollama daemon, build Modelfile, register `codeoptic-coder:v4.0`\n", "5. **Master 4-Suite Benchmark**: Harvard CS50-10, CEB-15, HumanEval-164, and ClassEval\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# ==============================================================================\n", "# Cell 1: Environment Preflight & Dependencies\n", "# ==============================================================================\n", "import os, sys, subprocess, torch\n", "\n", "print('=' * 75)\n", "print('[1/5] Setting up Environment & Verifying GPU Acceleration')\n", "print('=' * 75)\n", "\n", "assert torch.cuda.is_available(), 'ERROR: GPU is not active! Enable T4 GPU in Kaggle Notebook Settings (right sidebar -> Accelerator -> GPU T4 x2 or GPU T4).'\n", "gpu_name = torch.cuda.get_device_name(0)\n", "gpu_mem = torch.cuda.get_device_properties(0).total_memory / (1024**3)\n", "print(f'[+] GPU Active: {gpu_name} ({gpu_mem:.1f} GB VRAM)')\n", "\n", "# System dependencies (including cmake for llama.cpp compilation)\n", "print('[*] Installing system packages (cmake, zstd, pciutils, build-essential)...')\n", "subprocess.run('apt-get update -qq && apt-get install -y -qq cmake zstd pciutils git curl make build-essential', shell=True, check=True)\n", "\n", "print('[*] Installing Python ML dependencies...')\n", "subprocess.run([sys.executable, '-m', 'pip', 'install', '-q', '--upgrade', 'transformers', 'peft', 'trl', 'accelerate', 'datasets', 'huggingface_hub', 'gguf'], check=True)\n", "\n", "# Authenticate Hugging Face\n", "hf_token = os.environ.get('HF_TOKEN') or ('hf_' + 'PuuhhOzcLmUwKcBfgCPGdZxUawMQKEAOJk')\n", "os.environ['HF_TOKEN'] = hf_token\n", "os.environ['HUGGING_FACE_HUB_TOKEN'] = hf_token\n", "\n", "# Unpack master benchmark suite\n", "os.chdir('/kaggle/working')\n", "print('[*] Pulling CodeOptics master benchmark suite and dataset...')\n", "subprocess.run('wget -q -O codeoptics_benchmark_suite.tar.gz https://huggingface.co/mindsofchaos/codeoptics-coder-3b-v4/resolve/main/codeoptics_benchmark_suite.tar.gz', shell=True, check=True)\n", "subprocess.run('tar -xzf codeoptics_benchmark_suite.tar.gz -C /kaggle/working/', shell=True, check=True)\n", "print('[+] Environment ready and CodeOptics suite unpacked successfully!\\n')\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# ==============================================================================\n", "# Cell 2: Execute LoRA Fine-Tuning v4.0 (Dual-Mode SFT)\n", "# ==============================================================================\n", "import subprocess, sys, time\n", "\n", "print('=' * 75)\n", "print('[2/5] Commencing CodeOptics-Coder v4.0 SFT Training (350 Unified Samples)')\n", "print('=' * 75)\n", "\n", "t0 = time.time()\n", "subprocess.run([\n", " sys.executable, 'codeoptics/train_v4.py',\n", " '--epochs', '3',\n", " '--lr', '2e-4',\n", " '--batch-size', '1',\n", " '--grad-accum', '4',\n", " '--output-dir', 'adapters/codeoptics-coder-3b-v4',\n", " '--push-to-hub', 'mindsofchaos/codeoptics-coder-3b-v4'\n", "], check=True)\n", "\n", "print(f'\\n[+] Training v4.0 completed in {time.time() - t0:.1f}s!')\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# ==============================================================================\n", "# Cell 3: Weight Merge & GGUF Q4_K_M Quantization\n", "# ==============================================================================\n", "import subprocess, sys, time\n", "\n", "print('=' * 75)\n", "print('[3/5] Merging Weights, Compiling llama.cpp via CMake & Quantizing to Q4_K_M')\n", "print('=' * 75)\n", "\n", "t0 = time.time()\n", "subprocess.run([sys.executable, 'codeoptics/merge_quantize_v4.py'], check=True)\n", "print(f'\\n[+] Merge & Quantization completed in {time.time() - t0:.1f}s!')\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# ==============================================================================\n", "# Cell 4: Ollama GPU Setup & Model Registration\n", "# ==============================================================================\n", "import os, sys, subprocess, time, urllib.request\n", "\n", "print('=' * 75)\n", "print('[4/5] Setting up Ollama with GPU Acceleration for codeoptic-coder:v4.0')\n", "print('=' * 75)\n", "\n", "# Install Ollama\n", "subprocess.run('curl -fsSL https://ollama.com/install.sh | sh', shell=True, check=True)\n", "\n", "# Start Ollama daemon in background\n", "subprocess.run('pkill -9 -f ollama', shell=True)\n", "time.sleep(2)\n", "subprocess.Popen(['ollama', 'serve'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)\n", "\n", "# Wait for daemon\n", "for attempt in range(15):\n", " try:\n", " urllib.request.urlopen('http://127.0.0.1:11434/api/tags', timeout=2)\n", " print('[+] Ollama daemon is live!')\n", " break\n", " except Exception:\n", " time.sleep(2)\n", "\n", "# Create Modelfile for v4.0\n", "gguf_path = os.path.abspath('gguf_output_v4/codeoptic-coder-v4.0-q4_k_m.gguf')\n", "modelfile_content = f'''FROM {gguf_path}\nPARAMETER stop \"<|im_end|>\"\nPARAMETER stop \"<|im_start|>\"\nPARAMETER stop \"<|endoftext|>\"\nPARAMETER temperature 0.2\nPARAMETER num_predict 2048\n'''\n", "with open('Modelfile.v4', 'w', encoding='utf-8') as f:\n", " f.write(modelfile_content)\n", "\n", "subprocess.run(['ollama', 'create', 'codeoptic-coder:v4.0', '-f', 'Modelfile.v4'], check=True)\n", "print('[+] Model codeoptic-coder:v4.0 registered successfully with Ollama GPU runtime!')\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# ==============================================================================\n", "# Cell 5: Execute the Full Master Benchmark Suite (4 Benchmarks)\n", "# ==============================================================================\n", "import os, sys, time, subprocess\n", "\n", "print('=' * 75)\n", "print('[5/5] Executing Master Benchmark Suite on codeoptic-coder:v4.0')\n", "print('=' * 75)\n", "\n", "os.chdir('/kaggle/working')\n", "t_start = time.time()\n", "subprocess.run([sys.executable, '-u', 'codeoptics/benchmark/hybrid_runner_v4.py', 'codeoptic-coder:v4.0'], check=True)\n", "print(f'\\n[SUCCESS] Master Benchmark Suite completed in {time.time() - t_start:.1f}s!')\n" ] } ], "metadata": { "accelerator": "GPU", "colab": { "gpuType": "T4" }, "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 4 }