File size: 11,502 Bytes
08c2792
 
71ff162
 
 
 
 
 
08c2792
 
71ff162
 
 
 
 
 
 
 
 
 
 
 
f3e8b74
71ff162
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
225
---
library_name: transformers
license: apache-2.0
tags:
- finnish
- llama
inference: true
pipeline_tag: text-generation
---

# Llama-7b-instruct-v0.2 for Finnish


- This is 0.2 version release of our Instruct finetuned model from https://huggingface.co/Finnish-NLP/llama-7b-finnish 
- Model was trained for 3 epochs using 21946 samples and for this release we chose checkpoint at 8000 steps. 
- Future DPO/SFT+DPO variants are in the pipeline. Also we are investigating and testing different merging techiques

For finetuning we try to select well known and widely used dataset and then filter/translate those with multiple methods:
For this version we used a mix 21946 samples in total from the the following datasets:
 - LIMA from https://github.com/TurkuNLP/finnish-instructions 
 - Dolly from https://github.com/TurkuNLP/finnish-instructions 
 - OASST from https://github.com/TurkuNLP/finnish-instructions 
 - Ultrafeedback https://huggingface.co/datasets/HuggingFaceH4/ultrafeedback_binarized/viewer/default/train_sft translated with deepl
 - facebook/belebele Finnish subset
 - google/boolq translated with deepl
 - LDJnr/Capybara translated with deepl
 - allenai/ai2_arc translated with deepl


### How to use

Here is an example of using this model with Unsloth with some generation arguments you can modify:

