File size: 9,578 Bytes
6b9c98a
 
 
ef6b490
c04f880
ef6b490
 
 
 
c04f880
ef6b490
 
 
 
c04f880
6b9c98a
 
ef6b490
6b9c98a
ef6b490
6b9c98a
ef6b490
6b9c98a
ef6b490
6b9c98a
ef6b490
6b9c98a
ef6b490
6b9c98a
ef6b490
6b9c98a
ef6b490
6b9c98a
ef6b490
6b9c98a
ef6b490
6b9c98a
ef6b490
6b9c98a
ef6b490
6fa12ef
ef6b490
6b9c98a
ef6b490
6fa12ef
ef6b490
6b9c98a
ef6b490
6b9c98a
ef6b490
6b9c98a
ef6b490
 
6b9c98a
ef6b490
 
6b9c98a
ef6b490
6b151fb
ef6b490
 
6b9c98a
ef6b490
6b9c98a
ef6b490
6fa12ef
ef6b490
6fa12ef
ef6b490
6fa12ef
ef6b490
6fa12ef
ef6b490
6fa12ef
ef6b490
6b9c98a
ef6b490
 
6fa12ef
ef6b490
6fa12ef
ef6b490
6fa12ef
ef6b490
6fa12ef
ef6b490
6fa12ef
ef6b490
6fa12ef
ef6b490
6b9c98a
6fa12ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b22f8b9
6fa12ef
b22f8b9
 
6fa12ef
b22f8b9
6fa12ef
b22f8b9
6fa12ef
b22f8b9
 
6fa12ef
b22f8b9
6fa12ef
b22f8b9
6fa12ef
 
b22f8b9
6fa12ef
b22f8b9
6fa12ef
 
b22f8b9
6fa12ef
b22f8b9
6fa12ef
b22f8b9
 
6b9c98a
b22f8b9
 
 
 
 
 
 
 
 
 
 
6b9c98a
b22f8b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c04f880
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
---
base_model: Qwen/QwQ-32B-Preview
library_name: peft
license: apache-2.0
license_link: https://huggingface.co/Qwen/QwQ-32B-Preview/blob/main/LICENSE
datasets:
- Omartificial-Intelligence-Space/Arabic_Reasoning_Dataset
language:
- ar
- en
pipeline_tag: text-generation
tags:
- transformers
- unsloth
- chat
---

# Arabic QwQ 32B Preview:

