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.

FixedSizeQueue.cs 2.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace LLama.Common
  7. {
  8. /// <summary>
  9. /// A queue with fixed storage size.
  10. /// Currently it's only a naive implementation and needs to be further optimized in the future.
  11. /// </summary>
  12. public class FixedSizeQueue<T>: IEnumerable<T>
  13. {
  14. int _maxSize;
  15. List<T> _storage;
  16. public int Count => _storage.Count;
  17. public int Capacity => _maxSize;
  18. public FixedSizeQueue(int size)
  19. {
  20. _maxSize = size;
  21. _storage = new();
  22. }
  23. /// <summary>
  24. /// Fill the quene with the data. Please ensure that data.Count <= size
  25. /// </summary>
  26. /// <param name="size"></param>
  27. /// <param name="data"></param>
  28. public FixedSizeQueue(int size, IEnumerable<T> data)
  29. {
  30. _maxSize = size;
  31. if(data.Count() > size)
  32. {
  33. throw new ArgumentException($"The max size set for the quene is {size}, but got {data.Count()} initial values.");
  34. }
  35. _storage = new(data);
  36. }
  37. public FixedSizeQueue<T> FillWith(T value)
  38. {
  39. for(int i = 0; i < Count; i++)
  40. {
  41. _storage[i] = value;
  42. }
  43. return this;
  44. }
  45. /// <summary>
  46. /// Enquene an element.
  47. /// </summary>
  48. /// <returns></returns>
  49. public void Enqueue(T item)
  50. {
  51. _storage.Add(item);
  52. if(_storage.Count >= _maxSize)
  53. {
  54. _storage.RemoveAt(0);
  55. }
  56. }
  57. public T[] ToArray()
  58. {
  59. return _storage.ToArray();
  60. }
  61. public IEnumerator<T> GetEnumerator()
  62. {
  63. return _storage.GetEnumerator();
  64. }
  65. IEnumerator IEnumerable.GetEnumerator()
  66. {
  67. return GetEnumerator();
  68. }
  69. }
  70. }

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