Text Generation
PEFT
Galician
File size: 5,401 Bytes
e952cc2
c589e1a
 
285dc92
 
 
 
 
 
cbb17e4
d3c1e8f
 
 
 
 
b49ef6f
795f1cc
 
 
 
 
 
 
 
 
77f594a
e952cc2
c589e1a
285dc92
c589e1a
39c3a9a
c589e1a
39c3a9a
c589e1a
b1baa71
c589e1a
39c3a9a
c589e1a
 
 
39c3a9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8f4b1d
39c3a9a
a8f4b1d
 
39c3a9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c589e1a
 
 
 
 
 
 
 
 
 
 
 
 
39c3a9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c589e1a
 
39c3a9a
 
 
 
 
 
 
 
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
---
library_name: peft
base_model: huggyllama/llama-7b
license: other
datasets:
- irlab-udc/alpaca_data_galician
language:
- gl
pipeline_tag: text-generation
widget:
- text: >-
    Abaixo está unha instrución que describe unha tarefa. Escribe unha resposta que responda adecuadamente a entrada.
    ### Instrución:
    Cal é a fórmula química da auga?
    ### Resposta:
  example_title: Basic instruction
  
- text: >-
    Abaixo está unha instrución que describe unha tarefa, xunto cunha entrada que proporciona máis contexto. 
    Escribe unha resposta que responda adecuadamente a entrada.
    ### Instrución:
    Convence ao lector por que é importante un determinado tema.
    ### Entrada:
    Por que é esencial priorizar o sono?
    ### Resposta:
  example_title: Additional input
---

# Cabuxa-7B

Cabuxa is a [LLaMA-7B](https://huggingface.co/huggyllama/llama-7b) model for Galician that can answer instructions in the [Alpaca format](https://github.com/tloen/alpaca-lora/blob/main/templates/alpaca.json). 

It was fed with the 80% of the [irlab-udc/alpaca_data_galician](https://huggingface.co/datasets/irlab-udc/alpaca_data_galician) dataset, as we are keeping the remaining 20% for future evaluation and research. 

This work broadens the Portuguese effort from [22h/cabrita-lora-v0-1](https://huggingface.co/22h/cabrita-lora-v0-1) to Galician. Our working notes are available [here](https://arxiv.org/abs/2311.03812).

## How to Get Started with Cabuxa-7B

Use the code below to get started with the model.

```python
from peft import PeftModel
from transformers import AutoModelForCausalLM, LlamaTokenizer, GenerationConfig

config = PeftConfig.from_pretrained("irlab-udc/cabuxa-7b")
model = AutoModelForCausalLM.from_pretrained("huggyllama/llama-7b", device_map="auto")
model = PeftModel.from_pretrained(model, "irlab-udc/cabuxa-7b")
tokenizer = LlamaTokenizer.from_pretrained("huggyllama/llama-7b")

# This function builds the prompt in Alpaca format
def generate_prompt(instruction, input=None):
    if input:
        return f"""Abaixo está unha instrución que describe unha tarefa, xunto cunha entrada que proporciona máis contexto. 
               Escribe unha resposta que responda adecuadamente a entrada.
               ### Instrución:
               {instruction}
               ### Entrada:
               {input}
               ### Resposta:"""
    else:
        return f"""Abaixo está unha instrución que describe unha tarefa.
               Escribe unha resposta que responda adecuadamente a entrada.
               ### Instrución:
               {instruction}
               ### Resposta:"""


def evaluate(instruction, input=None):
    prompt = generate_prompt(instruction, input)
    inputs = tokenizer(prompt, return_tensors="pt")
    input_ids = inputs["input_ids"].cuda()
    generation_output = model.generate(
        input_ids=input_ids,
        generation_config=GenerationConfig(do_sample=True),
        return_dict_in_generate=True,
        output_scores=True,
        max_new_tokens=256,
    )
    for s in generation_output.sequences:
        output = tokenizer.decode(s)
        print("Resposta:", output.split("### Resposta:")[1].strip())

evaluate("Cal é a fórmula química da auga?")
evaluate(
    "Convence ao lector por que é importante un determinado tema.",
    "Por que é esencial priorizar o sono?",
)
```

```
Resposta: A fórmula química da auga é H₂O.

Resposta: O sono é esencial para todos os humanos, pero tamén é unha ferramenta importante para lograr obxectivos, aumentar a productividade, maximizar os beneficios do soño e mantenerse saudable.
```

## Training

#### Configurations and Hyperparameters

The following `LoraConfig` config was used during training:
- r: 8
- lora_alpha: 16
- target_modules: ["q_proj", "v_proj"]
- lora_dropout: 0.05
- bias: "none"
- task_type: "CAUSAL_LM"

The following `TrainingArguments` config was used during training:
- per_device_train_batch_size: 64
- gradient_accumulation_steps: 32
- warmup_steps: 100
- num_train_epochs: 20
- learning_rate: 3e-4
- fp16=True

The following `bitsandbytes` quantization config was used during training:
- quant_method: bitsandbytes
- load_in_8bit: True
- load_in_4bit: False
- llm_int8_threshold: 6.0
- llm_int8_skip_modules: None
- llm_int8_enable_fp32_cpu_offload: False
- llm_int8_has_fp16_weight: False
- bnb_4bit_quant_type: fp4
- bnb_4bit_use_double_quant: False
- bnb_4bit_compute_dtype: float32

#### Loss

| Epoch | Loss   |
|:-----:|:------:|
| 0.98  | 2.6109 |
| 1.97  | 2.0596 |
| 2.95  | 1.5092 |
| 3.93  | 1.379  |
| 4.92  | 1.2849 |
| 5.9   | 1.208  |
| 6.88  | 1.1508 |
| 7.86  | 1.117  |
| 8.85  | 1.0873 |
| 9.83  | 1.0666 |
| 10.81 | 1.0513 |
| 11.8  | 1.0365 |
| 12.78 | 1.0253 |
| 13.76 | 1.0169 |
| 14.75 | 1.0118 |
| 15.73 | 1.0035 |
| 16.71 | 0.9968 |
| 17.7  | 0.9983 |
| 18.68 | 0.9924 |
| 19.66 | 0.9908 |


#### Framework versions

- PyTorch 2.1.0
- PEFT 0.6.0.dev0
- 🤗 Transformers 4.34.0
- 🤗 Datasets 2.14.5
- 🤗 Tokenizers 0.14.0


## Environmental Impact

Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).

- **Hardware Type:** NVIDIA RTX A6000.
- **Hours used:** 72.
- **Cloud Provider:** Private infrastructure.
- **Carbon Emitted:** 9.33 Kg. CO2 eq.