File size: 4,912 Bytes
e7f82a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
---
library_name: peft
base_model: afriquellama_8b
language:
- ha
- en
tags:
- translation
- african-languages
- scientific-translation
- afriscience-mt
- lora
- peft
- llama
license: apache-2.0
pipeline_tag: translation
---

# afriquellama_8b-lora-r4-hau-eng

[![Model on HF](https://huggingface.co/datasets/huggingface/badges/raw/main/model-on-hf-sm.svg)](https://huggingface.co/AfriScience-MT/afriquellama_8b-lora-r4-hau-eng)

This is a **LoRA adapter** for the AfriScience-MT project, enabling efficient scientific machine translation for African languages.

## Adapter Description

| Property | Value |
|----------|-------|
| **Base Model** | [afriquellama_8b](https://huggingface.co/afriquellama_8b) |
| **Translation Direction** | Hausa → English |
| **LoRA Rank (r)** | 4 |
| **LoRA Alpha** | 8 |
| **Training Method** | QLoRA (4-bit quantization) |
| **Domain** | Scientific/Academic texts |

### Why LoRA?

LoRA (Low-Rank Adaptation) enables efficient fine-tuning by training only a small number of additional parameters. This adapter adds only **~2.0M parameters** to the base model while achieving strong translation performance.

## Usage

### Quick Start

```python
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from peft import PeftModel
import torch

# Configure 4-bit quantization (recommended for memory efficiency)
bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_compute_dtype=torch.bfloat16,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_use_double_quant=True,
)

# Load base model
base_model = AutoModelForCausalLM.from_pretrained(
    "afriquellama_8b",
    quantization_config=bnb_config,
    device_map="auto",
    torch_dtype=torch.bfloat16,
)
tokenizer = AutoTokenizer.from_pretrained("afriquellama_8b")

# Load LoRA adapter
adapter_name = "AfriScience-MT/afriquellama_8b-lora-r4-hau-eng"
model = PeftModel.from_pretrained(base_model, adapter_name)
model.eval()

# Prepare translation prompt
source_text = "Climate change significantly impacts agricultural productivity in sub-Saharan Africa."
instruction = "Translate the following Hausa scientific text to English."

# Format prompt
prompt = f"""### Instruction:
{instruction}

### Input:
{source_text}

### Response:
"""

# Generate translation
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
with torch.no_grad():
    outputs = model.generate(
        **inputs,
        max_new_tokens=256,
        num_beams=5,
        early_stopping=True,
        pad_token_id=tokenizer.pad_token_id,
    )

# Decode only the generated part
generated = outputs[0][inputs["input_ids"].shape[1]:]
translation = tokenizer.decode(generated, skip_special_tokens=True)
print(translation)
```

### Without Quantization (Full Precision)

```python
# For GPUs with sufficient memory (>24GB for larger models)
base_model = AutoModelForCausalLM.from_pretrained(
    "afriquellama_8b",
    device_map="auto",
    torch_dtype=torch.bfloat16,
)
model = PeftModel.from_pretrained(base_model, "AfriScience-MT/afriquellama_8b-lora-r4-hau-eng")
```

### Hardware Requirements

| Configuration | VRAM Required |
|---------------|---------------|
| 4-bit (QLoRA) | ~8-12 GB |
| 8-bit | ~16-20 GB |
| Full precision | ~24-40 GB |


## Reproducibility

To reproduce this adapter:

```bash
# Clone the AfriScience-MT repository
git clone https://github.com/afriscience-mt/afriscience-mt.git
cd afriscience-mt

# Install dependencies
pip install -r requirements.txt

# Run LoRA training
python -m afriscience_mt.scripts.run_lora_training \
    --data_dir ./data \
    --source_lang hau \
    --target_lang eng \
    --model_name afriquellama_8b \
    --model_type llama \
    --lora_rank 4 \
    --output_dir ./output \
    --num_epochs 3 \
    --batch_size 4 \
    --load_in_4bit
```

## Limitations

- **Domain Specificity**: Optimized for scientific/academic texts; may underperform on casual or colloquial language.
- **Language Direction**: Only supports Hausa → English translation.
- **Base Model Required**: Must be used with the [afriquellama_8b](https://huggingface.co/afriquellama_8b) base model.
- **Context Length**: Maximum context is model-dependent; longer texts should be chunked.

## Citation

If you use this adapter, please cite the AfriScience-MT project:

```bibtex
@inproceedings{afriscience-mt-2025,
  title={AfriScience-MT: Machine Translation for African Scientific Literature},
  author={AfriScience-MT Team},
  year={2025},
  url={https://github.com/afriscience-mt/afriscience-mt}
}
```

## License

This adapter is released under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0).

## Acknowledgments

- Base model: [afriquellama_8b](https://huggingface.co/afriquellama_8b)
- LoRA implementation: [PEFT](https://github.com/huggingface/peft)
- Evaluation: [SSA-COMET](https://huggingface.co/McGill-NLP/ssa-comet-stl) for African language assessment