ranjanrajib commited on
Commit
ccec681
·
verified ·
1 Parent(s): 98d5f75

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +359 -229
README.md CHANGED
@@ -23,419 +23,482 @@ tags:
23
  - parameter-efficient-fine-tuning
24
  ---
25
 
26
- # Qwen2.5-Coder-7B-Instruct — DPO Fine-Tuned for Python
27
 
28
- ## Model at a Glance
29
 
30
- This model is a **Python-focused, DPO fine-tuned version of Qwen2.5-Coder-7B-Instruct**, designed to improve the model's ability to generate **functionally correct Python code**.
31
 
32
- The model was trained using **Direct Preference Optimization (DPO)** with automatically constructed preference pairs. Candidate Python solutions were generated for programming problems and evaluated by executing them against unit tests. Solutions that passed the required tests were used as preferred examples, while solutions that failed tests were used as rejected examples.
33
 
34
  The resulting model was evaluated against the original Qwen2.5-Coder-7B-Instruct model on held-out programming problems.
35
 
36
- ### What changed?
37
 
38
  The base model:
39
 
40
- **Qwen2.5-Coder-7B-Instruct**
41
 
42
  was fine-tuned using:
43
 
44
- **DPO + LoRA + execution-based preference data**
45
 
46
  The resulting model demonstrates improved coding performance on the evaluated held-out benchmark:
47
 
48
- | Metric | Qwen2.5-Coder-7B-Instruct | DPO Model | Improvement |
49
- | ----------------------- | ------------------------: | ---------: | -----------: |
50
- | Pass@1 | 41.87% | **45.22%** | **+3.35 pp** |
51
- | Pass@5 | 48.87% | **51.10%** | **+2.23 pp** |
52
- | Pass@10 | 50.99% | **53.16%** | **+2.17 pp** |
53
- | Tests passed per answer | 66.20% | **69.41%** | **+3.21 pp** |
54
 
55
- The primary result is the improvement in **Pass@1**, meaning that a single generated solution was more likely to produce a correct solution on the evaluated programming problems.
 
 
 
 
 
 
 
 
 
 
56
 
57
  The model also showed improved Pass@5, Pass@10, and partial test coverage.
58
 
59
- ---
 
 
60
 
61
- **Base model:** `Qwen/Qwen2.5-Coder-7B-Instruct` at snapshot
62
- `c03e6d358207e414f1eca0bb1891e29f1db0e242`. The merge was performed against exactly these weights.
63
 
64
- ## Downloading and using
65
 
66
- ### The merged model
 
 
67
 
68
- ```python
69
  from transformers import AutoModelForCausalLM, AutoTokenizer
70
 
71
  model = AutoModelForCausalLM.from_pretrained(
 
72
  "ranjanrajib/DPO-trained-Qwen2.5-Coder-7B", dtype="bfloat16", device_map="auto")
 
73
  tok = AutoTokenizer.from_pretrained("ranjanrajib/DPO-trained-Qwen2.5-Coder-7B")
74
 
75
  msgs = [{"role": "user", "content": "Write a Python function that merges overlapping intervals."}]
 
76
  text = tok.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True)
 
77
  out = model.generate(**tok(text, return_tensors="pt").to(model.device), max_new_tokens=512)
 
78
  print(tok.decode(out[0], skip_special_tokens=True))
79
- ```
 
80
 
81
  Or fetch the files without loading them (~14 GB, four shards):
82
 
83
- ```bash
 
84
  hf download ranjanrajib/DPO-trained-Qwen2.5-Coder-7B --exclude "adapter/*" --local-dir ./d4
85
- ```
86
 
87
- ### The LoRA adapter only (308 MB)
 
 
 
 
88
 
89
- The adapter lives in `adapter/`. Applying it to the base model gives the same result as the
90
  merged weights, at a fraction of the download.
91
 
92
- ```bash
 
93
  hf download ranjanrajib/DPO-trained-Qwen2.5-Coder-7B --include "adapter/*" --local-dir ./d4-lora
94
- ```
95
 
96
- ```python
 
 
 
97
  import torch
 
98
  from peft import PeftModel
 
99
  from transformers import AutoModelForCausalLM
100
 
101
  BASE = "Qwen/Qwen2.5-Coder-7B-Instruct"
 
102
  REVISION = "c03e6d358207e414f1eca0bb1891e29f1db0e242" # the snapshot this was trained against
103
 
104
  base = AutoModelForCausalLM.from_pretrained(
 
105
  BASE, revision=REVISION, dtype=torch.bfloat16, device_map="auto")
 
106
  model = PeftModel.from_pretrained(
 
107
  base, "ranjanrajib/DPO-trained-Qwen2.5-Coder-7B", subfolder="adapter")
108
- ```
109
- ---
110
 
111
- # Why Use This Model?
 
 
 
 
112
 
113
- If your primary use case is **Python code generation**, this model is intended to be a stronger alternative to the original Qwen2.5-Coder-7B-Instruct model based on the evaluation performed in this project.
114
 
115
  It is particularly relevant for users who want to:
116
 
117
  * generate Python functions and implementations
 
118
  * solve programming problems
 
119
  * generate code that needs to satisfy functional requirements
 
120
  * improve first-attempt coding correctness
 
121
  * experiment with DPO-trained code-generation models
 
122
  * study execution-based preference optimization
 
123
  * build research or evaluation pipelines for Python code generation
124
 
125
  The main motivation for the fine-tuning was not simply to make the model generate more code.
126
 
127
- It was to encourage the model toward **solutions that work**.
128
 
129
- The preference data therefore used actual program execution and unit-test results rather than subjective judgments about whether generated code *looked* correct.
130
 
131
- ---
132
 
133
- # How Is It Different From Qwen2.5-Coder-7B-Instruct?
134
 
135
- The underlying model architecture and capabilities come from **Qwen2.5-Coder-7B-Instruct**.
136
 
137
  This model adds a preference-optimization stage on top of that model.
138
 
139
  Conceptually:
140
 
