|
| 1 | +using LLama.Common; |
| 2 | +using LLama.Examples.Extensions; |
| 3 | +using LLama.Native; |
| 4 | +using LLama.Sampling; |
| 5 | + |
| 6 | +namespace LLama.Examples.Examples |
| 7 | +{ |
| 8 | + public class CustomSampler |
| 9 | + { |
| 10 | + public static async Task Run() |
| 11 | + { |
| 12 | + var modelPath = UserSettings.GetModelPath(); |
| 13 | + |
| 14 | + var parameters = new ModelParams(modelPath); |
| 15 | + using var model = await LLamaWeights.LoadFromFileAsync(parameters); |
| 16 | + |
| 17 | + var ex = new StatelessExecutor(model, parameters); |
| 18 | + |
| 19 | + Console.ForegroundColor = ConsoleColor.Yellow; |
| 20 | + Console.WriteLine("In this example a custom sampling pipeline with a custom sampler stage is being used. This demonstrates how to customise the samplers used, and " + |
| 21 | + "how to create a completely custom sampler stage which modifies the logits or selects a token." + |
| 22 | + "" + |
| 23 | + "In this case the custom sampler stage removes the most likely token. This will probably produce bad results, it's just a demo!" |
| 24 | + ); |
| 25 | + Console.ForegroundColor = ConsoleColor.White; |
| 26 | + |
| 27 | + var inferenceParams = new InferenceParams |
| 28 | + { |
| 29 | + SamplingPipeline = new CustomSamplingPipeline(), |
| 30 | + MaxTokens = 50 |
| 31 | + }; |
| 32 | + |
| 33 | + while (true) |
| 34 | + { |
| 35 | + Console.Write("\nQuestion: "); |
| 36 | + Console.ForegroundColor = ConsoleColor.Green; |
| 37 | + var prompt = Console.ReadLine(); |
| 38 | + Console.ForegroundColor = ConsoleColor.White; |
| 39 | + Console.Write("Answer: "); |
| 40 | + prompt = $"Question: {prompt?.Trim()} Answer: "; |
| 41 | + await foreach (var text in ex.InferAsync(prompt, inferenceParams).Spinner()) |
| 42 | + { |
| 43 | + Console.Write(text); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public class CustomSamplingPipeline |
| 50 | + : BaseSamplingPipeline |
| 51 | + { |
| 52 | + protected override SafeLLamaSamplerChainHandle CreateChain(SafeLLamaContextHandle context) |
| 53 | + { |
| 54 | + var chain = SafeLLamaSamplerChainHandle.Create(LLamaSamplerChainParams.Default()); |
| 55 | + |
| 56 | + // Take only the 10 most likely tokens |
| 57 | + chain.AddTopK(10); |
| 58 | + |
| 59 | + // Remove the most likely token |
| 60 | + chain.AddCustom(new RemoveMostLikelyToken()); |
| 61 | + |
| 62 | + // Select from the distribution |
| 63 | + chain.AddSoftmax(); |
| 64 | + chain.AddDistributionSampler(42); |
| 65 | + |
| 66 | + return chain; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public class RemoveMostLikelyToken |
| 71 | + : ICustomSampler |
| 72 | + { |
| 73 | + public string Name => "Remove Most Likely Token"; |
| 74 | + |
| 75 | + public void Apply(ref LLamaTokenDataArrayNative tokenData) |
| 76 | + { |
| 77 | + // Doesn't make sense to run this stage if there is only one candidate left |
| 78 | + if (tokenData.Size <= 1) |
| 79 | + return; |
| 80 | + |
| 81 | + // Ensure token data is sorted, so most likely token is first. |
| 82 | + // Note that this is a descending sort, the **largest** value is first. |
| 83 | + if (!tokenData.Sorted) |
| 84 | + tokenData.Data.Sort((a, b) => b.Logit.CompareTo(a.Logit)); |
| 85 | + |
| 86 | + // Make the most likely token impossible to pick |
| 87 | + tokenData.Data[0].Logit = float.NegativeInfinity; |
| 88 | + |
| 89 | + // It's **critically** important to set this if the logits are no longer sorted after the custom |
| 90 | + // sampler has run. If you're not sure, it's always safer to set it to false. |
| 91 | + // |
| 92 | + // In this case, because the first logit has just been set to negative infinity |
| 93 | + // the token data is definitely not sorted! |
| 94 | + tokenData.Sorted = false; |
| 95 | + } |
| 96 | + |
| 97 | + public void Accept(LLamaToken token) |
| 98 | + { |
| 99 | + } |
| 100 | + |
| 101 | + public void Reset() |
| 102 | + { |
| 103 | + } |
| 104 | + |
| 105 | + public ICustomSampler Clone() |
| 106 | + { |
| 107 | + return new RemoveMostLikelyToken(); |
| 108 | + } |
| 109 | + |
| 110 | + public void Dispose() |
| 111 | + { |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments