cassuto commited on
Commit
811482d
·
verified ·
1 Parent(s): d543918

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. README.md +183 -0
  2. adapter_config.json +29 -0
  3. adapter_model.safetensors +3 -0
README.md ADDED
@@ -0,0 +1,183 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ datasets:
3
+ - facebook/anli
4
+ metrics:
5
+ - accuracy
6
+ base_model:
7
+ - meta-llama/Llama-3.1-8B-Instruct
8
+ pipeline_tag: sentence-similarity
9
+ library_name: peft
10
+ tags:
11
+ - NLI
12
+ ---
13
+ # Model Card for Model ID
14
+
15
+ The Meta Llama-3.1-8B-Instruct model fine-tuned on the Adversarial Natural Language Inference (ANLI) Benchmark.
16
+
17
+ **Evaluation Results**
18
+
19
+ Accuracy:
20
+ | ANLI-R1 | ANLI-R2 | ANLI-R3 | Avg. |
21
+ | ------- | ------- |-------|-------|
22
+ | 77.2 | 62.8 | 61.2 | 67.1 |
23
+
24
+ ## Usage
25
+
26
+ NLI use-case:
27
+
28
+ ```python
29
+ from transformers import AutoModelForCausalLM, AutoTokenizer
30
+ from transformers import AutoTokenizer
31
+ from peft import PeftModel
32
+ import torch
33
+
34
+ base_model_name = 'meta-llama/Llama-3.1-8B-Instruct'
35
+ tokenizer = AutoTokenizer.from_pretrained(base_model_name)
36
+ model = AutoModelForCausalLM.from_pretrained(model_name,
37
+ pad_token_id=tokenizer.eos_token_id,
38
+ device_map='auto')
39
+
40
+ lora_model = PeftModel.from_pretrained(model, 'cassuto/Llama-3.1-ANLI-R1-R2-R3-8B-Instruct')
41
+
42
+ label_str = ['entailment', 'neutral', 'contradiction']
43
+
44
+ def eval(premise : str, hypothesis : str, device = 'cuda'):
45
+ input = ("<|start_header_id|>system<|end_header_id|>\n\nBased on the following premise, determine if the hypothesis is entailment, contradiction, or neutral." +
46
+ "<|eot_id|><|start_header_id|>user<|end_header_id|>\n\n"
47
+ "<Premise>: " + premise + "\n\n<Hypothesis>: " + hypothesis + "\n\n" +
48
+ "<|eot_id|><|start_header_id|>assistant<|end_header_id|>")
49
+ tk = tokenizer(input)
50
+
51
+ with torch.no_grad():
52
+ input_ids = torch.tensor(tk['input_ids']).unsqueeze(0).to(device)
53
+ out = lora_model.generate(input_ids=input_ids,
54
+ attention_mask=torch.tensor(tk['attention_mask']).unsqueeze(0).to(device),
55
+ max_new_tokens=10
56
+ )
57
+ print(tokenizer.decode(out[0]))
58
+ s = tokenizer.decode(out[0][input_ids.shape[-1]:])
59
+ for lbl, l in enumerate(label_str):
60
+ if s.find(l) > -1:
61
+ return lbl
62
+ else:
63
+ assert False, 'Invalid model output: ' + s
64
+
65
+ print(eval("A man is playing a guitar.", "A woman is reading a book."))
66
+ ```
67
+
68
+ ## Training Details
69
+
70
+ - **Dataset:** facebook/anli
71
+ - **Hardware:** NIVIDA H20 (96GB) card x1.
72
+
73
+ #### Fine Tuning Hyperparameters
74
+
75
+ - **Training regime:** fp16 <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
76
+ - **LoRA rank:** 64
77
+ - **LoRA alpha:** 16
78
+ - **LoRA dropout:** 0.1
79
+ - **Learning rate:** 0.0001
80
+ - **Training Batch Size:** 4
81
+ - **Epoch:** 3
82
+ - **Context length:** 2048
83
+
84
+ #### Fine Tuning Code
85
+
86
+ ```python
87
+ from datasets import load_dataset
88
+ import numpy as np
89
+
90
+ dataset = load_dataset("anli")
91
+
92
+ model_name = "meta-llama/Llama-3.1-8B-Instruct"
93
+ def out_ckp(r):
94
+ return f"/path/to/project/Llama-3.1-ANLI-R1-R2-R3-8B-Instruct/checkpoints-r{r}"
95
+ def out_lora_model_fn(r):
96
+ return f'/path/to/project/Llama-3.1-ANLI-R1-R2-R3-8B-Instruct/lora-r{r}'
97
+
98
+ from transformers import AutoModelForCausalLM, AutoTokenizer
99
+
100
+ from transformers import AutoTokenizer, GenerationConfig
101
+ from peft import LoraConfig, get_peft_model, PeftModel
102
+ from trl import SFTConfig, SFTTrainer
103
+ import torch
104
+ from collections.abc import Mapping
105
+
106
+ label_str = ['entailment', 'neutral', 'contradiction']
107
+
108
+ def preprocess_function(examples):
109
+ inputs = ["<|start_header_id|>system<|end_header_id|>\n\nBased on the following premise, determine if the hypothesis is entailment, contradiction, or neutral." +
110
+ "<|eot_id|><|start_header_id|>user<|end_header_id|>\n\n"
111
+ "<Premise>: " + p + "\n\n<Hypothesis>: " + h + "\n\n" +
112
+ "<|eot_id|><|start_header_id|>assistant<|end_header_id|>" + label_str[lbl] + "<|eot_id|>\n" # FIXME remove \n
113
+ for p, h, lbl in zip(examples["premise"], examples["hypothesis"], examples['label'])]
114
+
115
+ model_inputs = {}
116
+ model_inputs['text'] = inputs
117
+ return model_inputs
118
+
119
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
120
+
121
+ model = AutoModelForCausalLM.from_pretrained(model_name,
122
+ pad_token_id=tokenizer.eos_token,
123
+ device_map='auto')
124
+ model.config.use_cache=False
125
+ model.config.pretraining_tp=1
126
+
127
+ tokenizer.padding_side = "right"
128
+ tokenizer.pad_token = tokenizer.eos_token
129
+
130
+ for r in range(1,4):
131
+ print('Round ', r)
132
+
133
+ train_data = dataset[f'train_r{r}']
134
+ val_data = dataset[f'dev_r{r}']
135
+ train_data = train_data.map(preprocess_function, batched=True,num_proc=8)
136
+ val_data = val_data.map(preprocess_function, batched=True,num_proc=8)
137
+
138
+ training_args = SFTConfig(
139
+ fp16=True,
140
+ output_dir=out_ckp(r),
141
+ learning_rate=1e-4,
142
+ per_device_train_batch_size=4,
143
+ per_device_eval_batch_size=1,
144
+ num_train_epochs=3,
145
+ logging_steps=10,
146
+ weight_decay=0,
147
+ logging_dir=f"./logs-r{r}",
148
+ save_strategy="epoch",
149
+ save_total_limit=1,
150
+ max_seq_length=2048,
151
+ packing=False,
152
+ dataset_text_field="text"
153
+ )
154
+
155
+ if r==1:
156
+ # create LoRA model
157
+ peft_config = LoraConfig(
158
+ r=64,
159
+ lora_alpha=16,
160
+ lora_dropout=0.1,
161
+ bias="none",
162
+ task_type='CAUSAL_LM'
163
+ )
164
+ lora_model = get_peft_model(model, peft_config)
165
+ else:
166
+ # load the previous trained LoRA part
167
+ lora_model = PeftModel.from_pretrained(model, out_lora_model_fn(r-1),
168
+ is_trainable=True)
169
+
170
+ trainer = SFTTrainer(
171
+ model=lora_model,
172
+ tokenizer=tokenizer,
173
+ args=training_args,
174
+ train_dataset=train_data,
175
+ )
176
+
177
+ trainer.train()
178
+ print(f'saving to "{out_lora_model_fn(r)}"')
179
+ lora_model.save_pretrained(out_lora_model_fn(r))
180
+ ```
181
+
182
+
183
+ - PEFT 0.13.2
adapter_config.json ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "alpha_pattern": {},
3
+ "auto_mapping": null,
4
+ "base_model_name_or_path": "/root/autodl-tmp/Llama-3.1-8B-Instruct",
5
+ "bias": "none",
6
+ "fan_in_fan_out": false,
7
+ "inference_mode": true,
8
+ "init_lora_weights": true,
9
+ "layer_replication": null,
10
+ "layers_pattern": null,
11
+ "layers_to_transform": null,
12
+ "loftq_config": {},
13
+ "lora_alpha": 16,
14
+ "lora_dropout": 0.1,
15
+ "megatron_config": null,
16
+ "megatron_core": "megatron.core",
17
+ "modules_to_save": null,
18
+ "peft_type": "LORA",
19
+ "r": 64,
20
+ "rank_pattern": {},
21
+ "revision": null,
22
+ "target_modules": [
23
+ "v_proj",
24
+ "q_proj"
25
+ ],
26
+ "task_type": "CAUSAL_LM",
27
+ "use_dora": false,
28
+ "use_rslora": false
29
+ }
adapter_model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a86019e18a1e0b62acc814d6adc53cdd914e0e0d198bd45e0f6ef9490b21d1a1
3
+ size 109069176