abumafrim commited on
Commit
76fc77d
·
verified ·
1 Parent(s): ff02074

Upload gemma_3_4b_it-lora-r32-hau-eng LoRA adapter

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ tokenizer.json filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,216 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: peft
3
+ base_model: google/gemma-3-4b-it
4
+ language:
5
+ - ha
6
+ - en
7
+ tags:
8
+ - translation
9
+ - african-languages
10
+ - scientific-translation
11
+ - afriscience-mt
12
+ - lora
13
+ - peft
14
+ - gemma
15
+ license: apache-2.0
16
+ pipeline_tag: translation
17
+ model-index:
18
+ - name: gemma_3_4b_it-lora-r32-hau-eng
19
+ results:
20
+ - task:
21
+ type: translation
22
+ metrics:
23
+ - name: BLEU (test)
24
+ type: bleu
25
+ value: 40.46
26
+ - name: chrF (test)
27
+ type: chrf
28
+ value: 59.83
29
+ - name: SSA-COMET (test)
30
+ type: comet
31
+ value: 65.03
32
+ ---
33
+
34
+ # gemma_3_4b_it-lora-r32-hau-eng
35
+
36
+ [![Model on HF](https://huggingface.co/datasets/huggingface/badges/raw/main/model-on-hf-sm.svg)](https://huggingface.co/AfriScience-MT/gemma_3_4b_it-lora-r32-hau-eng)
37
+
38
+ This is a **LoRA adapter** for the AfriScience-MT project, enabling efficient scientific machine translation for African languages.
39
+
40
+ ## Adapter Description
41
+
42
+ | Property | Value |
43
+ |----------|-------|
44
+ | **Base Model** | [google/gemma-3-4b-it](https://huggingface.co/google/gemma-3-4b-it) |
45
+ | **Translation Direction** | Hausa → English |
46
+ | **LoRA Rank (r)** | 32 |
47
+ | **LoRA Alpha** | 64 |
48
+ | **Training Method** | QLoRA (4-bit quantization) |
49
+ | **Domain** | Scientific/Academic texts |
50
+
51
+ ### Why LoRA?
52
+
53
+ LoRA (Low-Rank Adaptation) enables efficient fine-tuning by training only a small number of additional parameters. This adapter adds only **~16.0M parameters** to the base model while achieving strong translation performance.
54
+
55
+ ## Evaluation Results
56
+
57
+ Performance on the AfriScience-MT test set:
58
+
59
+ | Split | BLEU | chrF | SSA-COMET |
60
+ |-------|------|------|-----------|
61
+ | Validation | 43.28 | 62.36 | 66.33 |
62
+ | **Test** | **40.46** | **59.83** | **65.03** |
63
+
64
+ **Metrics explanation:**
65
+ - **BLEU**: Measures n-gram overlap with reference translations (0-100, higher is better)
66
+ - **chrF**: Character-level F-score, robust for morphologically rich languages (0-100, higher is better)
67
+ - **SSA-COMET**: Neural metric trained for Sub-Saharan African languages, shown as percentage (0-100, higher is better) ([McGill-NLP/ssa-comet-stl](https://huggingface.co/McGill-NLP/ssa-comet-stl))
68
+
69
+ ## Usage
70
+
71
+ ### Quick Start
72
+
73
+ ```python
74
+ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
75
+ from peft import PeftModel
76
+ import torch
77
+
78
+ # Configure 4-bit quantization (recommended for memory efficiency)
79
+ bnb_config = BitsAndBytesConfig(
80
+ load_in_4bit=True,
81
+ bnb_4bit_compute_dtype=torch.bfloat16,
82
+ bnb_4bit_quant_type="nf4",
83
+ bnb_4bit_use_double_quant=True,
84
+ )
85
+
86
+ # Load base model
87
+ base_model = AutoModelForCausalLM.from_pretrained(
88
+ "google/gemma-3-4b-it",
89
+ quantization_config=bnb_config,
90
+ device_map="auto",
91
+ torch_dtype=torch.bfloat16,
92
+ )
93
+ tokenizer = AutoTokenizer.from_pretrained("google/gemma-3-4b-it")
94
+
95
+ # Load LoRA adapter
96
+ adapter_name = "AfriScience-MT/gemma_3_4b_it-lora-r32-hau-eng"
97
+ model = PeftModel.from_pretrained(base_model, adapter_name)
98
+ model.eval()
99
+
100
+ # Prepare translation prompt
101
+ source_text = "Climate change significantly impacts agricultural productivity in sub-Saharan Africa."
102
+ instruction = "Translate the following Hausa scientific text to English."
103
+
104
+ # Format for Gemma chat template
105
+ messages = [{"role": "user", "content": f"{instruction}\n\n{source_text}"}]
106
+ prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
107
+
108
+ # Generate translation
109
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
110
+ with torch.no_grad():
111
+ outputs = model.generate(
112
+ **inputs,
113
+ max_new_tokens=256,
114
+ num_beams=5,
115
+ early_stopping=True,
116
+ pad_token_id=tokenizer.pad_token_id,
117
+ )
118
+
119
+ # Decode only the generated part
120
+ generated = outputs[0][inputs["input_ids"].shape[1]:]
121
+ translation = tokenizer.decode(generated, skip_special_tokens=True)
122
+ print(translation)
123
+ ```
124
+
125
+ ### Without Quantization (Full Precision)
126
+
127
+ ```python
128
+ # For GPUs with sufficient memory (>24GB for larger models)
129
+ base_model = AutoModelForCausalLM.from_pretrained(
130
+ "google/gemma-3-4b-it",
131
+ device_map="auto",
132
+ torch_dtype=torch.bfloat16,
133
+ )
134
+ model = PeftModel.from_pretrained(base_model, "AfriScience-MT/gemma_3_4b_it-lora-r32-hau-eng")
135
+ ```
136
+
137
+ ## Training Details
138
+
139
+ ### Hyperparameters
140
+
141
+ | Parameter | Value |
142
+ |-----------|-------|
143
+ | LoRA Rank (r) | 32 |
144
+ | LoRA Alpha | 64 |
145
+ | LoRA Dropout | 0.05 |
146
+ | Target Modules | q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj |
147
+ | Epochs | 3 |
148
+ | Batch Size | 2 |
149
+ | Learning Rate | 2e-04 |
150
+ | Max Sequence Length | 512 |
151
+ | Gradient Accumulation | 4 |
152
+
153
+ ### Hardware Requirements
154
+
155
+ | Configuration | VRAM Required |
156
+ |---------------|---------------|
157
+ | 4-bit (QLoRA) | ~8-12 GB |
158
+ | 8-bit | ~16-20 GB |
159
+ | Full precision | ~24-40 GB |
160
+
161
+
162
+ ## Reproducibility
163
+
164
+ To reproduce this adapter:
165
+
166
+ ```bash
167
+ # Clone the AfriScience-MT repository
168
+ git clone https://github.com/afriscience-mt/afriscience-mt.git
169
+ cd afriscience-mt
170
+
171
+ # Install dependencies
172
+ pip install -r requirements.txt
173
+
174
+ # Run LoRA training
175
+ python -m afriscience_mt.scripts.run_lora_training \
176
+ --data_dir ./data \
177
+ --source_lang hau \
178
+ --target_lang eng \
179
+ --model_name google/gemma-3-4b-it \
180
+ --model_type gemma \
181
+ --lora_rank 32 \
182
+ --output_dir ./output \
183
+ --num_epochs 3 \
184
+ --batch_size 4 \
185
+ --load_in_4bit
186
+ ```
187
+
188
+ ## Limitations
189
+
190
+ - **Domain Specificity**: Optimized for scientific/academic texts; may underperform on casual or colloquial language.
191
+ - **Language Direction**: Only supports Hausa → English translation.
192
+ - **Base Model Required**: Must be used with the [google/gemma-3-4b-it](https://huggingface.co/google/gemma-3-4b-it) base model.
193
+ - **Context Length**: Maximum context is model-dependent; longer texts should be chunked.
194
+
195
+ ## Citation
196
+
197
+ If you use this adapter, please cite the AfriScience-MT project:
198
+
199
+ ```bibtex
200
+ @inproceedings{afriscience-mt-2025,
201
+ title={AfriScience-MT: Machine Translation for African Scientific Literature},
202
+ author={AfriScience-MT Team},
203
+ year={2025},
204
+ url={https://github.com/afriscience-mt/afriscience-mt}
205
+ }
206
+ ```
207
+
208
+ ## License
209
+
210
+ This adapter is released under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0).
211
+
212
+ ## Acknowledgments
213
+
214
+ - Base model: [google/gemma-3-4b-it](https://huggingface.co/google/gemma-3-4b-it)
215
+ - LoRA implementation: [PEFT](https://github.com/huggingface/peft)
216
+ - Evaluation: [SSA-COMET](https://huggingface.co/McGill-NLP/ssa-comet-stl) for African language assessment
adapter_config.json ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "alora_invocation_tokens": null,
3
+ "alpha_pattern": {},
4
+ "arrow_config": null,
5
+ "auto_mapping": null,
6
+ "base_model_name_or_path": "google/gemma-3-4b-it",
7
+ "bias": "none",
8
+ "corda_config": null,
9
+ "ensure_weight_tying": false,
10
+ "eva_config": null,
11
+ "exclude_modules": null,
12
+ "fan_in_fan_out": false,
13
+ "inference_mode": true,
14
+ "init_lora_weights": true,
15
+ "layer_replication": null,
16
+ "layers_pattern": null,
17
+ "layers_to_transform": null,
18
+ "loftq_config": {},
19
+ "lora_alpha": 32,
20
+ "lora_bias": false,
21
+ "lora_dropout": 0.05,
22
+ "megatron_config": null,
23
+ "megatron_core": "megatron.core",
24
+ "modules_to_save": null,
25
+ "peft_type": "LORA",
26
+ "peft_version": "0.18.1",
27
+ "qalora_group_size": 16,
28
+ "r": 32,
29
+ "rank_pattern": {},
30
+ "revision": null,
31
+ "target_modules": [
32
+ "up_proj",
33
+ "q_proj",
34
+ "v_proj",
35
+ "down_proj",
36
+ "k_proj",
37
+ "gate_proj",
38
+ "o_proj"
39
+ ],
40
+ "target_parameters": null,
41
+ "task_type": "CAUSAL_LM",
42
+ "trainable_token_indices": null,
43
+ "use_dora": false,
44
+ "use_qalora": false,
45
+ "use_rslora": false
46
+ }
adapter_model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bab93192a40dabf2f46bb79b43f249a6075f78d42d05153f9f07ca710e100b65
3
+ size 262406656
added_tokens.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ {
2
+ "<image_soft_token>": 262144
3
+ }
chat_template.jinja ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {{ bos_token }}
2
+ {%- if messages[0]['role'] == 'system' -%}
3
+ {%- if messages[0]['content'] is string -%}
4
+ {%- set first_user_prefix = messages[0]['content'] + '
5
+
6
+ ' -%}
7
+ {%- else -%}
8
+ {%- set first_user_prefix = messages[0]['content'][0]['text'] + '
9
+
10
+ ' -%}
11
+ {%- endif -%}
12
+ {%- set loop_messages = messages[1:] -%}
13
+ {%- else -%}
14
+ {%- set first_user_prefix = "" -%}
15
+ {%- set loop_messages = messages -%}
16
+ {%- endif -%}
17
+ {%- for message in loop_messages -%}
18
+ {%- if (message['role'] == 'user') != (loop.index0 % 2 == 0) -%}
19
+ {{ raise_exception("Conversation roles must alternate user/assistant/user/assistant/...") }}
20
+ {%- endif -%}
21
+ {%- if (message['role'] == 'assistant') -%}
22
+ {%- set role = "model" -%}
23
+ {%- else -%}
24
+ {%- set role = message['role'] -%}
25
+ {%- endif -%}
26
+ {{ '<start_of_turn>' + role + '
27
+ ' + (first_user_prefix if loop.first else "") }}
28
+ {%- if message['content'] is string -%}
29
+ {{ message['content'] | trim }}
30
+ {%- elif message['content'] is iterable -%}
31
+ {%- for item in message['content'] -%}
32
+ {%- if item['type'] == 'image' -%}
33
+ {{ '<start_of_image>' }}
34
+ {%- elif item['type'] == 'text' -%}
35
+ {{ item['text'] | trim }}
36
+ {%- endif -%}
37
+ {%- endfor -%}
38
+ {%- else -%}
39
+ {{ raise_exception("Invalid content type") }}
40
+ {%- endif -%}
41
+ {{ '<end_of_turn>
42
+ ' }}
43
+ {%- endfor -%}
44
+ {%- if add_generation_prompt -%}
45
+ {{'<start_of_turn>model
46
+ '}}
47
+ {%- endif -%}
special_tokens_map.json ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "boi_token": "<start_of_image>",
3
+ "bos_token": {
4
+ "content": "<bos>",
5
+ "lstrip": false,
6
+ "normalized": false,
7
+ "rstrip": false,
8
+ "single_word": false
9
+ },
10
+ "eoi_token": "<end_of_image>",
11
+ "eos_token": {
12
+ "content": "<eos>",
13
+ "lstrip": false,
14
+ "normalized": false,
15
+ "rstrip": false,
16
+ "single_word": false
17
+ },
18
+ "image_token": "<image_soft_token>",
19
+ "pad_token": {
20
+ "content": "<pad>",
21
+ "lstrip": false,
22
+ "normalized": false,
23
+ "rstrip": false,
24
+ "single_word": false
25
+ },
26
+ "unk_token": {
27
+ "content": "<unk>",
28
+ "lstrip": false,
29
+ "normalized": false,
30
+ "rstrip": false,
31
+ "single_word": false
32
+ }
33
+ }
tokenizer.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4667f2089529e8e7657cfb6d1c19910ae71ff5f28aa7ab2ff2763330affad795
3
+ size 33384568
tokenizer.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1299c11d7cf632ef3b4e11937501358ada021bbdf7c47638d13c0ee982f2e79c
3
+ size 4689074
tokenizer_config.json ADDED
The diff for this file is too large to render. See raw diff