Text Generation
MLX
lora
gemma
code-generation
File size: 2,811 Bytes
a51030b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e596007
a51030b
 
 
 
e596007
a51030b
e596007
a51030b
e596007
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a51030b
 
 
 
 
 
e596007
 
 
 
a51030b
 
e596007
 
 
a51030b
 
 
e596007
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
---
license: apache-2.0
license_link: https://ai.google.dev/gemma/docs/gemma_4_license
base_model: mlx-community/gemma-4-e4b-4bit
library_name: mlx
pipeline_tag: text-generation
inference: false
tags:
  - mlx
  - lora
  - gemma
  - code-generation
  - text-generation
datasets:
  - iamtarun/python_code_instructions_18k_alpaca
---

# gemma-4-e4b-python-lora

A LoRA adapter that fine-tunes [`mlx-community/gemma-4-e4b-4bit`](https://huggingface.co/mlx-community/gemma-4-e4b-4bit)
for Python code generation, trained with [`mlx_lm`](https://github.com/ml-explore/mlx-lm).

This repo contains **only the adapter weights** — you load it on top of the base
model, you don't need to download a separate fused model.

## Training details

| | |
|---|---|
| Base model | `mlx-community/gemma-4-e4b-4bit` |
| Dataset | [`iamtarun/python_code_instructions_18k_alpaca`](https://huggingface.co/datasets/iamtarun/python_code_instructions_18k_alpaca) |
| Fine-tune type | LoRA |
| LoRA rank | 8 |
| LoRA scale | 20 |
| LoRA dropout | 0.0 |
| Layers adapted | 16 |
| Learning rate | 1e-5 |
| Optimizer | Adam |
| Iterations | 500 |

Training examples were formatted as:

```
### User:
{instruction}

### Assistant:
{output}
```

## Usage

`mlx_lm.load` needs the adapter on disk, so download it locally first, then load it
exactly as you would a local adapter:

```bash
pip install -U mlx-lm huggingface_hub
```

```python
from huggingface_hub import snapshot_download
from mlx_lm import load, generate

adapter_path = snapshot_download("Prashanth-24/gemma-4-e4b-python-lora")
model, tokenizer = load("mlx-community/gemma-4-e4b-4bit", adapter_path=adapter_path)

prompt = "### User:\nwrite a python code to ADD two numbers\n\n### Assistant:\n"
print(generate(model, tokenizer, prompt=prompt, max_tokens=200))
```

## Example output

Prompt: `Create a python program to multiply two numbers.`

**Base model**

```
Here's a response to your query:

**1. Introduction**

**2. Part I (Body)**

**Introduction**
The concept of unit multiplication is relevant in understanding how to multiply two numbers.

**Part II**

**Part II**

**Key Topics**

**Key Topics**
- **Factors to Consider**
- **Key Topics**
- **Key Topics**
...
```

**Fine-tuned model**

```python
def multiply_two_numbers(a, b):
    return a * b

print(multiply_two_numbers(2, 3))
```

The base model drifts into a generic essay outline and never writes any code. The
fine-tuned model answers directly with a correct, runnable Python function — the LoRA
clearly taught the `### Assistant:` turn to produce code instead of chatter.

## Limitations

This is a small run (500 iterations, rank 8). It reliably improves "answer with code"
behavior and formatting, but it's a lightweight fine-tune — treat outputs as a draft to
review, not production-ready code.