zetavg commited on
Commit
ffdd849
2 Parent(s): 194723b c5290ad

Merge branch 'main' into hf-ui-demo

Browse files
llama_lora/ui/finetune_ui.py CHANGED
@@ -316,6 +316,13 @@ def do_train(
316
  resume_from_checkpoint = os.path.join(Global.data_dir, "lora_models", continue_from_model)
317
  if continue_from_checkpoint:
318
  resume_from_checkpoint = os.path.join(resume_from_checkpoint, continue_from_checkpoint)
 
 
 
 
 
 
 
319
 
320
  output_dir = os.path.join(Global.data_dir, "lora_models", model_name)
321
  if os.path.exists(output_dir):
@@ -858,7 +865,8 @@ def finetune_ui():
858
  evaluate_data_count = gr.Slider(
859
  minimum=0, maximum=1, step=1, value=0,
860
  label="Evaluation Data Count",
861
- info="The number of data to be used for evaluation. This amount of data will not be used for training and will be used to assess the performance of the model during the process."
 
862
  )
863
 
864
  with gr.Box(elem_id="finetune_continue_from_model_box"):
@@ -870,7 +878,10 @@ def finetune_ui():
870
  elem_id="finetune_continue_from_model"
871
  )
872
  continue_from_checkpoint = gr.Dropdown(
873
- value="-", label="Checkpoint", choices=["-"])
 
 
 
874
  with gr.Column():
875
  load_params_from_model_btn = gr.Button(
876
  "Load training parameters from selected model", visible=False)
@@ -911,8 +922,6 @@ def finetune_ui():
911
  info="The dropout probability for LoRA, which controls the fraction of LoRA parameters that are set to zero during training. A larger lora_dropout increases the regularization effect of LoRA but also increases the risk of underfitting."
912
  )
913
 
914
- lora_target_module_choices = gr.State(value=default_lora_target_module_choices)
915
-
916
  lora_target_modules = gr.CheckboxGroup(
917
  label="LoRA Target Modules",
918
  choices=default_lora_target_module_choices,
@@ -920,6 +929,7 @@ def finetune_ui():
920
  info="Modules to replace with LoRA.",
921
  elem_id="finetune_lora_target_modules"
922
  )
 
923
  with gr.Box(elem_id="finetune_lora_target_modules_add_box"):
924
  with gr.Row():
925
  lora_target_modules_add = gr.Textbox(
@@ -1136,6 +1146,14 @@ def finetune_ui():
1136
  'Press to load a sample dataset of the current selected format into the textbox.',
1137
  });
1138
 
 
 
 
 
 
 
 
 
1139
  tippy('#finetune_save_total_limit', {
1140
  placement: 'bottom',
1141
  delay: [500, 0],
@@ -1165,6 +1183,24 @@ def finetune_ui():
1165
  content:
1166
  'The name of the new LoRA model. Must be unique.',
1167
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1168
  }, 100);
1169
 
1170
  // Show/hide start and stop button base on the state.
 
316
  resume_from_checkpoint = os.path.join(Global.data_dir, "lora_models", continue_from_model)
317
  if continue_from_checkpoint:
318
  resume_from_checkpoint = os.path.join(resume_from_checkpoint, continue_from_checkpoint)
319
+ will_be_resume_from_checkpoint_file = os.path.join(resume_from_checkpoint, "pytorch_model.bin")
320
+ if not os.path.exists(will_be_resume_from_checkpoint_file):
321
+ raise ValueError(f"Unable to resume from checkpoint {continue_from_model}/{continue_from_checkpoint}. Resuming is only possible from checkpoints stored locally in the data directory. Please ensure that the file '{will_be_resume_from_checkpoint_file}' exists.")
322
+ else:
323
+ will_be_resume_from_checkpoint_file = os.path.join(resume_from_checkpoint, "adapter_model.bin")
324
+ if not os.path.exists(will_be_resume_from_checkpoint_file):
325
+ raise ValueError(f"Unable to continue from model {continue_from_model}. Continuation is only possible from models stored locally in the data directory. Please ensure that the file '{will_be_resume_from_checkpoint_file}' exists.")
326
 
327
  output_dir = os.path.join(Global.data_dir, "lora_models", model_name)
328
  if os.path.exists(output_dir):
 
865
  evaluate_data_count = gr.Slider(
866
  minimum=0, maximum=1, step=1, value=0,
867
  label="Evaluation Data Count",
868
+ info="The number of data to be used for evaluation. This specific amount of data will be randomly chosen from the training dataset for evaluating the model's performance during the process, without contributing to the actual training.",
869
+ elem_id="finetune_evaluate_data_count"
870
  )
