SathishKumar89/my-python-coder
151
1 2---3base_model: Qwen/Qwen2.5-Coder-1.5B-Instruct4library_name: peft5license: apache-2.06language:7- en8tags:9- code10- python11- lora12- peft13- qwen214- code-generation15datasets:16- iamtarun/python_code_instructions_18k_alpaca17---18 19# my-python-coder20 21A LoRA fine-tune of [Qwen2.5-Coder-1.5B-Instruct](https://huggingface.co/Qwen/Qwen2.5-Coder-1.5B-Instruct) specialized for Python code generation.22 23This model was fine-tuned as a learning project to demonstrate the full workflow of taking a base model, training it on a custom dataset, and publishing it to the Hugging Face Hub.24 25## Training Details26 27| Parameter | Value |28|---|---|29| **Base model** | `Qwen/Qwen2.5-Coder-1.5B-Instruct` |30| **Dataset** | `iamtarun/python_code_instructions_18k_alpaca` (first 1,500 examples) |31| **Method** | LoRA (r=16, alpha=32, target_modules=`all-linear`) |32| **Training steps** | 200 |33| **Learning rate** | 2e-4 |34| **Effective batch size** | 8 (batch=2 × grad_accum=4) |35| **Max sequence length** | 1024 |36| **Hardware** | Google Colab (NVIDIA T4, 16 GB VRAM) |37| **Training time** | ~33 minutes |38 39## What Is This — A Model or an Adapter?40 41This repository contains a **LoRA adapter**, not a standalone model. Understanding the difference matters for how you load and use it.42 43### The Two Artifacts44 45| | **Base Model** | **LoRA Adapter (this repo)** |46|---|---|---|47| **What it is** | The full pretrained neural network | A small set of trained weights that modify the base |48| **Size** | ~3 GB | ~74 MB |49| **Who made it** | The Qwen team | Me (SathishKumar89) |50| **Repo** | `Qwen/Qwen2.5-Coder-1.5B-Instruct` | `SathishKumar89/my-python-coder` |51| **Contains** | All model weights, tokenizer, config | Only adapter weights + config + tokenizer copy |52| **Loadable alone?** | ✅ Yes | ❌ No — needs the base model |53 54### Why This Design?55 56Instead of retraining all ~1.5 billion parameters of the base model, **LoRA (Low-Rank Adaptation)** freezes the base model and only trains a tiny number of new parameters. This gives several advantages:57 58- **Tiny file size** — 74 MB vs. ~3 GB (a ~40× reduction)59- **Fast training** — minutes to hours instead of days60- **Runs on modest hardware** — a free Google Colab T4 GPU is enough61- **Easy to swap** — you can keep the same base model and load different adapters for different tasks62 63### How to Load It Correctly64 65Because this repo is an adapter, you must load **two** things — the base model first, then the adapter on top:66 67```python68from transformers import AutoModelForCausalLM, AutoTokenizer69from peft import PeftModel70import torch71 72# Step 1: Load the base model73base = AutoModelForCausalLM.from_pretrained(74 "Qwen/Qwen2.5-Coder-1.5B-Instruct",75 dtype=torch.float16,76 device_map="auto",77)78 79# Step 2: Attach the LoRA adapter80model = PeftModel.from_pretrained(base, "SathishKumar89/my-python-coder")81 82# Step 3: Load the tokenizer (included in this repo)83tokenizer = AutoTokenizer.from_pretrained("SathishKumar89/my-python-coder")84 85## Prompt Format86 87This model was trained with the following instruction format. Using the same format at inference time will give the best results:88 89```90### Instruction:91<your task description>92 93### Response:94<model's answer>95```96 97## Usage98 99```python100import torch101from transformers import AutoTokenizer, AutoModelForCausalLM102from peft import PeftModel103 104# Load base model and LoRA adapter105base_model = AutoModelForCausalLM.from_pretrained(106 "Qwen/Qwen2.5-Coder-1.5B-Instruct",107 dtype=torch.float16,108 device_map="auto",109)110model = PeftModel.from_pretrained(base_model, "SathishKumar89/my-python-coder")111tokenizer = AutoTokenizer.from_pretrained("SathishKumar89/my-python-coder")112 113# Prepare a prompt114prompt = """### Instruction:115Write a Python function that checks if a number is prime.116 117### Response:118"""119 120inputs = tokenizer(prompt, return_tensors="pt").to(model.device)121outputs = model.generate(**inputs, max_new_tokens=200, do_sample=False)122print(tokenizer.decode(outputs[0], skip_special_tokens=True))123```124 125## Example Output126 127**Prompt:**128```129### Instruction:130Write a Python function that checks if a number is prime.131 132### Response:133```134 135**Model output:**136```python137def is_prime(num):138 # Check for 0 and 1139 if num <= 1:140 return False141 142 # Check for even numbers greater than 2143 elif num == 2:144 return True145 elif num % 2 == 0:146 return False147 148 # Check for odd numbers greater than 3149 else:150 for i in range(3, int(num**0.5) + 1, 2):151 if num % i == 0:152 return False153 return True154```155 156## Limitations157 158- Trained on a **small subset** (1,500 of 18,612 examples) for only 200 steps — this is a proof-of-concept, not a production model.159- May not generalize well to complex Python tasks (large refactors, multi-file projects, advanced libraries).160- Inherits any biases or limitations present in the base model and training dataset.161- Not evaluated against standard benchmarks.162 163## Future Improvements164 165- Train on the full dataset for multiple epochs166- Increase LoRA rank for greater capacity167- Evaluate on HumanEval or MBPP benchmarks168 169## Acknowledgements170 171- Base model: [Qwen2.5-Coder-1.5B-Instruct](https://huggingface.co/Qwen/Qwen2.5-Coder-1.5B-Instruct) by the Qwen team172- Dataset: [iamtarun/python_code_instructions_18k_alpaca](https://huggingface.co/datasets/iamtarun/python_code_instructions_18k_alpaca)173- Training framework: Hugging Face `transformers`, `peft`, `trl`174```175 