{ "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", "**Architecture Summary**:\n", "- **Base Model**: `Qwen/Qwen2.5-Coder-3B-Instruct`\n", "- **Dataset**: `codeoptics/data/epistemic_sft_train_v4.jsonl` (350 balanced samples: Fast-Path + Systems Architecture + Multi-Method Classes + Diagnostic Self-Healing)\n", "- **Quantization**: `q4_k_m` GGUF via `llama.cpp`\n", "- **Benchmark Suites**: Harvard CS50-10, CEB-15, HumanEval-164, and ClassEval\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Cell 1: Environment Preflight & Dependencies\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 settings.'\n", "print(f'[+] GPU Detected: {torch.cuda.get_device_name(0)}')\n", "\n", "# System dependencies\n", "subprocess.run('apt-get update -qq && apt-get install -y -qq zstd pciutils git curl make build-essential', shell=True, check=True)\n", "subprocess.run([sys.executable, '-m', 'pip', 'install', '-q', '--upgrade', 'transformers', 'peft', 'trl', 'accelerate', 'datasets', 'huggingface_hub'], 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", "# Pull benchmark suite & dataset\n", "os.chdir('/kaggle/working')\n", "subprocess.run('wget -q -O codeoptics_benchmark_suite.tar.gz https://huggingface.co/mindsofchaos/codeoptics-coder-3b-v3-1/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!')\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Cell 2: Execute LoRA Fine-Tuning v4.0\n", "import subprocess, sys, time\n", "\n", "print('=' * 75)\n", "print('[2/5] Commencing CodeOptics-Coder v4.0 SFT Training')\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": [ "# Cell 3: Weight Merge & GGUF Q4_K_M Quantization\n", "import subprocess, sys, time\n", "\n", "print('=' * 75)\n", "print('[3/5] Merging Weights and Quantizing to GGUF 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": [ "# Cell 4: Ollama GPU Setup & Model Registration\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!')\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Cell 5: Execute the Full Master Benchmark Suite\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": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 4 }