871
 
872
  with gr.Box(elem_id="finetune_continue_from_model_box"):
 
878
  elem_id="finetune_continue_from_model"
879
  )
880
  continue_from_checkpoint = gr.Dropdown(
881
+ value="-",
882
+ label="Resume from Checkpoint",
883
+ choices=["-"],
884
+ elem_id="finetune_continue_from_checkpoint")
885
  with gr.Column():
886
  load_params_from_model_btn = gr.Button(
887
  "Load training parameters from selected model", visible=False)
 
922
  info="The dropout probability for LoRA, which controls the fraction of LoRA parameters that are set to zero during training. A larger lora_dropout increases the regularization effect of LoRA but also increases the risk of underfitting."
923
  )
924
 
 
 
925
  lora_target_modules = gr.CheckboxGroup(
926
  label="LoRA Target Modules",
927
  choices=default_lora_target_module_choices,
 
929
  info="Modules to replace with LoRA.",
930
  elem_id="finetune_lora_target_modules"
931
  )
932
+ lora_target_module_choices = gr.State(value=default_lora_target_module_choices)
933
  with gr.Box(elem_id="finetune_lora_target_modules_add_box"):
934
  with gr.Row():
935
  lora_target_modules_add = gr.Textbox(
 
1146
  'Press to load a sample dataset of the current selected format into the textbox.',
1147
  });
1148
 
1149
+ tippy('#finetune_evaluate_data_count', {
1150
+ placement: 'bottom',
1151
+ delay: [500, 0],
1152
+ animation: 'scale-subtle',
1153
+ content:
1154
+ 'While setting a value larger than 0, the checkpoint with the lowest loss on the evaluation data will be saved as the final trained model, thereby helping to prevent overfitting.',
1155
+ });
1156
+
1157
  tippy('#finetune_save_total_limit', {
1158
  placement: 'bottom',
1159
  delay: [500, 0],
 
1183
  content:
1184
  'The name of the new LoRA model. Must be unique.',
1185
  });
1186
+
1187
+ tippy('#finetune_continue_from_model', {
1188
+ placement: 'bottom',
1189
+ delay: [500, 0],
1190
+ animation: 'scale-subtle',
1191
+ content:
1192
+ 'Select a LoRA model to train a new model on top of that model.<br /><br />💡 To use the same training parameters of a previously trained model, select it here and click the <code>Load training parameters from selected model</code> button, then un-select it.',
1193
+ allowHTML: true,
1194
+ });
1195
+
1196
+ tippy('#finetune_continue_from_checkpoint', {
1197
+ placement: 'bottom',
1198
+ delay: [500, 0],
1199
+ animation: 'scale-subtle',
1200
+ content:
1201
+ 'If a checkpoint is selected, training will resume from that specific checkpoint, bypassing any previously completed steps up to the checkpoint\\'s moment. <br /><br />💡 Use this option to resume an unfinished training session. Remember to click the <code>Load training parameters from selected model</code> button to load the training parameters of the selected model.',
1202
+ allowHTML: true,
1203
+ });
1204
  }, 100);
1205
 
1206
  // Show/hide start and stop button base on the state.
llama_lora/ui/inference_ui.py CHANGED
@@ -104,11 +104,12 @@ def do_inference(
104
  model = get_model(base_model_name, lora_model_name)
105
 
106
  generation_config = GenerationConfig(
107
- temperature=temperature,
108
  top_p=top_p,
109
  top_k=top_k,
110
  repetition_penalty=repetition_penalty,
111
  num_beams=num_beams,
 
112
  )
113
 
114
  def ui_generation_stopping_criteria(input_ids, score, **kwargs):
@@ -190,6 +191,23 @@ def reload_selections(current_lora_model, current_prompt_template):
190
  gr.Dropdown.update(choices=available_template_names_with_none, value=current_prompt_template))
191
 
192
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193
  def handle_prompt_template_change(prompt_template, lora_model):
194
  prompter = Prompter(prompt_template)
195
  var_names = prompter.get_variable_names()
@@ -203,37 +221,32 @@ def handle_prompt_template_change(prompt_template, lora_model):
203
 
204
  model_prompt_template_message_update = gr.Markdown.update(
205
  "", visible=False)
