fchis commited on
Commit
d7eab72
·
verified ·
1 Parent(s): 9991a99

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +215 -12
README.md CHANGED
@@ -1,16 +1,219 @@
1
  ---
2
- base_model: mlx-community/Qwen2.5-Coder-7B-Instruct-4bit
3
- language:
4
- - en
5
- library_name: mlx
6
  license: apache-2.0
7
- license_link: https://huggingface.co/Qwen/Qwen2.5-Coder-7B-Instruct/blob/main/LICENSE
8
- pipeline_tag: text-generation
9
  tags:
10
- - code
11
- - codeqwen
12
- - chat
13
- - qwen
14
- - qwen-coder
15
- - mlx
 
 
 
 
 
 
 
 
 
16
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
 
 
 
 
2
  license: apache-2.0
3
+ base_model: Qwen/Qwen2.5-Coder-7B-Instruct
 
4
  tags:
5
+ - laravel
6
+ - php
7
+ - code-generation
8
+ - mlx
9
+ - lora
10
+ - fine-tuned
11
+ datasets:
12
+ - fchis/Laravel-13x-Code-Instructions
13
+ language:
14
+ - en
15
+ pipeline_tag: text-generation
16
+ library_name: mlx
17
+ model-index:
18
+ - name: Laravel-13x-Qwen2.5-Coder-7B-Instruct-LoRA
19
+ results: []
20
  ---
