kreimben commited on
Commit
31f0f64
โ€ข
1 Parent(s): dac597a

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +180 -41
README.md CHANGED
@@ -27,52 +27,191 @@ pipeline_tag: text-generation
27
  ---
28
 
29
  # CodeMind
30
- Coding Test Explanatory LLM Model.
31
-
32
- ## Model Details
33
- - **Model Name**: CodeMind
34
- - **Base Model**: google/gemma-2b, google/gemma-1.1-2b-it
35
- - **Fine-tuning Datasets**:
36
- - [kreimben/leetcode_user_submissions_only_python](https://huggingface.co/datasets/kreimben/leetcode_user_submissions_only_python)
37
- - [kreimben/leetcode_with_youtube_captions](https://huggingface.co/datasets/kreimben/leetcode_with_youtube_captions)
38
- - **Model Type**: Language Model
39
- - **Language**: English
40
- - **License**: gemma
41
- - **Model Size**: 2.51B
42
-
43
- ## Intended Use
44
- CodeMind is a fine-tuned language model specifically designed to assist users with coding test questions and provide programming education. It leverages the knowledge from LeetCode user submissions in Python and YouTube video captions related to LeetCode problems to offer guidance, explanations, and code examples.
45
-
46
- ## Training Data
47
- The model was fine-tuned using the following datasets:
48
- 1. **kreimben/leetcode_user_submissions_only_python**: This dataset contains a collection of LeetCode user submissions written in Python. It provides a wide range of coding solutions to various algorithmic problems commonly encountered in coding interviews.
49
- 2. **kreimben/leetcode_with_youtube_captions**: This dataset combines LeetCode problems with corresponding YouTube video captions. The captions provide explanations, thought processes, and step-by-step guidance for solving the coding problems.
50
-
51
- ## Training Procedure
52
- The model was fine-tuned using the Hugging Face Transformer library. The base model, gemma-1.1-2b-it, was further trained on the combined dataset of LeetCode user submissions and YouTube video captions. The fine-tuning process aimed to enhance the model's understanding of coding concepts, problem-solving strategies, and its ability to generate relevant code snippets and explanations.
53
-
54
- ## Evaluation
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  | Metric | Value |
56
  |--------------|--------|
57
- | average | 41.62 |
58
- | arc | 41.81 |
59
  | HellaSwag | 59.03 |
60
  | MMLU | 37.26 |
61
  | TruthfulQA | 43.45 |
62
  | Winogrande | 59.91 |
63
  | GSM8K | 8.26 |
64
 
65
- ## Bias and Limitations
66
- - The model's knowledge is primarily based on the LeetCode user submissions and YouTube video captions used for fine-tuning. It may have limitations in handling coding problems or concepts that are not well-represented in the training data.
67
- - The model's responses are generated based on patterns and information learned from the training data. It may sometimes produce incorrect or suboptimal solutions. Users should always review and verify the generated code before using it in practice.
68
- - The model may exhibit biases present in the training data, such as favoring certain programming styles, algorithms, or approaches. It is important to consider alternative solutions and best practices when using the model's outputs.
69
-
70
- ## Ethical Considerations
71
- - The model should be used as a supportive tool for learning and problem-solving, not as a substitute for human expertise and critical thinking.
72
- - Users should be aware that the model's responses are generated based on patterns in the training data and may not always be accurate, complete, or up to date.
73
- - The model should not be relied upon for making critical decisions or solving real-world problems without thorough validation and testing.
74
-
75
- ## Usage
76
- To use the CodeMind model, you can access it through the Hugging Face model hub or by integrating it into your own applications using the provided API. Provide a coding problem or a question related to programming concepts, and the model will generate relevant explanations, code snippets, or guidance based on its training.
77
-
78
- Please refer to the documentation and examples for detailed instructions on how to integrate and use the CodeMind model effectively.
 
27
  ---
28
 
29
  # CodeMind
30
+
31
+ ## ์†Œ๊ฐœ
32
+ `CodeMind-gemma-2b`๋Š” ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ๋ฌธ์ œ ํ•ด๊ฒฐ ๋ฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ต์œก์„ ์ง€์›ํ•˜๋Š” ์–ธ์–ด ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค.
33
+ ์ด ๋ชจ๋ธ์€ LeetCode ์ •๋ฆฌ๊ธ€๊ณผ ๊ด€๋ จ ์œ ํŠœ๋ธŒ ์บก์…˜์„ ํ™œ์šฉํ•˜์—ฌ ๋ฌธ์ œ ํ•ด๊ฒฐ์— ๋Œ€ํ•œ ์„ค๋ช…๊ณผ ์ฝ”๋“œ ์˜ˆ์ œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
34
+
35
+ ## ๋ชจ๋ธ ์„ธ๋ถ€ ์ •๋ณด
36
+ - **๋ชจ๋ธ ์ด๋ฆ„**: CodeMind
37
+ - **๊ธฐ๋ณธ ๋ชจ๋ธ**: google/gemma-2b
38
+ - **์–ธ์–ด**: ์˜์–ด
39
+ - **๋ชจ๋ธ ํฌ๊ธฐ**: 2.51B ํŒŒ๋ผ๋ฏธํ„ฐ
40
+ - **๋ผ์ด์„ ์Šค**: MIT
41
+
42
+ ## ์ฃผ์š” ๊ธฐ๋Šฅ
43
+ - ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ๋ฌธ์ œ ํ•ด๊ฒฐ
44
+ - ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ฐœ๋… ์„ค๋ช…
45
+ - ๊ด€๋ จ ์ฝ”๋“œ ์Šค๋‹ˆํŽซ ์ƒ์„ฑ
46
+
47
+ ## ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ
48
+ - [**LeetCode ์‚ฌ์šฉ์ž ์ œ์ถœ๋ฌผ**](https://huggingface.co/datasets/kreimben/leetcode_user_submissions): ๋‹ค์–‘ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ์˜ ํŒŒ์ด์ฌ ์†”๋ฃจ์…˜
49
+ - [**์œ ํŠœ๋ธŒ ์บก์…˜**](https://huggingface.co/datasets/kreimben/leetcode_with_youtube_captions): LeetCode ๋ฌธ์ œ์— ๋Œ€ํ•œ ์„ค๋ช… ๋ฐ ๋‹จ๊ณ„๋ณ„ ๊ฐ€์ด๋“œ
50
+
51
+ ## ์‚ฌ์šฉ๋œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
52
+ - [transformers](https://github.com/huggingface/transformers): ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ๋ชจ๋ธ์„ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
53
+ - [datasets](https://github.com/huggingface/datasets): ๋ฐ์ดํ„ฐ์…‹ ์ฒ˜๋ฆฌ ๋ฐ ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
54
+ - [bitsandbytes](https://github.com/TimDettmers/bitsandbytes): ์ตœ์ ํ™”๋œ ์—ฐ์‚ฐ์„ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
55
+ - [peft](https://github.com/huggingface/peft): ํŒŒ์ธ ํŠœ๋‹์„ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
56
+ - [trl](https://github.com/huggingface/trl): ์–ธ์–ด ๋ชจ๋ธ ํŠœ๋‹์„ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
57
+ - [pandas](https://github.com/pandas-dev/pandas): ๋ฐ์ดํ„ฐ ์กฐ์ž‘์„ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
58
+
59
+ ## ํŒŒ์ผ ๊ตฌ์กฐ
60
+ - **dataset/**: ๋ฐ์ดํ„ฐ์…‹ ํŒŒ์ผ์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.
61
+ - **eval/**: ํ‰๊ฐ€ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.
62
+ - **fine-tuning/**: fine tuning ๊ด€๋ จ ๋…ธํŠธ๋ถ ๋ฐ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.
63
+ - `gemma-1.1-2b-it peft qlora.ipynb`: fine tuning ๊ณผ์ •์— ๋Œ€ํ•œ ์„ธ๋ถ€ ์‚ฌํ•ญ์ด ํฌํ•จ๋œ ๋…ธํŠธ๋ถ์ž…๋‹ˆ๋‹ค.
64
+ - **demo.ipynb**: ๋ฐ๋ชจ ๋…ธํŠธ๋ถ์œผ๋กœ ๋ชจ๋ธ ์‚ฌ์šฉ ์˜ˆ์ œ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
65
+ - **requirements.txt**: ํ”„๋กœ์ ํŠธ ์˜์กด์„ฑ ๋ชฉ๋ก์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
66
+ - **utils.py**: ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜๋“ค์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
67
+
68
+ ## ์‚ฌ์šฉ ๋ฐฉ๋ฒ•
69
+ ์ด ๋ชจ๋ธ์€ HuggingFace์˜ ๋ชจ๋ธ ํ—ˆ๋ธŒ๋ฅผ ํ†ตํ•ด ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์— ํ†ตํ•ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฝ”๋”ฉ ๋ฌธ์ œ ๋˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ด€๋ จ ์งˆ๋ฌธ์„ ์ œ๊ณตํ•˜๋ฉด ๋ชจ๋ธ์ด ๊ด€๋ จ ์„ค๋ช…, ์ฝ”๋“œ ์Šค๋‹ˆํŽซ ๋˜๋Š” ๊ฐ€์ด๋“œ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
70
+
71
+ ```python
72
+ from transformers import AutoModelForCausalLM, AutoTokenizer
73
+
74
+ tokenizer = AutoTokenizer.from_pretrained("kreimben/CodeMind-gemma-2b")
75
+ model = AutoModelForCausalLM.from_pretrained("kreimben/CodeMind-gemma-2b")
76
+
77
+ inputs = tokenizer("์ฝ”๋”ฉ ๋ฌธ์ œ๋‚˜ ์งˆ๋ฌธ์„ ์—ฌ๊ธฐ์— ์ž…๋ ฅํ•˜์„ธ์š”", return_tensors="pt")
78
+ outputs = model.generate(inputs.input_ids)
79
+ print(tokenizer.decode(outputs[0]))
80
+ ```
81
+
82
+ ## ํ›ˆ๋ จ ๊ณผ์ •
83
+
84
+ ### ๋ชจ๋ธ ๋ฐ ํ† ํฌ๋‚˜์ด์ € ๋กœ๋“œ
85
+ ```python
86
+ import os
87
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
88
+
89
+ bnb_config = BitsAndBytesConfig(
90
+ load_in_4bit=True,
91
+ bnb_4bit_quant_type="nf4",
92
+ bnb_4bit_compute_dtype=torch.bfloat16
93
+ )
94
+
95
+ model_id = 'google/gemma-1.1-2b-it'
96
+ token = os.getenv('HF_READ')
97
+
98
+ model = AutoModelForCausalLM.from_pretrained(model_id, quantization_config=bnb_config, device_map={"": 0}, token=token)
99
+ model.config.use_cache = False
100
+ model.gradient_checkpointing_enable()
101
+
102
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
103
+ tokenizer.padding_side = 'right'
104
+ tokenizer.pad_token = tokenizer.eos_token
105
+ ```
106
+
107
+ ### LoRA ๊ตฌ์„ฑ ๋ฐ ๋ชจ๋ธ ์ค€๋น„
108
+ ```python
109
+ from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training
110
+ import bitsandbytes as bnb
111
+
112
+ model = prepare_model_for_kbit_training(model)
113
+
114
+ def find_all_linear_names(model):
115
+ cls = bnb.nn.Linear4bit
116
+ lora_module_names = set()
117
+ for name, module in model.named_modules():
118
+ if isinstance(module, cls):
119
+ names = name.split('.')
120
+ lora_module_names.add(names[0] if len(names) == 1 else names[-1])
121
+ if 'lm_head' in lora_module_names:
122
+ lora_module_names.remove('lm_head')
123
+ return list(lora_module_names)
124
+
125
+ modules = find_all_linear_names(model)
126
+ lora_config = LoraConfig(
127
+ r=64,
128
+ lora_alpha=32,
129
+ target_modules=modules,
130
+ lora_dropout=0.05,
131
+ bias="none",
132
+ task_type="CAUSAL_LM"
133
+ )
134
+
135
+ model = get_peft_model(model, lora_config)
136
+ ```
137
+
138
+ ### ๋ฐ์ดํ„ฐ ์ค€๋น„
139
+ ```python
140
+ import pandas as pd
141
+ from datasets import Dataset
142
+
143
+ submission_dataset = datasets.load_dataset('kreimben/leetcode_user_submissions_only_python', split='train').to_pandas()
144
+ submission_dataset = submission_dataset[['title', 'question_hints', 'question_content', 'content']]
145
+ captions_dataset = datasets.load_dataset('kreimben/leetcode_with_youtube_captions', split='train').to_pandas()
146
+ captions_dataset = captions_dataset[['title', 'question_hints', 'question_content', 'cc_content']]
147
+ captions_dataset.rename(columns={'cc_content': 'content'}, inplace=True)
148
+
149
+ dataset = pd.concat([submission_dataset, captions_dataset])
150
+ del submission_dataset, captions_dataset
151
+
152
+ dataset = Dataset.from_pandas(dataset)
153
+ GEMMA_2B_IT_MODEL_PREFIX_TEXT = "Below is an coding test problem. Solve the question."
154
+
155
+ def generate_prompt(data_point):
156
+ return f"<bos><start_of_turn>user {GEMMA_2B_IT_MODEL_PREFIX_TEXT}
157
+
158
+ I don't know {data_point['title']} problem. give me the insight or appoach.
159
+
160
+ this is problem's hint.
161
+ {data_point['question_hints']}
162
+
163
+ here are some content of question.
164
+ {data_point['question_content']}<end_of_turn>
165
+ <start_of_turn>model {data_point['content']}<end_of_turn><eos>"
166
+
167
+ text_column = [generate_prompt(data_point) for data_point in dataset]
168
+ dataset = dataset.add_column("prompt", text_column)
169
+ ```
170
+
171
+ ### ํ›ˆ๋ จ
172
+ ```python
173
+ from trl import SFTTrainer
174
+ import transformers
175
+ import torch
176
+
177
+ tokenizer.pad_token = tokenizer.eos_token
178
+ torch.cuda.empty_cache()
179
+
180
+ trainer = SFTTrainer(
181
+ model=model,
182
+ train_dataset=dataset,
183
+ dataset_text_field="prompt",
184
+ peft_config=lora_config,
185
+ data_collator=transformers.DataCollatorForLanguageModeling(tokenizer, mlm=False),
186
+ args=transformers.TrainingArguments(
187
+ output_dir='out',
188
+ bf16=True,
189
+ max_steps=100,
190
+ warmup_steps=50,
191
+ per_device_train_batch_size=1,
192
+ gradient_accumulation_steps=1,
193
+ optim="paged_adamw_8bit",
194
+ logging_steps=20,
195
+ report_to='wandb',
196
+ ),
197
+ )
198
+
199
+ trainer.train()
200
+ ```
201
+
202
+ ## ํ‰๊ฐ€
203
+ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‰๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค:
204
+
205
  | Metric | Value |
206
  |--------------|--------|
207
+ | Average | 41.62 |
208
+ | ARC | 41.81 |
209
  | HellaSwag | 59.03 |
210
  | MMLU | 37.26 |
211
  | TruthfulQA | 43.45 |
212
  | Winogrande | 59.91 |
213
  | GSM8K | 8.26 |
214
 
215
+ ## ์ œํ•œ ์‚ฌํ•ญ ๋ฐ ์œค๋ฆฌ์  ๊ณ ๋ ค์‚ฌํ•ญ
216
+ - ๋ชจ๋ธ์˜ ์ถœ๋ ฅ์€ ํ•™์Šต ๋ฐ์ดํ„ฐ์— ๊ธฐ๋ฐ˜ํ•˜๋ฏ€๋กœ ํ•ญ์ƒ ์ •ํ™•ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
217
+ - ์ค‘์š”ํ•œ ๊ฒฐ์ •์ด๋‚˜ ์‹ค์„ธ๊ณ„ ๋ฌธ์ œ ํ•ด๊ฒฐ์— ๋ชจ๋ธ ์ถœ๋ ฅ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— ๋ฐ˜๋“œ์‹œ ๊ฒ€์ฆ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.