Skip to content

Commit

Permalink
added sai model
Browse files Browse the repository at this point in the history
  • Loading branch information
vanbujm committed Sep 24, 2024
1 parent eb33b84 commit f6ec0d2
Show file tree
Hide file tree
Showing 9 changed files with 91,566 additions and 0 deletions.
147 changes: 147 additions & 0 deletions src/trian/train_sai.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import os
from dotenv import load_dotenv
from datasets import load_dataset
from unsloth import FastMistralModel
from trl import SFTTrainer
from transformers import TrainingArguments
from huggingface_hub import login
import json
import random
from datasets import Dataset
from datasets import concatenate_datasets
import torch

load_dotenv()

HUGGING_FACE_ACCESS_TOKEN = os.getenv('HUGGING_FACE_ACCESS_TOKEN')
login()

max_seq_length = 4096 # Can change to whatever number <= 4096
dtype = None # None for auto detection. Float16 for Tesla T4, V100, Bfloat16 for Ampere+
load_in_4bit = True # Use 4bit quantization to reduce memory usage. Can be False.

model, tokenizer = FastMistralModel.from_pretrained(
model_name="mistralai/Mistral-7B-Instruct-v0.1", # You can change this to any Llama model!
max_seq_length=max_seq_length,
dtype=dtype,
load_in_4bit=load_in_4bit,
# trust_remote_code=True,
token=HUGGING_FACE_ACCESS_TOKEN,
)
model = FastMistralModel.get_peft_model(
model,
r=16,
target_modules=["q_proj", "k_proj", "v_proj", "o_proj",
"gate_proj", "up_proj", "down_proj", ],
lora_alpha=16,
lora_dropout=0, # Currently only supports dropout = 0
bias="none", # Currently only supports bias = "none"
use_gradient_checkpointing=True,
random_state=3407,
max_seq_length=max_seq_length,
)

dataset = load_dataset("HuggingFaceH4/ultrachat_200k", token=HUGGING_FACE_ACCESS_TOKEN)

ultrachat_total_rows = len(dataset["train_sft"])

sai_data = []
with open('data/sai-processed.jsonl') as f:
for line in f:
sai_obj = json.loads(line)
sai_data.append({
"prompt": sai_obj["initialPrompt"],
"messages": [
{"content": sai_obj["initialPrompt"], "role": "user"},
{"content": sai_obj["revisionResponse"], "role": "assistant"}
]
})

sai_data = Dataset.from_list(sai_data)

sai_data = sai_data.shuffle(seed=42)

train_sft = dataset["train_sft"]
train_sft = train_sft.shuffle(seed=42)
test_sft = dataset["test_sft"]
test_sft = test_sft.shuffle(seed=42)

# i choose 50k sample for training and 2k for test
train_sft_subset = train_sft.select(range(150000))
test_sft_subset = test_sft.select(range(3000))

# Get the same proportion of the SAI data as the Ultrachat data
sai_train_sft = sai_data.select(range(int(len(train_sft_subset)/ultrachat_total_rows * len(sai_data))))
sai_test_sft = sai_data.select(range(int(len(test_sft_subset)/ultrachat_total_rows * len(sai_data))))

print("Ultrachat Total Rows: ", ultrachat_total_rows)
print("Ultrachat Train Length: ", len(train_sft_subset))
print("Ultrachat Test Length: ", len(test_sft_subset))
print("SAI Data Length: ", len(sai_data))
print("SAI Train Length: ", len(sai_train_sft))
print("SAI Test Length: ", len(sai_test_sft))

train_sft_subset_len = len(train_sft_subset) - len(sai_train_sft)
train_sft_subset = concatenate_datasets([train_sft_subset.select(range(train_sft_subset_len)), sai_train_sft])

test_sft_subset_len = len(test_sft_subset) - len(sai_test_sft)
test_sft_subset = concatenate_datasets([test_sft_subset.select(range(test_sft_subset_len)), sai_test_sft])

print("Combined Train Length: ", len(train_sft_subset))
print("Combined Test Length: ", len(test_sft_subset))

def formatting_func(example):
formatted_messages = []

for message in example['messages']:
content = message['content']
role = message['role']
formatted_message = {"role": role, "content": content}
formatted_messages.append(formatted_message)

return {"text": "\n".join([str(msg) for msg in formatted_messages])}

train_sft = train_sft_subset.map(formatting_func)
test_sft = test_sft_subset.map(formatting_func)


HAS_BFLOAT16 = torch.cuda.is_bf16_supported()
learning_rate = 1e-4
weight_decay = 0.01
warmup_steps = 10
lr_scheduler_type = "linear"
optimizer = "adamw_8bit"
random_state = 3407

