This repository provides a Low-Rank Adaptation (LoRA) adapter for Microsoft's Phi-2 (2.7B parameter language model), fine-tuned using 4-bit QLoRA on Python instruction-tuning data.
microsoft/phi-2 (2.78B parameters, frozen)adapter_model.safetensors, 5.24M parameters / 0.19% of base model)[!NOTE] This adapter is an experimental research artifact investigating parameter-efficient adaptation, EOS supervision, and response-only loss masking on consumer hardware. It is not a production-grade coding assistant. Always review and sandbox generated code before execution.
The adapter was evaluated on a leak-free, out-of-distribution 50-task algorithmic benchmark spanning eight programming categories:
| Model / Configuration | Training Scope | Target Modules | Resp Loss | Core Pass% (Greedy) | EOS Term% | Max-Token Cutoff% | Mean Length (tok) |
|---|---|---|---|---|---|---|---|
| Raw Phi-2 Base | Pretrained | None | 0.5792 | 52.0% (26/50) | 18.0% | 84.0% | 284.6 |
| Config E Step 100 (Selected Adapter) | 5,000 samples | q_proj, v_proj |
0.4803 | 68.0% (34/50) | 96.0% | 4.0% | 115.4 |
microsoft/phi-2 (2.78B parameters)q_proj, v_projPhase2CompletionCollator) focusing gradients solely on completion tokensadamw_torch), learning rate 2e-4, cosine schedule, warmup ratio 0.03pip install torch transformers peft bitsandbytes accelerate
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from peft import PeftModel
# Configure 4-bit NF4 quantization
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16,
)
# Load frozen base model
base_model = AutoModelForCausalLM.from_pretrained(
"microsoft/phi-2",
quantization_config=bnb_config,
device_map="auto",
trust_remote_code=True,
)
# Attach LoRA adapter
model = PeftModel.from_pretrained(base_model, "alfeenafsal/phi2-qlora-finetune")
model.eval()
tokenizer = AutoTokenizer.from_pretrained("alfeenafsal/phi2-qlora-finetune", trust_remote_code=True)
tokenizer.pad_token = tokenizer.eos_token
# Format prompt
prompt = "### Instruction:\nWrite a Python function that checks if two strings are anagrams of each other (case-insensitive).\n\n### Response:\n"
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=200,
do_sample=False, # Deterministic greedy decoding
eos_token_id=tokenizer.eos_token_id,
pad_token_id=tokenizer.pad_token_id,
)
completion = tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
print(completion)
Because LoRA modifies intermediate projections linearly without copying weights, you can easily compare the fine-tuned adapter against the raw base model on the same in-memory instance:
# Raw base model generation
with model.disable_adapter():
with torch.no_grad():
base_out = model.generate(**inputs, max_new_tokens=200, do_sample=False)
print("Base:", tokenizer.decode(base_out[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True))
# Fine-tuned adapted generation
with torch.no_grad():
adapted_out = model.generate(**inputs, max_new_tokens=200, do_sample=False)
print("Adapted:", tokenizer.decode(adapted_out[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True))
iamtarun/python_code_instructions_18k_alpaca (4,500 train / 500 validation, seed 42).### Instruction:
{instruction}
### Input:
{input}
### Response:
{output}<|endoftext|>
reports/phase3_benchmark_ood_verification.json).microsoft/phi-2