File size: 10,393 Bytes
1680a25
 
 
db4cfaa
 
 
 
 
 
 
 
 
 
 
1680a25
 
db4cfaa
1680a25
 
9de3576
 
52c8e22
1680a25
db4cfaa
1680a25
 
 
 
 
 
 
 
 
db4cfaa
 
 
 
 
 
1680a25
db4cfaa
1680a25
 
db4cfaa
1680a25
52c8e22
ebcbd64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52c8e22
ebcbd64
 
 
 
 
 
 
 
db4cfaa
1680a25
 
 
db4cfaa
9de3576
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db4cfaa
1680a25
9de3576
52c8e22
1680a25
db4cfaa
9de3576
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db4cfaa
1680a25
52c8e22
1680a25
52c8e22
 
1680a25
52c8e22
 
 
 
 
1680a25
52c8e22
 
 
 
 
 
 
 
 
 
 
 
 
 
1680a25
52c8e22
1680a25
52c8e22
 
1680a25
 
 
 
 
 
 
52c8e22
 
 
 
 
1680a25
 
 
 
 
 
52c8e22
 
1680a25
ebcbd64
1680a25
db4cfaa
ebcbd64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db4cfaa
1680a25
 
 
52c8e22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1680a25
 
 
 
52c8e22
 
 
 
 
3ac15e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1680a25
0b6a77f
1680a25
 
52c8e22
 
 
 
 
 
 
 
 
 
 
 
 
 
1680a25
3ac15e6
1680a25
52c8e22
1680a25
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
---
base_model: unsloth/qwen2.5-coder-32b-instruct-bnb-4bit
library_name: peft
datasets:
- 100suping/ko-bird-sql-schema
- won75/text_to_sql_ko
language:
- ko
pipeline_tag: text-generation
tags:
- SQL
- lora
- adapter
- instruction-tuning
---

# 100suping/Qwen2.5-Coder-34B-Instruct-kosql-adapter

<!-- Provide a quick summary of what the model is/does. -->
This Repo contains **LoRA (Low-Rank Adaptation) Adapter** for [unsloth/qwen2.5-coder-32b-instruct-bnb-4bit]

The Adapter was trained for **improving model's SQL generation capability** in **Korean question & multi-db context**. 

This adapter was created through **instruction tuning**.


## Model Details

### Model Description

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


- **Base Model:** unsloth/Qwen2.5-Coder-32B-Instruct
- **Task:** Instruction Following(Korean)
- **Language:** English (or relevant language)
- **Training Data:** 100suping/ko-bird-sql-schema, won75/text_to_sql_ko
- **Model type:** Causal Language Models.
- **Language(s) (NLP):** Multi-Language

## How to Get Started with the Model

<!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
To use this LoRA adapter, refer to the following code:

### Adapter Loading

```
from transformers import BitsAndBytesConfig

def get_bnb_config(bit=8):
    if bit == 8:
        return BitsAndBytesConfig(load_in_8bit=True)
    else:
        print(f"You put {bit} bit in argument.\nWhatever the number you put in, if it is not 8 then 4bit config would be returned.")
        return BitsAndBytesConfig(load_in_4bit=True)
```

```
from unsloth import FastLanguageModel

model_name = "unsloth/Qwen2.5-Coder-32B-Instruct"
adapter_revision = "checkpoint-200" # checkpoint-100 ~ 350, main(which is checkpoint-384)

bnb_config = get_bnb_config(bit=bit)
model, tokenizer = FastLanguageModel.from_pretrained(
    model_name=model_name,
    dtype=None,
    quantization_config=bnb_config,
)
model.load_adapter("100suping/Qwen2.5-Coder-34B-Instruct-kosql-adapter", revision=adapter_revision)
```

### Prompt

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

```
GENERAL_QUERY_PREFIX = """๋‹น์‹ ์€ ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์„ MySQL ์ฟผ๋ฆฌ๋ฌธ์œผ๋กœ ๋ฐ”๊พธ์–ด์ฃผ๋Š” ์กฐ์ง์˜ ํŒ€์›์ž…๋‹ˆ๋‹ค.
๋‹น์‹ ์˜ ์ž„๋ฌด๋Š” DB ์ด๋ฆ„ ๊ทธ๋ฆฌ๊ณ  DB๋‚ด ํ…Œ์ด๋ธ”์˜ ๋ฉ”ํƒ€ ์ •๋ณด๊ฐ€ ๋‹ด๊ธด ์•„๋ž˜์˜ (context)๋ฅผ ์ด์šฉํ•ด์„œ ์ฃผ์–ด์ง„ ์งˆ๋ฌธ(user_question)์— ๊ฑธ๋งž๋Š” MySQL ์ฟผ๋ฆฌ๋ฌธ์„ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

(context)
{context}
"""

GENERATE_QUERY_INSTRUCTIONS = """
์ฃผ์–ด์ง„ ์งˆ๋ฌธ(user_question)์— ๋Œ€ํ•ด์„œ ๋ฌธ๋ฒ•์ ์œผ๋กœ ์˜ฌ๋ฐ”๋ฅธ MySQL ์ฟผ๋ฆฌ๋ฌธ์„ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”.
"""
```

