import%20marimo%0A%0A__generated_with%20%3D%20%220.11.14%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20_fig%20%3D%20mo.md(%22!%5B%5D(https%3A%2F%2Fmedia.licdn.com%2Fdms%2Fimage%2Fv2%2FD4E22AQFZFRSwwzCSqQ%2Ffeedshare-shrink_2048_1536%2Ffeedshare-shrink_2048_1536%2F0%2F1725003016027%3Fe%3D2147483647%26v%3Dbeta%26t%3DoBH1s4V8N0wKCOJQakA_wrwgFrixs56S0s_QafZOvbA)%22)%0A%20%20%20%20_text%20%3D%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%20Generative%20Pre-trained%20Transformer%20(GPT)%0A%20%20%20%20%20%20%20%20%3Ccenter%3ESadamori%20Kojaku%3C%2Fcenter%3E%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20mo.hstack(%5B_text%2C%20_fig%5D%2C%20widths%20%3D%20%5B1%2C1%5D%2C%20justify%20%3D%20%22center%22%2C%20align%20%3D%20%22center%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%20What%20is%20GPT%3F%0A%0A%20%20%20%20%20%20%20%20!%5BGPT%20architecture%5D(https%3A%2F%2Fheidloff.net%2Fassets%2Fimg%2F2023%2F02%2Ftransformers.png)%0A%0A%20%20%20%20%20%20%20%20-%20GPT%20(Generative%20Pre-trained%20Transformers)%20consists%20of%20a%20variant%20of%20the%20decoder-transformers%0A%0A%20%20%20%20%20%20%20%20-%20Many%20GPTs%20have%20been%20developed%2C%20with%20increasing%20capabilities%2C%20driven%20mostly%20by%20the%20increasing%20number%20of%20parameters.%0A%0A%0A%20%20%20%20%20%20%20%20!%5B%5D(https%3A%2F%2Fpreview.redd.it%2Fp-converting-gpt-to-llama-step-by-step-code-guide-v0-qowi1sf12krd1.jpg%3Fwidth%3D4286%26format%3Dpjpg%26auto%3Dwebp%26s%3D9ddaad2249f1b1b8084dab7f1279ba48927dda83)%0A%0A%0A%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20tip%20%7C%20Scaling%20law%0A%20%20%20%20%20%20%20%20-%20Language%20model%20performance%20improves%20*predictably*%20as%20models%20get%20larger%2C%20following%20simple%20mathematical%20relationships%20(power%20laws).%0A%20%20%20%20%20%20%20%20-%20The%20larger%20the%20model%2C%20the%20better%20it%20performs%20-%20and%20this%20improvement%20is%20reliable%20and%20measurable.%0A%20%20%20%20%20%20%20%20-%20Larger%20models%20are%20more%20efficient%20learners%20-%20they%20need%20less%20training%20data%20and%20fewer%20training%20steps%20to%20achieve%20good%20performance.%0A%0A%20%20%20%20%20%20%20%20%3Ccenter%3E%3Cimg%20src%3D%22https%3A%2F%2Fmiro.medium.com%2Fv2%2Fresize%3Afit%3A1400%2F1*5fsJPwvFjS7fo8g8NwsxNA.png%22%20width%3D%22500%22%3E%3C%2Fcenter%3E%0A%0A%20%20%20%20%20%20%20%20%2F%2F%2F%0A%0A%0A%20%20%20%20%20%20%20%20%23%23%20GPT%20~%20Autoregressive%20model%0A%0A%20%20%20%20%20%20%20%20!%5B%5D(https%3A%2F%2Fmedia.licdn.com%2Fdms%2Fimage%2Fv2%2FD4E22AQFZFRSwwzCSqQ%2Ffeedshare-shrink_2048_1536%2Ffeedshare-shrink_2048_1536%2F0%2F1725003016027%3Fe%3D2147483647%26v%3Dbeta%26t%3DoBH1s4V8N0wKCOJQakA_wrwgFrixs56S0s_QafZOvbA)%0A%0A%20%20%20%20%20%20%20%20GPT%20is%20an%20**autoregressive%20(causal)%20language%20modeling**%0A%0A%20%20%20%20%20%20%20%20Given%20a%20sequence%20of%20tokens%20%24(x_1%2C%20x_2%2C%20...%2C%20x_n)%24%2C%20the%20model%20is%20trained%20to%20maximize%20the%20likelihood%3A%0A%0A%20%20%20%20%20%20%20%20%24%24%0A%20%20%20%20%20%20%20%20P(x_1%2C%20...%2C%20x_n)%20%3D%20%5Cprod_%7Bi%3D1%7D%5En%20P(x_i%7Cx_1%2C%20...%2C%20x_%7Bi-1%7D)%0A%20%20%20%20%20%20%20%20%24%24%0A%0A%20%20%20%20%20%20%20%20To%20generate%20text%2C%20the%20model%20predicts%20the%20next%20token%20given%20all%20previous%20tokens%20%24(x_1%2C%20...%2C%20x_%7Bi-1%7D)%24.%20And%20then%2C%20the%20predicted%20token%20is%20appended%20to%20the%20sequence%20%24(x_1%2C%20...%2C%20x_%7Bi-1%7D%2C%20x_%7Bi%7D)%24%2C%20and%20the%20process%20is%20repeated%20until%3A%0A%0A%20%20%20%20%20%20%20%201.%20the%20model%20predicts%20a%20special%20token%20called%20%22end-of-text%22%20token%2C%20e.g.%2C%20%3C%7Cendoftext%7C%3E%0A%20%20%20%20%20%20%20%202.%20the%20model%20generates%20a%20specific%20number%20of%20tokens%2C%20specified%20by%20the%20user.%0A%0A%20%20%20%20%20%20%20%20%23%23%20Sampling%20the%20most%20likely%20sequence%0A%0A%20%20%20%20%20%20%20%20We%20are%20interested%20in%20sampling%20the%20most%20likely%20sequence%20from%20the%20model.%0A%0A%20%20%20%20%20%20%20%20%24%24%0A%20%20%20%20%20%20%20%20%5Carg%5Cmax_%7Bx_1%2C%20...%2C%20x_n%7D%20P(x_%7B%5Cell%20%2B%201%7D%2C%20%5Cldots%2C%20x_n%20%5Cmid%20x_1%2C%20...%2C%20x_%7B%5Cell%7D)%0A%20%20%20%20%20%20%20%20%24%24%0A%0A%20%20%20%20%20%20%20%20where%3A%0A%0A%20%20%20%20%20%20%20%20-%20%24x_1%2C%20...%2C%20x_%7B%5Cell%7D%24%20is%20the%20prefix%20of%20the%20sequence%20(the%20context).%0A%20%20%20%20%20%20%20%20-%20%24x_%7B%5Cell%20%2B%201%7D%2C%20%5Cldots%2C%20x_n%24%20is%20the%20sequence%20to%20be%20sampled%20(the%20generated%20text).%0A%0A%20%20%20%20%20%20%20%20However%2C%20this%20is%20not%20easy%20because%20there%20are%20many%20tokens%20that%20are%20statistically%20dependent%20on%20each%20other.%20**GPT%20provides%20us%20a%20(conditional)%20probability%20distribution%20of%20tokens%20but%20not%20the%20joint%20probability%20distribution.%20It%20is%20our%20job%20to%20sample%20a%20token%20sequence%20from%20the%20distribution.**%22%22%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%23%20Greedy%20sampling%0A%0A%20%20%20%20%20%20%20%20-%20**Greedy%20sampling**%20is%20a%20heuristic%20that%20always%20picks%20the%20highest%20probability%20token.%0A%20%20%20%20%20%20%20%20-%20Deterministic%20but%20can%20lead%20to%20repetitive%20or%20trapped%20text.%0A%20%20%20%20%20%20%20%20-%20For%20example%2C%20if%20the%20model%20predicts%20%22the%22%20with%20high%20probability%2C%20it%20will%20always%20predict%20%22the%22%20again.%0A%0A%20%20%20%20%20%20%20%20%23%23%20Let's%20try%20it%20out%20%F0%9F%9A%80!%0A%0A%20%20%20%20%20%20%20%20We%20will%20use%20a%20small%20GPT%20model%20and%20generate%20text%20from%20it%20using%20greedy%20sampling.%20For%20this%20demonstration%2C%20we'll%20utilize%20the%20%60gpt2%60%20model%20from%20Hugging%20Face%2C%20accessed%20through%20the%20convenient%20%60pipeline%60%20interface%20provided%20by%20the%20%60transformers%60%20library.%0A%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20tip%20%7C%20What%20is%20pipeline%3F%0A%20%20%20%20%20%20%20%20The%20%60pipeline%60%20function%20simplifies%20using%20pre-trained%20models%20by%20abstracting%20away%20model%20loading%2C%20tokenization%2C%20and%20post-processing.%0A%0A%20%20%20%20%20%20%20%20For%20example%2C%20without%20pipeline%2C%20we%20need%20to%20write%20the%20following%20code%20to%20generate%20text%20from%20the%20%60gpt2%60%20model%3A%0A%0A%20%20%20%20%20%20%20%20%60%60%60python%0A%20%20%20%20%20%20%20%20from%20transformers%20import%20AutoTokenizer%2C%20AutoModelForCausalLM%0A%20%20%20%20%20%20%20%20tokenizer%20%3D%20AutoTokenizer.from_pretrained(%22gpt2%22)%0A%20%20%20%20%20%20%20%20model%20%3D%20AutoModelForCausalLM.from_pretrained(%22gpt2%22)%0A%20%20%20%20%20%20%20%20inputs%20%3D%20tokenizer(%22Hello%2C%20I'm%20a%22%2C%20return_tensors%3D%22pt%22)%0A%20%20%20%20%20%20%20%20outputs%20%3D%20model.generate(**inputs%2C%20max_new_tokens%3D10)%0A%20%20%20%20%20%20%20%20output%20%3D%20tokenizer.decode(outputs%5B0%5D%2C%20skip_special_tokens%3DTrue)%0A%20%20%20%20%20%20%20%20%60%60%60%0A%0A%20%20%20%20%20%20%20%20With%20pipeline%2C%20the%20code%20is%20much%20simpler%20(see%20the%20next%20cell%20%F0%9F%98%89)%0A%20%20%20%20%20%20%20%20%2F%2F%2F%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Import%20the%20pipeline%20from%20transformers%0A%20%20%20%20from%20transformers%20import%20pipeline%0A%0A%20%20%20%20%23%20Define%20the%20generator%20function%0A%20%20%20%20generator%20%3D%20pipeline(%22text-generation%22%2C%20%23%20the%20task%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20model%3D%22gpt2%22%2C%20%23%20the%20model%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20device%3D%22mps%22)%20%23%20%22mps%22%2C%20%22cpu%22%2C%20or%20%22cuda%22%20depending%20on%20your%20hardware%0A%0A%20%20%20%20greedy_output%20%3D%20generator(%22Hi%20there!%20%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20do_sample%20%3D%20False%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20max_new_tokens%3D20%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pad_token_id%3Dgenerator.tokenizer.eos_token_id%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20print(greedy_output%5B0%5D%5B%22generated_text%22%5D)%0A%20%20%20%20return%20generator%2C%20greedy_output%2C%20pipeline%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20f%22%22%22%0A%20%20%20%20%20%20%20%20The%20output%20is%20often%20repetitive%20%F0%9F%99%83%20because%20it%20always%20select%20the%20most%20probable%20token%20at%20each%20step.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%23%23%20Beam%20Search%0A%20%20%20%20%20%20%20%20**Beam%20search**%20finds%20the%20best%20sequence%20by%20exploring%20multiple%20options%20at%20each%20step.%0A%0A%20%20%20%20%20%20%20%201.%20The%20algorithm%20tracks%20%24k%24%20best%20sequences%20(called%20*beams*)%20at%20a%20time.%0A%20%20%20%20%20%20%20%202.%20At%20each%20step%2C%20it%20predicts%20the%20top%20%24B%24%20next%20tokens%20for%20each%20sequence.%0A%20%20%20%20%20%20%20%203.%20It%20keeps%20the%20%24k%24%20most%20likely%20sequences%20and%20discards%20the%20rest.%0A%20%20%20%20%20%20%20%204.%20This%20continues%20until%20a%20stopping%20condition%20is%20met%20(e.g.%2C%20max%20length%20or%20end%20token).%0A%0A%20%20%20%20%20%20%20%20!%5B%5D(https%3A%2F%2Ftowardsdatascience.com%2Fwp-content%2Fuploads%2F2021%2F04%2F1tEjhWqUgjX37VnT7gJN-4g.png)%0A%0A%20%20%20%20%20%20%20%20Let's%20try%20it%20out!%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(generator)%3A%0A%20%20%20%20beam_output%20%3D%20generator(%22Hi%20there!%20%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20do_sample%20%3D%20False%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20max_new_tokens%3D20%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pad_token_id%3Dgenerator.tokenizer.eos_token_id%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20num_beams%3D5%2C%20%23%20number%20of%20beams%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20num_return_sequences%3D5%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%23%20Print%20the%20output%0A%20%20%20%20for%20i%20in%20range(len(beam_output))%3A%0A%20%20%20%20%20%20%20%20print(beam_output%5Bi%5D%5B%22generated_text%22%5D)%0A%20%20%20%20return%20beam_output%2C%20i%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20From%20Deterministic%20to%20Stochastic%20Sampling%0A%0A%20%20%20%20%20%20%20%20-%20**Deterministic%20Methods**%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20Greedy%20and%20beam%20search%20can%20get%20stuck%20in%20loops%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20Predictable%20but%20repetitive%20outputs%0A%0A%20%20%20%20%20%20%20%20-%20**Stochastic%20Sampling**%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20Introduces%20randomness%20to%20avoid%20loops%20%F0%9F%8E%B2%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20But%20can%20lead%20to%20incoherent%20text%20%F0%9F%91%BE%0A%0A%20%20%20%20%20%20%20%20-%20**Key%20Challenge**%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20Expensive%20computation%20of%20the%20probabilities%20of%20all%20tokens%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20*Controlling*%20randomness%20is%20crucial%20for%20quality%20output%0A%0A%20%20%20%20%20%20%20%20%23%23%23%20Two%20Popular%20Stochastic%20Sampling%20Methods%0A%0A%20%20%20%20%20%20%20%20To%20reduce%20the%20computational%20cost%2C%20we%20can%20pre-sample%20a%20few%20tokens%2C%20instead%20of%20all%20tokens%2C%20with%20the%20highest%20probability.%20The%20two%20popular%20methods%20are%3A%0A%0A%20%20%20%20%20%20%20%20-%20**Top-k%20Sampling**%20selects%20the%20k%20most%20likely%20tokens%20at%20each%20step.%0A%0A%20%20%20%20%20%20%20%20-%20**Nucleus%20Sampling**%20selects%20the%20smallest%20set%20of%20tokens%20with%20the%20highest%20cumulative%20probability%20exceeding%20a%20threshold%20%24p%24%20(typically%200.9).%0A%0A%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20note%20%7C%20Nucleus%20sampling%0A%20%20%20%20%20%20%20%20**Nucleus%20sampling**%20dynamically%20adjusts%20by%20selecting%20fewer%20tokens%20when%20the%20distribution%20is%20concentrated%20and%20more%20tokens%20when%20the%20model%20is%20uncertain%20about%20predictions.%0A%0A%20%20%20%20%20%20%20%20!%5B%5D(https%3A%2F%2Fstorage.googleapis.com%2Fzenn-user-upload%2F8p2r9urhtn5nztdg6mnia3toibhl)%0A%20%20%20%20%20%20%20%20%2F%2F%2F%0A%0A%20%20%20%20%20%20%20%20To%20control%20the%20**randomness**%2C%20we%20can%20use%20**temperature**%20as%20follows.%0A%0A%20%20%20%20%20%20%20%20%24%24%0A%20%20%20%20%20%20%20%20p_i%20%3D%20%5C%5Cfrac%7B%5Cexp(z_i%2F%5C%5Ctau)%7D%7B%5Csum_j%20%5Cexp(z_j%2F%5C%5Ctau)%7D%0A%20%20%20%20%20%20%20%20%24%24%0A%0A%20%20%20%20%20%20%20%20where%20%24z_i%24%20is%20the%20logit%20of%20the%20%24i%24-th%20token%2C%20and%20%24%5Ctau%24%20is%20the%20temperature.%0A%0A%20%20%20%20%20%20%20%20-%20The%20temperature%20%24%5C%5Ctau%24%20controls%20the%20concentration%20of%20the%20probability%20distribution.%0A%20%20%20%20%20%20%20%20-%20Lower%20temperatures%20make%20the%20distribution%20more%20peaked%2C%20leading%20to%20more%20focused%20outputs.%0A%20%20%20%20%20%20%20%20-%20Higher%20temperatures%20make%20the%20distribution%20more%20flat%2C%20producing%20more%20diverse%20but%20potentially%20less%20coherent%20text.%0A%0A%20%20%20%20%20%20%20%20!%5B%5D(https%3A%2F%2Fcdn.prod.website-files.com%2F618399cd49d125734c8dec95%2F6639e35ce91c16b3b9564b2f_mxaIPcROZcBFYta1I0nzWjlGTgs-LxzUOE3p6Kbvf9qPpZzBh5AAZG7ciRtgVquhLTtrM8ToJdNd-ubXvuz8tRfrqBwSozWHCj457pm378buxz2-XrMfWzfSv3b793QP61kLxRKT299WP1gbas_E118.png)%0A%0A%20%20%20%20%20%20%20%20Let's%20try%20it%20out!%0A%0A%20%20%20%20%20%20%20%20%3Ccenter%3E%3Cb%3ETop-k%20Sampling%3C%2Fb%3E%3C%2Fcenter%3E%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(generator)%3A%0A%20%20%20%20top_k_output%20%3D%20generator(%22Hi%20there!%20%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20do_sample%20%3D%20True%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20max_new_tokens%3D20%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pad_token_id%3Dgenerator.tokenizer.eos_token_id%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20top_k%3D10%2C%20%23%20set%201%20to%20make%20it%20equivalent%20to%20greedy%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20print(top_k_output%5B0%5D%5B%22generated_text%22%5D)%0A%20%20%20%20return%20(top_k_output%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%3Ccenter%3E%3Cb%3ENucleus%20Sampling%3C%2Fb%3E%3C%2Fcenter%3E%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(generator)%3A%0A%20%20%20%20top_p_output%20%3D%20generator(%22Hi%20there!%20%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20do_sample%20%3D%20True%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20max_new_tokens%3D20%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pad_token_id%3Dgenerator.tokenizer.eos_token_id%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20top_p%3D0.95%2C%20%23%20set%200%20to%20make%20it%20equivalent%20to%20greedy%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20print(top_p_output%5B0%5D%5B%22generated_text%22%5D)%0A%20%20%20%20return%20(top_p_output%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%3Ccenter%3E%3Cb%3ETemperature%3C%2Fb%3E%3C%2Fcenter%3E%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(generator)%3A%0A%20%20%20%20for%20_tau%20in%20%5B0.1%2C%200.5%2C%201.0%2C%202.0%2C%205.0%5D%3A%0A%20%20%20%20%20%20%20%20output_tau%20%3D%20generator(%22Hi%20there!%20%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20do_sample%20%3D%20True%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20max_new_tokens%3D20%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pad_token_id%3Dgenerator.tokenizer.eos_token_id%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20temperature%3D_tau%2C%20%23%20set%200%20to%20make%20it%20equivalent%20to%20greedy%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20print(f%22tau%20%3D%20%7B_tau%7D%3A%20%7Boutput_tau%5B0%5D%5B'generated_text'%5D%7D%22)%0A%20%20%20%20return%20(output_tau%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%3Ccenter%3E%3Cb%3EEverything%20together%3C%2Fb%3E%3C%2Fcenter%3E%0A%20%20%20%20%20%20%20%20%20%20We%20can%20combine%20all%20the%20methods%20together%20to%20further%20limit%20the%20tokens%20to%20be%20sampled%20(e.g.%2C%20they%20must%20be%20among%20the%20top%20k%20and%20top%20p%20tokens)%20with%20randomness%20control%20(e.g.%2C%20temperature).%0A%20%20%20%20%20%20%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(generator)%3A%0A%20%20%20%20for%20_tau%20in%20%5B0.1%2C%200.5%2C%201.0%2C%202.0%2C%205.0%5D%3A%0A%20%20%20%20%20%20%20%20_output%20%3D%20generator(%22Hi%20there!%20%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20do_sample%20%3D%20True%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20max_new_tokens%3D20%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pad_token_id%3Dgenerator.tokenizer.eos_token_id%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20temperature%3D_tau%2C%20%23%20set%200%20to%20make%20it%20equivalent%20to%20greedy%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20top_k%3D10%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20top_p%3D0.95%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20print(f%22tau%20%3D%20%7B_tau%7D%3A%20%7B_output%5B0%5D%5B'generated_text'%5D%7D%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%23%20Summary%0A%0A%20%20%20%20%20%20%20%20%20%20In%20this%20notebook%2C%20we've%20explored%20different%20text%20generation%20strategies%20for%20language%20models%3A%0A%0A%20%20%20%20%20%20%20%20%20%201.%20**Greedy%20Sampling**%3A%20Always%20selects%20the%20most%20probable%20token%20at%20each%20step.%20Simple%20but%20often%20leads%20to%20repetitive%20text.%0A%0A%20%20%20%20%20%20%20%20%20%202.%20**Beam%20Search**%3A%20Explores%20multiple%20possible%20sequences%20simultaneously%2C%20keeping%20track%20of%20the%20k%20most%20likely%20sequences.%0A%0A%20%20%20%20%20%20%20%20%20%203.%20**Top-k%20Sampling**%3A%20Restricts%20sampling%20to%20only%20the%20k%20most%20likely%20tokens%20at%20each%20step%2C%20introducing%20controlled%20randomness.%0A%0A%20%20%20%20%20%20%20%20%20%204.%20**Top-p%20(Nucleus)%20Sampling**%3A%20Dynamically%20selects%20from%20the%20smallest%20set%20of%20tokens%20whose%20cumulative%20probability%20exceeds%20threshold%20p.%0A%0A%20%20%20%20%20%20%20%20%20%205.%20**Temperature%20Sampling**%3A%20Controls%20the%20randomness%20of%20predictions%20by%20scaling%20the%20logits%20before%20applying%20softmax.%20Lower%20temperature%20(%CF%84%20%3C%201)%20makes%20the%20distribution%20more%20peaked%2C%20while%20higher%20temperature%20(%CF%84%20%3E%201)%20makes%20it%20more%20uniform.%0A%0A%20%20%20%20%20%20%20%20%20%20These%20techniques%20can%20be%20combined%20to%20achieve%20the%20desired%20balance%20between%20diversity%20and%20quality%20in%20generated%20text.%20The%20optimal%20approach%20depends%20on%20your%20specific%20application%20and%20requirements.%0A%20%20%20%20%20%20%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20return%20(mo%2C)%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
bfc5991213566f81bcca315e9e7227fde54ca05813b1baf6bee5b0278dbda5a1