206
- lora_mode_info = get_info_of_available_lora_model(lora_model)
207
- if lora_mode_info and isinstance(lora_mode_info, dict):
208
- model_base_model = lora_mode_info.get("base_model")
209
- model_prompt_template = lora_mode_info.get("prompt_template")
210
- if model_base_model and model_base_model != Global.base_model_name:
211
- model_prompt_template_message_update = gr.Markdown.update(
212
- f"⚠️ This model was trained on top of base model `{model_base_model}`, it might not work properly with the selected base model `{Global.base_model_name}`.", visible=True)
213
- elif model_prompt_template and model_prompt_template != prompt_template:
214
- model_prompt_template_message_update = gr.Markdown.update(
215
- f"This model was trained with prompt template `{model_prompt_template}`.", visible=True)
216
 
217
  return [model_prompt_template_message_update] + gr_updates
218
 
219
 
220
  def handle_lora_model_change(lora_model, prompt_template):
221
  lora_mode_info = get_info_of_available_lora_model(lora_model)
222
- if not lora_mode_info:
223
- return gr.Markdown.update("", visible=False), prompt_template
224
-
225
- if not isinstance(lora_mode_info, dict):
226
- return gr.Markdown.update("", visible=False), prompt_template
227
 
228
- model_prompt_template = lora_mode_info.get("prompt_template")
229
- if not model_prompt_template:
230
- return gr.Markdown.update("", visible=False), prompt_template
 
 
 
231
 
232
- available_template_names = get_available_template_names()
233
- if model_prompt_template in available_template_names:
234
- return gr.Markdown.update("", visible=False), model_prompt_template
 
 
 
235
 
236
- return gr.Markdown.update(f"Trained with prompt template `{model_prompt_template}`", visible=True), prompt_template
237
 
238
 
239
  def update_prompt_preview(prompt_template,
@@ -313,7 +326,7 @@ def inference_ui():
313
  # with gr.Column():
314
  with gr.Accordion("Options", open=True, elem_id="inference_options_accordion"):
315
  temperature = gr.Slider(
316
- minimum=0, maximum=1, value=0.1, step=0.01,
317
  label="Temperature",
318
  elem_id="inference_temperature"
319
  )
@@ -525,7 +538,8 @@ def inference_ui():
525
  delay: [500, 0],
526
  animation: 'scale-subtle',
527
  content:
528
- 'Controls randomness: Lowering results in less random completions. Higher values (e.g., 1.0) make the model generate more diverse and random outputs. As the temperature approaches zero, the model will become deterministic and repetitive.',
 
529
  });
530
 
531
  tippy('#inference_top_p', {
@@ -533,7 +547,8 @@ def inference_ui():
533
  delay: [500, 0],
534
  animation: 'scale-subtle',
535
  content:
536
- 'Controls diversity via nucleus sampling: only the tokens whose cumulative probability exceeds "top_p" are considered. 0.5 means half of all likelihood-weighted options are considered.',
 
537
  });
538
 
539
  tippy('#inference_top_k', {
@@ -541,7 +556,8 @@ def inference_ui():
541
  delay: [500, 0],
542
  animation: 'scale-subtle',
543
  content:
544
- 'Controls diversity of the generated text by only considering the "top_k" tokens with the highest probabilities. This method can lead to more focused and coherent outputs by reducing the impact of low probability tokens.',
 
545
  });
546
 