```python
import torch
from unsloth import FastLlamaModel

max_seq_length = 2048
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.


use_unsloth = True
# use_transformers = True

# LOADING MODEL USIINIG TRANSFORMERS assumes at least 16GB of memory. Tested with this configuration
# If you have less memory use load_in_4bit or load_in_8_bit as needed
if use_transformers:
  major_version, minor_version = torch.cuda.get_device_capability()
  model = AutoModelForCausalLM.from_pretrained("Finnish-NLP/llama-7b-finnish-instruct-v0.2", device_map='cuda:0', torch_dtype = torch.bfloat16 if major_version >=8 else torch.float16)
  tokenizer = AutoTokenizer.from_pretrained("Finnish-NLP/llama-7b-finnish-instruct-v0.2")

# USING UNSLOTH, tested with load_in_4bit
if use_unsloth:
  model, tokenizer = FastLlamaModel.from_pretrained(
      model_name = "Finnish-NLP/llama-7b-finnish-instruct-v0.2"
      max_seq_length = max_seq_length,
      dtype = dtype,
      load_in_4bit = load_in_4bit
  )

alpaca_prompt = """<|alku|> Olet tekoälyavustaja. Seuraavaksi saat kysymyksen tai tehtävän. Kirjoita vastaus parhaasi mukaan siten että se täyttää kysymyksen tai tehtävän vaatimukset.
<|ihminen|> Kysymys/Tehtävä:
{}
<|avustaja|> Vastauksesi:
"""

sample_questions = ["Ketkä ovat Aku Ankan luona asuvat kolme ankanpoikaa?",\
"Mikä on Suomen korkein tunturi?",\
"Suomi soti Neuvostoliittoa vastaan talvisodan 1939-1940. Kuinka monta päivää sota kesti?",\
"Luettele viisi yleistä Suomessa yleisesti käytettyä pojan nimeä. Nimet:",\
"Luettele lyhyt, maksimissaan 50 sanan mittainen runo Suomesta. Runo:",\
]

from transformers import GenerationConfig

generation_config = GenerationConfig(
    pad_token_id=tokenizer.eos_token_id,
    eos_token_id=tokenizer.convert_tokens_to_ids("<|loppu|>"),
)


for sample_question in sample_questions:

  model.eval()
  
  inputs = tokenizer(
[
    alpaca_prompt.format(
        sample_question, # instruction
    )
]*1, return_tensors = "pt").to("cuda")
  
  with torch.no_grad():
      generated_ids = model.generate(
      input_ids=inputs["input_ids"], 
      attention_mask=inputs["attention_mask"], 
      generation_config=generation_config, **{
        "temperature": 0.1,
        "penalty_alpha": 0.6,
        "top_k": 3,
        "do_sample": True,
        "repetition_penalty": 1.28,
        "min_length": 10,
        "max_new_tokens": 200
      })
  
  generated_text = tokenizer.batch_decode(generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True)[0]
  print(len(generated_ids[0]))
  print("KYSYMYS:")
  print(generated_text.split('<|avustaja|>')[0])
  print("VASTAUS:")
  print(generated_text.split('<|avustaja|> Vastauksesi:')[1])
  print('##################################')

'''
-->
<s><|alku|> Olet tekoälyavustaja. Seuraavaksi saat kysymyksen tai tehtävän. Kirjoita vastaus parhaasi mukaan siten että se täyttää kysymyksen tai tehtävän vaatimukset.
<|ihminen|> Kysymys/Tehtävä:
 Aku Ankan luona asuu kolme ankanpoikaa. Mitkä ovat heidän nimet?
VASTAUS:
 Ankka Akun kanssa asuvat pojat ovat nimeltään Tupu, Hupu ja Lupu <|loppu|>
##################################

KYSYMYS:
<s><|alku|> Olet tekoälyavustaja. Seuraavaksi saat kysymyksen tai tehtävän. Kirjoita vastaus parhaasi mukaan siten että se täyttää kysymyksen tai tehtävän vaatimukset.
<|ihminen|> Kysymys/Tehtävä:
 Mikä on Suomen korkein tunturi?
VASTAUS:
 Suomen korkein tunturihuippu on Haltitunturi (1 324 metriä). <|loppu|>
##################################

KYSYMYS:
<s><|alku|> Olet tekoälyavustaja. Seuraavaksi saat kysymyksen tai tehtävän. Kirjoita vastaus parhaasi mukaan siten että se täyttää kysymyksen tai tehtävän vaatimukset.
<|ihminen|> Kysymys/Tehtävä:
 Suomi soti Neuvostoliittoa vastaan talvisodan 1939-1940. Kuinka monta päivää sota kesti?
VASTAUS:
 Talvisodan aikana Neuvostoliitto hyökkäsi Suomeen 30. marraskuuta ja 13. maaliskuuta välisenä aikana. Tämä tarkoittaa, että talvisota kesti 105 päivää. <|loppu|>
##################################

KYSYMYS:
<s><|alku|> Olet tekoälyavustaja. Seuraavaksi saat kysymyksen tai tehtävän. Kirjoita vastaus parhaasi mukaan siten että se täyttää kysymyksen tai tehtävän vaatimukset.
<|ihminen|> Kysymys/Tehtävä:
 Luettele viisi yleistä Suomessa yleisesti käytettyä pojan nimeä. Nimet:
VASTAUS:
 Yleisiä suomalaisia poikien nimiä ovat Eino, Onni, Olavi, Väinö ja Ilmari. <|loppu|>
##################################

KYSYMYS:
<s><|alku|> Olet tekoälyavustaja. Seuraavaksi saat kysymyksen tai tehtävän. Kirjoita vastaus parhaasi mukaan siten että se täyttää kysymyksen tai tehtävän vaatimukset.
<|ihminen|> Kysymys/Tehtävä:
 Luettele lyhyt, maksimissaan 50 sanan mittainen runo Suomesta. Runo:
VASTAUS:
 Olipa kerran kaunis maa,
 jossa ihmiset elivät sopusoinnussa.
 Se oli täynnä metsiä ja järviä,
 ja siellä asui onnellisia ja ystävällisiä ihmisiä. <|loppu|>
```

### Limitations and bias

The training data used for this model contains a lot of content from the internet, which is far from neutral. 
Therefore, the model can have biased predictions. This bias will also affect all fine-tuned versions of this model.
To reduce toxic content, the pretrained version of thiis model was trained with dataset filtered with a toxicity classifier but it cannot truly eliminate all toxic text.

### Finetuning

Training was conducted on RTX 4080 using Unsloth framework https://github.com/unslothai/unsloth \
Training script is available in this repo.


## Evaluation results

