index_params.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. // Licensed to the LF AI & Data foundation under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. package indexparams
  17. import (
  18. "encoding/json"
  19. "fmt"
  20. "strconv"
  21. "unsafe"
  22. "github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
  23. "github.com/milvus-io/milvus/pkg/common"
  24. "github.com/milvus-io/milvus/pkg/util/funcutil"
  25. "github.com/milvus-io/milvus/pkg/util/hardware"
  26. "github.com/milvus-io/milvus/pkg/util/paramtable"
  27. "github.com/milvus-io/milvus/pkg/util/typeutil"
  28. )
  29. const (
  30. PQCodeBudgetRatioKey = "pq_code_budget_gb_ratio"
  31. NumBuildThreadRatioKey = "num_build_thread_ratio"
  32. SearchCacheBudgetRatioKey = "search_cache_budget_gb_ratio"
  33. NumLoadThreadRatioKey = "num_load_thread_ratio"
  34. BeamWidthRatioKey = "beamwidth_ratio"
  35. MaxDegreeKey = "max_degree"
  36. SearchListSizeKey = "search_list_size"
  37. PQCodeBudgetKey = "pq_code_budget_gb"
  38. BuildDramBudgetKey = "build_dram_budget_gb"
  39. NumBuildThreadKey = "num_build_thread"
  40. SearchCacheBudgetKey = "search_cache_budget_gb"
  41. NumLoadThreadKey = "num_load_thread"
  42. BeamWidthKey = "beamwidth"
  43. MaxLoadThread = 64
  44. MaxBeamWidth = 16
  45. )
  46. var configableIndexParams = typeutil.NewSet[string]()
  47. func init() {
  48. configableIndexParams.Insert(common.MmapEnabledKey)
  49. configableIndexParams.Insert(common.IndexOffsetCacheEnabledKey)
  50. }
  51. func IsConfigableIndexParam(key string) bool {
  52. return configableIndexParams.Contain(key)
  53. }
  54. func getRowDataSizeOfFloatVector(numRows int64, dim int64) int64 {
  55. var floatValue float32
  56. /* #nosec G103 */
  57. return int64(unsafe.Sizeof(floatValue)) * dim * numRows
  58. }
  59. type BigDataIndexExtraParams struct {
  60. PQCodeBudgetGBRatio float64
  61. BuildNumThreadsRatio float64
  62. SearchCacheBudgetGBRatio float64
  63. LoadNumThreadRatio float64
  64. BeamWidthRatio float64
  65. }
  66. const (
  67. BuildRatioKey = "build_ratio"
  68. PrepareRatioKey = "prepare_ratio"
  69. DefaultPQCodeBudgetGBRatio = 0.125
  70. DefaultBuildNumThreadsRatio = 1.0
  71. DefaultSearchCacheBudgetGBRatio = 0.10
  72. DefaultLoadNumThreadRatio = 8.0
  73. DefaultBeamWidthRatio = 4.0
  74. )
  75. func NewBigDataExtraParamsFromJSON(jsonStr string) (*BigDataIndexExtraParams, error) {
  76. buffer, err := funcutil.JSONToMap(jsonStr)
  77. if err != nil {
  78. return nil, err
  79. }
  80. return NewBigDataExtraParamsFromMap(buffer)
  81. }
  82. func NewBigDataExtraParamsFromMap(value map[string]string) (*BigDataIndexExtraParams, error) {
  83. ret := &BigDataIndexExtraParams{}
  84. ret.SearchCacheBudgetGBRatio = DefaultSearchCacheBudgetGBRatio
  85. setSearchCache := false
  86. var err error
  87. buildRatio, ok := value[BuildRatioKey]
  88. if !ok {
  89. ret.PQCodeBudgetGBRatio = DefaultPQCodeBudgetGBRatio
  90. ret.BuildNumThreadsRatio = DefaultBuildNumThreadsRatio
  91. } else {
  92. valueMap1 := make(map[string]float64)
  93. err = json.Unmarshal([]byte(buildRatio), &valueMap1)
  94. if err != nil {
  95. return ret, err
  96. }
  97. PQCodeBudgetGBRatio, ok := valueMap1["pq_code_budget_gb"]
  98. if !ok {
  99. ret.PQCodeBudgetGBRatio = DefaultPQCodeBudgetGBRatio
  100. } else {
  101. ret.PQCodeBudgetGBRatio = PQCodeBudgetGBRatio
  102. }
  103. BuildNumThreadsRatio, ok := valueMap1["num_threads"]
  104. if !ok {
  105. ret.BuildNumThreadsRatio = DefaultBuildNumThreadsRatio
  106. } else {
  107. ret.BuildNumThreadsRatio = BuildNumThreadsRatio
  108. }
  109. SearchCacheBudgetGBRatio, ok := valueMap1["search_cache_budget_gb"]
  110. if ok {
  111. ret.SearchCacheBudgetGBRatio = SearchCacheBudgetGBRatio
  112. setSearchCache = true
  113. }
  114. }
  115. prepareRatio, ok := value[PrepareRatioKey]
  116. if !ok {
  117. ret.SearchCacheBudgetGBRatio = DefaultSearchCacheBudgetGBRatio
  118. ret.LoadNumThreadRatio = DefaultLoadNumThreadRatio
  119. } else {
  120. valueMap2 := make(map[string]float64)
  121. err = json.Unmarshal([]byte(prepareRatio), &valueMap2)
  122. if err != nil {
  123. return ret, err
  124. }
  125. SearchCacheBudgetGBRatio, ok := valueMap2["search_cache_budget_gb"]
  126. if ok && !setSearchCache {
  127. ret.SearchCacheBudgetGBRatio = SearchCacheBudgetGBRatio
  128. }
  129. LoadNumThreadRatio, ok := valueMap2["num_threads"]
  130. if !ok {
  131. ret.LoadNumThreadRatio = DefaultLoadNumThreadRatio
  132. } else {
  133. ret.LoadNumThreadRatio = LoadNumThreadRatio
  134. }
  135. }
  136. beamWidthRatioStr, ok := value[BeamWidthRatioKey]
  137. if !ok {
  138. ret.BeamWidthRatio = DefaultBeamWidthRatio
  139. } else {
  140. beamWidthRatio, err := strconv.ParseFloat(beamWidthRatioStr, 64)
  141. if err != nil {
  142. ret.BeamWidthRatio = DefaultBeamWidthRatio
  143. } else {
  144. ret.BeamWidthRatio = beamWidthRatio
  145. }
  146. }
  147. return ret, nil
  148. }
  149. // FillDiskIndexParams fill ratio params to index param on proxy node
  150. // Which will be used to calculate build and load params
  151. func FillDiskIndexParams(params *paramtable.ComponentParam, indexParams map[string]string) error {
  152. var maxDegree string
  153. var searchListSize string
  154. var pqCodeBudgetGBRatio string
  155. var buildNumThreadsRatio string
  156. var searchCacheBudgetGBRatio string
  157. if params.AutoIndexConfig.Enable.GetAsBool() {
  158. indexParams := params.AutoIndexConfig.IndexParams.GetAsJSONMap()
  159. var ok bool
  160. maxDegree, ok = indexParams[MaxDegreeKey]
  161. if !ok {
  162. return fmt.Errorf("index param max_degree not exist")
  163. }
  164. searchListSize, ok = indexParams[SearchListSizeKey]
  165. if !ok {
  166. return fmt.Errorf("index param search_list_size not exist")
  167. }
  168. extraParams, err := NewBigDataExtraParamsFromJSON(params.AutoIndexConfig.ExtraParams.GetValue())
  169. if err != nil {
  170. return err
  171. }
  172. pqCodeBudgetGBRatio = fmt.Sprintf("%f", extraParams.PQCodeBudgetGBRatio)
  173. buildNumThreadsRatio = fmt.Sprintf("%f", extraParams.BuildNumThreadsRatio)
  174. searchCacheBudgetGBRatio = fmt.Sprintf("%f", extraParams.SearchCacheBudgetGBRatio)
  175. } else {
  176. maxDegree = params.CommonCfg.MaxDegree.GetValue()
  177. searchListSize = params.CommonCfg.SearchListSize.GetValue()
  178. pqCodeBudgetGBRatio = params.CommonCfg.PQCodeBudgetGBRatio.GetValue()
  179. buildNumThreadsRatio = params.CommonCfg.BuildNumThreadsRatio.GetValue()
  180. searchCacheBudgetGBRatio = params.CommonCfg.SearchCacheBudgetGBRatio.GetValue()
  181. }
  182. indexParams[MaxDegreeKey] = maxDegree
  183. indexParams[SearchListSizeKey] = searchListSize
  184. indexParams[PQCodeBudgetRatioKey] = pqCodeBudgetGBRatio
  185. indexParams[NumBuildThreadRatioKey] = buildNumThreadsRatio
  186. indexParams[SearchCacheBudgetRatioKey] = searchCacheBudgetGBRatio
  187. return nil
  188. }
  189. func GetIndexParams(indexParams []*commonpb.KeyValuePair, key string) string {
  190. for _, param := range indexParams {
  191. if param.Key == key {
  192. return param.Value
  193. }
  194. }
  195. return ""
  196. }
  197. // UpdateDiskIndexBuildParams update index params for `buildIndex` (override search cache size in `CreateIndex`)
  198. func UpdateDiskIndexBuildParams(params *paramtable.ComponentParam, indexParams []*commonpb.KeyValuePair) ([]*commonpb.KeyValuePair, error) {
  199. existedVal := GetIndexParams(indexParams, SearchCacheBudgetRatioKey)
  200. var searchCacheBudgetGBRatio string
  201. if params.AutoIndexConfig.Enable.GetAsBool() {
  202. extraParams, err := NewBigDataExtraParamsFromJSON(params.AutoIndexConfig.ExtraParams.GetValue())
  203. if err != nil {
  204. return indexParams, fmt.Errorf("index param search_cache_budget_gb_ratio not exist in AutoIndex Config")
  205. }
  206. searchCacheBudgetGBRatio = fmt.Sprintf("%f", extraParams.SearchCacheBudgetGBRatio)
  207. } else {
  208. paramVal, err := strconv.ParseFloat(params.CommonCfg.SearchCacheBudgetGBRatio.GetValue(), 64)
  209. if err != nil {
  210. return indexParams, fmt.Errorf("index param search_cache_budget_gb_ratio not exist in Config")
  211. }
  212. searchCacheBudgetGBRatio = fmt.Sprintf("%f", paramVal)
  213. }
  214. // append when not exist
  215. if len(existedVal) == 0 {
  216. indexParams = append(indexParams,
  217. &commonpb.KeyValuePair{
  218. Key: SearchCacheBudgetRatioKey,
  219. Value: searchCacheBudgetGBRatio,
  220. })
  221. return indexParams, nil
  222. }
  223. // override when exist
  224. updatedParams := make([]*commonpb.KeyValuePair, 0, len(indexParams))
  225. for _, param := range indexParams {
  226. if param.Key == SearchCacheBudgetRatioKey {
  227. updatedParams = append(updatedParams,
  228. &commonpb.KeyValuePair{
  229. Key: SearchCacheBudgetRatioKey,
  230. Value: searchCacheBudgetGBRatio,
  231. })
  232. } else {
  233. updatedParams = append(updatedParams,
  234. &commonpb.KeyValuePair{
  235. Key: param.Key,
  236. Value: param.Value,
  237. })
  238. }
  239. }
  240. return updatedParams, nil
  241. }
  242. // SetDiskIndexBuildParams set index build params with ratio params on indexNode
  243. // IndexNode cal build param with ratio params and cpu count, memory count...
  244. func SetDiskIndexBuildParams(indexParams map[string]string, fieldDataSize int64) error {
  245. pqCodeBudgetGBRatioStr, ok := indexParams[PQCodeBudgetRatioKey]
  246. if !ok {
  247. return fmt.Errorf("index param pqCodeBudgetGBRatio not exist")
  248. }
  249. pqCodeBudgetGBRatio, err := strconv.ParseFloat(pqCodeBudgetGBRatioStr, 64)
  250. if err != nil {
  251. return err
  252. }
  253. buildNumThreadsRatioStr, ok := indexParams[NumBuildThreadRatioKey]
  254. if !ok {
  255. return fmt.Errorf("index param buildNumThreadsRatio not exist")
  256. }
  257. buildNumThreadsRatio, err := strconv.ParseFloat(buildNumThreadsRatioStr, 64)
  258. if err != nil {
  259. return err
  260. }
  261. searchCacheBudgetGBRatioStr, ok := indexParams[SearchCacheBudgetRatioKey]
  262. // set generate cache size when cache ratio param not set
  263. if ok {
  264. SearchCacheBudgetGBRatio, err := strconv.ParseFloat(searchCacheBudgetGBRatioStr, 64)
  265. if err != nil {
  266. return err
  267. }
  268. indexParams[SearchCacheBudgetKey] = fmt.Sprintf("%f", float32(fieldDataSize)*float32(SearchCacheBudgetGBRatio)/(1<<30))
  269. }
  270. indexParams[PQCodeBudgetKey] = fmt.Sprintf("%f", float32(fieldDataSize)*float32(pqCodeBudgetGBRatio)/(1<<30))
  271. indexParams[NumBuildThreadKey] = strconv.Itoa(int(float32(hardware.GetCPUNum()) * float32(buildNumThreadsRatio)))
  272. indexParams[BuildDramBudgetKey] = fmt.Sprintf("%f", float32(hardware.GetFreeMemoryCount())/(1<<30))
  273. return nil
  274. }
  275. func SetBitmapIndexLoadParams(params *paramtable.ComponentParam, indexParams map[string]string) {
  276. _, exist := indexParams[common.IndexOffsetCacheEnabledKey]
  277. if exist {
  278. return
  279. }
  280. indexParams[common.IndexOffsetCacheEnabledKey] = params.QueryNodeCfg.IndexOffsetCacheEnabled.GetValue()
  281. }
  282. // SetDiskIndexLoadParams set disk index load params with ratio params on queryNode
  283. // QueryNode cal load params with ratio params ans cpu count...
  284. func SetDiskIndexLoadParams(params *paramtable.ComponentParam, indexParams map[string]string, numRows int64) error {
  285. dimStr, ok := indexParams[common.DimKey]
  286. if !ok {
  287. // type param dim has been put into index params before build index
  288. return fmt.Errorf("type param dim not exist")
  289. }
  290. dim, err := strconv.ParseInt(dimStr, 10, 64)
  291. if err != nil {
  292. return err
  293. }
  294. var searchCacheBudgetGBRatio float64
  295. var loadNumThreadRatio float64
  296. var beamWidthRatio float64
  297. if params.AutoIndexConfig.Enable.GetAsBool() {
  298. extraParams, err := NewBigDataExtraParamsFromJSON(params.AutoIndexConfig.ExtraParams.GetValue())
  299. if err != nil {
  300. return err
  301. }
  302. searchCacheBudgetGBRatio = extraParams.SearchCacheBudgetGBRatio
  303. loadNumThreadRatio = extraParams.LoadNumThreadRatio
  304. beamWidthRatio = extraParams.BeamWidthRatio
  305. } else {
  306. searchCacheBudgetGBRatio, err = strconv.ParseFloat(params.CommonCfg.SearchCacheBudgetGBRatio.GetValue(), 64)
  307. if err != nil {
  308. return err
  309. }
  310. loadNumThreadRatio, err = strconv.ParseFloat(params.CommonCfg.LoadNumThreadRatio.GetValue(), 64)
  311. if err != nil {
  312. return err
  313. }
  314. beamWidthRatio, err = strconv.ParseFloat(params.CommonCfg.BeamWidthRatio.GetValue(), 64)
  315. if err != nil {
  316. return err
  317. }
  318. }
  319. indexParams[SearchCacheBudgetKey] = fmt.Sprintf("%f",
  320. float32(getRowDataSizeOfFloatVector(numRows, dim))*float32(searchCacheBudgetGBRatio)/(1<<30))
  321. numLoadThread := int(float32(hardware.GetCPUNum()) * float32(loadNumThreadRatio))
  322. if numLoadThread > MaxLoadThread {
  323. numLoadThread = MaxLoadThread
  324. }
  325. indexParams[NumLoadThreadKey] = strconv.Itoa(numLoadThread)
  326. beamWidth := int(float32(hardware.GetCPUNum()) * float32(beamWidthRatio))
  327. if beamWidth > MaxBeamWidth {
  328. beamWidth = MaxBeamWidth
  329. }
  330. indexParams[BeamWidthKey] = strconv.Itoa(beamWidth)
  331. return nil
  332. }
  333. func AppendPrepareLoadParams(params *paramtable.ComponentParam, indexParams map[string]string) error {
  334. if params.AutoIndexConfig.Enable.GetAsBool() { // `enable` only for cloud instance.
  335. // override prepare params by
  336. for k, v := range params.AutoIndexConfig.PrepareParams.GetAsJSONMap() {
  337. indexParams[k] = v
  338. }
  339. for k, v := range params.AutoIndexConfig.LoadAdaptParams.GetAsJSONMap() {
  340. indexParams[k] = v
  341. }
  342. }
  343. return nil
  344. }