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.

Utils.cs 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using LLama.Abstractions;
  2. using LLama.Native;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using LLama.Extensions;
  8. namespace LLama
  9. {
  10. using llama_token = Int32;
  11. /// <summary>
  12. /// Assorted llama utilities
  13. /// </summary>
  14. public static class Utils
  15. {
  16. [Obsolete("Use LLamaWeights.LoadFromFile and LLamaWeights.CreateContext instead")]
  17. #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
  18. public static SafeLLamaContextHandle InitLLamaContextFromModelParams(IModelParams @params)
  19. #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
  20. {
  21. using var weights = LLamaWeights.LoadFromFile(@params);
  22. using (@params.ToLlamaContextParams(out var lparams))
  23. return SafeLLamaContextHandle.Create(weights.NativeHandle, lparams);
  24. }
  25. [Obsolete("Use SafeLLamaContextHandle Tokenize method instead")]
  26. #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
  27. public static IEnumerable<llama_token> Tokenize(SafeLLamaContextHandle ctx, string text, bool add_bos, Encoding encoding)
  28. #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
  29. {
  30. return ctx.Tokenize(text, add_bos, encoding);
  31. }
  32. [Obsolete("Use SafeLLamaContextHandle GetLogits method instead")]
  33. #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
  34. public static Span<float> GetLogits(SafeLLamaContextHandle ctx, int length)
  35. #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
  36. {
  37. if (length != ctx.VocabCount)
  38. throw new ArgumentException("length must be the VocabSize");
  39. return ctx.GetLogits();
  40. }
  41. [Obsolete("Use SafeLLamaContextHandle Eval method instead")]
  42. #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
  43. public static int Eval(SafeLLamaContextHandle ctx, llama_token[] tokens, int startIndex, int n_tokens, int n_past, int n_threads)
  44. #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
  45. {
  46. var slice = tokens.AsSpan().Slice(startIndex, n_tokens);
  47. return ctx.Eval(slice, n_past, n_threads) ? 0 : 1;
  48. }
  49. [Obsolete("Use SafeLLamaContextHandle TokenToString method instead")]
  50. #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
  51. public static string TokenToString(llama_token token, SafeLLamaContextHandle ctx, Encoding encoding)
  52. #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
  53. {
  54. return ctx.TokenToString(token, encoding);
  55. }
  56. [Obsolete("No longer used internally by LlamaSharp")]
  57. #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
  58. public static string PtrToString(IntPtr ptr, Encoding encoding)
  59. #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
  60. {
  61. #if NET6_0_OR_GREATER
  62. // ReSharper disable once PossibleUnintendedReferenceComparison
  63. if(encoding == Encoding.UTF8)
  64. {
  65. return Marshal.PtrToStringUTF8(ptr)!;
  66. }
  67. // ReSharper disable once PossibleUnintendedReferenceComparison
  68. else if(encoding == Encoding.Unicode)
  69. {
  70. return Marshal.PtrToStringUni(ptr)!;
  71. }
  72. else
  73. {
  74. return Marshal.PtrToStringAuto(ptr)!;
  75. }
  76. #else
  77. unsafe
  78. {
  79. byte* tp = (byte*)ptr.ToPointer();
  80. List<byte> bytes = new();
  81. while (true)
  82. {
  83. byte c = *tp++;
  84. if (c == '\0')
  85. {
  86. break;
  87. }
  88. else
  89. {
  90. bytes.Add(c);
  91. }
  92. }
  93. return encoding.GetString(bytes.ToArray());
  94. }
  95. #endif
  96. }
  97. }
  98. }