Vipitis commited on
Commit
6631a55
1 Parent(s): 74b2bf0

generation parameters added

Browse files
Files changed (1) hide show
  1. app.py +57 -11
app.py CHANGED
@@ -269,7 +269,7 @@ outro_text ="""
269
  - [] generation history stating which function and orig/generated returns. (use State ??). do it as comments in the code?
270
  - [~] display errros/issues to the user (raise gr.Error could be one idea, but highlighting in the code would be awesome) currently adds a comment to the code.
271
  - [] generate whole shaders (via prompts guidance, recursive from errors)
272
- - [] accordion with generation parameters (as pipeline_kwargs?) look up starcoder playround and take "inspiration" from there
273
  - [] support FIM task for better model context
274
  - [~] include some context for prompt (title, comments before a functions) - now works with the first comment inside a function body (has to be first)
275
  - [] gradio examples
@@ -356,7 +356,7 @@ def get_full_replacement(orig_code, retn_start_idx, retn_end_idx, prediction) ->
356
  variation = orig_code[:retn_start_idx] + generated + orig_code[retn_end_idx:]
357
  return variation
358
 
359
- def alter_return(orig_code, func_idx, pipeline=PIPE): #default pipeline can't be passed as gloabl?
360
  """
361
  Replaces the return statement of a function with a generated one.
362
  Args:
@@ -378,6 +378,8 @@ def alter_return(orig_code, func_idx, pipeline=PIPE): #default pipeline can't be
378
  else:
379
  raise gr.Error(f"func_idx must be int or str, not {type(func_idx)}")
380
 
 
 
381
  retrns = []
382
  retrn_start_idx = orig_code.find("return")
383
  while retrn_start_idx != -1:
@@ -392,8 +394,7 @@ def alter_return(orig_code, func_idx, pipeline=PIPE): #default pipeline can't be
392
  retrn_start_idx, retrn_end_idx = retrns[func_idx]
393
  model_context = orig_code[:retrn_start_idx] #TODO: maximal context?
394
  model_inp = model_context + "return"
395
- new_toks = (retrn_end_idx - retrn_start_idx) * 2 #TODO: approximation, we do have early stopping? maybe also use a number instead?
396
- pipe_generation = pipeline(model_inp, max_new_tokens=new_toks, return_full_text=False)[0]["generated_text"] #pipeline kwargs are missing?!
397
  altered_code = get_full_replacement(orig_code, retrn_start_idx+7, retrn_end_idx, pipe_generation)
398
 
399
  return altered_code
@@ -409,8 +410,15 @@ def _line_chr2char(text, line_idx, chr_idx):
409
  char_idx += chr_idx
410
  return char_idx
411
 
 
 
 
 
 
 
 
412
 
