You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

TopKSampling.cs 869 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System;
  2. using LLama.Native;
  3. namespace LLama.Sampling.Tokens;
  4. /// <summary>
  5. /// Sample with TopK, removing all by the K most likely tokens.
  6. /// Top-K sampling described in academic paper "The Curious Case of Neural Text Degeneration" https://arxiv.org/abs/1904.09751
  7. /// </summary>
  8. public sealed class TopKSampling
  9. : ITokenDataProcessor
  10. {
  11. /// <summary>
  12. /// Number of tokens to keep
  13. /// </summary>
  14. public int Count { get; set; }
  15. /// <inheritdoc />
  16. public void ProcessTokens(SafeLLamaContextHandle ctx, LLamaTokenDataArray tokens, ReadOnlySpan<int> lastTokens)
  17. {
  18. tokens.TopK(ctx, Count);
  19. }
  20. /// <inheritdoc />
  21. public void AcceptToken(SafeLLamaContextHandle ctx, int token)
  22. {
  23. }
  24. /// <inheritdoc />
  25. public void Reset()
  26. {
  27. }
  28. /// <inheritdoc />
  29. public void Dispose()
  30. {
  31. }
  32. }