forked from meta-llama/llama-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_finetuning.py
272 lines (223 loc) · 8.18 KB
/
test_finetuning.py
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# Copyright (c) Meta Platforms, Inc. and affiliates.
# This software may be used and distributed according to the terms of the Llama 2 Community License Agreement.
import os
from contextlib import nullcontext
from dataclasses import dataclass
from unittest.mock import patch
import pytest
import torch
from llama_recipes.data.sampler import LengthBasedBatchSampler
from llama_recipes.finetuning import main
from pytest import approx
from torch.optim import AdamW
from torch.utils.data.dataloader import DataLoader
from torch.utils.data.sampler import BatchSampler
@dataclass
class Config:
model_type: str = "llama"
def get_fake_dataset():
return 8192*[
{
"input_ids": [1],
"attention_mask": [1],
"labels": [1],
}
]
@patch("llama_recipes.finetuning.torch.cuda.is_available")
@patch("llama_recipes.finetuning.train")
@patch("llama_recipes.finetuning.MllamaForConditionalGeneration.from_pretrained")
@patch("llama_recipes.finetuning.AutoProcessor.from_pretrained")
@patch("llama_recipes.finetuning.LlamaForCausalLM.from_pretrained")
@patch("llama_recipes.finetuning.AutoConfig.from_pretrained")
@patch("llama_recipes.finetuning.AutoTokenizer.from_pretrained")
@patch("llama_recipes.finetuning.get_preprocessed_dataset")
@patch("llama_recipes.finetuning.generate_peft_config")
@patch("llama_recipes.finetuning.get_peft_model")
@patch("llama_recipes.finetuning.optim.AdamW")
@patch("llama_recipes.finetuning.StepLR")
@pytest.mark.parametrize("cuda_is_available", [True, False])
@pytest.mark.parametrize("run_validation", [True, False])
@pytest.mark.parametrize("use_peft", [True, False])
def test_finetuning(
step_lr,
optimizer,
get_peft_model,
gen_peft_config,
get_dataset,
tokenizer,
get_config,
get_model,
get_processor,
get_mmodel,
train,
cuda,
cuda_is_available,
run_validation,
use_peft,
model_type,
):
kwargs = {
"run_validation": run_validation,
"use_peft": use_peft,
"batching_strategy": "packing" if model_type == "llama" else "padding",
}
get_dataset.return_value = get_fake_dataset()
cuda.return_value = cuda_is_available
get_model.return_value.get_input_embeddings.return_value.weight.shape = [0]
get_mmodel.return_value.get_input_embeddings.return_value.weight.shape = [0]
get_config.return_value = Config(model_type=model_type)
main(**kwargs)
assert train.call_count == 1
args, kwargs = train.call_args
train_dataloader = args[1]
eval_dataloader = args[2]
assert isinstance(train_dataloader, DataLoader)
if run_validation:
assert isinstance(eval_dataloader, DataLoader)
else:
assert eval_dataloader is None
if use_peft:
assert get_peft_model.return_value.print_trainable_parameters.call_count == 1
model = get_peft_model
elif model_type == "llama":
model = get_model
else:
model = get_mmodel
if cuda_is_available:
assert model.return_value.to.call_count == 1
assert model.return_value.to.call_args.args[0] == "cuda"
else:
assert model.return_value.to.call_count == 0
@patch("llama_recipes.finetuning.get_peft_model")
@patch("llama_recipes.finetuning.setup")
@patch("llama_recipes.finetuning.train")
@patch("llama_recipes.finetuning.MllamaForConditionalGeneration.from_pretrained")
@patch("llama_recipes.finetuning.AutoProcessor.from_pretrained")
@patch("llama_recipes.finetuning.LlamaForCausalLM.from_pretrained")
@patch("llama_recipes.finetuning.AutoConfig.from_pretrained")
@patch("llama_recipes.finetuning.AutoTokenizer.from_pretrained")
@patch("llama_recipes.finetuning.get_preprocessed_dataset")
def test_finetuning_peft_llama_adapter(
get_dataset,
tokenizer,
get_config,
get_model,
get_processor,
get_mmodel,
train,
setup,
get_peft_model,
model_type,
):
kwargs = {
"use_peft": True,
"peft_method": "llama_adapter",
"enable_fsdp": True,
"batching_strategy": "packing" if model_type == "llama" else "padding",
}
get_dataset.return_value = get_fake_dataset()
get_model.return_value.get_input_embeddings.return_value.weight.shape = [0]
get_mmodel.return_value.get_input_embeddings.return_value.weight.shape = [0]
get_config.return_value = Config(model_type=model_type)
os.environ["RANK"] = "0"
os.environ["LOCAL_RANK"] = "0"
os.environ["WORLD_SIZE"] = "1"
os.environ["MASTER_ADDR"] = "localhost"
os.environ["MASTER_PORT"] = "12345"
with pytest.raises(
RuntimeError,
match="Llama_adapter is currently not supported in combination with FSDP",
):
main(**kwargs)
GET_ME_OUT = "Get me out of here"
get_peft_model.side_effect = RuntimeError(GET_ME_OUT)
kwargs["enable_fsdp"] = False
with pytest.raises(
RuntimeError,
match=GET_ME_OUT,
):
main(**kwargs)
@patch("llama_recipes.finetuning.train")
@patch("llama_recipes.finetuning.MllamaForConditionalGeneration.from_pretrained")
@patch("llama_recipes.finetuning.AutoProcessor.from_pretrained")
@patch("llama_recipes.finetuning.LlamaForCausalLM.from_pretrained")
@patch("llama_recipes.finetuning.AutoConfig.from_pretrained")
@patch("llama_recipes.finetuning.AutoTokenizer.from_pretrained")
@patch("llama_recipes.finetuning.get_preprocessed_dataset")
@patch("llama_recipes.finetuning.get_peft_model")
@patch("llama_recipes.finetuning.StepLR")
def test_finetuning_weight_decay(
step_lr,
get_peft_model,
get_dataset,
tokenizer,
get_config,
get_model,
get_processor,
get_mmodel,
train,
model_type,
):
kwargs = {
"weight_decay": 0.01,
"batching_strategy": "packing" if model_type == "llama" else "padding",
}
get_dataset.return_value = get_fake_dataset()
model = get_model if model_type == "llama" else get_mmodel
model.return_value.parameters.return_value = [torch.ones(1, 1)]
model.return_value.get_input_embeddings.return_value.weight.shape = [0]
get_config.return_value = Config(model_type=model_type)
main(**kwargs)
assert train.call_count == 1
args, kwargs = train.call_args
optimizer = args[4]
assert isinstance(optimizer, AdamW)
assert optimizer.state_dict()["param_groups"][0]["weight_decay"] == approx(0.01)
@patch("llama_recipes.finetuning.train")
@patch("llama_recipes.finetuning.MllamaForConditionalGeneration.from_pretrained")
@patch("llama_recipes.finetuning.AutoProcessor.from_pretrained")
@patch("llama_recipes.finetuning.LlamaForCausalLM.from_pretrained")
@patch("llama_recipes.finetuning.AutoConfig.from_pretrained")
@patch("llama_recipes.finetuning.AutoTokenizer.from_pretrained")
@patch("llama_recipes.finetuning.get_preprocessed_dataset")
@patch("llama_recipes.finetuning.optim.AdamW")
@patch("llama_recipes.finetuning.StepLR")
def test_batching_strategy(
step_lr,
optimizer,
get_dataset,
tokenizer,
get_config,
get_model,
get_processor,
get_mmodel,
train,
model_type,
):
kwargs = {
"batching_strategy": "packing",
}
get_dataset.return_value = get_fake_dataset()
model = get_model if model_type == "llama" else get_mmodel
model.return_value.get_input_embeddings.return_value.weight.shape = [0]
get_config.return_value = Config(model_type=model_type)
c = nullcontext() if model_type == "llama" else pytest.raises(ValueError)
with c:
main(**kwargs)
assert train.call_count == (1 if model_type == "llama" else 0)
if model_type == "llama":
args, kwargs = train.call_args
train_dataloader, eval_dataloader = args[1:3]
assert isinstance(train_dataloader.batch_sampler, BatchSampler)
assert isinstance(eval_dataloader.batch_sampler, BatchSampler)
kwargs["batching_strategy"] = "padding"
train.reset_mock()
main(**kwargs)
assert train.call_count == 1
args, kwargs = train.call_args
train_dataloader, eval_dataloader = args[1:3]
assert isinstance(train_dataloader.batch_sampler, LengthBasedBatchSampler)
assert isinstance(eval_dataloader.batch_sampler, LengthBasedBatchSampler)
kwargs["batching_strategy"] = "none"
with pytest.raises(ValueError):
main(**kwargs)