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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. string _modelPath;
  15. public SaveAndLoadState(string modelPath, string prompt)
  16. {
  17. _prompt = prompt;
  18. _modelPath = modelPath;
  19. _executor = new InteractiveExecutor(new LLamaModel(new ModelParams(modelPath: modelPath)));
  20. foreach (var text in _executor.Infer(_prompt, new InferenceParams() { Temperature = 0.6f, AntiPrompts = new List<string> { "user:" } }))
  21. {
  22. Console.Write(text);
  23. }
  24. }
  25. public void Run(string prompt)
  26. {
  27. InferenceParams sessionParams = new InferenceParams() { Temperature = 0.2f, AntiPrompts = new List<string> { "user:" } };
  28. foreach (var text in _executor.Infer(prompt, sessionParams))
  29. {
  30. Console.Write(text);
  31. }
  32. }
  33. public void SaveState(string executorStateFile, string modelStateFile)
  34. {
  35. _executor.Model.SaveState(modelStateFile);
  36. _executor.SaveState(executorStateFile);
  37. Console.WriteLine("Saved state!");
  38. }
  39. public void LoadState(string executorStateFile, string modelStateFile)
  40. {
  41. var model = _executor.Model;
  42. model.LoadState(modelStateFile);
  43. _executor = new InteractiveExecutor(model);
  44. _executor.LoadState(executorStateFile);
  45. Console.WriteLine("Loaded state!");
  46. }
  47. public void Dispose()
  48. {
  49. _executor.Model.Dispose();
  50. }
  51. }
  52. }

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