141
- ```text
 
142
  Qwen2.5-Coder-7B-Instruct
 
143
  │
 
144
  ▼
 
145
  Generate multiple Python
 
146
  candidate solutions
 
147
  │
 
148
  ▼
 
149
  Execute candidates
 
150
  against tests
 
151
  │
 
152
  ┌─────┴─────┐
 
153
  ▼ ▼
 
154
  Pass Fail
 
155
  │ │
 
156
  ▼ ▼
 
157
  Chosen Rejected
 
158
  │ │
 
159
  └─────┬─────┘
 
160
  ▼
 
161
  DPO
 
162
  │
 
163
  ▼
 
164
  LoRA Adapter
 
165
  │
 
166
  ▼
 
167
  Python-focused model
168
- ```
 
169
 
170
  The goal is to shift the model toward coding behaviors associated with solutions that successfully execute against the available tests.
171
 
172
- ### Important distinction
173
 
174
- This is **not a new foundation model**.
175
 
176
  It is a parameter-efficient fine-tuned adapter for Qwen2.5-Coder-7B-Instruct.
177
 
178
  The base model remains unchanged.
179
 
180
- ---
181
 
182
- # What Kind of Model Is This?
183
 
184
- ### Primary task
185
 
186
- **Python code generation**
187
 
188
- ### Model family
189
 
190
  Qwen2.5-Coder
191
 
192
- ### Base model
193
 
194
- `Qwen/Qwen2.5-Coder-7B-Instruct`
195
 
196
- ### Fine-tuning
197
 
198
  Direct Preference Optimization (DPO)
199
 
200
- ### Parameter-efficient method
201
 
202
  LoRA
203
 
204
- ### Precision
205
 
206
  bfloat16
207
 
208
- ### Quantization
209
 
210
  None
211
 
212
- ### Model size
213
 
214
  7.7B-parameter base model
215
 
216
- ### Adapter
217
 
218
  Approximately 80.7M trainable parameters
219
 
220
- ---
221
 
222
- # Performance
223
 
224
  The model was evaluated against the original Qwen2.5-Coder-7B-Instruct using a controlled benchmark consisting of 508 held-out BigCodeBench programming problems.
225
 
226
  The same prompts, generation settings, random seeds, number of samples, and grading procedure were used for both models.
227
 
228
- ## Pass@k
 
 
 
 
 
 
 
 
229
 
230
- | Metric | Base Model | DPO Model |
231
- | ------- | ---------: | ---------: |
232
- | Pass@1 | 41.87% | **45.22%** |
233
- | Pass@5 | 48.87% | **51.10%** |
234
- | Pass@10 | 50.99% | **53.16%** |
235
 
236
- The improvement in Pass@1 is approximately **3.35 percentage points**.
237
 
238
- ## Partial Correctness
239
 
240
  The average proportion of tests passed by each generated answer increased from:
241
 
242
- **66.20% → 69.41%**
243
 
244
  This is a secondary metric intended to provide additional information about functional correctness beyond binary problem-level pass/fail.
245
 
246
- ---
247
 
248
- # Paired Problem-Level Analysis
249
 
250
  The evaluation used the same 508 problems for both models, allowing direct comparison of model behavior on individual problems.
251
 
252
  For the first generated answer:
253
 
254
- | Base → DPO | Problems |
255
- | --------------------- | -------: |
256
- | Correct → Correct | 186 |
257
- | Correct → Incorrect | 17 |
258
- | Incorrect → Correct | 41 |
259
- | Incorrect → Incorrect | 264 |
 
 
 
 
 
260
 
261
  The DPO model converted 41 problems from incorrect to correct while 17 previously correct problems became incorrect.
262
 
263
  This provides additional evidence that the aggregate improvement is associated with meaningful changes in problem-level behavior rather than simply differences in the evaluated problem sets.
264
 
265
- ---
266
 
267
- # Who Should Use This Model?
268
 
269
  This model is most appropriate for:
270
 
271
- ### Python Developers
272
 
273
  Developers interested in experimenting with a locally hosted code-generation model that has been specifically fine-tuned toward execution-validated solutions.
274
 
275
- ### AI/ML Researchers
276
 
277
  Researchers studying:
278
 
279
  * DPO
 
280
  * preference learning
 
281
  * LLM alignment
 
282
  * code-generation models
 
283
  * automated preference construction
 
284
  * execution-based evaluation
 
285
  * parameter-efficient fine-tuning
286
 
287
- ### LLM Evaluation Researchers
288
 
289
  The model can also be useful as an experimental model for comparing:
290
 
291
  * base versus preference-tuned models
 
292
  * coding correctness
 
293
  * Pass@k
 
294
  * partial test coverage
 
295
  * preference-learning behavior
296
 
297
- ---
298
 
299
- # When Should You Prefer the Original Qwen Model?
300
 
301
  This model should not automatically be considered better for every possible task.
302
 
303
- The experiment specifically focused on **Python code generation**.
304
 
305
  If your application requires broad general-purpose instruction following, other programming languages, or capabilities that were not evaluated here, you should benchmark both models for your specific workload.
306
 
307
  The reported improvement should therefore be interpreted as:
308
 
309
- > **Improved performance on the evaluated Python coding benchmark, not a guarantee of improvement across every coding or general-purpose task.**
310
 
311
- ---
312
-
313
- # Quick Start
314
-
315
- This repository contains the **LoRA adapter**, not a merged copy of the full model.
316
 
317
- You need the original base model:
318
 
319
- ```text
320
- Qwen/Qwen2.5-Coder-7B-Instruct
321
- ```
322
 
323
- and this adapter.
324
-
325
- Example with Transformers and PEFT:
326
-
327
- ```python
328
- from transformers import AutoModelForCausalLM, AutoTokenizer
329
- from peft import PeftModel
330
 
331
- base_model = "Qwen/Qwen2.5-Coder-7B-Instruct"
332
- adapter = "ranjanrajib/DPO-trained-Qwen2.5-Coder-7B"
333
 
334
- tokenizer = AutoTokenizer.from_pretrained(base_model)
335
 
336
- model = AutoModelForCausalLM.from_pretrained(
337
- base_model,
338
- torch_dtype="auto",
339
- device_map="auto"
340
- )
341
 
342
- model = PeftModel.from_pretrained(
343
- model,
344
- adapter
345
- )
346
 
347
- prompt = """Write a Python function that returns the
348
- factorial of a number."""
349
 
350
- messages = [
351
- {"role": "user", "content": prompt}
352
- ]
353
 
354
- text = tokenizer.apply_chat_template(
355
- messages,
356
- tokenize=False,
357
- add_generation_prompt=True
358
- )
359
 
360
- inputs = tokenizer(text, return_tensors="pt").to(model.device)
361
 
362
- outputs = model.generate(
363
- **inputs,
364
- max_new_tokens=256,
365
- temperature=0.2,
366
- top_p=0.95
367
- )
368
 
369
- print(tokenizer.decode(outputs[0], skip_special_tokens=True))
370
- ```
371
 
