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.1 kB

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