-
Notifications
You must be signed in to change notification settings - Fork 448
/
utils.py
235 lines (206 loc) · 7.16 KB
/
utils.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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
import json
import os
from pathlib import Path
from typing import List
import torch
from torch.utils.data import Dataset
CKPT_COMPONENT_MAP = {
"tune": "torchtune.training.FullModelTorchTuneCheckpointer",
"meta": "torchtune.training.FullModelMetaCheckpointer",
"hf": "torchtune.training.FullModelHFCheckpointer",
}
class DummyDataset(Dataset):
def __init__(self, *args, **kwargs):
self._data = torch.LongTensor(
[
[0, 2, 4, 2, 5, 6, 7, 8, 9, 1, 2, 4, 3, 3, 5, 6, 8, 2, 1, 1],
[1, 2, 5, 6, 7, 8, 2, 3, 1, 9, 9, 9, 5, 6, 7, 0, 0, 0, 1, 2],
[5, 6, 8, 2, 1, 0, 3, 4, 0, 0, 0, 2, 4, 7, 8, 8, 2, 2, 1, 0],
[4, 6, 7, 1, 0, 2, 0, 2, 0, 2, 3, 9, 9, 9, 7, 5, 1, 8, 4, 1],
]
)
self._labels = torch.LongTensor(
[
[2, 6, 7, 8, 2, 2, 1, 0, 0, 1],
[1, 2, 5, 6, 7, 8, 2, 3, 1, 9],
[6, 1, 1, 2, 5, 0, 9, 0, 2, 1],
[5, 8, 6, 0, 2, 0, 0, 3, 2, 1],
]
)
def __getitem__(self, index):
return {"tokens": self._data[index], "labels": self._labels[index]}
def __len__(self):
return len(self._data)
def get_assets_path():
return Path(__file__).parent.parent / "assets"
def dummy_stack_exchange_dataset_config():
data_files = os.path.join(get_assets_path(), "stack_exchange_paired_tiny.json")
out = [
"dataset._component_=torchtune.datasets.stack_exchange_paired_dataset",
"dataset.source='json'",
f"dataset.data_files={data_files}",
"dataset.split='train'",
]
return out
def dummy_alpaca_dataset_config():
data_files = os.path.join(get_assets_path(), "alpaca_tiny.json")
out = [
"dataset._component_=torchtune.datasets.alpaca_dataset",
"dataset.source='json'",
f"dataset.data_files={data_files}",
"dataset.split='train'",
]
return out
def dummy_text_completion_alpaca_dataset_config():
"""
Constructs a minimal text-completion-style dataset from ``alpaca_tiny.json``.
This is used for testing PPO fine-tuning.
"""
data_files = os.path.join(get_assets_path(), "alpaca_tiny.json")
out = [
"dataset._component_=torchtune.datasets.text_completion_dataset",
"dataset.source='json'",
f"dataset.data_files={data_files}",
"dataset.column='instruction'",
"dataset.split='train[:10%]'", # 10% of the dataset gets us 8 batches
"dataset.add_eos=False",
]
return out
def llama2_test_config() -> List[str]:
return [
"model._component_=torchtune.models.llama2.llama2",
"model.vocab_size=32_000",
"model.num_layers=4",
"model.num_heads=16",
"model.embed_dim=256",
"model.max_seq_len=2048",
"model.norm_eps=1e-5",
"model.num_kv_heads=8",
]
def llama2_classifier_test_config() -> List[str]:
return [
"model._component_=torchtune.models.llama2.llama2_classifier",
"model.num_classes=1",
"model.vocab_size=32_000",
"model.num_layers=4",
"model.num_heads=16",
"model.embed_dim=256",
"model.max_seq_len=2048",
"model.norm_eps=1e-5",
"model.num_kv_heads=8",
]
def llama3_test_config() -> List[str]:
return [
"model._component_=torchtune.models.llama3.llama3",
"model.vocab_size=128_256",
"model.num_layers=2",
"model.num_heads=8",
"model.embed_dim=64",
"model.max_seq_len=1024",
"model.norm_eps=1e-5",
"model.num_kv_heads=4",
]
def lora_llama2_test_config(
lora_attn_modules,
apply_lora_to_mlp: bool = False,
apply_lora_to_output: bool = False,
lora_rank: int = 8,
lora_alpha: float = 16,
quantize_base: bool = False,
use_dora: bool = False,
) -> List[str]:
return [
# Note: we explicitly use _component_ so that we can also call
# config.instantiate directly for easier comparison
"model._component_=torchtune.models.llama2.lora_llama2",
f"model.lora_attn_modules={lora_attn_modules}",
f"model.apply_lora_to_mlp={apply_lora_to_mlp}",
f"model.apply_lora_to_output={apply_lora_to_output}",
"model.vocab_size=32000",
"model.num_layers=4",
"model.num_heads=16",
"model.embed_dim=256",
"model.max_seq_len=2048",
"model.norm_eps=1e-5",
"model.num_kv_heads=8",
f"model.lora_rank={lora_rank}",
f"model.lora_alpha={lora_alpha}",
"model.lora_dropout=0.0",
f"model.quantize_base={quantize_base}",
f"model.use_dora={use_dora}",
]
def lora_llama3_test_config(
lora_attn_modules,
apply_lora_to_mlp: bool = False,
apply_lora_to_output: bool = False,
lora_rank: int = 8,
lora_alpha: float = 16,
quantize_base: bool = False,
) -> List[str]:
return [
# Note: we explicitly use _component_ so that we can also call
# config.instantiate directly for easier comparison
"model._component_=torchtune.models.llama3.lora_llama3",
f"model.lora_attn_modules={lora_attn_modules}",
f"model.apply_lora_to_mlp={apply_lora_to_mlp}",
f"model.apply_lora_to_output={apply_lora_to_output}",
"model.vocab_size=128_256",
"model.num_layers=2",
"model.num_heads=8",
"model.embed_dim=64",
"model.max_seq_len=1024",
"model.norm_eps=1e-5",
"model.num_kv_heads=4",
f"model.lora_rank={lora_rank}",
f"model.lora_alpha={lora_alpha}",
"model.lora_dropout=0.0",
f"model.quantize_base={quantize_base}",
]
def write_hf_ckpt_config(ckpt_dir: str):
config = {
"hidden_size": 256,
"num_attention_heads": 16,
"num_key_value_heads": 8,
}
config_file = Path.joinpath(Path(ckpt_dir), "config.json")
with config_file.open("w") as f:
json.dump(config, f)
MODEL_TEST_CONFIGS = {
"llama2": llama2_test_config(),
"llama3": llama3_test_config(),
"llama2_lora": lora_llama2_test_config(
lora_attn_modules=["q_proj", "k_proj", "v_proj", "output_proj"],
apply_lora_to_mlp=False,
apply_lora_to_output=False,
lora_rank=8,
lora_alpha=16,
),
"llama2_dora": lora_llama2_test_config(
lora_attn_modules=["q_proj", "k_proj", "v_proj", "output_proj"],
apply_lora_to_mlp=False,
apply_lora_to_output=False,
lora_rank=8,
lora_alpha=16,
use_dora=True,
),
"llama2_qlora": lora_llama2_test_config(
lora_attn_modules=["q_proj", "k_proj", "v_proj", "output_proj"],
apply_lora_to_mlp=True,
apply_lora_to_output=False,
lora_rank=8,
lora_alpha=16,
quantize_base=True,
),
"llama3_lora": lora_llama3_test_config(
lora_attn_modules=["q_proj", "k_proj", "v_proj", "output_proj"],
apply_lora_to_mlp=False,
apply_lora_to_output=False,
lora_rank=8,
lora_alpha=16,
),
}