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.

LoadAndSaveState.cs 1.8 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using LLama.Common;
  2. using LLama.OldVersion;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace LLama.Examples.NewVersion
  9. {
  10. public class SaveAndLoadState : IDisposable
  11. {
  12. InteractiveExecutor _executor;
  13. string _prompt;
  14. public SaveAndLoadState(string modelPath, string prompt)
  15. {
  16. _prompt = prompt;
  17. _executor = new InteractiveExecutor(new LLamaModel(new ModelParams(modelPath: modelPath)));
  18. foreach (var text in _executor.Infer(_prompt, new InferenceParams() { Temperature = 0.6f, AntiPrompts = new List<string> { "user:" } }))
  19. {
  20. Console.Write(text);
  21. }
  22. }
  23. public void Run(string prompt)
  24. {
  25. InferenceParams sessionParams = new InferenceParams() { Temperature = 0.2f, AntiPrompts = new List<string> { "user:" } };
  26. foreach (var text in _executor.Infer(prompt, sessionParams))
  27. {
  28. Console.Write(text);
  29. }
  30. }
  31. public void SaveState(string executorStateFile, string modelStateFile)
  32. {
  33. _executor.Model.SaveState(modelStateFile);
  34. _executor.SaveState(executorStateFile);
  35. Console.WriteLine("Saved state!");
  36. }
  37. public void LoadState(string executorStateFile, string modelStateFile)
  38. {
  39. var model = _executor.Model;
  40. model.LoadState(modelStateFile);
  41. _executor = new InteractiveExecutor(model);
  42. _executor.LoadState(executorStateFile);
  43. Console.WriteLine("Loaded state!");
  44. }
  45. public void Dispose()
  46. {
  47. _executor.Model.Dispose();
  48. }
  49. }
  50. }

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