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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 (@params.ToLlamaContextParams(out var lparams))
  16. {
  17. var model = SafeLlamaModelHandle.LoadFromFile(@params.ModelPath, lparams);
  18. var ctx = SafeLLamaContextHandle.Create(model, lparams);
  19. if (!string.IsNullOrEmpty(@params.LoraAdapter))
  20. model.ApplyLoraFromFile(@params.LoraAdapter, @params.LoraBase, @params.Threads);
  21. return ctx;
  22. }
  23. }
  24. [Obsolete("Use SafeLLamaContextHandle Tokenize method instead")]
  25. public static IEnumerable<llama_token> Tokenize(SafeLLamaContextHandle ctx, string text, bool add_bos, Encoding encoding)
  26. {
  27. return ctx.Tokenize(text, add_bos, encoding);
  28. }
  29. [Obsolete("Use SafeLLamaContextHandle GetLogits method instead")]
  30. public static Span<float> GetLogits(SafeLLamaContextHandle ctx, int length)
  31. {
  32. if (length != ctx.VocabCount)
  33. throw new ArgumentException("length must be the VocabSize");
  34. return ctx.GetLogits();
  35. }
  36. [Obsolete("Use SafeLLamaContextHandle Eval method instead")]
  37. public static int Eval(SafeLLamaContextHandle ctx, llama_token[] tokens, int startIndex, int n_tokens, int n_past, int n_threads)
  38. {
  39. var slice = tokens.AsMemory().Slice(startIndex, n_tokens);
  40. return ctx.Eval(slice, n_past, n_threads) ? 0 : 1;
  41. }
  42. [Obsolete("Use SafeLLamaContextHandle TokenToString method instead")]
  43. public static string TokenToString(llama_token token, SafeLLamaContextHandle ctx, Encoding encoding)
  44. {
  45. return ctx.TokenToString(token, encoding);
  46. }
  47. [Obsolete("No longer used internally by LlamaSharp")]
  48. public static string PtrToString(IntPtr ptr, Encoding encoding)
  49. {
  50. #if NET6_0_OR_GREATER
  51. if(encoding == Encoding.UTF8)
  52. {
  53. return Marshal.PtrToStringUTF8(ptr);
  54. }
  55. else if(encoding == Encoding.Unicode)
  56. {
  57. return Marshal.PtrToStringUni(ptr);
  58. }
  59. else
  60. {
  61. return Marshal.PtrToStringAuto(ptr);
  62. }
  63. #else
  64. unsafe
  65. {
  66. byte* tp = (byte*)ptr.ToPointer();
  67. List<byte> bytes = new();
  68. while (true)
  69. {
  70. byte c = *tp++;
  71. if (c == '\0')
  72. {
  73. break;
  74. }
  75. else
  76. {
  77. bytes.Add(c);
  78. }
  79. }
  80. return encoding.GetString(bytes.ToArray());
  81. }
  82. #endif
  83. }
  84. }
  85. }