peft region:us
---
library_name: peft
---
### Original model

TheBloke/Mistral-7B-Instruct-v0.1-GPTQ

### Train on the dataset

https://huggingface.co/datasets/W1lson/Book3


### Code based on Vasanthengineer4949 GitHub
Thank you Vasan!

### Pip installs

```python
! python -m pip install --upgrade pip
! pip install accelerate peft bitsandbytes pip install git+https://github.com/huggingface/transformers trl py7zr auto-gptq optimum
! pip install -qqq datasets==2.12.0
! pip install scipy
! pip install -qqq loralib==0.1.1
! pip install peft
```

### Finetuning Code and Settings

```python
import torch
from datasets import load_dataset, Dataset
from peft import LoraConfig, AutoPeftModelForCausalLM, prepare_model_for_kbit_training, get_peft_model
from transformers import AutoModelForCausalLM, AutoTokenizer, GPTQConfig, TrainingArguments, Trainer
from trl import SFTTrainer
import os
import pandas as pd
import transformers

def finetune_mistral_7b():
    data_df = pd.read_csv("Book3.csv")

    # Assuming your CSV has "category" and "description" columns
    # Combine "category" and "description" columns into a new "text" column
    data_df["text"] = data_df[["Requirement ID", " Requirement Description"]].apply(
        lambda x: "<human>: What requirement description does the ID" + x["Requirement ID"] + "have?" + "\n<assistant>" + x[" Requirement Description"],
        axis=1
    )
    # Convert the DataFrame into a Hugging Face dataset
    data = Dataset.from_pandas(data_df)
    tokenizer = AutoTokenizer.from_pretrained("TheBloke/Mistral-7B-Instruct-v0.1-GPTQ")
    tokenizer.pad_token = tokenizer.eos_token
    tokenizer.padding_side = 'right'
    quantization_config_loading = GPTQConfig(bits=4, disable_exllama=True, tokenizer=tokenizer)
    model = AutoModelForCausalLM.from_pretrained(
                              "TheBloke/Mistral-7B-Instruct-v0.1-GPTQ",
                              quantization_config=quantization_config_loading,
                              device_map="auto"
                        )
    print(model)
    model.config.use_cache=False
    model.config.pretraining_tp=1
    model.gradient_checkpointing_enable()
    model = prepare_model_for_kbit_training(model)
    
    peft_config = LoraConfig(
        r=16, lora_alpha=16, lora_dropout=0.05, bias="none", task_type="CAUSAL_LM", target_modules=["q_proj", "v_proj"]
    )
    peft_config.inference_mode = False
    
    model = get_peft_model(model, peft_config)
    training_arguments = TrainingArguments(
        output_dir="mistral-finetuned-Book3",
        per_device_train_batch_size=8,
        gradient_accumulation_steps=1,
        optim="paged_adamw_32bit",
        learning_rate=2e-4,
        lr_scheduler_type="cosine",
        save_strategy="epoch",
        logging_steps=100,
        num_train_epochs=1,
        max_steps=250,
        fp16=True,
        push_to_hub=True
    )
    trainer = SFTTrainer(
        model=model,
        train_dataset=data,
        peft_config=peft_config,
        dataset_text_field="text",
        args=training_arguments,
        tokenizer=tokenizer,
        packing=False,
        max_seq_length=512
    )
    trainer.train()
    trainer.push_to_hub()

if __name__ == "__main__":
    finetune_mistral_7b()
```
### Inference
```
from peft import AutoPeftModelForCausalLM
from transformers import GenerationConfig
from transformers import AutoTokenizer
import torch
tokenizer = AutoTokenizer.from_pretrained("mistral-finetuned-samsum")

inputs = tokenizer("<human>: What requirement description does the requirement ID RQMT099 have? \n<assistant>", return_tensors="pt").to("cuda")
#inputs = tokenizer("<human>: What are all the relevant Requirement ID's does the following Requirement Description 'The system must provide role-based access control.' belong to? \n<assistant>", return_tensors="pt").to("cuda")

model = AutoPeftModelForCausalLM.from_pretrained(
    "mistral-finetuned-samsum",
    low_cpu_mem_usage=True,
    return_dict=True,
    torch_dtype=torch.float16,
    device_map="cuda")

generation_config = GenerationConfig(
    do_sample=True,
    top_k=1,
    temperature=0.1,
    max_new_tokens=25,
    pad_token_id=tokenizer.eos_token_id
)
```
```python
import time
st_time = time.time()
outputs = model.generate(**inputs, generation_config=generation_config)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
print(time.time()-st_time)
```
### How to make you're own data set and push to hugging face
```python
import pandas as pd
from datasets import Dataset

# Read CSV file into a Pandas DataFrame
df = pd.read_csv("Book3.csv")

# Create a dictionary from DataFrame columns
data_dict = {
    "Requirement ID": df["Requirement ID"].tolist(),
    " Requirement Description": df[" Requirement Description"].tolist(),
    # Add more columns as needed
}

# Create a Hugging Face Dataset
dataset = Dataset.from_dict(data_dict)

dataset.push_to_hub("W1lson/Book3")
```
## Training procedure


The following `bitsandbytes` quantization config was used during training:
- quant_method: gptq
- bits: 4
- tokenizer: None
- dataset: None
- group_size: 128
- damp_percent: 0.1
- desc_act: True
- sym: True
- true_sequential: True
- use_cuda_fp16: False
- model_seqlen: None
- block_name_to_quantize: None
- module_name_preceding_first_block: None
- batch_size: 1
- pad_token_id: None
- disable_exllama: False
- max_input_length: None

The following `bitsandbytes` quantization config was used during training:
- quant_method: gptq
- bits: 4
- tokenizer: None
- dataset: None
- group_size: 128
- damp_percent: 0.1
- desc_act: True
- sym: True
- true_sequential: True
- use_cuda_fp16: False
- model_seqlen: None
- block_name_to_quantize: None
- module_name_preceding_first_block: None
- batch_size: 1
- pad_token_id: None
- disable_exllama: False
- max_input_length: None

The following `bitsandbytes` quantization config was used during training:
- quant_method: gptq
- bits: 4
- tokenizer: None
- dataset: None
- group_size: 128
- damp_percent: 0.1
- desc_act: True
- sym: True
- true_sequential: True
- use_cuda_fp16: False
- model_seqlen: None
- block_name_to_quantize: None
- module_name_preceding_first_block: None
- batch_size: 1
- pad_token_id: None
- disable_exllama: False
- max_input_length: None

### Framework versions

- PEFT 0.5.0
- PEFT 0.5.0

- PEFT 0.5.0
