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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using LLama.Native;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using LLama.Exceptions;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. namespace LLama
  9. {
  10. using llama_token = Int32;
  11. internal static class Utils
  12. {
  13. public static SafeLLamaContextHandle llama_init_from_gpt_params(ref GptParams @params)
  14. {
  15. var lparams = NativeApi.llama_context_default_params();
  16. lparams.n_ctx = @params.n_ctx;
  17. lparams.n_parts = @params.n_parts;
  18. lparams.seed = @params.seed;
  19. lparams.f16_kv = @params.memory_f16;
  20. lparams.use_mmap = @params.use_mmap;
  21. lparams.use_mlock = @params.use_mlock;
  22. lparams.logits_all = @params.perplexity;
  23. lparams.embedding = @params.embedding;
  24. var ctx_ptr = NativeApi.llama_init_from_file(@params.model, lparams);
  25. if(ctx_ptr == IntPtr.Zero )
  26. {
  27. throw new RuntimeError($"Failed to load model {@params.model}.");
  28. }
  29. SafeLLamaContextHandle ctx = new(ctx_ptr);
  30. if (!string.IsNullOrEmpty(@params.lora_adapter))
  31. {
  32. int err = NativeApi.llama_apply_lora_from_file(ctx, @params.lora_adapter,
  33. string.IsNullOrEmpty(@params.lora_base) ? null : @params.lora_base, @params.n_threads);
  34. if(err != 0)
  35. {
  36. throw new RuntimeError("Failed to apply lora adapter.");
  37. }
  38. }
  39. return ctx;
  40. }
  41. public static List<llama_token> llama_tokenize(SafeLLamaContextHandle ctx, string text, bool add_bos)
  42. {
  43. llama_token[] res = new llama_token[text.Length + (add_bos ? 1 : 0)];
  44. int n = NativeApi.llama_tokenize(ctx, text, res, res.Length, add_bos);
  45. Debug.Assert(n >= 0);
  46. return res.Take(n).ToList();
  47. }
  48. public unsafe static Span<float> llama_get_logits(SafeLLamaContextHandle ctx, int length)
  49. {
  50. var logits = NativeApi.llama_get_logits(ctx);
  51. return new Span<float>(logits, length);
  52. }
  53. }
  54. }

C#/.NET上易用的LLM高性能推理框架,支持LLaMA和LLaVA系列模型。

Contributors (1)