Views
No views yet
microsoft/phi-2, adapted for both Python code generation and step-by-step mathematical reasoning. The goal of this project was to distill the capabilities of larger "teacher" models (Qwen2.5-Coder-7B-Instruct for coding and Qwen2.5-Math-7B-Instruct for math) into the compact and efficient Phi-2 architecture.microsoft/phi-2transformers library.1from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
2
3model_id = "DeryFerd/Qwen-Math-Code-Distill-Phi-2"
4
5# Load the tokenizer and model
6tokenizer = AutoTokenizer.from_pretrained(model_id)
7model = AutoModelForCausalLM.from_pretrained(
8 model_id,
9 torch_dtype="auto",
10 device_map="auto",
11 trust_remote_code=True
12)
13
14# Create a text-generation pipeline
15pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
16
17# --- Example 1: Coding ---
18code_instruction = "Write a Python function that takes a list of strings and returns a new list with all strings converted to uppercase."
19prompt = f"Instruct: {code_instruction.strip()}\nOutput:"
20
21outputs = pipe(
22 prompt,
23 max_new_tokens=256,
24 do_sample=False,
25 pad_token_id=tokenizer.eos_token_id
26)
27response = outputs[0]['generated_text'].split("Output:")[1].strip()
28print("--- Coding Example ---")
29print(response)
30
31# --- Example 2: Math ---
32math_instruction = "A bakery has 150 cookies. They sell 60 in the morning and 35 in the afternoon. How many cookies are left at the end of the day?"
33prompt = f"Instruct: {math_instruction.strip()}\nOutput:"
34
35outputs = pipe(
36 prompt,
37 max_new_tokens=512,
38 do_sample=False,
39 pad_token_id=tokenizer.eos_token_id
40)
41response = outputs[0]['generated_text'].split("Output:")[1].strip()
42print("\n--- Math Example ---")
43print(response)
44
45## Training Details
46
47### Training Data
48
49The model was fine-tuned on a combined dataset of **3,474 instruction-response pairs**:
50- **2,500 math problems:** A mix of 2,000 samples from the GSM8K dataset and 500 samples from the MATH dataset. Generated using `Qwen2.5-Math-7B-Instruct`.
51- **974 coding problems:** A curated subset of the MBPP dataset. Generated using `Qwen2.5-Coder-7B-Instruct`.
52
53### Training Procedure
54
55The model was fine-tuned using the LoRA (Low-Rank Adaptation) method for parameter-efficient fine-tuning (PEFT).
56
57#### Training Hyperparameters
58
59- **Framework:** `trl.SFTTrainer`
60- **LoRA `r`:** 16
61- **LoRA `alpha`:** 32
62- **Target Modules:** `q_proj`, `k_proj`, `v_proj`, `dense`
63- **Learning Rate:** 2e-4
64- **LR Scheduler:** Constant
65- **Epochs:** 3
66- **Batch Size:** 1 (with gradient accumulation of 8)
67- **Optimizer:** Paged AdamW 8-bit
68
69### Compute Infrastructure
70
71- **Hardware Type:** Single NVIDIA T4 GPU
72- **Cloud Provider:** Kaggle Notebooks
73
74## Citation
75
76If you use this model, please consider citing the original Phi-2, MBPP, GSM8K, and MATH papers.