372
- For production serving, the adapter can also be served using infrastructure that supports LoRA adapters.
373
 
374
- ---
375
 
376
- # Model Architecture and Training
377
 
378
  The model starts from:
379
 
380
- `Qwen/Qwen2.5-Coder-7B-Instruct`
381
 
382
  The base model contains approximately 7.7 billion parameters.
383
 
384
- Rather than updating all parameters, the experiment uses **LoRA-based parameter-efficient fine-tuning**.
385
 
386
  The resulting adapter contains approximately 80.7 million trainable parameters.
387
 
388
  The adapter is approximately 308 MB.
389
 
390
- ---
391
 
392
- # DPO Training
393
 
394
- ## Why DPO?
395
 
396
  Direct Preference Optimization provides a method for optimizing a language model directly from preference pairs without requiring a separate reward-model training stage.
397
 
398
  For this experiment, a preference pair has the form:
399
 
400
- ```text
 
401
  Problem
 
402
  │
 
403
  ├── Candidate A → passes tests → chosen
 
404
  │
 
405
  └── Candidate B → fails tests → rejected
406
- ```
 
407
 
408
  DPO then learns to increase the relative likelihood of the chosen response compared with the rejected response.
409
 
410
- ---
411
 
412
- # Preference Data Construction
413
 
414
  The preference dataset was generated specifically for this experiment.
415
 
416
  It was constructed from programming problems drawn from:
417
 
418
  * BigCodeBench
 
419
  * MBPP
 
420
  * HumanEval
 
421
  * handwritten programming problems
422
 
423
- ### Training Data
 
 
 
 
 
 
 
 
424
 
425
- | Dataset | License |
426
- | ------------ | ---------- |
427
- | BigCodeBench | Apache 2.0 |
428
- | MBPP | CC BY 4.0 |
429
- | HumanEval | MIT |
430
 
431
  Candidate solutions were generated automatically.
432
 
433
  Five generation strategies were used:
434
 
435
  * normal
 
436
  * straightforward
 
437
  * edge-case-focused
 
438
  * alternative
 
439
  * optimized
440
 
441
  Multiple candidates were generated for each problem.
@@ -444,18 +507,21 @@ Every candidate was then executed in a Docker sandbox against the associated uni
444
 
445
  The resulting execution outcome was used to construct chosen/rejected preference pairs.
446
 
447
- ---
448
 
449
- # Preference Dataset
450
 
451
  After filtering and deduplication:
452
 
453
  * 1,326 preference pairs
 
454
  * 221 programming problems
 
455
  * 933 training pairs
 
456
  * 114 validation pairs
457
 
458
- The dataset was split by **problem**, rather than by individual preference pair.
459
 
460
  This prevents different candidate solutions from the same underlying programming problem from being distributed across training and validation.
461
 
@@ -465,91 +531,121 @@ No external LLM was used as a preference judge.
465
 
466
  The preference signal was generated from objective unit-test execution.
467
 
468
- ---
469
 
470
- # DPO Configuration
471
 
472
  The primary DPO configuration was:
473
 
474
- | Parameter | Value |
475
- | ----------------------- | ------- |
476
- | DPO beta | 0.1 |
477
- | Learning rate | 1e-5 |
478
- | Scheduler | Cosine |
479
- | Warmup | 5% |
480
- | Epochs | 2 |
481
- | Maximum sequence length | 1,024 |
482
- | Batch size | 4 |
483
- | Gradient accumulation | 4 |
484
- | Effective batch size | 16 |
485
- | Optimizer | AdamW |
486
- | Gradient clipping | 1.0 |
487
- | Gradient checkpointing | Enabled |
488
-
489
- ## LoRA Configuration
490
-
491
- | Parameter | Value |
492
- | -------------- | ----------------------------- |
493
- | Rank | 32 |
494
- | Alpha | 64 |
495
- | Dropout | 0.05 |
496
- | Target modules | Attention and MLP projections |
497
 
498
- ---
 
 
 
 
499
 
500
- # Training Hardware
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
501
 
502
  DPO training was performed on:
503
 
504
- **NVIDIA RTX A6000 — 48 GB VRAM**
505
 
506
  Training environment:
507
 
508
  * CUDA 12.8
 
509
  * PyTorch 2.11
 
510
  * bfloat16
 
511
  * gradient checkpointing
512
 
513
  Peak GPU memory consumption was approximately 33.4 GiB.
514
 
515
- ---
516
 
517
- # Evaluation Methodology
518
 
519
  The primary evaluation benchmark consisted of:
520
 
521
- **508 BigCodeBench programming problems**
522
 
523
  The evaluation problems were reserved separately from the preference-training pool.
524
 
525
  The same generation and grading procedure was applied to both models.
526
 
527
- ### Generation
528
 
529
  * 10 answers per problem
 
530
  * temperature: 0.2
 
531
  * top-p: 0.95
 
532
  * maximum new tokens: 512
 
533
  * repetition penalty: 1.0
 
534
  * sampling enabled
 
535
  * identical random seeds for base and DPO models
536
 
537
- ### Pass@k
538
 
539
  Pass@k was calculated using the standard unbiased estimator based on the number of correct samples among the generated candidates.
540
 
541
- ---
542
 
543
- # Preference-Learning Diagnostics
544
 
545
  The DPO training objective produced a strong preference signal on the training examples.
546
 
547
  However, the validation results were substantially weaker.
548
 
549
- | Metric | Training | Validation |
550
- | ------------------- | -------: | --------------------: |
551
- | Preference accuracy | ~84.4% | ~45.8% |
552
- | Reward margin | Positive | Approximately neutral |
 
 
 
553
 
554
  This indicates that the explicit preference signal learned during training did not generalize strongly to the validation preference examples.
555
 
@@ -557,28 +653,31 @@ At the same time, downstream coding performance improved on the held-out benchma
557
 
558
  This is an important observation from the experiment.
559
 
560
- ---
561
 
562
- # What Does This Mean?
563
 
