Spaces:
Running on Zero
Running on Zero
Upload app.py (#1)
Browse files- Upload app.py (9015a6079a70c1ceacba5d0ebf509c3a9d9be845)
Co-authored-by: Joao Gante <joaogante@users.noreply.huggingface.co>
173 unmodified lines17417517617717817918018118258 unmodified lines24124224324424524624725 unmodified lines2732742752762772782792802812822832842852862872882892902912924 unmodified lines2972982993003013023033042 unmodified lines3073083093103113123133143153163173183193203213222 unmodified lines3253263273283293302 unmodified lines33333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941048 unmodified lines4594604614624634644654664675 unmodified lines4734744754764774784794804814824834844854864874884894904914928 unmodified lines501502503504173 unmodified lines.nonselected-sequence {background-color: var(--primary-500);}.nopadding {padding-left: 0!important;}"""58 unmodified linesdef generate_html(start_sentence, original_tree):html_output = f"""<div class="custom-container"><div class="tree"><ul class="nopadding"> <li class="nopadding"> <a id='root'> <span> <b>{start_sentence}</b> </span> {original_tree.table} </a>"""html_output += "<ul> "for subnode in original_tree.children.values():html_output += generate_nodes(subnode, step=1)25 unmodified linesdef generate_beams(start_sentence, scores, length_penalty, decoded_sequences, beam_indexes_source):input_length = len(tokenizer([start_sentence], return_tensors="pt"))original_tree = BeamNode(cumulative_score=0,current_token_ix=None,table=None,current_sequence=start_sentence,children={},children_score_divider=((input_length + 1) ** length_penalty),total_score=None,is_final=False,is_selected_sequence=False,)n_beams = len(scores[0])beam_trees = [original_tree] * n_beamsfor step, step_scores in enumerate(scores):4 unmodified linesbeam_indexes,current_sequence,top_tokens,) = ([], [], [], [], [])for beam_ix in range(n_beams):current_beam = beam_trees[beam_ix]# skip if the beam is already final2 unmodified lines# Get top cumulative scores for the current beamcurrent_top_token_indexes = list(np.array(scores[step][beam_ix].argsort()[-n_beams:])[::-1])top_token_indexes += current_top_token_indexestop_cumulative_scores += list(np.array(scores[step][beam_ix][current_top_token_indexes])+ current_beam.cumulative_score)beam_indexes += [beam_ix] * n_beamscurrent_sequence += [beam_trees[beam_ix].current_sequence] * n_beamstop_tokens += [tokenizer.decode([el]) for el in current_top_token_indexes]top_df = pd.DataFrame.from_dict({2 unmodified lines"beam_index": beam_indexes,"current_sequence": current_sequence,"token": top_tokens,})maxes = top_df.groupby(["token_index", "current_sequence"])[2 unmodified linestop_df = top_df.loc[maxes]# Sort all top probabilities and keep top n_beamstop_df_selected = top_df.sort_values("cumulative_score", ascending=False).iloc[:n_beams]if any(["you enjoyed" in el for el in top_df["current_sequence"]]):print("Displaying debug info:::")print(top_df_selected)# Write the scores table - one per beam sourcefor beam_ix in reversed(list(range(n_beams))):current_beam = beam_trees[beam_ix]if current_beam.table is None:selected_tokens = top_df_selected.loc[top_df_selected["current_sequence"] == current_beam.current_sequence]markdown_table = generate_markdown_table(step_scores[beam_ix, :],current_beam.cumulative_score,current_beam.children_score_divider,chosen_tokens=list(selected_tokens["token"].values),)beam_trees[beam_ix].table = markdown_table# Add new children to each beamcumulative_scores = [beam.cumulative_score for beam in beam_trees]for _, row in top_df_selected.iterrows():# Update the source treesource_beam_ix = int(row["beam_index"])current_token_choice_ix = row["token_index"]current_token_choice = tokenizer.decode([current_token_choice_ix])cumulative_score = (cumulative_scores[source_beam_ix]+ scores[step][source_beam_ix][current_token_choice_ix].numpy())current_sequence = (beam_trees[source_beam_ix].current_sequence + current_token_choice)if current_token_choice_ix == 340:print("Found info:")print(f"We generate token '{current_token_choice}', and the total sequence is '{current_sequence}'")beam_trees[source_beam_ix].children[current_token_choice_ix] = BeamNode(current_token_ix=current_token_choice_ix,table=None,children={},current_sequence=current_sequence,cumulative_score=cumulative_score,total_score=cumulative_score/ ((input_length + step - 1) ** length_penalty),children_score_divider=((input_length + step) ** length_penalty),is_final=(step == len(scores) - 1or current_token_choice_ix == tokenizer.eos_token_id),is_selected_sequence=(current_sequence.replace("<|endoftext|>", "")in [el.replace("<|endoftext|>", "") for el in decoded_sequences]),)# Swap all beams by descending cumul score, so that n°1 has the highest cumulative score, and so onbeam_trees = [beam_trees[int(top_df_selected.iloc[beam_ix]["beam_index"])]for beam_ix in range(n_beams)]# Advance all beams by one tokenfor beam_ix in range(n_beams):current_token_choice_ix = top_df_selected.iloc[beam_ix]["token_index"]beam_trees[beam_ix] = beam_trees[beam_ix].children[current_token_choice_ix]return original_tree@spaces.GPU48 unmodified lines) as demo:gr.Markdown("""# <span style='color:var(--primary-500)!important'>Beam Search Visualizer</span>Play with the parameters below to understand how beam search decoding works!#### <span style='color:var(--primary-500)!important'>Parameters:</span>- **Sentence to decode from** (`inputs`): the input sequence to your decoder.- **Number of steps** (`max_new_tokens`): the number of tokens to generate.5 unmodified lines)text = gr.Textbox(label="Sentence to decode from",value="Thank you for",)with gr.Row():n_steps = gr.Slider(label="Number of steps", minimum=1, maximum=10, step=1, value=4)n_beams = gr.Slider(label="Number of beams", minimum=2, maximum=4, step=1, value=3)length_penalty = gr.Slider(label="Length penalty", minimum=-3, maximum=3, step=0.5, value=1)num_return_sequences = gr.Slider(label="Number of return sequences", minimum=1, maximum=3, step=1, value=2)n_beams.change(8 unmodified linesoutputs=[out_html, out_markdown],)demo.launch()173 unmodified lines17417517617717817958 unmodified lines23823924024124224324425 unmodified lines2702712722732742752762772782792802812822832842852862872882894 unmodified lines2942952962972982993003013023033042 unmodified lines3073083093103113123133143153163173183193203213223233242 unmodified lines3273283293303313323332 unmodified lines33633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942048 unmodified lines4694704714724734744754764775 unmodified lines4834844854864874884894904914924934944954964974984995005015028 unmodified lines511512513514173 unmodified lines.nonselected-sequence {background-color: var(--primary-500);}"""58 unmodified linesdef generate_html(start_sentence, original_tree):html_output = f"""<div class="custom-container"><div class="tree"><ul> <li> <a href='#' id='root'> <span> <b>{start_sentence}</b> </span> {original_tree.table} </a>"""html_output += "<ul> "for subnode in original_tree.children.values():html_output += generate_nodes(subnode, step=1)25 unmodified linesdef generate_beams(start_sentence, scores, length_penalty, decoded_sequences, beam_indexes_source):original_tree = BeamNode(cumulative_score=0,current_token_ix=None,table=None,current_sequence=start_sentence,children={},children_score_divider=(1 ** length_penalty),total_score=None,is_final=False,is_selected_sequence=False,)n_beams = len(scores[0])beam_trees = [original_tree] * n_beamsgeneration_length = len(scores)for step, step_scores in enumerate(scores):4 unmodified linesbeam_indexes,current_sequence,top_tokens,token_scores,) = ([], [], [], [], [], [])score_idx = 0for beam_ix in range(len(beam_trees)):current_beam = beam_trees[beam_ix]# skip if the beam is already final2 unmodified lines# Get top cumulative scores for the current beamcurrent_top_token_indexes = list(np.array(scores[step][score_idx].argsort()[-n_beams:])[::-1])top_token_indexes += current_top_token_indexestoken_scores += list(np.array(scores[step][score_idx][current_top_token_indexes]))top_cumulative_scores += list(np.array(scores[step][score_idx][current_top_token_indexes])+ current_beam.cumulative_score)beam_indexes += [beam_ix] * n_beamscurrent_sequence += [beam_trees[beam_ix].current_sequence] * n_beamstop_tokens += [tokenizer.decode([el]) for el in current_top_token_indexes]score_idx += 1top_df = pd.DataFrame.from_dict({2 unmodified lines"beam_index": beam_indexes,"current_sequence": current_sequence,"token": top_tokens,"token_score": token_scores,})maxes = top_df.groupby(["token_index", "current_sequence"])[2 unmodified linestop_df = top_df.loc[maxes]# Sort all top probabilities and keep top n_beams * 2 (* 2 because each beam may end this iteration, and we# want to keep at least `n_beams` beams alive)top_df_selected = top_df.sort_values("cumulative_score", ascending=False).iloc[:n_beams * 2]beams_to_keep = 0unfinished_beams = 0for _, row in top_df_selected.iterrows():beams_to_keep += 1current_token_choice_ix = row["token_index"]is_final = step == len(scores) - 1 or current_token_choice_ix == tokenizer.eos_token_idif not is_final:unfinished_beams += 1if unfinished_beams >= n_beams:breakif step == generation_length - 1 and beams_to_keep == n_beams:breaktop_df_selected_filtered = top_df_selected.iloc[:beams_to_keep]# Write the scores table - one per beam sourcescore_idx = 0for beam_ix in range(len(beam_trees)):current_beam = beam_trees[beam_ix]if current_beam.table is None:selected_tokens = top_df_selected_filtered.loc[top_df_selected_filtered["current_sequence"] == current_beam.current_sequence]markdown_table = generate_markdown_table(step_scores[score_idx, :],current_beam.cumulative_score,current_beam.children_score_divider,chosen_tokens=list(selected_tokens["token"].values),)beam_trees[beam_ix].table = markdown_tableif not current_beam.is_final:score_idx = min(score_idx + 1, n_beams - 1)# Add new children to each beamcumulative_scores = [beam.cumulative_score for beam in beam_trees]for _, row in top_df_selected_filtered.iterrows():# Update the source treesource_beam_ix = int(row["beam_index"])current_token_choice_ix = row["token_index"]current_token_choice = tokenizer.decode([current_token_choice_ix])token_scores = row["token_score"]cumulative_score = cumulative_scores[source_beam_ix] + np.asarray(token_scores)current_sequence = (beam_trees[source_beam_ix].current_sequence + current_token_choice)is_final = step == len(scores) - 1 or current_token_choice_ix == tokenizer.eos_token_idbeam_trees[source_beam_ix].children[current_token_choice_ix] = BeamNode(current_token_ix=current_token_choice_ix,table=None,children={},current_sequence=current_sequence,cumulative_score=cumulative_score,total_score=cumulative_score / (step + 1 ** length_penalty),children_score_divider=((step + 2) ** length_penalty),is_final=is_final,is_selected_sequence=(current_sequence.replace("<|endoftext|>", "")in [el.replace("<|endoftext|>", "") for el in decoded_sequences]),)# Swap all beams by descending cumul score, so that n°1 has the highest cumulative score, and so onbeam_trees = [beam_trees[int(top_df_selected_filtered.iloc[beam_ix]["beam_index"])]for beam_ix in range(beams_to_keep)]# Advance all beams by one tokenfor beam_ix in range(beams_to_keep):current_token_choice_ix = top_df_selected_filtered.iloc[beam_ix]["token_index"]beam_trees[beam_ix] = beam_trees[beam_ix].children[current_token_choice_ix]print(f"Step {step}, beams kept: {beams_to_keep}")return original_tree@spaces.GPU48 unmodified lines) as demo:gr.Markdown("""# <span style='color:var(--primary-500)!important'>Beam Search Visualizer</span>Play with the parameters below to understand how beam search decoding works!#### <span style='color:var(--primary-500)!important'>Parameters:</span>- **Sentence to decode from** (`inputs`): the input sequence to your decoder.- **Number of steps** (`max_new_tokens`): the number of tokens to generate.5 unmodified lines)text = gr.Textbox(label="Sentence to decode from",value="Conclusion: thanks a lot. That's all for today",)with gr.Row():n_steps = gr.Slider(label="Number of steps", minimum=1, maximum=10, step=1, value=10)n_beams = gr.Slider(label="Number of beams", minimum=2, maximum=4, step=1, value=4)length_penalty = gr.Slider(label="Length penalty", minimum=-3, maximum=3, step=0.5, value=1)num_return_sequences = gr.Slider(label="Number of return sequences", minimum=1, maximum=4, step=1, value=3)n_beams.change(8 unmodified linesoutputs=[out_html, out_markdown],)demo.launch()
app.py+56-46