# Qwen4-35B-A3B-Pi-Agent — Fine-Tuned Coding Agent LoRA & Dynamic GGUFs (Apache 2.0)

> Open-weight fine-tuned coding agent built on `Qwen/Qwen4-35B-A3B` (35B total + 20B Engram, 3B active) by James Arslan (Stockholm, Sweden). Engineered for the `pi 1.5.2` coding agent harness across Python, Rust, TypeScript, Java 17, AWS/Terraform, and Git, and verified across **534 benchmarks** (20 isolated SWE repos + 514 public coding, math, and tool-calling problems) without catastrophic forgetting.

- **Web page & charts:** https://jamesarslan.com/qwen4-pi-agent
- **Hugging Face LoRA + PEFT (Apache 2.0):** https://huggingface.co/jamesarslan/Qwen4-35B-A3B-Pi-Agent-LoRA
- **Hugging Face Standalone Dynamic GGUFs (Apache 2.0):** https://huggingface.co/jamesarslan/Qwen4-35B-A3B-Pi-Agent-GGUF
- **Author:** James Arslan (`jamesarslan` on Hugging Face & GitHub) · https://jamesarslan.com/

## Two Curated Adapter Variants (44.4 MB Fused F16 GGUF LoRA + PEFT)
1. **Flagship (`v2-frontier-final120`, default):**
   - `20/20 (100.0%)` Pi 1.5.2 SWE (`+5.0%` vs Base `19/20` — only GGUF matching Blackwell `SGLang NVFP4 + DFlash2`)
   - `18/20 (90.0%, +15.0%)` BFCL-Hard Multi-Turn Tool Calling · `45/50 (90.0%, +8.0%)` Combined Agentic Tools
   - `151/164 (92.1%, +0.0%)` HumanEval · `92.0%` MBPP · `98.0%` GSM8K · `83.0%` MATH-500
   - `489/534 (91.57%)` Grand Total — strictly beats Base `Qwen4-35B-A3B` (`487/534 = 91.20%`)
2. **KL-DPOP Efficiency (`v6-kl-dpo-ckpt15`):**
   - **All-time high `28/30 (93.3%, +6.7%)`** BFCL-Pi15 (`187` avg tokens) · `45/50 (90.0%)` Combined Tools
   - **Zero math forgetting:** `99.0%` GSM8K + `84.0%` MATH-500 (`+6.0%` recovery vs reference-free CPO `78.0%`)
   - `18/20 (90.0%)` Pi 1.5.2 SWE (`8/8` Deep tasks, `9.67` Core+Hard avg tools, `5×` fewer tool errors)

## Six Standalone LoRA-Preserving Dynamic GGUFs (Zero Runtime LoRA + Vision)
Every merged GGUF locks 100% of the ~2.2B LoRA-modified dense attention, linear-attention/SSM, shared-expert, and hyper-connection tensors at lossless `Q8_0` (using Unsloth's 93-chunk `imatrix_unsloth.gguf_file`), compresses the 20B Engram table to `Q4_0` (`11.52 GB` in host RAM via `-ot per_layer_token_embd=CPU`), includes all 247,587 Qwen4 BPE merges + `eos_token_id=248046` + official Qwen4 Jinja `chat_template`, and ships alongside `mmproj-BF16.gguf` (`0.90 GB`) for vision:

- **`UD-IQ3_XXS`** (`27.15 GB`, ~`15.6 GB` VRAM): **`85.0%` MATH-500 (`+5.0%` vs Base)**, `98.0%` GSM8K, `85.0%` BFCL-Hard
- **`UD-IQ4_XS`** (`31.93 GB`, ~`20.4 GB` VRAM): **`88.0%` All Tools (`+6.0%` vs Base, `90.0%` BFCL-Pi15)**, `91.5%` HumanEval (`+1.3%` vs runtime LoRA)
- **`UD-Q4_K_XL`** (`35.71 GB`, ~`24.2 GB` VRAM): **`92.7%` HumanEval (all-time peak, `+0.6%` vs Base)**, `99.0%` GSM8K, `92.0%` MBPP
- **`UD-Q5_K_XL`** (`38.39 GB`, ~`26.9 GB` VRAM) · **`Q6_K`** (`41.25 GB`, ~`29.7 GB` VRAM) · **`Q8_0`** (`59.99 GB`, 2 shards, lossless `8.51 BPW`)

## Key Engineering Findings
1. **Qwen4 `in_proj_qkvz` GGUF LoRA Fusion:** Standard `convert_lora_to_gguf.py` emits split `in_proj_qkv`/`in_proj_z` matrices that fail to attach to GGUF's fused `attn_qkv_gate`; we block-diagonally fused them so our `44.4 MB` F16 GGUF LoRAs attach cleanly on `llama.cpp b12977/b12992+`.
2. **Curing RL Likelihood Displacement via KL-Anchored DPOP:** Reference-free SimPO/CPO (`v4`) on 160 `safeagents` anti-tamper Pi rollouts cut tool errors `2×` but dropped MATH-500 `84% → 78%`. Pre-computing exact frozen `π_ref` log-probs + DPOP penalty (`max(0, log π_ref(y_w) - log π_θ(y_w))`) + 45% clean official train-split replay (`v6`) recovered MATH-500 back to `84.0%`, GSM8K to `99.0%`, and pushed BFCL-Pi15 to `93.3%`.
3. **LoRA-Preserving Dynamic Quantization for MoE Models:** Plain `Q4_K_M` crushed LoRA-modified dense attention/shared-expert tensors to 4-bit (`-10%` BFCL-Pi15). Keeping those dense tensors at `Q8_0` (`+1 GB` only) preserved adapter gains across 18 GB, 24 GB, and 32 GB GPUs.
4. **Verified Local RTX 5090 Runtimes:** `llama.cpp b12992` (CUDA 13.5, MTP 2), `Strata v0.2.26` (`--loop on --parallel 1`, `172.7 tok/s`, `12/12` Hard+Deep), and `SGLang` (`NVFP4 + DFlash2`, `395–452 tok/s`, `20/20` Pi SWE).

---
More: [Summary](https://jamesarslan.com/llms.txt) · [Full Profile](https://jamesarslan.com/llms-full.txt) · [Agent Workbench](https://jamesarslan.com/agent-workbench.md) · [Projects](https://jamesarslan.com/projects.md)
