From e50ea32f2b5d30c9c248dced1b614f597f5d128c Mon Sep 17 00:00:00 2001 From: nikhilchintawar Date: Mon, 2 Oct 2023 11:58:22 +0530 Subject: [PATCH 1/4] Add base for fine tuning --- genai_stack/fine_tune/__init__.py | 0 genai_stack/fine_tune/base.py | 40 +++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 genai_stack/fine_tune/__init__.py create mode 100644 genai_stack/fine_tune/base.py diff --git a/genai_stack/fine_tune/__init__.py b/genai_stack/fine_tune/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/genai_stack/fine_tune/base.py b/genai_stack/fine_tune/base.py new file mode 100644 index 00000000..231969b3 --- /dev/null +++ b/genai_stack/fine_tune/base.py @@ -0,0 +1,40 @@ +from pydantic import BaseModel as PydanticBaseModel + + +class HyperParametersConfigModel(PydanticBaseModel): + pass + + +class BaseFineTuneConfigModel(PydanticBaseModel): + """ + Data Model for the configs + """ + + instruction: str = "text-summarization" + hyperparameters: HyperParametersConfigModel + + +class BaseFineTune: + """ + Class which provides base structure for fine tuning models + """ + + config_class = BaseFineTuneConfigModel + + def __init__(self, **kwargs) -> None: + self.config = self.config_class(**kwargs) + + def load_model(self): + pass + + def load_dataset(self): + pass + + def train(self): + pass + + def tune(self): + pass + + def evaluate(self): + pass From 4914a21a7fc1e13bb166e14aa4f33286e16c6700 Mon Sep 17 00:00:00 2001 From: nikhilchintawar Date: Mon, 2 Oct 2023 12:07:20 +0530 Subject: [PATCH 2/4] Add llama2 7b base --- genai_stack/fine_tune/llama27b.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 genai_stack/fine_tune/llama27b.py diff --git a/genai_stack/fine_tune/llama27b.py b/genai_stack/fine_tune/llama27b.py new file mode 100644 index 00000000..4661929d --- /dev/null +++ b/genai_stack/fine_tune/llama27b.py @@ -0,0 +1,11 @@ +from genai_stack.fine_tune.base import BaseFineTune, BaseFineTuneConfigModel + + +class LLama27bConfigModel(BaseFineTuneConfigModel): + pass + + +class LLama27bFineTune(BaseFineTune): + config_class = LLama27bConfigModel + + pass From 54b22990a2bd301a9cbd2cb4b390a36f7e341096 Mon Sep 17 00:00:00 2001 From: nikhilchintawar Date: Mon, 2 Oct 2023 13:47:35 +0530 Subject: [PATCH 3/4] Add load_model method --- genai_stack/fine_tune/base.py | 2 +- genai_stack/fine_tune/llama27b.py | 29 +++++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/genai_stack/fine_tune/base.py b/genai_stack/fine_tune/base.py index 231969b3..a87f74ad 100644 --- a/genai_stack/fine_tune/base.py +++ b/genai_stack/fine_tune/base.py @@ -11,7 +11,7 @@ class BaseFineTuneConfigModel(PydanticBaseModel): """ instruction: str = "text-summarization" - hyperparameters: HyperParametersConfigModel + # hyperparameters: HyperParametersConfigModel class BaseFineTune: diff --git a/genai_stack/fine_tune/llama27b.py b/genai_stack/fine_tune/llama27b.py index 4661929d..7cf337f1 100644 --- a/genai_stack/fine_tune/llama27b.py +++ b/genai_stack/fine_tune/llama27b.py @@ -1,11 +1,36 @@ +from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, AutoTokenizer +import torch + from genai_stack.fine_tune.base import BaseFineTune, BaseFineTuneConfigModel class LLama27bConfigModel(BaseFineTuneConfigModel): - pass + model_name: str = "daryl149/llama-2-7b-chat-hf" class LLama27bFineTune(BaseFineTune): config_class = LLama27bConfigModel - pass + def load_model(self, model_name: str): + bnb_config = BitsAndBytesConfig( + load_in_4bit=True, + bnb_4bit_use_double_quant=True, + bnb_4bit_quant_type="nf4", + bnb_4bit_compute_dtype=torch.bfloat16, + ) + torch.cuda.get_device_capability() + device_map = "auto" + + model = AutoModelForCausalLM.from_pretrained( + model_name, + quantization_config=bnb_config, + device_map=device_map, + ) + model.config.pretraining_tp = 1 + + tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True, use_fast=True) + tokenizer.pad_token = tokenizer + torch.cuda.empty_cache() + print("model", model) + print("tokenizer", tokenizer) + return model, tokenizer From 3fe12e9e4632cbdd99ecc12b5e3ec9afd0d9e247 Mon Sep 17 00:00:00 2001 From: akshaj000 Date: Mon, 2 Oct 2023 15:16:51 +0530 Subject: [PATCH 4/4] Add load dataset method --- genai_stack/fine_tune/llama27b.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/genai_stack/fine_tune/llama27b.py b/genai_stack/fine_tune/llama27b.py index 7cf337f1..65aaed1e 100644 --- a/genai_stack/fine_tune/llama27b.py +++ b/genai_stack/fine_tune/llama27b.py @@ -1,11 +1,14 @@ +from datasets import load_dataset from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, AutoTokenizer import torch from genai_stack.fine_tune.base import BaseFineTune, BaseFineTuneConfigModel +import ray class LLama27bConfigModel(BaseFineTuneConfigModel): model_name: str = "daryl149/llama-2-7b-chat-hf" + dataset: str class LLama27bFineTune(BaseFineTune): @@ -34,3 +37,12 @@ def load_model(self, model_name: str): print("model", model) print("tokenizer", tokenizer) return model, tokenizer + + def load_dataset(self): + datasets = load_dataset(self.config.dataset) + ray_datasets = { + "train": ray.data.from_huggingface(datasets["train"]), + "validation": ray.data.from_huggingface(datasets["validation"]), + "test": ray.data.from_huggingface(datasets["test"]), + } + return ray_datasets