564
  The results should not be interpreted as proof that the model learned a broadly generalized notion of code quality.
565
 
566
- Instead, they suggest that **optimizing an automatically constructed preference objective can improve downstream coding performance even when explicit preference metrics show weak validation generalization.**
567
 
568
  Several explanations are possible, including:
569
 
570
  * differences between training and validation preference distributions
 
571
  * imperfect test-derived preference labels
 
572
  * overfitting to characteristics of the training preference pairs
 
573
  * changes in generation behavior that are not fully captured by preference accuracy
574
 
575
  These are hypotheses for further investigation rather than established conclusions.
576
 
577
- ---
578
 
579
- # Important Limitations
580
 
581
- ## No Human Preference Evaluation
582
 
583
  No human preference study was performed.
584
 
@@ -586,99 +685,127 @@ The preference signal was derived from automated unit-test execution.
586
 
587
  Therefore, this model should not be described as being aligned to human preferences based on this experiment.
588
 
589
- ## Unit Tests Are Imperfect
590
 
591
  Passing the available tests does not guarantee that a solution is universally correct.
592
 
593
  A test suite may fail to cover some edge cases or undesirable implementation behavior.
594
 
595
- ## Benchmark Source Overlap
596
 
597
  The evaluation problems were held out at the problem level.
598
 
599
  However, the preference-training data includes other BigCodeBench problems.
600
 
601
- Therefore, the evaluation is **problem-held-out**, but not completely source-family-disjoint.
602
 
603
- ## Python Focus
604
 
605
  The primary objective and evaluation were focused on Python programming.
606
 
607
  Performance should not be assumed to generalize to other programming languages without additional evaluation.
608
 
609
- ## Experimental Model
610
 
611
  This is an experimental research model.
612
 
613
  Users should evaluate it against their own workloads before using it in production.
614
 
615
- ---
616
 
617
- # Reproducibility
618
 
619
  The experiment documents:
620
 
621
  * base model
 
622
  * preference-data construction
 
623
  * data sources
 
624
  * dataset split methodology
 
625
  * DPO configuration
 
626
  * LoRA configuration
 
627
  * training hardware
 
628
  * generation configuration
 
629
  * evaluation methodology
 
630
  * random seed strategy
 
631
  * benchmark methodology
 
632
  * limitations
633
 
634
  The objective is to make the experiment sufficiently transparent for others to reproduce, extend, or critique.
635
 
636
- ---
637
 
638
- # Independent Research Project
639
 
640
- This model was developed as an **independent, out-of-office AI/ML research project**.
641
 
642
  The project was conceived, designed, implemented, trained, evaluated, analyzed, and released independently.
643
 
644
  The work covers the complete experimental lifecycle:
645
 
646
- ```text
 
647
  Research Question
 
648
  ↓
 
649
  Experimental Design
 
650
  ↓
 
651
  Preference Data Generation
 
652
  ↓
 
653
  Automated Evaluation
 
654
  ↓
 
655
  DPO + LoRA Training
 
656
  ↓
 
657
  Controlled Benchmarking
 
658
  ↓
 
659
  Statistical / Paired Analysis
 
660
  ↓
 
661
  Model Release
662
- ```
663
 
664
- ---
 
 
665
 
666
- # Model Release
667
 
668
  The trained LoRA adapter and technical documentation are available here:
669
 
670
- **Hugging Face:**
 
671
  https://huggingface.co/ranjanrajib/DPO-trained-Qwen2.5-Coder-7B
672
 
673
- The repository contains the model adapter and documentation required to understand the training methodology, evaluation, results, intended use, and limitations.
674
 
675
- ---
676
 
677
- # Licensing and Attribution
678
 
679
  The model is derived from:
680
 
681
- `Qwen/Qwen2.5-Coder-7B-Instruct`
682
 
683
  Users should review and comply with the licensing and usage terms of the base model.
684
 
@@ -686,47 +813,50 @@ The preference-generation pipeline used programming problems from BigCodeBench,
686
 
687
  The respective dataset licenses and attribution requirements should be observed when using or redistributing material derived from those datasets.
688
 
689
- ---
690
 
691
- # Citation
692
 
693
  If you use this model or the experimental methodology in your work, please cite the original Qwen2.5-Coder model and the relevant datasets used in the experiment.
694
 
695
- ---
696
 
697
- # Summary
698
 
699
- **What is this?**
700
 
701
  A Python-focused DPO fine-tuned version of Qwen2.5-Coder-7B-Instruct.
702
 
703
- **What is different?**
704
 
705
  The model was trained using LoRA-based Direct Preference Optimization with automatically constructed preferences derived from unit-test execution.
706
 
707
- **Does it improve coding performance?**
708
 
709
- On the evaluated held-out benchmark, yes. Pass@1 improved from **41.87% to 45.22%**, with improvements also observed in Pass@5, Pass@10, and partial test coverage.
710
 
711
- **Is it human-aligned?**
712
 
713
  No human preference evaluation was performed. The preference signal was generated automatically from unit-test outcomes.
714
 
715
- **Who should use it?**
716
 
717
  Users primarily interested in Python code generation, coding experiments, DPO research, preference learning, and execution-based LLM evaluation.
718
 
719
- **What is being released?**
720
 
721
- A LoRA adapter that can be loaded on top of `Qwen/Qwen2.5-Coder-7B-Instruct`.
722
 
723
- **Why is the experiment interesting?**
724
 
725
  The model improved downstream coding performance even though explicit preference metrics showed weak validation generalization, highlighting the distinction between optimizing a preference objective and improving downstream model capability.
726
 
727
 
728
- ## Licence and attribution
729
 
730
- Apache-2.0, inherited from `Qwen/Qwen2.5-Coder-7B-Instruct`. This is a modified derivative of that
 
 
 
731
  model; the original is by the Qwen team at Alibaba Cloud. The preference data was generated from the
 
732
  base model's own outputs on BigCodeBench problems.
 
23
  - parameter-efficient-fine-tuning
24
  ---
25
 
26
+ # Qwen2.5-Coder-7B-Instruct — DPO Fine-Tuned for Python**
27
 
28
+ **## Model at a Glance**
29
 
30
+ This model is a ****Python-focused, DPO fine-tuned version of Qwen2.5-Coder-7B-Instruct****, designed to improve the model's ability to generate ****functionally correct Python code****.
31
 
