|
|
@ -36,7 +36,8 @@ from trl import AutoModelForCausalLMWithValueHead |
|
|
|
from .config import ( |
|
|
|
ModelArguments, |
|
|
|
DataTrainingArguments, |
|
|
|
FinetuningArguments |
|
|
|
FinetuningArguments, |
|
|
|
GeneratingArguments |
|
|
|
) |
|
|
|
|
|
|
|
from .template import Template |
|
|
@ -54,7 +55,8 @@ check_min_version("4.29.1") |
|
|
|
require_version("datasets>=2.12.0", "To fix: pip install datasets>=2.12.0") |
|
|
|
require_version("accelerate>=0.19.0", "To fix: pip install accelerate>=0.19.0") |
|
|
|
require_version("peft>=0.3.0", "To fix: pip install peft>=0.3.0") |
|
|
|
require_version("trl>=0.4.1", "To fix: pip install trl>=0.4.1") |
|
|
|
require_version("trl>=0.4.4", "To fix: pip install trl>=0.4.4") |
|
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__name__) |
|
|
|
|
|
|
@ -91,12 +93,10 @@ def _init_adapter( |
|
|
|
|
|
|
|
if model_args.checkpoint_dir is not None: |
|
|
|
if finetuning_args.finetuning_type != "lora": |
|
|
|
assert is_mergeable and len( |
|
|
|
model_args.checkpoint_dir) == 1, "Only LoRA tuning accepts multiple checkpoints." |
|
|
|
assert is_mergeable and len(model_args.checkpoint_dir) == 1, "Only LoRA tuning accepts multiple checkpoints." |
|
|
|
load_trainable_params(model, model_args.checkpoint_dir[0]) # load model checkpoints for non-peft methods |
|
|
|
else: |
|
|
|
assert is_mergeable or len( |
|
|
|
model_args.checkpoint_dir) == 1, "Quantized model only accepts a single checkpoint." |
|
|
|
assert is_mergeable or len(model_args.checkpoint_dir) == 1, "Quantized model only accepts a single checkpoint." |
|
|
|
|
|
|
|
if finetuning_args.finetuning_type == "lora": |
|
|
|
logger.info("Fine-tuning method: LoRA") |
|
|
@ -106,8 +106,7 @@ def _init_adapter( |
|
|
|
assert os.path.exists(os.path.join(model_args.checkpoint_dir[0], CONFIG_NAME)), \ |
|
|
|
"The given checkpoint is not a LoRA checkpoint, please specify `--finetuning_type full/freeze` instead." |
|
|
|
|
|
|
|
if (is_trainable and model_args.resume_lora_training) or ( |
|
|
|
not is_mergeable): # continually train on the lora weights |
|
|
|
if (is_trainable and model_args.resume_lora_training) or (not is_mergeable): # continually train on the lora weights |
|
|
|
checkpoints_to_merge, lastest_checkpoint = model_args.checkpoint_dir[:-1], model_args.checkpoint_dir[-1] |
|
|
|
else: |
|
|
|
checkpoints_to_merge = model_args.checkpoint_dir |
|
|
@ -186,11 +185,9 @@ def load_pretrained( |
|
|
|
) |
|
|
|
elif model_args.quantization_bit == 4: |
|
|
|
require_version("bitsandbytes>=0.39.0", "To fix: pip install bitsandbytes>=0.39.0") |
|
|
|
require_version("transformers>=4.30.0.dev0", |
|
|
|
"To fix: pip install git+https://github.com/huggingface/transformers.git") |
|
|
|
require_version("transformers>=4.30.1", "To fix: pip install transformers>=4.30.1") |
|
|
|
require_version("accelerate>=0.20.3", "To fix: pip install accelerate>=0.20.3") |
|
|
|
require_version("peft>=0.4.0.dev0", "To fix: pip install git+https://github.com/huggingface/peft.git") |
|
|
|
require_version("accelerate>=0.20.0.dev0", |
|
|
|
"To fix: pip install git+https://github.com/huggingface/accelerate.git") |
|
|
|
config_kwargs["load_in_4bit"] = True |
|
|
|
config_kwargs["quantization_config"] = BitsAndBytesConfig( |
|
|
|
load_in_4bit=True, |
|
|
@ -201,10 +198,10 @@ def load_pretrained( |
|
|
|
else: |
|
|
|
raise NotImplementedError |
|
|
|
is_mergeable = False |
|
|
|
config_kwargs["device_map"] = {"": int(os.environ.get("LOCAL_RANK") or 0)} |
|
|
|
config_kwargs["device_map"] = {"": int(os.environ.get("LOCAL_RANK", "0"))} |
|
|
|
logger.info("Quantizing model to {} bit.".format(model_args.quantization_bit)) |
|
|
|
|
|
|
|
if not is_trainable: |
|
|
|
if not is_trainable: # `device_map=auto` should be used for inference only |
|
|
|
config_kwargs["device_map"] = "auto" |
|
|
|
|
|
|
|
# Load and prepare pretrained models (without valuehead). |
|
|
@ -221,6 +218,13 @@ def load_pretrained( |
|
|
|
if stage == "rm" or stage == "ppo": # add value head |
|
|
|
model = AutoModelForCausalLMWithValueHead.from_pretrained(model) |
|
|
|
|
|
|
|
if stage == "rm" and model_args.checkpoint_dir is not None: # load valuehead weights to evaluate reward model |
|
|
|
load_valuehead_params(model, model_args.checkpoint_dir[0]) |
|
|
|
model.v_head.load_state_dict({ |
|
|
|
"summary.weight": getattr(model, "reward_head_weight"), |
|
|
|
"summary.bias": getattr(model, "reward_head_bias") |
|
|
|
}) |
|
|
|
|
|
|
|
if stage == "ppo": # load reward model |
|
|
|
assert is_trainable, "PPO stage cannot be performed at evaluation." |
|
|
|
assert model_args.reward_model is not None, "Reward model is necessary for PPO training." |
|
|
@ -228,11 +232,6 @@ def load_pretrained( |
|
|
|
model.pretrained_model.load_adapter(model_args.reward_model, "reward", is_trainable=False) |
|
|
|
load_valuehead_params(model, model_args.reward_model) |
|
|
|
|
|
|
|
# Set the parameter _is_int8_training_enabled for the AutoModelForCausalLMWithValueHead model |
|
|
|
# To meet the compliance requirements of the transformers library |
|
|
|
if model_args.quantization_bit is not None: |
|
|
|
model._is_int8_training_enabled = True |
|
|
|
|
|
|
|
if not is_trainable: |
|
|
|
model.requires_grad_(False) # fix all model params |
|
|
|
model = model.half() if model_args.quantization_bit is None else model # cast from fp32 to fp16 |
|
|
@ -245,11 +244,11 @@ def load_pretrained( |
|
|
|
def prepare_args( |
|
|
|
stage: Literal["pt", "sft", "rm", "ppo"] |
|
|
|
) -> Tuple[ModelArguments, DataTrainingArguments, Seq2SeqTrainingArguments, FinetuningArguments]: |
|
|
|
|
|
|
|
parser = HfArgumentParser((ModelArguments, DataTrainingArguments, Seq2SeqTrainingArguments, FinetuningArguments)) |
|
|
|
|
|
|
|
if len(sys.argv) == 2 and sys.argv[1].endswith(".json"): # Provide arguments with a json file. |
|
|
|
model_args, data_args, training_args, finetuning_args = parser.parse_json_file( |
|
|
|
json_file=os.path.abspath(sys.argv[1])) |
|
|
|
model_args, data_args, training_args, finetuning_args = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1])) |
|
|
|
else: |
|
|
|
model_args, data_args, training_args, finetuning_args = parser.parse_args_into_dataclasses() |
|
|
|
|
|
|
@ -313,13 +312,14 @@ def prepare_args( |
|
|
|
return model_args, data_args, training_args, finetuning_args |
|
|
|
|
|
|
|
|
|
|
|
def prepare_infer_args() -> Tuple[ModelArguments, DataTrainingArguments, FinetuningArguments]: |
|
|
|
parser = HfArgumentParser((ModelArguments, DataTrainingArguments, FinetuningArguments)) |
|
|
|
def prepare_infer_args() -> Tuple[ModelArguments, DataTrainingArguments, FinetuningArguments, GeneratingArguments]: |
|
|
|
|
|
|
|
parser = HfArgumentParser((ModelArguments, DataTrainingArguments, FinetuningArguments, GeneratingArguments)) |
|
|
|
|
|
|
|
if len(sys.argv) == 2 and sys.argv[1].endswith(".json"): # Provide arguments with a json file. |
|
|
|
model_args, data_args, finetuning_args = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1])) |
|
|
|
model_args, data_args, finetuning_args, generating_args = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1])) |
|
|
|
else: |
|
|
|
model_args, data_args, finetuning_args = parser.parse_args_into_dataclasses() |
|
|
|
model_args, data_args, finetuning_args, generating_args = parser.parse_args_into_dataclasses() |
|
|
|
|
|
|
|
if model_args.quantization_bit is not None and finetuning_args.finetuning_type != "lora": |
|
|
|
raise ValueError("Quantization is only compatible with the LoRA method.") |
|
|
@ -327,13 +327,14 @@ def prepare_infer_args() -> Tuple[ModelArguments, DataTrainingArguments, Finetun |
|
|
|
if data_args.prompt_template == "alpaca": |
|
|
|
logger.warning("Please specify `prompt_template` if you are using other pre-trained models.") |
|
|
|
|
|
|
|
return model_args, data_args, finetuning_args |
|
|
|
return model_args, data_args, finetuning_args, generating_args |
|
|
|
|
|
|
|
|
|
|
|
def prepare_data( |
|
|
|
model_args: ModelArguments, |
|
|
|
data_args: DataTrainingArguments |
|
|
|
) -> Dataset: |
|
|
|
|
|
|
|
def checksum(file_path, hash): |
|
|
|
with open(file_path, "rb") as datafile: |
|
|
|
binary_data = datafile.read() |
|
|
@ -358,10 +359,12 @@ def prepare_data( |
|
|
|
elif dataset_attr.load_from == "file": |
|
|
|
data_file = os.path.join(data_args.dataset_dir, dataset_attr.file_name) |
|
|
|
extension = dataset_attr.file_name.split(".")[-1] |
|
|
|
|
|
|
|
if dataset_attr.file_sha1 is not None: |
|
|
|
checksum(data_file, dataset_attr.file_sha1) |
|
|
|
else: |
|
|
|
logger.warning("Checksum failed: missing SHA-1 hash value in dataset_info.json.") |
|
|
|
|
|
|
|
raw_datasets = load_dataset( |
|
|
|
extension if extension in ["csv", "json"] else "text", |
|
|
|
data_files=data_file, |
|
|
@ -406,6 +409,7 @@ def preprocess_data( |
|
|
|
training_args: Seq2SeqTrainingArguments, |
|
|
|
stage: Literal["pt", "sft", "rm", "ppo"] |
|
|
|
) -> Dataset: |
|
|
|
|
|
|
|
column_names = list(dataset.column_names) |
|
|
|
prefix = data_args.source_prefix if data_args.source_prefix is not None else "" |
|
|
|
prompt_template = Template(data_args.prompt_template) |
|
|
|