更新.gitignore以排除rsync.sh,修改TODO列表,重命名evaluate脚本,删除run.sh,添加持续学习的参数类,更新训练和评估脚本以支持新的数据集逻辑
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
from accelerate import Accelerator, DataLoaderConfiguration
|
||||
|
||||
|
||||
def create_accelerator_and_postprocess(args):
|
||||
# We explicitly don't rely on the `Accelerator` to do gradient accumulation
|
||||
grad_acc_kwargs = {}
|
||||
if args.accelerator_config.gradient_accumulation_kwargs is not None:
|
||||
grad_acc_kwargs = args.accelerator_config.gradient_accumulation_kwargs
|
||||
|
||||
# check if num_steps is attempted to be passed in gradient_accumulation_kwargs
|
||||
if "num_steps" in grad_acc_kwargs:
|
||||
if args.gradient_accumulation_steps > 1:
|
||||
# raise because we do not know which setting is intended.
|
||||
raise ValueError(
|
||||
"The `AcceleratorConfig`'s `num_steps` is set but `gradient_accumulation_steps` is greater than 1 in the passed `TrainingArguments`"
|
||||
"If using the passed `AcceleratorConfig` is desired, do not set the `TrainingArguments` `gradient_accumulation_steps`."
|
||||
)
|
||||
else:
|
||||
args.gradient_accumulation_steps = grad_acc_kwargs["num_steps"]
|
||||
|
||||
accelerator_config = args.accelerator_config.to_dict()
|
||||
|
||||
dataloader_config = DataLoaderConfiguration(
|
||||
split_batches=accelerator_config.pop("split_batches"),
|
||||
dispatch_batches=accelerator_config.pop("dispatch_batches"),
|
||||
even_batches=accelerator_config.pop("even_batches"),
|
||||
use_seedable_sampler=accelerator_config.pop("use_seedable_sampler"),
|
||||
)
|
||||
dataloader_config.data_seed = args.data_seed
|
||||
|
||||
non_blocking = accelerator_config.pop("non_blocking")
|
||||
dataloader_config.non_blocking = non_blocking
|
||||
# this would have been updated above, no need for it anymore
|
||||
accelerator_config.pop("gradient_accumulation_kwargs")
|
||||
|
||||
accelerator_args = {
|
||||
"deepspeed_plugin": args.deepspeed_plugin,
|
||||
}
|
||||
accelerator_args["dataloader_config"] = dataloader_config
|
||||
# create accelerator object
|
||||
accelerator = Accelerator(**accelerator_args)
|
||||
|
||||
# deepspeed and accelerate flags covering both trainer args and accelerate launcher
|
||||
is_deepspeed_enabled = (
|
||||
getattr(accelerator.state, "deepspeed_plugin", None) is not None
|
||||
)
|
||||
is_fsdp_enabled = getattr(accelerator.state, "fsdp_plugin", None) is not None
|
||||
|
||||
# post accelerator creation setup
|
||||
if is_fsdp_enabled:
|
||||
fsdp_plugin = accelerator.state.fsdp_plugin
|
||||
fsdp_plugin.limit_all_gathers = args.fsdp_config.get(
|
||||
"limit_all_gathers", fsdp_plugin.limit_all_gathers
|
||||
)
|
||||
fsdp_plugin.activation_checkpointing = args.fsdp_config.get(
|
||||
"activation_checkpointing", fsdp_plugin.activation_checkpointing
|
||||
)
|
||||
if fsdp_plugin.activation_checkpointing and args.gradient_checkpointing:
|
||||
raise ValueError(
|
||||
"The activation_checkpointing in FSDP config and the gradient_checkpointing in training arg "
|
||||
"can't be set to True simultaneously. Please use FSDP's activation_checkpointing logic "
|
||||
"when using FSDP."
|
||||
)
|
||||
|
||||
def propagate_args_to_deepspeed(auto_find_batch_size=False):
|
||||
from transformers.integrations.deepspeed import HfTrainerDeepSpeedConfig
|
||||
|
||||
ds_plugin = accelerator.state.deepspeed_plugin
|
||||
|
||||
ds_plugin.hf_ds_config = HfTrainerDeepSpeedConfig(ds_plugin.hf_ds_config.config)
|
||||
ds_plugin.deepspeed_config = ds_plugin.hf_ds_config.config
|
||||
ds_plugin.hf_ds_config.trainer_config_process(args, auto_find_batch_size)
|
||||
|
||||
if is_deepspeed_enabled and getattr(args, "hf_deepspeed_config", None) is None:
|
||||
propagate_args_to_deepspeed()
|
||||
return accelerator
|
||||
@@ -0,0 +1,17 @@
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Optional
|
||||
|
||||
|
||||
@dataclass
|
||||
class ContinualScriptArguments:
|
||||
"""Script arguments for continual learning."""
|
||||
|
||||
dataset_name: list[str] = field(
|
||||
default_factory=lambda: ["cifar10", "cifar100", "imagenet2012"]
|
||||
)
|
||||
dataset_config: Optional[str] = None
|
||||
dataset_train_split: str = "train"
|
||||
dataset_test_split: str = "test"
|
||||
dataset_generation_split: str = "generation"
|
||||
gradient_checkpointing_use_reentrant: bool = False
|
||||
ignore_bias_buffers: bool = False
|
||||
@@ -1,9 +1,9 @@
|
||||
# _________________________________________________________
|
||||
|
||||
from trl import SFTTrainer
|
||||
from transformers import Trainer
|
||||
|
||||
|
||||
class ContinualTrainer(SFTTrainer):
|
||||
class ContinualTrainer(Trainer):
|
||||
def __init__(
|
||||
self, model, args, data_collator, train_dataset, eval_dataset, accelerator
|
||||
):
|
||||
@@ -19,6 +19,7 @@ class ContinualTrainer(SFTTrainer):
|
||||
self.is_fsdp_enabled = (
|
||||
getattr(self.accelerator.state, "fsdp_plugin", None) is not None
|
||||
)
|
||||
self.gather_function = self.accelerator.gather_for_metrics
|
||||
return
|
||||
else:
|
||||
super().create_accelerator_and_postprocess()
|
||||
Reference in New Issue
Block a user