import%20marimo%0A%0A__generated_with%20%3D%20%220.24.0%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20plotly.graph_objects%20as%20go%0A%20%20%20%20from%20plotly.subplots%20import%20make_subplots%0A%0A%20%20%20%20return%20go%2C%20make_subplots%2C%20mo%2C%20np%2C%20pd%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%5B%E2%86%90%2053%20LayerNorm%20vs%20RMSNorm%5D(53_layer_and_rms_normalization.py)%20%7C%20%5BIndex%5D(..%2Findex.html)%20%7C%20%5B55%20Perplexity%20%E2%86%92%5D(55_perplexity.py)%0A%0A%20%20%20%20%23%2054.%20Autoregressive%20Decoding%20Strategies%3A%20Greedy%20Search%2C%20Beam%20Search%2C%20Top-k%2C%20and%20Nucleus%20(Top-p)%20Sampling%0A%0A%20%20%20%20%23%23%23%20Executive%20Summary%0A%0A%20%20%20%20In%20autoregressive%20language%20models%2C%20the%20forward%20pass%20outputs%20an%20unnormalized%20logit%20vector%20%24z_%7Bt%2B1%7D%20%5Cin%20%5Cmathbb%7BR%7D%5E%7B%7C%5Cmathcal%7BV%7D%7C%7D%24%20representing%20raw%20affinities%20across%20a%20vocabulary%20%24%5Cmathcal%7BV%7D%24%20of%20%2432%7B%2C%7D000%24%20to%20%24128%7B%2C%7D000%24%20tokens.%20Transforming%20these%20raw%20probabilities%20into%20coherent%2C%20fluent%2C%20and%20diverse%20natural%20language%20is%20the%20core%20objective%20of%20**decoding%20strategies**.%0A%0A%20%20%20%20The%20choice%20of%20decoding%20algorithm%20governs%20the%20fundamental%20trade-off%20between%20**coherence**%20(optimizing%20sequence%20likelihood)%20and%20**creativity**%20(avoiding%20repetitive%20loops%20and%20degenerate%20attractors)%3A%0A%20%20%20%20-%20**Deterministic%20Search%20(Greedy%20and%20Beam%20Search)**%3A%20Optimizes%20for%20global%20likelihood%2C%20making%20it%20ideal%20for%20closed-domain%2C%20high-precision%20tasks%20like%20machine%20translation%2C%20mathematical%20reasoning%2C%20and%20code%20synthesis.%20However%2C%20on%20open-ended%20text%20generation%2C%20it%20frequently%20degrades%20into%20repetitive%2C%20generic%20phrases.%0A%20%20%20%20-%20**Stochastic%20Truncation%20(Top-k%20and%20Top-p%20%2F%20Nucleus%20Sampling)**%3A%20Dynamically%20eliminates%20the%20unreliable%2C%20low-probability%20tail%20of%20the%20vocabulary%20distribution%20while%20preserving%20controlled%20stochasticity%2C%20achieving%20human-like%20lexical%20diversity.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20%5Bb%5D%20Mathematical%20Formulations%20of%20Decoding%20Strategies%0A%0A%20%20%20%20%23%23%23%201.%20The%20Decoding%20Optimization%20Objective%0A%0A%20%20%20%20Given%20a%20conditioning%20prompt%20sequence%20%24x_%7B1%3At%7D%20%3D%20(x_1%2C%20x_2%2C%20%5Cdots%2C%20x_t)%24%2C%20the%20model%20computes%20conditional%20token%20probabilities%3A%0A%0A%20%20%20%20%24%24P(w%20%5Cmid%20x_%7B1%3At%7D)%20%3D%20%5Cfrac%7B%5Cexp(z_w%20%2F%20T)%7D%7B%5Csum_%7Bv%20%5Cin%20%5Cmathcal%7BV%7D%7D%20%5Cexp(z_v%20%2F%20T)%7D%24%24%0A%0A%20%20%20%20The%20theoretically%20optimal%20sequence%20continuation%20%24y%5E*%20%3D%20(y_1%2C%20y_2%2C%20%5Cdots%2C%20y_L)%24%20maximizes%20the%20joint%20sequence%20log-likelihood%3A%0A%0A%20%20%20%20%24%24y%5E*%20%3D%20%5Carg%5Cmax_%7By_%7B1%3AL%7D%7D%20%5Csum_%7Bi%3D1%7D%5EL%20%5Cln%20P(y_i%20%5Cmid%20x_%7B1%3At%7D%2C%20y_%7B%3Ci%7D)%24%24%0A%0A%20%20%20%20Because%20the%20search%20space%20%24%7C%5Cmathcal%7BV%7D%7C%5EL%24%20is%20astronomically%20large%20(e.g.%2C%20%2450%7B%2C%7D000%5E%7B20%7D%20%5Capprox%2010%5E%7B94%7D%24%20states)%2C%20exact%20global%20optimization%20is%20NP-hard%2C%20necessitating%20heuristic%20approximations.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%202.%20Deterministic%20Search%20Strategies%0A%0A%20%20%20%20%23%23%23%23%20A.%20Greedy%20Search%0A%20%20%20%20Greedy%20search%20selects%20the%20single%20most%20probable%20token%20at%20each%20individual%20step%3A%0A%0A%20%20%20%20%24%24y_i%20%3D%20%5Carg%5Cmax_%7Bw%20%5Cin%20%5Cmathcal%7BV%7D%7D%20P(w%20%5Cmid%20x_%7B1%3At%7D%2C%20y_%7B%3Ci%7D)%24%24%0A%0A%20%20%20%20-%20**Computational%20Complexity**%3A%20%24%5Cmathcal%7BO%7D(L%20%5Ccdot%20%7C%5Cmathcal%7BV%7D%7C)%24%20FLOPs%20(extremely%20fast).%0A%20%20%20%20-%20**Failure%20Mode**%3A%20Myopic%20blindness.%20An%20initially%20lower-probability%20token%20might%20unlock%20a%20subsequent%20trajectory%20of%20significantly%20higher%20joint%20likelihood.%20Once%20greedy%20search%20commits%20to%20an%20early%20branch%2C%20it%20can%20never%20backtrack.%0A%0A%20%20%20%20%23%23%23%23%20B.%20Beam%20Search%0A%20%20%20%20Beam%20search%20maintains%20a%20fixed%20number%20%24B%24%20(the%20beam%20width)%20of%20highest-scoring%20partial%20sequences%20at%20each%20time%20step.%20At%20step%20%24i%24%2C%20each%20of%20the%20%24B%24%20active%20hypotheses%20is%20expanded%20into%20all%20%24%7C%5Cmathcal%7BV%7D%7C%24%20vocabulary%20candidates%2C%20yielding%20%24B%20%5Ccdot%20%7C%5Cmathcal%7BV%7D%7C%24%20candidate%20paths.%20Only%20the%20top%20%24B%24%20paths%20under%20a%20length-normalized%20log-likelihood%20score%20are%20retained%3A%0A%0A%20%20%20%20%24%24%5Coperatorname%7BScore%7D(y_%7B1%3Ai%7D)%20%3D%20%5Cfrac%7B1%7D%7Bi%5E%5Calpha%7D%20%5Csum_%7Bj%3D1%7D%5Ei%20%5Cln%20P(y_j%20%5Cmid%20x_%7B1%3At%7D%2C%20y_%7B%3Cj%7D)%24%24%0A%0A%20%20%20%20where%20%24%5Calpha%20%5Cin%20%5B0.6%2C%201.0%5D%24%20is%20a%20length%20penalty%20parameter%20that%20counteracts%20the%20natural%20probabilistic%20bias%20toward%20shorter%20sequences.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%203.%20Stochastic%20Truncation%20Strategies%0A%0A%20%20%20%20%23%23%23%23%20A.%20Top-k%20Sampling%20(Fan%20et%20al.%2C%202018)%0A%20%20%20%20Standard%20temperature%20sampling%20from%20the%20raw%20distribution%20%24P(w)%24%20risks%20drawing%20tokens%20from%20the%20vast%2C%20unreliable%20long%20tail.%20Top-k%20sampling%20filters%20the%20vocabulary%20down%20to%20the%20%24k%24%20most%20probable%20tokens%3A%0A%0A%20%20%20%20%24%24%5Cmathcal%7BV%7D%5E%7B(k)%7D%20%3D%20%5Cleft%5C%7B%20w%20%5Cin%20%5Cmathcal%7BV%7D%20%5Cmid%20%5Coperatorname%7Brank%7D(P(w))%20%5Cle%20k%20%5Cright%5C%7D%24%24%0A%0A%20%20%20%20Probabilities%20are%20renormalized%20strictly%20over%20%24%5Cmathcal%7BV%7D%5E%7B(k)%7D%24%3A%0A%0A%20%20%20%20%24%24P'(w)%20%3D%20%5Cbegin%7Bcases%7D%20%5Cfrac%7BP(w)%7D%7B%5Csum_%7Bv%20%5Cin%20%5Cmathcal%7BV%7D%5E%7B(k)%7D%7D%20P(v)%7D%20%26%20%5Ctext%7Bif%20%7D%20w%20%5Cin%20%5Cmathcal%7BV%7D%5E%7B(k)%7D%20%5C%5C%200%20%26%20%5Ctext%7Botherwise%7D%20%5Cend%7Bcases%7D%24%24%0A%0A%20%20%20%20-%20**Pathology%20of%20Fixed%20%24k%24**%3A%20The%20distribution's%20shape%20changes%20dynamically%20across%20contexts.%20In%20rigid%20syntactic%20contexts%20(e.g.%2C%20%60%22The%20capital%20of%20France%20is%22%60)%2C%20only%201%20token%20is%20valid%2C%20yet%20%24k%3D50%24%20permits%20low-probability%20tokens.%20In%20open%20creative%20contexts%2C%20100%2B%20tokens%20may%20be%20valid%2C%20yet%20%24k%3D50%24%20prematurely%20truncates%20valid%20synonyms.%0A%0A%20%20%20%20%23%23%23%23%20B.%20Nucleus%20(Top-p)%20Sampling%20(Holtzman%20et%20al.%2C%202019)%0A%20%20%20%20Nucleus%20sampling%20dynamically%20adjusts%20the%20candidate%20pool%20size%20by%20selecting%20the%20smallest%20subset%20of%20tokens%20whose%20cumulative%20probability%20mass%20exceeds%20threshold%20%24p%20%5Cin%20(0%2C%201)%24%3A%0A%0A%20%20%20%20%24%24%5Cmathcal%7BV%7D%5E%7B(p)%7D%20%3D%20%5Carg%5Cmin_%7BS%20%5Csubseteq%20%5Cmathcal%7BV%7D%7D%20%7CS%7C%20%5Cquad%20%5Ctext%7Bsubject%20to%7D%20%5Cquad%20%5Csum_%7Bw%20%5Cin%20S%7D%20P(w)%20%5Cge%20p%24%24%0A%0A%20%20%20%20Tokens%20outside%20%24%5Cmathcal%7BV%7D%5E%7B(p)%7D%24%20are%20assigned%20zero%20probability%2C%20and%20remaining%20tokens%20are%20renormalized%3A%0A%0A%20%20%20%20%24%24P'(w)%20%3D%20%5Cbegin%7Bcases%7D%20%5Cfrac%7BP(w)%7D%7B%5Csum_%7Bv%20%5Cin%20%5Cmathcal%7BV%7D%5E%7B(p)%7D%7D%20P(v)%7D%20%26%20%5Ctext%7Bif%20%7D%20w%20%5Cin%20%5Cmathcal%7BV%7D%5E%7B(p)%7D%20%5C%5C%200%20%26%20%5Ctext%7Botherwise%7D%20%5Cend%7Bcases%7D%24%24%0A%0A%20%20%20%20-%20**Self-Adjusting%20Head**%3A%20When%20the%20model%20is%20confident%20(peaked%20entropy)%2C%20%24%7C%5Cmathcal%7BV%7D%5E%7B(p)%7D%7C%24%20automatically%20contracts%20to%20%241%24%20or%20%242%24%20tokens.%20When%20the%20model%20is%20uncertain%20(flat%20entropy)%2C%20%24%7C%5Cmathcal%7BV%7D%5E%7B(p)%7D%7C%24%20dynamically%20expands%20to%20dozens%20of%20tokens%2C%20preventing%20both%20wild%20tail%20errors%20and%20premature%20truncation.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(go%2C%20make_subplots%2C%20mo%2C%20np)%3A%0A%20%20%20%20%23%20Panel%201%3A%20Interactive%20Search%20Tree%20Comparing%20Greedy%20vs%20Beam%20Search%20(B%3D2)%0A%20%20%20%20%23%20Tree%20nodes%3A%20(x%2C%20y%2C%20label%2C%20cumulative_log_prob%2C%20path_type)%0A%20%20%20%20%23%20Root%0A%20%20%20%20tree_nodes%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%7B%22x%22%3A%200.0%2C%20%22y%22%3A%203.0%2C%20%22label%22%3A%20%22Prompt%3A%20'The'%22%2C%20%22score%22%3A%200.0%2C%20%22type%22%3A%20%22root%22%7D%2C%0A%20%20%20%20%20%20%20%20%23%20Step%201%0A%20%20%20%20%20%20%20%20%7B%22x%22%3A%20-2.0%2C%20%22y%22%3A%202.0%2C%20%22label%22%3A%20%22'dog'%20(p%3D0.60)%22%2C%20%22score%22%3A%20np.log(0.60)%2C%20%22type%22%3A%20%22beam%2Bgreedy%22%7D%2C%0A%20%20%20%20%20%20%20%20%7B%22x%22%3A%202.0%2C%20%22y%22%3A%202.0%2C%20%22label%22%3A%20%22'galaxy'%20(p%3D0.40)%22%2C%20%22score%22%3A%20np.log(0.40)%2C%20%22type%22%3A%20%22beam_only%22%7D%2C%0A%20%20%20%20%20%20%20%20%23%20Step%202%20from%20'dog'%0A%20%20%20%20%20%20%20%20%7B%22x%22%3A%20-3.0%2C%20%22y%22%3A%201.0%2C%20%22label%22%3A%20%22'barks'%20(p%3D0.30)%22%2C%20%22score%22%3A%20np.log(0.60)%20%2B%20np.log(0.30)%2C%20%22type%22%3A%20%22greedy%22%7D%2C%0A%20%20%20%20%20%20%20%20%7B%22x%22%3A%20-1.0%2C%20%22y%22%3A%201.0%2C%20%22label%22%3A%20%22'runs'%20(p%3D0.20)%22%2C%20%22score%22%3A%20np.log(0.60)%20%2B%20np.log(0.20)%2C%20%22type%22%3A%20%22pruned%22%7D%2C%0A%20%20%20%20%20%20%20%20%23%20Step%202%20from%20'galaxy'%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22x%22%3A%201.0%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22y%22%3A%201.0%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22label%22%3A%20%22'rotates'%20(p%3D0.90)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22score%22%3A%20np.log(0.40)%20%2B%20np.log(0.90)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22type%22%3A%20%22beam_winner%22%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7B%22x%22%3A%203.0%2C%20%22y%22%3A%201.0%2C%20%22label%22%3A%20%22'shines'%20(p%3D0.08)%22%2C%20%22score%22%3A%20np.log(0.40)%20%2B%20np.log(0.08)%2C%20%22type%22%3A%20%22pruned%22%7D%2C%0A%20%20%20%20%5D%0A%0A%20%20%20%20%23%20Tree%20edges%0A%20%20%20%20tree_edges%20%3D%20%5B%0A%20%20%20%20%20%20%20%20(0%2C%201%2C%20%22Greedy%20%26%20Beam%20Choice%22)%2C%0A%20%20%20%20%20%20%20%20(0%2C%202%2C%20%22Beam%20Retained%20(Rank%202)%22)%2C%0A%20%20%20%20%20%20%20%20(1%2C%203%2C%20%22Greedy%20Path%20Continues%22)%2C%0A%20%20%20%20%20%20%20%20(1%2C%204%2C%20%22Pruned%22)%2C%0A%20%20%20%20%20%20%20%20(2%2C%205%2C%20%22Beam%20Global%20Winner%22)%2C%0A%20%20%20%20%20%20%20%20(2%2C%206%2C%20%22Pruned%22)%2C%0A%20%20%20%20%5D%0A%0A%20%20%20%20fig%20%3D%20make_subplots(%0A%20%20%20%20%20%20%20%20rows%3D1%2C%0A%20%20%20%20%20%20%20%20cols%3D2%2C%0A%20%20%20%20%20%20%20%20subplot_titles%3D%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%3Cb%3EGreedy%20vs%20Beam%20Search%20(B%3D2)%20Exploration%20Tree%3C%2Fb%3E%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%3Cb%3ETop-k%20(k%3D5)%20vs%20Nucleus%20Top-p%20(p%3D0.85)%20Truncation%3C%2Fb%3E%22%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20horizontal_spacing%3D0.14%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Panel%201%3A%20Tree%20Edges%0A%20%20%20%20for%20start_idx%2C%20end_idx%2C%20edge_lbl%20in%20tree_edges%3A%0A%20%20%20%20%20%20%20%20n1%20%3D%20tree_nodes%5Bstart_idx%5D%0A%20%20%20%20%20%20%20%20n2%20%3D%20tree_nodes%5Bend_idx%5D%0A%20%20%20%20%20%20%20%20is_winner%20%3D%20%22Beam%20Global%20Winner%22%20in%20edge_lbl%0A%20%20%20%20%20%20%20%20is_greedy%20%3D%20%22Greedy%22%20in%20edge_lbl%20and%20not%20is_winner%0A%20%20%20%20%20%20%20%20line_color%20%3D%20%22%230D9488%22%20if%20is_winner%20else%20(%22%23DC2626%22%20if%20is_greedy%20else%20%22%239CA3AF%22)%0A%20%20%20%20%20%20%20%20line_width%20%3D%203.0%20if%20(is_winner%20or%20is_greedy)%20else%201.5%0A%0A%20%20%20%20%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%3D%5Bn1%5B%22x%22%5D%2C%20n2%5B%22x%22%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5Bn1%5B%22y%22%5D%2C%20n2%5B%22y%22%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3Dline_color%2C%20width%3Dline_width)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hoverinfo%3D%22none%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20showlegend%3DFalse%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20col%3D1%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%23%20Panel%201%3A%20Tree%20Nodes%0A%20%20%20%20for%20node%20in%20tree_nodes%3A%0A%20%20%20%20%20%20%20%20node_color%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%231D4ED8%22%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20node%5B%22type%22%5D%20%3D%3D%20%22root%22%0A%20%20%20%20%20%20%20%20%20%20%20%20else%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%230D9488%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20node%5B%22type%22%5D%20%3D%3D%20%22beam_winner%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%20(%22%23DC2626%22%20if%20node%5B%22type%22%5D%20%3D%3D%20%22greedy%22%20else%20%22%236B7280%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%3D%5Bnode%5B%22x%22%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5Bnode%5B%22y%22%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%2Btext%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D28%2C%20color%3Dnode_color)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20text%3D%5Bf%22%7Bnode%5B'label'%5D%7D%3Cbr%3EScore%3A%20%7Bnode%5B'score'%5D%3A.2f%7D%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20textposition%3D%22bottom%20center%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%3Dnode%5B%22label%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20showlegend%3DFalse%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20col%3D1%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%23%20Panel%202%3A%20Truncation%20Dynamics%20(Zipfian%20Distribution)%0A%20%20%20%20np.random.seed(42)%0A%20%20%20%20demo_vocab%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%22the%22%2C%0A%20%20%20%20%20%20%20%20%22quick%22%2C%0A%20%20%20%20%20%20%20%20%22brown%22%2C%0A%20%20%20%20%20%20%20%20%22fox%22%2C%0A%20%20%20%20%20%20%20%20%22jumps%22%2C%0A%20%20%20%20%20%20%20%20%22over%22%2C%0A%20%20%20%20%20%20%20%20%22lazy%22%2C%0A%20%20%20%20%20%20%20%20%22dog%22%2C%0A%20%20%20%20%20%20%20%20%22quietly%22%2C%0A%20%20%20%20%20%20%20%20%22swiftly%22%2C%0A%20%20%20%20%20%20%20%20%22bright%22%2C%0A%20%20%20%20%20%20%20%20%22moon%22%2C%0A%20%20%20%20%20%20%20%20%22river%22%2C%0A%20%20%20%20%20%20%20%20%22forest%22%2C%0A%20%20%20%20%20%20%20%20%22silent%22%2C%0A%20%20%20%20%5D%0A%20%20%20%20raw_logits%20%3D%20np.array(%5B5.2%2C%204.8%2C%204.3%2C%204.1%2C%203.8%2C%203.2%2C%202.7%2C%202.4%2C%201.8%2C%201.3%2C%200.8%2C%200.4%2C%20-0.1%2C%20-0.7%2C%20-1.5%5D)%0A%20%20%20%20probs%20%3D%20np.exp(raw_logits)%20%2F%20np.sum(np.exp(raw_logits))%0A%20%20%20%20cum_probs%20%3D%20np.cumsum(probs)%0A%0A%20%20%20%20k_val%20%3D%205%0A%20%20%20%20p_val%20%3D%200.85%0A%0A%20%20%20%20bar_colors%20%3D%20%5B%5D%0A%20%20%20%20for%20idx%20in%20range(len(probs))%3A%0A%20%20%20%20%20%20%20%20in_top_k%20%3D%20idx%20%3C%20k_val%0A%20%20%20%20%20%20%20%20in_top_p%20%3D%20cum_probs%5Bidx%5D%20%3C%3D%20p_val%20or%20(idx%20%3E%200%20and%20cum_probs%5Bidx%20-%201%5D%20%3C%20p_val)%0A%0A%20%20%20%20%20%20%20%20if%20in_top_k%20and%20in_top_p%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20bar_colors.append(%22%231D4ED8%22)%20%20%23%20Both%20(Blue)%0A%20%20%20%20%20%20%20%20elif%20in_top_p%20and%20not%20in_top_k%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20bar_colors.append(%22%230D9488%22)%20%20%23%20Top-p%20only%20(Teal)%0A%20%20%20%20%20%20%20%20elif%20in_top_k%20and%20not%20in_top_p%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20bar_colors.append(%22%23F59E0B%22)%20%20%23%20Top-k%20only%20(Amber)%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20bar_colors.append(%22%23E5E7EB%22)%20%20%23%20Truncated%20tail%20(Grey)%0A%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Bar(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Ddemo_vocab%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dprobs%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker_color%3Dbar_colors%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Token%20Probability%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20showlegend%3DFalse%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D2%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Cumulative%20curve%20on%20secondary%20axis%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Ddemo_vocab%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dcum_probs%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%2Bmarkers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23DC2626%22%2C%20width%3D2.0)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Cumulative%20Probability%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D2%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Threshold%20horizontal%20line%0A%20%20%20%20fig.add_hline(%0A%20%20%20%20%20%20%20%20y%3Dp_val%2C%0A%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23DC2626%22%2C%20dash%3D%22dash%22%2C%20width%3D1.5)%2C%0A%20%20%20%20%20%20%20%20annotation_text%3Df%22Nucleus%20Cutoff%20p%20%3D%20%7Bp_val%7D%22%2C%0A%20%20%20%20%20%20%20%20annotation_position%3D%22bottom%20right%22%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D2%2C%0A%20%20%20%20)%0A%0A%20%20%20%20fig.update_xaxes(showgrid%3DFalse%2C%20zeroline%3DFalse%2C%20showticklabels%3DFalse%2C%20row%3D1%2C%20col%3D1)%0A%20%20%20%20fig.update_yaxes(showgrid%3DFalse%2C%20zeroline%3DFalse%2C%20showticklabels%3DFalse%2C%20range%3D%5B0.3%2C%203.4%5D%2C%20row%3D1%2C%20col%3D1)%0A%20%20%20%20fig.update_xaxes(title_text%3D%22Vocabulary%20Tokens%20(Ranked%20by%20Probability)%22%2C%20tickangle%3D-45%2C%20row%3D1%2C%20col%3D2)%0A%20%20%20%20fig.update_yaxes(title_text%3D%22Probability%20%2F%20Cumulative%20Mass%22%2C%20range%3D%5B0%2C%201.05%5D%2C%20row%3D1%2C%20col%3D2)%0A%0A%20%20%20%20fig.update_layout(%0A%20%20%20%20%20%20%20%20template%3D%22plotly_white%22%2C%0A%20%20%20%20%20%20%20%20height%3D540%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D40%2C%20r%3D40%2C%20t%3D70%2C%20b%3D70)%2C%0A%20%20%20%20%20%20%20%20legend%3Ddict(orientation%3D%22h%22%2C%20yanchor%3D%22bottom%22%2C%20y%3D-0.3%2C%20xanchor%3D%22center%22%2C%20x%3D0.75)%2C%0A%20%20%20%20)%0A%0A%20%20%20%20viz%20%3D%20mo.ui.plotly(fig)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo%2C%20np%2C%20pd)%3A%0A%20%20%20%20%23%20Vectorized%20NumPy%20Implementation%20of%20Decoding%20Algorithms%0A%20%20%20%20%23%201.%20Greedy%20Search%0A%20%20%20%20def%20numpy_greedy_decode(log_prob_func%2C%20initial_token_id%2C%20max_length%3D5)%3A%0A%20%20%20%20%20%20%20%20sequence%20%3D%20%5Binitial_token_id%5D%0A%20%20%20%20%20%20%20%20total_log_prob%20%3D%200.0%0A%20%20%20%20%20%20%20%20for%20_%20in%20range(max_length)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20logits%20%3D%20log_prob_func(sequence)%0A%20%20%20%20%20%20%20%20%20%20%20%20next_token%20%3D%20int(np.argmax(logits))%0A%20%20%20%20%20%20%20%20%20%20%20%20probs%20%3D%20np.exp(logits%20-%20np.max(logits))%0A%20%20%20%20%20%20%20%20%20%20%20%20probs%20%2F%3D%20np.sum(probs)%0A%20%20%20%20%20%20%20%20%20%20%20%20total_log_prob%20%2B%3D%20np.log(max(probs%5Bnext_token%5D%2C%201e-12))%0A%20%20%20%20%20%20%20%20%20%20%20%20sequence.append(next_token)%0A%20%20%20%20%20%20%20%20return%20sequence%2C%20total_log_prob%0A%0A%20%20%20%20%23%202.%20Vectorized%20Beam%20Search%20Engine%0A%20%20%20%20def%20numpy_beam_search(log_prob_func%2C%20initial_token_id%2C%20beam_width%3D3%2C%20max_length%3D5%2C%20length_penalty%3D0.7)%3A%0A%20%20%20%20%20%20%20%20%23%20Candidates%3A%20list%20of%20tuples%20(sequence_list%2C%20cumulative_log_prob)%0A%20%20%20%20%20%20%20%20beams%20%3D%20%5B(%5Binitial_token_id%5D%2C%200.0)%5D%0A%0A%20%20%20%20%20%20%20%20for%20_step%20in%20range(max_length)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20all_candidates%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20seq%2C%20cum_log_prob%20in%20beams%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logits%20%3D%20log_prob_func(seq)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20probs%20%3D%20np.exp(logits%20-%20np.max(logits))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20probs%20%2F%3D%20np.sum(probs)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20top_indices%20%3D%20np.argsort(probs)%5B%3A%3A-1%5D%5B%3Abeam_width%5D%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20tok%20in%20top_indices%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cand_seq%20%3D%20seq%20%2B%20%5Bint(tok)%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cand_log_prob%20%3D%20cum_log_prob%20%2B%20np.log(max(probs%5Btok%5D%2C%201e-12))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20Apply%20length%20normalization%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20norm_score%20%3D%20cand_log_prob%20%2F%20(len(cand_seq)%20**%20length_penalty)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20all_candidates.append((cand_seq%2C%20cand_log_prob%2C%20norm_score))%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Select%20top%20B%20candidates%0A%20%20%20%20%20%20%20%20%20%20%20%20all_candidates.sort(key%3Dlambda%20x%3A%20x%5B2%5D%2C%20reverse%3DTrue)%0A%20%20%20%20%20%20%20%20%20%20%20%20beams%20%3D%20%5B(cand%5B0%5D%2C%20cand%5B1%5D)%20for%20cand%20in%20all_candidates%5B%3Abeam_width%5D%5D%0A%0A%20%20%20%20%20%20%20%20best_seq%2C%20best_log_prob%20%3D%20beams%5B0%5D%0A%20%20%20%20%20%20%20%20return%20best_seq%2C%20best_log_prob%0A%0A%20%20%20%20%23%203.%20Top-k%20and%20Nucleus%20(Top-p)%20Sampling%20Engine%0A%20%20%20%20def%20sample_next_token(logits%2C%20temperature%3D1.0%2C%20top_k%3D0%2C%20top_p%3D0.0)%3A%0A%20%20%20%20%20%20%20%20scaled_logits%20%3D%20logits%20%2F%20max(temperature%2C%201e-5)%0A%0A%20%20%20%20%20%20%20%20%23%20Apply%20Top-k%20truncation%0A%20%20%20%20%20%20%20%20if%20top_k%20%3E%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20top_k_indices%20%3D%20np.argsort(scaled_logits)%5B%3A%3A-1%5D%5B%3Atop_k%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20masked_logits%20%3D%20np.full_like(scaled_logits%2C%20-1e9)%0A%20%20%20%20%20%20%20%20%20%20%20%20masked_logits%5Btop_k_indices%5D%20%3D%20scaled_logits%5Btop_k_indices%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20scaled_logits%20%3D%20masked_logits%0A%0A%20%20%20%20%20%20%20%20%23%20Apply%20Top-p%20Nucleus%20truncation%0A%20%20%20%20%20%20%20%20if%200.0%20%3C%20top_p%20%3C%201.0%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20sorted_indices%20%3D%20np.argsort(scaled_logits)%5B%3A%3A-1%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20sorted_logits%20%3D%20scaled_logits%5Bsorted_indices%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20exp_l%20%3D%20np.exp(sorted_logits%20-%20np.max(sorted_logits))%0A%20%20%20%20%20%20%20%20%20%20%20%20cum_probs%20%3D%20np.cumsum(exp_l%20%2F%20np.sum(exp_l))%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Identify%20cutoff%0A%20%20%20%20%20%20%20%20%20%20%20%20cutoff_mask%20%3D%20cum_probs%20%3E%20top_p%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Keep%20at%20least%20one%20token%0A%20%20%20%20%20%20%20%20%20%20%20%20cutoff_mask%5B1%3A%5D%20%3D%20cutoff_mask%5B%3A-1%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20cutoff_mask%5B0%5D%20%3D%20False%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20filtered_indices%20%3D%20sorted_indices%5Bcutoff_mask%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20scaled_logits%5Bfiltered_indices%5D%20%3D%20-1e9%0A%0A%20%20%20%20%20%20%20%20%23%20Softmax%20and%20categorical%20sampling%0A%20%20%20%20%20%20%20%20exp_logits%20%3D%20np.exp(scaled_logits%20-%20np.max(scaled_logits))%0A%20%20%20%20%20%20%20%20sample_probs%20%3D%20exp_logits%20%2F%20np.sum(exp_logits)%0A%20%20%20%20%20%20%20%20sampled_token%20%3D%20np.random.choice(len(sample_probs)%2C%20p%3Dsample_probs)%0A%20%20%20%20%20%20%20%20return%20int(sampled_token)%2C%20sample_probs%0A%0A%20%20%20%20%23%20Simulation%20setup%3A%20Toy%20Transition%20Model%20with%2010%20tokens%0A%20%20%20%20vocab_map%20%3D%20%7B0%3A%20%22%3Cs%3E%22%2C%201%3A%20%22The%22%2C%202%3A%20%22dog%22%2C%203%3A%20%22galaxy%22%2C%204%3A%20%22barks%22%2C%205%3A%20%22rotates%22%2C%206%3A%20%22loudly%22%2C%207%3A%20%22fast%22%2C%208%3A%20%22in%22%2C%209%3A%20%22space%22%7D%0A%20%20%20%20inv_vocab%20%3D%20%7Bv%3A%20k%20for%20k%2C%20v%20in%20vocab_map.items()%7D%0A%0A%20%20%20%20def%20synthetic_model_logits(seq)%3A%0A%20%20%20%20%20%20%20%20last_tok%20%3D%20seq%5B-1%5D%0A%20%20%20%20%20%20%20%20logits%20%3D%20np.zeros(len(vocab_map))%0A%20%20%20%20%20%20%20%20if%20last_tok%20%3D%3D%200%3A%20%20%23%20%3Cs%3E%20-%3E%20%22The%22%0A%20%20%20%20%20%20%20%20%20%20%20%20logits%5B1%5D%20%3D%206.0%0A%20%20%20%20%20%20%20%20elif%20last_tok%20%3D%3D%201%3A%20%20%23%20%22The%22%20-%3E%20%22dog%22%20(0.6)%20or%20%22galaxy%22%20(0.4)%0A%20%20%20%20%20%20%20%20%20%20%20%20logits%5B2%5D%20%3D%202.0%20%20%23%20dog%0A%20%20%20%20%20%20%20%20%20%20%20%20logits%5B3%5D%20%3D%201.6%20%20%23%20galaxy%0A%20%20%20%20%20%20%20%20elif%20last_tok%20%3D%3D%202%3A%20%20%23%20%22dog%22%20-%3E%20%22barks%22%20(0.4)%20or%20%22loudly%22%20(0.2)%0A%20%20%20%20%20%20%20%20%20%20%20%20logits%5B4%5D%20%3D%202.0%20%20%23%20barks%0A%20%20%20%20%20%20%20%20%20%20%20%20logits%5B6%5D%20%3D%201.3%20%20%23%20loudly%0A%20%20%20%20%20%20%20%20elif%20last_tok%20%3D%3D%203%3A%20%20%23%20%22galaxy%22%20-%3E%20%22rotates%22%20(0.85)%0A%20%20%20%20%20%20%20%20%20%20%20%20logits%5B5%5D%20%3D%204.5%20%20%23%20rotates%0A%20%20%20%20%20%20%20%20%20%20%20%20logits%5B9%5D%20%3D%200.5%20%20%23%20space%0A%20%20%20%20%20%20%20%20elif%20last_tok%20%3D%3D%204%3A%20%20%23%20%22barks%22%20-%3E%20%22loudly%22%0A%20%20%20%20%20%20%20%20%20%20%20%20logits%5B6%5D%20%3D%203.5%0A%20%20%20%20%20%20%20%20elif%20last_tok%20%3D%3D%205%3A%20%20%23%20%22rotates%22%20-%3E%20%22fast%22%20(0.7)%20or%20%22in%22%20(0.3)%0A%20%20%20%20%20%20%20%20%20%20%20%20logits%5B7%5D%20%3D%203.2%0A%20%20%20%20%20%20%20%20%20%20%20%20logits%5B8%5D%20%3D%202.1%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20logits%5B9%5D%20%3D%202.0%0A%20%20%20%20%20%20%20%20return%20logits%0A%0A%20%20%20%20%23%20Verification%201%3A%20Compare%20Deterministic%20Search%20Outputs%0A%20%20%20%20greedy_seq%2C%20greedy_score%20%3D%20numpy_greedy_decode(synthetic_model_logits%2C%20initial_token_id%3D0%2C%20max_length%3D4)%0A%20%20%20%20beam_seq%2C%20beam_score%20%3D%20numpy_beam_search(synthetic_model_logits%2C%20initial_token_id%3D0%2C%20beam_width%3D3%2C%20max_length%3D4)%0A%0A%20%20%20%20df_deterministic%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Search_Strategy%22%3A%20%22Greedy%20Search%20(Myopic)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Decoded_Sequence%22%3A%20%22%20%22.join(%5Bvocab_map%5Bt%5D%20for%20t%20in%20greedy_seq%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Total_Log_Likelihood%22%3A%20f%22%7Bgreedy_score%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Joint_Probability%22%3A%20f%22%7Bnp.exp(greedy_score)%3A.6f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Optimality_Status%22%3A%20%22Suboptimal%20(Trapped%20by%20local%20argmax)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Search_Strategy%22%3A%20%22Beam%20Search%20(Width%20B%20%3D%203)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Decoded_Sequence%22%3A%20%22%20%22.join(%5Bvocab_map%5Bt%5D%20for%20t%20in%20beam_seq%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Total_Log_Likelihood%22%3A%20f%22%7Bbeam_score%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Joint_Probability%22%3A%20f%22%7Bnp.exp(beam_score)%3A.6f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Optimality_Status%22%3A%20%22Globally%20Superior%20Sequence%20Found%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Verification%202%3A%20Monte%20Carlo%20Sampling%20Diversity%20Benchmark%20(1%2C000%20Generation%20Trials)%0A%20%20%20%20np.random.seed(42)%0A%20%20%20%20n_sims%20%3D%201000%0A%0A%20%20%20%20def%20run_simulations(strategy_kwargs)%3A%0A%20%20%20%20%20%20%20%20trajectories%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20for%20_%20in%20range(n_sims)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20seq%20%3D%20%5B0%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20_%20in%20range(4)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20lgs%20%3D%20synthetic_model_logits(seq)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20nxt%2C%20_%20%3D%20sample_next_token(lgs%2C%20**strategy_kwargs)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20seq.append(nxt)%0A%20%20%20%20%20%20%20%20%20%20%20%20trajectories.append(tuple(seq))%0A%0A%20%20%20%20%20%20%20%20unique_seqs%20%3D%20len(set(trajectories))%0A%20%20%20%20%20%20%20%20%23%20Frequency%20of%20most%20common%20sequence%0A%20%20%20%20%20%20%20%20counts%20%3D%20pd.Series(trajectories).value_counts()%0A%20%20%20%20%20%20%20%20mode_share%20%3D%20counts.iloc%5B0%5D%20%2F%20n_sims%20*%20100.0%0A%20%20%20%20%20%20%20%20%23%20Token%20diversity%3A%20distinct%20tokens%20%2F%20total%20generated%20tokens%0A%20%20%20%20%20%20%20%20all_tokens%20%3D%20%5Btok%20for%20traj%20in%20trajectories%20for%20tok%20in%20traj%5B1%3A%5D%5D%0A%20%20%20%20%20%20%20%20token_diversity%20%3D%20len(set(all_tokens))%20%2F%20len(vocab_map)%20*%20100.0%0A%0A%20%20%20%20%20%20%20%20return%20unique_seqs%2C%20mode_share%2C%20token_diversity%0A%0A%20%20%20%20beam_u%2C%20beam_mode%2C%20beam_div%20%3D%201%2C%20100.0%2C%20len(set(beam_seq%5B1%3A%5D))%20%2F%20len(vocab_map)%20*%20100.0%0A%20%20%20%20k_u%2C%20k_mode%2C%20k_div%20%3D%20run_simulations(%7B%22temperature%22%3A%200.8%2C%20%22top_k%22%3A%202%7D)%0A%20%20%20%20p_u%2C%20p_mode%2C%20p_div%20%3D%20run_simulations(%7B%22temperature%22%3A%200.8%2C%20%22top_p%22%3A%200.85%7D)%0A%20%20%20%20pure_u%2C%20pure_mode%2C%20pure_div%20%3D%20run_simulations(%7B%22temperature%22%3A%201.0%7D)%0A%0A%20%20%20%20df_diversity%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Sampling_Regime%22%3A%20%22Deterministic%20Beam%20Search%20(B%20%3D%203)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Unique_Sequences_Produced%22%3A%20f%22%7Bbeam_u%7D%20(0%25%20randomness)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Modal_Sequence_Share%22%3A%20f%22%7Bbeam_mode%3A.1f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Vocabulary_Utilization%22%3A%20f%22%7Bbeam_div%3A.1f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Ideal_Use_Case%22%3A%20%22Code%20Generation%20%26%20Exact%20QA%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Sampling_Regime%22%3A%20%22Top-k%20Truncation%20(k%20%3D%202%2C%20T%20%3D%200.8)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Unique_Sequences_Produced%22%3A%20f%22%7Bk_u%7D%20distinct%20paths%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Modal_Sequence_Share%22%3A%20f%22%7Bk_mode%3A.1f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Vocabulary_Utilization%22%3A%20f%22%7Bk_div%3A.1f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Ideal_Use_Case%22%3A%20%22Factual%20Summarization%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Sampling_Regime%22%3A%20%22Nucleus%20Truncation%20(Top-p%20%3D%200.85%2C%20T%20%3D%200.8)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Unique_Sequences_Produced%22%3A%20f%22%7Bp_u%7D%20distinct%20paths%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Modal_Sequence_Share%22%3A%20f%22%7Bp_mode%3A.1f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Vocabulary_Utilization%22%3A%20f%22%7Bp_div%3A.1f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Ideal_Use_Case%22%3A%20%22Chatbots%20%26%20Conversational%20AI%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Sampling_Regime%22%3A%20%22Unconstrained%20Pure%20Sampling%20(T%20%3D%201.0)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Unique_Sequences_Produced%22%3A%20f%22%7Bpure_u%7D%20distinct%20paths%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Modal_Sequence_Share%22%3A%20f%22%7Bpure_mode%3A.1f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Vocabulary_Utilization%22%3A%20f%22%7Bpure_div%3A.1f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Ideal_Use_Case%22%3A%20%22Creative%20Writing%20%26%20Brainstorming%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%0A%20%20%20%20table_det%20%3D%20mo.ui.table(df_deterministic)%0A%20%20%20%20table_div%20%3D%20mo.ui.table(df_diversity)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
ac8c8e325b88f7f89f447d1747673113