Vector search method and apparatus, and electronic device and storage medium
By clustering and grouping vector libraries, using the characteristics of disk and memory, the response speed and recall problems of large-scale vector search under resource constraints are solved, and efficient vector search is achieved.
Patent Information
- Application Number
- PCT/CN2024/093075
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Priority Date
- 2023-05-17
- Filing Date
- 2024-05-14
- Publication Date
- 2024-11-21
AI Technical Summary
In the case of resource limitation, how to achieve close-linear high retrieval recall and second-level response speed in large-scale vector search tasks, especially in the search scenario of billion-level vector libraries, the existing technology is difficult to effectively solve.
By pre-clustering and grouping vectors in the vector library, a vector group corresponding to k cluster center vectors and each cluster center vector is obtained. These data are written to the disk, and the persistence and storage characteristics of the disk are used to reduce memory resources. Consumption: Load the cluster center vector from disk into memory during the search stage, and search vectors from vector groups in memory to reduce the search space and improve response speed.
The feasibility of large-scale vector search under resource constraints is realized, the search response speed and recall rate is improved, and the accuracy is close to global linear search, reducing the number of disk reads, making the average single search time almost pure memory type Search framework.
Smart Images

Figure CN2024093075_21112024_PF_FP_ABST
Abstract
Description
Vector retrieval method, device, electronic device and storage medium
[0001] CROSS-REFERENCE TO RELATED APPLICATIONS
[0002] This application claims priority to a Chinese patent application filed with the China Patent Office on May 17, 2023, with application number 202310559610.4 and application name “Vector Retrieval Method, Device, Electronic Device and Storage Medium,” the entire contents of which are incorporated herein by reference. Technical Field
[0003] The present application relates to the field of data processing technology, and in particular to a vector retrieval method, device, electronic device, and storage medium. Background Art
[0004] With the increasing popularity of deep learning, some search scenarios, such as image search, face recognition, and content recommendation, currently use deep neural networks to extract corresponding high-dimensional features from vector libraries before searching within this vector space. When the number of vector libraries reaches hundreds of millions, and the dimensions of each vector exceed 512, completing the search task with a recall rate exceeding 95% and a response speed of seconds typically requires a server with 256GB of memory or higher. However, in real-world applications, limited available resources can severely impact the recall rate and response speed of searches.
[0005] Therefore, how to achieve a near-linear high search recall rate and a response speed of seconds in large-scale search tasks, such as search tasks for billion-level vector libraries, under resource constraints is an urgent problem that needs to be solved.
[0006] Summary of the Invention
[0007] The purpose of an embodiment of the present application is to provide a vector retrieval method, including: obtaining n cluster center vectors from the k cluster center vectors based on the distance between a first vector and each cluster center vector in the k cluster center vectors, wherein k is a positive integer greater than 1 and n is a positive integer less than k; and obtaining N vectors from the vector group corresponding to the n cluster center vectors as retrieval results based on the distance between the first vector and each vector in the vector group corresponding to each of the n cluster center vectors, wherein N is a positive integer.
[0008] The purpose of an embodiment of the present application is also to provide an electronic device, comprising: a processor; and a memory for storing instructions; wherein, when the instructions are executed by the processor, the processor performs the following steps: based on the distance between the first vector and each of the k cluster center vectors, obtaining n cluster center vectors from the k cluster center vectors, wherein k is a positive integer greater than 1, and n is a positive integer less than k; and based on the distance between the first vector and each vector in the vector group corresponding to each of the n cluster center vectors, obtaining N vectors from the vector group corresponding to the n cluster center vectors as retrieval results, wherein N is a positive integer.
[0009] The purpose of an embodiment of the present application is also to provide a computer-readable storage medium. When the instructions in the storage medium are executed by the processor of an electronic device, the electronic device executes a vector retrieval method. The vector retrieval method includes: obtaining n cluster center vectors from the k cluster center vectors based on the distance between the first vector and each cluster center vector in the k cluster center vectors, wherein k is a positive integer greater than 1 and n is a positive integer less than k; and obtaining N vectors from the vector group corresponding to the n cluster center vectors as retrieval results based on the distance between the first vector and each vector in the vector group corresponding to each of the n cluster center vectors, wherein N is a positive integer.
[0010] The purpose of an embodiment of the present application is also to provide a vector retrieval device, including: a first acquisition unit, used to obtain n cluster center vectors from the k cluster center vectors based on the distance between the first vector and each cluster center vector in the k cluster center vectors, wherein k is a positive integer greater than 1 and n is a positive integer less than k; a second acquisition unit, used to obtain N vectors from the vector group corresponding to the n cluster center vectors as retrieval results based on the distance between the first vector and each vector in the vector group corresponding to each of the n cluster center vectors, wherein N is a positive integer. BRIEF DESCRIPTION OF THE DRAWINGS
[0011] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:
[0012] FIG1 is a schematic diagram of an application scenario applicable to a vector retrieval method provided by an embodiment of the present application;
[0013] FIG2 is a schematic diagram of a flow chart of a vector search method provided by an embodiment of the present application;
[0014] FIG3 is a schematic flow chart of a vector search method provided in another embodiment of the present application;
[0015] FIG4 is a schematic flow chart of a vector search method provided in yet another embodiment of the present application;
[0016] FIG5 is a flow chart of a vector search method provided in yet another embodiment of the present application;
[0017] FIG6 is a schematic structural diagram of a vector search device provided by an embodiment of the present application;
[0018] FIG7 is a schematic structural diagram of an electronic device provided by an embodiment of the present application. DETAILED DESCRIPTION
[0019] To make the purpose, technical solutions, and advantages of this application more clear, the technical solutions of this application will be clearly and completely described below in conjunction with the specific embodiments of this application and the corresponding drawings. Obviously, the embodiments described are only part of the embodiments of this application, not all of them. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of this application.
[0020] The terms "first," "second," and the like in this specification and claims are used to distinguish similar objects and are not intended to describe a particular order or precedence. It should be understood that such terms are interchangeable, where appropriate, so that the embodiments of the present application may be implemented in an order other than that illustrated or described herein. Furthermore, the term "and / or" in this specification and claims refers to at least one of the connected objects, and the character " / " generally indicates that the connected objects are in an "or" relationship.
[0021] As mentioned in the background, achieving high recall rates and sub-second response times for large-scale, high-dimensional vector retrieval tasks typically consumes significant computing resources. However, in practice, limited resources severely impact recall rates and response times.
[0022] In view of this, the embodiment of the present application aims to propose a vector retrieval solution that combines disk and memory, taking advantage of the disk's advantages of large capacity and persistent data storage, clustering and grouping the vectors in the vector library in advance through a clustering algorithm, obtaining k cluster center vectors and vector groups corresponding to each cluster center vector, and writing these data to the disk to ensure that these data can be persisted and not easily lost, providing reliable data support for subsequent vector retrieval; since the vectors that meet the retrieval requirements in the vector library are usually vectors with a close distance to the first vector, and the similarity between different vectors that are close to the same cluster center vector is high, the vectors in the vector group corresponding to the cluster center vector that is closer to the first vector have a high similarity with the first vector, based on this, in In the retrieval stage, the k cluster center vectors of the vector library are loaded from the disk into the memory, and then the vector is retrieved from the vector group stored in the memory. This can reduce the consumption of memory resources, reduce the vector search space, and improve the retrieval response speed, making large-scale (for example, billions) vector retrieval feasible under resource-constrained conditions; especially when there are many first vectors, since the same cluster center vector may be a close neighbor of multiple first vectors, by loading the vector group corresponding to the cluster center vector of the first vector's neighbors into the memory, the vector group in the memory can be reused by multiple first vectors without having to read the corresponding data from the disk each time, thereby greatly reducing the number of disk reads and making the average single retrieval time close to that of a pure memory-based retrieval framework (faiss, milvus, etc.). In addition, the vector retrieval method of the embodiment of the present application is based on the distance between the first vector and each vector in the related vector group. It is a linear matching method with no precision loss. Therefore, under this overall process, the precision is close to that of the global linear retrieval, and a high retrieval recall rate close to linear can be achieved.
[0023] It should be understood that the vector retrieval method provided in the embodiments of the present application can be executed by an electronic device or software installed in the electronic device. The electronic device herein may include a terminal device, such as a smartphone, tablet computer, laptop computer, desktop computer, intelligent voice interaction device, smart home appliance, smartwatch, vehicle-mounted terminal, aircraft, etc.; or, the electronic device may also include a server, such as an independent physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server providing cloud computing services.
[0024] The following describes in detail the technical solutions provided by various embodiments of the present application in conjunction with the accompanying drawings.
[0025] First, the application scenario of the vector search method provided in the embodiment of the present application is described with reference to Figure 1. It should be understood that the application of the vector search method provided in the embodiment of the present application to the scenario shown in Figure 1 is only an exemplary description and should not be understood as limiting the application environment of the method.
[0026] As shown in Figure 1, a practical application scenario for the vector retrieval method provided in an embodiment of the present application includes a caller and a retrieval system. The caller is the party that requires vector retrieval. The caller can send a retrieval request for a first vector to the retrieval system and receive the retrieval results returned by the retrieval system. In actual applications, the retrieval system provides an interface for the caller to call, through which the caller can send a retrieval request to the retrieval system.
[0027] The retrieval system is deployed with a vector library, disk, and memory. In practical applications, the retrieval system can run on electronic devices. These electronic devices can include terminal devices such as smartphones, tablets, laptops, desktop computers, intelligent voice interaction devices, smart home appliances, smart watches, in-vehicle terminals, and aircraft. Alternatively, the electronic devices can include servers, such as standalone physical servers, server clusters or distributed systems composed of multiple physical servers, or cloud servers providing cloud computing services.
[0028] The vector library stores a large number of vectors. The retrieval system can start the master control node (i.e., the main service process) and pre-cluster and group the vectors in the vector library through the master control node to obtain k (k is a positive integer greater than 1) cluster center vectors and the vector group corresponding to each cluster center vector. The k cluster center vectors and the identifiers (IDs) corresponding to each cluster center vector are structured and stored on disk to form a file file_reg_vector. The vector groups corresponding to each of the k cluster center vectors are also structured and stored on disk to form a file file_k_cluster_center for subsequent vector retrieval.
[0029] After receiving a search request from the caller, the retrieval system loads the file file_reg_vector from disk into memory via the master node during the pre-processing phase. The system then retrieves the first n (n is a positive integer less than k) cluster center vectors closest to the first vector from memory. These cluster center vectors are the cluster center vectors of the first vector's neighbors. The system then reads the vector groups corresponding to each of these n cluster center vectors from the file file_k_cluster_center and loads them into memory. During the retrieval phase, the retrieval node (i.e., the child service process) is started. The retrieval node retrieves vectors similar to the first vector from the vector group stored in memory and returns them to the master node. The master node then summarizes and organizes the retrieval results and returns them to the caller.
[0030] The embodiment of the present application will describe in detail the process of the retrieval system performing vector retrieval on the first vector.
[0031] Please refer to Figure 2, which is a flowchart of a vector search method provided in one embodiment of the present application. This method can be performed by an electronic device, specifically a search system deployed in the electronic device (the search system shown in Figure 1). As shown in Figure 2, the method may include the following steps:
[0032] S202: Receive a first vector.
[0033] Among them, the first vector can be carried in the retrieval request sent by the caller. The retrieval request is sent by the caller based on the target processing task. The first vector can be obtained by the caller to extract features of the target object, specifically through an algorithm model, such as Convolutional Neural Networks (CNN), Recurrent Neural Networks (RNN), a language model based on an attention mechanism (Transformer), etc., after extracting features of the target object. The target object may, for example, include but is not limited to at least one of the following objects: image, speech, and text. The target processing task may, for example, include but is not limited to image detection tasks, text classification tasks, speech recognition tasks, etc., and the target object varies depending on the target processing task. In this case, the vectors stored in the vector library may include feature vectors obtained by extracting features from multiple sample objects.
[0034] For example, if the target processing task is to perform face recognition based on a facial image, then the target object is the facial image, and the first vector is the image feature vector obtained by the caller through feature extraction of the facial image. For another example, if the target processing task is to perform speaker recognition on a speech segment, then the target object is the speech, and the first vector is the speech feature vector obtained by the caller through feature extraction of the speech. For another example, if the target processing task is to perform intent recognition on a text segment, then the target object is the text, and the first vector is the text feature vector obtained by the caller through feature extraction of the text.
[0035] It is worth noting that, in practical applications, each target object may correspond to a first vector, and each first vector is composed of a number of values, that is, each first vector is a multi-dimensional vector. The vectors in the vector library are stored on disk.
[0036] S204, loading the k cluster center vectors of the vector library from the disk into the memory.
[0037] Wherein, k is a positive integer greater than 1, and k can be set according to actual conditions such as the number of vectors in the vector library, the vector dimension, and the size of memory resources, and the embodiments of the present application do not limit this. The k cluster center vectors are obtained by clustering the vectors in the vector library based on a preset clustering algorithm and written to the disk. The specific implementation method of clustering to obtain k cluster center vectors will be described in detail later. It can be understood that since the disk has the advantages of large capacity and persistent data storage, by writing the k cluster center vectors of the vector library to the disk, it can be ensured that these data are persisted and not easily lost, providing reliable data support for subsequent vector retrieval.
[0038] Since vector retrieval is based on the distance between vectors, and k cluster center vectors can cluster vectors with similar distances in the vector library together, loading k cluster center vectors into memory is helpful for quickly screening out vectors in the vector library that may be similar to the first vector, and performing accurate retrieval based on the screened vectors, thereby helping to improve the vector retrieval speed.
[0039] In practical applications, the k cluster center vectors are stored in a file file_reg_vector on the disk. In this case, the file file_reg_vector can be loaded from the disk into the memory.
[0040] S206 , obtaining the first n cluster center vectors closest to the first vector from the memory.
[0041] Here, n is a positive integer less than k.
[0042] Specifically, for each cluster center vector stored in the memory, the distance between the cluster center vector and the first vector can be calculated, and then the first n cluster center vectors closest to the first vector can be obtained from the k cluster center vectors stored in the memory. It is worth noting that in the embodiments of the present application, the distance between any two vectors can be represented by various distances in the field, such as Euclidean distance, cosine distance, etc., and the embodiments of the present application are not limited to this.
[0043] It can be understood that since the similarity between two vectors with close distances is high, the first n cluster center vectors closest to the first vector have a high similarity with the first vector and can be called the neighboring cluster center vectors of the first vector.
[0044] S208 , loading the vector groups corresponding to the first n cluster center vectors from the disk into the memory.
[0045] The disk stores k vector groups corresponding to the cluster center vectors. Each vector group is created by grouping the vectors in the vector library based on the k cluster center vectors and then written to the disk. As can be understood, due to the advantages of disks such as large capacity and persistent data storage, writing the vector groups corresponding to the k cluster center vectors to the disk ensures that this data is persistently stored and not easily lost, providing reliable data support for subsequent vector retrieval.
[0046] Since the vectors that meet the retrieval requirements in the vector library are usually vectors that are close to the first vector, and the similarity between different vectors that are close to the same cluster center vector is high, the vectors in the vector group corresponding to the cluster center vector that is closer to the first vector have a high similarity to the first vector; by loading the vector groups corresponding to the first n cluster center vectors from the disk into the memory, so as to further accurately retrieve vectors from these vector groups, not only can the vector retrieval space be reduced and the vector retrieval speed be improved, but also the memory resource usage can be reduced.
[0047] In practical applications, the vector groups corresponding to the k cluster center vectors are structured and stored in a file file_k_cluster_center on disk. In this case, the vector groups corresponding to the first n cluster center vectors can be read from the file file_k_cluster_center on disk and loaded into memory.
[0048] S210 , obtaining first N vectors closest to the first vector from the vector group stored in the memory as search results.
[0049] Wherein, N is a positive integer. In practical applications, N can be set according to the search requirements of the caller, etc., and the embodiments of the present application do not limit this.
[0050] Since the vectors that meet the retrieval requirements in the vector library are usually vectors that are close to the first vector, and the similarity between different vectors that are close to the same cluster center vector is high, the vectors in the vector group corresponding to the cluster center vector that is closer to the first vector have a higher similarity with the first vector. Based on this, by performing vector retrieval from the vector group corresponding to the first vector's nearest cluster center vector, not only can the accuracy of the retrieval results be ensured, but also because only some vectors in the vector library that are close to the first vector are loaded into the memory, rather than the full vectors of the vector library, performing vector retrieval on this basis is beneficial to reducing the consumption of memory resources, shrinking the vector search space, and improving the retrieval response speed compared to directly retrieving the full vector from the vector library, making large-scale (hundreds of millions) vector retrieval feasible under resource-constrained conditions; especially when there are many first vectors, since the same cluster center vector may be a close neighbor of multiple first vectors, by loading the vector group corresponding to the cluster center vectors of the first vector's neighbors into the memory, the vector group in the memory can be reused by multiple first vectors without having to read the corresponding data from the disk each time, thereby greatly reducing the number of disk reads and making the average single retrieval time close to that of pure memory-based retrieval frameworks (faiss, milvus, etc.).
[0051] Optionally, to further improve the accuracy of the search results, the above S210 may include the following steps:
[0052] S2101 , obtaining first T vectors closest to a first vector from a target vector group stored in a memory as candidate vectors of the first vector.
[0053] The target vector group is any one of the vector groups corresponding to the first n cluster center vectors, and T is a positive integer not less than N.
[0054] Specifically, each vector group in the memory can be used as a target vector group in turn, and the distance between each vector in the target vector group and the first vector can be calculated respectively; then, the first T vectors closest to the first vector are selected from the target vector group as candidate vectors of the first vector.
[0055] S2102 , summarizing candidate vectors obtained from each vector group stored in the memory to obtain a candidate vector set corresponding to the first vector.
[0056] For example, assuming that the vector group in the memory includes two vector groups corresponding to the respective cluster center vectors, the candidate vectors obtained from the first vector group include vector a and vector b, and the candidate vectors obtained from the second vector group include vector b and vector c, then by summarizing these candidate vectors, a candidate vector set {a, b, c} can be obtained.
[0057] S2103 : Select the first N candidate vectors closest to the first vector from the candidate vector set as the search results.
[0058] Since the number of candidate vectors in the candidate vector set may be greater than N, the vectors in the candidate vector set are sorted by their distance from the first vector, and the first N candidate vectors closest to the first vector are selected as the search results corresponding to the first vector. This not only meets the search requirement for the number of vectors, but also ensures the accuracy of the search results.
[0059] For example, taking the candidate vector set {a, b, c} as an example, the candidate vectors are sorted in order from near to far from the first vector as vector a>vector b>vector c. Assuming N=2, vector a and vector b can be selected as the search results corresponding to the first vector.
[0060] Of course, it should be understood that if the number of candidate vectors in the candidate vector set is equal to N, the candidate vector set is used as the search result corresponding to the first vector.
[0061] It is worth noting that, in practical applications, the number of the first vectors may be one or more. In the case of multiple first vectors, the above steps S2101 to S2103 may be executed for each first vector to obtain the search result corresponding to the first vector.
[0062] Optionally, to further improve search response speed and search accuracy, upon receiving a first vector, after obtaining the first n cluster center vectors closest to the first vector in S206, the vector search method provided in an embodiment of the present application may further include: for each first vector, determining, via the master control node, the task queues corresponding to the first n cluster center vectors closest to the first vector in memory, thereby obtaining n target task queues corresponding to the first vector; and then, writing, via the master control node, the first vector into each of the n target task queues. The memory includes task queues corresponding to each of the k cluster center vectors.
[0063] For example, assuming that the received first vector includes vector A and vector B, where the cluster center vectors closest to vector A include cluster center vector 1 and cluster center vector 2, and the cluster center vectors closest to vector B include cluster center vector 2 and cluster center vector 3, and the memory contains task queues 1 to 3, with task queue 1 corresponding to cluster center vector 1, task queue 2 corresponding to cluster center vector 2, and task queue 3 corresponding to cluster center vector 3, then vector A can be written to task queue 1 and task queue 2, and vector B can be written to task queue 2 and task queue 3. Thus, task queue 1 includes vector A, task queue 2 includes vector A and vector B, and task queue 3 includes vector B.
[0064] Furthermore, the above-mentioned step S2101 may include: traversing the first vector of each of the k task queues via a search node; and for each task queue, obtaining, via the search node, from the vector group corresponding to the cluster center vector of the task queue in memory, the first T vectors closest to the currently traversed first vector, as candidate vectors for the currently traversed first vector. Of course, after executing the above-mentioned step S2101, the memory occupied by the vector group corresponding to the cluster center vector of the task queue may be released to further reduce memory resource consumption.
[0065] It's understandable that, because task queues inherently offer advantages such as improving system performance, reducing peak load, and reducing system coupling through asynchronous processing, when there are multiple first vectors, the master control node first stores them in the corresponding task queue, and the search node process then retrieves them by traversing each task queue. This decouples the master control node from the search node, allowing the master control node to perform other operations while the search node is executing a search, such as continuing to receive other first vectors sent by the caller, without having to wait for the search node to return a search result. This enables asynchronous processing during the search process, thereby improving the performance of the entire search system and achieving peak load reduction.
[0066] In an optional implementation, a serial method can be used to traverse the first vectors in each task queue in the memory. Specifically, the retrieval node includes a first retrieval node, which can be started (i.e., a first sub-service process for executing the retrieval task) to poll each task queue in the memory through the first retrieval node; if the currently polled task queue is not empty, it indicates that the first vector exists in the task queue, and then the first vector in the currently polled task queue is traversed. It is understandable that traversing the first vectors in each task queue in a serial method can reduce the number of retrieval nodes, which is conducive to reducing the consumption of computing resources.
[0067] In another optional implementation, the first vectors in each task queue in memory can be traversed in parallel. Specifically, the search node includes k second search nodes, and k second search nodes (i.e., second sub-service processes for executing search tasks) can be started, each second search node being used to traverse one of the k task queues. The first vectors in the task queues corresponding to each second search node are traversed in parallel by the k second search nodes.
[0068] For example, taking the above-mentioned task queue 1 to task queue 3 as an example, a total of three second search nodes, namely, second search node 1 to second search node 3, can be started, wherein second search node 1 corresponds to task queue 1, second search node 2 corresponds to task queue 2, and second search node 3 corresponds to task queue 3, and these three second search nodes run in parallel; the first vector in task queue 1 is traversed through the second search node 1 to obtain the first T vectors closest to the first vector, and then the first T vectors are submitted to the main control node (i.e., the main service process), and the main control node executes the above-mentioned S2102 to S2103 to obtain the search result corresponding to the traversed first vector. Search result; traverse the first vector in task queue 2 through the second search node 2, obtain the first T vectors closest to the first vector, and then submit the first T vectors to the master control node (i.e., the master service process), and the master control node executes the above S2102 to S2103 to obtain the search result corresponding to the traversed first vector; traverse the first vector in task queue 3 through the second search node 3, obtain the first T vectors closest to the first vector, and then submit the first T vectors to the master control node (i.e., the master service process), and the master control node executes the above S2102 to S2103 to obtain the search result corresponding to the traversed first vector.
[0069] It is understandable that traversing the first vectors in each task queue in parallel can improve the traversal efficiency of the first vectors, thereby improving the search response speed. In addition, a distributed processing architecture is formed between the multiple second search nodes and the master control node, and the nodes cooperate with each other to jointly perform vector searches, which is conducive to further improving the search response speed.
[0070] In practical applications, all the first vectors in the task queue can be taken out to form a vector matrix, and the vectors in each vector group can be formed into another vector matrix. The distance between these two vector matrices is calculated, and the vector closest to each first vector is output, which can further improve the retrieval speed.
[0071] The embodiments of the present application illustrate some specific implementations of S210. Of course, it should be understood that S210 can also be implemented in other ways, such as sorting the vectors stored in the memory according to their distance from the first vector, and then obtaining the top N vectors closest to the first vector as the search results.
[0072] Optionally, to further improve search response speed, especially when the dimensions of each vector in the vector library are high, operations such as vector quantization can be used when performing vector searches within a vector group, sacrificing some precision in exchange for higher search efficiency. Specifically, in S210 , the vectors in each vector group stored in the memory can be quantized, and the first N vectors closest to the first vector are obtained from the vector groups after the vector quantization process as the search results corresponding to the first vector.
[0073] For example, the vector quantization processing of each vector group stored in the memory can be implemented using various vector quantization methods commonly used in the art, such as the scalar quantization (SQ) algorithm, the vector quantization (PQ) algorithm, etc., wherein the SQ algorithm quantizes each dimension into a number of specified bits, such as quantizing a 32-bit int into an 8-bit int, reducing storage and computing costs by losing a certain amount of precision; the PQ algorithm records the distance between the first vector x and the vector y in the vector group as the distance between the first vector and the bucket where the vector y is located. A global search only requires ksub calculations (calculating the distance between the first vector x and the representative vectors of the ksub buckets) and n table lookups (checking which bucket the vector y in the vector group belongs to). Using the PQ algorithm for vector quantization loses less precision than the SQ algorithm. The SQ algorithm and the PQ algorithm will not be described in detail.
[0074] Optionally, when the received first vector comes from a retrieval request of the caller, after S210, the vector retrieval method provided in the embodiment of the present application may further include: returning a retrieval result corresponding to the first vector to the caller, where the retrieval result is used by the caller to perform a target processing task on the target object.
[0075] For example, if the target processing task is a task of performing face recognition based on a target face image, the target object is the target face image, and the first vector is the image feature vector obtained by the caller through feature extraction of the target face image. Then, the retrieval result may include N face feature vectors similar to the image feature vector and the face identity identifier to which each face feature vector belongs; by returning the retrieval result to the caller, the caller can search for the user identity corresponding to each face feature vector from a pre-established identity information library based on the face identity identifier to which the face feature vector belongs; further, if N=1, then the user identity can be used as the face recognition result of the target face image; if N is greater than 1, then the user identity corresponding to each face identity identifier can be searched from the pre-established identity information library and displayed to relevant personnel, who can further determine the user identity to which the target face image belongs from these user identities, thereby improving the accuracy of face recognition.
[0076] For example, if the target processing task is to identify the intent of a target text, then the target object is the target text, and the first vector is the text feature vector obtained by the caller through feature extraction of the target text. Then, the retrieval result may include N sample text feature vectors similar to the text feature vector and the text identifier corresponding to each sample text feature vector; by returning the retrieval result to the caller, the caller can search for the intent corresponding to the text to which the sample text identifier belongs from a pre-established intent information library for each sample text feature vector based on the text identifier corresponding to the sample text feature vector; further, if N=1, then the intent can be used as the intent recognition result of the target text; if N is greater than 1, then the intent corresponding to the text to which each text identifier belongs can be searched from the pre-established intent information library and displayed to relevant personnel, who can further determine the intent of the target text from these intentions, thereby improving the accuracy of intent recognition.
[0077] Please refer to Figure 3, which is a schematic diagram of a vector retrieval method provided in another embodiment of the present application. Compared to the vector retrieval method shown in Figure 2, this method also includes the steps of obtaining k cluster center vectors and a vector group corresponding to each cluster center vector. As shown in Figure 3, the method may include the following steps:
[0078] S302: Receive a first vector.
[0079] Specifically, the specific implementation method of S302 is similar to the specific implementation method of S202 in the embodiment shown in Figure 2. Please refer to the detailed description of S202 above and it will not be repeated here.
[0080] S304: Perform uniform random sampling on the vector library to obtain a vector subset.
[0081] In an optional implementation, uniform random sampling may be performed on all vectors in the vector library to obtain a vector subset.
[0082] In another optional implementation, to further improve the efficiency of vector retrieval, the vectors in the vector library can be divided into multiple attribute subsets based on the target attributes of the vectors in the vector library, where the attribute values of the target attributes of the vectors in the same attribute subset are the same; then, for each attribute subset, the attribute subset is uniformly randomly sampled to obtain the vector subset under the attribute subset.
[0083] For example, assuming that the vector library stores facial feature vectors of multiple sample faces, these facial feature vectors can be divided into male subsets and female subsets according to the target attribute "gender"; further, the male subset is uniformly randomly sampled to obtain vector subset 1 under the male subset, and the female subset is uniformly randomly sampled to obtain vector subset 2 under the female subset.
[0084] S306 , clustering the vectors in the vector subset based on a preset clustering algorithm to obtain k cluster center vectors.
[0085] Among them, the preset clustering algorithm can adopt various clustering algorithms commonly used in this field, such as K-means clustering algorithm, hierarchical clustering algorithm, etc., which is not limited in the embodiment of the present application.
[0086] Optionally, the number k of cluster center vectors can be set based on the vector scale and the required retrieval accuracy; then, the k-means clustering algorithm is used to perform clustering based on the distance between each vector in the vector subset to obtain k clusters, each cluster containing at least one vector; further, for each cluster, the cluster center vector of the cluster is calculated based on the vectors contained in the cluster, wherein the cluster center vector represents the center point of the corresponding cluster, thereby obtaining k cluster center vectors.
[0087] When the vectors in the vector library are divided into multiple attribute subsets, the k cluster center vectors corresponding to the vector subset under the attribute subset can be obtained for each attribute subset through the above-mentioned k-means clustering method. These cluster center vectors are also called k cluster center vectors under the attribute subset.
[0088] S308 : Based on the distances between the vectors in the vector library and the k cluster center vectors, the vectors in the vector library are divided into vector groups corresponding to the cluster center vectors with the closest distances.
[0089] When the vector subset is obtained by uniform random sampling of all vectors in the vector library, the distance between each vector in the vector library and the k cluster center vectors can be calculated; then, the vector is divided into the vector group corresponding to the cluster center vector closest to it.
[0090] When the vectors in the vector library are divided into multiple attribute subsets, for each attribute subset, based on the distances between the vectors in the attribute subset and the k cluster center vectors under the attribute subset, the vectors in the attribute subset can be divided into the vector group corresponding to the nearest cluster center vector, thereby obtaining the vector group corresponding to each of the k cluster center vectors under the attribute subset.
[0091] For example, taking the case where the multiple attribute subsets include a male subset and a female subset, for the male subset, the distance between each vector in the male subset and the k cluster center vectors in the male subset can be calculated, and the vector can be divided into the vector group corresponding to the cluster center vector closest to the male subset, thereby obtaining the vector group corresponding to each of the k cluster center vectors in the male subset; similarly, executing S308 above for the female subset can obtain the vector group corresponding to each of the k cluster center vectors in the female subset.
[0092] It can be understood that after the vectors in the vector subset are clustered by a preset clustering algorithm, the vectors in the vector subset are grouped according to k cluster center vectors, so that each vector in the vector subset is accessed only once on average (calculating the distance between it and the cluster center vector) and can be assigned to the corresponding vector group, thereby further improving the vector retrieval efficiency.
[0093] S310, loading k cluster center vectors of the vector library from the disk into the memory.
[0094] Specifically, the specific implementation of S310 is similar to the specific implementation of S204 in the embodiment shown in FIG. 2 . Please refer to the detailed description of S204 above and no further details will be given.
[0095] Optionally, if the vectors in the vector library are divided into multiple attribute subsets, in S310 above, a target attribute subset corresponding to the first vector may be determined from the multiple attribute subsets; and the k cluster center vectors under the target attribute subset may be loaded from disk into memory. For example, assuming that the first vector is a facial feature vector obtained by extracting features from a female face image, and the vectors in the vector library are divided into male and female subsets, the female subset may be determined as the target attribute subset, and the k cluster center vectors under the female subset may be loaded from disk into memory.
[0096] In this way, in the subsequent vector retrieval stage, vector retrieval is performed from the vector group corresponding to each cluster center vector under the target attribute subset, further reducing the vector retrieval space and improving the retrieval response speed.
[0097] S312: Obtain the first n cluster center vectors closest to the first vector from the memory.
[0098] Specifically, the specific implementation method of S312 is similar to the specific implementation method of S206 in the embodiment shown in Figure 2. Please refer to the detailed description of S206 above and it will not be repeated here.
[0099] S314: Load the vector groups corresponding to the first n cluster center vectors from the disk into the memory.
[0100] Specifically, the specific implementation method of S314 is similar to the specific implementation method of S208 in the embodiment shown in Figure 2. Please refer to the detailed description of S208 above and it will not be repeated here.
[0101] S316 , obtaining the first N vectors closest to the first vector from the vector group stored in the memory as the search result.
[0102] Specifically, the specific implementation method of S316 is similar to the specific implementation method of S210 in the embodiment shown in Figure 2. Please refer to the detailed description of S210 above and will not be repeated here.
[0103] The vector retrieval method provided by one or more embodiments of the present application utilizes the advantages of large capacity and persistent data storage of disks, and pre-clusters and groups the vectors in the vector library through a clustering algorithm to obtain k cluster center vectors and vector groups corresponding to each cluster center vector, and writes these data to the disk to ensure that the data can be persisted and not easily lost, thereby providing reliable data support for subsequent vector retrieval; since the vectors in the vector library that meet the retrieval requirements are usually vectors with a close distance to the first vector, and the similarity between different vectors that are close to the same cluster center vector is high, the vectors in the vector group corresponding to the cluster center vector that is closer to the first vector have a higher similarity to the first vector. Based on this, in the retrieval stage, the k cluster center vectors of the vector library are loaded from the disk into the memory, the first n cluster center vectors closest to the first vector are obtained from the memory, and then each of these n cluster center vectors is loaded into the memory. Since the corresponding vector group is loaded from the disk into the memory, the vectors that are closer to the first vector are searched from these vector groups in the memory as the search results; since only some vectors in the vector library that are closer to the first vector are loaded into the memory, rather than the full vector of the vector library, vector retrieval is performed on this basis, which is conducive to reducing the consumption of memory resources, shrinking the vector search space, and improving the retrieval response speed, making large-scale (hundreds of millions) vector retrieval feasible under resource-constrained conditions; especially when there are many first vectors, since the same cluster center vector may be a close neighbor of multiple first vectors, by loading the vector group corresponding to the cluster center vector of the first vector's neighbors into the memory, the vector group in the memory can be reused by multiple first vectors without having to read the corresponding data from the disk each time, thereby greatly reducing the number of disk reads and making the average single retrieval time close to that of a pure memory-based retrieval framework (faiss, milvus, etc.). In addition, the vector retrieval method of the embodiment of the present application is based on the distance between the first vector and each vector in the relevant vector group. It is a linear matching method with no precision loss. Therefore, under this overall process, the precision is close to that of global linear retrieval, and a high retrieval recall rate close to linear can be achieved.
[0104] The vector retrieval method provided in the embodiment of the present application has been proven effective in the task of retrieving 400 million face vectors (512 dimensions): when the server is configured as a single machine with 16G of memory, the recall rate is close to that of a full linear retrieval (number of units = 1024, n = 32). In the case of batch retrieval (475 first vectors), the average single retrieval time is 1.6 seconds.
[0105] The above describes the vector retrieval methods provided by the embodiments of the present application. These methods are applicable to various application scenarios with vector retrieval requirements, including but not limited to face recognition, text classification, speech segmentation, etc. The following description uses the face recognition scenario as an example.
[0106] Combining Figures 4 and 5, the caller first obtains S sample face images, performs face detection on the S sample face images, and extracts the face area in each sample face image; then, calls the feature extraction algorithm to extract features of the face area in each sample face image, and obtains the image features of each sample face image (dimension is D); then, calls the interface of the retrieval system to batch send these feature vectors into the vector library of the retrieval system.
[0107] The retrieval system starts the master control node and uses it to perform feature database construction. Specifically, the master control node uniformly randomly samples all vectors in the vector library to obtain a vector subset. Then, it clusters the vectors in the vector subset using a preset clustering algorithm to obtain k cluster center vectors. Then, based on the distance between the vectors in the vector library and the k cluster center vectors, the vectors in the vector library are grouped to obtain vector groups corresponding to each of the k cluster center vectors. Finally, the k cluster center vectors and the vector groups corresponding to each cluster center vector are written to disk for persistent storage.
[0108] When the caller needs to perform vector retrieval on a batch of target face images (a total of M), it can call the feature extraction algorithm to extract features from each target face image, use the extracted image feature vector as the first vector, obtain the first vector group (D×M), and send a retrieval request carrying the first vector to the retrieval system.
[0109] In response to a retrieval request from the caller, the retrieval system performs retrieval preprocessing through the master control node. Specifically, the master control node loads the k cluster center vectors in the vector library from disk into memory. For each first vector, the master control node sequentially retrieves the first n cluster center vectors closest to the first vector from memory and inserts the retrieval vector into the task queue corresponding to each of these n cluster center vectors. It then continues to perform a neighbor search for the next first vector, repeating the above process until all first vectors have been inserted into the corresponding task queue. Furthermore, the master control node loads the vector groups corresponding to the first n cluster center vectors corresponding to each of these first vectors from disk into memory.
[0110] Furthermore, the retrieval system starts the retrieval node and performs the retrieval process through the retrieval node. Specifically, the retrieval node obtains the first T vectors closest to the first vector from the target vector group stored in the memory as candidate vectors of the first vector and returns them to the master node.
[0111] The retrieval system, through the master control node, aggregates, for each first vector, candidate vectors corresponding to the first vector obtained from each vector group stored in the memory to obtain a candidate vector set corresponding to the first vector; then, the first N candidate vectors closest to the first vector are selected from the candidate vector set corresponding to the first vector as the retrieval result and returned to the caller.
[0112] The caller can display the target face image and the sample face images corresponding to the search results corresponding to the first vector in the application for user confirmation. In actual applications, the identity information of the faces in these sample face images, such as name and de-identified ID number, can also be displayed to help users confirm the identity of the face in the target face image.
[0113] The foregoing description of this specification describes specific embodiments. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recited in the claims can be performed in an order different from that described in the embodiments and still achieve the desired results. Furthermore, the processes depicted in the accompanying drawings do not necessarily require the specific order shown or the sequential order to achieve the desired results. In certain embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0114] Corresponding to the vector search method shown in FIG2 , an embodiment of the present application further provides a vector search device. FIG6 is a schematic diagram of a vector search device 600 provided in an embodiment of the present application. The device 600 includes:
[0115] A receiving unit 610, configured to receive a first vector;
[0116] A first acquisition unit 620 is configured to load k cluster center vectors of a vector library from a disk into a memory, and to acquire first n cluster center vectors closest to the first vector from the memory, where k is a positive integer greater than 1 and n is a positive integer less than k, and the k cluster center vectors are obtained by clustering the vectors in the vector library based on a preset clustering algorithm and written to the disk;
[0117] The second acquisition unit 630 is used to load the vector groups corresponding to the first n cluster center vectors from the disk into the memory, and obtain the first N vectors closest to the first vector from the vector groups stored in the memory as retrieval results, wherein N is a positive integer, and the disk stores vector groups corresponding to k cluster center vectors, each vector group is obtained by grouping the vectors in the vector library based on the k cluster center vectors and written to the disk.
[0118] Optionally, when the second obtaining unit 630 obtains the first N vectors closest to the first vector from the vector group stored in the memory as the search result, it performs the following steps:
[0119] Obtaining, from the target vector group stored in the memory, first T vectors closest to the first vector as candidate vectors of the first vector, wherein the target vector group is any one of the vector groups corresponding to each of the first n cluster center vectors, and T is a positive integer not less than N;
[0120] Summarizing the candidate vectors obtained from each vector group stored in the memory to obtain a candidate vector set corresponding to the first vector;
[0121] The first N candidate vectors closest to the first vector are selected from the candidate vector set as search results.
[0122] Optionally, when the receiving unit 610 receives multiple first vectors, the first obtaining unit 620 is further configured to, after obtaining the first n cluster center vectors closest to the first vector from the memory, determine, for each first vector, through a master control node, the task queues corresponding to the first n cluster center vectors closest to the first vector in the memory, to obtain n target task queues corresponding to the first vector, where the memory includes task queues corresponding to k cluster center vectors; and write the first vectors into the n target task queues respectively through the master control node;
[0123] When the second acquisition unit 630 obtains the first T vectors closest to the first vector from the target vector group stored in the memory as candidate vectors for the first vector, it performs the following steps: traversing the first vectors in each of the k task queues through the retrieval node; for each task queue, obtaining, through the retrieval node, the first T vectors closest to the currently traversed first vector from the vector group of cluster center vectors corresponding to the task queue in the memory as candidate vectors for the currently traversed first vector.
[0124] Optionally, the retrieval node includes a first retrieval node;
[0125] When traversing the first vector in each of the k task queues through the search node, the second acquisition unit 630 performs the following steps:
[0126] Start the first search node;
[0127] Polling each task queue in the memory through the first retrieval node;
[0128] If the currently polled task queue is not empty, the first vector in the currently polled task queue is traversed.
[0129] Optionally, the retrieval node includes k second retrieval nodes;
[0130] When traversing the first vector in each of the k task queues through the search node, the second acquisition unit 630 performs the following steps:
[0131] Starting the k second search nodes, each second search node is used to traverse one of the k task queues;
[0132] The first vectors in the task queues corresponding to the k second search nodes are traversed in parallel.
[0133] Optionally, the apparatus 600 further includes:
[0134] a sampling unit, configured to perform uniform random sampling on the vector library to obtain a vector subset before the first acquiring unit 620 loads the k cluster center vectors of the vector library from the disk into the memory;
[0135] A clustering unit, configured to cluster the vectors in the vector subset based on a preset clustering algorithm to obtain k cluster center vectors;
[0136] The grouping unit is configured to divide the vectors in the vector library into vector groups corresponding to the closest cluster center vectors based on the distances between the vectors in the vector library and the k cluster center vectors.
[0137] Optionally, the sampling unit is specifically configured to: divide the vectors in the vector library into a plurality of attribute subsets based on target attributes of the vectors in the vector library, wherein the attribute values of the target attributes of the vectors in the same attribute subset are the same; and for each attribute subset, perform uniform random sampling on the attribute subset to obtain a vector subset under the attribute subset;
[0138] The clustering unit is specifically configured to: for each attribute subset, cluster the vectors contained in the vector subset under the attribute subset based on a preset clustering algorithm to obtain k cluster center vectors under the attribute subset;
[0139] The grouping unit is specifically used to: for each attribute subset, based on the distances between the vectors in the attribute subset and the k cluster center vectors under the attribute subset, divide the vectors in the first attribute subset into vector groups corresponding to the cluster center vectors with the closest distance, and obtain vector groups corresponding to the k cluster center vectors under the first attribute subset.
[0140] Optionally, when loading the k cluster center vectors of the vector library from the disk into the memory, the first acquisition unit 620 performs the following steps: determining a target attribute subset corresponding to the first vector from the multiple attribute subsets; loading the k cluster center vectors under the target attribute subset from the disk into the memory;
[0141] When loading the vector groups corresponding to the first n cluster center vectors from the disk into the memory, the second acquisition unit 630 performs the following steps: loading the vector groups corresponding to the first n cluster center vectors under the target attribute subset from the disk into the memory.
[0142] Optionally, when the second acquisition unit 630 obtains the first N vectors closest to the first vector from the vector group stored in the memory as the retrieval result, it performs the following steps: performing vector quantization processing on the vectors in each vector group stored in the memory; and obtaining the first N vectors closest to the first vector from the vector group after the vector quantization processing as the retrieval result corresponding to the first vector.
[0143] Optionally, the receiving unit is specifically used to: receive a retrieval request sent by a caller based on a target processing task, the retrieval request carries a first vector, the first vector is a feature vector obtained by the caller for feature extraction of a target object, the target object includes at least one of the following objects: image, speech, and text, the vectors in the vector library include feature vectors obtained by feature extraction of multiple sample objects, and the vectors in the vector library are stored on the disk.
[0144] The apparatus 600 further includes:
[0145] A sending unit is configured to return the retrieval result to the caller after the second acquisition unit 630 acquires the first N vectors closest to the first vector from the vector group stored in the memory as a retrieval result, wherein the retrieval result is used for the caller to perform the target processing task on the target object.
[0146] Obviously, the vector retrieval device provided in the embodiment of the present application can serve as the execution body of the vector retrieval method shown in Figure 2. For example, in the vector retrieval method shown in Figure 2, step S202 can be executed by the receiving unit 610 in the vector retrieval device shown in Figure 6, steps S204 and S206 can be executed by the first acquisition unit 620 in the vector retrieval device shown in Figure 6, and steps S208 and S210 can be executed by the second acquisition unit 630 in the vector retrieval device shown in Figure 6.
[0147] According to another embodiment of the present application, the various units in the vector retrieval device shown in Figure 6 can be individually or all combined into one or several other units to form a whole, or one (or some) of the units can be further divided into multiple functionally smaller units to form a whole, which can achieve the same operation without affecting the realization of the technical effects of the embodiments of the present application. The above-mentioned units are divided based on logical functions. In actual applications, the functions of one unit can also be implemented by multiple units, or the functions of multiple units can be implemented by one unit. In other embodiments of the present application, the vector retrieval device can also include other units. In actual applications, these functions can also be implemented with the assistance of other units, and can be implemented by the collaboration of multiple units.
[0148] According to another embodiment of the present application, a vector search device as shown in FIG6 can be constructed, and the vector search method of the embodiment of the present application can be implemented, by running a computer program (including program code) capable of executing each step involved in the corresponding method shown in FIG2 on a general-purpose computing device such as a computer, including processing elements and storage elements such as a central processing unit (CPU), random access memory (RAM), and read-only memory (ROM). The computer program can be recorded on a computer-readable storage medium, for example, and transferred to an electronic device via the computer-readable storage medium and executed therein.
[0149] FIG7 is a schematic diagram of the structure of an electronic device according to an embodiment of the present application. Referring to FIG7 , at the hardware level, the electronic device includes a processor, and optionally also includes an internal bus, a network interface, and a memory. The memory may include a memory, such as a high-speed random access memory (RAM), and may also include a non-volatile memory (non-volatile memory), such as at least one disk storage device. Of course, the electronic device may also include hardware required for other services.
[0150] The processor, network interface, and memory can be interconnected via an internal bus, such as an ISA (Industry Standard Architecture) bus, a PCI (Peripheral Component Interconnect) bus, or an EISA (Extended Industry Standard Architecture) bus. These buses can be classified as address buses, data buses, and control buses. For ease of illustration, FIG7 shows only one bidirectional arrow, but this does not imply that there is only one bus or only one type of bus.
[0151] The memory is used to store programs. Specifically, the program may include program code, which includes computer operating instructions. The memory may include internal memory and non-volatile memory, and provides instructions and data to the processor.
[0152] The processor reads the corresponding computer program from the non-volatile memory into the internal memory and then runs it, forming a vector search device at the logical level. The processor executes the program stored in the memory and is specifically used to perform the following operations:
[0153] receiving a first vector;
[0154] Loading k cluster center vectors of a vector library from a disk into a memory, and obtaining the first n cluster center vectors closest to the first vector from the memory, where k is a positive integer greater than 1 and n is a positive integer less than k, and the k cluster center vectors are obtained by clustering the vectors in the vector library based on a preset clustering algorithm and written to the disk;
[0155] The vector groups corresponding to the first n cluster center vectors are loaded from the disk into the memory, and the first N vectors closest to the first vector are obtained as retrieval results from the vector groups stored in the memory, where N is a positive integer, and the disk stores vector groups corresponding to k cluster center vectors, each vector group being obtained by grouping vectors in the vector library based on the k cluster center vectors and written to the disk.
[0156] The method performed by the vector search device disclosed in the embodiment shown in FIG2 of the present application can be applied to a processor or implemented by a processor. The processor may be an integrated circuit chip with signal processing capabilities. During implementation, each step of the method can be completed by hardware integrated logic circuits in the processor or by software instructions. The processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components. The methods, steps, and logic block diagrams disclosed in the embodiments of the present application can be implemented or executed. The general-purpose processor can be a microprocessor or any conventional processor. The steps of the method disclosed in the embodiments of the present application can be directly implemented and executed by a hardware decoding processor, or by a combination of hardware and software modules in the decoding processor. The software module can be located in a storage medium well-known in the art, such as random access memory, flash memory, read-only memory, programmable read-only memory, electrically erasable programmable memory, registers, etc. The storage medium is located in the memory, and the processor reads the information in the memory and, in conjunction with its hardware, completes the steps of the above method.
[0157] The electronic device can also execute the method of FIG. 2 and realize the functions of the vector search device in the embodiments shown in FIG. 2 to FIG. 5 , and the embodiments of the present application will not be described in detail here.
[0158] Of course, in addition to software implementation, the electronic device of this application does not exclude other implementation methods, such as logic devices or a combination of software and hardware, etc. That is to say, the execution subject of the following processing flow is not limited to each logic unit, but can also be hardware or logic devices.
[0159] The present application also provides a computer-readable storage medium that stores one or more programs. The one or more programs include instructions. When the instructions are executed by an electronic device, the electronic device can execute the method of the embodiment shown in FIG. 2 , and is specifically configured to perform the following operations:
[0160] receiving a first vector;
[0161] Loading k cluster center vectors of a vector library from a disk into a memory, and obtaining the first n cluster center vectors closest to the first vector from the memory, where k is a positive integer greater than 1 and n is a positive integer less than k, and the k cluster center vectors are obtained by clustering the vectors in the vector library based on a preset clustering algorithm and written to the disk;
[0162] The vector groups corresponding to the first n cluster center vectors are loaded from the disk into the memory, and the first N vectors closest to the first vector are obtained as retrieval results from the vector groups stored in the memory, where N is a positive integer, and the disk stores vector groups corresponding to k cluster center vectors, each vector group being obtained by grouping vectors in the vector library based on the k cluster center vectors and written to the disk.
[0163] In short, the above description is only a preferred embodiment of the present application and is not intended to limit the scope of protection of the present application. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principles of the present application shall be included in the scope of protection of the present application.
[0164] The systems, devices, modules, or units described in the above embodiments may be implemented by computer chips or entities, or by products having certain functions. A typical implementation device is a computer. Specifically, the computer may be, for example, a personal computer, a laptop computer, a cellular phone, a camera phone, a smartphone, a personal digital assistant, a media player, a navigation device, an email device, a game console, a tablet computer, a wearable device, or a combination of any of these devices.
[0165] Computer-readable media includes permanent and non-permanent, removable and non-removable media that can be implemented by any method or technology to store information. The information can be computer-readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassettes, magnetic disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer-readable media does not include transitory computer-readable media (transitory media), such as modulated data signals and carrier waves.
[0166] It should also be noted that the terms "comprises," "includes," or any other variations thereof are intended to encompass non-exclusive inclusion, such that a process, method, commodity, or apparatus that includes a series of elements includes not only those elements but also other elements not explicitly listed, or includes elements inherent to such process, method, commodity, or apparatus. In the absence of further limitations, an element defined by the phrase "comprises a ..." does not exclude the presence of other identical elements in the process, method, commodity, or apparatus that includes the element.
[0167] The various embodiments in this specification are described in a progressive manner. Similar parts between the various embodiments can be referred to in conjunction with each other. Each embodiment focuses on the differences between the other embodiments. In particular, the system embodiments are generally similar to the method embodiments, so the description is relatively simple. For relevant parts, refer to the description of the method embodiments.
Claims
1. A vector retrieval method, characterized in that: The method includes: Obtaining n cluster center vectors from the k cluster center vectors according to the distance between the first vector and each of the k cluster center vectors, wherein k is a positive integer greater than 1, and n is a positive integer less than k; and Based on the distance between the first vector and each vector in the vector group corresponding to each of the n cluster center vectors, N vectors are obtained from the vector group corresponding to the n cluster center vectors as search results, where N is a positive integer.
2. The method according to claim 1, characterized in that The acquiring N vectors as search results from the vector groups corresponding to the n cluster center vectors based on the distance between the first vector and each vector in the vector groups corresponding to the n cluster center vectors respectively includes: According to the distance between the first vector and each vector in the target vector group, T vectors are obtained from the target vector group as candidate vectors of the first vector, wherein the target vector group is a vector group corresponding to any one of the n cluster center vectors, and T is a positive integer not less than N; Summarizing the candidate vectors obtained from the vector group corresponding to each cluster center vector of the n cluster center vectors to obtain a candidate vector set corresponding to the first vector; and According to the distance between the first vector and each vector in the candidate vector set, the N candidate vectors are selected from the candidate vector set as the search result.
3. The method according to claim 2, characterized in that In the case where multiple first vectors are received, after obtaining the n cluster center vectors, the method further includes: For each first vector, the main control node determines the task queues corresponding to the n cluster center vectors in the memory, and obtains n target task queues corresponding to the first vector, wherein the memory includes the task queues corresponding to the k cluster center vectors; Writing the first vector into the n target task queues respectively through the master control node; The acquiring T vectors from the target vector group as candidate vectors of the first vector according to the distance between the first vector and each vector in the target vector group includes: Traversing the first vector in each of the k task queues through at least one retrieval node; For each of the k task queues, through the at least one retrieval node, according to the distance between the first vector currently traversed and each vector in the vector group of cluster center vectors corresponding to the task queue, T vectors are obtained from the vector group of cluster center vectors corresponding to the task queue as candidate vectors of the first vector currently traversed.
4. The method according to claim 3, characterized in that The at least one retrieval node includes a first retrieval node; The traversing the first vector in each of the k task queues through at least one retrieval node includes: Starting the first retrieval node; Poll each task queue in the memory through the first retrieval node; If the currently polled task queue is not empty, the first vector in the currently polled task queue is traversed.
5. The method according to claim 3, characterized in that: The at least one retrieval node includes k second retrieval nodes; The traversing the first vector in each of the k task queues through at least one retrieval node includes: Starting the k second retrieval nodes, each of the second retrieval nodes is used to traverse one of the k task queues; The first vectors in the k task queues are traversed in parallel through the k second search nodes.
6. The method according to claim 1, characterized in that The method further comprises: Perform uniform random sampling on the vector library to obtain a vector subset; Clustering the vectors in the vector subset based on a preset clustering algorithm to obtain the k cluster center vectors; Based on the distances between the vectors in the vector library and the k cluster center vectors, the vectors in the vector library are divided into vector groups corresponding to the cluster center vectors with the closest distances.
7. The method according to claim 6, characterized in that The vector library is uniformly randomly sampled to obtain a vector subset, including: Based on target attributes of the vectors in the vector library, the vectors in the vector library are divided into a plurality of attribute subsets, wherein the attribute values of the target attributes of the vectors in the same attribute subset are the same; For each attribute subset, uniformly randomly sample the attribute subset to obtain a vector subset under the attribute subset; The clustering of the vectors in the vector subset based on a preset clustering algorithm to obtain the k cluster center vectors includes: For each attribute subset, cluster the vectors contained in the vector subset under the attribute subset based on a preset clustering algorithm to obtain k cluster center vectors under the attribute subset; The dividing the vectors in the vector library into vector groups corresponding to the closest cluster center vectors based on the distances between the vectors in the vector library and the k cluster center vectors respectively includes: For each attribute subset, based on the distances between the vectors in the attribute subset and the k cluster center vectors under the attribute subset, the vectors in the attribute subset are divided into vector groups corresponding to the cluster center vectors with the closest distance, so as to obtain vector groups corresponding to the k cluster center vectors under the attribute subset.
8. The method according to claim 7, characterized in that The method further includes: Determine a target attribute subset corresponding to the first vector from the multiple attribute subsets; Loading the k cluster center vectors under the target attribute subset; and The vector groups corresponding to the n cluster center vectors among the k cluster center vectors are loaded.
9. The method according to any one of claims 1 to 8, characterized in that The method further includes: receiving a search request sent by a caller based on a target processing task, wherein the search request carries the first vector, and the first vector is a feature vector obtained by the caller for extracting features of a target object, wherein the target object includes at least one of the following objects: an image, a voice, and a text; and After obtaining the retrieval result, the retrieval result is returned to the caller, and the retrieval result is used for the caller to perform the target processing task on the target object.
10. An electronic device, characterized in that: include: processor; and A memory for storing instructions; When the instruction is executed by the processor, the processor performs the following steps: Obtaining n cluster center vectors from the k cluster center vectors according to the distance between the first vector and each of the k cluster center vectors, wherein k is a positive integer greater than 1, and n is a positive integer less than k; and Based on the distance between the first vector and each vector in the vector group corresponding to each of the n cluster center vectors, N vectors are obtained from the vector group corresponding to the n cluster center vectors as search results, where N is a positive integer.
11. The electronic device according to claim 10, characterized in that: The acquiring N vectors as search results from the vector groups corresponding to the n cluster center vectors based on the distance between the first vector and each vector in the vector groups corresponding to the n cluster center vectors respectively includes: According to the distance between the first vector and each vector in the target vector group, T vectors are obtained from the target vector group as candidate vectors of the first vector, wherein the target vector group is the n cluster center vectors The vector group corresponding to any cluster center vector in , T is a positive integer not less than N; Summarizing the candidate vectors obtained from the vector group corresponding to each cluster center vector of the n cluster center vectors to obtain a candidate vector set corresponding to the first vector; and According to the distance between the first vector and each vector in the candidate vector set, the N candidate vectors are selected from the candidate vector set as the search result.
12. The electronic device according to claim 11, characterized in that: In the case of receiving multiple first vectors, after acquiring the n cluster center vectors, the processor further performs the following steps: For each first vector, the main control node determines the task queues corresponding to the n cluster center vectors in the memory, and obtains n target task queues corresponding to the first vector, wherein the memory includes the task queues corresponding to the k cluster center vectors; Writing the first vector into the n target task queues respectively through the master control node; The acquiring T vectors from the target vector group as candidate vectors for the first vector according to the distance between the first vector and each vector in the target vector group includes: Traversing the first vector in each of the k task queues through at least one retrieval node; For each of the k task queues, through the at least one retrieval node, according to the distance between the first vector currently traversed and each vector in the vector group of cluster center vectors corresponding to the task queue, T vectors are obtained from the vector group of cluster center vectors corresponding to the task queue as candidate vectors of the first vector currently traversed.
13. The electronic device according to claim 12, characterized in that: The at least one retrieval node includes a first retrieval node; The traversing the first vector in each of the k task queues through at least one retrieval node includes: Starting the first retrieval node; Poll each task queue in the memory through the first retrieval node; If the currently polled task queue is not empty, the first vector in the currently polled task queue is traversed.
14. The electronic device according to claim 12, characterized in that: The at least one retrieval node includes k second retrieval nodes; The traversing the first vector in each of the k task queues through at least one retrieval node includes: Starting the k second retrieval nodes, each of the second retrieval nodes is used to traverse one of the k task queues; The first vectors in the k task queues are traversed in parallel through the k second search nodes.
15. The electronic device according to claim 10, characterized in that: The processor also performs the following steps: Perform uniform random sampling on the vector library to obtain a vector subset; Clustering the vectors in the vector subset based on a preset clustering algorithm to obtain the k cluster center vectors; Based on the distances between the vectors in the vector library and the k cluster center vectors, the vectors in the vector library are divided into vector groups corresponding to the cluster center vectors with the closest distances.
16. The electronic device according to claim 15, characterized in that: The uniform random sampling of the vector library is performed to obtain a vector subset, and the processor is further caused to perform the following steps: Based on target attributes of the vectors in the vector library, the vectors in the vector library are divided into a plurality of attribute subsets, wherein the attribute values of the target attributes of the vectors in the same attribute subset are the same; For each attribute subset, uniformly randomly sample the attribute subset to obtain a vector subset under the attribute subset; The clustering of the vectors in the vector subset based on a preset clustering algorithm to obtain the k cluster center vectors includes: For each attribute subset, cluster the vectors contained in the vector subset under the attribute subset based on a preset clustering algorithm to obtain k cluster center vectors under the attribute subset; The vectors in the vector library are respectively based on the distances between the k cluster center vectors. The vectors in the library are divided into vector groups corresponding to the nearest cluster center vectors, including: For each attribute subset, based on the distances between the vectors in the attribute subset and the k cluster center vectors under the attribute subset, the vectors in the attribute subset are divided into vector groups corresponding to the cluster center vectors with the closest distance, so as to obtain vector groups corresponding to the k cluster center vectors under the attribute subset.
17. The electronic device according to claim 16, characterized in that: The processor also performs the following steps: Determine a target attribute subset corresponding to the first vector from the multiple attribute subsets; Recording the k cluster center vectors under the target attribute subset; and The vector groups corresponding to the n cluster center vectors among the k cluster center vectors are loaded.
18. The electronic device according to any one of claims 10 to 17, characterized in that: The processor also performs the following steps: receiving a search request sent by a caller based on a target processing task, wherein the search request carries the first vector, and the first vector is a feature vector obtained by the caller for extracting features of a target object, wherein the target object includes at least one of the following objects: an image, a voice, and a text; and After obtaining the retrieval result, the retrieval result is returned to the caller, and the retrieval result is used for the caller to perform the target processing task on the target object.
19. A computer-readable storage medium, characterized in that: When the instructions in the storage medium are executed by a processor of an electronic device, the electronic device executes a vector retrieval method, which includes: Obtaining n cluster center vectors from the k cluster center vectors according to the distance between the first vector and each of the k cluster center vectors, wherein k is a positive integer greater than 1, and n is a positive integer less than k; and Based on the distance between the first vector and each vector in the vector group corresponding to each of the n cluster center vectors, N vectors are obtained from the vector group corresponding to the n cluster center vectors as search results, where N is a positive integer.
20. A vector search device, characterized in that: include: A first acquisition unit, configured to acquire n cluster center vectors from the k cluster center vectors according to a distance between the first vector and each of the k cluster center vectors, wherein k is a positive integer greater than 1, and n is a positive integer less than k; The second acquisition unit is used to acquire N vectors from the vector groups corresponding to the n cluster center vectors as search results based on the distance between the first vector and each vector in the vector groups corresponding to the n cluster center vectors, where N is a positive integer.
Citation Information
Patent Citations
Balanced image clustering method based on hierarchical clustering
CN103049514A
Image retrieval method, system and equipment based on memory management and storage medium
CN113792171A
Distributed image search
US8352494B1
Cited By
Distributed vector diagram retrieval method, electronic equipment and computer program product
CN122548013A