21
+
22
+ # Laravel 13.x Qwen2.5-Coder-7B-Instruct LoRA
23
+
24
+ A 7B code-specialized model fine-tuned on Laravel 13.x instruction-to-code pairs. **Trained entirely on an Apple M2 Pro with 16GB RAM** using Apple's MLX framework — no cloud GPU needed.
25
+
26
+ Part of a two-model pipeline for AI-assisted Laravel code generation. See [github.com/florinel-chis/laravel-ai-gen](https://github.com/florinel-chis/laravel-ai-gen) for the complete tool.
27
+
28
+ ## Proven: Fine-Tuning Works
29
+
30
+ A/B comparison of base vs fine-tuned model on the same prompts:
31
+
32
+ | Behavior | Base Qwen2.5-Coder-7B | This model (fine-tuned) |
33
+ |----------|----------------------|------------------------|
34
+ | Output format | "Certainly! Below is a clean..." prose + markdown | **Pure PHP code, no prose** |
35
+ | Token efficiency | ~500 tokens for incomplete output with explanation | **Complete files in 100-200 tokens** |
36
+ | Migration style | Old named class `class CreateContactsTable` | **Modern `return new class extends Migration`** |
37
+ | Validation syntax | N/A (never reached code in 500 tokens) | **Array syntax `['required', 'max:255']`** |
38
+ | Controller pattern | N/A (spent all tokens on "Step 1: Create the Model...") | **FormRequest injection + `response()->json()`** |
39
+
40
+ The base model literally never produces usable code at 500 max_tokens because it spends all tokens on explanation. The fine-tuned model produces complete, runnable PHP files.
41
+
42
+ ## Test Results: 4/4 Perfect on Specific Instructions
43
+
44
+ | Instruction | Output | PHP Valid |
45
+ |------------|--------|-----------|
46
+ | "Create model for Contact with fillable: name, email, subject, message" | Exact 4 fields, no extras, no relationships | **Yes** |
47
+ | "Write migration for contacts with name, email, subject, message" | Full `return new class extends Migration` wrapper | **Yes** |
48
+ | "Create Form Request with validation rules" | Modern array syntax, correct 4 rules | **Yes** |
49
+ | "Write ContactController with store using StoreContactRequest, return 201" | FormRequest injection, `Contact::create`, 201 JSON | **Yes** |
50
+
51
+ ### Sample output: Controller
52
+
53
+ **Prompt:** "Write a Laravel ContactController with a store method that uses StoreContactRequest and creates a Contact. Return 201 JSON."
54
+
55
+ ```php
56
+ <?php
57
+
58
+ namespace App\Http\Controllers;
59
+
60
+ use App\Http\Requests\StoreContactRequest;
61
+ use App\Models\Contact;
62
+
63
+ class ContactController extends Controller
64
+ {
65
+ public function store(StoreContactRequest $request)
66
+ {
67
+ $contact = Contact::create($request->validated());
68
+
69
+ return response()->json($contact, 201);
70
+ }
71
+ }
72
+ ```
73
+
74
+ ### Sample output: Model
75
+
76
+ **Prompt:** "Create a Laravel Eloquent model for Contact with fillable fields: name, email, subject, message."
77
+
78
+ ```php
79
+ <?php
80
+
81
+ namespace App\Models;
82
+
83
+ use Illuminate\Database\Eloquent\Model;
84
+
85
+ class Contact extends Model
86
+ {
87
+ protected $fillable = [
88
+ 'name',
89
+ 'email',
90
+ 'subject',
91
+ 'message',
92
+ ];
93
+ }
94
+ ```
95
+
96
+ ## 7B vs 3B Comparison
97
+
98
+ Both trained on the same 139 examples:
99
+
100
+ | Aspect | 3B (LoRA) | **7B (LoRA)** |
101
+ |--------|-----------|---------------|
102
+ | Form Request | Pipe syntax `'required\|max:255'` | **Array syntax `['required', 'max:255']`** |
103
+ | Controller | Minor `\response` return type issue | **Clean, no issues** |
104
+ | Training peak | 5.8 GB | **7.6 GB** |
105
+ | Inference speed | ~1-2s per file | ~12-14s per file |
106
+ | Quality | Good | **Better** |
107
+
108
+ The 7B is recommended when quality matters. The 3B is 6x faster for iteration.
109
+
110
+ ## Real Laravel Test: Contact Form
111
+
112
+ Tested in a real Laravel 13.x project with `php artisan serve`:
113
+
114
+ | Endpoint | Status | Result |
115
+ |----------|--------|--------|
116
+ | POST /api/contact (valid data) | **201 Created** | Contact saved to database |
117
+ | POST /api/contact (missing fields) | **422 Validation** | Correct error messages |
118
+ | POST /api/contact (invalid email) | **422 Validation** | "must be a valid email address" |
119
+
120
+ Migration + Form Request files were usable as-is. Model and Controller needed fixes when instructions came from the planner (vague), but were perfect when given specific instructions directly.
121
+
122
+ ## How to Use
123
+
124
+ ### Standalone (specific instructions)
125
+
126
+ ```python
127
+ from mlx_lm import load, generate
128
+
129
+ model, tok = load("fchis/Laravel-13x-Qwen2.5-Coder-7B-Instruct-LoRA")
130
+
131
+ messages = [
132
+ {"role": "system", "content": "You are a senior Laravel developer. Write clean, production-ready Laravel code."},
133
+ {"role": "user", "content": "Create a Laravel Eloquent model for Contact with fillable fields: name, email, subject, message."}
134
+ ]
135
+ text = tok.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
136
+ response = generate(model, tok, prompt=text, max_tokens=500)
137
+ print(response)
138
+ ```
139
+
140
+ ### Two-model pipeline (feature descriptions)
141
+
142
+ ```bash
143
+ pip install mlx-lm
144
+ git clone https://github.com/florinel-chis/laravel-ai-gen
145
+ cd laravel-ai-gen
146
+
147
+ # Use 7B coder for best quality
148
+ python3 laravel-gen.py --model 7b "simple contact form - name, email, subject, message"
149
+ ```
150
+
151
+ ## Model Details
152
+
153
+ | Detail | Value |
154
+ |--------|-------|
155
+ | **Base model** | [Qwen2.5-Coder-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-Coder-7B-Instruct) (4-bit via MLX) |
156
+ | **Fine-tuning** | LoRA, 16 layers, batch=1, seq=768, mask-prompt |
157
+ | **Training data** | 139 instruction-to-code pairs from Laravel 13.x docs |
158
+ | **Training hardware** | Apple M2 Pro, 16GB RAM |
159
+ | **Training time** | ~5 minutes |
160
+ | **Peak memory** | 7.6 GB |
161
+ | **Best val loss** | 0.503 (iter 50) |
162
+ | **Trainable params** | 11.534M / 7,615.617M (0.151%) |
163
+
164
+ ## Training Data
165
+
166
+ Dataset: [fchis/Laravel-13x-Code-Instructions](https://huggingface.co/datasets/fchis/Laravel-13x-Code-Instructions) (139 examples)
167
+
168
+ Covers: Eloquent models (with and without relationships), migrations (full class wrappers), Form Requests, controllers (short, with FormRequest injection), API Resources, routes, middleware, Blade templates, View Composers.
169
+
170
+ Key training data decisions:
171
+ - All migration examples have full `return new class extends Migration` wrapper (no bare Schema calls)
172
+ - Simple model examples WITHOUT relationships (prevents hallucinating unwanted belongsTo)
173
+ - Controller examples use FormRequest injection (not inline Validator)
174
+ - Attribute-based controller examples removed (toxic at small scale — caused hallucinated imports)
175
+ - 8 instruction-grounded model examples (exact field matching)
176
+
177
+ ## Limitations
178
+
179
+ - **Works best with specific instructions** — list exact fields, relationships, methods
180
+ - **Vague instructions cause hallucination** — "fillable fields" without listing them triggers pretraining archetypes
181
+ - **Repetition loops on long outputs** — models/controllers >300 tokens may enter loops. Cap at 500-600 tokens.
182
+ - **139 training examples** — more data (500+) would improve instruction following
183
+ - **MLX format** — requires Apple Silicon Mac for inference
184
+ - **7B is slower** — ~12-14s per file vs ~1-2s for the 3B variant
185
+
186
+ ### The Repetition Problem
187
+
188
+ Small models (3B-7B) can enter repetition loops on longer outputs. Root causes:
189
+ 1. Vague instructions trigger pretraining archetypes (the model "remembers" generic patterns)
190
+ 2. Autoregressive generation has no self-correction — once in a loop, it can't escape
191
+
192
+ Mitigations: specific instructions, max token caps, post-processing repetition detection.
193
+
194
+ ## Also Tested: DoRA
195
+
196
+ DoRA (LoRA + magnitude decomposition) was tested but OOM'd at 16 layers on 7B (>10.5 GB). At 8 layers it ran but produced worse output than 16-layer LoRA. On 16GB Mac, **LoRA with more layers beats DoRA with fewer layers**.
197
+
198
+ ## Companion Models
199
+
200
+ | Model | Params | Role | Link |
201
+ |-------|--------|------|------|
202
+ | Planner | 1.7B | Decompose features into tasks | [fchis/Laravel-13x-Planner-Qwen3-1.7B-LoRA](https://huggingface.co/fchis/Laravel-13x-Planner-Qwen3-1.7B-LoRA) |
203
+ | Coder (3B) | 3B | Generate code (faster, lighter) | [fchis/Laravel-13x-Qwen2.5-Coder-3B-Instruct-LoRA](https://huggingface.co/fchis/Laravel-13x-Qwen2.5-Coder-3B-Instruct-LoRA) |
204
+ | **Coder (7B)** | **7B** | **Generate code (best quality)** | **You're here** |
205
+ | CLI tool | — | End-to-end pipeline | [github.com/florinel-chis/laravel-ai-gen](https://github.com/florinel-chis/laravel-ai-gen) |
206
+
207
+ ## Research Context
208
+
209
+ Built as part of a 21-step research investigation into local AI-assisted code generation on consumer hardware. Key discoveries:
210
+
211
+ - 90 instruction-to-code pairs beat 14,000 doc-Q&A pairs (training format > quantity)
212
+ - Fine-tuned 7B produces perfect code when given specific instructions
213
+ - The planner→coder two-model pipeline runs in ~4 GB total on any 8GB+ Mac
214
+ - GRPO is ineffective for this use case (failures are semantic, not syntactic)
215
+ - DoRA is better than LoRA per-layer but needs more memory — LoRA wins when memory-constrained
216
+
217
+ ## License
218
+
219
+ Apache 2.0 (same as base model)