547
  tippy('#inference_beams', {
 
104
  model = get_model(base_model_name, lora_model_name)
105
 
106
  generation_config = GenerationConfig(
107
+ temperature=float(temperature), # to avoid ValueError('`temperature` has to be a strictly positive float, but is 2')
108
  top_p=top_p,
109
  top_k=top_k,
110
  repetition_penalty=repetition_penalty,
111
  num_beams=num_beams,
112
+ do_sample=temperature > 0, # https://github.com/huggingface/transformers/issues/22405#issuecomment-1485527953
113
  )
114
 
115
  def ui_generation_stopping_criteria(input_ids, score, **kwargs):
 
191
  gr.Dropdown.update(choices=available_template_names_with_none, value=current_prompt_template))
192
 
193
 
194
+ def get_warning_message_for_lora_model_and_prompt_template(lora_model, prompt_template):
195
+ messages = []
196
+
197
+ lora_mode_info = get_info_of_available_lora_model(lora_model)
198
+
199
+ if lora_mode_info and isinstance(lora_mode_info, dict):
200
+ model_base_model = lora_mode_info.get("base_model")
201
+ if model_base_model and model_base_model != Global.base_model_name:
202
+ messages.append(f"⚠️ This model was trained on top of base model `{model_base_model}`, it might not work properly with the selected base model `{Global.base_model_name}`.")
203
+
204
+ model_prompt_template = lora_mode_info.get("prompt_template")
205
+ if model_prompt_template and model_prompt_template != prompt_template:
206
+ messages.append(f"This model was trained with prompt template `{model_prompt_template}`.")
207
+
208
+ return " ".join(messages)
209
+
210
+
211
  def handle_prompt_template_change(prompt_template, lora_model):
212
  prompter = Prompter(prompt_template)
213
  var_names = prompter.get_variable_names()
 
221
 
222
  model_prompt_template_message_update = gr.Markdown.update(
223
  "", visible=False)
224
+ warning_message = get_warning_message_for_lora_model_and_prompt_template(lora_model, prompt_template)
225
+ if warning_message:
226
+ model_prompt_template_message_update = gr.Markdown.update(
227
+ warning_message, visible=True)
 
 
 
 
 
 
228
 
229
  return [model_prompt_template_message_update] + gr_updates
230
 
231
 
232
  def handle_lora_model_change(lora_model, prompt_template):
233
  lora_mode_info = get_info_of_available_lora_model(lora_model)
 
 
 
 
 
234
 
235
+ if lora_mode_info and isinstance(lora_mode_info, dict):
236
+ model_prompt_template = lora_mode_info.get("prompt_template")
237
+ if model_prompt_template:
238
+ available_template_names = get_available_template_names()
239
+ if model_prompt_template in available_template_names:
240
+ prompt_template = model_prompt_template
241
 
242
+ model_prompt_template_message_update = gr.Markdown.update(
243
+ "", visible=False)
244
+ warning_message = get_warning_message_for_lora_model_and_prompt_template(lora_model, prompt_template)
245
+ if warning_message:
246
+ model_prompt_template_message_update = gr.Markdown.update(
247
+ warning_message, visible=True)
248
 
249
+ return model_prompt_template_message_update, prompt_template
250
 
251
 
252
  def update_prompt_preview(prompt_template,
 
326
  # with gr.Column():
327
  with gr.Accordion("Options", open=True, elem_id="inference_options_accordion"):
328
  temperature = gr.Slider(
329
+ minimum=0, maximum=2, value=0, step=0.01,
330
  label="Temperature",
331
  elem_id="inference_temperature"
332
  )
 
538
  delay: [500, 0],
539
  animation: 'scale-subtle',
540
  content:
541
+ '<strong>Controls randomness</strong>: Higher values (e.g., <code>1.0</code>) make the model generate more diverse and random outputs. As the temperature approaches zero, the model will become deterministic and repetitive.<br /><i>Setting a value larger then <code>0</code> will enable sampling.</i>',
542
+ allowHTML: true,
543
  });
544
 
545
  tippy('#inference_top_p', {
 
547
  delay: [500, 0],
548
  animation: 'scale-subtle',
549
  content:
550
+ 'Controls diversity via nucleus sampling: only the tokens whose cumulative probability exceeds <code>top_p</code> are considered. <code>0.5</code> means half of all likelihood-weighted options are considered.<br />Will only take effect if Temperature is set to > 0.',
551
+ allowHTML: true,
552
  });
553
 
554
  tippy('#inference_top_k', {
 
556
  delay: [500, 0],
557
  animation: 'scale-subtle',
558
  content:
559
+ 'Controls diversity of the generated text by only considering the <code>top_k</code> tokens with the highest probabilities. This method can lead to more focused and coherent outputs by reducing the impact of low probability tokens.<br />Will only take effect if Temperature is set to > 0.',
560
+ allowHTML: true,
561
  });
562
 
563
  tippy('#inference_beams', {
llama_lora/ui/main_page.py CHANGED
@@ -134,6 +134,10 @@ def main_page_custom_css():
134
  border: 1px solid var(--border-color-primary);
135
  border-radius: 4px;
136
  box-shadow: 0 2px 20px rgba(5,5,5,.08);
 
 
 
 
137
  }
138
  .tippy-arrow {
139
  color: var(--block-background-fill);
@@ -144,6 +148,45 @@ def main_page_custom_css():
144
  font-weight: 100;
145
  }
146
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  /*
148
  .codemirror-wrapper .cm-editor .cm-gutters {
149
  background-color: var(--background-fill-secondary);
@@ -248,12 +291,18 @@ def main_page_custom_css():
248
  #inference_lora_model_prompt_template_message:not(.hidden) + #inference_lora_model {
249
  padding-bottom: 28px;
250
  }
 
 
 
 
 
 
 
 
251
  #inference_lora_model_group > #inference_lora_model_prompt_template_message {
252
- position: absolute;
253
- bottom: 8px;
254
- left: 20px;
255
- z-index: 61;
256
- width: 999px;
257
  font-size: 12px;
258
  opacity: 0.7;
259
  }
@@ -608,7 +657,7 @@ def main_page_custom_css():
608
  }
609
 
610
  @media screen and (max-width: 392px) {
611
- #inference_lora_model, #finetune_template {
612
  border-bottom-left-radius: 0;
613
  border-bottom-right-radius: 0;
614
  }
 
134
  border: 1px solid var(--border-color-primary);
135
  border-radius: 4px;
136
  box-shadow: 0 2px 20px rgba(5,5,5,.08);
137
+ /* box-shadow: var(--shadow-drop-lg); */
138
+ }
139
+ body.dark .tippy-box {
140
+ box-shadow: 0 0 8px rgba(160,160,160,0.12);
141
  }