### Example input

```
<|im_start|>system
๋‹น์‹ ์€ ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์„ MySQL ์ฟผ๋ฆฌ๋ฌธ์œผ๋กœ ๋ฐ”๊พธ์–ด์ฃผ๋Š” ์กฐ์ง์˜ ํŒ€์›์ž…๋‹ˆ๋‹ค.
๋‹น์‹ ์˜ ์ž„๋ฌด๋Š” DB ์ด๋ฆ„ ๊ทธ๋ฆฌ๊ณ  DB๋‚ด ํ…Œ์ด๋ธ”์˜ ๋ฉ”ํƒ€ ์ •๋ณด๊ฐ€ ๋‹ด๊ธด ์•„๋ž˜์˜ (context)๋ฅผ ์ด์šฉํ•ด์„œ ์ฃผ์–ด์ง„ ์งˆ๋ฌธ(user_question)์— ๊ฑธ๋งž๋Š” MySQL ์ฟผ๋ฆฌ๋ฌธ์„ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

(context)
DB: movie_platform
table DDL: CREATE TABLE `movies` ( `movie_id` INTEGER `movie_title` TEXT `movie_release_year` INTEGER `movie_url` TEXT `movie_title_language` TEXT `movie_popularity` INTEGER `movie_image_url` TEXT `director_id` TEXT `director_name` TEXT `director_url` TEXT PRIMARY KEY (movie_id) FOREIGN KEY (user_id) REFERENCES `lists_users`(user_id) FOREIGN KEY (user_id) REFERENCES `lists_users`(user_id) FOREIGN KEY (user_id) REFERENCES `lists`(user_id) FOREIGN KEY (list_id) REFERENCES `lists`(list_id) FOREIGN KEY (user_id) REFERENCES `ratings_users`(user_id) FOREIGN KEY (user_id) REFERENCES `lists_users`(user_id) FOREIGN KEY (movie_id) REFERENCES `movies`(movie_id) );


์ฃผ์–ด์ง„ ์งˆ๋ฌธ(user_question)์— ๋Œ€ํ•ด์„œ ๋ฌธ๋ฒ•์ ์œผ๋กœ ์˜ฌ๋ฐ”๋ฅธ MySQL ์ฟผ๋ฆฌ๋ฌธ์„ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”.
<|im_end|>
<|im_start|>user
๊ฐ€์žฅ ์ธ๊ธฐ ์žˆ๋Š” ์˜ํ™”๋Š” ๋ฌด์—‡์ธ๊ฐ€์š”? ๊ทธ ์˜ํ™”๋Š” ์–ธ์ œ ๊ฐœ๋ด‰๋˜์—ˆ๊ณ  ๋ˆ„๊ฐ€ ๊ฐ๋…์ธ๊ฐ€์š”?<|im_end|>
<|im_start|>assistant
```sql
SELECT movie_title, movie_release_year, director_name FROM movies ORDER BY movie_popularity DESC LIMIT 1 ;
```<|im_end|>
```


### Inference - pytorch

```
messages = [
        {"role": "system", "content": GENERAL_QUERY_PREFIX.format(context=context) + GENERATE_QUERY_INSTRUCTIONS},
        {"role": "user", "content": "user_question: "+ user_question}
    ]


text = tokenizer.apply_chat_template(
        messages,
        tokenize=False,
        add_generation_prompt=True
    )

model_inputs = tokenizer([text], return_tensors="pt").to(model.device)

generated_ids = model.generate(
    **model_inputs,
    max_new_tokens=max_new_tokens
)
generated_ids = [
    output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
]

response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
```

### Inference - LangChain & HuggingFacePipeline

```
bnb_config = get_bnb_config(bit=bit)

model, tokenizer = FastLanguageModel.from_pretrained(
    model_name=model_name,
    dtype=None,
    quantization_config=bnb_config,
)

pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=max_new_tokens)
hf_llm = HuggingFacePipeline(pipeline=pipe)

prompt = ChatPromptTemplate.from_messages(
        [
            SystemMessage(
                content=GENERAL_QUERY_PREFIX.format(context=context) + GENERATE_QUERY_INSTRUCTIONS
            ),
            (
                "human",
                "์งˆ๋ฌธ(user_question): {user_question}",
            ),
        ]
    )

chain = prompt | hf_llm

response = chain.invoke({"user_question" : user_question})
```


