shahidul034
commited on
Update README.md
Browse files
README.md
CHANGED
@@ -3,73 +3,64 @@ library_name: transformers
|
|
3 |
tags: []
|
4 |
---
|
5 |
|
6 |
-
# Model Card for Model ID
|
7 |
-
|
8 |
-
<!-- Provide a quick summary of what the model is/does. -->
|
9 |
-
|
10 |
-
|
11 |
|
12 |
## Model Details
|
13 |
|
14 |
### Model Description
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.
|
19 |
-
|
20 |
-
- **Developed by:** [More Information Needed]
|
21 |
-
- **Funded by [optional]:** [More Information Needed]
|
22 |
-
- **Shared by [optional]:** [More Information Needed]
|
23 |
-
- **Model type:** [More Information Needed]
|
24 |
-
- **Language(s) (NLP):** [More Information Needed]
|
25 |
-
- **License:** [More Information Needed]
|
26 |
-
- **Finetuned from model [optional]:** [More Information Needed]
|
27 |
-
|
28 |
-
### Model Sources [optional]
|
29 |
-
|
30 |
-
<!-- Provide the basic links for the model. -->
|
31 |
-
|
32 |
-
- **Repository:** [More Information Needed]
|
33 |
-
- **Paper [optional]:** [More Information Needed]
|
34 |
-
- **Demo [optional]:** [More Information Needed]
|
35 |
|
36 |
-
|
|
|
|
|
|
|
37 |
|
38 |
-
<!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
|
39 |
-
|
40 |
-
### Direct Use
|
41 |
-
|
42 |
-
<!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
|
43 |
-
|
44 |
-
[More Information Needed]
|
45 |
-
|
46 |
-
### Downstream Use [optional]
|
47 |
-
|
48 |
-
<!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
|
49 |
-
|
50 |
-
[More Information Needed]
|
51 |
-
|
52 |
-
### Out-of-Scope Use
|
53 |
-
|
54 |
-
<!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
|
55 |
-
|
56 |
-
[More Information Needed]
|
57 |
-
|
58 |
-
## Bias, Risks, and Limitations
|
59 |
-
|
60 |
-
<!-- This section is meant to convey both technical and sociotechnical limitations. -->
|
61 |
-
|
62 |
-
[More Information Needed]
|
63 |
-
|
64 |
-
### Recommendations
|
65 |
-
|
66 |
-
<!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
|
67 |
-
|
68 |
-
Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
|
69 |
|
70 |
## How to Get Started with the Model
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
[More Information Needed]
|
75 |
|
@@ -77,13 +68,132 @@ Use the code below to get started with the model.
|
|
77 |
|
78 |
### Training Data
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
[More Information Needed]
|
83 |
|
84 |
### Training Procedure
|
85 |
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
#### Preprocessing [optional]
|
89 |
|
@@ -92,7 +202,17 @@ Use the code below to get started with the model.
|
|
92 |
|
93 |
#### Training Hyperparameters
|
94 |
|
95 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
#### Speeds, Sizes, Times [optional]
|
98 |
|
@@ -108,7 +228,7 @@ Use the code below to get started with the model.
|
|
108 |
|
109 |
#### Testing Data
|
110 |
|
111 |
-
|
112 |
|
113 |
[More Information Needed]
|
114 |
|
@@ -128,15 +248,6 @@ Use the code below to get started with the model.
|
|
128 |
|
129 |
[More Information Needed]
|
130 |
|
131 |
-
#### Summary
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
## Model Examination [optional]
|
136 |
-
|
137 |
-
<!-- Relevant interpretability work for the model goes here -->
|
138 |
-
|
139 |
-
[More Information Needed]
|
140 |
|
141 |
## Environmental Impact
|
142 |
|
@@ -144,58 +255,17 @@ Use the code below to get started with the model.
|
|
144 |
|
145 |
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).
|
146 |
|
147 |
-
- **
|
148 |
-
- **Hours used:** [More Information Needed]
|
149 |
-
- **Cloud Provider:** [More Information Needed]
|
150 |
-
- **Compute Region:** [More Information Needed]
|
151 |
-
- **Carbon Emitted:** [More Information Needed]
|
152 |
-
|
153 |
-
## Technical Specifications [optional]
|
154 |
|
155 |
-
### Model Architecture and Objective
|
156 |
-
|
157 |
-
[More Information Needed]
|
158 |
-
|
159 |
-
### Compute Infrastructure
|
160 |
-
|
161 |
-
[More Information Needed]
|
162 |
|
163 |
#### Hardware
|
|
|
164 |
|
165 |
-
[More Information Needed]
|
166 |
-
|
167 |
-
#### Software
|
168 |
-
|
169 |
-
[More Information Needed]
|
170 |
-
|
171 |
-
## Citation [optional]
|
172 |
-
|
173 |
-
<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
|
174 |
-
|
175 |
-
**BibTeX:**
|
176 |
-
|
177 |
-
[More Information Needed]
|
178 |
-
|
179 |
-
**APA:**
|
180 |
|
181 |
-
[More Information Needed]
|
182 |
|
183 |
-
## Glossary [optional]
|
184 |
|
185 |
-
<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
|
186 |
|
187 |
-
[More Information Needed]
|
188 |
|
189 |
-
## More Information [optional]
|
190 |
|
191 |
-
[More Information Needed]
|
192 |
-
|
193 |
-
## Model Card Authors [optional]
|
194 |
-
|
195 |
-
[More Information Needed]
|
196 |
-
|
197 |
-
## Model Card Contact
|
198 |
-
|
199 |
-
[More Information Needed]
|
200 |
|
201 |
|
|
|
3 |
tags: []
|
4 |
---
|
5 |
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
## Model Details
|
8 |
|
9 |
### Model Description
|
10 |
|
11 |
+
This model is created for answering the KUET(Khulna University of Engineering & Technology) information.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
- **Developed by:** Md. Shahidul Salim
|
14 |
+
- **Model type:** Question answering
|
15 |
+
- **Language(s) (NLP):** English
|
16 |
+
- **Finetuned from model:** mistralai/Mistral-7B-Instruct-v0.1
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
## How to Get Started with the Model
|
20 |
+
```
|
21 |
+
import transformers
|
22 |
+
from transformers import AutoTokenizer
|
23 |
+
model_name="shahidul034/KUET_LLM_Mistral"
|
24 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
25 |
+
model = transformers.AutoModelForCausalLM.from_pretrained(model_name)
|
26 |
+
pipe = pipeline("text-generation",
|
27 |
+
model=full_output,
|
28 |
+
tokenizer= tokenizer,
|
29 |
+
torch_dtype=torch.bfloat16,
|
30 |
+
device_map="auto",
|
31 |
+
max_new_tokens = 512,
|
32 |
+
do_sample=True,
|
33 |
+
top_k=30,
|
34 |
+
num_return_sequences=1,
|
35 |
+
eos_token_id=tokenizer.eos_token_id
|
36 |
+
)
|
37 |
+
from langchain import HuggingFacePipeline
|
38 |
+
llm = HuggingFacePipeline(pipeline = pipe, model_kwargs = {'temperature':0})
|
39 |
+
from langchain.llms import HuggingFaceTextGenInference
|
40 |
+
from langchain.llms import HuggingFaceTextGenInference
|
41 |
+
from langchain import PromptTemplate
|
42 |
+
from langchain.schema import StrOutputParser
|
43 |
+
|
44 |
+
template = """
|
45 |
+
<s>[INST] <<SYS>>
|
46 |
+
{role}
|
47 |
+
<</SYS>>
|
48 |
+
{text} [/INST]
|
49 |
+
"""
|
50 |
+
|
51 |
+
prompt = PromptTemplate(
|
52 |
+
input_variables = [
|
53 |
+
"role",
|
54 |
+
"text"
|
55 |
+
],
|
56 |
+
template = template,
|
57 |
+
)
|
58 |
+
role = "You are a KUET authority managed chatbot, help users by answering their queries about KUET."
|
59 |
+
chain = prompt | llm | StrOutputParser()
|
60 |
+
ques="What is KUET?"
|
61 |
+
ans=chain.invoke({"role": role,"text":ques})
|
62 |
+
print(ans)
|
63 |
+
```
|
64 |
|
65 |
[More Information Needed]
|
66 |
|
|
|
68 |
|
69 |
### Training Data
|
70 |
|
71 |
+
Custom dataset for collecting from KUET website.
|
|
|
|
|
72 |
|
73 |
### Training Procedure
|
74 |
|
75 |
+
```
|
76 |
+
import os
|
77 |
+
import torch
|
78 |
+
from datasets import load_dataset, Dataset
|
79 |
+
import pandas as pd
|
80 |
+
import transformers
|
81 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
82 |
+
from trl import SFTTrainer
|
83 |
+
import transformers
|
84 |
+
# from peft import AutoPeftModelForCausalLM
|
85 |
+
from transformers import GenerationConfig
|
86 |
+
from pynvml import *
|
87 |
+
import glob
|
88 |
+
base_model = "mistralai/Mistral-7B-Instruct-v0.2"
|
89 |
+
lora_output = 'models/lora_KUET_LLM_Mistral'
|
90 |
+
full_output = 'models/full_KUET_LLM_Mistral'
|
91 |
+
DEVICE = 'cuda'
|
92 |
+
bnb_config = BitsAndBytesConfig(
|
93 |
+
load_in_8bit= True,
|
94 |
+
# bnb_4bit_quant_type= "nf4",
|
95 |
+
# bnb_4bit_compute_dtype= torch.bfloat16,
|
96 |
+
# bnb_4bit_use_double_quant= False,
|
97 |
+
)
|
98 |
+
model = AutoModelForCausalLM.from_pretrained(
|
99 |
+
base_model,
|
100 |
+
# load_in_4bit=True,
|
101 |
+
quantization_config=bnb_config,
|
102 |
+
torch_dtype=torch.bfloat16,
|
103 |
+
device_map="auto",
|
104 |
+
trust_remote_code=True,
|
105 |
+
)
|
106 |
+
model.config.use_cache = False # silence the warnings
|
107 |
+
model.config.pretraining_tp = 1
|
108 |
+
model.gradient_checkpointing_enable()
|
109 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model, trust_remote_code=True)
|
110 |
+
tokenizer.padding_side = 'right'
|
111 |
+
tokenizer.pad_token = tokenizer.eos_token
|
112 |
+
tokenizer.add_eos_token = True
|
113 |
+
tokenizer.add_bos_token, tokenizer.add_eos_token
|
114 |
+
|
115 |
+
### read csv with Prompt, Answer pair
|
116 |
+
data_location = r"/home/sdm/Desktop/shakib/KUET LLM/data/dataset_shakibV2.xlsx" ## replace here
|
117 |
+
data_df=pd.read_excel( data_location )
|
118 |
+
def formatted_text(x):
|
119 |
+
temp = [
|
120 |
+
# {"role": "system", "content": "Answer as a medical assistant. Respond concisely."},
|
121 |
+
{"role": "user", "content": """Answer the question concisely as a medical assisstant.
|
122 |
+
Question: """ + x["Prompt"]},
|
123 |
+
{"role": "assistant", "content": x["Reply"]}
|
124 |
+
]
|
125 |
+
return tokenizer.apply_chat_template(temp, add_generation_prompt=False, tokenize=False)
|
126 |
+
|
127 |
+
### set formatting
|
128 |
+
data_df["text"] = data_df[["Prompt", "Reply"]].apply(lambda x: formatted_text(x), axis=1) ## replace Prompt and Answer if collected dataset has different column names
|
129 |
+
print(data_df.iloc[0])
|
130 |
+
dataset = Dataset.from_pandas(data_df)
|
131 |
+
# Set PEFT adapter config (16:32)
|
132 |
+
from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training
|
133 |
+
|
134 |
+
# target modules are currently selected for zephyr base model
|
135 |
+
config = LoraConfig(
|
136 |
+
r=16,
|
137 |
+
lora_alpha=32,
|
138 |
+
target_modules=["q_proj", "v_proj","k_proj","o_proj","gate_proj","up_proj","down_proj"], # target all the linear layers for full finetuning
|
139 |
+
lora_dropout=0.05,
|
140 |
+
bias="none",
|
141 |
+
task_type="CAUSAL_LM")
|
142 |
+
|
143 |
+
# stabilize output layer and layernorms
|
144 |
+
model = prepare_model_for_kbit_training(model, 8)
|
145 |
+
# Set PEFT adapter on model (Last step)
|
146 |
+
model = get_peft_model(model, config)
|
147 |
+
# Set Hyperparameters
|
148 |
+
MAXLEN=512
|
149 |
+
BATCH_SIZE=4
|
150 |
+
GRAD_ACC=4
|
151 |
+
OPTIMIZER='paged_adamw_8bit' # save memory
|
152 |
+
LR=5e-06 # slightly smaller than pretraining lr | and close to LoRA standard
|
153 |
+
# Set training config
|
154 |
+
training_config = transformers.TrainingArguments(per_device_train_batch_size=BATCH_SIZE,
|
155 |
+
gradient_accumulation_steps=GRAD_ACC,
|
156 |
+
optim=OPTIMIZER,
|
157 |
+
learning_rate=LR,
|
158 |
+
fp16=True, # consider compatibility when using bf16
|
159 |
+
logging_steps=10,
|
160 |
+
num_train_epochs = 2,
|
161 |
+
output_dir=lora_output,
|
162 |
+
remove_unused_columns=True,
|
163 |
+
)
|
164 |
+
|
165 |
+
# Set collator
|
166 |
+
data_collator = transformers.DataCollatorForLanguageModeling(tokenizer,mlm=False)
|
167 |
+
|
168 |
+
# Setup trainer
|
169 |
+
trainer = SFTTrainer(model=model,
|
170 |
+
train_dataset=dataset,
|
171 |
+
data_collator=data_collator,
|
172 |
+
args=training_config,
|
173 |
+
dataset_text_field="text",
|
174 |
+
# callbacks=[early_stop], need to learn, lora easily overfits
|
175 |
+
)
|
176 |
+
|
177 |
+
trainer.train()
|
178 |
+
trainer.save_model(lora_output)
|
179 |
+
|
180 |
+
# Get peft config
|
181 |
+
from peft import PeftConfig
|
182 |
+
config = PeftConfig.from_pretrained(lora_output)
|
183 |
+
# Get base model
|
184 |
+
model = transformers.AutoModelForCausalLM.from_pretrained(config.base_model_name_or_path)
|
185 |
+
tokenizer = transformers.AutoTokenizer.from_pretrained(base_model)
|
186 |
+
# Load the Lora model
|
187 |
+
from peft import PeftModel
|
188 |
+
model = PeftModel.from_pretrained(model, lora_output)
|
189 |
+
|
190 |
+
# Get tokenizer
|
191 |
+
tokenizer = transformers.AutoTokenizer.from_pretrained(config.base_model_name_or_path)
|
192 |
+
merged_model = model.merge_and_unload()
|
193 |
+
merged_model.save_pretrained(full_output)
|
194 |
+
tokenizer.save_pretrained(full_output)
|
195 |
+
|
196 |
+
```
|
197 |
|
198 |
#### Preprocessing [optional]
|
199 |
|
|
|
202 |
|
203 |
#### Training Hyperparameters
|
204 |
|
205 |
+
- The following hyperparameters were used during training:
|
206 |
+
- learning_rate: 0.0002
|
207 |
+
- train_batch_size: 24
|
208 |
+
- eval_batch_size: 8
|
209 |
+
- seed: 42
|
210 |
+
- gradient_accumulation_steps: 4
|
211 |
+
- total_train_batch_size: 96
|
212 |
+
- optimizer: Adam with betas=(0.9,0.999) and epsilon=1e-08
|
213 |
+
- lr_scheduler_type: linear
|
214 |
+
- num_epochs: 2
|
215 |
+
- mixed_precision_training: Native AMP
|
216 |
|
217 |
#### Speeds, Sizes, Times [optional]
|
218 |
|
|
|
228 |
|
229 |
#### Testing Data
|
230 |
|
231 |
+
194 questions are generated by students.
|
232 |
|
233 |
[More Information Needed]
|
234 |
|
|
|
248 |
|
249 |
[More Information Needed]
|
250 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
251 |
|
252 |
## Environmental Impact
|
253 |
|
|
|
255 |
|
256 |
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).
|
257 |
|
258 |
+
- **Hours used:** 2 hours
|
|
|
|
|
|
|
|
|
|
|
|
|
259 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
260 |
|
261 |
#### Hardware
|
262 |
+
RTX 4090
|
263 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
264 |
|
|
|
265 |
|
|
|
266 |
|
|
|
267 |
|
|
|
268 |
|
|
|
269 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
270 |
|
271 |
|