forked from meta-llama/llama-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_batching.py
187 lines (159 loc) · 5.85 KB
/
test_batching.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
# 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 pytest
from contextlib import nullcontext
from dataclasses import dataclass
from datasets import Dataset
from unittest.mock import patch
@dataclass
class Config:
model_type: str = "llama"
EXPECTED_SAMPLE_NUMBER ={
"meta-llama/Llama-2-7b-hf": {
"train": 4,
"eval": 37,
},
"meta-llama/Meta-Llama-3.1-8B-Instruct": {
"train": 3,
"eval": 30,
},
"fake_llama": {
"train": 2,
"eval": 17,
}
}
fake_samsum_dataset = 2048*[{'id': '420',
'dialogue': "Mario: It's a me, Mario!\nLuigi: It's a me, your brother!\nMario: I'm going to save the princess.\nLuigi: I'm going to help Mario.",
'summary': 'Mario and Luigi are going to save the princess.'}]
@pytest.mark.skip_missing_tokenizer
@patch('llama_recipes.finetuning.train')
@patch('llama_recipes.finetuning.AutoTokenizer')
@patch("llama_recipes.finetuning.AutoConfig.from_pretrained")
@patch("llama_recipes.finetuning.AutoProcessor")
@patch("llama_recipes.finetuning.MllamaForConditionalGeneration.from_pretrained")
@patch('llama_recipes.finetuning.LlamaForCausalLM.from_pretrained')
@patch('llama_recipes.finetuning.optim.AdamW')
@patch('llama_recipes.finetuning.StepLR')
@patch('llama_recipes.datasets.samsum_dataset.datasets')
def test_packing(
datasets,
step_lr,
optimizer,
get_model,
get_mmodel,
processor,
get_config,
tokenizer,
train,
setup_tokenizer,
setup_processor,
llama_version,
model_type,
):
from llama_recipes.finetuning import main
setup_tokenizer(tokenizer)
setup_processor(processor)
get_model.return_value.get_input_embeddings.return_value.weight.shape = [32000 if "Llama-2" in llama_version else 128256]
get_mmodel.return_value.get_input_embeddings.return_value.weight.shape = [0]
get_config.return_value = Config(model_type=model_type)
datasets.load_dataset.return_value = Dataset.from_list(fake_samsum_dataset)
kwargs = {
"model_name": llama_version,
"batch_size_training": 8,
"val_batch_size": 1,
"use_peft": False,
"dataset": "samsum_dataset",
"batching_strategy": "packing",
}
c = nullcontext() if model_type == "llama" else pytest.raises(ValueError)
with c:
main(**kwargs)
if model_type == "llama":
assert train.call_count == 1
args, kwargs = train.call_args
train_dataloader = args[1]
eval_dataloader = args[2]
assert len(train_dataloader) == EXPECTED_SAMPLE_NUMBER[llama_version]["train"]
assert len(eval_dataloader) == EXPECTED_SAMPLE_NUMBER[llama_version]["eval"]
batch = next(iter(train_dataloader))
assert "labels" in batch.keys()
assert "input_ids" in batch.keys()
assert "attention_mask" in batch.keys()
assert batch["labels"][0].size(0) == 4096
assert batch["input_ids"][0].size(0) == 4096
assert batch["attention_mask"][0].size(0) == 4096
@pytest.mark.skip_missing_tokenizer
@patch("llama_recipes.utils.train_utils.torch.cuda.is_bf16_supported")
@patch("llama_recipes.finetuning.torch.cuda.is_available")
@patch('llama_recipes.finetuning.train')
@patch('llama_recipes.finetuning.AutoTokenizer')
@patch("llama_recipes.finetuning.AutoConfig.from_pretrained")
@patch("llama_recipes.finetuning.AutoProcessor")
@patch("llama_recipes.finetuning.MllamaForConditionalGeneration.from_pretrained")
@patch('llama_recipes.finetuning.LlamaForCausalLM.from_pretrained')
@patch('llama_recipes.finetuning.optim.AdamW')
@patch('llama_recipes.finetuning.StepLR')
@patch('llama_recipes.finetuning.setup')
@patch('llama_recipes.finetuning.FSDP')
@patch('llama_recipes.finetuning.torch.distributed.is_initialized')
@patch('llama_recipes.utils.config_utils.dist')
@patch('llama_recipes.datasets.samsum_dataset.datasets')
def test_distributed_packing(
datasets,
dist,
is_initialized,
fsdp,
setup,
step_lr,
optimizer,
get_model,
get_mmodel,
processor,
get_config,
tokenizer,
train,
cuda_is_available,
cuda_is_bf16_supported,
setup_tokenizer,
setup_processor,
llama_version,
model_type,
):
import os
from llama_recipes.finetuning import main
setup_tokenizer(tokenizer)
setup_processor(processor)
get_model.return_value.get_input_embeddings.return_value.weight.shape = [32000 if "Llama-2" in llama_version else 128256]
get_mmodel.return_value.get_input_embeddings.return_value.weight.shape = [0]
get_config.return_value = Config(model_type=model_type)
cuda_is_available.return_value = False
cuda_is_bf16_supported.return_value = False
datasets.load_dataset.return_value = Dataset.from_list(fake_samsum_dataset)
rank = 1
os.environ['LOCAL_RANK'] = f'{rank}'
os.environ['RANK'] = f'{rank}'
os.environ['WORLD_SIZE'] = '2'
os.environ['MASTER_ADDR'] = 'localhost'
os.environ['MASTER_PORT'] = '12345'
kwargs = {
"model_name": llama_version,
"batch_size_training": 8,
"val_batch_size": 1,
"use_peft": False,
"dataset": "samsum_dataset",
"batching_strategy": "packing",
"enable_fsdp": True
}
is_initialized.return_value = True
dist.get_rank.return_value = rank
dist.get_world_size.return_value = 2
c = nullcontext() if model_type == "llama" else pytest.raises(ValueError)
with c:
main(**kwargs)
if model_type == "llama":
assert train.call_count == 1
args, kwargs = train.call_args
train_dataloader = args[1]
eval_dataloader = args[2]
assert len(train_dataloader) == EXPECTED_SAMPLE_NUMBER[llama_version]["train"] //2
assert len(eval_dataloader) == EXPECTED_SAMPLE_NUMBER[llama_version]["eval"] //2