## 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. -->
https://huggingface.co/datasets/100suping/ko-bird-sql-schema
- Naive translation of english quesiton to korean quesiton

https://huggingface.co/datasets/won75/text_to_sql_ko
- Generated data from 100 seed data


### Training Procedure

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

https://github.com/100suping/train_with_unsloth


### Preprocess Functions

```
def get_conversation_data(examples):
    questions = examples['question']
    schemas =examples['schema']
    sql_queries =examples['SQL']
    convos = []
    for question, schema, sql in zip(questions, schemas, sql_queries):
        conv = [
        {"role": "system", "content": GENERAL_QUERY_PREFIX.format(context=schema) + GENERATE_QUERY_INSTRUCTIONS},
        {"role": "user", "content": question},
        {"role": "assistant", "content": "```sql\n"+sql+";\n```"}
        ]
        convos.append(conv)
    return {"conversation":convos,}

def formatting_prompts_func(examples):
    convos = examples["conversation"]
    texts = [tokenizer.apply_chat_template(convo, tokenize = False, add_generation_prompt = False) for convo in convos]
    return { "text" : texts, }
```

#### Training Hyperparameters

- **Training regime:** bf16 mixed-precision
- **Load-in-8bit:** True
- **LoRA config:**
  - r=16
  - lora_alpha=32
  - lora_dropout=0.0
  - target_modules = "q proj", "k_proj", "v_proj", "o_proj","gate_proj", "up_proj", "down_proj"
  - bias = "none"
  - use_gradient_checkpointing = "unsloth"
  - use_rslora = False
  - loftq_config = None
- **Training Data:** 100suping/ko-bird-sql-schema, won75/text_to_sql_ko
- **Max_seq_length:** 4096
- **Save_steps:** 50
- **Epochs:** 2
- **Global_steps:** 384
- **Batch_size:** 16
- **Gradient_accumulation_steps:** 2
- **Learning_rate:** 2e-4
- **Warmup_steps:** 20 

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

<!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
- **Device:** G-NAHP-80 from EliceCloud(https://elice.io/ko/products/cloud/on-demand)
  - A100 80GB PCle (However somehow if i use more than 60GB, error shows up)
  - CPU 16 vCore
  - Memory 192 GiB
  - Storage 100 GiB
- **Memory-Used(GPU VRAM):** ~60GB

## For Continuous Instruction-tuning

To use this LoRA adapter, refer to the following code:

```
from peft import PeftModel

bnb_config = get_bnb_config(bit=bit)

model, tokenizer = FastLanguageModel.from_pretrained(
    model_name=model_name,
    dtype=None,
    quantization_config=bnb_config,
)

model = PeftModel.from_pretrained(model, adapter_path, is_trainable=True)
model = FastLanguageModel.patch_peft_model(model, use_gradient_checkpointing="unsloth")

model.print_trainable_parameters()
```


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

```
@article{hui2024qwen2,
      title={Qwen2. 5-Coder Technical Report},
      author={Hui, Binyuan and Yang, Jian and Cui, Zeyu and Yang, Jiaxi and Liu, Dayiheng and Zhang, Lei and Liu, Tianyu and Zhang, Jiajun and Yu, Bowen and Dang, Kai and others},
      journal={arXiv preprint arXiv:2409.12186},
      year={2024}
}
@article{qwen2,
      title={Qwen2 Technical Report}, 
      author={An Yang and Baosong Yang and Binyuan Hui and Bo Zheng and Bowen Yu and Chang Zhou and Chengpeng Li and Chengyuan Li and Dayiheng Liu and Fei Huang and Guanting Dong and Haoran Wei and Huan Lin and Jialong Tang and Jialin Wang and Jian Yang and Jianhong Tu and Jianwei Zhang and Jianxin Ma and Jin Xu and Jingren Zhou and Jinze Bai and Jinzheng He and Junyang Lin and Kai Dang and Keming Lu and Keqin Chen and Kexin Yang and Mei Li and Mingfeng Xue and Na Ni and Pei Zhang and Peng Wang and Ru Peng and Rui Men and Ruize Gao and Runji Lin and Shijie Wang and Shuai Bai and Sinan Tan and Tianhang Zhu and Tianhao Li and Tianyu Liu and Wenbin Ge and Xiaodong Deng and Xiaohuan Zhou and Xingzhang Ren and Xinyu Zhang and Xipin Wei and Xuancheng Ren and Yang Fan and Yang Yao and Yichang Zhang and Yu Wan and Yunfei Chu and Yuqiong Liu and Zeyu Cui and Zhenru Zhang and Zhihao Fan},
      journal={arXiv preprint arXiv:2407.10671},
      year={2024}
}
```

## Model Card Authors

joonavel[https://github.com/joonavel] from 100suping [https://github.com/100suping]

### Framework versions

- PEFT 0.13.2