From c98feb840087d461e15a198c8a27705a70ffe861 Mon Sep 17 00:00:00 2001 From: "Ye, Xinyu" Date: Fri, 1 Sep 2023 02:17:57 -0400 Subject: [PATCH 1/3] refined finetuning config. Signed-off-by: Ye, Xinyu --- .../llm/finetuning/finetuning.py | 4 +- .../neural_chat/__init__.py | 7 +++- .../neural_chat/chatbot.py | 8 ++-- .../neural_chat/config.py | 41 ++++++++++++++++++- .../instruction_tuning/finetune_clm.py | 6 +-- .../instruction_tuning/finetune_seq2seq.py | 4 +- .../tests/finetuning/test_finetuning.py | 6 +-- 7 files changed, 60 insertions(+), 16 deletions(-) diff --git a/intel_extension_for_transformers/llm/finetuning/finetuning.py b/intel_extension_for_transformers/llm/finetuning/finetuning.py index b52dc181af0..1d2cf59ceb1 100644 --- a/intel_extension_for_transformers/llm/finetuning/finetuning.py +++ b/intel_extension_for_transformers/llm/finetuning/finetuning.py @@ -54,7 +54,7 @@ import importlib.util from transformers.utils.import_utils import is_optimum_available from .data_utils import preprocess_dataset, ALPACA_PROMPT_DICT -from intel_extension_for_transformers.neural_chat.config import FinetuningConfig +from intel_extension_for_transformers.neural_chat.config import BaseFinetuningConfig def is_optimum_habana_available(): @@ -62,7 +62,7 @@ def is_optimum_habana_available(): class Finetuning: - def __init__(self, finetuning_config: FinetuningConfig): + def __init__(self, finetuning_config: BaseFinetuningConfig): self.model_args, self.data_args, self.training_args, self.finetune_args = ( finetuning_config.model_args, finetuning_config.data_args, diff --git a/intel_extension_for_transformers/neural_chat/__init__.py b/intel_extension_for_transformers/neural_chat/__init__.py index 8b653840426..e3222e0b79d 100644 --- a/intel_extension_for_transformers/neural_chat/__init__.py +++ b/intel_extension_for_transformers/neural_chat/__init__.py @@ -17,7 +17,12 @@ from .config import PipelineConfig from .config import GenerationConfig -from .config import FinetuningConfig +from .config import ( + TextGenerationFinetuningConfig, + SummarizationFinetuningConfig, + CodeGenerationFinetuningConfig, + TTSFinetuningConfig +) from .config import OptimizationConfig from .chatbot import build_chatbot from .chatbot import finetune_model diff --git a/intel_extension_for_transformers/neural_chat/chatbot.py b/intel_extension_for_transformers/neural_chat/chatbot.py index 33256f2e8cc..1e3a20f7138 100644 --- a/intel_extension_for_transformers/neural_chat/chatbot.py +++ b/intel_extension_for_transformers/neural_chat/chatbot.py @@ -20,7 +20,7 @@ from intel_extension_for_transformers.llm.quantization.optimization import Optimization from .config import PipelineConfig from .config import OptimizationConfig -from .config import FinetuningConfig +from .config import BaseFinetuningConfig from .plugins import is_plugin_enabled, get_plugin_instance, get_registered_plugins from .config import DeviceOptions from .models.base_model import get_model_adapter @@ -89,14 +89,14 @@ def build_chatbot(config: PipelineConfig=None): return adapter -def finetune_model(config: FinetuningConfig): +def finetune_model(config: BaseFinetuningConfig): """Finetune the model based on the provided configuration. Args: - config (FinetuningConfig): Configuration for finetuning the model. + config (BaseFinetuningConfig): Configuration for finetuning the model. """ - assert config is not None, "FinetuningConfig is needed for finetuning." + assert config is not None, "BaseFinetuningConfig is needed for finetuning." finetuning = Finetuning(config) finetuning.finetune() diff --git a/intel_extension_for_transformers/neural_chat/config.py b/intel_extension_for_transformers/neural_chat/config.py index d37c7f544f2..e5c817cfe42 100644 --- a/intel_extension_for_transformers/neural_chat/config.py +++ b/intel_extension_for_transformers/neural_chat/config.py @@ -312,12 +312,51 @@ class FinetuningArguments: ) @dataclass -class FinetuningConfig: +class TTSDatasetArguments: + audio_paths: Optional[str] = field(default=None, metadata={"help": "The path of audios."}) + gender: Optional[str] = field(default=None, metadata={"help": "Gender."}) + language: Optional[str] = field(default="English", metadata={"help": "Language."}) + +@dataclass +class TTSModelArguments: + step: int = field(default=0, metadata={"help": "TTS model step."}) + warmup_step: int = field(default=0, metadata={"help": "TTS model warmup step."}) + learning_rate: float = field(default=5e-5, metadata={"help": "Learning rate."}) + +@dataclass +class BaseFinetuningConfig: + model_args: ModelArguments + data_args: DataArguments + training_args: TrainingArguments + finetune_args: FinetuningArguments + +@dataclass +class TextGenerationFinetuningConfig(BaseFinetuningConfig): + model_args: ModelArguments + data_args: DataArguments + training_args: TrainingArguments + finetune_args: FinetuningArguments + +@dataclass +class SummarizationFinetuningConfig(BaseFinetuningConfig): + model_args: ModelArguments + data_args: DataArguments + training_args: TrainingArguments + finetune_args: FinetuningArguments + +@dataclass +class CodeGenerationFinetuningConfig(BaseFinetuningConfig): model_args: ModelArguments data_args: DataArguments training_args: TrainingArguments finetune_args: FinetuningArguments +@dataclass +class TTSFinetuningConfig(BaseFinetuningConfig): + training_args: TrainingArguments + dataset_args: TTSDatasetArguments + model_args: TTSModelArguments + @dataclass class GenerationConfig: device: str = "cpu" diff --git a/intel_extension_for_transformers/neural_chat/examples/instruction_tuning/finetune_clm.py b/intel_extension_for_transformers/neural_chat/examples/instruction_tuning/finetune_clm.py index 494822b53dd..a8c422e3979 100644 --- a/intel_extension_for_transformers/neural_chat/examples/instruction_tuning/finetune_clm.py +++ b/intel_extension_for_transformers/neural_chat/examples/instruction_tuning/finetune_clm.py @@ -22,10 +22,10 @@ ModelArguments, DataArguments, FinetuningArguments, - FinetuningConfig, + TextGenerationFinetuningConfig, ) from intel_extension_for_transformers.neural_chat.chatbot import finetune_model -from intel_extension_for_transformers.neural_chat.pipeline.finetuning.finetuning import is_optimum_habana_available +from intel_extension_for_transformers.llm.finetuning.finetuning import is_optimum_habana_available def main(): # See all possible arguments in src/transformers/training_args.py @@ -56,7 +56,7 @@ def main(): finetune_args, ) = parser.parse_args_into_dataclasses() - finetune_cfg = FinetuningConfig( + finetune_cfg = TextGenerationFinetuningConfig( model_args=model_args, data_args=data_args, training_args=training_args, diff --git a/intel_extension_for_transformers/neural_chat/examples/instruction_tuning/finetune_seq2seq.py b/intel_extension_for_transformers/neural_chat/examples/instruction_tuning/finetune_seq2seq.py index 271e48b2dd6..f96e79e88a2 100644 --- a/intel_extension_for_transformers/neural_chat/examples/instruction_tuning/finetune_seq2seq.py +++ b/intel_extension_for_transformers/neural_chat/examples/instruction_tuning/finetune_seq2seq.py @@ -22,7 +22,7 @@ ModelArguments, DataArguments, FinetuningArguments, - FinetuningConfig, + TextGenerationFinetuningConfig, ) from intel_extension_for_transformers.neural_chat.chatbot import finetune_model @@ -48,7 +48,7 @@ def main(): finetune_args, ) = parser.parse_args_into_dataclasses() - finetune_cfg = FinetuningConfig( + finetune_cfg = TextGenerationFinetuningConfig( model_args=model_args, data_args=data_args, training_args=training_args, diff --git a/intel_extension_for_transformers/neural_chat/tests/finetuning/test_finetuning.py b/intel_extension_for_transformers/neural_chat/tests/finetuning/test_finetuning.py index 68fac625c6e..70bcf0d8ef7 100644 --- a/intel_extension_for_transformers/neural_chat/tests/finetuning/test_finetuning.py +++ b/intel_extension_for_transformers/neural_chat/tests/finetuning/test_finetuning.py @@ -23,7 +23,7 @@ ModelArguments, DataArguments, FinetuningArguments, - FinetuningConfig, + TextGenerationFinetuningConfig, ) from intel_extension_for_transformers.neural_chat.chatbot import finetune_model @@ -57,7 +57,7 @@ def test_finetune_clm(self): overwrite_output_dir=True ) finetune_args = FinetuningArguments() - finetune_cfg = FinetuningConfig( + finetune_cfg = TextGenerationFinetuningConfig( model_args=model_args, data_args=data_args, training_args=training_args, @@ -75,7 +75,7 @@ def test_finetune_seq2seq(self): overwrite_output_dir=True ) finetune_args = FinetuningArguments() - finetune_cfg = FinetuningConfig( + finetune_cfg = TextGenerationFinetuningConfig( model_args=model_args, data_args=data_args, training_args=training_args, From b61d03670f4f2ed1a54de38c53489eccf207aff4 Mon Sep 17 00:00:00 2001 From: "Ye, Xinyu" Date: Fri, 1 Sep 2023 03:26:24 -0400 Subject: [PATCH 2/3] updated readme for new finetuning config. Signed-off-by: Ye, Xinyu --- .../neural_chat/README.md | 77 ++++++++++++++++++- 1 file changed, 75 insertions(+), 2 deletions(-) diff --git a/intel_extension_for_transformers/neural_chat/README.md b/intel_extension_for_transformers/neural_chat/README.md index 65cda31526a..36847984e0d 100644 --- a/intel_extension_for_transformers/neural_chat/README.md +++ b/intel_extension_for_transformers/neural_chat/README.md @@ -122,6 +122,26 @@ We provide multiple plugins to augment the chatbot on top of LLM inference. Our Finetune the pretrained large language model (LLM) with the instruction-following dataset for creating the customized chatbot is very easy for NeuralChat. +### Finetuning for Text Generation Task + +**command line experience** + +```shell +neuralchat finetune --base_model "meta-llama/Llama-2-7b-chat-hf" --config pipeline/finetuning/config/finetuning.yaml +``` + + +**Python API experience** + +```python +>>> from intel_extension_for_transformers.neural_chat import TextGenerationFinetuningConfig +>>> from intel_extension_for_transformers.neural_chat import finetune_model +>>> finetune_cfg = TextGenerationFinetuningConfig() +>>> finetuned_model = finetune_model(finetune_cfg) +``` + +### Finetuning for Summarization Task + **command line experience** ```shell @@ -132,12 +152,65 @@ neuralchat finetune --base_model "meta-llama/Llama-2-7b-chat-hf" --config pipeli **Python API experience** ```python ->>> from intel_extension_for_transformers.neural_chat import FinetuningConfig +>>> from intel_extension_for_transformers.neural_chat import SummarizationFinetuningConfig >>> from intel_extension_for_transformers.neural_chat import finetune_model ->>> finetune_cfg = FinetuningConfig() +>>> finetune_cfg = SummarizationFinetuningConfig() >>> finetuned_model = finetune_model(finetune_cfg) ``` +### Finetuning for Code Generation Task + +**command line experience** + +```shell +neuralchat finetune --base_model "meta-llama/Llama-2-7b-chat-hf" --config pipeline/finetuning/config/finetuning.yaml +``` + + +**Python API experience** + +```python +>>> from intel_extension_for_transformers.neural_chat import CodeGenerationFinetuningConfig +>>> from intel_extension_for_transformers.neural_chat import finetune_model +>>> finetune_cfg = CodeGenerationFinetuningConfig() +>>> finetuned_model = finetune_model(finetune_cfg) +``` + +### Finetuning for Text-to-Speech(TTS) Task + +**command line experience** + +```shell +neuralchat finetune --base_model "meta-llama/Llama-2-7b-chat-hf" --config pipeline/finetuning/config/finetuning.yaml +``` + + +**Python API experience** + +```python +>>> from intel_extension_for_transformers.neural_chat import TTSFinetuningConfig +>>> from intel_extension_for_transformers.neural_chat import finetune_model +>>> finetune_cfg = TTSFinetuningConfig() +>>> finetuned_model = finetune_model(finetune_cfg) +``` + +### Inference with Finetuned Model + +By default, Parameter-Efficient Fine-Tuning (PEFT) methods are used to accelerate the finetuning process, and to reduce the finetuning cost as well. Below shows the way to load the finetuned model of such and inference with it. + +**Python API experience** + +```python +>>> from intel_extension_for_transformers.neural_chat import build_chatbot +>>> from intel_extension_for_transformers.neural_chat.config import PipelineConfig, LoadingModelConfig +>>> chatbot = build_chatbot( + PipelineConfig( + loading_config=LoadingModelConfig(peft_path="/path/to/peft_model") + ) +) +>>> response = chatbot.predict("Tell me about Intel Xeon Scalable Processors.") +``` + ## Quantization NeuralChat provides three quantization approaches respectively (PostTrainingDynamic, PostTrainingStatic, QuantAwareTraining) based on [IntelĀ® Neural Compressor](https://github.com/intel/neural-compressor). From 0e846c5e7dd13c7b0a5b1821624729a078c06210 Mon Sep 17 00:00:00 2001 From: "Ye, Xinyu" Date: Fri, 1 Sep 2023 04:31:14 -0400 Subject: [PATCH 3/3] simplified code. Signed-off-by: Ye, Xinyu --- .../neural_chat/config.py | 21 +++---------------- 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/intel_extension_for_transformers/neural_chat/config.py b/intel_extension_for_transformers/neural_chat/config.py index e5c817cfe42..fac098e2b5c 100644 --- a/intel_extension_for_transformers/neural_chat/config.py +++ b/intel_extension_for_transformers/neural_chat/config.py @@ -330,26 +330,11 @@ class BaseFinetuningConfig: training_args: TrainingArguments finetune_args: FinetuningArguments -@dataclass -class TextGenerationFinetuningConfig(BaseFinetuningConfig): - model_args: ModelArguments - data_args: DataArguments - training_args: TrainingArguments - finetune_args: FinetuningArguments +TextGenerationFinetuningConfig = BaseFinetuningConfig -@dataclass -class SummarizationFinetuningConfig(BaseFinetuningConfig): - model_args: ModelArguments - data_args: DataArguments - training_args: TrainingArguments - finetune_args: FinetuningArguments +SummarizationFinetuningConfig = BaseFinetuningConfig -@dataclass -class CodeGenerationFinetuningConfig(BaseFinetuningConfig): - model_args: ModelArguments - data_args: DataArguments - training_args: TrainingArguments - finetune_args: FinetuningArguments +CodeGenerationFinetuningConfig = BaseFinetuningConfig @dataclass class TTSFinetuningConfig(BaseFinetuningConfig):