This model was evaluated using [FIN-bench by TurkuNLP](https://github.com/TurkuNLP/FIN-bench) with zero-shot setting, but \
the evaluation script had some problems running succesfully, so the results reported below should perhaps be viewed with some caution.

[llama-7b-finnish-instruct-v0.2](https://huggingface.co/Finnish-NLP/llama-7b-finnish-instruct-v0.2):

|                      Task                      |Version|       Metric        |Value |   |Stderr|
|------------------------------------------------|------:|---------------------|-----:|---|-----:|
|bigbench_analogies                              |      0|multiple_choice_grade|0.5385|±  |0.0439|
|bigbench_arithmetic_1_digit_addition            |      0|multiple_choice_grade|0.3400|±  |0.0476|
|bigbench_arithmetic_1_digit_division            |      0|multiple_choice_grade|0.4783|±  |0.1065|
|bigbench_arithmetic_1_digit_multiplication      |      0|multiple_choice_grade|0.5200|±  |0.0502|
|bigbench_arithmetic_1_digit_subtraction         |      0|multiple_choice_grade|0.3400|±  |0.0476|
|bigbench_arithmetic_2_digit_addition            |      0|multiple_choice_grade|0.3200|±  |0.0469|
|bigbench_arithmetic_2_digit_division            |      0|multiple_choice_grade|0.3400|±  |0.0476|
|bigbench_arithmetic_2_digit_multiplication      |      0|multiple_choice_grade|0.2200|±  |0.0416|
|bigbench_arithmetic_2_digit_subtraction         |      0|multiple_choice_grade|0.2800|±  |0.0451|
|bigbench_arithmetic_3_digit_addition            |      0|multiple_choice_grade|0.3000|±  |0.0461|
|bigbench_arithmetic_3_digit_division            |      0|multiple_choice_grade|0.2500|±  |0.0435|
|bigbench_arithmetic_3_digit_multiplication      |      0|multiple_choice_grade|0.2200|±  |0.0416|
|bigbench_arithmetic_3_digit_subtraction         |      0|multiple_choice_grade|0.4000|±  |0.0492|
|bigbench_arithmetic_4_digit_addition            |      0|multiple_choice_grade|0.3500|±  |0.0479|
|bigbench_arithmetic_4_digit_division            |      0|multiple_choice_grade|0.2600|±  |0.0441|
|bigbench_arithmetic_4_digit_multiplication      |      0|multiple_choice_grade|0.2100|±  |0.0409|
|bigbench_arithmetic_4_digit_subtraction         |      0|multiple_choice_grade|0.4400|±  |0.0499|
|bigbench_arithmetic_5_digit_addition            |      0|multiple_choice_grade|0.4500|±  |0.0500|
|bigbench_arithmetic_5_digit_division            |      0|multiple_choice_grade|0.1800|±  |0.0386|
|bigbench_arithmetic_5_digit_multiplication      |      0|multiple_choice_grade|0.2000|±  |0.0402|
|bigbench_arithmetic_5_digit_subtraction         |      0|multiple_choice_grade|0.5000|±  |0.0503|
|bigbench_cause_and_effect_one_sentence          |      0|multiple_choice_grade|0.5294|±  |0.0706|
|bigbench_cause_and_effect_one_sentence_no_prompt|      0|multiple_choice_grade|0.8627|±  |0.0487|
|bigbench_cause_and_effect_two_sentences         |      0|multiple_choice_grade|0.4314|±  |0.0700|
|bigbench_emotions                               |      0|multiple_choice_grade|0.4750|±  |0.0396|
|bigbench_empirical_judgments                    |      0|multiple_choice_grade|0.4141|±  |0.0498|
|bigbench_general_knowledge                      |      0|multiple_choice_grade|0.4429|±  |0.0598|
|bigbench_hhh_alignment_harmless                 |      0|multiple_choice_grade|0.3793|±  |0.0643|
|bigbench_hhh_alignment_helpful                  |      0|multiple_choice_grade|0.3220|±  |0.0614|
|bigbench_hhh_alignment_honest                   |      0|multiple_choice_grade|0.3898|±  |0.0640|
|bigbench_hhh_alignment_other                    |      0|multiple_choice_grade|0.5581|±  |0.0766|
|bigbench_intent_recognition                     |      0|multiple_choice_grade|0.2717|±  |0.0169|
|bigbench_misconceptions                         |      0|multiple_choice_grade|0.5373|±  |0.0432|
|bigbench_paraphrase                             |      0|multiple_choice_grade|0.5000|±  |0.0354|
|bigbench_sentence_ambiguity                     |      0|multiple_choice_grade|0.5333|±  |0.0649|
|bigbench_similarities_abstraction               |      0|multiple_choice_grade|0.5921|±  |0.0567|



## Team Members

- Aapo Tanskanen, [Hugging Face profile](https://huggingface.co/aapot), [LinkedIn profile](https://www.linkedin.com/in/aapotanskanen/)
- Rasmus Toivanen, [Hugging Face profile](https://huggingface.co/RASMUS), [LinkedIn profile](https://www.linkedin.com/in/rasmustoivanen/)

Feel free to contact us for more details 🤗