> This is the **Arabic** version of [QwQ 32B Preview](https://huggingface.co/Qwen/QwQ-32B-Preview), specifically fine-tuned for **Arabic reasoning tasks**, with a primary focus on **math**. It is trained on a dataset featuring questions, step-by-step solutions, and detailed chains of thought, guiding users to arrive at the correct answers effectively.

**Arabic-QwQ-32B-Preview** is fine-tuned for Arabic reasoning tasks using the [Unsloth](https://huggingface.co/unsloth) and the newly intorduced [Arabic_Reasoning_Dataset](https://huggingface.co/datasets/Omartificial-Intelligence-Space/Arabic_Reasoning_Dataset). 

## Overview on the training:

We fine-tuned a pre-trained language model to improve its reasoning capabilities on Arabic datasets. The model leverages advanced techniques like LoRA (Low-Rank Adaptation) for parameter-efficient fine-tuning.

### Key Features

⏺ 4-bit Quantization: Reduces memory usage and speeds up training.

⏺ Gradient Checkpointing: Saves VRAM and supports long contexts.

⏺ Early Stopping: Prevents overfitting during training.

⏺ Evaluation Strategy: Regular checkpoints and validation to monitor performance.

### Dataset

🔹 Training Source: [Omartificial-Intelligence-Space/Arabic_Reasoning_Dataset](https://huggingface.co/datasets/Omartificial-Intelligence-Space/Arabic_Reasoning_Dataset) with 10,000 samples.

🔹 Description: Contains instruction-answer pairs for reasoning tasks in Arabic.

🔹 Validation Source: [MohammedNasser/Arabic_Reasoning_Instruct_QA](https://huggingface.co/datasets/MohammedNasser/ARabic_Reasoning_QA/viewer/default/test)

🔹 Description: Contains reasoning challenges to validate model performance.

### Preprocessing

Each dataset is formatted using the alpaca_prompt template:

```bash
Below is an instruction that describes a task. Write a response that appropriately completes the request.

### Instruction:
{instruction}

### Response:
{output}
```
### Fine-Tuning Configuration

#### Model

▪️ Base Model: Qwen/QwQ-32B-Preview

▪️ Optimization: LoRA with the following parameters:

▪️Rank r: 16

▪️ LoRA alpha: 16

▪️ Dropout: 0

▪️ Gradient checkpointing: "unsloth" for long contexts.

#### Training Arguments
▪️ Batch Size: 8 (per device)

▪️ Gradient Accumulation Steps: 2

▪️ Epochs: 3

▪️ Learning Rate: 2e-4

▪️ Optimizer: adamw_8bit

▪️ Scheduler: Linear

▪️ FP16/BF16: Enabled based on hardware support.

## Usage

```bash
pip install unsloth
```

```bash
from unsloth import FastLanguageModel
import torch
max_seq_length = 2048 # Choose any! We auto support RoPE Scaling internally!
dtype = None # None for auto detection. Float16 for Tesla T4, V100, Bfloat16 for Ampere+
load_in_4bit = True # Use 4bit quantization to reduce memory usage. Can be False.

model, tokenizer = FastLanguageModel.from_pretrained(
    model_name = "Omartificial-Intelligence-Space/Arabic-QWQ-32B-Preview",
    max_seq_length = max_seq_length,
    dtype = dtype,
    load_in_4bit = load_in_4bit,
    # token = "hf_...", # use one if using gated models like meta-llama/Llama-2-7b-hf
)

prompt = """Below is an instruction that describes a task. Write a response that appropriately completes the request.

### Instruction:
{}

### Response:
{}"""

# alpaca_prompt = Copied from above
FastLanguageModel.for_inference(model) # Enable native 2x faster inference
inputs = tokenizer(
[
    prompt.format(
        "YOUR INSTRUCTION", # instruction
        "", # output - leave this blank for generation!
    )
], return_tensors = "pt").to("cuda")

outputs = model.generate(**inputs, max_new_tokens = 256, use_cache = True)
tokenizer.batch_decode(outputs)
```

## Results and Comparsion 

> [!IMPORTANT]
> The Qwen/QwQ-32B model, while inherently multilingual and supportive of Arabic, exhibits inconsistent performance in Arabic reasoning tasks compared to its stronger default capabilities in English.
> Our observations indicate that the model often requires explicit, structured prompting to generate coherent Arabic responses, and even then, its reasoning abilities in Arabic can be limited.
> To address this, we have adapted the model by fine-tuning it with targeted Arabic reasoning datasets and task-specific instructions, enhancing its understanding and alignment with Arabic language tasks.
> This adaptation demonstrates the need for language-specific adjustments to optimize multilingual models for underrepresented languages like Arabic.

The following results of the **Arabic-QwQ** and **QwQ-Preivew** models were analyzed to better understand the impact of fine-tuning on the model's performance, particularly in enhancing its capabilities for Arabic language tasks.

1. An example illustrating how base models generate Chinese responses when provided with an Arabic question:

![image/png](https://cdn-uploads.huggingface.co/production/uploads/628f7a71dd993507cfcbe587/WheOO0ZgoCkwwKQ55kLsS.png)

> [!NOTE]
> The fine-tuned model effectively resolves the issue of unintended Chinese interference in the responses, delivering clear and accurate answers in Arabic.

2. An example demonstrating how base models sometimes respond in English unless explicitly instructed to answer in Arabic. In contrast, the fine-tuned model seamlessly responds in Arabic without requiring additional instructions, simply by providing the question in Arabic.

![image/png](https://cdn-uploads.huggingface.co/production/uploads/628f7a71dd993507cfcbe587/GFvctZu9rN7SHgv8BQUjR.png)

> [!NOTE]
> Although the base model provides the correct answer, it is often in English, making it challenging for Arabic users to understand unless they are proficient in English.

3. At times, the base models provide additional context, resulting in unnecessarily lengthy answers. The fine-tuned model addresses this issue by focusing on delivering concise, straightforward solutions without extra context.

![image/png](https://cdn-uploads.huggingface.co/production/uploads/628f7a71dd993507cfcbe587/IX4oUhVZyAo4aiJQ9WQa1.png)


4. There are instances where both the base and fine-tuned models perform well in answering the query, demonstrating their capability to comprehend and provide accurate responses. However, the fine-tuned model consistently outperforms by aligning more closely with the requirements of Arabic users. 

![image/png](https://cdn-uploads.huggingface.co/production/uploads/628f7a71dd993507cfcbe587/apKq-ip2XestSEQacunof.png)


## How to Use

To utilize the Arabic-QwQ model effectively:

1. **Use Unsloth for Faster Inference**  
   We recommend using [Unsloth](https://github.com/unslothai/unsloth) to load and perform inference with the model. This method is optimized for speed and offers better performance compared to traditional loading methods.

2. **Incorporate Prompt Templates for Structured Instructions**  
   For more specific instructions or complex tasks, use a prompt template to guide the model's responses. For example, structure your input like:  
   ```plaintext
        prompt = """Below is an instruction that describes a task. Write a response that appropriately completes the request.
        
        ### Instruction:
        {}
        
        ### Response:
        {}"""
   ```

## Acknowledgments

We would like to express our gratitude to Prince Sultan University for their support in the development and fine-tuning of this model. Their contributions were invaluable in making this work possible.

## Citation

If you use this model in your research or application, please cite it as follows:

```plaintext
@misc{Arabic_QWQ,
  author = {Omer Nacar},
  title = {Arabic-QwQ: Fine-tuned QwQ LLM for Arabic Reasoning and Understanding},
  year = {2024},
  url = {https://huggingface.co/Omartificial-Intelligence-Space/Arabic-QWQ-32B-Preview},
  institution = {Prince Sultan University},
  note = {Fine-tuned version of the QwQ-32B model for Arabic-specific tasks.}
}
```

This work is built upon the great work done by **Qwen**:

```plaintext
@misc{qwq-32b-preview,
    title = {QwQ: Reflect Deeply on the Boundaries of the Unknown},
    url = {https://qwenlm.github.io/blog/qwq-32b-preview/},
    author = {Qwen Team},
    month = {November},
    year = {2024}
}

@article{qwen2,
      title={Qwen2 Technical Report}, 
      author={An Yang and Baosong Yang and Binyuan Hui and Bo Zheng and Bowen Yu and Chang Zhou and Chengpeng Li and Chengyuan Li and Dayiheng Liu and Fei Huang and Guanting Dong and Haoran Wei and Huan Lin and Jialong Tang and Jialin Wang and Jian Yang and Jianhong Tu and Jianwei Zhang and Jianxin Ma and Jin Xu and Jingren Zhou and Jinze Bai and Jinzheng He and Junyang Lin and Kai Dang and Keming Lu and Keqin Chen and Kexin Yang and Mei Li and Mingfeng Xue and Na Ni and Pei Zhang and Peng Wang and Ru Peng and Rui Men and Ruize Gao and Runji Lin and Shijie Wang and Shuai Bai and Sinan Tan and Tianhang Zhu and Tianhao Li and Tianyu Liu and Wenbin Ge and Xiaodong Deng and Xiaohuan Zhou and Xingzhang Ren and Xinyu Zhang and Xipin Wei and Xuancheng Ren and Yang Fan and Yang Yao and Yichang Zhang and Yu Wan and Yunfei Chu and Yuqiong Liu and Zeyu Cui and Zhenru Zhang and Zhihao Fan},
      journal={arXiv preprint arXiv:2407.10671},
      year={2024}
}
```