32
+ The model was trained using ****Direct Preference Optimization (DPO)**** with automatically constructed preference pairs. Candidate Python solutions were generated for programming problems and evaluated by executing them against unit tests. Solutions that passed the required tests were used as preferred examples, while solutions that failed tests were used as rejected examples.
33
 
34
  The resulting model was evaluated against the original Qwen2.5-Coder-7B-Instruct model on held-out programming problems.
35
 
36
+ **### What changed?**
37
 
38
  The base model:
39
 
40
+ ****Qwen2.5-Coder-7B-Instruct****
41
 
42
  was fine-tuned using:
43
 
44
+ ****DPO + LoRA + execution-based preference data****
45
 
46
  The resulting model demonstrates improved coding performance on the evaluated held-out benchmark:
47
 
48
+ \| Metric | Qwen2.5-Coder-7B-Instruct | DPO Model | Improvement |
 
 
 
 
 
49
 
50
+ \| ----------------------- | ------------------------: | ---------: | -----------: |
51
+
52
+ \| Pass@1 | 41.87% | ****45.22%**** | ****+3.35 pp**** |
53
+
54
+ \| Pass@5 | 48.87% | ****51.10%**** | ****+2.23 pp**** |
55
+
56
+ \| Pass@10 | 50.99% | ****53.16%**** | ****+2.17 pp**** |
57
+
58
+ \| Tests passed per answer | 66.20% | ****69.41%**** | ****+3.21 pp**** |
59
+
60
+ The primary result is the improvement in ****Pass@1****, meaning that a single generated solution was more likely to produce a correct solution on the evaluated programming problems.
61
 
62
  The model also showed improved Pass@5, Pass@10, and partial test coverage.
63
 