142
  .tippy-arrow {
143
  color: var(--block-background-fill);
 
148
  font-weight: 100;
149
  }
150
 
151
+ .tippy-arrow::before {
152
+ z-index: 1;
153
+ }
154
+ .tippy-arrow::after {
155
+ content: "";
156
+ position: absolute;
157
+ z-index: -1;
158
+ border-color: transparent;
159
+ border-style: solid;
160
+ }
161
+ .tippy-box[data-placement^=top]> .tippy-arrow::after {
162
+ bottom: -9px;
163
+ left: -1px;
164
+ border-width: 9px 9px 0;
165
+ border-top-color: var(--border-color-primary);
166
+ transform-origin: center top;
167
+ }
168
+ .tippy-box[data-placement^=bottom]> .tippy-arrow::after {
169
+ top: -9px;
170
+ left: -1px;
171
+ border-width: 0 9px 9px;
172
+ border-bottom-color: var(--border-color-primary);
173
+ transform-origin: center bottom;
174
+ }
175
+ .tippy-box[data-placement^=left]> .tippy-arrow:after {
176
+ border-width: 9px 0 9px 9px;
177
+ border-left-color: var(--border-color-primary);
178
+ top: -1px;
179
+ right: -9px;
180
+ transform-origin: center left;
181
+ }
182
+ .tippy-box[data-placement^=right]> .tippy-arrow::after {
183
+ top: -1px;
184
+ left: -9px;
185
+ border-width: 9px 9px 9px 0;
186
+ border-right-color: var(--border-color-primary);
187
+ transform-origin: center right;
188
+ }
189
+
190
  /*
191
  .codemirror-wrapper .cm-editor .cm-gutters {
192
  background-color: var(--background-fill-secondary);
 
291
  #inference_lora_model_prompt_template_message:not(.hidden) + #inference_lora_model {
292
  padding-bottom: 28px;
293
  }
294
+ #inference_lora_model_group {
295
+ flex-direction: column-reverse;
296
+ border-width: var(--block-border-width);
297
+ border-color: var(--block-border-color);
298
+ }
299
+ #inference_lora_model_group #inference_lora_model {
300
+ border: 0;
301
+ }
302
  #inference_lora_model_group > #inference_lora_model_prompt_template_message {
303
+ padding: var(--block-padding) !important;
304
+ margin-top: -50px !important;
305
+ margin-left: 4px !important;
 
 
306
  font-size: 12px;
307
  opacity: 0.7;
308
  }
 
657
  }
658
 
659
  @media screen and (max-width: 392px) {
660
+ #inference_lora_model, #inference_lora_model_group, #finetune_template {
661
  border-bottom-left-radius: 0;
662
  border-bottom-right-radius: 0;
663
  }
lora_models/unhelpful-ai-v01/finetune_params.json CHANGED
@@ -1,8 +1,9 @@
1
  {
2
- "num_train_epochs": 16,
3
  "learning_rate": 0.0003,
4
  "cutoff_len": 512,
5
- "lora_r": 12,
 
6
  "lora_alpha": 32,
7
  "lora_dropout": 0.05,
8
  "lora_target_modules": [
@@ -11,9 +12,5 @@
11
  "k_proj",
12
  "o_proj"
13
  ],
14
- "train_on_inputs": false,
15
- "group_by_length": false,
16
- "save_steps": 500,
17
- "save_total_limit": 5,
18
- "logging_steps": 10
19
  }
 
1
  {
2
+ "num_train_epochs": 8,
3
  "learning_rate": 0.0003,
4
  "cutoff_len": 512,
5
+ "val_set_size": 0,
6
+ "lora_r": 16,
7
  "lora_alpha": 32,
8
  "lora_dropout": 0.05,
9
  "lora_target_modules": [
 
12
  "k_proj",
13
  "o_proj"
14
  ],
15
+ "train_on_inputs": false
 
 
 
 
16
  }