|
| 1 | +# |
| 2 | +# Copyright 2023 The LLM-on-Ray Authors. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | +import torch |
| 17 | +import transformers |
| 18 | +from peft import LoraConfig |
| 19 | +from transformers import AutoModelForCausalLM |
| 20 | +from typing import Dict |
| 21 | + |
| 22 | +from llm_on_ray.finetune.data_preprocess import DPOIntelOrcaPreprocesser |
| 23 | +from itertools import chain |
| 24 | + |
| 25 | +from llm_on_ray.finetune.finetuning import Finetuning |
| 26 | + |
| 27 | +IGNORE_INDEX = -100 |
| 28 | + |
| 29 | + |
| 30 | +class DPOFineTuning(Finetuning): |
| 31 | + def tokenize_dataset(self, config: Dict, tokenizer, dataset): |
| 32 | + print("tokenize_dataset") |
| 33 | + print(dataset) |
| 34 | + config["Dataset"].get("group", True) |
| 35 | + config["Dataset"].get("block_size", 512) |
| 36 | + tokenizer.pad_token = tokenizer.eos_token |
| 37 | + tokenized_dataset = DPOIntelOrcaPreprocesser.tokenize_dataset(config, tokenizer, dataset) |
| 38 | + print(tokenized_dataset) |
| 39 | + return tokenized_dataset |
| 40 | + |
| 41 | + def load_model(self, config: Dict): |
| 42 | + model_name = config["General"]["base_model"] |
| 43 | + model_dtype = self.convert_dtype(config["Training"].get("mixed_precision", "no")) |
| 44 | + model_config = config["General"].get("config", {}) |
| 45 | + model = transformers.AutoModelForCausalLM.from_pretrained( |
| 46 | + model_name, torch_dtype=model_dtype, **model_config |
| 47 | + ) |
| 48 | + |
| 49 | + egc = config["General"].get("enable_gradient_checkpointing", False) |
| 50 | + if egc: |
| 51 | + model.enable_input_require_grads() |
| 52 | + model.gradient_checkpointing_enable() |
| 53 | + model.config.use_cache = False |
| 54 | + |
| 55 | + model.to(dtype=model_dtype, device=torch.device(config["Training"]["device"])) |
| 56 | + |
| 57 | + return model |
| 58 | + |
| 59 | + def load_model_ref(self, config: Dict): |
| 60 | + model_name = config["General"]["base_model"] |
| 61 | + model_dtype = self.convert_dtype(config["Training"].get("mixed_precision", "no")) |
| 62 | + model_config = config["General"].get("config", {}) |
| 63 | + |
| 64 | + # load reference model |
| 65 | + model_ref = transformers.AutoModelForCausalLM.from_pretrained( |
| 66 | + model_name, torch_dtype=model_dtype, **model_config |
| 67 | + ) |
| 68 | + |
| 69 | + model_ref.config.use_cache = False |
| 70 | + model_ref.to(dtype=model_dtype, device=torch.device(config["Training"]["device"])) |
| 71 | + |
| 72 | + return model_ref |
| 73 | + |
| 74 | + def get_trainer(self, config: Dict, model, tokenizer, tokenized_dataset, data_collator): |
| 75 | + device = config["Training"]["device"] |
| 76 | + lora_config = config["General"].get("lora_config", None) |
| 77 | + |
| 78 | + if device in ["cpu", "gpu"]: |
| 79 | + from transformers import Trainer, TrainingArguments |
| 80 | + from trl import DPOTrainer |
| 81 | + |
| 82 | + training_args = self.convert_to_training_args(TrainingArguments, config) |
| 83 | + |
| 84 | + trainer = DPOTrainer( |
| 85 | + model, |
| 86 | + self.load_model_ref(config) if lora_config is not None else None, |
| 87 | + args=training_args, |
| 88 | + beta=config["Training"].get("beta"), |
| 89 | + train_dataset=tokenized_dataset["train"], |
| 90 | + eval_dataset=tokenized_dataset["validation"] |
| 91 | + if tokenized_dataset.get("validation") is not None |
| 92 | + else None, |
| 93 | + tokenizer=tokenizer, |
| 94 | + peft_config=LoraConfig(**lora_config) if lora_config is not None else None, |
| 95 | + max_length=config["Dataset"].get("max_length"), |
| 96 | + max_prompt_length=config["Dataset"].get("max_prompt_length"), |
| 97 | + ) |
| 98 | + elif device in ["hpu"]: |
| 99 | + from optimum.habana.trl import GaudiDPOTrainer as DPOTrainer |
| 100 | + from optimum.habana.transformers import GaudiTrainingArguments |
| 101 | + from optimum.habana import GaudiConfig |
| 102 | + |
| 103 | + # If gaudi_config_name is provided, load gaudi_config from huggingface model hub(https://huggingface.co/Habana), otherwise use default gaudi_config |
| 104 | + gaudi_config_name = config["General"].get("gaudi_config_name", None) |
| 105 | + if gaudi_config_name is not None: |
| 106 | + gaudi_config = GaudiConfig.from_pretrained(gaudi_config_name) |
| 107 | + else: |
| 108 | + gaudi_config = GaudiConfig() |
| 109 | + gaudi_config.use_fused_adam = True |
| 110 | + gaudi_config.use_fused_clip_norm = True |
| 111 | + |
| 112 | + training_args = self.convert_to_training_args(GaudiTrainingArguments, config) |
| 113 | + trainer = DPOTrainer( |
| 114 | + model, |
| 115 | + self.load_model_ref(config) if lora_config is not None else None, |
| 116 | + args=training_args, |
| 117 | + gaudi_config=gaudi_config, |
| 118 | + beta=config["Training"].get("beta"), |
| 119 | + train_dataset=tokenized_dataset["train"], |
| 120 | + eval_dataset=tokenized_dataset["validation"] |
| 121 | + if tokenized_dataset.get("validation") is not None |
| 122 | + else None, |
| 123 | + tokenizer=tokenizer, |
| 124 | + peft_config=LoraConfig(**lora_config) if lora_config is not None else None, |
| 125 | + max_length=config["Dataset"].get("max_length"), |
| 126 | + max_prompt_length=config["Dataset"].get("max_prompt_length"), |
| 127 | + ) |
| 128 | + |
| 129 | + return training_args, trainer |
0 commit comments