more tweaks to do pre-training with bettertransformers
Browse files- scripts/finetune.py +2 -0
- src/axolotl/utils/callbacks.py +24 -0
- src/axolotl/utils/data.py +7 -5
- src/axolotl/utils/models.py +2 -2
- src/axolotl/utils/trainer.py +7 -1
- src/axolotl/utils/validation.py +12 -4
scripts/finetune.py
CHANGED
@@ -14,6 +14,7 @@ import torch
|
|
14 |
import yaml
|
15 |
|
16 |
# add src to the pythonpath so we don't need to pip install this
|
|
|
17 |
from optimum.bettertransformer import BetterTransformer
|
18 |
from transformers import GenerationConfig, TextStreamer
|
19 |
|
@@ -214,6 +215,7 @@ def train(
|
|
214 |
train_dataset = load_pretraining_dataset(
|
215 |
pretraining_dataset, tokenizer, max_tokens=cfg.sequence_len
|
216 |
)
|
|
|
217 |
eval_dataset = None
|
218 |
|
219 |
if cfg.debug or "debug" in kwargs:
|
|
|
14 |
import yaml
|
15 |
|
16 |
# add src to the pythonpath so we don't need to pip install this
|
17 |
+
from datasets import Dataset
|
18 |
from optimum.bettertransformer import BetterTransformer
|
19 |
from transformers import GenerationConfig, TextStreamer
|
20 |
|
|
|
215 |
train_dataset = load_pretraining_dataset(
|
216 |
pretraining_dataset, tokenizer, max_tokens=cfg.sequence_len
|
217 |
)
|
218 |
+
train_dataset = Dataset.from_list(list(train_dataset))
|
219 |
eval_dataset = None
|
220 |
|
221 |
if cfg.debug or "debug" in kwargs:
|
src/axolotl/utils/callbacks.py
CHANGED
@@ -2,6 +2,7 @@
|
|
2 |
|
3 |
import os
|
4 |
|
|
|
5 |
from transformers import (
|
6 |
TrainerCallback,
|
7 |
TrainerControl,
|
@@ -30,3 +31,26 @@ class SavePeftModelCallback(TrainerCallback): # pylint: disable=too-few-public-
|
|
30 |
kwargs["model"].save_pretrained(peft_model_path)
|
31 |
|
32 |
return control
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
import os
|
4 |
|
5 |
+
from optimum.bettertransformer import BetterTransformer
|
6 |
from transformers import (
|
7 |
TrainerCallback,
|
8 |
TrainerControl,
|
|
|
31 |
kwargs["model"].save_pretrained(peft_model_path)
|
32 |
|
33 |
return control
|
34 |
+
|
35 |
+
|
36 |
+
class SaveBetterTransformerModelCallback(
|
37 |
+
TrainerCallback
|
38 |
+
): # pylint: disable=too-few-public-methods
|
39 |
+
"""Callback to save the BatterTransformer wrapped model"""
|
40 |
+
|
41 |
+
def on_save(
|
42 |
+
self,
|
43 |
+
args: TrainingArguments,
|
44 |
+
state: TrainerState,
|
45 |
+
control: TrainerControl,
|
46 |
+
**kwargs,
|
47 |
+
):
|
48 |
+
checkpoint_folder = os.path.join(
|
49 |
+
args.output_dir,
|
50 |
+
f"{PREFIX_CHECKPOINT_DIR}-{state.global_step}",
|
51 |
+
)
|
52 |
+
|
53 |
+
model = BetterTransformer.reverse(kwargs["model"])
|
54 |
+
model.save_pretrained(checkpoint_folder)
|
55 |
+
|
56 |
+
return control
|
src/axolotl/utils/data.py
CHANGED
@@ -409,14 +409,16 @@ class PretrainingDatasetWrapper(IterableDataset):
|
|
409 |
buffer = []
|
410 |
for sample in load_dataset(
|
411 |
self.dataset_path,
|
412 |
-
|
413 |
-
split="train",
|
414 |
-
streaming=True,
|
415 |
-
).shuffle(buffer_size=10000):
|
416 |
buffer += self.tokenizer(sample["text"])["input_ids"]
|
417 |
buffer += [self.tokenizer.eos_token_id]
|
418 |
while len(buffer) > self.max_tokens:
|
419 |
-
|
|
|
|
|
|
|
|
|
|
|
420 |
buffer = buffer[self.max_tokens :]
|
421 |
|
422 |
|
|
|
409 |
buffer = []
|
410 |
for sample in load_dataset(
|
411 |
self.dataset_path,
|
412 |
+
)["train"].shuffle():
|
|
|
|
|
|
|
413 |
buffer += self.tokenizer(sample["text"])["input_ids"]
|
414 |
buffer += [self.tokenizer.eos_token_id]
|
415 |
while len(buffer) > self.max_tokens:
|
416 |
+
input_ids = torch.tensor(buffer[: self.max_tokens])
|
417 |
+
yield {
|
418 |
+
"input_ids": input_ids,
|
419 |
+
"attention_mask": torch.ones(input_ids.size()),
|
420 |
+
"labels": input_ids,
|
421 |
+
}
|
422 |
buffer = buffer[self.max_tokens :]
|
423 |
|
424 |
|
src/axolotl/utils/models.py
CHANGED
@@ -10,8 +10,8 @@ from typing import TYPE_CHECKING, Optional, Tuple # noqa: F401
|
|
10 |
import bitsandbytes as bnb
|
11 |
import torch
|
12 |
import transformers
|
13 |
-
from transformers import PreTrainedModel # noqa: F401
|
14 |
from optimum.bettertransformer import BetterTransformer
|
|
|
15 |
from transformers import (
|
16 |
AutoConfig,
|
17 |
AutoModelForCausalLM,
|
@@ -136,7 +136,7 @@ def load_model(
|
|
136 |
logging.info("patching with xpos rope")
|
137 |
replace_llama_rope_with_xpos_rope()
|
138 |
|
139 |
-
if cfg.bf16:
|
140 |
torch_dtype = torch.bfloat16
|
141 |
elif cfg.load_in_8bit or cfg.fp16 or cfg.float16:
|
142 |
torch_dtype = torch.float16
|
|
|
10 |
import bitsandbytes as bnb
|
11 |
import torch
|
12 |
import transformers
|
|
|
13 |
from optimum.bettertransformer import BetterTransformer
|
14 |
+
from transformers import PreTrainedModel # noqa: F401
|
15 |
from transformers import (
|
16 |
AutoConfig,
|
17 |
AutoModelForCausalLM,
|
|
|
136 |
logging.info("patching with xpos rope")
|
137 |
replace_llama_rope_with_xpos_rope()
|
138 |
|
139 |
+
if cfg.bf16 or cfg.bfloat16:
|
140 |
torch_dtype = torch.bfloat16
|
141 |
elif cfg.load_in_8bit or cfg.fp16 or cfg.float16:
|
142 |
torch_dtype = torch.float16
|
src/axolotl/utils/trainer.py
CHANGED
@@ -16,7 +16,10 @@ from torch.optim.lr_scheduler import OneCycleLR
|
|
16 |
from transformers import EarlyStoppingCallback, Trainer
|
17 |
from transformers.trainer_pt_utils import get_parameter_names
|
18 |
|
19 |
-
from axolotl.utils.callbacks import
|
|
|
|
|
|
|
20 |
from axolotl.utils.schedulers import InterpolatingLogScheduler
|
21 |
|
22 |
|
@@ -228,6 +231,9 @@ def setup_trainer(cfg, train_dataset, eval_dataset, model, tokenizer):
|
|
228 |
]: # only save in rank 0
|
229 |
callbacks.append(SavePeftModelCallback)
|
230 |
|
|
|
|
|
|
|
231 |
data_collator_kwargs = {
|
232 |
"padding": True,
|
233 |
}
|
|
|
16 |
from transformers import EarlyStoppingCallback, Trainer
|
17 |
from transformers.trainer_pt_utils import get_parameter_names
|
18 |
|
19 |
+
from axolotl.utils.callbacks import (
|
20 |
+
SaveBetterTransformerModelCallback,
|
21 |
+
SavePeftModelCallback,
|
22 |
+
)
|
23 |
from axolotl.utils.schedulers import InterpolatingLogScheduler
|
24 |
|
25 |
|
|
|
231 |
]: # only save in rank 0
|
232 |
callbacks.append(SavePeftModelCallback)
|
233 |
|
234 |
+
if hasattr(model, "use_bettertransformer") and model.use_bettertransformer is True:
|
235 |
+
callbacks.append(SaveBetterTransformerModelCallback)
|
236 |
+
|
237 |
data_collator_kwargs = {
|
238 |
"padding": True,
|
239 |
}
|
src/axolotl/utils/validation.py
CHANGED
@@ -1,8 +1,10 @@
|
|
1 |
"""Module for validating config files"""
|
2 |
|
3 |
import logging
|
|
|
4 |
import torch
|
5 |
|
|
|
6 |
def validate_config(cfg):
|
7 |
if cfg.gradient_accumulation_steps and cfg.batch_size:
|
8 |
raise ValueError(
|
@@ -59,14 +61,20 @@ def validate_config(cfg):
|
|
59 |
|
60 |
if cfg.flash_optimum is True:
|
61 |
if cfg.adapter:
|
62 |
-
logging.warning(
|
|
|
|
|
63 |
if cfg.fp16 or cfg.bf16:
|
64 |
raise ValueError("AMP is not supported with BetterTransformer")
|
65 |
if cfg.float16 is not True:
|
66 |
-
logging.warning(
|
67 |
-
|
|
|
|
|
68 |
logging.warning("torch>=2.0.0 required")
|
69 |
-
raise ValueError(
|
|
|
|
|
70 |
|
71 |
# TODO
|
72 |
# MPT 7b
|
|
|
1 |
"""Module for validating config files"""
|
2 |
|
3 |
import logging
|
4 |
+
|
5 |
import torch
|
6 |
|
7 |
+
|
8 |
def validate_config(cfg):
|
9 |
if cfg.gradient_accumulation_steps and cfg.batch_size:
|
10 |
raise ValueError(
|
|
|
61 |
|
62 |
if cfg.flash_optimum is True:
|
63 |
if cfg.adapter:
|
64 |
+
logging.warning(
|
65 |
+
"BetterTransformers probably doesn't work with PEFT adapters"
|
66 |
+
)
|
67 |
if cfg.fp16 or cfg.bf16:
|
68 |
raise ValueError("AMP is not supported with BetterTransformer")
|
69 |
if cfg.float16 is not True:
|
70 |
+
logging.warning(
|
71 |
+
"You should probably set float16 to true to load the model in float16 for BetterTransformers"
|
72 |
+
)
|
73 |
+
if int(torch.__version__.split(".")[0]) < 2:
|
74 |
logging.warning("torch>=2.0.0 required")
|
75 |
+
raise ValueError(
|
76 |
+
f"flash_optimum for BetterTransformers may not be used with {torch.__version__}"
|
77 |
+
)
|
78 |
|
79 |
# TODO
|
80 |
# MPT 7b
|