argument = TrainingArguments(
per_device_train_batch_size = 1,
gradient_accumulation_steps = 4,
warmup_steps = warmup_steps,
max_steps = 240,
learning_rate = learning_rate,
fp16 = not HAS_BFLOAT16,
bf16 = HAS_BFLOAT16,
logging_steps = 1,
output_dir = "outputs",
optim = optimizer,
weight_decay = weight_decay,
lr_scheduler_type = lr_scheduler_type,
seed = random_state,
report_to = "none",
)

trainer = SFTTrainer(
model = model,
train_dataset=train_sft,
eval_dataset=test_sft,
dataset_text_field = "text",
max_seq_length = max_seq_length,
args = argument,
)

trainer.train()

trainer.save_model("./ultrachat_sai")

trainer.push_to_hub("vanbujm/ultrachat_sai")
tokenizer.push_to_hub("vanbujm/ultrachat_sai")
200 changes: 200 additions & 0 deletions ultrachat_sai/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
---
library_name: transformers
tags:
- unsloth
---

# Model Card for Model ID

<!-- Provide a quick summary of what the model is/does. -->



## Model Details

### Model Description

<!-- Provide a longer summary of what this model is. -->

This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.

- **Developed by:** [More Information Needed]
- **Funded by [optional]:** [More Information Needed]
- **Shared by [optional]:** [More Information Needed]
- **Model type:** [More Information Needed]
- **Language(s) (NLP):** [More Information Needed]
- **License:** [More Information Needed]
- **Finetuned from model [optional]:** [More Information Needed]

### Model Sources [optional]

<!-- Provide the basic links for the model. -->

- **Repository:** [More Information Needed]
- **Paper [optional]:** [More Information Needed]
- **Demo [optional]:** [More Information Needed]

## Uses

<!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->

### Direct Use

<!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->

[More Information Needed]

### Downstream Use [optional]

<!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->

[More Information Needed]

### Out-of-Scope Use

<!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->

[More Information Needed]

## Bias, Risks, and Limitations

<!-- This section is meant to convey both technical and sociotechnical limitations. -->

[More Information Needed]

### Recommendations

<!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->

Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.

## How to Get Started with the Model

Use the code below to get started with the model.

[More Information Needed]

## Training Details

### Training Data

<!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->

[More Information Needed]

### Training Procedure

<!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->

#### Preprocessing [optional]

[More Information Needed]


#### Training Hyperparameters

- **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->

#### Speeds, Sizes, Times [optional]

<!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->

[More Information Needed]

## Evaluation

<!-- This section describes the evaluation protocols and provides the results. -->

### Testing Data, Factors & Metrics

#### Testing Data

<!-- This should link to a Dataset Card if possible. -->

[More Information Needed]

#### Factors

<!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->

[More Information Needed]

#### Metrics

<!-- These are the evaluation metrics being used, ideally with a description of why. -->

[More Information Needed]

### Results

[More Information Needed]

#### Summary



## Model Examination [optional]

<!-- Relevant interpretability work for the model goes here -->

[More Information Needed]

## Environmental Impact

<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->

Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).

- **Hardware Type:** [More Information Needed]
- **Hours used:** [More Information Needed]
- **Cloud Provider:** [More Information Needed]
- **Compute Region:** [More Information Needed]
- **Carbon Emitted:** [More Information Needed]

## Technical Specifications [optional]

### Model Architecture and Objective

[More Information Needed]

### Compute Infrastructure

[More Information Needed]

#### Hardware

[More Information Needed]

#### Software

[More Information Needed]

## Citation [optional]

<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->

**BibTeX:**

[More Information Needed]

**APA:**

[More Information Needed]

## Glossary [optional]

<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->

[More Information Needed]

## More Information [optional]

[More Information Needed]

## Model Card Authors [optional]

[More Information Needed]

## Model Card Contact

[More Information Needed]
34 changes: 34 additions & 0 deletions ultrachat_sai/adapter_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"alpha_pattern": {},
"auto_mapping": null,
"base_model_name_or_path": "mistralai/Mistral-7B-Instruct-v0.1",
"bias": "none",
"fan_in_fan_out": false,
"inference_mode": true,
"init_lora_weights": true,
"layer_replication": null,
"layers_pattern": null,
"layers_to_transform": null,
"loftq_config": {},
"lora_alpha": 16,
"lora_dropout": 0,
"megatron_config": null,
"megatron_core": "megatron.core",
"modules_to_save": null,
"peft_type": "LORA",
"r": 16,
"rank_pattern": {},
"revision": null,
"target_modules": [
"o_proj",
"v_proj",
"k_proj",
"up_proj",
"gate_proj",
"q_proj",
"down_proj"
],
"task_type": "CAUSAL_LM",
"use_dora": false,
"use_rslora": false
}
3 changes: 3 additions & 0 deletions ultrachat_sai/adapter_model.safetensors
Git LFS file not shown
Loading

0 comments on commit f6ec0d2

Please sign in to comment.