413
- def alter_body(old_code, func_id, funcs_list: list, pipeline=PIPE):
414
  """
415
  Replaces the body of a function with a generated one.
416
  Args:
@@ -430,6 +438,7 @@ def alter_body(old_code, func_id, funcs_list: list, pipeline=PIPE):
430
  func_node = funcs_list[func_id]
431
  print(f"using for generation: {func_node=}")
432
 
 
433
 
434
  print(f"{pipeline=}") # check if default even loaded
435
  if pipeline is None:
@@ -449,10 +458,7 @@ def alter_body(old_code, func_id, funcs_list: list, pipeline=PIPE):
449
  # print(second_child.text.decode())
450
  model_context += " { \n " + second_child.text.decode()
451
  print(f"{model_context=}")
452
- num_new_tokens = max(160,(body_end_idx - body_start_idx) + 10) #TODO: approximation, we do have early stopping? maybe also use a number instead? HARD MAX for performance limits.
453
-
454
- print(f"generating up to {num_new_tokens} after {model_context!r}")
455
- generation = pipeline(model_context, max_new_tokens=num_new_tokens, return_full_text=False)[0]["generated_text"]
456
  print(f"{generation=}")
457
  ctx_with_generation = model_context + generation
458
  print(f"{ctx_with_generation=}")
@@ -493,10 +499,50 @@ with gr.Blocks() as site:
493
  model_cp = gr.Textbox(value="Vipitis/santacoder-finetuned-Shadertoys-fine", label="Model Checkpoint (Enter to load!)", interactive=True)
494
  sample_idx = gr.Slider(minimum=0, maximum=num_samples, value=3211, label="pick sample from dataset", step=1.0)
495
  func_dropdown = gr.Dropdown(value=["0: edit the Code (or load a shader) to update this dropdown"], label="chose a function to modify") #breaks if I add a string in before that? #TODO: use type="index" to get int - always gives None?
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
496
  with gr.Row():
497
  gen_return_button = gr.Button("generate a alternate return statement", label="generate return")
498
  gen_func_button = gr.Button("generate an alternate function body", label="generate function")
499
- # update_funcs_button = gr.Button("update functions", label="update functions")
500
  with gr.Row():
501
  with gr.Column():
502
  source_embed = gr.HTML('<iframe width="640" height="360" frameborder="0" src="" allowfullscreen></iframe>', label="How this shader originally renders")
@@ -514,7 +560,7 @@ with gr.Blocks() as site:
514
  model_cp.submit(fn=_make_pipeline, inputs=[model_cp], outputs=[pipe]) # how can we trigger this on load?
515
  sample_idx.release(fn=grab_sample, inputs=[sample_idx], outputs=[sample_pass, sample_code, source_embed])
516
  gen_return_button.click(fn=alter_return, inputs=[sample_code, func_dropdown, pipe], outputs=[sample_code])
517
- gen_func_button.click(fn=alter_body, inputs=[sample_code, func_dropdown, funcs, pipe], outputs=[sample_code, pipe])
518
  sample_code.change(fn=list_dropdown, inputs=[sample_code], outputs=[funcs, func_dropdown]) # to update this after generation, so spans aren't messed up
519
  sample_code.change(fn=make_iframe, inputs=[sample_code], outputs=[our_embed]) #twice could cause issues, find better ways.
520
  site.launch()
 
269
  - [] generation history stating which function and orig/generated returns. (use State ??). do it as comments in the code?
270
  - [~] display errros/issues to the user (raise gr.Error could be one idea, but highlighting in the code would be awesome) currently adds a comment to the code.
271
  - [] generate whole shaders (via prompts guidance, recursive from errors)
272
+ - [x] accordion with generation parameters (as pipeline_kwargs?) look up starcoder playround and take "inspiration" from there (implemented for both buttons, untested)
273
  - [] support FIM task for better model context
274
  - [~] include some context for prompt (title, comments before a functions) - now works with the first comment inside a function body (has to be first)
275
  - [] gradio examples
 
356
  variation = orig_code[:retn_start_idx] + generated + orig_code[retn_end_idx:]
357
  return variation
358
 
359
+ def alter_return(orig_code, func_idx, temperature, max_new_tokens, top_p, repetition_penalty, pipeline=PIPE): #default pipeline can't be passed as gloabl?
360
  """
361
  Replaces the return statement of a function with a generated one.
362
  Args:
 
378
  else:
379
  raise gr.Error(f"func_idx must be int or str, not {type(func_idx)}")
380
 
381
+ generation_kwargs = _combine_generation_kwargs(temperature, max_new_tokens, top_p, repetition_penalty)
382
+
383
  retrns = []
384
  retrn_start_idx = orig_code.find("return")
385
  while retrn_start_idx != -1:
 
394
  retrn_start_idx, retrn_end_idx = retrns[func_idx]
395
  model_context = orig_code[:retrn_start_idx] #TODO: maximal context?
396
  model_inp = model_context + "return"
397
+ pipe_generation = pipeline(model_inp, return_full_text=False, **generation_kwargs)[0]["generated_text"] #pipeline kwargs are missing?!
 
398
  altered_code = get_full_replacement(orig_code, retrn_start_idx+7, retrn_end_idx, pipe_generation)
399
 
400
  return altered_code
 
410
  char_idx += chr_idx
411
  return char_idx
412
 
413
+ def _combine_generation_kwargs(temperature, max_new_tokens, top_p, repetition_penalty):
414
+ gen_kwargs = {}
415
+ gen_kwargs["temperature"] = temperature
416
+ gen_kwargs["max_new_tokens"] = max_new_tokens
417
+ gen_kwargs["top_p"] = top_p
418
+ gen_kwargs["repetition_penalty"] = repetition_penalty
419
+ return gen_kwargs
420
 