64
+ **---**
65
+
66
+ ****Base model:**** \`Qwen/Qwen2.5-Coder-7B-Instruct\` at snapshot
67
 
68
+ \`c03e6d358207e414f1eca0bb1891e29f1db0e242\`. The adapter was trained against exactly these weights.
 
69
 
70
+ **## Downloading and using**
71
 
72
+ **### The LoRA adapter (recommended release)**
73
+
74
+ \`\`\`python
75
 
 
76
  from transformers import AutoModelForCausalLM, AutoTokenizer
77
 
78
  model = AutoModelForCausalLM.from_pretrained(
79
+
80
  "ranjanrajib/DPO-trained-Qwen2.5-Coder-7B", dtype="bfloat16", device_map="auto")
81
+
82
  tok = AutoTokenizer.from_pretrained("ranjanrajib/DPO-trained-Qwen2.5-Coder-7B")
83
 
84
  msgs = [{"role": "user", "content": "Write a Python function that merges overlapping intervals."}]
85
+
86
  text = tok.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True)
87
+
88
  out = model.generate(**tok(text, return_tensors="pt").to(model.device), max_new_tokens=512)
89
+
90
  print(tok.decode(out[0], skip_special_tokens=True))
91
+
92
+ \`\`\`
93
 
94
  Or fetch the files without loading them (~14 GB, four shards):
95
 
96
+ \`\`\`bash
97
+
98
  hf download ranjanrajib/DPO-trained-Qwen2.5-Coder-7B --exclude "adapter/*" --local-dir ./d4
 
99
 
100
+ \`\`\`
101
+
102
+ **### The LoRA adapter only (308 MB)**
103
+
104
+ The adapter lives in \`adapter/\`. Applying it to the base model gives the same result as the
105
 
 
106
  merged weights, at a fraction of the download.
107
 
108
+ \`\`\`bash
109
+
110
  hf download ranjanrajib/DPO-trained-Qwen2.5-Coder-7B --include "adapter/*" --local-dir ./d4-lora
 
111
 
112
+ \`\`\`
113
+
114
+ \`\`\`python
115
+
116
  import torch
117
+
118
  from peft import PeftModel
119
+
120
  from transformers import AutoModelForCausalLM
121
 
122
  BASE = "Qwen/Qwen2.5-Coder-7B-Instruct"
123
+
124
  REVISION = "c03e6d358207e414f1eca0bb1891e29f1db0e242" # the snapshot this was trained against
125
 
126
  base = AutoModelForCausalLM.from_pretrained(
127
+
128
  BASE, revision=REVISION, dtype=torch.bfloat16, device_map="auto")
129
+
130
  model = PeftModel.from_pretrained(
131
+
132
  base, "ranjanrajib/DPO-trained-Qwen2.5-Coder-7B", subfolder="adapter")
 
 
133
 
134
+ \`\`\`
135
+
136
+ **---**
137
+
138
+ **# Why Use This Model?**
139
 
140
+ If your primary use case is ****Python code generation****, this model is intended as an experimental alternative to the original Qwen2.5-Coder-7B-Instruct model, with higher measured performance on the held-out benchmark used in this project.
141
 
142
  It is particularly relevant for users who want to:
143
 
144
  * generate Python functions and implementations
145
+
146
  * solve programming problems
147
+
148
  * generate code that needs to satisfy functional requirements
149
+
150
  * improve first-attempt coding correctness
151
+
152
  * experiment with DPO-trained code-generation models
153
+
154
  * study execution-based preference optimization
155
+
156
  * build research or evaluation pipelines for Python code generation
157
 
158
  The main motivation for the fine-tuning was not simply to make the model generate more code.
159
 
160
+ It was to encourage the model toward ****solutions that work****.
161
 
162
+ The preference data therefore used actual program execution and unit-test results rather than subjective judgments about whether generated code **looked** correct.
163
 
164
+ **---**
165
 
166
+ **# How Is It Different From Qwen2.5-Coder-7B-Instruct?**
167
 
168
+ The underlying model architecture and capabilities come from ****Qwen2.5-Coder-7B-Instruct****.
169
 
170
  This model adds a preference-optimization stage on top of that model.
171
 
172
  Conceptually:
173
 
174
+ \`\`\`text
175
+
176
  Qwen2.5-Coder-7B-Instruct
177
+
178
  │
179
+
180
  ▼
181
+
182
  Generate multiple Python
183
+
184
  candidate solutions
185
+
186
  │
187
+
188
  ▼
189
+
190
  Execute candidates
191
+
192
  against tests
193
+
194
  │
195
+
196
  ┌─────┴─────┐
197
+
198
  ▼ ▼
199
+
200
  Pass Fail
201
+
202
  │ │
203
+
204
  ▼ ▼
205
+
206
  Chosen Rejected
207
+
208
  │ │
209
+
210
  └─────┬─────┘
211
+
212
  ▼
213
+
214
  DPO
215
+
216
  │
217
+
218
  ▼
219
+
220
  LoRA Adapter
221
+
222
  │
223
+
224
  ▼
225
+
226
  Python-focused model
227
+
228
+ \`\`\`
229
 
230
  The goal is to shift the model toward coding behaviors associated with solutions that successfully execute against the available tests.
231
 
232
+ **### Important distinction**
233
 
234
+ This is ****not a new foundation model****.
235
 
236
  It is a parameter-efficient fine-tuned adapter for Qwen2.5-Coder-7B-Instruct.
237
 
238
  The base model remains unchanged.
239
 
240
+ **---**
241
 
242
+ **# What Kind of Model Is This?**
243
 
244
+ **### Primary task**
245
 
246
+ ****Python code generation****
247
 
248
+ **### Model family**
249
 
250
  Qwen2.5-Coder
251
 
252
+ **### Base model**
253
 
254
+ \`Qwen/Qwen2.5-Coder-7B-Instruct\`
255
 
256
+ **### Fine-tuning**
257
 
258
  Direct Preference Optimization (DPO)
259
 
260
+ **### Parameter-efficient method**
261
 
262
  LoRA
263
 
264
+ **### Precision**
265
 
266
  bfloat16
267
 
268
+ **### Quantization**
269
 
270
  None
271
 
272
+ **### Model size**
273
 
274
  7.7B-parameter base model
275
 
276
+ **### Adapter**
277
 
278
  Approximately 80.7M trainable parameters
279
 
280
+ **---**
281
 
282
+ **# Performance**
283
 
284
  The model was evaluated against the original Qwen2.5-Coder-7B-Instruct using a controlled benchmark consisting of 508 held-out BigCodeBench programming problems.
285
 
286
  The same prompts, generation settings, random seeds, number of samples, and grading procedure were used for both models.
287
 
288
+ **## Pass@k**
289
+
290
+ \| Metric | Base Model | DPO Model |
291
+
292
+ \| ------- | ---------: | ---------: |
293
+
294
+ \| Pass@1 | 41.87% | ****45.22%**** |
295
+
296
+ \| Pass@5 | 48.87% | ****51.10%**** |
297
 
298
+ \| Pass@10 | 50.99% | ****53.16%**** |
 
 
 
 
299
 
300
+ The improvement in Pass@1 is approximately ****3.35 percentage points****.
301
 
302
+ **## Partial Correctness**
303
 
304
  The average proportion of tests passed by each generated answer increased from:
305
 
306
+ ****66.20% → 69.41%****
307
 
308
  This is a secondary metric intended to provide additional information about functional correctness beyond binary problem-level pass/fail.
309
 
310
+ **---**
311
 
312
+ **# Paired Problem-Level Analysis**
313
 
314
  The evaluation used the same 508 problems for both models, allowing direct comparison of model behavior on individual problems.
315
 
316
  For the first generated answer:
317
 
318
+ \| Base → DPO | Problems |
319
+
320
+ \| --------------------- | -------: |
321
+
322
+ \| Correct → Correct | 186 |
323
+
324
+ \| Correct → Incorrect | 17 |
325
+
326
+ \| Incorrect → Correct | 41 |
327
+
328
+ \| Incorrect → Incorrect | 264 |
329
 
330
  The DPO model converted 41 problems from incorrect to correct while 17 previously correct problems became incorrect.
331
 
332
  This provides additional evidence that the aggregate improvement is associated with meaningful changes in problem-level behavior rather than simply differences in the evaluated problem sets.
333
 
334
+ **---**
335
 
336
+ **# Who Should Use This Model?**
337
 
338
  This model is most appropriate for:
339
 
340
+ **### Python Developers**
341
 
342
  Developers interested in experimenting with a locally hosted code-generation model that has been specifically fine-tuned toward execution-validated solutions.
343
 
344
+ **### AI/ML Researchers**
345
 
346
  Researchers studying:
347
 
348
  * DPO
349
+
350
  * preference learning
351
+
352
  * LLM alignment
353
+
354
  * code-generation models
355
+
356
  * automated preference construction
357
+
358
  * execution-based evaluation
359
+
360
  * parameter-efficient fine-tuning
361
 
362
+ **### LLM Evaluation Researchers**
363
 
364
  The model can also be useful as an experimental model for comparing:
365
 
366
  * base versus preference-tuned models
367
+
368
  * coding correctness
369
+
370
  * Pass@k
371
+
372
  * partial test coverage
373
+
374
  * preference-learning behavior
375
 
376
+ **---**
377
 
378
+ **# When Should You Prefer the Original Qwen Model?**
379
 
380
  This model should not automatically be considered better for every possible task.
381
 
382
+ The experiment specifically focused on ****Python code generation****.
383
 
384
  If your application requires broad general-purpose instruction following, other programming languages, or capabilities that were not evaluated here, you should benchmark both models for your specific workload.
385
 
386
  The reported improvement should therefore be interpreted as:
387
 
388
+ \> ****Improved performance on the evaluated Python coding benchmark, not a guarantee of improvement across every coding or general-purpose task.****
389
 
390
+ **---**
 
 
 
 
391
 
392
+ The base model and the trained model use the same VRAM.
393
 
394
+ The merge changed weight values, not shapes — same 7.6B parameters, same
 
 
395
 
396
+ architecture — so they are byte-for-byte the same size.
 
 
 
 
 
 
397
 
398
+ ┌───────────────────────────────┬────────────┐
 
399
 
400
+ │ │ bf16 VRAM │
401
 
402
+ ├───────────────────────────────┼────────────┤
 
 
 
 
403
 
404
+ │ Weights (base or merged D4) │ 14.19 GiB │
 
 
 
405
 
406
+ ├───────────────────────────────┼────────────┤
 
407
 
408
+ │ KV cache, per token │ 56 KiB │
 
 
409
 
410
+ ├───────────────────────────────┼────────────┤
 
 
 
 
411
 
412
+ │ Activations / workspace │ ~0.5–1 GiB │
413
 
414
+ ├───────────────────────────────┼────────────┤
 
 
 
 
 
415
 
416
+ │ Practical single-stream total │ ~15–16 GiB │
 
417
 
418
+ └───────────────────────────────┴────────────┘
419
 
420
+ **---**
421
 
422
+ **# Model Architecture and Training**
423
 
424
  The model starts from:
425
 
426
+ \`Qwen/Qwen2.5-Coder-7B-Instruct\`
427
 
428
  The base model contains approximately 7.7 billion parameters.
429
 
430
+ Rather than updating all parameters, the experiment uses ****LoRA-based parameter-efficient fine-tuning****.
431
 
432
  The resulting adapter contains approximately 80.7 million trainable parameters.
433
 
434
  The adapter is approximately 308 MB.
435
 
436
+ **---**
437
 
438
+ **# DPO Training**
439
 
440
+ **## Why DPO?**
441
 
442
  Direct Preference Optimization provides a method for optimizing a language model directly from preference pairs without requiring a separate reward-model training stage.
443
 
444
  For this experiment, a preference pair has the form:
445
 
446
+ \`\`\`text
447
+
448
  Problem
449
+
450
  │
451
+
452
  ├── Candidate A → passes tests → chosen
453
+
454
  │
455
+
456
  └── Candidate B → fails tests → rejected
457
+
458
+ \`\`\`
459
 
460
  DPO then learns to increase the relative likelihood of the chosen response compared with the rejected response.
461
 
462
+ **---**
463
 
464
+ **# Preference Data Construction**
465
 
466
  The preference dataset was generated specifically for this experiment.
467
 
468
  It was constructed from programming problems drawn from:
469
 
470
  * BigCodeBench
471
+
472
  * MBPP
473
+
474
  * HumanEval
475
+
476
  * handwritten programming problems
477
 
478
+ **### Training Data**
479
+
480
+ \| Dataset | License |
481
+
482
+ \| ------------ | ---------- |
483
+
484
+ \| BigCodeBench | Apache 2.0 |
485
+
486
+ \| MBPP | CC BY 4.0 |
487
 
488
+ \| HumanEval | MIT |
 
 
 
 
489
 
490
  Candidate solutions were generated automatically.
491
 
492
  Five generation strategies were used:
493
 
494
  * normal
495
+
496
  * straightforward
497
+
498
  * edge-case-focused
499
+
500
  * alternative
501
+
502
  * optimized
503
 
504
  Multiple candidates were generated for each problem.
 
507
 
508
  The resulting execution outcome was used to construct chosen/rejected preference pairs.
509
 
510
+ **---**
511
 
512
+ **# Preference Dataset**
513
 
514
  After filtering and deduplication:
515
 
516
  * 1,326 preference pairs
517
+
518
  * 221 programming problems
519
+
520
  * 933 training pairs
521
+
522
  * 114 validation pairs
523
 
524
+ The dataset was split by ****problem****, rather than by individual preference pair.
525
 
526
  This prevents different candidate solutions from the same underlying programming problem from being distributed across training and validation.
527
 
 
531
 
532
  The preference signal was generated from objective unit-test execution.
533
 
534
+ **---**
535
 
536
+ **# DPO Configuration**
537
 
538
  The primary DPO configuration was:
539
 
540
+ \| Parameter | Value |
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
541
 
542
+ \| ----------------------- | ------- |
543
+
544
+ \| DPO beta | 0.1 |
545
+
546
+ \| Learning rate | 1e-5 |
547
 
548
+ \| Scheduler | Cosine |
549
+
550
+ \| Warmup | 5% |
551
+
552
+ \| Epochs | 2 |
553
+
554
+ \| Maximum sequence length | 1,024 |
555
+
556
+ \| Batch size | 4 |
557
+
558
+ \| Gradient accumulation | 4 |
559
+
560
+ \| Effective batch size | 16 |
561
+
562
+ \| Optimizer | AdamW |
563
+
564
+ \| Gradient clipping | 1.0 |
565
+
566
+ \| Gradient checkpointing | Enabled |
567
+
568
+ **## LoRA Configuration**
569
+
570
+ \| Parameter | Value |
571
+
572
+ \| -------------- | ----------------------------- |
573
+
574
+ \| Rank | 32 |
575
+
576
+ \| Alpha | 64 |
577
+
578
+ \| Dropout | 0.05 |
579
+
580
+ \| Target modules | Attention and MLP projections |
581
+
582
+ **---**
583
+
584
+ **# Training Hardware**
585
 
586
  DPO training was performed on:
587
 
588
+ ****NVIDIA RTX A6000 — 48 GB VRAM****
589
 
590
  Training environment:
591
 
592
  * CUDA 12.8
593
+
594
  * PyTorch 2.11
595
+
596
  * bfloat16
597
+
598
  * gradient checkpointing
599
 
600
  Peak GPU memory consumption was approximately 33.4 GiB.
601
 
602
+ **---**
603
 
604
+ **# Evaluation Methodology**
605
 
606
  The primary evaluation benchmark consisted of:
607
 
608
+ ****508 BigCodeBench programming problems****
609
 
610
  The evaluation problems were reserved separately from the preference-training pool.
611
 
612
  The same generation and grading procedure was applied to both models.
613
 
614
+ **### Generation**
615
 
616
  * 10 answers per problem
617
+
618
  * temperature: 0.2
619
+
620
  * top-p: 0.95
621
+
622
  * maximum new tokens: 512
623
+
624
  * repetition penalty: 1.0
625
+
626
  * sampling enabled
627
+
628
  * identical random seeds for base and DPO models
629
 
630
+ **### Pass@k**
631
 
632
  Pass@k was calculated using the standard unbiased estimator based on the number of correct samples among the generated candidates.
633
 
634
+ **---**
635
 
636
+ **# Preference-Learning Diagnostics**
637
 
638
  The DPO training objective produced a strong preference signal on the training examples.
639
 
640
  However, the validation results were substantially weaker.
641
 
642
+ \| Metric | Training | Validation |
643
+
644
+ \| ------------------- | -------: | --------------------: |
645
+
646
+ \| Preference accuracy | ~84.4% | ~45.8% |
647
+
648
+ \| Reward margin | Positive | Approximately neutral |
649
 
650
  This indicates that the explicit preference signal learned during training did not generalize strongly to the validation preference examples.
651
 
 
653
 
654
  This is an important observation from the experiment.
655
 
656
+ **---**
657
 
658
+ **# What Does This Mean?**
659
 
660
  The results should not be interpreted as proof that the model learned a broadly generalized notion of code quality.
661
 
662
+ Instead, they suggest that ****optimizing an automatically constructed preference objective can improve downstream coding performance even when explicit preference metrics show weak validation generalization.****
663
 
664
  Several explanations are possible, including:
665
 
666
  * differences between training and validation preference distributions
667
+
668
  * imperfect test-derived preference labels
669
+
670
  * overfitting to characteristics of the training preference pairs
671
+
672
  * changes in generation behavior that are not fully captured by preference accuracy
673
 
674
  These are hypotheses for further investigation rather than established conclusions.
675
 
676
+ **---**
677
 
678
+ **# Important Limitations**
679
 
680
+ **## No Human Preference Evaluation**
681
 
682
  No human preference study was performed.
683
 
 
685
 
686
  Therefore, this model should not be described as being aligned to human preferences based on this experiment.
687
 
688
+ **## Unit Tests Are Imperfect**
689
 
690
  Passing the available tests does not guarantee that a solution is universally correct.
691
 
692
  A test suite may fail to cover some edge cases or undesirable implementation behavior.
693
 
694
+ **## Benchmark Source Overlap**
695
 
696
  The evaluation problems were held out at the problem level.
697
 
698
  However, the preference-training data includes other BigCodeBench problems.
699
 
700
+ Therefore, the evaluation is ****problem-held-out****, but not completely source-family-disjoint.
701
 
702
+ **## Python Focus**
703
 
704
  The primary objective and evaluation were focused on Python programming.
705
 
706
  Performance should not be assumed to generalize to other programming languages without additional evaluation.
707
 
708
+ **## Experimental Model**
709
 
710
  This is an experimental research model.
711
 
712
  Users should evaluate it against their own workloads before using it in production.
713
 
714
+ **---**
715
 
716
+ **# Reproducibility**
717
 
718
  The experiment documents:
719
 
720
  * base model
721
+
722
  * preference-data construction
723
+
724
  * data sources
725
+
726
  * dataset split methodology
727
+
728
  * DPO configuration
729
+
730
  * LoRA configuration
731
+
732
  * training hardware
733
+
734
  * generation configuration
735
+
736
  * evaluation methodology
737
+
738
  * random seed strategy
739
+
740
  * benchmark methodology
741
+
742
  * limitations
743
 
744
  The objective is to make the experiment sufficiently transparent for others to reproduce, extend, or critique.
745
 
746
+ **---**
747
 
748
+ **# Independent Research Project**
749
 
750
+ This model was developed as an ****independent, out-of-office AI/ML research project****.
751
 
752
  The project was conceived, designed, implemented, trained, evaluated, analyzed, and released independently.
753
 
754
  The work covers the complete experimental lifecycle:
755
 
756
+ \`\`\`text
757
+
758
  Research Question
759
+
760
  ↓
761
+
762
  Experimental Design
763
+
764
  ↓
765
+
766
  Preference Data Generation
767
+
768
  ↓
769
+
770
  Automated Evaluation
771
+
772
  ↓
773
+
774
  DPO + LoRA Training
775
+
776
  ↓
777
+
778
  Controlled Benchmarking
779
+
780
  ↓
781
+
782
  Statistical / Paired Analysis
783
+
784
  ↓
785
+
786
  Model Release
 
787
 
788
+ \`\`\`
789
+
790
+ **---**
791
 
792
+ **# Model Release**
793
 
794
  The trained LoRA adapter and technical documentation are available here:
795
 
796
+ ****Hugging Face:****
797
+
798
  https://huggingface.co/ranjanrajib/DPO-trained-Qwen2.5-Coder-7B
799
 
800
+ The repository contains the LoRA adapter and documentation required to understand the training methodology, evaluation, results, intended use, and limitations. The base Qwen weights are not redistributed in this repository.
801
 
802
+ **---**
803
 
804
+ **# Licensing and Attribution**
805
 
806
  The model is derived from:
807
 
808
+ \`Qwen/Qwen2.5-Coder-7B-Instruct\`
809
 
810
  Users should review and comply with the licensing and usage terms of the base model.
811
 
 
813
 
814
  The respective dataset licenses and attribution requirements should be observed when using or redistributing material derived from those datasets.
815
 
816
+ **---**
817
 
818
+ **# Citation**
819
 
820
  If you use this model or the experimental methodology in your work, please cite the original Qwen2.5-Coder model and the relevant datasets used in the experiment.
821
 
822
+ **---**
823
 
824
+ **# Summary**
825
 
826
+ ****What is this?****
827
 
828
  A Python-focused DPO fine-tuned version of Qwen2.5-Coder-7B-Instruct.
829
 
830
+ ****What is different?****
831
 
832
  The model was trained using LoRA-based Direct Preference Optimization with automatically constructed preferences derived from unit-test execution.
833
 
834
+ ****Does it improve coding performance?****
835
 
836
+ On the evaluated held-out benchmark, yes. Pass@1 improved from ****41.87% to 45.22%****, with improvements also observed in Pass@5, Pass@10, and partial test coverage.
837
 
838
+ ****Is it human-aligned?****
839
 
840
  No human preference evaluation was performed. The preference signal was generated automatically from unit-test outcomes.
841
 
842
+ ****Who should use it?****
843
 
844
  Users primarily interested in Python code generation, coding experiments, DPO research, preference learning, and execution-based LLM evaluation.
845
 
846
+ ****What is being released?****
847
 
848
+ A LoRA adapter that can be loaded on top of \`Qwen/Qwen2.5-Coder-7B-Instruct\`.
849
 
850
+ ****Why is the experiment interesting?****
851
 
852
  The model improved downstream coding performance even though explicit preference metrics showed weak validation generalization, highlighting the distinction between optimizing a preference objective and improving downstream model capability.
853
 
854
 
 
855
 
856
+ **## Licence and attribution**
857
+
858
+ Apache-2.0, inherited from \`Qwen/Qwen2.5-Coder-7B-Instruct\`. This is a modified derivative of that
859
+
860
  model; the original is by the Qwen team at Alibaba Cloud. The preference data was generated from the
861
+
862
  base model's own outputs on BigCodeBench problems.