SizingTool.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { INDEX_TYPES_ENUM, SIZE_STATUS } from '../pages/schema/Types';
  2. const commonValueCalculator = (
  3. vector: number,
  4. dimensions: number,
  5. nlistArg: number,
  6. fileSize: number
  7. ) => {
  8. const vectorCount = Math.min(fileSize / (dimensions * 4), vector);
  9. const segmentCount = Math.round(vector / vectorCount);
  10. const nlist = Math.min(nlistArg, vectorCount / 40);
  11. return {
  12. vectorCount,
  13. segmentCount,
  14. nlist,
  15. };
  16. };
  17. const pqCalculator = (
  18. vectorCount: number,
  19. segmentCount: number,
  20. dimensions: number,
  21. m: number,
  22. nlist: number
  23. ) => {
  24. const singleDiskSize =
  25. nlist * dimensions * 4 + m * vectorCount + 256 * dimensions * 4;
  26. const singleMemorySize = singleDiskSize + 256 * m * nlist * 4;
  27. return {
  28. pq_diskSize: singleDiskSize * segmentCount,
  29. pq_memorySize: singleMemorySize * segmentCount,
  30. };
  31. };
  32. export const computMilvusRecommonds = (
  33. vector: number,
  34. dimensions: number,
  35. nlistArg: number,
  36. m: number,
  37. fileSize: number
  38. ): { [key in string]: any } => {
  39. const { vectorCount, segmentCount, nlist } = commonValueCalculator(
  40. vector,
  41. dimensions,
  42. nlistArg,
  43. fileSize
  44. );
  45. const { pq_diskSize, pq_memorySize } = pqCalculator(
  46. vectorCount,
  47. segmentCount,
  48. dimensions,
  49. m,
  50. nlist
  51. );
  52. const size = vector * dimensions * 4;
  53. const nlistSize = dimensions * 4 * nlist;
  54. const byteSize = (dimensions / 8) * vector;
  55. const rawFileSize = {
  56. [INDEX_TYPES_ENUM.FLAT]: size,
  57. [INDEX_TYPES_ENUM.IVF_FLAT]: size,
  58. [INDEX_TYPES_ENUM.IVF_SQ8]: size,
  59. [INDEX_TYPES_ENUM.IVF_SQ8_HYBRID]: size,
  60. [INDEX_TYPES_ENUM.IVF_PQ]: size,
  61. };
  62. const memorySize = {
  63. [INDEX_TYPES_ENUM.FLAT]: size,
  64. [INDEX_TYPES_ENUM.IVF_FLAT]: size + nlistSize * segmentCount,
  65. [INDEX_TYPES_ENUM.IVF_SQ8]: size * 0.25 + nlistSize * segmentCount,
  66. [INDEX_TYPES_ENUM.IVF_SQ8_HYBRID]: size * 0.25 + nlistSize * segmentCount,
  67. [INDEX_TYPES_ENUM.IVF_PQ]: pq_memorySize,
  68. };
  69. const diskSize = {
  70. [INDEX_TYPES_ENUM.FLAT]: size,
  71. [INDEX_TYPES_ENUM.IVF_FLAT]:
  72. rawFileSize[INDEX_TYPES_ENUM.IVF_FLAT] +
  73. memorySize[INDEX_TYPES_ENUM.IVF_FLAT],
  74. [INDEX_TYPES_ENUM.IVF_SQ8]:
  75. rawFileSize[INDEX_TYPES_ENUM.IVF_SQ8] +
  76. memorySize[INDEX_TYPES_ENUM.IVF_SQ8],
  77. [INDEX_TYPES_ENUM.IVF_SQ8_HYBRID]:
  78. rawFileSize[INDEX_TYPES_ENUM.IVF_SQ8_HYBRID] +
  79. memorySize[INDEX_TYPES_ENUM.IVF_SQ8_HYBRID],
  80. [INDEX_TYPES_ENUM.IVF_PQ]:
  81. rawFileSize[INDEX_TYPES_ENUM.IVF_PQ] + pq_diskSize,
  82. };
  83. const byteRawFileSize = {
  84. [INDEX_TYPES_ENUM.BIN_FLAT]: byteSize,
  85. [INDEX_TYPES_ENUM.BIN_IVF_FLAT]: byteSize,
  86. };
  87. const byteMemorySize = {
  88. [INDEX_TYPES_ENUM.BIN_FLAT]: byteSize,
  89. [INDEX_TYPES_ENUM.BIN_IVF_FLAT]: dimensions * nlist + byteSize,
  90. };
  91. const byteDiskSize = {
  92. [INDEX_TYPES_ENUM.BIN_FLAT]: byteSize,
  93. [INDEX_TYPES_ENUM.BIN_IVF_FLAT]:
  94. byteRawFileSize[INDEX_TYPES_ENUM.BIN_IVF_FLAT] +
  95. byteMemorySize[INDEX_TYPES_ENUM.BIN_IVF_FLAT],
  96. };
  97. return {
  98. rawFileSize,
  99. memorySize,
  100. diskSize,
  101. byteRawFileSize,
  102. byteMemorySize,
  103. byteDiskSize,
  104. };
  105. };
  106. export const formatSize = (size: number) => {
  107. // 1:B, 2:KB, 3:MB, 4:GB, 5:TB
  108. let sizeStatus = 1;
  109. let status = 'BYTE';
  110. while (sizeStatus < 4 && size > 4096) {
  111. size = size / 1024;
  112. sizeStatus++;
  113. }
  114. status = SIZE_STATUS[sizeStatus] ?? 'KB';
  115. size = Math.ceil(size);
  116. return `${size} ${status}`;
  117. };