421
+ def alter_body(old_code, func_id, funcs_list: list, temperature, max_new_tokens, top_p, repetition_penalty, pipeline=PIPE):
422
  """
423
  Replaces the body of a function with a generated one.
424
  Args:
 
438
  func_node = funcs_list[func_id]
439
  print(f"using for generation: {func_node=}")
440
 
441
+ generation_kwargs = _combine_generation_kwargs(temperature, max_new_tokens, top_p, repetition_penalty)
442
 
443
  print(f"{pipeline=}") # check if default even loaded
444
  if pipeline is None:
 
458
  # print(second_child.text.decode())
459
  model_context += " { \n " + second_child.text.decode()
460
  print(f"{model_context=}")
461
+ generation = pipeline(model_context, return_full_text=False, **generation_kwargs)[0]["generated_text"]
 
 
 
462
  print(f"{generation=}")
463
  ctx_with_generation = model_context + generation
464
  print(f"{ctx_with_generation=}")
 
499
  model_cp = gr.Textbox(value="Vipitis/santacoder-finetuned-Shadertoys-fine", label="Model Checkpoint (Enter to load!)", interactive=True)
500
  sample_idx = gr.Slider(minimum=0, maximum=num_samples, value=3211, label="pick sample from dataset", step=1.0)
501
  func_dropdown = gr.Dropdown(value=["0: edit the Code (or load a shader) to update this dropdown"], label="chose a function to modify") #breaks if I add a string in before that? #TODO: use type="index" to get int - always gives None?
502
+ with gr.Accordion("Advanced settings", open=False): # from: https://huggingface.co/spaces/bigcode/bigcode-playground/blob/main/app.py
503
+ with gr.Row():
504
+ column_1, column_2 = gr.Column(), gr.Column()
505
+ with column_1:
506
+ temperature = gr.Slider(
507
+ label="Temperature",
508
+ value=0.0, #start out at 0 to do greedy? or will there be an error?
509
+ minimum=0.0,
510
+ maximum=1.0,
511
+ step=0.05,
512
+ interactive=True,
513
+ info="Higher values produce more diverse outputs",
514
+ )
515
+ max_new_tokens = gr.Slider(
516
+ label="Max new tokens",
517
+ value=160,
518
+ minimum=0,
519
+ maximum=2048, #this could be inferred from the model?
520
+ step=32,
521
+ interactive=True,
522
+ info="The maximum numbers of new tokens",
523
+ )
524
+ with column_2:
525
+ top_p = gr.Slider(
526
+ label="Top-p (nucleus sampling)",
527
+ value=0.30,
528
+ minimum=0.0,
529
+ maximum=1,
530
+ step=0.05,
531
+ interactive=True,
532
+ info="Higher values sample more low-probability tokens",
533
+ )
534
+ repetition_penalty = gr.Slider(
535
+ label="Repetition penalty",
536
+ value=1.2,
537
+ minimum=1.0,
538
+ maximum=2.0,
539
+ step=0.05,
540
+ interactive=True,
541
+ info="Penalize repeated tokens",
542
+ )
543
  with gr.Row():
544
  gen_return_button = gr.Button("generate a alternate return statement", label="generate return")
545
  gen_func_button = gr.Button("generate an alternate function body", label="generate function")
 
546
  with gr.Row():
547
  with gr.Column():
548
  source_embed = gr.HTML('<iframe width="640" height="360" frameborder="0" src="" allowfullscreen></iframe>', label="How this shader originally renders")
 
560
  model_cp.submit(fn=_make_pipeline, inputs=[model_cp], outputs=[pipe]) # how can we trigger this on load?
561
  sample_idx.release(fn=grab_sample, inputs=[sample_idx], outputs=[sample_pass, sample_code, source_embed])
562
  gen_return_button.click(fn=alter_return, inputs=[sample_code, func_dropdown, pipe], outputs=[sample_code])
563
+ gen_func_button.click(fn=alter_body, inputs=[sample_code, func_dropdown, funcs, temperature, max_new_tokens, top_p, repetition_penalty, pipe], outputs=[sample_code, pipe])
564
  sample_code.change(fn=list_dropdown, inputs=[sample_code], outputs=[funcs, func_dropdown]) # to update this after generation, so spans aren't messed up
565
  sample_code.change(fn=make_iframe, inputs=[sample_code], outputs=[our_embed]) #twice could cause issues, find better ways.
566
  site.launch()