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 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. public static IEnumerable<llama_token> Tokenize(SafeLLamaContextHandle ctx, string text, bool add_bos, Encoding encoding)
  21. {
  22. return ctx.Tokenize(text, add_bos, encoding);
  23. }
  24. [Obsolete("Use SafeLLamaContextHandle GetLogits method instead")]
  25. public static Span<float> GetLogits(SafeLLamaContextHandle ctx, int length)
  26. {
  27. if (length != ctx.VocabCount)
  28. throw new ArgumentException("length must be the VocabSize");
  29. return ctx.GetLogits();
  30. }
  31. [Obsolete("Use SafeLLamaContextHandle Eval method instead")]
  32. public static int Eval(SafeLLamaContextHandle ctx, llama_token[] tokens, int startIndex, int n_tokens, int n_past, int n_threads)
  33. {
  34. var slice = tokens.AsSpan().Slice(startIndex, n_tokens);
  35. return ctx.Eval(slice, n_past, n_threads) ? 0 : 1;
  36. }
  37. [Obsolete("Use SafeLLamaContextHandle TokenToString method instead")]
  38. public static string TokenToString(llama_token token, SafeLLamaContextHandle ctx, Encoding encoding)
  39. {
  40. return ctx.TokenToString(token, encoding);
  41. }
  42. [Obsolete("No longer used internally by LlamaSharp")]
  43. public static string PtrToString(IntPtr ptr, Encoding encoding)
  44. {
  45. #if NET6_0_OR_GREATER
  46. if(encoding == Encoding.UTF8)
  47. {
  48. return Marshal.PtrToStringUTF8(ptr);
  49. }
  50. else if(encoding == Encoding.Unicode)
  51. {
  52. return Marshal.PtrToStringUni(ptr);
  53. }
  54. else
  55. {
  56. return Marshal.PtrToStringAuto(ptr);
  57. }
  58. #else
  59. unsafe
  60. {
  61. byte* tp = (byte*)ptr.ToPointer();
  62. List<byte> bytes = new();
  63. while (true)
  64. {
  65. byte c = *tp++;
  66. if (c == '\0')
  67. {
  68. break;
  69. }
  70. else
  71. {
  72. bytes.Add(c);
  73. }
  74. }
  75. return encoding.GetString(bytes.ToArray());
  76. }
  77. #endif
  78. }
  79. /// <summary>
  80. /// Converts a bool "value" to a signed byte of "1" for true and "0" for false to be compatible with a 1 byte C-style bool.
  81. /// </summary>
  82. /// <param name="value"></param>
  83. /// <returns></returns>
  84. public static sbyte BoolToSignedByte(bool value)
  85. {
  86. return value ? (sbyte)1 : (sbyte)0;
  87. }
  88. /// <summary>
  89. /// Converts a sbyte "value" to a C# bool.
  90. /// </summary>
  91. /// <param name="value"></param>
  92. /// <returns></returns>
  93. public static bool SignedByteToBool(sbyte value)
  94. {
  95. return value > 0;
  96. }
  97. }
  98. }