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.

StatelessExecutorTest.cs 2.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System.Diagnostics;
  2. using LLama.Common;
  3. using Xunit.Abstractions;
  4. namespace LLama.Unittest
  5. {
  6. public class StatelessExecutorTest
  7. : IDisposable
  8. {
  9. private readonly ITestOutputHelper _testOutputHelper;
  10. private readonly LLamaWeights _weights;
  11. private readonly ModelParams _params;
  12. public StatelessExecutorTest(ITestOutputHelper testOutputHelper)
  13. {
  14. _testOutputHelper = testOutputHelper;
  15. _params = new ModelParams(Constants.ModelPath)
  16. {
  17. ContextSize = 60,
  18. Seed = 1754,
  19. };
  20. _weights = LLamaWeights.LoadFromFile(_params);
  21. }
  22. public void Dispose()
  23. {
  24. _weights.Dispose();
  25. }
  26. [Fact]
  27. public async Task Stateless()
  28. {
  29. var executor = new StatelessExecutor(_weights, _params);
  30. const string question = "Question. what is a cat?\nAnswer: ";
  31. var @params = new InferenceParams { MaxTokens = 32, AntiPrompts = new[] { "." } };
  32. var timer = new Stopwatch();
  33. timer.Start();
  34. var result1 = string.Join("", await executor.InferAsync(question, @params).ToListAsync());
  35. var result2 = string.Join("", await executor.InferAsync(question, @params).ToListAsync());
  36. timer.Stop();
  37. _testOutputHelper.WriteLine($"{timer.ElapsedMilliseconds}ms");
  38. _testOutputHelper.WriteLine(result1);
  39. _testOutputHelper.WriteLine(result2);
  40. // Check that it produced the exact same result both times
  41. Assert.Equal(result1, result2);
  42. }
  43. [Fact(Skip = "Very very slow in CI")]
  44. public async Task OutOfContext()
  45. {
  46. var executor = new StatelessExecutor(_weights, _params);
  47. const string question = " Question. cats or dogs?\nAnswer: ";
  48. // The context size is set to 60. Generate more than that, forcing it to generate a coherent response
  49. // with a modified context
  50. var @params = new InferenceParams()
  51. {
  52. MaxTokens = 65,
  53. TokensKeep = question.Length,
  54. };
  55. var result1 = string.Join("", await executor.InferAsync(question, @params).ToListAsync());
  56. var result2 = string.Join("", await executor.InferAsync(question, @params).ToListAsync());
  57. _testOutputHelper.WriteLine(result1);
  58. // Check that it produced the exact same result both times
  59. Assert.Equal(result1, result2);
  60. }
  61. }
  62. }